[Libtooling] Parse file with previous AST

Hello,

I’m writing a some tool for doing static analysis on C++ code.

My tool has obviously two inputs: the file(s) to check (let’s say f.cpp) and the property to check .

Currently (I’m in a early stage of development), I want to describe the property directly in C++ (let’s say prop.cpp).
and use clang/libtooling to read it .The thing is that the property will use symbols that are defined in the code to analyze.

Thus, I wish to parse prop.cpp knowing f.cpp’s AST so I don’t run into unknown symbols. But I haven’t been successful so far.

i could cheat and merge the two files in a temporary file but I don’t like that idea.
I could also require prop.cpp to be self contained (with all the headers needed) but that would hinder the tool’s usability (headers to include, need to provide to clang all the compilation options…).

If you have any lead,

Thanks,
David.

Hello,

I’m writing a some tool for doing static analysis on C++ code.

My tool has obviously two inputs: the file(s) to check (let’s say f.cpp) and the property to check .

Currently (I’m in a early stage of development), I want to describe the property directly in C++ (let’s say prop.cpp).
and use clang/libtooling to read it .The thing is that the property will use symbols that are defined in the code to analyze.

Thus, I wish to parse prop.cpp knowing f.cpp’s AST so I don’t run into unknown symbols. But I haven’t been successful so far.

i could cheat and merge the two files in a temporary file but I don’t like that idea.
I could also require prop.cpp to be self contained (with all the headers needed) but that would hinder the tool’s usability (headers to include, need to provide to clang all the compilation options…).

Unfortunately, that’s a general restriction of C++ tools. C++ is unparsable without knowing the full types.

Yeah, I know.
That is why I'm asking if I can re-use the AST from one file when parsing another file so I don't encounter that problem.

You can try the trick with one pass: when prepare cmd line options for parsing the second TU pass “-inlclude first_file.cpp” and add cmd line options of first_file.cpp as well. it will invisibly add #include “first_file.cpp” as the first line text in your prop.cpp Note that you should add cmd line options of first_file.cpp as well, because you want this phantom #include to be correctly handled. Hope it helps, Vladimir.