Skip to content

Conversation

@dev-priyanshu15
Copy link

[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

@github-actions
Copy link

Thank you for submitting a Pull Request (PR) to the LLVM Project!

This PR will be automatically labeled and the relevant teams will be notified.

If you wish to, you can add reviewers by using the "Reviewers" section on this page.

If this is not working for you, it is probably because you do not have write permissions for the repository. In which case you can instead tag reviewers by name in a comment by using @ followed by their GitHub username.

If you have received no comments on your PR for a week, you can request a review by "ping"ing the PR by adding a comment “Ping”. The common courtesy "ping" rate is once a week. Please remember that you are asking for valuable time from other developers.

If you have further questions, they may be answered by the LLVM GitHub User Guide.

You can also ask questions in a comment on this PR, on the LLVM Discord or on the forums.

@llvmbot llvmbot added clang:as-a-library libclang and C++ API flang Flang issues not falling into any other category flang:fir-hlfir labels Dec 12, 2025
@llvmbot
Copy link
Member

llvmbot commented Dec 12, 2025

@llvm/pr-subscribers-vectorizers
@llvm/pr-subscribers-clang

@llvm/pr-subscribers-flang-fir-hlfir

Author: Priyanshu Singh (dev-priyanshu15)

Changes

[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


Full diff: https://github.com/llvm/llvm-project/pull/172092.diff

4 Files Affected:

  • (added) clang/test/Index/auto-function-param.cpp (+14)
  • (modified) clang/tools/libclang/CIndex.cpp (+9)
  • (modified) flang/lib/Optimizer/Transforms/AddAliasTags.cpp (+12-8)
  • (added) flang/test/Transforms/alias-tags-master-private-target.f90 (+26)
diff --git a/clang/test/Index/auto-function-param.cpp b/clang/test/Index/auto-function-param.cpp
new file mode 100644
index 0000000000000..5366d8468007e
--- /dev/null
+++ b/clang/test/Index/auto-function-param.cpp
@@ -0,0 +1,14 @@
+// Test case for auto function parameter reported as CXType_Auto
+// This test verifies that auto parameters in function declarations
+// are properly reported as CXType_Auto in the libclang C API
+// See issue #172072
+
+// RUN: c-index-test -test-type %s | FileCheck %s
+
+// Function with auto parameter
+int bar(auto p) {
+  return p;
+}
+
+// CHECK: FunctionDecl=bar:{{.*}} CXType_FunctionProto
+// CHECK: ParmDecl=p:{{.*}} CXType_Auto
diff --git a/clang/tools/libclang/CIndex.cpp b/clang/tools/libclang/CIndex.cpp
index 32e84248c1b27..bb0816b0447a9 100644
--- a/clang/tools/libclang/CIndex.cpp
+++ b/clang/tools/libclang/CIndex.cpp
@@ -1789,6 +1789,15 @@ bool CursorVisitor::VisitAdjustedTypeLoc(AdjustedTypeLoc TL) {
   return Visit(TL.getOriginalLoc());
 }
 
+bool CursorVisitor::VisitAutoTypeLoc(AutoTypeLoc TL) {
+  // AutoTypeLoc represents the location of an auto type specifier.
+  // We do not visit children because the auto type itself is complete.
+  // This handler ensures that auto function parameters are properly
+  // reported as CXType_Auto in the libclang C API, rather than being
+  // incorrectly reported as TypeRef/unexposed.
+  return false;
+}
+
 bool CursorVisitor::VisitDeducedTemplateSpecializationTypeLoc(
     DeducedTemplateSpecializationTypeLoc TL) {
   if (VisitTemplateName(TL.getTypePtr()->getTemplateName(),
diff --git a/flang/lib/Optimizer/Transforms/AddAliasTags.cpp b/flang/lib/Optimizer/Transforms/AddAliasTags.cpp
index 3718848c05775..a0b10d1858a92 100644
--- a/flang/lib/Optimizer/Transforms/AddAliasTags.cpp
+++ b/flang/lib/Optimizer/Transforms/AddAliasTags.cpp
@@ -702,20 +702,24 @@ void AddAliasTagsPass::runOnAliasInterface(fir::FirAliasTagOpInterface op,
              source.kind == fir::AliasAnalysis::SourceKind::Argument) {
     LLVM_DEBUG(llvm::dbgs().indent(2)
                << "Found reference to dummy argument at " << *op << "\n");
-    std::string name = getFuncArgName(llvm::cast<mlir::Value>(source.origin.u));
     // POINTERS can alias with any POINTER or TARGET. Assume that TARGET dummy
     // arguments might alias with each other (because of the "TARGET" hole for
     // dummy arguments). See flang/docs/Aliasing.md.
+    // If it is a TARGET or POINTER, then we do not care about the name,
+    // because the tag points to the root of the subtree currently.
     if (source.isTargetOrPointer()) {
       tag = state.getFuncTreeWithScope(func, scopeOp).targetDataTree.getTag();
-    } else if (!name.empty()) {
-      tag = state.getFuncTreeWithScope(func, scopeOp)
-                .dummyArgDataTree.getTag(name);
     } else {
-      LLVM_DEBUG(llvm::dbgs().indent(2)
-                 << "WARN: couldn't find a name for dummy argument " << *op
-                 << "\n");
-      tag = state.getFuncTreeWithScope(func, scopeOp).dummyArgDataTree.getTag();
+      std::string name = getFuncArgName(llvm::cast<mlir::Value>(source.origin.u));
+      if (!name.empty()) {
+        tag = state.getFuncTreeWithScope(func, scopeOp)
+                  .dummyArgDataTree.getTag(name);
+      } else {
+        LLVM_DEBUG(llvm::dbgs().indent(2)
+                   << "WARN: couldn't find a name for dummy argument " << *op
+                   << "\n");
+        tag = state.getFuncTreeWithScope(func, scopeOp).dummyArgDataTree.getTag();
+      }
     }
 
     // TBAA for global variables without descriptors
diff --git a/flang/test/Transforms/alias-tags-master-private-target.f90 b/flang/test/Transforms/alias-tags-master-private-target.f90
new file mode 100644
index 0000000000000..0251376476c64
--- /dev/null
+++ b/flang/test/Transforms/alias-tags-master-private-target.f90
@@ -0,0 +1,26 @@
+! Test case for regression in OpenMP master region with private target integer
+! This test was failing with an assertion error in AddAliasTags.cpp
+! See issue #172075
+
+! RUN: %flang -fopenmp -c -O1 %s -o %t.o 2>&1 | FileCheck %s --allow-empty
+
+module test
+contains
+subroutine omp_master_repro()
+  implicit none
+  integer, parameter :: nim = 4
+  integer, parameter :: nvals = 8
+  integer, target :: ui
+  integer :: hold1(nvals, nim)
+  hold1 = 0
+  !$OMP PARALLEL DEFAULT(NONE) &
+  !$OMP PRIVATE(ui) &
+  !$OMP SHARED(hold1, nim)
+  !$OMP MASTER
+  do ui = 1, nim
+     hold1(:, ui) = 1
+  end do
+  !$OMP END MASTER
+  !$OMP END PARALLEL
+end subroutine omp_master_repro
+end module test

@clementval
Copy link
Contributor

The flang changes seem unrelated to the issue you are fixing.

@Serafean
Copy link

Serafean commented Dec 13, 2025

Sorry, this doesn't seem to work... when the parameters are visited like so:

  auto kind = clang_getCursorKind(current_cursor);
  if (kind == CXCursor_ParmDecl){
        auto type = clang_getCursorType(current_cursor);
        std::cout << "Param : \n\ttype:" << type << "  kind: " << type.kind <<   std::endl;

    }

the TypeKind in still unexposed.

AutoTypeLoc isn't called for the auto parameter, that honor goes to VisitFunctionTypeLoc explicitly calling Visit() for every param, and then VisitTemplateTypeParmTypeLoc for the auto keyword.

for the CXType_Auto not being reported, so far I managed to paper over (wrongly) it this way:

--- a/clang/tools/libclang/CXType.cpp
+++ b/clang/tools/libclang/CXType.cpp
@@ -149,6 +149,16 @@ CXType cxtype::MakeCXType(QualType T, CXTranslationUnit TU) {
     if (auto *PTT = T->getAs<ParenType>()) {
       return MakeCXType(PTT->getInnerType(), TU);
     }
+    
+    // auto in function parameters
+    if(auto *TTP = T->getAs<TemplateTypeParmType>()) {
+      auto *D = TTP->getDecl();
+      if (D && D->isImplicit()) {
+        if (auto *TC = D->getTypeConstraint(); !TC) {
+          TK = CXType_Auto;
+        }
+      }
+    }

Logic taken from TypePrinter.cpp:1676.

No idea yet how to stop visiting and exposing the auto keyword as a Cursor.

Copy link
Contributor

@clementval clementval left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you remove the unrelated flang changes?

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 llvm#172072
@dev-priyanshu15 dev-priyanshu15 force-pushed the fix/172072-auto-function-param branch from 38b16cc to e4b8541 Compare December 13, 2025 18:11
@llvmbot llvmbot added the clang Clang issues not falling into any other category label Dec 13, 2025
@dev-priyanshu15
Copy link
Author

Thanks @Serafean and @clementval. I've removed the Flang changes and updated the fix using your approach from CXType.cpp. The improved commit has been force-pushed.

@eugeneepshteyn eugeneepshteyn removed flang:fir-hlfir flang Flang issues not falling into any other category labels Dec 14, 2025
@dev-priyanshu15 dev-priyanshu15 force-pushed the fix/172072-auto-function-param branch from 32055ec to e0bb367 Compare December 14, 2025 13:26
@dev-priyanshu15
Copy link
Author

Thanks for the review feedback.

I've fixed the issue fully in CXType.cpp by treating any implicit TemplateTypeParmType as CXType_Auto, which correctly handles both unconstrained auto (e.g., auto p) and constrained auto (e.g., Integral auto q).

I've also added Index tests covering both unconstrained and constrained auto parameter cases in the test file.

The fix is now in the correct layer (type classification) and handles all auto parameter variants as indicated by your review.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

clang:as-a-library libclang and C++ API clang Clang issues not falling into any other category llvm:transforms vectorizers

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants