[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 08:19:38 PDT 2022
aaron.ballman updated this revision to Diff 418884.
aaron.ballman marked 2 inline comments as done.
aaron.ballman added a comment.
Added test cases based on review feedback.
CHANGES SINCE LAST ACTION
https://reviews.llvm.org/D122656/new/
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,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}}
+}
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.418884.patch
Type: text/x-patch
Size: 4911 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20220329/eeb0575a/attachment.bin>
More information about the cfe-commits
mailing list