Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make use of Clang's AST?

Tags:

I am looking at making use of the Clang's AST for my C code and do some analysis over the AST. Some pointers on where to start, how to obtain the Clang's AST, tutorials or anything in this regard will be of great help!!!

I have been trying to find some and I got this link which was created 2 years back. But for some reason, it is not working for me. The sample code, in the tutorial, gives me too many errors. So I am not sure, if I build the code properly or some thing is wrong with the tutorial. But I would be happy to start from some other page as well.

like image 927
bsoundra Avatar asked Feb 27 '11 01:02

bsoundra


People also ask

What is AST dump?

The ast. dump() method returns a formatted string of the tree structure in a tree. The visit method available to the visitor object visits all the nodes in the tree structure.

What is AST in LLVM?

2.2. The Abstract Syntax Tree (AST) The AST for a program captures its behavior in such a way that it is easy for later stages of the compiler (e.g. code generation) to interpret. We basically want one object for each construct in the language, and the AST should closely model the language.

What is C++ AST?

An AST is essentially the same thing, a tree-like diagram of the meaningful content of a program. The part of the compiler responsible for producing the AST is called the front-end. That's where all the grammatical rules of C++ are interpreted and applied to the incoming source code.


2 Answers

Start with the tutorial linked by sharth. Then go through Clang's Doxygen. Start with SemaConsumer.

Read a lot of source code. Clang is a moving target. If you are writing tools based on clang, then you need to recognize that clang is adding and fixing features daily, so you should be prepared to read a lot of code!

like image 51
Sohail Avatar answered Sep 20 '22 15:09

Sohail


You probably want the stable C API provided in the libclang library, as opposed to the unstable C++ internal APIs that others have mentioned.

The best documentation to start with currently is the video/slides of the talk, "libclang: Thinking Beyond the Compiler" available on the LLVM Developers Meeting website.

However, do note that the stability of the API comes at a cost of comprehensiveness. You won't be able to do everything with this API, but it is much easier to use.

like image 23
exclipy Avatar answered Sep 20 '22 15:09

exclipy