[cfe-dev] How to determine if a raw_id token is a macro name?

David Rector via cfe-dev cfe-dev at lists.llvm.org
Fri Jan 7 04:45:23 PST 2022


I believe the best you can do is to convert the string to an IdentifierInfo and access hadMacroDefinition:
```
IdentifierInfo &FooII = Context.Idents.get("Foo");
bool FooMaybeMacroName = FooII.hadMacroDefinition();
```
This won’t be perfect — it won’t tell you whether Foo was defined at the particular SourceLocation you are interested in, only whether at some point in the TU a macro with that name was defined.

It would be nice to be able to test `FooII.hasMacroDefinition()` instead, but the problem mentioned in the other thread rears its head here too: the AST matching on which clang tidy depends is all done during HandleTranslationUnit, *after* all parsing has completed, so hasMacroDefinition() will only return true if a macro with that name was not #undef’d.  

> On Jan 6, 2022, at 7:39 PM, Richard via cfe-dev <cfe-dev at lists.llvm.org> wrote:
> 
> Hi Team,
> 
> Preprocessor::isMacroDefined seems to be what I want, but how can I
> access the active preprocesor from inside a clang-tidy check?
> 
> Currently I have a check that walks over a declaration string with the
> Lexer.  A bug was filed that this check had a negative interaction
> with macros (no surprise).  I've got a hacked up solution that passes
> my test cases, but it would be really nice to know if a raw_id token I
> just encountered while lexing the declaration is the name of a macro.
> 
> For context, see:
> Lexing state machine
> <https://github.com/llvm/llvm-project/blob/main/clang-tools-extra/clang-tidy/modernize/RedundantVoidArgCheck.cpp#L118>
> bug
> <https://github.com/llvm/llvm-project/issues/43791>
> 
> Thanks,
> 
> -- Richard
> -- 
> "The Direct3D Graphics Pipeline" free book <http://tinyurl.com/d3d-pipeline>
>             The Terminals Wiki <http://terminals-wiki.org>
>     The Computer Graphics Museum <http://ComputerGraphicsMuseum.org>
>  Legalize Adulthood! (my blog) <http://LegalizeAdulthood.wordpress.com>
> _______________________________________________
> cfe-dev mailing list
> cfe-dev at lists.llvm.org
> https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20220107/316a7ff8/attachment.html>


More information about the cfe-dev mailing list