Author: Krystian Stasiowski
Date: 2024-04-16T07:34:27-04:00
New Revision: 71b9f6648222771470473431bc8ef2a2c25e872c

URL: 
https://github.com/llvm/llvm-project/commit/71b9f6648222771470473431bc8ef2a2c25e872c
DIFF: 
https://github.com/llvm/llvm-project/commit/71b9f6648222771470473431bc8ef2a2c25e872c.diff

LOG: [clang][Index] Use canonical function parameter types in USRs (#68222)

This is necessary to ensure that functions declared in different
translation units whose parameter types only differ in top-level
cv-qualification generate the same USR.

For example:
```
// A.cpp
void f(const int x); // c:@F@f#1I#

// B.cpp
void f(int x);       // c:@F@f#I#
``` 
With this patch, the USR for both functions will be
`c:@F@f#I#`.

Added: 
    

Modified: 
    clang/lib/Index/USRGeneration.cpp
    clang/test/Index/USR/func-type.cpp

Removed: 
    


################################################################################
diff  --git a/clang/lib/Index/USRGeneration.cpp 
b/clang/lib/Index/USRGeneration.cpp
index 5acc86191f8f9c..31c4a3345c09d1 100644
--- a/clang/lib/Index/USRGeneration.cpp
+++ b/clang/lib/Index/USRGeneration.cpp
@@ -267,10 +267,13 @@ void USRGenerator::VisitFunctionDecl(const FunctionDecl 
*D) {
     Out << '>';
   }
 
+  QualType CanonicalType = D->getType().getCanonicalType();
   // Mangle in type information for the arguments.
-  for (auto *PD : D->parameters()) {
-    Out << '#';
-    VisitType(PD->getType());
+  if (const auto *FPT = CanonicalType->getAs<FunctionProtoType>()) {
+    for (QualType PT : FPT->param_types()) {
+      Out << '#';
+      VisitType(PT);
+    }
   }
   if (D->isVariadic())
     Out << '.';

diff  --git a/clang/test/Index/USR/func-type.cpp 
b/clang/test/Index/USR/func-type.cpp
index ff1cd37a7fc421..459a8cd6da5584 100644
--- a/clang/test/Index/USR/func-type.cpp
+++ b/clang/test/Index/USR/func-type.cpp
@@ -16,3 +16,15 @@ void Func( void (* (*)(int, int))(int, int) );
 // CHECK: {{[0-9]+}}:6 | function/C | Func | c:@F@Func#*F*Fv(#I#I)(#I#I)# |
 void Func( void (* (*)(int, int, int))(int) );
 // CHECK: {{[0-9]+}}:6 | function/C | Func | c:@F@Func#*F*Fv(#I)(#I#I#I)# |
+
+// Functions with parameter types that only 
diff er in top-level cv-qualification should generate the same USR.
+
+void f( const int );
+// CHECK: {{[0-9]+}}:6 | function/C | f | c:@F@f#I# |
+void f( int );
+// CHECK: {{[0-9]+}}:6 | function/C | f | c:@F@f#I# |
+
+void g( int );
+// CHECK: {{[0-9]+}}:6 | function/C | g | c:@F@g#I# |
+void g( const int );
+// CHECK: {{[0-9]+}}:6 | function/C | g | c:@F@g#I# |


        
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to