From 005df9362ed578b99a6a545449a7802b9fb8cece Mon Sep 17 00:00:00 2001 From: Aaron Ballman Date: Thu, 18 Feb 2016 16:36:01 +0000 Subject: [PATCH] Add an AST matcher for real floating-point types. e.g., float, double, long double, but not complex. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@261221 91177308-0d34-0410-b5e6-96231b3b80d8 --- docs/LibASTMatchersReference.html | 58 ++++++++++++++++------- include/clang/ASTMatchers/ASTMatchers.h | 13 +++++ lib/ASTMatchers/Dynamic/Registry.cpp | 1 + unittests/ASTMatchers/ASTMatchersTest.cpp | 9 ++++ 4 files changed, 63 insertions(+), 18 deletions(-) diff --git a/docs/LibASTMatchersReference.html b/docs/LibASTMatchersReference.html index fc4870433c..4dabc4b454 100644 --- a/docs/LibASTMatchersReference.html +++ b/docs/LibASTMatchersReference.html @@ -2176,23 +2176,6 @@ fieldDecl(isPublic()) -Matcher<Expr>nullPointerConstant -
Matches expressions that resolve to a null pointer constant, such as
-GNU's __null, C++11's nullptr, or C's NULL macro.
-
-Given:
-  void *v1 = NULL;
-  void *v2 = nullptr;
-  void *v3 = __null; GNU extension
-  char *cp = (char *)0;
-  int *ip = 0;
-  int i = 0;
-expr(nullPointerConstant())
-  matches the initializer for v1, v2, v3, cp, and ip. Does not match the
-  initializer for i.
-
- - Matcher<FloatingLiteral>equalsValueT Value
Matches literals that are equal to the given value.
 
@@ -2429,7 +2412,7 @@ memberExpr(isArrow())
 
-Matcher<NamedDecl>hasNamestd::string Name +Matcher<NamedDecl>hasNamestd::string Name
Matches NamedDecl nodes that have the specified name.
 
 Supports specifying enclosing namespaces or classes by prefixing the name
@@ -2621,6 +2604,17 @@ matches "a(char)", "b(wchar_t)", but not "c(double)".
 
+Matcher<QualType>isAnyPointer +
Matches QualType nodes that are of any pointer type.
+
+Given
+  int *i = nullptr;
+  int j;
+varDecl(hasType(isAnyPointer()))
+  matches "int *i", but not "int j".
+
+ + Matcher<QualType>isConstQualified
Matches QualType nodes that are const-qualified, i.e., that
 include "top-level" const.
@@ -2894,6 +2888,17 @@ and reference to that variable declaration within a compound statement.
 
+Matcher<Type>realFloatingPointType +
Matches any real floating-point type (float, double, long double).
+
+Given
+  int i;
+  float f;
+realFloatingPointType()
+  matches "float f" but not "int i"
+
+ + Matcher<Type>voidType
Matches type void.
 
@@ -3080,6 +3085,23 @@ functionDecl(isInstantiated())
 
+Matcher<internal::Matcher<Expr>>nullPointerConstant +
Matches expressions that resolve to a null pointer constant, such as
+GNU's __null, C++11's nullptr, or C's NULL macro.
+
+Given:
+  void *v1 = NULL;
+  void *v2 = nullptr;
+  void *v3 = __null; GNU extension
+  char *cp = (char *)0;
+  int *ip = 0;
+  int i = 0;
+expr(nullPointerConstant())
+  matches the initializer for v1, v2, v3, cp, and ip. Does not match the
+  initializer for i.
+
+ + Matcher<internal::Matcher<Stmt>>isInTemplateInstantiation
Matches statements inside of a template instantiation.
 
diff --git a/include/clang/ASTMatchers/ASTMatchers.h b/include/clang/ASTMatchers/ASTMatchers.h
index 873cef48af..9759d638f3 100644
--- a/include/clang/ASTMatchers/ASTMatchers.h
+++ b/include/clang/ASTMatchers/ASTMatchers.h
@@ -3961,6 +3961,19 @@ AST_TYPE_MATCHER(ArrayType, arrayType);
 ///   matches "_Complex float f"
 AST_TYPE_MATCHER(ComplexType, complexType);
 
+/// \brief Matches any real floating-point type (float, double, long double).
+///
+/// Given
+/// \code
+///   int i;
+///   float f;
+/// \endcode
+/// realFloatingPointType()
+///   matches "float f" but not "int i"
+AST_MATCHER(Type, realFloatingPointType) {
+  return Node.isRealFloatingType();
+}
+
 /// \brief Matches arrays and C99 complex types that have a specific element
 /// type.
 ///
diff --git a/lib/ASTMatchers/Dynamic/Registry.cpp b/lib/ASTMatchers/Dynamic/Registry.cpp
index 07c2298d34..24f7e98127 100644
--- a/lib/ASTMatchers/Dynamic/Registry.cpp
+++ b/lib/ASTMatchers/Dynamic/Registry.cpp
@@ -342,6 +342,7 @@ RegistryMaps::RegistryMaps() {
   REGISTER_MATCHER(pointee);
   REGISTER_MATCHER(pointerType);
   REGISTER_MATCHER(qualType);
+  REGISTER_MATCHER(realFloatingPointType);
   REGISTER_MATCHER(recordDecl);
   REGISTER_MATCHER(recordType);
   REGISTER_MATCHER(referenceType);
diff --git a/unittests/ASTMatchers/ASTMatchersTest.cpp b/unittests/ASTMatchers/ASTMatchersTest.cpp
index 9b586ab580..eb871f91de 100644
--- a/unittests/ASTMatchers/ASTMatchersTest.cpp
+++ b/unittests/ASTMatchers/ASTMatchersTest.cpp
@@ -4415,6 +4415,15 @@ TEST(TypeMatching, MatchesVoid) {
                       cxxMethodDecl(returns(voidType()))));
 }
 
+TEST(TypeMatching, MatchesRealFloats) {
+  EXPECT_TRUE(matches("struct S { float func(); };",
+                      cxxMethodDecl(returns(realFloatingPointType()))));
+  EXPECT_TRUE(notMatches("struct S { int func(); };",
+                         cxxMethodDecl(returns(realFloatingPointType()))));
+  EXPECT_TRUE(matches("struct S { long double func(); };",
+                      cxxMethodDecl(returns(realFloatingPointType()))));
+}
+
 TEST(TypeMatching, MatchesArrayTypes) {
   EXPECT_TRUE(matches("int a[] = {2,3};", arrayType()));
   EXPECT_TRUE(matches("int a[42];", arrayType()));
-- 
2.40.0