Search code examples
c++ccompiler-constructionclangllvm

Using LLVM to traverse AST


Are there any helper methods to traverse the AST, basic blocks etc. generated by LLVM compiler for a C code ?


Solution

  • If you're trying to load a module (from a .bc file compiled from a .c file by clang -emit-llvm) and traverse its functions, basic blocks, etc., then you might want to start with the llvm::Module class. It has functions for iterating through global variables and functions. Then the llvm::Function class has functions for iterating through basic blocks. Then the llvm::BasicBlock class has functions for iterating through instructions.

    Or if you'd prefer, you can traverse the AST structure created by Clang. Here's some example code: http://eli.thegreenplace.net/2012/06/08/basic-source-to-source-transformation-with-clang/.