LibASTMatchers.rst 6.1 KB

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