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

via cfe-commits cfe-commits at lists.llvm.org
Thu Feb 15 05:55:29 PST 2024


Author: Fred Tingaud
Date: 2024-02-15T14:55:25+01:00
New Revision: c609211d912dfa9849c5ca873d40d10e32e0a975

URL: https://github.com/llvm/llvm-project/commit/c609211d912dfa9849c5ca873d40d10e32e0a975
DIFF: https://github.com/llvm/llvm-project/commit/c609211d912dfa9849c5ca873d40d10e32e0a975.diff

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

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.

Added: 
    

Modified: 
    clang/docs/ReleaseNotes.rst
    clang/lib/AST/DeclBase.cpp
    clang/unittests/ASTMatchers/ASTMatchersNarrowingTest.cpp

Removed: 
    


################################################################################
diff  --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index b5e6eae9f1bf7d..5a19c2ea36bdf4 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -327,6 +327,8 @@ Fixed Point Support in Clang
 AST Matchers
 ------------
 
+- ``isInStdNamespace`` now supports Decl declared with ``extern "C++"``.
+
 clang-format
 ------------
 

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

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


        


More information about the cfe-commits mailing list