[PATCH] D122681: Don't diagnostic atomic object access as UB in an unevaluated context

Aaron Ballman via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Tue Mar 29 12:37:34 PDT 2022


aaron.ballman created this revision.
aaron.ballman added reviewers: jyknight, erichkeane, clang-language-wg.
Herald added a project: All.
aaron.ballman requested review of this revision.
Herald added a project: clang.

We started diagnosing this situation with a more clear diagnostic message, but it was pointed out that unevaluated contexts don't really have the undefined behavior property as there is no runtime access involved.

This augments the changes in https://reviews.llvm.org/D122656 to not diagnose in an unevaluated context.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D122681

Files:
  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
@@ -102,4 +102,10 @@
   _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}}
+
+  // Don't diagnose in an unevaluated context, however.
+  (void)sizeof(x.val);
+  (void)sizeof(xp->val);
+  (void)sizeof(y.ival);
+  (void)sizeof(yp->ival);
 }
Index: clang/lib/Sema/SemaExprMember.cpp
===================================================================
--- clang/lib/Sema/SemaExprMember.cpp
+++ clang/lib/Sema/SemaExprMember.cpp
@@ -1298,7 +1298,8 @@
   // 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);
+    S.DiagRuntimeBehavior(OpLoc, nullptr,
+                          S.PDiag(diag::warn_atomic_member_access));
     BaseType = ATy->getValueType().getUnqualifiedType();
     BaseExpr = ImplicitCastExpr::Create(
         S.Context, IsArrow ? S.Context.getPointerType(BaseType) : BaseType,


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D122681.418957.patch
Type: text/x-patch
Size: 1345 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20220329/9f0e849c/attachment-0001.bin>


More information about the cfe-commits mailing list