diff --git a/docs/LibASTMatchersReference.html b/docs/LibASTMatchersReference.html index a0403a5edf6524093e27337eb735ba8d801a12e4..cb5020af49c6173cb7fcb6fc3923d1a1c3aef06b 100644 --- a/docs/LibASTMatchersReference.html +++ b/docs/LibASTMatchersReference.html @@ -1872,6 +1872,14 @@ floatLiteral(equals(3.14)) and floatLiteral(equals(314e-2)) 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<<a href="http://clang.llvm.org/doxygen/classclang_1_1CharacterLiteral.html">CharacterLiteral</a>>, Matcher<<a href="http://clang.llvm.org/doxygen/classclang_1_1CXXBoolLiteralExpr.html">CXXBoolLiteralExpr</a>>, Matcher<<a href="http://clang.llvm.org/doxygen/classclang_1_1FloatingLiteral.html">FloatingLiteral</a>>, Matcher<<a href="http://clang.llvm.org/doxygen/classclang_1_1IntegerLiteral.html">IntegerLiteral</a>> </pre></td></tr> @@ -2327,6 +2335,14 @@ floatLiteral(equals(3.14)) and floatLiteral(equals(314e-2)) 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<<a href="http://clang.llvm.org/doxygen/classclang_1_1CharacterLiteral.html">CharacterLiteral</a>>, Matcher<<a href="http://clang.llvm.org/doxygen/classclang_1_1CXXBoolLiteralExpr.html">CXXBoolLiteralExpr</a>>, Matcher<<a href="http://clang.llvm.org/doxygen/classclang_1_1FloatingLiteral.html">FloatingLiteral</a>>, Matcher<<a href="http://clang.llvm.org/doxygen/classclang_1_1IntegerLiteral.html">IntegerLiteral</a>> </pre></td></tr> @@ -2583,6 +2599,14 @@ floatLiteral(equals(3.14)) and floatLiteral(equals(314e-2)) 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<<a href="http://clang.llvm.org/doxygen/classclang_1_1CharacterLiteral.html">CharacterLiteral</a>>, Matcher<<a href="http://clang.llvm.org/doxygen/classclang_1_1CXXBoolLiteralExpr.html">CXXBoolLiteralExpr</a>>, Matcher<<a href="http://clang.llvm.org/doxygen/classclang_1_1FloatingLiteral.html">FloatingLiteral</a>>, Matcher<<a href="http://clang.llvm.org/doxygen/classclang_1_1IntegerLiteral.html">IntegerLiteral</a>> </pre></td></tr> @@ -2866,6 +2890,14 @@ floatLiteral(equals(3.14)) and floatLiteral(equals(314e-2)) 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<<a href="http://clang.llvm.org/doxygen/classclang_1_1CharacterLiteral.html">CharacterLiteral</a>>, Matcher<<a href="http://clang.llvm.org/doxygen/classclang_1_1CXXBoolLiteralExpr.html">CXXBoolLiteralExpr</a>>, Matcher<<a href="http://clang.llvm.org/doxygen/classclang_1_1FloatingLiteral.html">FloatingLiteral</a>>, Matcher<<a href="http://clang.llvm.org/doxygen/classclang_1_1IntegerLiteral.html">IntegerLiteral</a>> </pre></td></tr> diff --git a/include/clang/ASTMatchers/ASTMatchers.h b/include/clang/ASTMatchers/ASTMatchers.h index cba4c99be959f55083a1e3476b7b3dc3ae6197c7..c9b496df33f7cbdc0ace1f32d9d7f9e88fc6eebb 100644 --- a/include/clang/ASTMatchers/ASTMatchers.h +++ b/include/clang/ASTMatchers/ASTMatchers.h @@ -3821,6 +3821,14 @@ AST_MATCHER_P(CompoundStmt, statementCountIs, unsigned, N) { /// 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> template <typename ValueT> diff --git a/unittests/ASTMatchers/ASTMatchersNodeTest.cpp b/unittests/ASTMatchers/ASTMatchersNodeTest.cpp index dfaa441cd7643db4539be75c093cfbfaebad48b0..58c26eafd7e0d1225c3ef815e262eb73a037b4f9 100644 --- a/unittests/ASTMatchers/ASTMatchersNodeTest.cpp +++ b/unittests/ASTMatchers/ASTMatchersNodeTest.cpp @@ -666,6 +666,12 @@ TEST(Matcher, IntegerLiterals) { EXPECT_TRUE(notMatches("int i = 'a';", HasIntLiteral)); EXPECT_TRUE(notMatches("int i = 1e10;", HasIntLiteral)); EXPECT_TRUE(notMatches("int i = 10.0;", HasIntLiteral)); + + // Negative integers. + EXPECT_TRUE( + matches("int i = -10;", + unaryOperator(hasOperatorName("-"), + hasUnaryOperand(integerLiteral(equals(10)))))); } TEST(Matcher, FloatLiterals) {