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

via cfe-commits cfe-commits at lists.llvm.org
Sun Sep 8 12:42:14 PDT 2024


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-clang

Author: Daniel Petrovic (daniel-petrovic)

<details>
<summary>Changes</summary>

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.



---
Full diff: https://github.com/llvm/llvm-project/pull/107773.diff


2 Files Affected:

- (modified) clang/lib/Sema/SemaExprCXX.cpp (+5-1) 
- (modified) clang/test/Sema/atomic-expr.c (+17) 


``````````diff
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;
+}

``````````

</details>


https://github.com/llvm/llvm-project/pull/107773


More information about the cfe-commits mailing list