[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 14:37:40 PDT 2024


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

>From 20f065d70ab4ff8b3f77b53db6ce6cf1033e00aa 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-rvalue.c | 23 +++++++++++++++++++++++
 2 files changed, 28 insertions(+), 1 deletion(-)
 create mode 100644 clang/test/Sema/atomic-expr-rvalue.c

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-rvalue.c b/clang/test/Sema/atomic-expr-rvalue.c
new file mode 100644
index 00000000000000..8314afd9ecb051
--- /dev/null
+++ b/clang/test/Sema/atomic-expr-rvalue.c
@@ -0,0 +1,23 @@
+// RUN: %clang_cc1 -fsyntax-only %s -verify
+// expected-no-diagnostics
+
+typedef _Atomic char atomic_char;
+atomic_char counter;
+
+// Check correct implicit conversions of r-value atomic expressions.
+// Bugfix: https://github.com/llvm/llvm-project/issues/106576
+char load_plus_one_stmtexpr() {
+  return ({counter;}) + 1;
+}
+
+char load_stmtexpr() {
+  return ({counter;});
+}
+
+char load_cast_plus_one() {
+  return (atomic_char)('x') + 1;
+}
+
+char load_cast() {
+  return (atomic_char)('x');
+}



More information about the cfe-commits mailing list