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