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

kadir çetinkaya via cfe-commits cfe-commits at lists.llvm.org
Thu Oct 5 08:42:33 PDT 2023


https://github.com/kadircet created https://github.com/llvm/llvm-project/pull/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.


>From 5603c6ee481332f82c8ec1589e3e1610c570526d Mon Sep 17 00:00:00 2001
From: Kadir Cetinkaya <kadircet at google.com>
Date: Thu, 5 Oct 2023 16:55:17 +0200
Subject: [PATCH] [clang][USR] Encode full decl-context also for anon
 namespaces

---
 clang/lib/Index/USRGeneration.cpp     |  9 +++++----
 clang/test/Index/USR/decl-context.cpp | 14 ++++++++++++++
 2 files changed, 19 insertions(+), 4 deletions(-)
 create mode 100644 clang/test/Index/USR/decl-context.cpp

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 at N@ns at aN@S at Foo
+}
+}
+namespace ns2 {
+namespace {
+struct Foo {};
+// CHECK: [[@LINE-1]]:8 | struct/C | Foo | c:decl-context.cpp at N@ns2 at aN@S at Foo
+}
+}



More information about the cfe-commits mailing list