[PATCH] D75202: [ASTMatchers] HasNameMatcher handles `extern "C"`
Nathan James via Phabricator via cfe-commits
cfe-commits at lists.llvm.org
Wed Feb 26 11:14:46 PST 2020
njames93 created this revision.
njames93 added reviewers: klimek, aaron.ballman, gribozavr2.
Herald added a project: clang.
Herald added a subscriber: cfe-commits.
Fixes hasName AST matcher is confused by extern "C" in namespace. <https://bugs.llvm.org/show_bug.cgi?id=42193>
Repository:
rG LLVM Github Monorepo
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,15 @@
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"))));
+}
+
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;
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D75202.246798.patch
Type: text/x-patch
Size: 1646 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20200226/e79c9c9a/attachment.bin>
More information about the cfe-commits
mailing list