Skip to content

[clang][USR] Encode full decl-context also for anon namespaces #68325

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 1 commit into from
Oct 18, 2023

Conversation

kadircet
Copy link
Member

@kadircet kadircet commented Oct 5, 2023

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.

@kadircet kadircet requested a review from sam-mccall October 5, 2023 15:42
@llvmbot llvmbot added the clang Clang issues not falling into any other category label Oct 5, 2023
@llvmbot
Copy link
Member

llvmbot commented Oct 5, 2023

@llvm/pr-subscribers-clang

Changes

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.


Full diff: https://github.com/llvm/llvm-project/pull/68325.diff

2 Files Affected:

  • (modified) clang/lib/Index/USRGeneration.cpp (+5-4)
  • (added) clang/test/Index/USR/decl-context.cpp (+14)
diff --git a/clang/lib/Index/USRGeneration.cpp b/clang/lib/Index/USRGeneration.cpp
index f778a6208d5122d..614f5d8d2cad520 100644
--- a/clang/lib/Index/USRGeneration.cpp
+++ b/clang/lib/Index/USRGeneration.cpp
@@ -9,6 +9,7 @@
 #include "clang/Index/USRGeneration.h"
 #include "clang/AST/ASTContext.h"
 #include "clang/AST/Attr.h"
+#include "clang/AST/DeclCXX.h"
 #include "clang/AST/DeclTemplate.h"
 #include "clang/AST/DeclVisitor.h"
 #include "clang/Basic/FileManager.h"
@@ -368,14 +369,14 @@ void USRGenerator::VisitTemplateTemplateParmDecl(
 }
 
 void USRGenerator::VisitNamespaceDecl(const NamespaceDecl *D) {
+  if (IgnoreResults)
+    return;
+  VisitDeclContext(D->getDeclContext());
   if (D->isAnonymousNamespace()) {
     Out << "@aN";
     return;
   }
-
-  VisitDeclContext(D->getDeclContext());
-  if (!IgnoreResults)
-    Out << "@N@" << D->getName();
+  Out << "@N@" << D->getName();
 }
 
 void USRGenerator::VisitFunctionTemplateDecl(const FunctionTemplateDecl *D) {
diff --git a/clang/test/Index/USR/decl-context.cpp b/clang/test/Index/USR/decl-context.cpp
new file mode 100644
index 000000000000000..a57137a5c89b5fd
--- /dev/null
+++ b/clang/test/Index/USR/decl-context.cpp
@@ -0,0 +1,14 @@
+// RUN: c-index-test core -print-source-symbols -- -std=c++20 %s | FileCheck %s
+
+namespace ns {
+namespace {
+struct Foo {};
+// CHECK: [[@LINE-1]]:8 | struct/C | Foo | c:decl-context.cpp@N@ns@aN@S@Foo
+}
+}
+namespace ns2 {
+namespace {
+struct Foo {};
+// CHECK: [[@LINE-1]]:8 | struct/C | Foo | c:decl-context.cpp@N@ns2@aN@S@Foo
+}
+}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
clang Clang issues not falling into any other category
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants