Author: Serafean
Date: 2026-02-24T21:50:33Z
New Revision: 23ea5d537dedb31e767876a871790c111e6ef6b6

URL: 
https://github.com/llvm/llvm-project/commit/23ea5d537dedb31e767876a871790c111e6ef6b6
DIFF: 
https://github.com/llvm/llvm-project/commit/23ea5d537dedb31e767876a871790c111e6ef6b6.diff

LOG: [clang][libclang]Check auto type for type constraints (#172472)

the "auto" keyword was visited by the default visitor only, making any
type constraints unavailable through the libclang API.

Add an explicit visitor for AutoType, check for constraints, and visit
those.

Fixes issue #166580

I modeled the visiting after
https://github.com/llvm/llvm-project/blob/main/clang/tools/libclang/CIndex.cpp#L1346
, and the tests after what I found in
https://github.com/llvm/llvm-project/blob/main/clang/test/Index/index-concepts.cpp
. Hopefully it fits.

Added: 
    clang/test/Index/index-auto.cpp

Modified: 
    clang/docs/ReleaseNotes.rst
    clang/tools/libclang/CIndex.cpp

Removed: 
    


################################################################################
diff  --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 01235ab1df1d9..cb1010aee1edd 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -427,6 +427,7 @@ clang-format
 
 libclang
 --------
+- Visit constraints of `auto` type to properly visit concept usages (#GH166580)
 - Visit switch initializer statements 
(https://bugs.kde.org/show_bug.cgi?id=415537#c2)
 - Fix crash in clang_getBinaryOperatorKindSpelling and 
clang_getUnaryOperatorKindSpelling
 

diff  --git a/clang/test/Index/index-auto.cpp b/clang/test/Index/index-auto.cpp
new file mode 100644
index 0000000000000..5e8af7287d9bb
--- /dev/null
+++ b/clang/test/Index/index-auto.cpp
@@ -0,0 +1,18 @@
+// RUN: c-index-test -test-load-source all %s -std=gnu++20 | FileCheck %s
+
+template<typename T>
+concept Decrementable = requires (T t){ --t; };
+
+auto i = 42;
+// CHECK: index-auto.cpp:[[@LINE-1]]:6: VarDecl=i:[[@LINE-1]]:6 (Definition) 
Extent=[[[@LINE-1]]:1 - [[@LINE-1]]:12]
+
+auto foo(){ return 42;}
+// CHECK: index-auto.cpp:[[@LINE-1]]:6: FunctionDecl=foo:[[@LINE-1]]:6 
(Definition) Extent=[[[@LINE-1]]:1 - [[@LINE-1]]:24]
+
+Decrementable auto j = 43;
+// CHECK: index-auto.cpp:[[@LINE-1]]:20: VarDecl=j:[[@LINE-1]]:20 (Definition) 
Extent=[[[@LINE-1]]:1 - [[@LINE-1]]:26]
+// CHECK: index-auto.cpp:[[@LINE-2]]:1: TemplateRef=Decrementable:4:9 
Extent=[[[@LINE-2]]:1 - [[@LINE-2]]:14]
+
+Decrementable auto bar() { return 43; }
+// CHECK: index-auto.cpp:[[@LINE-1]]:20: FunctionDecl=bar:[[@LINE-1]]:20 
(Definition) Extent=[[[@LINE-1]]:1 - [[@LINE-1]]:40]
+// CHECK: index-auto.cpp:[[@LINE-2]]:1: TemplateRef=Decrementable:4:9 
Extent=[[[@LINE-2]]:1 - [[@LINE-2]]:14]

diff  --git a/clang/tools/libclang/CIndex.cpp b/clang/tools/libclang/CIndex.cpp
index 6f38a722f74f4..31b6a3222d916 100644
--- a/clang/tools/libclang/CIndex.cpp
+++ b/clang/tools/libclang/CIndex.cpp
@@ -1793,6 +1793,20 @@ bool CursorVisitor::VisitAdjustedTypeLoc(AdjustedTypeLoc 
TL) {
   return Visit(TL.getOriginalLoc());
 }
 
+bool CursorVisitor::VisitAutoTypeLoc(AutoTypeLoc TL) {
+
+  if (TL.isConstrained()) {
+    if (auto *CR = TL.getConceptReference()) {
+      if (CR->getNamedConcept()) {
+        return Visit(MakeCursorTemplateRef(CR->getNamedConcept(),
+                                           CR->getConceptNameLoc(), TU));
+      }
+    }
+  }
+
+  return false;
+}
+
 bool CursorVisitor::VisitDeducedTemplateSpecializationTypeLoc(
     DeducedTemplateSpecializationTypeLoc TL) {
   if (VisitTemplateName(TL.getTypePtr()->getTemplateName(),
@@ -1893,7 +1907,6 @@ DEFAULT_TYPELOC_IMPL(Enum, TagType)
 DEFAULT_TYPELOC_IMPL(SubstTemplateTypeParm, Type)
 DEFAULT_TYPELOC_IMPL(SubstTemplateTypeParmPack, Type)
 DEFAULT_TYPELOC_IMPL(SubstBuiltinTemplatePack, Type)
-DEFAULT_TYPELOC_IMPL(Auto, Type)
 DEFAULT_TYPELOC_IMPL(BitInt, Type)
 DEFAULT_TYPELOC_IMPL(DependentBitInt, Type)
 


        
_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to