1*f4a2713aSLionel Sambuc====================== 2*f4a2713aSLionel SambucMatching the Clang AST 3*f4a2713aSLionel Sambuc====================== 4*f4a2713aSLionel Sambuc 5*f4a2713aSLionel SambucThis document explains how to use Clang's LibASTMatchers to match interesting 6*f4a2713aSLionel Sambucnodes of the AST and execute code that uses the matched nodes. Combined with 7*f4a2713aSLionel Sambuc:doc:`LibTooling`, LibASTMatchers helps to write code-to-code transformation 8*f4a2713aSLionel Sambuctools or query tools. 9*f4a2713aSLionel Sambuc 10*f4a2713aSLionel SambucWe assume basic knowledge about the Clang AST. See the :doc:`Introduction 11*f4a2713aSLionel Sambucto the Clang AST <IntroductionToTheClangAST>` if you want to learn more 12*f4a2713aSLionel Sambucabout how the AST is structured. 13*f4a2713aSLionel Sambuc 14*f4a2713aSLionel Sambuc.. FIXME: create tutorial and link to the tutorial 15*f4a2713aSLionel Sambuc 16*f4a2713aSLionel SambucIntroduction 17*f4a2713aSLionel Sambuc------------ 18*f4a2713aSLionel Sambuc 19*f4a2713aSLionel SambucLibASTMatchers provides a domain specific language to create predicates on 20*f4a2713aSLionel SambucClang's AST. This DSL is written in and can be used from C++, allowing users 21*f4a2713aSLionel Sambucto write a single program to both match AST nodes and access the node's C++ 22*f4a2713aSLionel Sambucinterface to extract attributes, source locations, or any other information 23*f4a2713aSLionel Sambucprovided on the AST level. 24*f4a2713aSLionel Sambuc 25*f4a2713aSLionel SambucAST matchers are predicates on nodes in the AST. Matchers are created by 26*f4a2713aSLionel Sambuccalling creator functions that allow building up a tree of matchers, where 27*f4a2713aSLionel Sambucinner matchers are used to make the match more specific. 28*f4a2713aSLionel Sambuc 29*f4a2713aSLionel SambucFor example, to create a matcher that matches all class or union declarations 30*f4a2713aSLionel Sambucin the AST of a translation unit, you can call `recordDecl() 31*f4a2713aSLionel Sambuc<LibASTMatchersReference.html#recordDecl0Anchor>`_. To narrow the match down, 32*f4a2713aSLionel Sambucfor example to find all class or union declarations with the name "``Foo``", 33*f4a2713aSLionel Sambucinsert a `hasName <LibASTMatchersReference.html#hasName0Anchor>`_ matcher: the 34*f4a2713aSLionel Sambuccall ``recordDecl(hasName("Foo"))`` returns a matcher that matches classes or 35*f4a2713aSLionel Sambucunions that are named "``Foo``", in any namespace. By default, matchers that 36*f4a2713aSLionel Sambucaccept multiple inner matchers use an implicit `allOf() 37*f4a2713aSLionel Sambuc<LibASTMatchersReference.html#allOf0Anchor>`_. This allows further narrowing 38*f4a2713aSLionel Sambucdown the match, for example to match all classes that are derived from 39*f4a2713aSLionel Sambuc"``Bar``": ``recordDecl(hasName("Foo"), isDerivedFrom("Bar"))``. 40*f4a2713aSLionel Sambuc 41*f4a2713aSLionel SambucHow to create a matcher 42*f4a2713aSLionel Sambuc----------------------- 43*f4a2713aSLionel Sambuc 44*f4a2713aSLionel SambucWith more than a thousand classes in the Clang AST, one can quickly get lost 45*f4a2713aSLionel Sambucwhen trying to figure out how to create a matcher for a specific pattern. This 46*f4a2713aSLionel Sambucsection will teach you how to use a rigorous step-by-step pattern to build the 47*f4a2713aSLionel Sambucmatcher you are interested in. Note that there will always be matchers missing 48*f4a2713aSLionel Sambucfor some part of the AST. See the section about :ref:`how to write your own 49*f4a2713aSLionel SambucAST matchers <astmatchers-writing>` later in this document. 50*f4a2713aSLionel Sambuc 51*f4a2713aSLionel Sambuc.. FIXME: why is it linking back to the same section?! 52*f4a2713aSLionel Sambuc 53*f4a2713aSLionel SambucThe precondition to using the matchers is to understand how the AST for what you 54*f4a2713aSLionel Sambucwant to match looks like. The 55*f4a2713aSLionel Sambuc:doc:`Introduction to the Clang AST <IntroductionToTheClangAST>` teaches you 56*f4a2713aSLionel Sambuchow to dump a translation unit's AST into a human readable format. 57*f4a2713aSLionel Sambuc 58*f4a2713aSLionel Sambuc.. FIXME: Introduce link to ASTMatchersTutorial.html 59*f4a2713aSLionel Sambuc.. FIXME: Introduce link to ASTMatchersCookbook.html 60*f4a2713aSLionel Sambuc 61*f4a2713aSLionel SambucIn general, the strategy to create the right matchers is: 62*f4a2713aSLionel Sambuc 63*f4a2713aSLionel Sambuc#. Find the outermost class in Clang's AST you want to match. 64*f4a2713aSLionel Sambuc#. Look at the `AST Matcher Reference <LibASTMatchersReference.html>`_ for 65*f4a2713aSLionel Sambuc matchers that either match the node you're interested in or narrow down 66*f4a2713aSLionel Sambuc attributes on the node. 67*f4a2713aSLionel Sambuc#. Create your outer match expression. Verify that it works as expected. 68*f4a2713aSLionel Sambuc#. Examine the matchers for what the next inner node you want to match is. 69*f4a2713aSLionel Sambuc#. Repeat until the matcher is finished. 70*f4a2713aSLionel Sambuc 71*f4a2713aSLionel Sambuc.. _astmatchers-bind: 72*f4a2713aSLionel Sambuc 73*f4a2713aSLionel SambucBinding nodes in match expressions 74*f4a2713aSLionel Sambuc---------------------------------- 75*f4a2713aSLionel Sambuc 76*f4a2713aSLionel SambucMatcher expressions allow you to specify which parts of the AST are interesting 77*f4a2713aSLionel Sambucfor a certain task. Often you will want to then do something with the nodes 78*f4a2713aSLionel Sambucthat were matched, like building source code transformations. 79*f4a2713aSLionel Sambuc 80*f4a2713aSLionel SambucTo that end, matchers that match specific AST nodes (so called node matchers) 81*f4a2713aSLionel Sambucare bindable; for example, ``recordDecl(hasName("MyClass")).bind("id")`` will 82*f4a2713aSLionel Sambucbind the matched ``recordDecl`` node to the string "``id``", to be later 83*f4a2713aSLionel Sambucretrieved in the `match callback 84*f4a2713aSLionel Sambuc<http://clang.llvm.org/doxygen/classclang_1_1ast__matchers_1_1MatchFinder_1_1MatchCallback.html>`_. 85*f4a2713aSLionel Sambuc 86*f4a2713aSLionel Sambuc.. FIXME: Introduce link to ASTMatchersTutorial.html 87*f4a2713aSLionel Sambuc.. FIXME: Introduce link to ASTMatchersCookbook.html 88*f4a2713aSLionel Sambuc 89*f4a2713aSLionel SambucWriting your own matchers 90*f4a2713aSLionel Sambuc------------------------- 91*f4a2713aSLionel Sambuc 92*f4a2713aSLionel SambucThere are multiple different ways to define a matcher, depending on its type 93*f4a2713aSLionel Sambucand flexibility. 94*f4a2713aSLionel Sambuc 95*f4a2713aSLionel Sambuc``VariadicDynCastAllOfMatcher<Base, Derived>`` 96*f4a2713aSLionel Sambuc^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 97*f4a2713aSLionel Sambuc 98*f4a2713aSLionel SambucThose match all nodes of type *Base* if they can be dynamically casted to 99*f4a2713aSLionel Sambuc*Derived*. The names of those matchers are nouns, which closely resemble 100*f4a2713aSLionel Sambuc*Derived*. ``VariadicDynCastAllOfMatchers`` are the backbone of the matcher 101*f4a2713aSLionel Sambuchierarchy. Most often, your match expression will start with one of them, and 102*f4a2713aSLionel Sambucyou can :ref:`bind <astmatchers-bind>` the node they represent to ids for later 103*f4a2713aSLionel Sambucprocessing. 104*f4a2713aSLionel Sambuc 105*f4a2713aSLionel Sambuc``VariadicDynCastAllOfMatchers`` are callable classes that model variadic 106*f4a2713aSLionel Sambuctemplate functions in C++03. They take an aribtrary number of 107*f4a2713aSLionel Sambuc``Matcher<Derived>`` and return a ``Matcher<Base>``. 108*f4a2713aSLionel Sambuc 109*f4a2713aSLionel Sambuc``AST_MATCHER_P(Type, Name, ParamType, Param)`` 110*f4a2713aSLionel Sambuc^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 111*f4a2713aSLionel Sambuc 112*f4a2713aSLionel SambucMost matcher definitions use the matcher creation macros. Those define both 113*f4a2713aSLionel Sambucthe matcher of type ``Matcher<Type>`` itself, and a matcher-creation function 114*f4a2713aSLionel Sambucnamed *Name* that takes a parameter of type *ParamType* and returns the 115*f4a2713aSLionel Sambuccorresponding matcher. 116*f4a2713aSLionel Sambuc 117*f4a2713aSLionel SambucThere are multiple matcher definition macros that deal with polymorphic return 118*f4a2713aSLionel Sambucvalues and different parameter counts. See `ASTMatchersMacros.h 119*f4a2713aSLionel Sambuc<http://clang.llvm.org/doxygen/ASTMatchersMacros_8h.html>`_. 120*f4a2713aSLionel Sambuc 121*f4a2713aSLionel Sambuc.. _astmatchers-writing: 122*f4a2713aSLionel Sambuc 123*f4a2713aSLionel SambucMatcher creation functions 124*f4a2713aSLionel Sambuc^^^^^^^^^^^^^^^^^^^^^^^^^^ 125*f4a2713aSLionel Sambuc 126*f4a2713aSLionel SambucMatchers are generated by nesting calls to matcher creation functions. Most of 127*f4a2713aSLionel Sambucthe time those functions are either created by using 128*f4a2713aSLionel Sambuc``VariadicDynCastAllOfMatcher`` or the matcher creation macros (see below). 129*f4a2713aSLionel SambucThe free-standing functions are an indication that this matcher is just a 130*f4a2713aSLionel Sambuccombination of other matchers, as is for example the case with `callee 131*f4a2713aSLionel Sambuc<LibASTMatchersReference.html#callee1Anchor>`_. 132*f4a2713aSLionel Sambuc 133*f4a2713aSLionel Sambuc.. FIXME: "... macros (see below)" --- there isn't anything below 134*f4a2713aSLionel Sambuc 135