[PATCH] D22222: [ASTMatchers] isSignedInteger() and isUnsignedInteger()

Clement Courbet via cfe-commits cfe-commits at lists.llvm.org
Mon Jul 11 09:03:51 PDT 2016


courbet created this revision.
courbet added a reviewer: aaron.ballman.
courbet added a subscriber: cfe-commits.
Herald added a subscriber: klimek.

I could not find a way to match signedness of integers in the AST, so I'm adding these two matchers.

http://reviews.llvm.org/D22222

Files:
  include/clang/ASTMatchers/ASTMatchers.h
  lib/ASTMatchers/Dynamic/Registry.cpp
  unittests/ASTMatchers/ASTMatchersNarrowingTest.cpp

Index: unittests/ASTMatchers/ASTMatchersNarrowingTest.cpp
===================================================================
--- unittests/ASTMatchers/ASTMatchersNarrowingTest.cpp
+++ unittests/ASTMatchers/ASTMatchersNarrowingTest.cpp
@@ -723,6 +723,18 @@
                            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()))));
 }
Index: lib/ASTMatchers/Dynamic/Registry.cpp
===================================================================
--- lib/ASTMatchers/Dynamic/Registry.cpp
+++ lib/ASTMatchers/Dynamic/Registry.cpp
@@ -321,9 +321,11 @@
   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: include/clang/ASTMatchers/ASTMatchers.h
===================================================================
--- include/clang/ASTMatchers/ASTMatchers.h
+++ include/clang/ASTMatchers/ASTMatchers.h
@@ -4067,6 +4067,34 @@
     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


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D22222.63520.patch
Type: text/x-patch
Size: 2674 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20160711/d23ed10a/attachment.bin>


More information about the cfe-commits mailing list