[C] Silence unreachable -Watomic-access diagnostics (#140064)

Accessing the member of a structure or union which is _Atomic-qualified
is undefined behavior in C. We currently diagnose that with a warning
that defaults to an error. In turn, this means we reject a valid program
if the access it not reachable because of the error. e.g.,

  if (0)
    SomeAtomicStruct.Member = 12; // Was diagnosed

This silences the diagnostic if the member access is not reachable.
This commit is contained in:
Aaron Ballman
2025-05-15 12:24:12 -04:00
committed by GitHub
parent 5defe490c9
commit 0eb4bd27d1
3 changed files with 31 additions and 1 deletions

View File

@@ -533,6 +533,19 @@ Improvements to Clang's diagnostics
- A new off-by-default warning ``-Wms-bitfield-padding`` has been added to alert to cases where bit-field
packing may differ under the MS struct ABI (#GH117428).
- ``-Watomic-access`` no longer fires on unreachable code. e.g.,
.. code-block:: c
_Atomic struct S { int a; } s;
void func(void) {
if (0)
s.a = 12; // Previously diagnosed with -Watomic-access, now silenced
s.a = 12; // Still diagnosed with -Watomic-access
return;
s.a = 12; // Previously diagnosed, now silenced
}
- A new ``-Wcharacter-conversion`` warns where comparing or implicitly converting
between different Unicode character types (``char8_t``, ``char16_t``, ``char32_t``).

View File

@@ -1385,7 +1385,7 @@ static ExprResult LookupMemberExpr(Sema &S, LookupResult &R,
// lvalue. Because this is inherently unsafe as an atomic operation, the
// warning defaults to an error.
if (const auto *ATy = BaseType->getAs<AtomicType>()) {
S.DiagRuntimeBehavior(OpLoc, nullptr,
S.DiagRuntimeBehavior(OpLoc, BaseExpr.get(),
S.PDiag(diag::warn_atomic_member_access));
BaseType = ATy->getValueType().getUnqualifiedType();
BaseExpr = ImplicitCastExpr::Create(

View File

@@ -114,6 +114,23 @@ void func_16(void) {
(void)sizeof(xp->val);
(void)sizeof(y.ival);
(void)sizeof(yp->ival);
// Also, do not diagnose in unreachable code paths.
{
if (0) {
x.val = 12;
xp->val = 12;
(void)y.ival;
(void)yp->ival;
}
return;
x.val = 12;
xp->val = 12;
(void)y.ival;
(void)yp->ival;
}
}
// Ensure that we correctly implement assignment constraints from C2x 6.5.16.1.