IntroductionToTheClangAST.rst 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. =============================
  2. Introduction to the Clang AST
  3. =============================
  4. This document gives a gentle introduction to the mysteries of the Clang
  5. AST. It is targeted at developers who either want to contribute to
  6. Clang, or use tools that work based on Clang's AST, like the AST
  7. matchers.
  8. .. raw:: html
  9. <center><iframe width="560" height="315" src="http://www.youtube.com/embed/VqCkCDFLSsc?vq=hd720" frameborder="0" allowfullscreen></iframe></center>
  10. `Slides <http://llvm.org/devmtg/2013-04/klimek-slides.pdf>`_
  11. Introduction
  12. ============
  13. Clang's AST is different from ASTs produced by some other compilers in
  14. that it closely resembles both the written C++ code and the C++
  15. standard. For example, parenthesis expressions and compile time
  16. constants are available in an unreduced form in the AST. This makes
  17. Clang's AST a good fit for refactoring tools.
  18. Documentation for all Clang AST nodes is available via the generated
  19. `Doxygen <http://clang.llvm.org/doxygen>`_. The doxygen online
  20. documentation is also indexed by your favorite search engine, which will
  21. make a search for clang and the AST node's class name usually turn up
  22. the doxygen of the class you're looking for (for example, search for:
  23. clang ParenExpr).
  24. Examining the AST
  25. =================
  26. A good way to familarize yourself with the Clang AST is to actually look
  27. at it on some simple example code. Clang has a builtin AST-dump mode,
  28. which can be enabled with the flag ``-ast-dump``.
  29. Let's look at a simple example AST:
  30. ::
  31. $ cat test.cc
  32. int f(int x) {
  33. int result = (x / 42);
  34. return result;
  35. }
  36. # Clang by default is a frontend for many tools; -Xclang is used to pass
  37. # options directly to the C++ frontend.
  38. $ clang -Xclang -ast-dump -fsyntax-only test.cc
  39. TranslationUnitDecl 0x5aea0d0 <<invalid sloc>>
  40. ... cutting out internal declarations of clang ...
  41. `-FunctionDecl 0x5aeab50 <test.cc:1:1, line:4:1> f 'int (int)'
  42. |-ParmVarDecl 0x5aeaa90 <line:1:7, col:11> x 'int'
  43. `-CompoundStmt 0x5aead88 <col:14, line:4:1>
  44. |-DeclStmt 0x5aead10 <line:2:3, col:24>
  45. | `-VarDecl 0x5aeac10 <col:3, col:23> result 'int'
  46. | `-ParenExpr 0x5aeacf0 <col:16, col:23> 'int'
  47. | `-BinaryOperator 0x5aeacc8 <col:17, col:21> 'int' '/'
  48. | |-ImplicitCastExpr 0x5aeacb0 <col:17> 'int' <LValueToRValue>
  49. | | `-DeclRefExpr 0x5aeac68 <col:17> 'int' lvalue ParmVar 0x5aeaa90 'x' 'int'
  50. | `-IntegerLiteral 0x5aeac90 <col:21> 'int' 42
  51. `-ReturnStmt 0x5aead68 <line:3:3, col:10>
  52. `-ImplicitCastExpr 0x5aead50 <col:10> 'int' <LValueToRValue>
  53. `-DeclRefExpr 0x5aead28 <col:10> 'int' lvalue Var 0x5aeac10 'result' 'int'
  54. The toplevel declaration in
  55. a translation unit is always the `translation unit
  56. declaration <http://clang.llvm.org/doxygen/classclang_1_1TranslationUnitDecl.html>`_.
  57. In this example, our first user written declaration is the `function
  58. declaration <http://clang.llvm.org/doxygen/classclang_1_1FunctionDecl.html>`_
  59. of "``f``". The body of "``f``" is a `compound
  60. statement <http://clang.llvm.org/doxygen/classclang_1_1CompoundStmt.html>`_,
  61. whose child nodes are a `declaration
  62. statement <http://clang.llvm.org/doxygen/classclang_1_1DeclStmt.html>`_
  63. that declares our result variable, and the `return
  64. statement <http://clang.llvm.org/doxygen/classclang_1_1ReturnStmt.html>`_.
  65. AST Context
  66. ===========
  67. All information about the AST for a translation unit is bundled up in
  68. the class
  69. `ASTContext <http://clang.llvm.org/doxygen/classclang_1_1ASTContext.html>`_.
  70. It allows traversal of the whole translation unit starting from
  71. `getTranslationUnitDecl <http://clang.llvm.org/doxygen/classclang_1_1ASTContext.html#abd909fb01ef10cfd0244832a67b1dd64>`_,
  72. or to access Clang's `table of
  73. identifiers <http://clang.llvm.org/doxygen/classclang_1_1ASTContext.html#a4f95adb9958e22fbe55212ae6482feb4>`_
  74. for the parsed translation unit.
  75. AST Nodes
  76. =========
  77. Clang's AST nodes are modeled on a class hierarchy that does not have a
  78. common ancestor. Instead, there are multiple larger hierarchies for
  79. basic node types like
  80. `Decl <http://clang.llvm.org/doxygen/classclang_1_1Decl.html>`_ and
  81. `Stmt <http://clang.llvm.org/doxygen/classclang_1_1Stmt.html>`_. Many
  82. important AST nodes derive from
  83. `Type <http://clang.llvm.org/doxygen/classclang_1_1Type.html>`_,
  84. `Decl <http://clang.llvm.org/doxygen/classclang_1_1Decl.html>`_,
  85. `DeclContext <http://clang.llvm.org/doxygen/classclang_1_1DeclContext.html>`_
  86. or `Stmt <http://clang.llvm.org/doxygen/classclang_1_1Stmt.html>`_, with
  87. some classes deriving from both Decl and DeclContext.
  88. There are also a multitude of nodes in the AST that are not part of a
  89. larger hierarchy, and are only reachable from specific other nodes, like
  90. `CXXBaseSpecifier <http://clang.llvm.org/doxygen/classclang_1_1CXXBaseSpecifier.html>`_.
  91. Thus, to traverse the full AST, one starts from the
  92. `TranslationUnitDecl <http://clang.llvm.org/doxygen/classclang_1_1TranslationUnitDecl.html>`_
  93. and then recursively traverses everything that can be reached from that
  94. node - this information has to be encoded for each specific node type.
  95. This algorithm is encoded in the
  96. `RecursiveASTVisitor <http://clang.llvm.org/doxygen/classclang_1_1RecursiveASTVisitor.html>`_.
  97. See the `RecursiveASTVisitor
  98. tutorial <http://clang.llvm.org/docs/RAVFrontendAction.html>`_.
  99. The two most basic nodes in the Clang AST are statements
  100. (`Stmt <http://clang.llvm.org/doxygen/classclang_1_1Stmt.html>`_) and
  101. declarations
  102. (`Decl <http://clang.llvm.org/doxygen/classclang_1_1Decl.html>`_). Note
  103. that expressions
  104. (`Expr <http://clang.llvm.org/doxygen/classclang_1_1Expr.html>`_) are
  105. also statements in Clang's AST.