Skip to content

Commit c609211

Browse files
[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.
1 parent 513e4dc commit c609211

File tree

3 files changed

+8
-1
lines changed

3 files changed

+8
-1
lines changed

clang/docs/ReleaseNotes.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -327,6 +327,8 @@ Fixed Point Support in Clang
327327
AST Matchers
328328
------------
329329

330+
- ``isInStdNamespace`` now supports Decl declared with ``extern "C++"``.
331+
330332
clang-format
331333
------------
332334

clang/lib/AST/DeclBase.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -402,7 +402,7 @@ bool Decl::isInAnonymousNamespace() const {
402402

403403
bool Decl::isInStdNamespace() const {
404404
const DeclContext *DC = getDeclContext();
405-
return DC && DC->isStdNamespace();
405+
return DC && DC->getNonTransparentContext()->isStdNamespace();
406406
}
407407

408408
bool Decl::isFileContextDecl() const {

clang/unittests/ASTMatchers/ASTMatchersNarrowingTest.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3637,6 +3637,11 @@ TEST_P(ASTMatchersTest, InStdNamespace) {
36373637
" class vector {};"
36383638
"}",
36393639
cxxRecordDecl(hasName("vector"), isInStdNamespace())));
3640+
3641+
EXPECT_TRUE(matches("namespace std {"
3642+
" extern \"C++\" class vector {};"
3643+
"}",
3644+
cxxRecordDecl(hasName("vector"), isInStdNamespace())));
36403645
}
36413646

36423647
TEST_P(ASTMatchersTest, InAnonymousNamespace) {

0 commit comments

Comments
 (0)