[PATCH] D75202: [ASTMatchers] HasNameMatcher handles `extern "C"`

2020-02-26 Thread Nathan James via Phabricator via cfe-commits
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(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(Ctx))
+  continue;
+if (!isa(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(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(Ctx))
+  continue;
+if (!isa(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


[PATCH] D75202: [ASTMatchers] HasNameMatcher handles `extern "C"`

2020-02-26 Thread Nathan James via Phabricator via cfe-commits
njames93 marked an inline comment as done.
njames93 added inline comments.



Comment at: clang/lib/ASTMatchers/ASTMatchersInternal.cpp:518
+// Linkage Spec can just be ignored
+// FIXME: Any other DeclContext kinds that can be safely disregarded
+if (isa(Ctx))

aaron.ballman wrote:
> I would imagine we'd want to skip an `ExternCContextDecl` for similar 
> reasons, though I am struggling to find a test case that actually generates 
> that context.
I couldn't figure a way to generate one of those either. However if a context 
type that isnt found matched the assertions should go off and we can look into 
the issue again


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D75202/new/

https://reviews.llvm.org/D75202



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D75202: [ASTMatchers] HasNameMatcher handles `extern "C"`

2020-02-26 Thread Nathan James via Phabricator via cfe-commits
njames93 updated this revision to Diff 246829.
njames93 marked an inline comment as done.
njames93 added a comment.

- Add extra test case


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(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(Ctx))
+  continue;
+if (!isa(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(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(Ctx))
+  continue;
+if (!isa(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


[PATCH] D75202: [ASTMatchers] HasNameMatcher handles `extern "C"`

2020-02-26 Thread Aaron Ballman via Phabricator via cfe-commits
aaron.ballman accepted this revision.
aaron.ballman added a comment.
This revision is now accepted and ready to land.

LGTM!




Comment at: clang/lib/ASTMatchers/ASTMatchersInternal.cpp:518
+// Linkage Spec can just be ignored
+// FIXME: Any other DeclContext kinds that can be safely disregarded
+if (isa(Ctx))

I would imagine we'd want to skip an `ExternCContextDecl` for similar reasons, 
though I am struggling to find a test case that actually generates that context.



Comment at: clang/unittests/ASTMatchers/ASTMatchersNarrowingTest.cpp:1648
+  // 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";

As another test:
```
namespace foo {
extern "C" {
void test();
}
}
```


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D75202/new/

https://reviews.llvm.org/D75202



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D75202: [ASTMatchers] HasNameMatcher handles `extern "C"`

2020-02-26 Thread Nathan James via Phabricator via cfe-commits
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. 



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(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(Ctx))
+  continue;
+if (!isa(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,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(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(Ctx))
+  continue;
+if (!isa(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