From 190bb71b7c8d7f8861078825374c7fc4e6827ca8 Mon Sep 17 00:00:00 2001 From: Anton Bikineev Date: Thu, 25 Jul 2019 14:48:55 +0000 Subject: [PATCH] [clang] Fail for empty names in is*DerivedFrom matchers. Differential Revision: https://reviews.llvm.org/D65279 git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@367022 91177308-0d34-0410-b5e6-96231b3b80d8 --- include/clang/ASTMatchers/ASTMatchers.h | 9 ++++++--- unittests/ASTMatchers/ASTMatchersNarrowingTest.cpp | 7 +++++++ 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/include/clang/ASTMatchers/ASTMatchers.h b/include/clang/ASTMatchers/ASTMatchers.h index a61358ea0e..724d08c3d8 100644 --- a/include/clang/ASTMatchers/ASTMatchers.h +++ b/include/clang/ASTMatchers/ASTMatchers.h @@ -2639,7 +2639,8 @@ AST_MATCHER_P(CXXRecordDecl, isDerivedFrom, /// Overloaded method as shortcut for \c isDerivedFrom(hasName(...)). AST_MATCHER_P_OVERLOAD(CXXRecordDecl, isDerivedFrom, std::string, BaseName, 1) { - assert(!BaseName.empty()); + if (BaseName.empty()) + return false; return isDerivedFrom(hasName(BaseName)).matches(Node, Finder, Builder); } @@ -2655,7 +2656,8 @@ AST_MATCHER_P_OVERLOAD(CXXRecordDecl, isSameOrDerivedFrom, /// \c isSameOrDerivedFrom(hasName(...)). AST_MATCHER_P_OVERLOAD(CXXRecordDecl, isSameOrDerivedFrom, std::string, BaseName, 1) { - assert(!BaseName.empty()); + if (BaseName.empty()) + return false; return isSameOrDerivedFrom(hasName(BaseName)).matches(Node, Finder, Builder); } @@ -2687,7 +2689,8 @@ AST_MATCHER_P_OVERLOAD(CXXRecordDecl, isDirectlyDerivedFrom, /// Overloaded method as shortcut for \c isDirectlyDerivedFrom(hasName(...)). AST_MATCHER_P_OVERLOAD(CXXRecordDecl, isDirectlyDerivedFrom, std::string, BaseName, 1) { - assert(!BaseName.empty()); + if (BaseName.empty()) + return false; return isDirectlyDerivedFrom(hasName(BaseName)) .matches(Node, Finder, Builder); } diff --git a/unittests/ASTMatchers/ASTMatchersNarrowingTest.cpp b/unittests/ASTMatchers/ASTMatchersNarrowingTest.cpp index 0fedb0c8d6..cb5cf9ebf5 100644 --- a/unittests/ASTMatchers/ASTMatchersNarrowingTest.cpp +++ b/unittests/ASTMatchers/ASTMatchersNarrowingTest.cpp @@ -566,6 +566,13 @@ TEST(DeclarationMatcher, ClassIsDerived) { cxxRecordDecl(isDerivedFrom(namedDecl(hasName("X")))))); } +TEST(DeclarationMatcher, IsDerivedFromEmptyName) { + const char *const Code = "class X {}; class Y : public X {};"; + EXPECT_TRUE(notMatches(Code, cxxRecordDecl(isDerivedFrom("")))); + EXPECT_TRUE(notMatches(Code, cxxRecordDecl(isDirectlyDerivedFrom("")))); + EXPECT_TRUE(notMatches(Code, cxxRecordDecl(isSameOrDerivedFrom("")))); +} + TEST(DeclarationMatcher, IsLambda) { const auto IsLambda = cxxMethodDecl(ofClass(cxxRecordDecl(isLambda()))); EXPECT_TRUE(matches("auto x = []{};", IsLambda)); -- 2.40.0