xref: /aosp_15_r20/external/clang/docs/LibASTMatchers.rst (revision 67e74705e28f6214e480b399dd47ea732279e315)
1*67e74705SXin Li======================
2*67e74705SXin LiMatching the Clang AST
3*67e74705SXin Li======================
4*67e74705SXin Li
5*67e74705SXin LiThis document explains how to use Clang's LibASTMatchers to match interesting
6*67e74705SXin Linodes of the AST and execute code that uses the matched nodes.  Combined with
7*67e74705SXin Li:doc:`LibTooling`, LibASTMatchers helps to write code-to-code transformation
8*67e74705SXin Litools or query tools.
9*67e74705SXin Li
10*67e74705SXin LiWe assume basic knowledge about the Clang AST.  See the :doc:`Introduction
11*67e74705SXin Lito the Clang AST <IntroductionToTheClangAST>` if you want to learn more
12*67e74705SXin Liabout how the AST is structured.
13*67e74705SXin Li
14*67e74705SXin Li..  FIXME: create tutorial and link to the tutorial
15*67e74705SXin Li
16*67e74705SXin LiIntroduction
17*67e74705SXin Li------------
18*67e74705SXin Li
19*67e74705SXin LiLibASTMatchers provides a domain specific language to create predicates on
20*67e74705SXin LiClang's AST.  This DSL is written in and can be used from C++, allowing users
21*67e74705SXin Lito write a single program to both match AST nodes and access the node's C++
22*67e74705SXin Liinterface to extract attributes, source locations, or any other information
23*67e74705SXin Liprovided on the AST level.
24*67e74705SXin Li
25*67e74705SXin LiAST matchers are predicates on nodes in the AST.  Matchers are created by
26*67e74705SXin Licalling creator functions that allow building up a tree of matchers, where
27*67e74705SXin Liinner matchers are used to make the match more specific.
28*67e74705SXin Li
29*67e74705SXin LiFor example, to create a matcher that matches all class or union declarations
30*67e74705SXin Liin the AST of a translation unit, you can call `recordDecl()
31*67e74705SXin Li<LibASTMatchersReference.html#recordDecl0Anchor>`_.  To narrow the match down,
32*67e74705SXin Lifor example to find all class or union declarations with the name "``Foo``",
33*67e74705SXin Liinsert a `hasName <LibASTMatchersReference.html#hasName0Anchor>`_ matcher: the
34*67e74705SXin Licall ``recordDecl(hasName("Foo"))`` returns a matcher that matches classes or
35*67e74705SXin Liunions that are named "``Foo``", in any namespace.  By default, matchers that
36*67e74705SXin Liaccept multiple inner matchers use an implicit `allOf()
37*67e74705SXin Li<LibASTMatchersReference.html#allOf0Anchor>`_.  This allows further narrowing
38*67e74705SXin Lidown the match, for example to match all classes that are derived from
39*67e74705SXin Li"``Bar``": ``recordDecl(hasName("Foo"), isDerivedFrom("Bar"))``.
40*67e74705SXin Li
41*67e74705SXin LiHow to create a matcher
42*67e74705SXin Li-----------------------
43*67e74705SXin Li
44*67e74705SXin LiWith more than a thousand classes in the Clang AST, one can quickly get lost
45*67e74705SXin Liwhen trying to figure out how to create a matcher for a specific pattern.  This
46*67e74705SXin Lisection will teach you how to use a rigorous step-by-step pattern to build the
47*67e74705SXin Limatcher you are interested in.  Note that there will always be matchers missing
48*67e74705SXin Lifor some part of the AST.  See the section about :ref:`how to write your own
49*67e74705SXin LiAST matchers <astmatchers-writing>` later in this document.
50*67e74705SXin Li
51*67e74705SXin Li..  FIXME: why is it linking back to the same section?!
52*67e74705SXin Li
53*67e74705SXin LiThe precondition to using the matchers is to understand how the AST for what you
54*67e74705SXin Liwant to match looks like.  The
55*67e74705SXin Li:doc:`Introduction to the Clang AST <IntroductionToTheClangAST>` teaches you
56*67e74705SXin Lihow to dump a translation unit's AST into a human readable format.
57*67e74705SXin Li
58*67e74705SXin Li..  FIXME: Introduce link to ASTMatchersTutorial.html
59*67e74705SXin Li..  FIXME: Introduce link to ASTMatchersCookbook.html
60*67e74705SXin Li
61*67e74705SXin LiIn general, the strategy to create the right matchers is:
62*67e74705SXin Li
63*67e74705SXin Li#. Find the outermost class in Clang's AST you want to match.
64*67e74705SXin Li#. Look at the `AST Matcher Reference <LibASTMatchersReference.html>`_ for
65*67e74705SXin Li   matchers that either match the node you're interested in or narrow down
66*67e74705SXin Li   attributes on the node.
67*67e74705SXin Li#. Create your outer match expression.  Verify that it works as expected.
68*67e74705SXin Li#. Examine the matchers for what the next inner node you want to match is.
69*67e74705SXin Li#. Repeat until the matcher is finished.
70*67e74705SXin Li
71*67e74705SXin Li.. _astmatchers-bind:
72*67e74705SXin Li
73*67e74705SXin LiBinding nodes in match expressions
74*67e74705SXin Li----------------------------------
75*67e74705SXin Li
76*67e74705SXin LiMatcher expressions allow you to specify which parts of the AST are interesting
77*67e74705SXin Lifor a certain task.  Often you will want to then do something with the nodes
78*67e74705SXin Lithat were matched, like building source code transformations.
79*67e74705SXin Li
80*67e74705SXin LiTo that end, matchers that match specific AST nodes (so called node matchers)
81*67e74705SXin Liare bindable; for example, ``recordDecl(hasName("MyClass")).bind("id")`` will
82*67e74705SXin Libind the matched ``recordDecl`` node to the string "``id``", to be later
83*67e74705SXin Liretrieved in the `match callback
84*67e74705SXin Li<http://clang.llvm.org/doxygen/classclang_1_1ast__matchers_1_1MatchFinder_1_1MatchCallback.html>`_.
85*67e74705SXin Li
86*67e74705SXin Li..  FIXME: Introduce link to ASTMatchersTutorial.html
87*67e74705SXin Li..  FIXME: Introduce link to ASTMatchersCookbook.html
88*67e74705SXin Li
89*67e74705SXin LiWriting your own matchers
90*67e74705SXin Li-------------------------
91*67e74705SXin Li
92*67e74705SXin LiThere are multiple different ways to define a matcher, depending on its type
93*67e74705SXin Liand flexibility.
94*67e74705SXin Li
95*67e74705SXin Li``VariadicDynCastAllOfMatcher<Base, Derived>``
96*67e74705SXin Li^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
97*67e74705SXin Li
98*67e74705SXin LiThose match all nodes of type *Base* if they can be dynamically casted to
99*67e74705SXin Li*Derived*.  The names of those matchers are nouns, which closely resemble
100*67e74705SXin Li*Derived*.  ``VariadicDynCastAllOfMatchers`` are the backbone of the matcher
101*67e74705SXin Lihierarchy.  Most often, your match expression will start with one of them, and
102*67e74705SXin Liyou can :ref:`bind <astmatchers-bind>` the node they represent to ids for later
103*67e74705SXin Liprocessing.
104*67e74705SXin Li
105*67e74705SXin Li``VariadicDynCastAllOfMatchers`` are callable classes that model variadic
106*67e74705SXin Litemplate functions in C++03.  They take an aribtrary number of
107*67e74705SXin Li``Matcher<Derived>`` and return a ``Matcher<Base>``.
108*67e74705SXin Li
109*67e74705SXin Li``AST_MATCHER_P(Type, Name, ParamType, Param)``
110*67e74705SXin Li^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
111*67e74705SXin Li
112*67e74705SXin LiMost matcher definitions use the matcher creation macros.  Those define both
113*67e74705SXin Lithe matcher of type ``Matcher<Type>`` itself, and a matcher-creation function
114*67e74705SXin Linamed *Name* that takes a parameter of type *ParamType* and returns the
115*67e74705SXin Licorresponding matcher.
116*67e74705SXin Li
117*67e74705SXin LiThere are multiple matcher definition macros that deal with polymorphic return
118*67e74705SXin Livalues and different parameter counts.  See `ASTMatchersMacros.h
119*67e74705SXin Li<http://clang.llvm.org/doxygen/ASTMatchersMacros_8h.html>`_.
120*67e74705SXin Li
121*67e74705SXin Li.. _astmatchers-writing:
122*67e74705SXin Li
123*67e74705SXin LiMatcher creation functions
124*67e74705SXin Li^^^^^^^^^^^^^^^^^^^^^^^^^^
125*67e74705SXin Li
126*67e74705SXin LiMatchers are generated by nesting calls to matcher creation functions.  Most of
127*67e74705SXin Lithe time those functions are either created by using
128*67e74705SXin Li``VariadicDynCastAllOfMatcher`` or the matcher creation macros (see below).
129*67e74705SXin LiThe free-standing functions are an indication that this matcher is just a
130*67e74705SXin Licombination of other matchers, as is for example the case with `callee
131*67e74705SXin Li<LibASTMatchersReference.html#callee1Anchor>`_.
132*67e74705SXin Li
133*67e74705SXin Li..  FIXME: "... macros (see below)" --- there isn't anything below
134*67e74705SXin Li
135