Index: clang/docs/ReleaseNotes.rst =================================================================== --- clang/docs/ReleaseNotes.rst +++ clang/docs/ReleaseNotes.rst @@ -85,6 +85,10 @@ - Assignment expressions in C11 and later mode now properly strip the _Atomic qualifier when determining the type of the assignment expression. Fixes `Issue 48742 `_. +- Improved the diagnostic when accessing a member of an atomic structure or + union object in C; was previously an unhelpful error, but now issues a + `-Watomic-access` warning which defaults to an error. Fixes + `Issue 54563 `_. - Unevaluated lambdas in dependant contexts no longer result in clang crashing. This fixes Issues `50376 `_, `51414 `_, Index: clang/include/clang/Basic/DiagnosticSemaKinds.td =================================================================== --- clang/include/clang/Basic/DiagnosticSemaKinds.td +++ clang/include/clang/Basic/DiagnosticSemaKinds.td @@ -6439,6 +6439,9 @@ "%select{incomplete |array |function |reference |atomic |qualified " "|sizeless ||integer }0type " "%1 %select{|||||||which is not trivially copyable|}0">; +def warn_atomic_member_access : Warning< + "accessing a member of an atomic structure or union is undefined behavior">, + InGroup>, DefaultError; // Expressions. def ext_sizeof_alignof_function_type : Extension< Index: clang/lib/Sema/SemaExprMember.cpp =================================================================== --- clang/lib/Sema/SemaExprMember.cpp +++ clang/lib/Sema/SemaExprMember.cpp @@ -1292,6 +1292,20 @@ } } + // If the base type is an atomic type, this access is undefined behavior per + // C11 6.5.2.3p5. Instead of giving a typecheck error, we'll warn the user + // about the UB and recover by converting the atomic lvalue into a non-atomic + // lvalue. Because this is inherently unsafe as an atomic operation, the + // warning defaults to an error. + if (const auto *ATy = BaseType->getAs()) { + S.Diag(OpLoc, diag::warn_atomic_member_access); + BaseType = ATy->getValueType().getUnqualifiedType(); + BaseExpr = ImplicitCastExpr::Create( + S.Context, IsArrow ? S.Context.getPointerType(BaseType) : BaseType, + CK_AtomicToNonAtomic, BaseExpr.get(), nullptr, + BaseExpr.get()->getValueKind(), FPOptionsOverride()); + } + // Handle field access to simple records. if (const RecordType *RTy = BaseType->getAs()) { TypoExpr *TE = nullptr; Index: clang/test/Sema/atomic-expr.c =================================================================== --- clang/test/Sema/atomic-expr.c +++ clang/test/Sema/atomic-expr.c @@ -1,5 +1,6 @@ // RUN: %clang_cc1 %s -verify -fsyntax-only -// expected-no-diagnostics +// RUN: %clang_cc1 %s -verify=off -fsyntax-only -Wno-atomic-access +// off-no-diagnostics _Atomic(unsigned int) data1; int _Atomic data2; @@ -75,3 +76,30 @@ _Static_assert(__builtin_types_compatible_p(__typeof__(x = 2), int), "incorrect"); _Static_assert(__builtin_types_compatible_p(__typeof__(x += 2), int), "incorrect"); } + +// Ensure that member access of an atomic structure or union type is properly +// diagnosed as being undefined behavior; Issue 54563. +void func_16(void) { + // LHS member access. + _Atomic struct { int val; } x, *xp; + x.val = 12; // expected-error {{accessing a member of an atomic structure or union is undefined behavior}} + xp->val = 12; // expected-error {{accessing a member of an atomic structure or union is undefined behavior}} + + _Atomic union { + int ival; + float fval; + } y, *yp; + y.ival = 12; // expected-error {{accessing a member of an atomic structure or union is undefined behavior}} + yp->fval = 1.2f; // expected-error {{accessing a member of an atomic structure or union is undefined behavior}} + + // RHS member access. + int xval = x.val; // expected-error {{accessing a member of an atomic structure or union is undefined behavior}} + xval = xp->val; // expected-error {{accessing a member of an atomic structure or union is undefined behavior}} + int yval = y.val; // expected-error {{accessing a member of an atomic structure or union is undefined behavior}} + yval = yp->val; // expected-error {{accessing a member of an atomic structure or union is undefined behavior}} + + // Using the type specifier instead of the type qualifier. + _Atomic(struct { int val; }) z; + z.val = 12; // expected-error {{accessing a member of an atomic structure or union is undefined behavior}} + int zval = z.val; // expected-error {{accessing a member of an atomic structure or union is undefined behavior}} +}