krammer
krammer

Reputation: 2668

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 ?

Upvotes: 2

Views: 1475

Answers (2)

Yushan
Yushan

Reputation: 633

Basically, it is impossible to do full operations on the AST in LLVM. Because the LLVM pass works on bitcode level not on the AST. I think what you want is an AST iterator.

You could refer to Chapter 3 in Artem Degrachev: Clang Static Analyzer: A Checker Developer's Guide.

Clang now have a page for checker developers. You could find more following the link.

Upvotes: 0

jlstrecker
jlstrecker

Reputation: 5033

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/.

Upvotes: 1

Related Questions