From ed455d4062f1a47cd7f060989f85010c967595e5 Mon Sep 17 00:00:00 2001 From: Aaron Ballman Date: Tue, 11 Aug 2015 20:42:00 +0000 Subject: [PATCH] Add an AST matcher to match member intializers of a CXXCtorInitializer. llvm-svn: 244662 --- clang/docs/LibASTMatchersReference.html | 17 ++++++++++++++++ clang/include/clang/ASTMatchers/ASTMatchers.h | 20 +++++++++++++++++++ clang/lib/ASTMatchers/Dynamic/Registry.cpp | 1 + .../unittests/ASTMatchers/ASTMatchersTest.cpp | 6 ++++++ 4 files changed, 44 insertions(+) diff --git a/clang/docs/LibASTMatchersReference.html b/clang/docs/LibASTMatchersReference.html index 4d383817b339..799b47af6fd3 100644 --- a/clang/docs/LibASTMatchersReference.html +++ b/clang/docs/LibASTMatchersReference.html @@ -1512,6 +1512,23 @@ constructorDecl(hasAnyConstructorInitializer(isBaseInitializer())) +Matcher<CXXCtorInitializer>isMemberInitializer +
Matches a constructor initializer if it is initializing a direct member, as opposed to a base.
+
+Given
+  struct B {};
+  struct D : B {
+    int I;
+    D(int i) : I(i) {}
+  };
+  struct E : B {
+    E() : B() {}
+  };
+constructorDecl(hasAnyConstructorInitializer(isMemberInitializer()))
+  will match D(int), but not match E().
+
+ + Matcher<CXXCtorInitializer>isWritten
Matches a constructor initializer if it is explicitly written in
 code (as opposed to implicitly added by the compiler).
diff --git a/clang/include/clang/ASTMatchers/ASTMatchers.h b/clang/include/clang/ASTMatchers/ASTMatchers.h
index 0e5868397054..778e1f4dfe7c 100644
--- a/clang/include/clang/ASTMatchers/ASTMatchers.h
+++ b/clang/include/clang/ASTMatchers/ASTMatchers.h
@@ -2652,6 +2652,26 @@ AST_MATCHER(CXXCtorInitializer, isBaseInitializer) {
   return Node.isBaseInitializer();
 }
 
+/// \brief Matches a constructor initializer if it is initializing a member, as
+/// opposed to a base.
+///
+/// Given
+/// \code
+///   struct B {};
+///   struct D : B {
+///     int I;
+///     D(int i) : I(i) {}
+///   };
+///   struct E : B {
+///     E() : B() {}
+///   };
+/// \endcode
+/// constructorDecl(hasAnyConstructorInitializer(isMemberInitializer()))
+///   will match D(int), but not match E().
+AST_MATCHER(CXXCtorInitializer, isMemberInitializer) {
+  return Node.isMemberInitializer();
+}
+
 /// \brief Matches any argument of a call expression or a constructor call
 /// expression.
 ///
diff --git a/clang/lib/ASTMatchers/Dynamic/Registry.cpp b/clang/lib/ASTMatchers/Dynamic/Registry.cpp
index a98011044797..7e3df2dfd9a3 100644
--- a/clang/lib/ASTMatchers/Dynamic/Registry.cpp
+++ b/clang/lib/ASTMatchers/Dynamic/Registry.cpp
@@ -262,6 +262,7 @@ RegistryMaps::RegistryMaps() {
   REGISTER_MATCHER(isIntegral);
   REGISTER_MATCHER(isInTemplateInstantiation);
   REGISTER_MATCHER(isListInitialization);
+  REGISTER_MATCHER(isMemberInitializer);
   REGISTER_MATCHER(isMoveConstructor);
   REGISTER_MATCHER(isOverride);
   REGISTER_MATCHER(isPrivate);
diff --git a/clang/unittests/ASTMatchers/ASTMatchersTest.cpp b/clang/unittests/ASTMatchers/ASTMatchersTest.cpp
index 338325999118..d51a5e28b5e5 100644
--- a/clang/unittests/ASTMatchers/ASTMatchersTest.cpp
+++ b/clang/unittests/ASTMatchers/ASTMatchersTest.cpp
@@ -2097,6 +2097,12 @@ TEST(HasAnyConstructorInitializer, IsBaseInitializer) {
   EXPECT_TRUE(notMatches(Code, constructorDecl(allOf(
     hasAnyConstructorInitializer(allOf(isBaseInitializer(), isWritten())),
     hasName("D")))));
+  EXPECT_TRUE(matches(Code, constructorDecl(allOf(
+    hasAnyConstructorInitializer(allOf(isMemberInitializer(), isWritten())),
+    hasName("D")))));
+  EXPECT_TRUE(notMatches(Code, constructorDecl(allOf(
+    hasAnyConstructorInitializer(allOf(isMemberInitializer(), isWritten())),
+    hasName("E")))));
 }
 
 TEST(Matcher, NewExpression) {