https://github.com/mkovacevic99 updated 
https://github.com/llvm/llvm-project/pull/197874

>From ecb0a38ef34bca16a5be025129d668fd3187c22c Mon Sep 17 00:00:00 2001
From: Milica Kovacevic <[email protected]>
Date: Fri, 15 May 2026 08:43:37 +0200
Subject: [PATCH] [Clang] Handle deduced auto types within AtomicType

---
 clang/docs/ReleaseNotes.rst              |  3 +++
 clang/lib/AST/Type.cpp                   |  4 ++++
 clang/lib/Sema/SemaTemplateDeduction.cpp | 21 ++++++++++++++++++-
 clang/test/CodeGen/atomic-auto-type.c    | 10 +++++++++
 clang/test/Sema/atomic-auto-type.c       | 26 ++++++++++++++++++++++++
 5 files changed, 63 insertions(+), 1 deletion(-)
 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..2fbfe157d71ba 100644
--- a/clang/lib/Sema/SemaTemplateDeduction.cpp
+++ b/clang/lib/Sema/SemaTemplateDeduction.cpp
@@ -3742,7 +3742,13 @@ CheckOriginalCallArgDeduction(Sema &S, 
TemplateDeductionInfo &Info,
   QualType OriginalParamType = OriginalArg.OriginalParamType;
 
   // Check for type equality (top-level cv-qualifiers are ignored).
-  if (Context.hasSameUnqualifiedType(A, DeducedA))
+  // _Atomic is treated as a qualifier, so strip it from both sides.
+  auto StripAtomic = [](QualType T) -> QualType {
+    if (const auto *AT = T->getAs<AtomicType>())
+      return AT->getValueType();
+    return T;
+  };
+  if (Context.hasSameUnqualifiedType(StripAtomic(A), StripAtomic(DeducedA)))
     return TemplateDeductionResult::Success;
 
   // Strip off references on the argument types; they aren't needed for
@@ -5112,6 +5118,19 @@ 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.
+      if (!Replacement.isNull() && Replacement->isTemplateTypeParmType() &&
+          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;
+}

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

Reply via email to