[cfe-dev] Traversing the (macro-expanded) tokens that make up a Decl?

Stephan Bergmann via cfe-dev cfe-dev at lists.llvm.org
Thu Mar 2 03:10:10 PST 2017


My problem is as follows:  I have a FunctionDecl d for which I know that 
d->isConstexpr(), so somewhere in d->getSourceRange() the token 
"constexpr" should appear.  Now, I want to know whether that "constexpr" 
is from the expansion of a specific object-like macro

   #define M constexpr

(whose use could be nested in some other macros, so it's important here 
to look into macro invocations in general).

I'm trying to traverse d->getSourceRange() with 
Lexer::MeasureTokenLength/SourceManager::getCharacterData to get at a 
token's content, then SourceLocation::getLocWithOffset to get at the 
next token.  Which works more-or-less well.  However:

* When d->getSourceRange() happens to start off within a macro 
expansion, I could fall off its end before reaching the declaration's 
end.  This one is easy, I can use 
SourceManager::isAtEndOfImmediateMacroExpansion to climb back out of the 
macro expansion and continue traversal one level further out.

* But when the traversal reaches a macro invocation, I get the token of 
that invocation (multiple tokens in case of a function-like macro with 
its parentheses and arguments), without a clue that it is a macro and 
without a way to step into its expansion.  Am I missing anything, or is 
there just no way to do that?

(I find at least two unanswered questions on stackoverflow that look 
like they ask the same question, 
<http://stackoverflow.com/questions/30831936/how-to-get-tokens-for-a-declaration-using-clang-c-api> 
and 
<http://stackoverflow.com/questions/36235445/clang-getting-preprocessed-characters-with-sourcelocation-sourcerange>.)



More information about the cfe-dev mailing list