From 5472a52c205ad38808322901eff02defb87e47ab Mon Sep 17 00:00:00 2001 From: Manuel Klimek Date: Tue, 4 Dec 2012 13:40:29 +0000 Subject: [PATCH] Fixes crash in isDerivedFrom for recursive templates. llvm-svn: 169262 --- clang/lib/ASTMatchers/ASTMatchFinder.cpp | 7 ++++++- clang/unittests/ASTMatchers/ASTMatchersTest.cpp | 11 +++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/clang/lib/ASTMatchers/ASTMatchFinder.cpp b/clang/lib/ASTMatchers/ASTMatchFinder.cpp index c13cf4a..7f89550 100644 --- a/clang/lib/ASTMatchers/ASTMatchFinder.cpp +++ b/clang/lib/ASTMatchers/ASTMatchFinder.cpp @@ -605,7 +605,12 @@ bool MatchASTVisitor::classIsDerivedFrom(const CXXRecordDecl *Declaration, ClassDecl = TypeNode->getAsCXXRecordDecl(); } assert(ClassDecl != NULL); - assert(ClassDecl != Declaration); + if (ClassDecl == Declaration) { + // This can happen for recursive template definitions; if the + // current declaration did not match, we can safely return false. + assert(TemplateType); + return false; + } if (Base.matches(*ClassDecl, this, Builder)) return true; if (classIsDerivedFrom(ClassDecl, Base, Builder)) diff --git a/clang/unittests/ASTMatchers/ASTMatchersTest.cpp b/clang/unittests/ASTMatchers/ASTMatchersTest.cpp index f3d377b..f640dff 100644 --- a/clang/unittests/ASTMatchers/ASTMatchersTest.cpp +++ b/clang/unittests/ASTMatchers/ASTMatchersTest.cpp @@ -232,6 +232,17 @@ TEST(DeclarationMatcher, ClassIsDerived) { "template <> class Z {};" "template class Z : public Z, public X {};", ZIsDerivedFromX)); + EXPECT_TRUE( + notMatches("template struct X;" + "template struct X : public X {};", + recordDecl(isDerivedFrom(recordDecl(hasName("Some")))))); + EXPECT_TRUE(matches( + "struct A {};" + "template struct X;" + "template struct X : public X {};" + "template<> struct X<0> : public A {};" + "struct B : public X<42> {};", + recordDecl(hasName("B"), isDerivedFrom(recordDecl(hasName("A")))))); // FIXME: Once we have better matchers for template type matching, // get rid of the Variable(...) matching and match the right template -- 2.7.4