[cfe-dev] How to find macro arguments?
Mario Schwalbe
mario.schwalbe at tu-dresden.de
Fri Jul 10 05:53:24 PDT 2015
Hi all,
I'm trying to familiarize myself with clang and in particular its AST matchers.
Therefore, I tried to write some piece of code to determine if an expression
has been passed as an argument to a macro:
auto& context = /* ASTContext */
auto& SM = context.getSourceManager();
auto loc = expr.getLocStart();
if (SM.isMacroArgExpansion(loc)) {
const auto expLoc = SM.getImmediateExpansionRange(loc).first;
const auto name = Lexer::getImmediateMacroName(expLoc, SM, context.getLangOpts());
if (name == "MACRO")
return true;
}
Given the following code:
#define MACRO(v) (v + 1) - 2
int test(int x)
{
return MACRO(x);
}
it also returns true for the expression `x + 1' (BinaryOperator) within
the macro expansion, not just the DeclRefExpr `x' itself. Looks like both
expressions have the same location (I dumped its internal representation).
But why? And how can the 2 cases be distinguished? Or what am I missing here?
Thanks in advance for any pointers.
ciao,
Mario
More information about the cfe-dev
mailing list