1709c44d1SSean Silva====================== 2709c44d1SSean SilvaMatching the Clang AST 3709c44d1SSean Silva====================== 4709c44d1SSean Silva 5709c44d1SSean SilvaThis document explains how to use Clang's LibASTMatchers to match interesting 6709c44d1SSean Silvanodes of the AST and execute code that uses the matched nodes. Combined with 7709c44d1SSean Silva:doc:`LibTooling`, LibASTMatchers helps to write code-to-code transformation 8709c44d1SSean Silvatools or query tools. 9709c44d1SSean Silva 10173d2526SSean SilvaWe assume basic knowledge about the Clang AST. See the :doc:`Introduction 11173d2526SSean Silvato the Clang AST <IntroductionToTheClangAST>` if you want to learn more 12173d2526SSean Silvaabout how the AST is structured. 13709c44d1SSean Silva 14709c44d1SSean Silva.. FIXME: create tutorial and link to the tutorial 15709c44d1SSean Silva 16709c44d1SSean SilvaIntroduction 17709c44d1SSean Silva------------ 18709c44d1SSean Silva 19709c44d1SSean SilvaLibASTMatchers provides a domain specific language to create predicates on 20709c44d1SSean SilvaClang's AST. This DSL is written in and can be used from C++, allowing users 21709c44d1SSean Silvato write a single program to both match AST nodes and access the node's C++ 22709c44d1SSean Silvainterface to extract attributes, source locations, or any other information 23709c44d1SSean Silvaprovided on the AST level. 24709c44d1SSean Silva 25709c44d1SSean SilvaAST matchers are predicates on nodes in the AST. Matchers are created by 26709c44d1SSean Silvacalling creator functions that allow building up a tree of matchers, where 27709c44d1SSean Silvainner matchers are used to make the match more specific. 28709c44d1SSean Silva 29709c44d1SSean SilvaFor example, to create a matcher that matches all class or union declarations 30709c44d1SSean Silvain the AST of a translation unit, you can call `recordDecl() 31709c44d1SSean Silva<LibASTMatchersReference.html#recordDecl0Anchor>`_. To narrow the match down, 32709c44d1SSean Silvafor example to find all class or union declarations with the name "``Foo``", 33709c44d1SSean Silvainsert a `hasName <LibASTMatchersReference.html#hasName0Anchor>`_ matcher: the 34709c44d1SSean Silvacall ``recordDecl(hasName("Foo"))`` returns a matcher that matches classes or 35709c44d1SSean Silvaunions that are named "``Foo``", in any namespace. By default, matchers that 36709c44d1SSean Silvaaccept multiple inner matchers use an implicit `allOf() 37709c44d1SSean Silva<LibASTMatchersReference.html#allOf0Anchor>`_. This allows further narrowing 38709c44d1SSean Silvadown the match, for example to match all classes that are derived from 39709c44d1SSean Silva"``Bar``": ``recordDecl(hasName("Foo"), isDerivedFrom("Bar"))``. 40709c44d1SSean Silva 41709c44d1SSean SilvaHow to create a matcher 42709c44d1SSean Silva----------------------- 43709c44d1SSean Silva 44709c44d1SSean SilvaWith more than a thousand classes in the Clang AST, one can quickly get lost 45709c44d1SSean Silvawhen trying to figure out how to create a matcher for a specific pattern. This 46709c44d1SSean Silvasection will teach you how to use a rigorous step-by-step pattern to build the 47709c44d1SSean Silvamatcher you are interested in. Note that there will always be matchers missing 48709c44d1SSean Silvafor some part of the AST. See the section about :ref:`how to write your own 49709c44d1SSean SilvaAST matchers <astmatchers-writing>` later in this document. 50709c44d1SSean Silva 51709c44d1SSean Silva.. FIXME: why is it linking back to the same section?! 52709c44d1SSean Silva 53709c44d1SSean SilvaThe precondition to using the matchers is to understand how the AST for what you 54709c44d1SSean Silvawant to match looks like. The 55173d2526SSean Silva:doc:`Introduction to the Clang AST <IntroductionToTheClangAST>` teaches you 56709c44d1SSean Silvahow to dump a translation unit's AST into a human readable format. 57709c44d1SSean Silva 58709c44d1SSean Silva.. FIXME: Introduce link to ASTMatchersTutorial.html 59709c44d1SSean Silva.. FIXME: Introduce link to ASTMatchersCookbook.html 60709c44d1SSean Silva 61709c44d1SSean SilvaIn general, the strategy to create the right matchers is: 62709c44d1SSean Silva 63709c44d1SSean Silva#. Find the outermost class in Clang's AST you want to match. 64709c44d1SSean Silva#. Look at the `AST Matcher Reference <LibASTMatchersReference.html>`_ for 65709c44d1SSean Silva matchers that either match the node you're interested in or narrow down 66709c44d1SSean Silva attributes on the node. 67709c44d1SSean Silva#. Create your outer match expression. Verify that it works as expected. 68709c44d1SSean Silva#. Examine the matchers for what the next inner node you want to match is. 69709c44d1SSean Silva#. Repeat until the matcher is finished. 70709c44d1SSean Silva 71709c44d1SSean Silva.. _astmatchers-bind: 72709c44d1SSean Silva 73709c44d1SSean SilvaBinding nodes in match expressions 74709c44d1SSean Silva---------------------------------- 75709c44d1SSean Silva 76709c44d1SSean SilvaMatcher expressions allow you to specify which parts of the AST are interesting 77709c44d1SSean Silvafor a certain task. Often you will want to then do something with the nodes 78709c44d1SSean Silvathat were matched, like building source code transformations. 79709c44d1SSean Silva 80709c44d1SSean SilvaTo that end, matchers that match specific AST nodes (so called node matchers) 81709c44d1SSean Silvaare bindable; for example, ``recordDecl(hasName("MyClass")).bind("id")`` will 82709c44d1SSean Silvabind the matched ``recordDecl`` node to the string "``id``", to be later 83709c44d1SSean Silvaretrieved in the `match callback 84*bc5c3f57SSylvestre Ledru<https://clang.llvm.org/doxygen/classclang_1_1ast__matchers_1_1MatchFinder_1_1MatchCallback.html>`_. 85709c44d1SSean Silva 86709c44d1SSean Silva.. FIXME: Introduce link to ASTMatchersTutorial.html 87709c44d1SSean Silva.. FIXME: Introduce link to ASTMatchersCookbook.html 88709c44d1SSean Silva 89709c44d1SSean SilvaWriting your own matchers 90709c44d1SSean Silva------------------------- 91709c44d1SSean Silva 92709c44d1SSean SilvaThere are multiple different ways to define a matcher, depending on its type 93709c44d1SSean Silvaand flexibility. 94709c44d1SSean Silva 95709c44d1SSean Silva``VariadicDynCastAllOfMatcher<Base, Derived>`` 96709c44d1SSean Silva^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 97709c44d1SSean Silva 98709c44d1SSean SilvaThose match all nodes of type *Base* if they can be dynamically casted to 99709c44d1SSean Silva*Derived*. The names of those matchers are nouns, which closely resemble 100709c44d1SSean Silva*Derived*. ``VariadicDynCastAllOfMatchers`` are the backbone of the matcher 101709c44d1SSean Silvahierarchy. Most often, your match expression will start with one of them, and 102709c44d1SSean Silvayou can :ref:`bind <astmatchers-bind>` the node they represent to ids for later 103709c44d1SSean Silvaprocessing. 104709c44d1SSean Silva 105709c44d1SSean Silva``VariadicDynCastAllOfMatchers`` are callable classes that model variadic 106f947f391SJames Dennetttemplate functions in C++03. They take an arbitrary number of 107709c44d1SSean Silva``Matcher<Derived>`` and return a ``Matcher<Base>``. 108709c44d1SSean Silva 109709c44d1SSean Silva``AST_MATCHER_P(Type, Name, ParamType, Param)`` 110709c44d1SSean Silva^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 111709c44d1SSean Silva 112709c44d1SSean SilvaMost matcher definitions use the matcher creation macros. Those define both 113709c44d1SSean Silvathe matcher of type ``Matcher<Type>`` itself, and a matcher-creation function 114709c44d1SSean Silvanamed *Name* that takes a parameter of type *ParamType* and returns the 115709c44d1SSean Silvacorresponding matcher. 116709c44d1SSean Silva 117709c44d1SSean SilvaThere are multiple matcher definition macros that deal with polymorphic return 118709c44d1SSean Silvavalues and different parameter counts. See `ASTMatchersMacros.h 119*bc5c3f57SSylvestre Ledru<https://clang.llvm.org/doxygen/ASTMatchersMacros_8h.html>`_. 120709c44d1SSean Silva 121709c44d1SSean Silva.. _astmatchers-writing: 122709c44d1SSean Silva 123709c44d1SSean SilvaMatcher creation functions 124709c44d1SSean Silva^^^^^^^^^^^^^^^^^^^^^^^^^^ 125709c44d1SSean Silva 126709c44d1SSean SilvaMatchers are generated by nesting calls to matcher creation functions. Most of 127709c44d1SSean Silvathe time those functions are either created by using 128709c44d1SSean Silva``VariadicDynCastAllOfMatcher`` or the matcher creation macros (see below). 129709c44d1SSean SilvaThe free-standing functions are an indication that this matcher is just a 130709c44d1SSean Silvacombination of other matchers, as is for example the case with `callee 131709c44d1SSean Silva<LibASTMatchersReference.html#callee1Anchor>`_. 132709c44d1SSean Silva 133709c44d1SSean Silva.. FIXME: "... macros (see below)" --- there isn't anything below 134709c44d1SSean Silva 135