<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><br></div><div>Thanks in advance,</div><div><br></div><div>Scott Constable</div></div>