creating AST from file with custom entry point

Hi all.

Is there a possiblilty to create an AST for C++ source code which has a custom entry point.

To illustrate, the following code expample should be parsed to an AST:

int z = a + b;
while (z < 10) {
x = x * x;
}

There is no function declaration in the file and not all variables are declared. Is there a way to create
an AST for this code so that all lexed tokens are parsed into the AST.

I tried to do this with the “C” interface libclang but there only a complete translation unit can be parsed. When i parse
this code i get a some diagnostic messages and the generated AST is incomplete.
So my question is: is it possible to parse this code in an AST and if so, whats the best way to do this.

Thx in advance
Michael Hochgatterer

One technique that I have used successfully in the past for something similar (not with clang though), is to just wrap the code inside an “environment” that will make it compile, and then extract the AST nodes you want. So in this case, you would wrap a function and some variable declarations around it.

Also, I don’t know much about how it works, but the clang-bot in the IRC channel does do some things that are probably related, but I don’t know enough about how it works to give you any useful advice.

–Sean Silva