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

Kim Gräsman kim.grasman at gmail.com
Thu Sep 19 03:28:19 PDT 2013


OK, I think I see where I'm going wrong with this reasoning: the clang
preprocessor doesn't see through preprocessor conditionals, right?

So if the #error never triggers, I won't be able to spy into it using
the Lexer/PP. Did I get that right?

Do I have any options besides a regex over the entire file?

Thanks,
- Kim

On Thu, Sep 19, 2013 at 10:43 AM, Kim Gräsman <kim.grasman at gmail.com> wrote:
> 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