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

James Dennett via cfe-dev cfe-dev at lists.llvm.org
Thu Mar 2 16:05:58 PST 2017


On Thu, Mar 2, 2017 at 1:10 AM, Stephan Bergmann via cfe-dev <
cfe-dev at lists.llvm.org> wrote:

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

You might try to make a lexer to Lex(token) for you until you've reached
the end of the range, rather than navigating by source locations.  Though
actually I think we should make the AST record the location of the
`constexpr` keyword, so that applications don't have to jump through these
hoops.

-- James
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20170302/3bbf6bcd/attachment.html>


More information about the cfe-dev mailing list