https://github.com/mkovacevic99 updated https://github.com/llvm/llvm-project/pull/197874
>From d9ffe0e2f00b2207810ec6709a2e56bc03f97821 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 | 19 +++++++++++++--- clang/test/Sema/atomic-auto-type.c | 29 ++++++++++++++++++++++++ 4 files changed, 52 insertions(+), 3 deletions(-) create mode 100644 clang/test/Sema/atomic-auto-type.c diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst index 745f9e6556eec..c355f7d0b5984 100644 --- a/clang/docs/ReleaseNotes.rst +++ b/clang/docs/ReleaseNotes.rst @@ -797,6 +797,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. .. comment: This is for the Static Analyzer. diff --git a/clang/lib/AST/Type.cpp b/clang/lib/AST/Type.cpp index 96a398aa21dad..cf42fafab6b9e 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 c04fff6cbd964..78fd5e685cdcd 100644 --- a/clang/lib/Sema/SemaTemplateDeduction.cpp +++ b/clang/lib/Sema/SemaTemplateDeduction.cpp @@ -1902,8 +1902,16 @@ static TemplateDeductionResult DeduceTemplateArgumentsByTypeMatch( // _Atomic T [extension] case Type::Atomic: { const auto *PA = P->castAs<AtomicType>(), *AA = A->getAs<AtomicType>(); - if (!AA) - return TemplateDeductionResult::NonDeducedMismatch; + if (!AA) { + if (!P->getContainedAutoType()) + return TemplateDeductionResult::NonDeducedMismatch; + // If it contains an auto type, we can try to deduce from the value type + // of the atomic type. + return DeduceTemplateArgumentsByTypeMatch( + S, TemplateParams, PA->getValueType(), A, Info, Deduced, TDF, + degradeCallPartialOrderingKind(POK), + /*DeducedFromArrayBound=*/false, HasDeducedAnyParam); + } return DeduceTemplateArgumentsByTypeMatch( S, TemplateParams, PA->getValueType(), AA->getValueType(), Info, Deduced, TDF, degradeCallPartialOrderingKind(POK), @@ -3744,7 +3752,12 @@ CheckOriginalCallArgDeduction(Sema &S, TemplateDeductionInfo &Info, QualType OriginalParamType = OriginalArg.OriginalParamType; // Check for type equality (top-level cv-qualifiers are ignored). - if (Context.hasSameUnqualifiedType(A, DeducedA)) + // If it's an atomic type, we need to check contained value type as well since + // it can be an auto type. + if (Context.hasSameUnqualifiedType(A, DeducedA) || + (DeducedA->isAtomicType() && + Context.hasSameUnqualifiedType( + A, cast<AtomicType>(DeducedA)->getValueType()))) return TemplateDeductionResult::Success; // Strip off references on the argument types; they aren't needed for diff --git a/clang/test/Sema/atomic-auto-type.c b/clang/test/Sema/atomic-auto-type.c new file mode 100644 index 0000000000000..245b5621e59f4 --- /dev/null +++ b/clang/test/Sema/atomic-auto-type.c @@ -0,0 +1,29 @@ +// RUN: %clang_cc1 -triple x86_64-pc-linux-gnu -emit-llvm -o - %s + +// 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. + +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
