Static analyser for multiple files in clang

Hi,

I am writing a static analyser/type checker in clang, which needs to
follow the control flow of the source code, including going into bodies
of functions. It is currently implemented as an ASTConsumer and used as
a clang plugin. However, from what I understood, it works only for one
TranslationUnit/source file for each instance of the plugin, and I
intend to make it work for multiple source files as one single
program/AST.

So I am writing to the list to see whether anyone has tackled this
problem before and whether there are any 'best practices' or examples
for building static analysis tool with similar requirements?

I am looking at a few possibilities:

1. Merging ASTs from each source then run the analysis on the merged AST
2. Start analysing a 'main' AST and if something (eg. function body) is
missing, try search/walk other ASTs

Any directions would be much appreciated!

Many thanks,
Nicholas

Hi,

I am writing a static analyser/type checker in clang, which needs to
follow the control flow of the source code, including going into bodies
of functions. It is currently implemented as an ASTConsumer and used as
a clang plugin. However, from what I understood, it works only for one
TranslationUnit/source file for each instance of the plugin, and I
intend to make it work for multiple source files as one single
program/AST.

So I am writing to the list to see whether anyone has tackled this
problem before and whether there are any 'best practices' or examples
for building static analysis tool with similar requirements?

I am looking at a few possibilities:

1. Merging ASTs from each source then run the analysis on the merged AST
2. Start analysing a 'main' AST and if something (eg. function body) is
missing, try search/walk other ASTs

Any directions would be much appreciated!

The way we handle that (and make it scale to static analysis on many
thousand TUs) is to output the relevant data from all the TUs first,
and then reduce it in a second step. That way you can fully
parallelize the expensive part (namely parsing C++).

You can also run the same ASTConsumer over multiple TUs via the
LibTooling stuff (http://clang.llvm.org/docs/LibTooling.html) in a
single process, thus at least allowing you to share results between
the runs. It'll still get you different ASTs though (type pointers etc
will not be reusable).

Cheers,
/Manuel