[clang] 16cabf2 - [ASTMatchers] HasNameMatcher handles `extern "C"`
Nathan James via cfe-commits
cfe-commits at lists.llvm.org
Wed Feb 26 14:07:55 PST 2020
Author: Nathan James
Date: 2020-02-26T22:07:47Z
New Revision: 16cabf278fc8c14d415e677ce0bc40d46a6de30d
URL: https://github.com/llvm/llvm-project/commit/16cabf278fc8c14d415e677ce0bc40d46a6de30d
DIFF: https://github.com/llvm/llvm-project/commit/16cabf278fc8c14d415e677ce0bc40d46a6de30d.diff
LOG: [ASTMatchers] HasNameMatcher handles `extern "C"`
Summary: Fixes [[ https://bugs.llvm.org/show_bug.cgi?id=42193 | hasName AST matcher is confused by extern "C" in namespace. ]]
Reviewers: klimek, aaron.ballman, gribozavr2
Reviewed By: aaron.ballman
Subscribers: cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D75202
Added:
Modified:
clang/lib/ASTMatchers/ASTMatchersInternal.cpp
clang/unittests/ASTMatchers/ASTMatchersNarrowingTest.cpp
Removed:
################################################################################
diff --git a/clang/lib/ASTMatchers/ASTMatchersInternal.cpp b/clang/lib/ASTMatchers/ASTMatchersInternal.cpp
index 59c77a167b1f..777400f84974 100644
--- a/clang/lib/ASTMatchers/ASTMatchersInternal.cpp
+++ b/clang/lib/ASTMatchers/ASTMatchersInternal.cpp
@@ -513,7 +513,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 4b9fce9e3107..65fb11caa489 100644
--- a/clang/unittests/ASTMatchers/ASTMatchersNarrowingTest.cpp
+++ b/clang/unittests/ASTMatchers/ASTMatchersNarrowingTest.cpp
@@ -1643,6 +1643,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; } }";
More information about the cfe-commits
mailing list