]> granicus.if.org Git - clang/commitdiff
[ASTMatchers] isSignedInteger() and isUnsignedInteger()
authorClement Courbet <courbet@google.com>
Tue, 12 Jul 2016 06:36:00 +0000 (06:36 +0000)
committerClement Courbet <courbet@google.com>
Tue, 12 Jul 2016 06:36:00 +0000 (06:36 +0000)
Complementary to isInteger(), these match signed and unsigned integers
respectively.

Review: http://reviews.llvm.org/D21989

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@275157 91177308-0d34-0410-b5e6-96231b3b80d8

docs/LibASTMatchersReference.html
include/clang/ASTMatchers/ASTMatchers.h
lib/ASTMatchers/Dynamic/Registry.cpp
unittests/ASTMatchers/ASTMatchersNarrowingTest.cpp

index a8a47d5b54bd1fe4f2a2a63bcd4b90cd60393f58..70c83ea5ccaa71e82db56e3345e9fd21c72d69a0 100644 (file)
@@ -2968,6 +2968,30 @@ matches "a(int)", "b(long)", but not "c(double)".
 </pre></td></tr>
 
 
+<tr><td>Matcher&lt;<a href="http://clang.llvm.org/doxygen/classclang_1_1QualType.html">QualType</a>&gt;</td><td class="name" onclick="toggle('isSignedInteger0')"><a name="isSignedInteger0Anchor">isSignedInteger</a></td><td></td></tr>
+<tr><td colspan="4" class="doc" id="isSignedInteger0"><pre>Matches QualType nodes that are of signed integer type.
+
+Given
+  void a(int);
+  void b(unsigned long);
+  void c(double);
+functionDecl(hasAnyParameter(hasType(isInteger())))
+matches "a(int)", but not "b(unsigned long)" and "c(double)".
+</pre></td></tr>
+
+
+<tr><td>Matcher&lt;<a href="http://clang.llvm.org/doxygen/classclang_1_1QualType.html">QualType</a>&gt;</td><td class="name" onclick="toggle('isUnsignedInteger0')"><a name="isUnsignedInteger0Anchor">isUnsignedInteger</a></td><td></td></tr>
+<tr><td colspan="4" class="doc" id="isUnsignedInteger0"><pre>Matches QualType nodes that are of unsigned integer type.
+
+Given
+  void a(int);
+  void b(unsigned long);
+  void c(double);
+functionDecl(hasAnyParameter(hasType(isInteger())))
+matches "b(unsigned long)", but not "a(int)" and "c(double)".
+</pre></td></tr>
+
+
 <tr><td>Matcher&lt;<a href="http://clang.llvm.org/doxygen/classclang_1_1QualType.html">QualType</a>&gt;</td><td class="name" onclick="toggle('isVolatileQualified0')"><a name="isVolatileQualified0Anchor">isVolatileQualified</a></td><td></td></tr>
 <tr><td colspan="4" class="doc" id="isVolatileQualified0"><pre>Matches QualType nodes that are volatile-qualified, i.e., that
 include "top-level" volatile.
@@ -3849,8 +3873,11 @@ Given
   void f(int i);
   int y;
   f(y);
-callExpr(declRefExpr(to(varDecl(hasName("y")))),
-parmVarDecl(hasType(isInteger())))
+callExpr(
+  forEachArgumentWithParam(
+    declRefExpr(to(varDecl(hasName("y")))),
+    parmVarDecl(hasType(isInteger()))
+))
   matches f(y);
 with declRefExpr(...)
   matching int y
@@ -4139,8 +4166,11 @@ Given
   void f(int i);
   int y;
   f(y);
-callExpr(declRefExpr(to(varDecl(hasName("y")))),
-parmVarDecl(hasType(isInteger())))
+callExpr(
+  forEachArgumentWithParam(
+    declRefExpr(to(varDecl(hasName("y")))),
+    parmVarDecl(hasType(isInteger()))
+))
   matches f(y);
 with declRefExpr(...)
   matching int y
index 28449eaacd6bb35497bc2c1de15cef749bf982c0..aef4b4eafd9ac868828e6cfaa62d70f746449563 100644 (file)
@@ -4067,6 +4067,34 @@ AST_MATCHER(QualType, isInteger) {
     return Node->isIntegerType();
 }
 
+/// \brief Matches QualType nodes that are of unsigned integer type.
+///
+/// Given
+/// \code
+///   void a(int);
+///   void b(unsigned long);
+///   void c(double);
+/// \endcode
+/// functionDecl(hasAnyParameter(hasType(isInteger())))
+/// matches "b(unsigned long)", but not "a(int)" and "c(double)".
+AST_MATCHER(QualType, isUnsignedInteger) {
+    return Node->isUnsignedIntegerType();
+}
+
+/// \brief Matches QualType nodes that are of signed integer type.
+///
+/// Given
+/// \code
+///   void a(int);
+///   void b(unsigned long);
+///   void c(double);
+/// \endcode
+/// functionDecl(hasAnyParameter(hasType(isInteger())))
+/// matches "a(int)", but not "b(unsigned long)" and "c(double)".
+AST_MATCHER(QualType, isSignedInteger) {
+    return Node->isSignedIntegerType();
+}
+
 /// \brief Matches QualType nodes that are of character type.
 ///
 /// Given
index d8ddf55733fd31877d74f7081c2267b2fd47ac08..a8d4b88d8580970f458b1d4c551445b9d4b20495 100644 (file)
@@ -321,9 +321,11 @@ RegistryMaps::RegistryMaps() {
   REGISTER_MATCHER(isProtected);
   REGISTER_MATCHER(isPublic);
   REGISTER_MATCHER(isPure);
+  REGISTER_MATCHER(isSignedInteger);
   REGISTER_MATCHER(isStruct);
   REGISTER_MATCHER(isTemplateInstantiation);
   REGISTER_MATCHER(isUnion);
+  REGISTER_MATCHER(isUnsignedInteger);
   REGISTER_MATCHER(isVariadic);
   REGISTER_MATCHER(isVirtual);
   REGISTER_MATCHER(isVirtualAsWritten);
index 28b462e746f0bff37e85211f04f7486a0bf2a74d..82c5139a78f58a0817f4244dc179be489836d81a 100644 (file)
@@ -723,6 +723,18 @@ TEST(IsInteger, ReportsNoFalsePositives) {
                            to(varDecl(hasType(isInteger()))))))));
 }
 
+TEST(IsSignedInteger, MatchesSignedIntegers) {
+  EXPECT_TRUE(matches("int i = 0;", varDecl(hasType(isSignedInteger()))));
+  EXPECT_TRUE(notMatches("unsigned i = 0;",
+                         varDecl(hasType(isSignedInteger()))));
+}
+
+TEST(IsUnsignedInteger, MatchesUnsignedIntegers) {
+  EXPECT_TRUE(notMatches("int i = 0;", varDecl(hasType(isUnsignedInteger()))));
+  EXPECT_TRUE(matches("unsigned i = 0;",
+                      varDecl(hasType(isUnsignedInteger()))));
+}
+
 TEST(IsAnyPointer, MatchesPointers) {
   EXPECT_TRUE(matches("int* i = nullptr;", varDecl(hasType(isAnyPointer()))));
 }