-
Notifications
You must be signed in to change notification settings - Fork 14.3k
[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
[clang] Fix isInStdNamespace for Decl flagged extern c++ #81776
Conversation
@llvm/pr-subscribers-clang Author: Fred Tingaud (frederic-tingaud-sonarsource) ChangesThe MSVC STL implementation declares multiple classes using: namespace std {
extern "C++" class locale {
...
};
}
Full diff: https://github.com/llvm/llvm-project/pull/81776.diff 2 Files Affected:
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) {
|
There was a problem hiding this 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
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM, thanks!
Hum, should we use |
Oh, I didn't know about this, but it does look better. Thanks! |
Thanks! Do you need me to merge this for you? |
Yes please :) |
The MSVC STL implementation declares multiple classes using:
isInStdNamespace
uses the first DeclContext to check whether a Decl is inside thestd
namespace. Here, the first DeclContext of thelocale
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.