我正在尝试使用 libTooling 编写源到源转换器。
我正在使用 ASTMatchers 尝试查找if
没有大括号的语句,然后使用重写器添加大括号。
我正在使用的匹配器是:
ifStmt(unless(hasDescendant(compoundStmt())))
然后我只得到开始和结束位置,并重写花括号。
这是它的源代码:
if (const IfStmt *IfS = Result.Nodes.getNodeAs<clang::IfStmt>("ifStmt")) {
const Stmt *Then = IfS->getThen();
Rewrite.InsertText(Then->getLocStart(), "{", true, true);
Rewrite.InsertText(Then->getLocEnd(),"}",true,true);
现在的问题是,由于某种原因,结束位置总是偏离 2 个字符。为什么会这样?