[cfe-dev] fatal/error/warning control in clang/llvm

A.C.Telea a.c.telea at rug.nl
Thu Apr 14 01:48:41 PDT 2011


Hi all,

I'm working on developing a static analysis back-end where information from a clang AST is filtered and translated to a third-party format for integration in an existing toolchain. In brief, given a set of C/C++ source files, I extract compound graphs (function calls and symbol scope chains being the two types of relations in the graph). For this, I first extract ASTs from each source file

clang -emit-ast foo.cpp -o foo.ast

next run my back-end which is very similar to clang-wpa on the resulting ASTs.

Here's the problem: if the first operation (clang -emit-ast) does encounter some errors (fatal or not), then there is no AST output. Errors here can be of many kind, e.g. caused by incorrect code (missing declarations for symbols etc) or missing search paths for headers.

>From the documentation of clang, I understood (correct me if I'm wrong!) that clang CAN be used as a tolerant static analyzer/parser. By this, I mean the type of tolerance to errors provided e.g. by Elsa/Elkhound. This would allow my toolchain to analyze incomplete/incorrect code and STILL output a compound call graph, albeit possibly with some omissions (which are fine for me).

Question: how can I easily, and centrally, instruct clang (or some other system component?) to simply ignore errors and proceed further as much as possible with whatever it is instructed to do? From the documentation, I understood one can disable warnings, but I am not sure how to disable errors (taking the responsibility of whatever ensues).

The only way I found this to work is by an ordinary hack - edit the .td files like DiagnosticSemaKind.td and similar, and replace Error<...> by Warning<...> for specific errors. This indeed allows clang to continue parsing when there's a missing header, or some unknown symbol. However, this is a low-level hack that intrusively modifies stuff deep within clang itself.

Is there some elegant, e.g. command-line, way to achieve the effect I desire?

If not, can I do it still centrally e.g. by subclassing a specific (Diagnostics?) class, or similar?

Thanks in advance for any help on this,

Alex



More information about the cfe-dev mailing list