[clang] [Clang][Index] Add support for dependent class scope explicit specializations of function templates to USRGenerator (PR #98027)

Krystian Stasiowski via cfe-commits cfe-commits at lists.llvm.org
Mon Jul 8 07:22:33 PDT 2024


https://github.com/sdkrystian created https://github.com/llvm/llvm-project/pull/98027

Given the following:
```cpp
template<typename T>
struct A
{
    void f(int); // #1
    
    template<typename U>
    void f(U); // #2
    
    template<>
    void f<int>(int); // #3
};
```
Clang will generate the same USR for `#1` and `#2`. This patch fixes the issue by including the template arguments of dependent class scope explicit specializations in their USRs.

>From 7c8de9219c85115b98d8a69b2d191c40d453e5ba Mon Sep 17 00:00:00 2001
From: Krystian Stasiowski <sdkrystian at gmail.com>
Date: Mon, 8 Jul 2024 09:33:31 -0400
Subject: [PATCH] [Clang][Index] Add support for dependent class scope explicit
 specializations of function templates to USRGenerator

---
 clang/lib/Index/USRGeneration.cpp      | 18 +++++++++++++-----
 clang/test/Index/USR/func-template.cpp | 15 +++++++++++++++
 2 files changed, 28 insertions(+), 5 deletions(-)
 create mode 100644 clang/test/Index/USR/func-template.cpp

diff --git a/clang/lib/Index/USRGeneration.cpp b/clang/lib/Index/USRGeneration.cpp
index 5036ddee35fd12..ad7870309c5df1 100644
--- a/clang/lib/Index/USRGeneration.cpp
+++ b/clang/lib/Index/USRGeneration.cpp
@@ -257,12 +257,20 @@ void USRGenerator::VisitFunctionDecl(const FunctionDecl *D) {
       !D->hasAttr<OverloadableAttr>())
     return;
 
-  if (const TemplateArgumentList *
-        SpecArgs = D->getTemplateSpecializationArgs()) {
+  if (D->isFunctionTemplateSpecialization()) {
     Out << '<';
-    for (unsigned I = 0, N = SpecArgs->size(); I != N; ++I) {
-      Out << '#';
-      VisitTemplateArgument(SpecArgs->get(I));
+    if (const TemplateArgumentList *SpecArgs =
+            D->getTemplateSpecializationArgs()) {
+      for (const auto &Arg : SpecArgs->asArray()) {
+        Out << '#';
+        VisitTemplateArgument(Arg);
+      }
+    } else if (const ASTTemplateArgumentListInfo *SpecArgsWritten =
+                   D->getTemplateSpecializationArgsAsWritten()) {
+      for (const auto &ArgLoc : SpecArgsWritten->arguments()) {
+        Out << '#';
+        VisitTemplateArgument(ArgLoc.getArgument());
+      }
     }
     Out << '>';
   }
diff --git a/clang/test/Index/USR/func-template.cpp b/clang/test/Index/USR/func-template.cpp
new file mode 100644
index 00000000000000..c9c82f5e30a751
--- /dev/null
+++ b/clang/test/Index/USR/func-template.cpp
@@ -0,0 +1,15 @@
+// RUN: c-index-test core -print-source-symbols -- %s | FileCheck %s
+
+template<typename T>
+struct A {
+  void f(int);
+  // CHECK: {{[0-9]+}}:8 | instance-method/C++ | f | c:@ST>1#T at A@F at f#I# |
+  
+  template<typename U>
+  void f(U);
+  // CHECK: {{[0-9]+}}:8 | instance-method/C++ | f | c:@ST>1#T at A@FT@>1#Tf#t1.0#v# |
+  
+  template<>
+  void f<int>(int);
+  // CHECK: {{[0-9]+}}:8 | instance-method/C++ | f | c:@ST>1#T at A@F at f<#I>#I# |
+};



More information about the cfe-commits mailing list