[clang] [clang] Fix isInStdNamespace for Decl flagged extern c++ (PR #81776)

Fred Tingaud via cfe-commits cfe-commits at lists.llvm.org
Thu Feb 15 05:23:35 PST 2024


https://github.com/frederic-tingaud-sonarsource updated https://github.com/llvm/llvm-project/pull/81776

>From c12b2e26ad7a114aff09b89c75178d4ab7890a20 Mon Sep 17 00:00:00 2001
From: Fred Tingaud <frederic.tingaud at sonarsource.com>
Date: Wed, 14 Feb 2024 19:40:29 +0100
Subject: [PATCH 1/3] [clang] Fix isInStdNamespace for Decl flagged extern c++

---
 clang/lib/AST/DeclBase.cpp                               | 3 +++
 clang/unittests/ASTMatchers/ASTMatchersNarrowingTest.cpp | 5 +++++
 2 files changed, 8 insertions(+)

diff --git a/clang/lib/AST/DeclBase.cpp b/clang/lib/AST/DeclBase.cpp
index 8163f9bdaf8d97..4cfe9ed3340735 100644
--- a/clang/lib/AST/DeclBase.cpp
+++ b/clang/lib/AST/DeclBase.cpp
@@ -402,6 +402,9 @@ bool Decl::isInAnonymousNamespace() const {
 
 bool Decl::isInStdNamespace() const {
   const DeclContext *DC = getDeclContext();
+  while (auto const LD = dyn_cast_or_null<LinkageSpecDecl>(DC)) {
+    DC = LD->getDeclContext();
+  }
   return DC && DC->isStdNamespace();
 }
 
diff --git a/clang/unittests/ASTMatchers/ASTMatchersNarrowingTest.cpp b/clang/unittests/ASTMatchers/ASTMatchersNarrowingTest.cpp
index edcdae4559d970..b75da7bc1ed069 100644
--- a/clang/unittests/ASTMatchers/ASTMatchersNarrowingTest.cpp
+++ b/clang/unittests/ASTMatchers/ASTMatchersNarrowingTest.cpp
@@ -3637,6 +3637,11 @@ TEST_P(ASTMatchersTest, InStdNamespace) {
                       "  class vector {};"
                       "}",
                       cxxRecordDecl(hasName("vector"), isInStdNamespace())));
+
+  EXPECT_TRUE(matches("namespace std {"
+                      "  extern \"C++\" class vector {};"
+                      "}",
+                      cxxRecordDecl(hasName("vector"), isInStdNamespace())));
 }
 
 TEST_P(ASTMatchersTest, InAnonymousNamespace) {

>From 3d37a1b59032889115ef1c9e7039c65d3c6ff830 Mon Sep 17 00:00:00 2001
From: Fred Tingaud <frederic.tingaud at sonarsource.com>
Date: Thu, 15 Feb 2024 10:18:46 +0100
Subject: [PATCH 2/3] Add ReleaseNotes entry.

---
 clang/docs/ReleaseNotes.rst | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 6cf48d63dd512e..db3dc724997286 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -308,6 +308,8 @@ Floating Point Support in Clang
 AST Matchers
 ------------
 
+- ``isInStdNamespace`` now supports Decl declared with ``extern "C++"``.
+
 clang-format
 ------------
 

>From 58c993eaa58bcac8afd28652584ab75d56ba21ae Mon Sep 17 00:00:00 2001
From: Fred Tingaud <frederic.tingaud at sonarsource.com>
Date: Thu, 15 Feb 2024 14:22:25 +0100
Subject: [PATCH 3/3] Simplify the code

---
 clang/lib/AST/DeclBase.cpp | 5 +----
 1 file changed, 1 insertion(+), 4 deletions(-)

diff --git a/clang/lib/AST/DeclBase.cpp b/clang/lib/AST/DeclBase.cpp
index 4cfe9ed3340735..10fe8bb97ce660 100644
--- a/clang/lib/AST/DeclBase.cpp
+++ b/clang/lib/AST/DeclBase.cpp
@@ -402,10 +402,7 @@ bool Decl::isInAnonymousNamespace() const {
 
 bool Decl::isInStdNamespace() const {
   const DeclContext *DC = getDeclContext();
-  while (auto const LD = dyn_cast_or_null<LinkageSpecDecl>(DC)) {
-    DC = LD->getDeclContext();
-  }
-  return DC && DC->isStdNamespace();
+  return DC && DC->getNonTransparentContext()->isStdNamespace();
 }
 
 bool Decl::isFileContextDecl() const {



More information about the cfe-commits mailing list