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

Krystian Stasiowski via cfe-commits cfe-commits at lists.llvm.org
Wed Oct 4 07:16:08 PDT 2023


https://github.com/sdkrystian created https://github.com/llvm/llvm-project/pull/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:
```cpp
// A.cpp
void f(const int x); // c:@F at f#1I#

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

>From 0dc45b8b3303acff3703565af6d98c37988c7d66 Mon Sep 17 00:00:00 2001
From: Krystian Stasiowski <sdkrystian at gmail.com>
Date: Wed, 4 Oct 2023 10:06:28 -0400
Subject: [PATCH] [clang][Index] Use canonical function parameter types in USRs

---
 clang/lib/Index/USRGeneration.cpp | 9 ++++++---
 1 file changed, 6 insertions(+), 3 deletions(-)

diff --git a/clang/lib/Index/USRGeneration.cpp b/clang/lib/Index/USRGeneration.cpp
index f778a6208d5122d..fa0fd094c223f7f 100644
--- a/clang/lib/Index/USRGeneration.cpp
+++ b/clang/lib/Index/USRGeneration.cpp
@@ -265,10 +265,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 (auto *FPT = CanonicalType->getAs<FunctionProtoType>()) {
+    for (QualType PT : FPT->param_types()) {
+      Out << '#';
+      VisitType(PT);
+    }
   }
   if (D->isVariadic())
     Out << '.';



More information about the cfe-commits mailing list