<div dir="ltr"><div class="gmail_quote"><div dir="ltr">On Fri, Jul 10, 2015 at 6:56 PM Mario Schwalbe <<a href="mailto:mario.schwalbe@tu-dresden.de">mario.schwalbe@tu-dresden.de</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Hi all,<br>
<br>
I'm trying to familiarize myself with clang and in particular its AST matchers.<br>
Therefore, I tried to write some piece of code to determine if an expression<br>
has been passed as an argument to a macro:<br>
<br>
    auto& context = /* ASTContext */<br>
    auto& SM = context.getSourceManager();<br>
    auto loc = expr.getLocStart();<br>
<br>
    if (SM.isMacroArgExpansion(loc)) {<br>
        const auto expLoc = SM.getImmediateExpansionRange(loc).first;<br>
        const auto name   = Lexer::getImmediateMacroName(expLoc, SM, context.getLangOpts());<br>
        if (name == "MACRO")<br>
            return true;<br>
    }<br>
<br>
Given the following code:<br>
<br>
#define MACRO(v)  (v + 1) - 2<br>
<br>
int test(int x)<br>
{<br>
    return MACRO(x);<br>
}<br>
<br>
it also returns true for the expression `x + 1' (BinaryOperator) within<br>
the macro expansion, not just the DeclRefExpr `x' itself. Looks like both<br>
expressions have the same location (I dumped its internal representation).<br>
But why? And how can the 2 cases be distinguished? Or what am I missing here?<br>
Thanks in advance for any pointers.<br></blockquote><div><br></div><div>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).</div><div>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.</div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<br>
ciao,<br>
Mario<br>
_______________________________________________<br>
cfe-dev mailing list<br>
<a href="mailto:cfe-dev@cs.uiuc.edu" target="_blank">cfe-dev@cs.uiuc.edu</a><br>
<a href="http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev" rel="noreferrer" target="_blank">http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev</a><br>
</blockquote></div></div>