From: Clement Courbet Date: Tue, 12 Jul 2016 06:36:00 +0000 (+0000) Subject: [ASTMatchers] isSignedInteger() and isUnsignedInteger() X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=7df5d83622a25d1c25be9bf5dbe214e8a09f2d86;p=clang [ASTMatchers] isSignedInteger() and isUnsignedInteger() 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 --- diff --git a/docs/LibASTMatchersReference.html b/docs/LibASTMatchersReference.html index a8a47d5b54..70c83ea5cc 100644 --- a/docs/LibASTMatchersReference.html +++ b/docs/LibASTMatchersReference.html @@ -2968,6 +2968,30 @@ matches "a(int)", "b(long)", but not "c(double)". +Matcher<QualType>isSignedInteger +
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)".
+
+ + +Matcher<QualType>isUnsignedInteger +
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)".
+
+ + Matcher<QualType>isVolatileQualified
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
diff --git a/include/clang/ASTMatchers/ASTMatchers.h b/include/clang/ASTMatchers/ASTMatchers.h
index 28449eaacd..aef4b4eafd 100644
--- a/include/clang/ASTMatchers/ASTMatchers.h
+++ b/include/clang/ASTMatchers/ASTMatchers.h
@@ -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
diff --git a/lib/ASTMatchers/Dynamic/Registry.cpp b/lib/ASTMatchers/Dynamic/Registry.cpp
index d8ddf55733..a8d4b88d85 100644
--- a/lib/ASTMatchers/Dynamic/Registry.cpp
+++ b/lib/ASTMatchers/Dynamic/Registry.cpp
@@ -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);
diff --git a/unittests/ASTMatchers/ASTMatchersNarrowingTest.cpp b/unittests/ASTMatchers/ASTMatchersNarrowingTest.cpp
index 28b462e746..82c5139a78 100644
--- a/unittests/ASTMatchers/ASTMatchersNarrowingTest.cpp
+++ b/unittests/ASTMatchers/ASTMatchersNarrowingTest.cpp
@@ -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()))));
 }