Skip to content

Commit e4b8541

Browse files
[clang][libclang] Fix auto function parameter type reporting
When auto is used as a function parameter type, clang-c API was incorrectly reporting it as TypeRef with kind unexposed instead of reporting it as CXType_Auto. The issue was that CursorVisitor did not have a handler for AutoTypeLoc, so it fell back to default behavior which resulted in unexposed types. Add VisitAutoTypeLoc method to properly handle auto type locations in function parameters. This brings consistency with how auto is handled in variable declarations. Fixes issue #172072
1 parent f3c1645 commit e4b8541

File tree

3 files changed

+33
-0
lines changed

3 files changed

+33
-0
lines changed
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// Test case for auto function parameter reported as CXType_Auto
2+
// This test verifies that auto parameters in function declarations
3+
// are properly reported as CXType_Auto in the libclang C API
4+
// See issue #172072
5+
6+
// RUN: c-index-test -test-type %s | FileCheck %s
7+
8+
// Function with auto parameter
9+
int bar(auto p) {
10+
return p;
11+
}
12+
13+
// CHECK: FunctionDecl=bar:{{.*}} CXType_FunctionProto
14+
// CHECK: ParmDecl=p:{{.*}} CXType_Auto

clang/tools/libclang/CIndex.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1789,6 +1789,15 @@ bool CursorVisitor::VisitAdjustedTypeLoc(AdjustedTypeLoc TL) {
17891789
return Visit(TL.getOriginalLoc());
17901790
}
17911791

1792+
bool CursorVisitor::VisitAutoTypeLoc(AutoTypeLoc TL) {
1793+
// AutoTypeLoc represents the location of an auto type specifier.
1794+
// We do not visit children because the auto type itself is complete.
1795+
// This handler ensures that auto function parameters are properly
1796+
// reported as CXType_Auto in the libclang C API, rather than being
1797+
// incorrectly reported as TypeRef/unexposed.
1798+
return false;
1799+
}
1800+
17921801
bool CursorVisitor::VisitDeducedTemplateSpecializationTypeLoc(
17931802
DeducedTemplateSpecializationTypeLoc TL) {
17941803
if (VisitTemplateName(TL.getTypePtr()->getTemplateName(),

clang/tools/libclang/CXType.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,16 @@ CXType cxtype::MakeCXType(QualType T, CXTranslationUnit TU) {
150150
return MakeCXType(PTT->getInnerType(), TU);
151151
}
152152

153+
// Handle auto in function parameters
154+
if (auto *TTP = T->getAs<TemplateTypeParmType>()) {
155+
auto *D = TTP->getDecl();
156+
if (D && D->isImplicit()) {
157+
if (auto *TC = D->getTypeConstraint(); !TC) {
158+
TK = CXType_Auto;
159+
}
160+
}
161+
}
162+
153163
ASTContext &Ctx = cxtu::getASTUnit(TU)->getASTContext();
154164
if (Ctx.getLangOpts().ObjC) {
155165
QualType UnqualT = T.getUnqualifiedType();

0 commit comments

Comments
 (0)