[cfe-dev] Lexical scope for macro expansion

Abramo Bagnara abramobagnara at tin.it
Thu Dec 10 10:25:52 PST 2009

Il 10/12/2009 18:37, Chris Lattner ha scritto:
> On Dec 10, 2009, at 8:56 AM, Abramo Bagnara wrote:
>> Is there any way from MacroExpands callback to reach current lexical scope?
>> What I'm trying to do is to detect if some identifiers in the macro body
>> is a known typedef id.
> Nope, there is no way to do this, because the lexer/preprocessor
> lives at a lower level (and macros can be used and defined in
> multiple contexts, and don't follow the grammar of the language).
> However, given a typedef, you can tell if it came from a macro.

Let see if I've understand correctly:

1) I collect macro bodies using MacroExpands callback

2) I collect the spelling locations for the NameLoc of each encountered

3) I scan the previously collected tokens of the macros body: if one of
them has the same location of one of that collected in step 2 I know for
sure that the macro body contains a typedef name


More information about the cfe-dev mailing list