Skip to content

Commit 48a5350

Browse files
authored
[clang][USR] Encode full decl-context also for anon namespaces (#68325)
Otherwise we create collisions, e.g. a struct named Foo inside an anonymous namespace will get the same USR no matter what the surrounding decl-context is.
1 parent 8c369eb commit 48a5350

File tree

2 files changed

+19
-4
lines changed

2 files changed

+19
-4
lines changed

clang/lib/Index/USRGeneration.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include "clang/Index/USRGeneration.h"
1010
#include "clang/AST/ASTContext.h"
1111
#include "clang/AST/Attr.h"
12+
#include "clang/AST/DeclCXX.h"
1213
#include "clang/AST/DeclTemplate.h"
1314
#include "clang/AST/DeclVisitor.h"
1415
#include "clang/Basic/FileManager.h"
@@ -368,14 +369,14 @@ void USRGenerator::VisitTemplateTemplateParmDecl(
368369
}
369370

370371
void USRGenerator::VisitNamespaceDecl(const NamespaceDecl *D) {
372+
if (IgnoreResults)
373+
return;
374+
VisitDeclContext(D->getDeclContext());
371375
if (D->isAnonymousNamespace()) {
372376
Out << "@aN";
373377
return;
374378
}
375-
376-
VisitDeclContext(D->getDeclContext());
377-
if (!IgnoreResults)
378-
Out << "@N@" << D->getName();
379+
Out << "@N@" << D->getName();
379380
}
380381

381382
void USRGenerator::VisitFunctionTemplateDecl(const FunctionTemplateDecl *D) {

clang/test/Index/USR/decl-context.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// RUN: c-index-test core -print-source-symbols -- -std=c++20 %s | FileCheck %s
2+
3+
namespace ns {
4+
namespace {
5+
struct Foo {};
6+
// CHECK: [[@LINE-1]]:8 | struct/C | Foo | c:decl-context.cpp@N@ns@aN@S@Foo
7+
}
8+
}
9+
namespace ns2 {
10+
namespace {
11+
struct Foo {};
12+
// CHECK: [[@LINE-1]]:8 | struct/C | Foo | c:decl-context.cpp@N@ns2@aN@S@Foo
13+
}
14+
}

0 commit comments

Comments
 (0)