我正在尝试使用 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。
我将不胜感激任何帮助。