Skip to content

Commit

Permalink
[clang][Index] Use canonical function parameter types in USRs (#68222)
Browse files Browse the repository at this point in the history
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#`.
  • Loading branch information
sdkrystian committed Apr 16, 2024
1 parent c18a3b6 commit 71b9f66
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 3 deletions.
9 changes: 6 additions & 3 deletions clang/lib/Index/USRGeneration.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 << '.';
Expand Down
12 changes: 12 additions & 0 deletions clang/test/Index/USR/func-type.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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# |

0 comments on commit 71b9f66

Please sign in to comment.