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

Douglas Gregor dgregor at apple.com
Fri May 6 07:33:47 PDT 2011


On Apr 14, 2011, at 1:48 AM, A.C.Telea wrote:

> 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.

Right. This is intentional, because Clang's AST serialization mechanism depends on some of the invariants of a well-formed AST, so it would be dangerous to allow saving/loading an AST that resulted from ill-formed code.

Is it possible that the AST reader/writer could be made to work on ill-formed code? Absolutely, but it's going to require auditing all of the reader/writer code (especially the reader) and performing a bunch of testing.

>> 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.

... and you're breaking some potentially-important invariants of the compiler.

> 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?

There's no elegant way to do this that I know of. Errors are often errors because allowing them through breaks invariants that later stages depend on.

	- Doug

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20110506/62f83001/attachment.html>


More information about the cfe-dev mailing list