[clang] [Clang] Do not perform integral promotion if operands of expression `x ? : y` have the same type (PR #108837)

Yanzuo Liu via cfe-commits cfe-commits at lists.llvm.org
Mon Sep 16 08:06:10 PDT 2024


https://github.com/zwuis updated https://github.com/llvm/llvm-project/pull/108837

>From 7e5f88c322852939ae68c65f6adf4a5d2973d095 Mon Sep 17 00:00:00 2001
From: Yanzuo Liu <zwuis at outlook.com>
Date: Mon, 16 Sep 2024 21:50:11 +0800
Subject: [PATCH 1/2] Fix computing result type of conditional operand

---
 clang/docs/ReleaseNotes.rst                |  2 ++
 clang/lib/Sema/SemaExpr.cpp                |  4 +---
 clang/test/SemaCXX/conditional-gnu-ext.cpp | 19 +++++++++++++++++++
 3 files changed, 22 insertions(+), 3 deletions(-)
 create mode 100644 clang/test/SemaCXX/conditional-gnu-ext.cpp

diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 17ec1fe0b946de..db8a7568a12114 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -389,6 +389,8 @@ Bug Fixes to C++ Support
 - Fixed a crash when clang tries to subtitute parameter pack while retaining the parameter
   pack. #GH63819, #GH107560
 - Fix a crash when a static assert declaration has an invalid close location. (#GH108687)
+- Fixed a bug in computing result type of conditional operator with omitted middle operand
+  (a GNU extension). (#GH15998)
 
 Bug Fixes to AST Handling
 ^^^^^^^^^^^^^^^^^^^^^^^^^
diff --git a/clang/lib/Sema/SemaExpr.cpp b/clang/lib/Sema/SemaExpr.cpp
index 8f3e15cc9a9bb7..8414a55e4868b9 100644
--- a/clang/lib/Sema/SemaExpr.cpp
+++ b/clang/lib/Sema/SemaExpr.cpp
@@ -8785,11 +8785,9 @@ ExprResult Sema::ActOnConditionalOp(SourceLocation QuestionLoc,
       commonExpr = result.get();
     }
     // We usually want to apply unary conversions *before* saving, except
-    // in the special case of a C++ l-value conditional.
+    // in the special case in C++ that operands have the same type.
     if (!(getLangOpts().CPlusPlus
           && !commonExpr->isTypeDependent()
-          && commonExpr->getValueKind() == RHSExpr->getValueKind()
-          && commonExpr->isGLValue()
           && commonExpr->isOrdinaryOrBitFieldObject()
           && RHSExpr->isOrdinaryOrBitFieldObject()
           && Context.hasSameType(commonExpr->getType(), RHSExpr->getType()))) {
diff --git a/clang/test/SemaCXX/conditional-gnu-ext.cpp b/clang/test/SemaCXX/conditional-gnu-ext.cpp
new file mode 100644
index 00000000000000..83a6fff8467863
--- /dev/null
+++ b/clang/test/SemaCXX/conditional-gnu-ext.cpp
@@ -0,0 +1,19 @@
+// RUN: %clang_cc1 -fsyntax-only -verify -std=c++11 %s
+// RUN: %clang_cc1 -fsyntax-only -verify -std=c++11 %s -fexperimental-new-constant-interpreter
+// expected-no-diagnostics
+
+/*
+FIXME: Support constexpr
+
+constexpr int evaluate_once(int x) {
+  return (++x) ? : 10;
+}
+static_assert(evaluate_once(0) == 1, "");
+*/
+
+namespace GH15998 {
+  enum E { Zero, One };
+  E test(E e) {
+    return e ? : One;
+  }
+}

>From 447c8b9c6e957308c9ff62e8c83b15b12f7fc1cb Mon Sep 17 00:00:00 2001
From: Yanzuo Liu <zwuis at outlook.com>
Date: Mon, 16 Sep 2024 23:05:50 +0800
Subject: [PATCH 2/2] Format code

---
 clang/lib/Sema/SemaExpr.cpp | 9 ++++-----
 1 file changed, 4 insertions(+), 5 deletions(-)

diff --git a/clang/lib/Sema/SemaExpr.cpp b/clang/lib/Sema/SemaExpr.cpp
index 8414a55e4868b9..c232d40ca31ac6 100644
--- a/clang/lib/Sema/SemaExpr.cpp
+++ b/clang/lib/Sema/SemaExpr.cpp
@@ -8786,11 +8786,10 @@ ExprResult Sema::ActOnConditionalOp(SourceLocation QuestionLoc,
     }
     // We usually want to apply unary conversions *before* saving, except
     // in the special case in C++ that operands have the same type.
-    if (!(getLangOpts().CPlusPlus
-          && !commonExpr->isTypeDependent()
-          && commonExpr->isOrdinaryOrBitFieldObject()
-          && RHSExpr->isOrdinaryOrBitFieldObject()
-          && Context.hasSameType(commonExpr->getType(), RHSExpr->getType()))) {
+    if (!(getLangOpts().CPlusPlus && !commonExpr->isTypeDependent() &&
+          commonExpr->isOrdinaryOrBitFieldObject() &&
+          RHSExpr->isOrdinaryOrBitFieldObject() &&
+          Context.hasSameType(commonExpr->getType(), RHSExpr->getType()))) {
       ExprResult commonRes = UsualUnaryConversions(commonExpr);
       if (commonRes.isInvalid())
         return ExprError();



More information about the cfe-commits mailing list