Lexical scope for macro expansion

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


Sounds like it could work! I don't know the real use case you're going for here though.


Just suppose we want something that warn the user that:

typedef intptr_t Handle;

is preferred to:

#define Handle intptr_t