[clang] fix usr rhs (PR #68329)

kadir çetinkaya via cfe-commits cfe-commits at lists.llvm.org
Thu Oct 5 09:06:56 PDT 2023


https://github.com/kadircet created https://github.com/llvm/llvm-project/pull/68329

None

>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 1/2] [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
+}
+}

>From 44a0c88778e5fea419838b2fae1ccd92dab696f5 Mon Sep 17 00:00:00 2001
From: Kadir Cetinkaya <kadircet at google.com>
Date: Thu, 5 Oct 2023 18:05:11 +0200
Subject: [PATCH 2/2] [clang][Index] Improve USR generation for using-decls

Make sure we include filename and namespace for the target declarations.
---
 .../clangd/unittests/SymbolInfoTests.cpp      |  2 +-
 clang/lib/Index/USRGeneration.cpp             | 27 +++++++++++++------
 clang/test/Index/USR/using-decl.cpp           | 16 +++++++++++
 clang/test/Index/using_if_exists.cpp          |  2 +-
 clang/test/Index/usrs.cpp                     |  2 +-
 5 files changed, 38 insertions(+), 11 deletions(-)
 create mode 100644 clang/test/Index/USR/using-decl.cpp

diff --git a/clang-tools-extra/clangd/unittests/SymbolInfoTests.cpp b/clang-tools-extra/clangd/unittests/SymbolInfoTests.cpp
index 6c91f3783a6220a..64c8834ab2e95bc 100644
--- a/clang-tools-extra/clangd/unittests/SymbolInfoTests.cpp
+++ b/clang-tools-extra/clangd/unittests/SymbolInfoTests.cpp
@@ -202,7 +202,7 @@ TEST(SymbolInfoTests, All) {
                                      "def_bool"},
                ExpectedSymbolDetails{"foo", "", "c:@F at foo#I#", "def_int",
                                      "def_int"},
-               ExpectedSymbolDetails{"foo", "bar::", "c:@N at bar@UD at foo",
+               ExpectedSymbolDetails{"foo", "bar::", "c:TestTU.cpp at N@bar at UD@foo",
                                      "decl"}}},
           {
               R"cpp( // Multiple symbols returned - implicit conversion
diff --git a/clang/lib/Index/USRGeneration.cpp b/clang/lib/Index/USRGeneration.cpp
index 614f5d8d2cad520..5a409f34242ecd1 100644
--- a/clang/lib/Index/USRGeneration.cpp
+++ b/clang/lib/Index/USRGeneration.cpp
@@ -114,14 +114,7 @@ class USRGenerator : public ConstDeclVisitor<USRGenerator> {
     IgnoreResults = true;
   }
 
-  void VisitUsingDecl(const UsingDecl *D) {
-    VisitDeclContext(D->getDeclContext());
-    Out << "@UD@";
-
-    bool EmittedDeclName = !EmitDeclName(D);
-    assert(EmittedDeclName && "EmitDeclName can not fail for UsingDecls");
-    (void)EmittedDeclName;
-  }
+  void VisitBaseUsingDecl(const BaseUsingDecl *D);
 
   bool ShouldGenerateLocation(const NamedDecl *D);
 
@@ -1070,6 +1063,24 @@ void USRGenerator::VisitMSGuidDecl(const MSGuidDecl *D) {
   D->NamedDecl::printName(Out);
 }
 
+void USRGenerator::VisitBaseUsingDecl(const BaseUsingDecl *D) {
+  // Add the filename when needed to disambiguate using decls from different
+  // files.
+  if (ShouldGenerateLocation(D) && GenLoc(D, /*IncludeOffset=*/false))
+    return;
+  VisitDeclContext(D->getDeclContext());
+  Out << "@UD";
+
+  // When the using-decl is resolved also print the context of the first target
+  // decl. All shadows must be from the same decl-context.
+  if (auto FirstShadow = D->shadow_begin(); FirstShadow != D->shadow_end())
+    VisitDeclContext(FirstShadow->getTargetDecl()->getDeclContext());
+  Out << "@";
+  bool EmittedDeclName = !EmitDeclName(D);
+  assert(EmittedDeclName && "EmitDeclName can not fail for UsingDecls");
+  (void)EmittedDeclName;
+}
+
 //===----------------------------------------------------------------------===//
 // USR generation functions.
 //===----------------------------------------------------------------------===//
diff --git a/clang/test/Index/USR/using-decl.cpp b/clang/test/Index/USR/using-decl.cpp
new file mode 100644
index 000000000000000..91a4a8f7946cd61
--- /dev/null
+++ b/clang/test/Index/USR/using-decl.cpp
@@ -0,0 +1,16 @@
+// RUN: c-index-test core -print-source-symbols -- -std=c++20 %s | FileCheck %s
+
+namespace ns { void foo(); }
+namespace ns2 { void foo(int); }
+
+namespace exporting {
+namespace {
+using ns::foo;
+// CHECK: [[@LINE-1]]:11 | using/C++ | foo | c:using-decl.cpp at N@exporting at aN@UD at N@ns at foo
+using ::ns::foo;
+// CHECK: [[@LINE-1]]:13 | using/C++ | foo | c:using-decl.cpp at N@exporting at aN@UD at N@ns at foo
+// FIXME: Also put the qualified name for the target decl
+using ns2::foo;
+// CHECK: [[@LINE-1]]:12 | using/C++ | foo | c:using-decl.cpp at N@exporting at aN@UD at N@ns2 at foo
+}
+}
diff --git a/clang/test/Index/using_if_exists.cpp b/clang/test/Index/using_if_exists.cpp
index 73d1be739e42816..339d7fa233d68bd 100644
--- a/clang/test/Index/using_if_exists.cpp
+++ b/clang/test/Index/using_if_exists.cpp
@@ -6,5 +6,5 @@ namespace ns {
 }
 
 using ns::foo __attribute__((using_if_exists));
-// CHECK: [[@LINE-1]]:11 | using/C++ | foo | c:@UD at foo | <no-cgname> | Decl | rel: 0
+// CHECK: [[@LINE-1]]:11 | using/C++ | foo | c:using_if_exists.cpp at UD@foo | <no-cgname> | Decl | rel: 0
 // CHECK-NOT: <unknown>
diff --git a/clang/test/Index/usrs.cpp b/clang/test/Index/usrs.cpp
index dbfa44f4b676489..08b9c6f60bd8cc8 100644
--- a/clang/test/Index/usrs.cpp
+++ b/clang/test/Index/usrs.cpp
@@ -158,7 +158,7 @@ __m128 vectorOverload(__m128 f);
 // CHECK: usrs.cpp c:@NA at foo_alias
 // CHECK-NOT: foo
 // CHECK: usrs.cpp c:@NA at foo_alias2
-// CHECK: usrs.cpp c:@UD at ClsB Extent=[64:1 - 64:16]
+// CHECK: usrs.cpp c:usrs.cpp at UD@N at foo@ClsB Extent=[64:1 - 64:16]
 // CHECK: usrs.cpp c:@NA at foo_alias3
 // CHECK: usrs.cpp c:@aN Extent=[68:1 - 73:2]
 // CHECK: usrs.cpp c:usrs.cpp at aN@S at RDar9371763_Foo Extent=[69:1 - 72:2]



More information about the cfe-commits mailing list