[PATCH] D122656: [C11] Improve the diagnostic when accessing a member of an atomic struct
Aaron Ballman via Phabricator via cfe-commits
cfe-commits at lists.llvm.org
Tue Mar 29 07:16:43 PDT 2022
aaron.ballman created this revision.
aaron.ballman added reviewers: eli.friedman, rjmccall, jyknight, erichkeane, clang-language-wg.
Herald added a project: All.
aaron.ballman requested review of this revision.
Herald added a project: clang.
Member access for an atomic structure or union is unconditional undefined behavior (C11 6.5.2.3p5). However, we would issue a confusing error message about the base expression not being a structure or union type.
GCC issues a warning for this case. Clang now warns as well, but the warning is defaulted to an error because the actual access is still unsafe.
This fixes Issue 54563.
Repository:
rG LLVM Github Monorepo
https://reviews.llvm.org/D122656
Files:
clang/docs/ReleaseNotes.rst
clang/include/clang/Basic/DiagnosticSemaKinds.td
clang/lib/Sema/SemaExprMember.cpp
clang/test/Sema/atomic-expr.c
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,18 @@
_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) {
+ _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}}
+}
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<AtomicType>()) {
+ 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<RecordType>()) {
TypoExpr *TE = nullptr;
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<DiagGroup<"atomic-access">>, DefaultError;
// Expressions.
def ext_sizeof_alignof_function_type : Extension<
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 <https://github.com/llvm/llvm-project/issues/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 <https://github.com/llvm/llvm-project/issues/54563>`_.
- Unevaluated lambdas in dependant contexts no longer result in clang crashing.
This fixes Issues `50376 <https://github.com/llvm/llvm-project/issues/50376>`_,
`51414 <https://github.com/llvm/llvm-project/issues/51414>`_,
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D122656.418866.patch
Type: text/x-patch
Size: 4060 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20220329/df29d6a4/attachment.bin>
More information about the cfe-commits
mailing list