summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--clang/lib/ASTMatchers/ASTMatchersInternal.cpp8
-rw-r--r--clang/unittests/ASTMatchers/ASTMatchersNarrowingTest.cpp15
2 files changed, 22 insertions, 1 deletions
diff --git a/clang/lib/ASTMatchers/ASTMatchersInternal.cpp b/clang/lib/ASTMatchers/ASTMatchersInternal.cpp
index 199a6d839e2..ae127d77584 100644
--- a/clang/lib/ASTMatchers/ASTMatchersInternal.cpp
+++ b/clang/lib/ASTMatchers/ASTMatchersInternal.cpp
@@ -523,7 +523,13 @@ bool HasNameMatcher::matchesNodeFullFast(const NamedDecl &Node) const {
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;
diff --git a/clang/unittests/ASTMatchers/ASTMatchersNarrowingTest.cpp b/clang/unittests/ASTMatchers/ASTMatchersNarrowingTest.cpp
index 92678a30919..bb8aecfe8b8 100644
--- a/clang/unittests/ASTMatchers/ASTMatchersNarrowingTest.cpp
+++ b/clang/unittests/ASTMatchers/ASTMatchersNarrowingTest.cpp
@@ -1520,6 +1520,21 @@ TEST(Matcher, HasNameSupportsFunctionScope) {
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; } }";
OpenPOWER on IntegriCloud