Skip to content

Commit

Permalink
[clang] Add isDirectlyDerivedFrom AST matcher.
Browse files Browse the repository at this point in the history
Differential Revision: https://reviews.llvm.org/D65092

llvm-svn: 367010
  • Loading branch information
AntonBikineev committed Jul 25, 2019
1 parent ec67e73 commit 4e1d188
Show file tree
Hide file tree
Showing 6 changed files with 97 additions and 6 deletions.
25 changes: 25 additions & 0 deletions clang/docs/LibASTMatchersReference.html
Expand Up @@ -2581,6 +2581,11 @@ <h2 id="narrowing-matchers">Narrowing Matchers</h2>
</pre></td></tr>


<tr><td>Matcher&lt;<a href="https://clang.llvm.org/doxygen/classclang_1_1CXXRecordDecl.html">CXXRecordDecl</a>&gt;</td><td class="name" onclick="toggle('isDirectlyDerivedFrom1')"><a name="isDirectlyDerivedFrom1Anchor">isDirectlyDerivedFrom</a></td><td>std::string BaseName</td></tr>
<tr><td colspan="4" class="doc" id="isDirectlyDerivedFrom1"><pre>Overloaded method as shortcut for isDirectlyDerivedFrom(hasName(...)).
</pre></td></tr>


<tr><td>Matcher&lt;<a href="https://clang.llvm.org/doxygen/classclang_1_1CXXRecordDecl.html">CXXRecordDecl</a>&gt;</td><td class="name" onclick="toggle('isExplicitTemplateSpecialization2')"><a name="isExplicitTemplateSpecialization2Anchor">isExplicitTemplateSpecialization</a></td><td></td></tr>
<tr><td colspan="4" class="doc" id="isExplicitTemplateSpecialization2"><pre>Matches explicit template specializations of function, class, or
static member variable template instantiations.
Expand Down Expand Up @@ -5263,6 +5268,26 @@ <h2 id="traversal-matchers">AST Traversal Matchers</h2>
</pre></td></tr>


<tr><td>Matcher&lt;<a href="https://clang.llvm.org/doxygen/classclang_1_1CXXRecordDecl.html">CXXRecordDecl</a>&gt;</td><td class="name" onclick="toggle('isDirectlyDerivedFrom0')"><a name="isDirectlyDerivedFrom0Anchor">isDirectlyDerivedFrom</a></td><td>Matcher&lt;<a href="https://clang.llvm.org/doxygen/classclang_1_1NamedDecl.html">NamedDecl</a>&gt; Base</td></tr>
<tr><td colspan="4" class="doc" id="isDirectlyDerivedFrom0"><pre>Matches C++ classes that are directly derived from a class matching Base.

Note that a class is not considered to be derived from itself.

Example matches Y, C (Base == hasName("X"))
class X;
class Y : public X {}; // directly derived
class Z : public Y {}; // indirectly derived
typedef X A;
typedef A B;
class C : public B {}; // derived from a typedef of X

In the following example, Bar matches isDerivedFrom(hasName("X")):
class Foo;
typedef Foo X;
class Bar : public Foo {}; // derived from a type that X is a typedef of
</pre></td></tr>


<tr><td>Matcher&lt;<a href="https://clang.llvm.org/doxygen/classclang_1_1CXXRecordDecl.html">CXXRecordDecl</a>&gt;</td><td class="name" onclick="toggle('isSameOrDerivedFrom0')"><a name="isSameOrDerivedFrom0Anchor">isSameOrDerivedFrom</a></td><td>Matcher&lt;<a href="https://clang.llvm.org/doxygen/classclang_1_1NamedDecl.html">NamedDecl</a>&gt; Base</td></tr>
<tr><td colspan="4" class="doc" id="isSameOrDerivedFrom0"><pre>Similar to isDerivedFrom(), but also matches classes that directly
match Base.
Expand Down
34 changes: 33 additions & 1 deletion clang/include/clang/ASTMatchers/ASTMatchers.h
Expand Up @@ -2634,7 +2634,7 @@ hasOverloadedOperatorName(StringRef Name) {
/// \endcode
AST_MATCHER_P(CXXRecordDecl, isDerivedFrom,
internal::Matcher<NamedDecl>, Base) {
return Finder->classIsDerivedFrom(&Node, Base, Builder);
return Finder->classIsDerivedFrom(&Node, Base, Builder, /*Directly=*/false);
}

/// Overloaded method as shortcut for \c isDerivedFrom(hasName(...)).
Expand All @@ -2659,6 +2659,38 @@ AST_MATCHER_P_OVERLOAD(CXXRecordDecl, isSameOrDerivedFrom, std::string,
return isSameOrDerivedFrom(hasName(BaseName)).matches(Node, Finder, Builder);
}

/// Matches C++ classes that are directly derived from a class matching \c Base.
///
/// Note that a class is not considered to be derived from itself.
///
/// Example matches Y, C (Base == hasName("X"))
/// \code
/// class X;
/// class Y : public X {}; // directly derived
/// class Z : public Y {}; // indirectly derived
/// typedef X A;
/// typedef A B;
/// class C : public B {}; // derived from a typedef of X
/// \endcode
///
/// In the following example, Bar matches isDerivedFrom(hasName("X")):
/// \code
/// class Foo;
/// typedef Foo X;
/// class Bar : public Foo {}; // derived from a type that X is a typedef of
/// \endcode
AST_MATCHER_P_OVERLOAD(CXXRecordDecl, isDirectlyDerivedFrom,
internal::Matcher<NamedDecl>, Base, 0) {
return Finder->classIsDerivedFrom(&Node, Base, Builder, /*Directly=*/true);
}

/// Overloaded method as shortcut for \c isDirectlyDerivedFrom(hasName(...)).
AST_MATCHER_P_OVERLOAD(CXXRecordDecl, isDirectlyDerivedFrom, std::string,
BaseName, 1) {
assert(!BaseName.empty());
return isDirectlyDerivedFrom(hasName(BaseName))
.matches(Node, Finder, Builder);
}
/// Matches the first method of a class or struct that satisfies \c
/// InnerMatcher.
///
Expand Down
5 changes: 3 additions & 2 deletions clang/include/clang/ASTMatchers/ASTMatchersInternal.h
Expand Up @@ -974,10 +974,11 @@ class ASTMatchFinder {
/// Returns true if the given class is directly or indirectly derived
/// from a base type matching \c base.
///
/// A class is considered to be also derived from itself.
/// A class is not considered to be derived from itself.
virtual bool classIsDerivedFrom(const CXXRecordDecl *Declaration,
const Matcher<NamedDecl> &Base,
BoundNodesTreeBuilder *Builder) = 0;
BoundNodesTreeBuilder *Builder,
bool Directly) = 0;

template <typename T>
bool matchesChildOf(const T &Node, const DynTypedMatcher &Matcher,
Expand Down
8 changes: 5 additions & 3 deletions clang/lib/ASTMatchers/ASTMatchFinder.cpp
Expand Up @@ -430,7 +430,8 @@ class MatchASTVisitor : public RecursiveASTVisitor<MatchASTVisitor>,

bool classIsDerivedFrom(const CXXRecordDecl *Declaration,
const Matcher<NamedDecl> &Base,
BoundNodesTreeBuilder *Builder) override;
BoundNodesTreeBuilder *Builder,
bool Directly) override;

// Implements ASTMatchFinder::matchesChildOf.
bool matchesChildOf(const ast_type_traits::DynTypedNode &Node,
Expand Down Expand Up @@ -817,7 +818,8 @@ getAsCXXRecordDeclOrPrimaryTemplate(const Type *TypeNode) {
// derived from itself.
bool MatchASTVisitor::classIsDerivedFrom(const CXXRecordDecl *Declaration,
const Matcher<NamedDecl> &Base,
BoundNodesTreeBuilder *Builder) {
BoundNodesTreeBuilder *Builder,
bool Directly) {
if (!Declaration->hasDefinition())
return false;
for (const auto &It : Declaration->bases()) {
Expand All @@ -842,7 +844,7 @@ bool MatchASTVisitor::classIsDerivedFrom(const CXXRecordDecl *Declaration,
*Builder = std::move(Result);
return true;
}
if (classIsDerivedFrom(ClassDecl, Base, Builder))
if (!Directly && classIsDerivedFrom(ClassDecl, Base, Builder, Directly))
return true;
}
return false;
Expand Down
1 change: 1 addition & 0 deletions clang/lib/ASTMatchers/Dynamic/Registry.cpp
Expand Up @@ -108,6 +108,7 @@ RegistryMaps::RegistryMaps() {
REGISTER_OVERLOADED_2(hasType);
REGISTER_OVERLOADED_2(ignoringParens);
REGISTER_OVERLOADED_2(isDerivedFrom);
REGISTER_OVERLOADED_2(isDirectlyDerivedFrom);
REGISTER_OVERLOADED_2(isSameOrDerivedFrom);
REGISTER_OVERLOADED_2(loc);
REGISTER_OVERLOADED_2(pointsTo);
Expand Down
30 changes: 30 additions & 0 deletions clang/unittests/ASTMatchers/ASTMatchersNarrowingTest.cpp
Expand Up @@ -331,6 +331,16 @@ TEST(DeclarationMatcher, ClassIsDerived) {
EXPECT_TRUE(notMatches("class Y;", IsDerivedFromX));
EXPECT_TRUE(notMatches("", IsDerivedFromX));

DeclarationMatcher IsDirectlyDerivedFromX =
cxxRecordDecl(isDirectlyDerivedFrom("X"));

EXPECT_TRUE(
matches("class X {}; class Y : public X {};", IsDirectlyDerivedFromX));
EXPECT_TRUE(notMatches("class X {};", IsDirectlyDerivedFromX));
EXPECT_TRUE(notMatches("class X;", IsDirectlyDerivedFromX));
EXPECT_TRUE(notMatches("class Y;", IsDirectlyDerivedFromX));
EXPECT_TRUE(notMatches("", IsDirectlyDerivedFromX));

DeclarationMatcher IsAX = cxxRecordDecl(isSameOrDerivedFrom("X"));

EXPECT_TRUE(matches("class X {}; class Y : public X {};", IsAX));
Expand All @@ -341,13 +351,22 @@ TEST(DeclarationMatcher, ClassIsDerived) {

DeclarationMatcher ZIsDerivedFromX =
cxxRecordDecl(hasName("Z"), isDerivedFrom("X"));
DeclarationMatcher ZIsDirectlyDerivedFromX =
cxxRecordDecl(hasName("Z"), isDirectlyDerivedFrom("X"));
EXPECT_TRUE(
matches("class X {}; class Y : public X {}; class Z : public Y {};",
ZIsDerivedFromX));
EXPECT_TRUE(
notMatches("class X {}; class Y : public X {}; class Z : public Y {};",
ZIsDirectlyDerivedFromX));
EXPECT_TRUE(
matches("class X {};"
"template<class T> class Y : public X {};"
"class Z : public Y<int> {};", ZIsDerivedFromX));
EXPECT_TRUE(notMatches("class X {};"
"template<class T> class Y : public X {};"
"class Z : public Y<int> {};",
ZIsDirectlyDerivedFromX));
EXPECT_TRUE(matches("class X {}; template<class T> class Z : public X {};",
ZIsDerivedFromX));
EXPECT_TRUE(
Expand Down Expand Up @@ -411,6 +430,9 @@ TEST(DeclarationMatcher, ClassIsDerived) {
matches("class X {}; class Y : public X {}; "
"typedef Y V; typedef V W; class Z : public W {};",
ZIsDerivedFromX));
EXPECT_TRUE(notMatches("class X {}; class Y : public X {}; "
"typedef Y V; typedef V W; class Z : public W {};",
ZIsDirectlyDerivedFromX));
EXPECT_TRUE(
matches("template<class T, class U> class X {}; "
"template<class T> class A { class Z : public X<T, int> {}; };",
Expand Down Expand Up @@ -467,6 +489,14 @@ TEST(DeclarationMatcher, ClassIsDerived) {
"template<> struct X<0> : public A {};"
"struct B : public X<42> {};",
cxxRecordDecl(hasName("B"), isDerivedFrom(recordDecl(hasName("A"))))));
EXPECT_TRUE(notMatches(
"struct A {};"
"template<int> struct X;"
"template<int i> struct X : public X<i-1> {};"
"template<> struct X<0> : public A {};"
"struct B : public X<42> {};",
cxxRecordDecl(hasName("B"),
isDirectlyDerivedFrom(recordDecl(hasName("A"))))));

// FIXME: Once we have better matchers for template type matching,
// get rid of the Variable(...) matching and match the right template
Expand Down

0 comments on commit 4e1d188

Please sign in to comment.