[clang] fix usr rhs (PR #68329)

kadir çetinkaya via cfe-commits cfe-commits at lists.llvm.org
Wed Oct 18 01:53:04 PDT 2023


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

>From 80fa52f6efa36e04bf87c525013513d4429788ae 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] [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