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

via cfe-commits cfe-commits at lists.llvm.org
Wed Feb 14 10:50:47 PST 2024


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-clang

Author: Fred Tingaud (frederic-tingaud-sonarsource)

<details>
<summary>Changes</summary>

The MSVC STL implementation declares multiple classes using:

```cpp
namespace std {
  extern "C++" class locale {
    ...
  };
}
```

`isInStdNamespace` uses the first DeclContext to check whether a Decl is inside the `std` namespace. Here, the first DeclContext of the `locale` Decl is a LinkageSpecDecl so the method will return false.
We need to skip this LinkageSpecDecl to find the first DeclContext of type Namespace and actually check whether we're in the `std` namespace.

---
Full diff: https://github.com/llvm/llvm-project/pull/81776.diff


2 Files Affected:

- (modified) clang/lib/AST/DeclBase.cpp (+3) 
- (modified) clang/unittests/ASTMatchers/ASTMatchersNarrowingTest.cpp (+5) 


``````````diff
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) {

``````````

</details>


https://github.com/llvm/llvm-project/pull/81776


More information about the cfe-commits mailing list