[cfe-dev] How to find macro arguments?

Manuel Klimek klimek at google.com
Mon Jul 13 05:55:24 PDT 2015


On Fri, Jul 10, 2015 at 6:56 PM Mario Schwalbe <mario.schwalbe at tu-dresden.de>
wrote:

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

I'd expect that for the second case you'd have start and end location of
the range in different expansions (which is generally where we'll have to
through our hands in the air and go home).
You can look at what getFileCharRange / getSourceText in Lexer:: does to
make sure a range in a macro is fully on the same expansion level.


>
> ciao,
> Mario
> _______________________________________________
> cfe-dev mailing list
> cfe-dev at cs.uiuc.edu
> http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20150713/4c7575ca/attachment.html>


More information about the cfe-dev mailing list