0

我正在尝试使用 clang ASTMatcher 来查找是否具有 RHS 为整数值 0 或 0.0f 的条件的语句。为此,我能够编写如下查询:

clang-query> match ifStmt(hasCondition(binaryOperator(hasRHS(integerLiteral(equals(0)))))

当我使用上述查询时,我收到如下错误:

1:2: Error parsing argument 1 for matcher ifStmt.
1:9: Error parsing argument 1 for matcher hasCondition.
1:22: Error parsing argument 1 for matcher binaryOperator.
1:37: Error parsing argument 1 for matcher hasRHS.
1:44: Error parsing argument 1 for matcher integerLiteral.
1:59: Matcher not found: equals

但是,当我参考http://clang.llvm.org/docs/LibASTMatchersReference.html上的在线指南时,它说 Matcher equals 可用:

Matcher<IntegerLiteral> equals  const ValueT Value

Matches literals that are equal to the given value of type ValueT.

Given
  f('false, 3.14, 42);
characterLiteral(equals(0))
  matches 'cxxBoolLiteral(equals(false)) and cxxBoolLiteral(equals(0))
  match false
floatLiteral(equals(3.14)) and floatLiteral(equals(314e-2))
  match 3.14
integerLiteral(equals(42))
  matches 42

Note that you cannot directly match a negative numeric literal because the
minus sign is not part of the literal: It is a unary operator whose operand
is the positive numeric literal. Instead, you must use a unaryOperator()
matcher to match the minus sign:

unaryOperator(hasOperatorName("-"),
              hasUnaryOperand(integerLiteral(equals(13))))

Usable as: Matcher<CharacterLiteral>, Matcher<CXXBoolLiteralExpr>,
           Matcher<FloatingLiteral>, Matcher<IntegerLiteral>

我在 Ubuntu 16.04LTS 上使用 llvm-4.0 和 clang 3.8。

我将不胜感激任何帮助。

4

1 回答 1

0

好吧,我也在探索 AST Matchers。我也面临与 clang-query 相同的问题,但是当我在一个小的 cpp 文件中使用相同的匹配器表达式时(示例可以在这里找到https://clang.llvm.org/docs/LibASTMatchersTutorial.html)并将其编译为一个可执行文件并传递了如果我想检查“if statements”的文件,它会正确显示结果。

由于某些原因,equals() 在 clang-query 中不可用。

于 2018-04-18T18:46:39.490 回答