[clang] [clang][libclang]Check auto type for type constraints (PR #172472)
via cfe-commits
cfe-commits at lists.llvm.org
Tue Dec 16 07:38:08 PST 2025
https://github.com/Serafean updated https://github.com/llvm/llvm-project/pull/172472
>From 17ce04400a351a0ebc17f2b69ef7e2f40080dc93 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Martin=20Bedn=C3=A1r?= <martin at serafean.cz>
Date: Tue, 16 Dec 2025 13:21:14 +0100
Subject: [PATCH] [clang][libclang]Check auto type for type constraints
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
---
clang/test/Index/index-auto.cpp | 18 ++++++++++++++++++
clang/tools/libclang/CIndex.cpp | 15 ++++++++++++++-
2 files changed, 32 insertions(+), 1 deletion(-)
create mode 100644 clang/test/Index/index-auto.cpp
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 32e84248c1b27..a1a59c9dea97d 100644
--- a/clang/tools/libclang/CIndex.cpp
+++ b/clang/tools/libclang/CIndex.cpp
@@ -1789,6 +1789,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(),
@@ -1889,7 +1903,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)
More information about the cfe-commits
mailing list