This revision was automatically updated to reflect the committed changes. Closed by commit rG16cabf278fc8: [ASTMatchers] HasNameMatcher handles `extern "C"` (authored by njames93).
Repository: rG LLVM Github Monorepo CHANGES SINCE LAST ACTION https://reviews.llvm.org/D75202/new/ https://reviews.llvm.org/D75202 Files: clang/lib/ASTMatchers/ASTMatchersInternal.cpp clang/unittests/ASTMatchers/ASTMatchersNarrowingTest.cpp Index: clang/unittests/ASTMatchers/ASTMatchersNarrowingTest.cpp =================================================================== --- clang/unittests/ASTMatchers/ASTMatchersNarrowingTest.cpp +++ clang/unittests/ASTMatchers/ASTMatchersNarrowingTest.cpp @@ -1643,6 +1643,21 @@ EXPECT_TRUE(matches(code, fieldDecl(hasName("::a::F(int)::S::m")))); } +TEST(Matcher, HasNameQualifiedSupportsLinkage) { + // https://bugs.llvm.org/show_bug.cgi?id=42193 + std::string code = R"cpp(namespace foo { extern "C" void test(); })cpp"; + EXPECT_TRUE(matches(code, functionDecl(hasName("test")))); + EXPECT_TRUE(matches(code, functionDecl(hasName("foo::test")))); + EXPECT_TRUE(matches(code, functionDecl(hasName("::foo::test")))); + EXPECT_TRUE(notMatches(code, functionDecl(hasName("::test")))); + + code = R"cpp(namespace foo { extern "C" { void test(); } })cpp"; + EXPECT_TRUE(matches(code, functionDecl(hasName("test")))); + EXPECT_TRUE(matches(code, functionDecl(hasName("foo::test")))); + EXPECT_TRUE(matches(code, functionDecl(hasName("::foo::test")))); + EXPECT_TRUE(notMatches(code, functionDecl(hasName("::test")))); +} + TEST(Matcher, HasAnyName) { const std::string Code = "namespace a { namespace b { class C; } }"; Index: clang/lib/ASTMatchers/ASTMatchersInternal.cpp =================================================================== --- clang/lib/ASTMatchers/ASTMatchersInternal.cpp +++ clang/lib/ASTMatchers/ASTMatchersInternal.cpp @@ -513,7 +513,13 @@ if (Ctx->isFunctionOrMethod()) return Patterns.foundMatch(/*AllowFullyQualified=*/false); - for (; Ctx && isa<NamedDecl>(Ctx); Ctx = Ctx->getParent()) { + for (; Ctx; Ctx = Ctx->getParent()) { + // Linkage Spec can just be ignored + // FIXME: Any other DeclContext kinds that can be safely disregarded + if (isa<LinkageSpecDecl>(Ctx)) + continue; + if (!isa<NamedDecl>(Ctx)) + break; if (Patterns.foundMatch(/*AllowFullyQualified=*/false)) return true;
Index: clang/unittests/ASTMatchers/ASTMatchersNarrowingTest.cpp =================================================================== --- clang/unittests/ASTMatchers/ASTMatchersNarrowingTest.cpp +++ clang/unittests/ASTMatchers/ASTMatchersNarrowingTest.cpp @@ -1643,6 +1643,21 @@ EXPECT_TRUE(matches(code, fieldDecl(hasName("::a::F(int)::S::m")))); } +TEST(Matcher, HasNameQualifiedSupportsLinkage) { + // https://bugs.llvm.org/show_bug.cgi?id=42193 + std::string code = R"cpp(namespace foo { extern "C" void test(); })cpp"; + EXPECT_TRUE(matches(code, functionDecl(hasName("test")))); + EXPECT_TRUE(matches(code, functionDecl(hasName("foo::test")))); + EXPECT_TRUE(matches(code, functionDecl(hasName("::foo::test")))); + EXPECT_TRUE(notMatches(code, functionDecl(hasName("::test")))); + + code = R"cpp(namespace foo { extern "C" { void test(); } })cpp"; + EXPECT_TRUE(matches(code, functionDecl(hasName("test")))); + EXPECT_TRUE(matches(code, functionDecl(hasName("foo::test")))); + EXPECT_TRUE(matches(code, functionDecl(hasName("::foo::test")))); + EXPECT_TRUE(notMatches(code, functionDecl(hasName("::test")))); +} + TEST(Matcher, HasAnyName) { const std::string Code = "namespace a { namespace b { class C; } }"; Index: clang/lib/ASTMatchers/ASTMatchersInternal.cpp =================================================================== --- clang/lib/ASTMatchers/ASTMatchersInternal.cpp +++ clang/lib/ASTMatchers/ASTMatchersInternal.cpp @@ -513,7 +513,13 @@ if (Ctx->isFunctionOrMethod()) return Patterns.foundMatch(/*AllowFullyQualified=*/false); - for (; Ctx && isa<NamedDecl>(Ctx); Ctx = Ctx->getParent()) { + for (; Ctx; Ctx = Ctx->getParent()) { + // Linkage Spec can just be ignored + // FIXME: Any other DeclContext kinds that can be safely disregarded + if (isa<LinkageSpecDecl>(Ctx)) + continue; + if (!isa<NamedDecl>(Ctx)) + break; if (Patterns.foundMatch(/*AllowFullyQualified=*/false)) return true;
_______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits