1 minute read

https://youtu.be/VqCkCDFLSsc

Tutorial video about using libclang. ~30min from 2013.

Libclang
Library to access clang's AST for source code. Contains utility functions to simplify workflow and apply changes.

The AST is not a tree but rather a graph (there are methods which connect nodes that are not directly children of each other).

Core classes: Decl, Stmt, Type

Glue classes

AST is complicated. There are multiple ways to go from one node to another.

Visiting nodes is complicated as there is no general base class. Use RecursiveASTVisitor (macro monster which outputs horrendous error messages in case of failure).

AST matchers
Supposedly really useful!

Tools
Make use of clang debug tools:

  • clang -Xclang -ast-dump main.cpp
  • clang-check main.cpp -ast-list | grep foo
  • clang-check main.cpp -ast-dump-filter foo | less -R
  • clang-check main.cpp -ast-dump -ast-dump-filter foo --