[clang] [Clang] Handle deduced auto types within AtomicType (PR #197874)
via cfe-commits
cfe-commits at lists.llvm.org
Mon Jun 29 01:11:51 PDT 2026
https://github.com/mkovacevic99 updated https://github.com/llvm/llvm-project/pull/197874
>From 3828c304a3c215a88243a90cdd976866bc4b5317 Mon Sep 17 00:00:00 2001
From: Milica Kovacevic <mkovacevic at baylibre.com>
Date: Fri, 15 May 2026 08:43:37 +0200
Subject: [PATCH 1/2] [Clang] Handle deduced auto types within AtomicType
---
clang/docs/ReleaseNotes.rst | 3 +++
clang/lib/AST/Type.cpp | 4 ++++
clang/lib/Sema/SemaTemplateDeduction.cpp | 25 +++++++++++++++++++++--
clang/test/CodeGen/atomic-auto-type.c | 10 +++++++++
clang/test/Sema/atomic-auto-type.c | 26 ++++++++++++++++++++++++
5 files changed, 66 insertions(+), 2 deletions(-)
create mode 100644 clang/test/CodeGen/atomic-auto-type.c
create mode 100644 clang/test/Sema/atomic-auto-type.c
diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index e86f1d9602bed..7389a6ea72d3e 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -1041,6 +1041,9 @@ Crash and bug fixes
- Fixed ``security.VAList`` checker producing false positives when analyzing
C23 code where ``va_start`` expands to ``__builtin_c23_va_start``.
+
+- Fixed a compiler crash when combining ``_Atomic`` and ``__auto_type``
+ in C, for example ``_Atomic __auto_type x = expr``. Fixes #118058.
Improvements
^^^^^^^^^^^^
diff --git a/clang/lib/AST/Type.cpp b/clang/lib/AST/Type.cpp
index b7bef40ca89f3..42d148715bc40 100644
--- a/clang/lib/AST/Type.cpp
+++ b/clang/lib/AST/Type.cpp
@@ -2102,6 +2102,10 @@ class GetContainedDeducedTypeVisitor
Type *VisitPackExpansionType(const PackExpansionType *T) {
return Visit(T->getPattern());
}
+
+ Type *VisitAtomicType(const AtomicType *T) {
+ return Visit(T->getValueType());
+ }
};
} // namespace
diff --git a/clang/lib/Sema/SemaTemplateDeduction.cpp b/clang/lib/Sema/SemaTemplateDeduction.cpp
index 26c397afdd6ef..d93b528facbcc 100644
--- a/clang/lib/Sema/SemaTemplateDeduction.cpp
+++ b/clang/lib/Sema/SemaTemplateDeduction.cpp
@@ -3741,8 +3741,10 @@ CheckOriginalCallArgDeduction(Sema &S, TemplateDeductionInfo &Info,
QualType A = OriginalArg.OriginalArgType;
QualType OriginalParamType = OriginalArg.OriginalParamType;
- // Check for type equality (top-level cv-qualifiers are ignored).
- if (Context.hasSameUnqualifiedType(A, DeducedA))
+ // Check for type equality (top-level cv-qualifiers and _Atomic are ignored,
+ // since _Atomic is treated as a qualifier).
+ if (Context.hasSameType(A.getAtomicUnqualifiedType(),
+ DeducedA.getAtomicUnqualifiedType()))
return TemplateDeductionResult::Success;
// Strip off references on the argument types; they aren't needed for
@@ -5112,6 +5114,25 @@ namespace {
return Result;
}
+ QualType TransformAtomicType(TypeLocBuilder &TLB, AtomicTypeLoc TL) {
+ // When building the function parameter for placeholder type deduction
+ // (Replacement is the invented template parameter), dig through _Atomic
+ // around an auto placeholder so deduction matches the non-atomic
+ // argument. The _Atomic wrapper is re-applied by the final substitution
+ // pass, which uses a concrete Replacement and falls through to the
+ // default transform.
+ //
+ // This handles only the simple case where _Atomic wraps auto directly
+ // (e.g. _Atomic(auto)), which is what the C standard currently permits.
+ // If more complex forms such as _Atomic(auto*) are ever allowed, the
+ // correct fix would be to treat _Atomic as a qualifier inside
+ // DeduceTemplateArgumentsByTypeMatch instead.
+ if (isa_and_nonnull<TemplateTypeParmType>(Replacement) &&
+ TL.getValueLoc().getType()->getContainedAutoType())
+ return getDerived().TransformType(TLB, TL.getValueLoc());
+ return inherited::TransformAtomicType(TLB, TL);
+ }
+
ExprResult TransformLambdaExpr(LambdaExpr *E) {
// Lambdas never need to be transformed.
return E;
diff --git a/clang/test/CodeGen/atomic-auto-type.c b/clang/test/CodeGen/atomic-auto-type.c
new file mode 100644
index 0000000000000..0969f22596421
--- /dev/null
+++ b/clang/test/CodeGen/atomic-auto-type.c
@@ -0,0 +1,10 @@
+// RUN: %clang_cc1 -triple x86_64-pc-linux-gnu -emit-llvm -o - %s > /dev/null
+
+// This is a regression test for handling of __auto_type inside _Atomic.
+// Previously this could lead to an undeduced AutoType escaping into
+// ASTContext::getTypeInfoImpl and causing an assertion failure.
+
+void f(double x) {
+ __auto_type _Atomic xa = x;
+ _Atomic __auto_type ax = x;
+}
diff --git a/clang/test/Sema/atomic-auto-type.c b/clang/test/Sema/atomic-auto-type.c
new file mode 100644
index 0000000000000..ff067662a759e
--- /dev/null
+++ b/clang/test/Sema/atomic-auto-type.c
@@ -0,0 +1,26 @@
+// RUN: %clang_cc1 -triple x86_64-pc-linux-gnu -fsyntax-only %s
+// expected-no-diagnostics
+
+int main() {
+ double x = 37;
+
+ __auto_type _Atomic xa = x;
+ _Atomic __auto_type ax = x;
+
+ _Static_assert(
+ __builtin_types_compatible_p(__typeof(xa), _Atomic double),
+ "incorrect xa type");
+
+ _Static_assert(
+ __builtin_types_compatible_p(__typeof(ax), _Atomic double),
+ "incorrect ax type");
+
+ _Static_assert(
+ __builtin_types_compatible_p(_Atomic double, __typeof(xa)),
+ "incorrect");
+
+ _Static_assert(
+ __builtin_types_compatible_p(_Atomic double, __typeof(ax)),
+ "incorrect");
+ return 0;
+}
>From 4a15191ffde943c893ac2e043731bf66af14e988 Mon Sep 17 00:00:00 2001
From: mkovacevic99 <mkovacevic at baylibre.com>
Date: Mon, 29 Jun 2026 10:11:41 +0200
Subject: [PATCH 2/2] Update clang/docs/ReleaseNotes.rst
Co-authored-by: Corentin Jabot <corentinjabot at gmail.com>
---
clang/docs/ReleaseNotes.rst | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 7389a6ea72d3e..844d7e6804f99 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -1043,7 +1043,7 @@ Crash and bug fixes
C23 code where ``va_start`` expands to ``__builtin_c23_va_start``.
- Fixed a compiler crash when combining ``_Atomic`` and ``__auto_type``
- in C, for example ``_Atomic __auto_type x = expr``. Fixes #118058.
+ in C, for example ``_Atomic __auto_type x = expr``. (#GH118058)
Improvements
^^^^^^^^^^^^
More information about the cfe-commits
mailing list