Skip to content

Commit

Permalink
[ASTMatchers][NFC] integerLiteral(): Mention negative integers in
Browse files Browse the repository at this point in the history
documentation.

Trying to match integerLiteral(-1) will silently fail, because an numeric
literal is always positive.
- Update the documentation to explain how to match negative numeric
  literals.
- Add a unit test.

Differential Revision: https://reviews.llvm.org/D35196

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@307663 91177308-0d34-0410-b5e6-96231b3b80d8
  • Loading branch information
legrosbuffle committed Jul 11, 2017
1 parent 1e30b7a commit e575997
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 0 deletions.
32 changes: 32 additions & 0 deletions docs/LibASTMatchersReference.html
Original file line number Diff line number Diff line change
Expand Up @@ -1872,6 +1872,14 @@ <h2 id="narrowing-matchers">Narrowing Matchers</h2>
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&lt;<a href="http://clang.llvm.org/doxygen/classclang_1_1CharacterLiteral.html">CharacterLiteral</a>&gt;, Matcher&lt;<a href="http://clang.llvm.org/doxygen/classclang_1_1CXXBoolLiteralExpr.html">CXXBoolLiteralExpr</a>&gt;,
Matcher&lt;<a href="http://clang.llvm.org/doxygen/classclang_1_1FloatingLiteral.html">FloatingLiteral</a>&gt;, Matcher&lt;<a href="http://clang.llvm.org/doxygen/classclang_1_1IntegerLiteral.html">IntegerLiteral</a>&gt;
</pre></td></tr>
Expand Down Expand Up @@ -2327,6 +2335,14 @@ <h2 id="narrowing-matchers">Narrowing Matchers</h2>
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&lt;<a href="http://clang.llvm.org/doxygen/classclang_1_1CharacterLiteral.html">CharacterLiteral</a>&gt;, Matcher&lt;<a href="http://clang.llvm.org/doxygen/classclang_1_1CXXBoolLiteralExpr.html">CXXBoolLiteralExpr</a>&gt;,
Matcher&lt;<a href="http://clang.llvm.org/doxygen/classclang_1_1FloatingLiteral.html">FloatingLiteral</a>&gt;, Matcher&lt;<a href="http://clang.llvm.org/doxygen/classclang_1_1IntegerLiteral.html">IntegerLiteral</a>&gt;
</pre></td></tr>
Expand Down Expand Up @@ -2583,6 +2599,14 @@ <h2 id="narrowing-matchers">Narrowing Matchers</h2>
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&lt;<a href="http://clang.llvm.org/doxygen/classclang_1_1CharacterLiteral.html">CharacterLiteral</a>&gt;, Matcher&lt;<a href="http://clang.llvm.org/doxygen/classclang_1_1CXXBoolLiteralExpr.html">CXXBoolLiteralExpr</a>&gt;,
Matcher&lt;<a href="http://clang.llvm.org/doxygen/classclang_1_1FloatingLiteral.html">FloatingLiteral</a>&gt;, Matcher&lt;<a href="http://clang.llvm.org/doxygen/classclang_1_1IntegerLiteral.html">IntegerLiteral</a>&gt;
</pre></td></tr>
Expand Down Expand Up @@ -2866,6 +2890,14 @@ <h2 id="narrowing-matchers">Narrowing Matchers</h2>
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&lt;<a href="http://clang.llvm.org/doxygen/classclang_1_1CharacterLiteral.html">CharacterLiteral</a>&gt;, Matcher&lt;<a href="http://clang.llvm.org/doxygen/classclang_1_1CXXBoolLiteralExpr.html">CXXBoolLiteralExpr</a>&gt;,
Matcher&lt;<a href="http://clang.llvm.org/doxygen/classclang_1_1FloatingLiteral.html">FloatingLiteral</a>&gt;, Matcher&lt;<a href="http://clang.llvm.org/doxygen/classclang_1_1IntegerLiteral.html">IntegerLiteral</a>&gt;
</pre></td></tr>
Expand Down
8 changes: 8 additions & 0 deletions include/clang/ASTMatchers/ASTMatchers.h
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down
6 changes: 6 additions & 0 deletions unittests/ASTMatchers/ASTMatchersNodeTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit e575997

Please sign in to comment.