Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Feb 15, 2024

Conversation

frederic-tingaud-sonarsource
Copy link
Contributor

The MSVC STL implementation declares multiple classes using:

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.

@llvmbot llvmbot added clang Clang issues not falling into any other category clang:frontend Language frontend issues, e.g. anything involving "Sema" labels Feb 14, 2024
@llvmbot
Copy link
Member

llvmbot commented Feb 14, 2024

@llvm/pr-subscribers-clang

Author: Fred Tingaud (frederic-tingaud-sonarsource)

Changes

The MSVC STL implementation declares multiple classes using:

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

Copy link
Contributor

@cor3ntin cor3ntin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good.
Can you add an entry to clang/docs/ReleseNotes.rst?
There is an ast matchers section.

Thanks

Copy link
Contributor

@cor3ntin cor3ntin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thanks!

@cor3ntin
Copy link
Contributor

Hum, should we use getNonTransparentContext instead? That would be cleaner

@frederic-tingaud-sonarsource
Copy link
Contributor Author

Oh, I didn't know about this, but it does look better. Thanks!

@cor3ntin
Copy link
Contributor

Thanks! Do you need me to merge this for you?

@frederic-tingaud-sonarsource
Copy link
Contributor Author

Yes please :)

@cor3ntin cor3ntin merged commit c609211 into llvm:main Feb 15, 2024
@frederic-tingaud-sonarsource frederic-tingaud-sonarsource deleted the ft/externstd branch February 15, 2024 13:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
clang:frontend Language frontend issues, e.g. anything involving "Sema" clang Clang issues not falling into any other category
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants