From: Aaron Ballman Date: Wed, 29 Nov 2017 21:21:51 +0000 (+0000) Subject: Add the hasDefinition() AST matcher to match class declarations that also have a... X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=709440ae21bce19fcde67f4812721fab6c509ece;p=clang Add the hasDefinition() AST matcher to match class declarations that also have a definition. Patch by Julie Hockett. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@319360 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/docs/LibASTMatchersReference.html b/docs/LibASTMatchersReference.html index bcd236adf5..ed1ec193d8 100644 --- a/docs/LibASTMatchersReference.html +++ b/docs/LibASTMatchersReference.html @@ -2307,6 +2307,15 @@ Usable as: Matcher<CXXRecordDecl>hasDefinition +
Matches a class declaration that is defined.
+
+Example matches x (matcher = cxxRecordDecl(hasDefinition()))
+class x {};
+class y;
+
+ + Matcher<CXXRecordDecl>isDerivedFromstd::string BaseName
Overloaded method as shortcut for isDerivedFrom(hasName(...)).
 
diff --git a/include/clang/ASTMatchers/ASTMatchers.h b/include/clang/ASTMatchers/ASTMatchers.h index 93fe9fbb9d..64e7e908d5 100644 --- a/include/clang/ASTMatchers/ASTMatchers.h +++ b/include/clang/ASTMatchers/ASTMatchers.h @@ -5853,6 +5853,17 @@ AST_MATCHER_P(CXXNewExpr, hasArraySize, internal::Matcher, InnerMatcher) { InnerMatcher.matches(*Node.getArraySize(), Finder, Builder); } +/// \brief Matches a class declaration that is defined. +/// +/// Example matches x (matcher = cxxRecordDecl(hasDefinition())) +/// \code +/// class x {}; +/// class y; +/// \endcode +AST_MATCHER(CXXRecordDecl, hasDefinition) { + return Node.hasDefinition(); +} + } // namespace ast_matchers } // namespace clang diff --git a/lib/ASTMatchers/Dynamic/Registry.cpp b/lib/ASTMatchers/Dynamic/Registry.cpp index 52b8d3a6c9..2b7bb7a212 100644 --- a/lib/ASTMatchers/Dynamic/Registry.cpp +++ b/lib/ASTMatchers/Dynamic/Registry.cpp @@ -250,6 +250,7 @@ RegistryMaps::RegistryMaps() { REGISTER_MATCHER(hasDeclContext); REGISTER_MATCHER(hasDeducedType); REGISTER_MATCHER(hasDefaultArgument); + REGISTER_MATCHER(hasDefinition); REGISTER_MATCHER(hasDescendant); REGISTER_MATCHER(hasDestinationType); REGISTER_MATCHER(hasDynamicExceptionSpec); diff --git a/unittests/ASTMatchers/ASTMatchersNarrowingTest.cpp b/unittests/ASTMatchers/ASTMatchersNarrowingTest.cpp index f6b217c0cb..7e7d027071 100644 --- a/unittests/ASTMatchers/ASTMatchersNarrowingTest.cpp +++ b/unittests/ASTMatchers/ASTMatchersNarrowingTest.cpp @@ -2008,5 +2008,26 @@ TEST(HasArraySize, Basic) { cxxNewExpr(hasArraySize(integerLiteral(equals(10)))))); } +TEST(HasDefinition, MatchesStructDefinition) { + EXPECT_TRUE(matches("struct x {};", + cxxRecordDecl(hasDefinition()))); + EXPECT_TRUE(notMatches("struct x;", + cxxRecordDecl(hasDefinition()))); +} + +TEST(HasDefinition, MatchesClassDefinition) { + EXPECT_TRUE(matches("class x {};", + cxxRecordDecl(hasDefinition()))); + EXPECT_TRUE(notMatches("class x;", + cxxRecordDecl(hasDefinition()))); +} + +TEST(HasDefinition, MatchesUnionDefinition) { + EXPECT_TRUE(matches("union x {};", + cxxRecordDecl(hasDefinition()))); + EXPECT_TRUE(notMatches("union x;", + cxxRecordDecl(hasDefinition()))); +} + } // namespace ast_matchers } // namespace clang