我希望在 clang ast 匹配器的帮助下更改以下代码。
foo(NUM << DEV_SHIFT | DEVICE);
到
foo(ADDR(NUM, DEVICE));
和
#define ADDR(a, b) (((a) << NUM_SHIFT) | (b))
我有以下 AST 匹配器,它似乎可以很好地识别代码。
Finder->addMatcher(
callExpr(hasArgument(
0, binaryOperator(hasOperatorName("|"),
hasLHS(ignoringParenImpCasts(
binaryOperator(hasOperatorName("<<")))))
.bind("replaceWithMacro"))),
this);
但是我在理解如何写支票和翻译方面有一个问题。我目前坚持使用此代码:
void FirstCheckCheck::check(const MatchFinder::MatchResult &Result) {
// FIXME: Add callback implementation.
if (const auto MatchedDecl =
Result.Nodes.getNodeAs<CallExpr>("replaceWithMacro")) {
diag(MatchedDecl->getExprLoc(), "CallExp");
} else if (const auto MatchedDecl =
Result.Nodes.getNodeAs<Expr>("replaceWithMacro")) {
diag(MatchedDecl->getExprLoc(), "Expr");
diag(MatchedDecl->getBeginLoc(), "BeginLOC");
diag(MatchedDecl->getEndLoc(), "EndLOC");
}
我不知道如何将这两个变量提取为字符串。我正在查看 Expr 类的文档(http://clang.llvm.org/doxygen/classclang_1_1Expr.html),但我找不到有用的东西。
如果有人能指出我正确的方向,将不胜感激。
添加编辑。