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