From aa6f7e6671996c85b2b8a7651c62928330f428b2 Mon Sep 17 00:00:00 2001 From: Haojian Wu Date: Thu, 30 Jun 2016 07:50:01 +0000 Subject: [PATCH] [ASTMatcher] Add a node matcher for EnumType. Reviewers: aaron.ballman Subscribers: cfe-commits, klimek Differential Revision: http://reviews.llvm.org/D21860 git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@274217 91177308-0d34-0410-b5e6-96231b3b80d8 --- docs/LibASTMatchersReference.html | 15 +++++++++++++++ include/clang/ASTMatchers/ASTMatchers.h | 15 +++++++++++++++ lib/ASTMatchers/Dynamic/Registry.cpp | 1 + unittests/ASTMatchers/ASTMatchersNodeTest.cpp | 7 +++++++ 4 files changed, 38 insertions(+) diff --git a/docs/LibASTMatchersReference.html b/docs/LibASTMatchersReference.html index b6ad1db4cd..6f06b9164a 100644 --- a/docs/LibASTMatchersReference.html +++ b/docs/LibASTMatchersReference.html @@ -1448,6 +1448,21 @@ c and d. +Matcher<Type>enumTypeMatcher<EnumType>... +
Matches enum types.
+
+Given
+  enum C { Green };
+  enum S { Red };
+
+  C c;
+  S s;
+
+enumType() matches the type of the variable declarations of both c and
+s.
+
+ + Matcher<Type>functionProtoTypeMatcher<FunctionProtoType>...
Matches FunctionProtoType nodes.
 
diff --git a/include/clang/ASTMatchers/ASTMatchers.h b/include/clang/ASTMatchers/ASTMatchers.h
index c7d10859a5..23d2f84f67 100644
--- a/include/clang/ASTMatchers/ASTMatchers.h
+++ b/include/clang/ASTMatchers/ASTMatchers.h
@@ -4654,6 +4654,21 @@ AST_TYPELOC_TRAVERSE_MATCHER(pointee, getPointee,
 ///   matches "typedef int X"
 AST_TYPE_MATCHER(TypedefType, typedefType);
 
+/// \brief Matches enum types.
+///
+/// Given
+/// \code
+///   enum C { Green };
+///   enum class S { Red };
+///
+///   C c;
+///   S s;
+/// \endcode
+//
+/// \c enumType() matches the type of the variable declarations of both \c c and
+/// \c s.
+AST_TYPE_MATCHER(EnumType, enumType);
+
 /// \brief Matches template specialization types.
 ///
 /// Given
diff --git a/lib/ASTMatchers/Dynamic/Registry.cpp b/lib/ASTMatchers/Dynamic/Registry.cpp
index a2d9eee054..b6479b0931 100644
--- a/lib/ASTMatchers/Dynamic/Registry.cpp
+++ b/lib/ASTMatchers/Dynamic/Registry.cpp
@@ -171,6 +171,7 @@ RegistryMaps::RegistryMaps() {
   REGISTER_MATCHER(elaboratedType);
   REGISTER_MATCHER(enumConstantDecl);
   REGISTER_MATCHER(enumDecl);
+  REGISTER_MATCHER(enumType);
   REGISTER_MATCHER(equalsBoundNode);
   REGISTER_MATCHER(equalsIntegralValue);
   REGISTER_MATCHER(explicitCastExpr);
diff --git a/unittests/ASTMatchers/ASTMatchersNodeTest.cpp b/unittests/ASTMatchers/ASTMatchersNodeTest.cpp
index c659c6a002..bf2c08a5aa 100644
--- a/unittests/ASTMatchers/ASTMatchersNodeTest.cpp
+++ b/unittests/ASTMatchers/ASTMatchersNodeTest.cpp
@@ -1299,6 +1299,13 @@ TEST(TypeMatching, AutoRefTypes) {
                                         hasType(rValueReferenceType()))));
 }
 
+TEST(TypeMatching, MatchesEnumTypes) {
+  EXPECT_TRUE(matches("enum Color { Green }; Color color;",
+                      loc(enumType())));
+  EXPECT_TRUE(matches("enum class Color { Green }; Color color;",
+                      loc(enumType())));
+}
+
 TEST(TypeMatching, MatchesPointersToConstTypes) {
   EXPECT_TRUE(matches("int b; int * const a = &b;",
                       loc(pointerType())));
-- 
2.40.0