Skip to content

Commit 71b9f66

Browse files
authored
[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#`.
1 parent c18a3b6 commit 71b9f66

File tree

2 files changed

+18
-3
lines changed

2 files changed

+18
-3
lines changed

clang/lib/Index/USRGeneration.cpp

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -267,10 +267,13 @@ void USRGenerator::VisitFunctionDecl(const FunctionDecl *D) {
267267
Out << '>';
268268
}
269269

270+
QualType CanonicalType = D->getType().getCanonicalType();
270271
// Mangle in type information for the arguments.
271-
for (auto *PD : D->parameters()) {
272-
Out << '#';
273-
VisitType(PD->getType());
272+
if (const auto *FPT = CanonicalType->getAs<FunctionProtoType>()) {
273+
for (QualType PT : FPT->param_types()) {
274+
Out << '#';
275+
VisitType(PT);
276+
}
274277
}
275278
if (D->isVariadic())
276279
Out << '.';

clang/test/Index/USR/func-type.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,15 @@ void Func( void (* (*)(int, int))(int, int) );
1616
// CHECK: {{[0-9]+}}:6 | function/C | Func | c:@F@Func#*F*Fv(#I#I)(#I#I)# |
1717
void Func( void (* (*)(int, int, int))(int) );
1818
// CHECK: {{[0-9]+}}:6 | function/C | Func | c:@F@Func#*F*Fv(#I)(#I#I#I)# |
19+
20+
// Functions with parameter types that only differ in top-level cv-qualification should generate the same USR.
21+
22+
void f( const int );
23+
// CHECK: {{[0-9]+}}:6 | function/C | f | c:@F@f#I# |
24+
void f( int );
25+
// CHECK: {{[0-9]+}}:6 | function/C | f | c:@F@f#I# |
26+
27+
void g( int );
28+
// CHECK: {{[0-9]+}}:6 | function/C | g | c:@F@g#I# |
29+
void g( const int );
30+
// CHECK: {{[0-9]+}}:6 | function/C | g | c:@F@g#I# |

0 commit comments

Comments
 (0)