[cfe-dev] Detecting #error directive inside #if block

Kim Gräsman kim.grasman at gmail.com
Thu Sep 19 01:43:33 PDT 2013


Hello,

I'm toying with the idea of detecting and parsing user diagnostics to
help IWYU guess which is the public header for a given private header.

For example, glib has the following convention:

 #if !defined (__GLIB_H_INSIDE__) && !defined (GLIB_COMPILATION)
 #error "Only <glib.h> can be included directly."
 #endif

https://github.com/djdeath/glib/blob/master/glib/gmem.h#L30

Essentially I would like to trap all #error directives within a #if,
#ifdef or #ifndef, scan the raw message for a pattern and extract the
quoted include name. This would be an optional heuristic, but I think
it could be made to work reasonably well in the general case.

Is there a way to use PPCallbacks for this? I noticed there's no
PPCallback for #error, but even given that I'm not sure if I can
reason about structure in the preprocessor (like I would in the AST),
e.g. find the nearest #if block.

Any ideas?

Thanks,
- Kim



More information about the cfe-dev mailing list