[clang] [clang][Frontend] Fix Sema::PerformImplicitConversion for atomic expressions (PR #107773)

Daniel Petrovic via cfe-commits cfe-commits at lists.llvm.org
Sun Sep 8 12:41:29 PDT 2024


https://github.com/daniel-petrovic created https://github.com/llvm/llvm-project/pull/107773

Fixes #106576 

In `Sema::PerformImplicitConversion` for standard conversion sequence the NonAtomicToAtomic conversion is not reverted back after 3.rd conversion step if not starting from scalar initially.

This solves this issue and also other issues  like e.g. returning r-value atomic expressions where AtomicToNonAtomic conversion should also normally occur.

Basically this means that atomicity is stripped after lvalue to rvalue conversoin of an atomic expression.



>From 3ab605f2d939130d973892d21e538631cebb42bc Mon Sep 17 00:00:00 2001
From: Daniel Petrovic <daniel.petrovic at ebcont.com>
Date: Sun, 8 Sep 2024 21:29:57 +0200
Subject: [PATCH] [clang][Frontend] Fix Sema::PerformImplicitConversion for
 atomic expressions

---
 clang/lib/Sema/SemaExprCXX.cpp |  6 +++++-
 clang/test/Sema/atomic-expr.c  | 17 +++++++++++++++++
 2 files changed, 22 insertions(+), 1 deletion(-)

diff --git a/clang/lib/Sema/SemaExprCXX.cpp b/clang/lib/Sema/SemaExprCXX.cpp
index 746c67ff1e979f..24d0df132d6648 100644
--- a/clang/lib/Sema/SemaExprCXX.cpp
+++ b/clang/lib/Sema/SemaExprCXX.cpp
@@ -4393,6 +4393,10 @@ Sema::PerformImplicitConversion(Expr *From, QualType ToType,
     ToType = ToAtomic->getValueType();
   }
 
+  // Save the initial `atomicity` information of the From-expression for later check
+  // before it's potentially removed in the StandardConversionSequence steps 1-3.
+  const AtomicType* StartingFromAtomic = From->getType()->getAs<AtomicType>();
+
   QualType InitialFromType = FromType;
   // Perform the first implicit conversion.
   switch (SCS.First) {
@@ -4918,7 +4922,7 @@ Sema::PerformImplicitConversion(Expr *From, QualType ToType,
 
   // If this conversion sequence involved a scalar -> atomic conversion, perform
   // that conversion now.
-  if (!ToAtomicType.isNull()) {
+  if (!StartingFromAtomic && !ToAtomicType.isNull()) {
     assert(Context.hasSameType(
         ToAtomicType->castAs<AtomicType>()->getValueType(), From->getType()));
     From = ImpCastExprToType(From, ToAtomicType, CK_NonAtomicToAtomic,
diff --git a/clang/test/Sema/atomic-expr.c b/clang/test/Sema/atomic-expr.c
index 7e5219dd3f14ab..167de036ae73d6 100644
--- a/clang/test/Sema/atomic-expr.c
+++ b/clang/test/Sema/atomic-expr.c
@@ -227,3 +227,20 @@ void func_19(void) {
   _Atomic int a = 0;
   switch (a) { }
 }
+
+// Test correct implicit conversions of r-value atomic expressions.
+// Bugfix: https://github.com/llvm/llvm-project/issues/106576
+typedef _Atomic char atomic_char;
+atomic_char counter;
+char load_stmtexpr() {
+  return ({counter;});
+}
+char load_plus_one_stmtexpr() {
+  return ({counter;}) + 1;
+}
+char load_cast() {
+  return (atomic_char)('x');
+}
+char load_cast_plus_one() {
+  return (atomic_char)('x') + 1;
+}



More information about the cfe-commits mailing list