<div dir="ltr"><div class="gmail_quote"><div dir="ltr">On Thu, Jun 30, 2016 at 7:02 AM scott constable via cfe-dev <<a href="mailto:cfe-dev@lists.llvm.org">cfe-dev@lists.llvm.org</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr">Hi All,<div><br></div><div>I have an AST matcher which has successfully matched all "if" statements in a large program:</div><div><br></div><div><div>StatementMatcher CondMatcher = ifStmt(hasCondition(expr().bind("condition")));</div></div><div><br></div><div>My goal is to use libtooling replacements to refactor all "if" conditions <cond> into my_func(<cond>). For instance,</div><div><br></div><div>if (true) { ... }</div><div><br></div><div>should refactor to</div><div><br></div><div>if (my_func(true)) { ... }</div><div><br></div><div>The only time I run into trouble is when I try to refactor complex macros. For instance, take the following code:</div><div><br></div><div><div>#define GUARD2 GUARD1(999)</div><div>#define GUARD1(X) if (1 | X) { return 11; }</div><div><br></div><div>int main() {</div><div>  GUARD2</div><div>  return 0;</div><div>}</div></div><div><br></div><div>My AST matcher successfully grabs the expanded if statement's condition. To perform my replacement, I would like to obtain the following source locations:</div><div><br></div><div><div>#define GUARD2 GUARD1(999)</div><div>#define GUARD1(X) if (<LBegin>1 | X<LEnd>) { return 11; }</div><div><br></div><div>int main() {</div><div>  GUARD2</div><div>  return 0;</div><div>}</div></div><div><br></div><div>but if I do something like</div><div><br></div><div>SourceLocation LBegin = MySourceManager.getSpellingLoc(Condition->getBeginLoc());</div><div>SourceLocation LEnd = MySourceManager.getSpellingLoc(Condition->getEndLoc());<br></div><div>LEnd = Lexer::getLocForEndOfToken(LEnd, 0, MySourceManager, opts);</div><div><br></div><div>then the source locations are as follows:</div><div><br></div><div><div>#define GUARD2 GUARD1(999<LEnd>)</div><div>#define GUARD1(X) if (<LBegin>1 | X) { return 11; }</div><div><br></div><div>int main() {</div><div>  GUARD2</div><div>  return 0;</div><div>}</div></div><div><br></div><div>And thus the replacements turn out as </div><div><br></div><div><div>1 #define GUARD2 GUARD1(999))</div><div>2 #define GUARD1(X) if (my_func(1 | X) { return 11; }</div><div>3 </div><div>4 int main() {</div><div>5   GUARD2</div><div>6   return 0;</div><div>7 }</div></div><div><br></div><div>which is incorrect. Note the extra parenthesis in line 1, and missing parenthesis in line 2. Any help at all would be very much appreciated.</div></div></blockquote><div><br></div><div>That's what Lexer::makeFileCharRange solves - for a given range, it will give you the range on the same macro expansion level, if such a range exists.</div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr"><div><br></div><div>Thanks in advance,</div><div><br></div><div>Scott Constable</div></div>
_______________________________________________<br>
cfe-dev mailing list<br>
<a href="mailto:cfe-dev@lists.llvm.org" target="_blank">cfe-dev@lists.llvm.org</a><br>
<a href="http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev" rel="noreferrer" target="_blank">http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev</a><br>
</blockquote></div></div>