[clang] 3c84e4a - [C11] Improve the diagnostic when accessing a member of an atomic struct
Aaron Ballman via cfe-commits
cfe-commits at lists.llvm.org
Tue Mar 29 09:16:14 PDT 2022
Author: Aaron Ballman
Date: 2022-03-29T12:14:36-04:00
New Revision: 3c84e4a0dbd08fc03bbcdd8354a984e0efcf7672
URL: https://github.com/llvm/llvm-project/commit/3c84e4a0dbd08fc03bbcdd8354a984e0efcf7672
DIFF: https://github.com/llvm/llvm-project/commit/3c84e4a0dbd08fc03bbcdd8354a984e0efcf7672.diff
LOG: [C11] Improve the diagnostic when accessing a member of an atomic struct
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.
Added:
Modified:
clang/docs/ReleaseNotes.rst
clang/include/clang/Basic/DiagnosticSemaKinds.td
clang/lib/Sema/SemaExprMember.cpp
clang/test/Sema/atomic-expr.c
Removed:
################################################################################
diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 9e12ad17beeb9..fb363ecb783df 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -85,6 +85,10 @@ Bug Fixes
- 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>`_,
diff --git a/clang/include/clang/Basic/DiagnosticSemaKinds.td b/clang/include/clang/Basic/DiagnosticSemaKinds.td
index d7a532856c2f1..172a10a65c8c0 100644
--- a/clang/include/clang/Basic/DiagnosticSemaKinds.td
+++ b/clang/include/clang/Basic/DiagnosticSemaKinds.td
@@ -6439,6 +6439,9 @@ def err_atomic_specifier_bad_type
"%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<
diff --git a/clang/lib/Sema/SemaExprMember.cpp b/clang/lib/Sema/SemaExprMember.cpp
index ad0999de034fc..152b39dabe361 100644
--- a/clang/lib/Sema/SemaExprMember.cpp
+++ b/clang/lib/Sema/SemaExprMember.cpp
@@ -1292,6 +1292,20 @@ static ExprResult LookupMemberExpr(Sema &S, LookupResult &R,
}
}
+ // 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;
diff --git a/clang/test/Sema/atomic-expr.c b/clang/test/Sema/atomic-expr.c
index 097ed4fd39ee4..0dceadf4e705c 100644
--- a/clang/test/Sema/atomic-expr.c
+++ b/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 @@ void func_15(void) {
_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}}
+}
More information about the cfe-commits
mailing list