summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorKrystian Stasiowski <sdkrystian@gmail.com>2024-04-16 07:34:27 -0400
committerGitHub <noreply@github.com>2024-04-16 07:34:27 -0400
commit71b9f6648222771470473431bc8ef2a2c25e872c (patch)
tree07d5bca84e65896f33521903316962f6651281fd
parentc18a3b6bd30456305cf1b3d78ad5a805577388c1 (diff)
[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#`.
-rw-r--r--clang/lib/Index/USRGeneration.cpp9
-rw-r--r--clang/test/Index/USR/func-type.cpp12
2 files changed, 18 insertions, 3 deletions
diff --git a/clang/lib/Index/USRGeneration.cpp b/clang/lib/Index/USRGeneration.cpp
index 5acc86191f8f..31c4a3345c09 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 ff1cd37a7fc4..459a8cd6da55 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 differ 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# |