[clang] [clang][bytecode] Fix crash on discarded complex comparison (PR #177731)
via cfe-commits
cfe-commits at lists.llvm.org
Sun Jan 25 10:20:14 PST 2026
https://github.com/FYLGQ updated https://github.com/llvm/llvm-project/pull/177731
>From 128f0bf6edc86c0f1c1fd279466afccc95e1ebfc Mon Sep 17 00:00:00 2001
From: FYLGQ <799648409 at qq.com>
Date: Sat, 24 Jan 2026 10:58:51 +0800
Subject: [PATCH 1/4] fix-constant-eval-complex-discard
---
clang/lib/AST/ByteCode/Compiler.cpp | 3 ++-
clang/lib/AST/ByteCode/constant-eval-complex-discard.c | 6 ++++++
2 files changed, 8 insertions(+), 1 deletion(-)
create mode 100644 clang/lib/AST/ByteCode/constant-eval-complex-discard.c
diff --git a/clang/lib/AST/ByteCode/Compiler.cpp b/clang/lib/AST/ByteCode/Compiler.cpp
index 272d08f5e455c..d864fdc5d7e6d 100644
--- a/clang/lib/AST/ByteCode/Compiler.cpp
+++ b/clang/lib/AST/ByteCode/Compiler.cpp
@@ -7406,7 +7406,8 @@ bool Compiler<Emitter>::emitComplexComparison(const Expr *LHS, const Expr *RHS,
const BinaryOperator *E) {
assert(E->isComparisonOp());
assert(!Initializing);
- assert(!DiscardResult);
+ if (DiscardResult)
+ return true;
PrimType ElemT;
bool LHSIsComplex;
diff --git a/clang/lib/AST/ByteCode/constant-eval-complex-discard.c b/clang/lib/AST/ByteCode/constant-eval-complex-discard.c
new file mode 100644
index 0000000000000..1d111fc049cc1
--- /dev/null
+++ b/clang/lib/AST/ByteCode/constant-eval-complex-discard.c
@@ -0,0 +1,6 @@
+// RUN: %clang_cc1 -std=c11 -fsyntax-only -fexperimental-new-constant-interpreter %s
+
+void foo(void) {
+ // Complex comparison evaluated in a discarded context.
+ (void)(0 && (1i == 1i));
+}
>From b71ec175022ef25291c4bffb5bea65748730c430 Mon Sep 17 00:00:00 2001
From: FYLGQ <799648409 at qq.com>
Date: Sat, 24 Jan 2026 12:36:01 +0800
Subject: [PATCH 2/4] Move test file constant-eval-complex-discard.c to
clang/test/AST/ByteCode
---
clang/{lib => test}/AST/ByteCode/constant-eval-complex-discard.c | 0
1 file changed, 0 insertions(+), 0 deletions(-)
rename clang/{lib => test}/AST/ByteCode/constant-eval-complex-discard.c (100%)
diff --git a/clang/lib/AST/ByteCode/constant-eval-complex-discard.c b/clang/test/AST/ByteCode/constant-eval-complex-discard.c
similarity index 100%
rename from clang/lib/AST/ByteCode/constant-eval-complex-discard.c
rename to clang/test/AST/ByteCode/constant-eval-complex-discard.c
>From ca0d29573e2182cc78f0036f17542a94b8231c50 Mon Sep 17 00:00:00 2001
From: FYLGQ <799648409 at qq.com>
Date: Sun, 25 Jan 2026 17:47:25 +0800
Subject: [PATCH 3/4] [clang][bytecode] Fix crash on discarded complex
comparison
---
clang/lib/AST/ByteCode/Compiler.cpp | 7 ++++++-
.../AST/ByteCode/constant-eval-complex-discard.c | 6 ------
.../ByteCode/constant-eval-complex-discard.cpp | 16 ++++++++++++++++
3 files changed, 22 insertions(+), 7 deletions(-)
delete mode 100644 clang/test/AST/ByteCode/constant-eval-complex-discard.c
create mode 100644 clang/test/AST/ByteCode/constant-eval-complex-discard.cpp
diff --git a/clang/lib/AST/ByteCode/Compiler.cpp b/clang/lib/AST/ByteCode/Compiler.cpp
index d864fdc5d7e6d..5db454c797565 100644
--- a/clang/lib/AST/ByteCode/Compiler.cpp
+++ b/clang/lib/AST/ByteCode/Compiler.cpp
@@ -7406,8 +7406,13 @@ bool Compiler<Emitter>::emitComplexComparison(const Expr *LHS, const Expr *RHS,
const BinaryOperator *E) {
assert(E->isComparisonOp());
assert(!Initializing);
- if (DiscardResult)
+if (DiscardResult) {
+ if (!this->discard(LHS))
+ return false;
+ if (!this->discard(RHS))
+ return false;
return true;
+}
PrimType ElemT;
bool LHSIsComplex;
diff --git a/clang/test/AST/ByteCode/constant-eval-complex-discard.c b/clang/test/AST/ByteCode/constant-eval-complex-discard.c
deleted file mode 100644
index 1d111fc049cc1..0000000000000
--- a/clang/test/AST/ByteCode/constant-eval-complex-discard.c
+++ /dev/null
@@ -1,6 +0,0 @@
-// RUN: %clang_cc1 -std=c11 -fsyntax-only -fexperimental-new-constant-interpreter %s
-
-void foo(void) {
- // Complex comparison evaluated in a discarded context.
- (void)(0 && (1i == 1i));
-}
diff --git a/clang/test/AST/ByteCode/constant-eval-complex-discard.cpp b/clang/test/AST/ByteCode/constant-eval-complex-discard.cpp
new file mode 100644
index 0000000000000..6c2cf5739ba13
--- /dev/null
+++ b/clang/test/AST/ByteCode/constant-eval-complex-discard.cpp
@@ -0,0 +1,16 @@
+// RUN: %clang_cc1 -std=c++20 -fsyntax-only -verify -fexperimental-new-constant-interpreter %s
+// RUN: %clang_cc1 -std=c++20 -fsyntax-only -verify %s
+
+// expected-no-diagnostics
+
+void test_no_crash() {
+ _Complex int x = 1i;
+ (void)(x == 1i);
+}
+
+constexpr int test_side_effect() {
+ int k = 0;
+ (void)(1i == (++k, 1i));
+ return k;
+}
+static_assert(test_side_effect() == 1);
\ No newline at end of file
>From 6710445dfdb18ef977729196156a841abcb42925 Mon Sep 17 00:00:00 2001
From: FYLGQ <799648409 at qq.com>
Date: Mon, 26 Jan 2026 02:19:54 +0800
Subject: [PATCH 4/4] Address review comments: simplify code and move test
---
clang/lib/AST/ByteCode/Compiler.cpp | 9 ++-------
clang/test/AST/ByteCode/complex.cpp | 13 +++++++++++++
.../ByteCode/constant-eval-complex-discard.cpp | 16 ----------------
3 files changed, 15 insertions(+), 23 deletions(-)
delete mode 100644 clang/test/AST/ByteCode/constant-eval-complex-discard.cpp
diff --git a/clang/lib/AST/ByteCode/Compiler.cpp b/clang/lib/AST/ByteCode/Compiler.cpp
index 5db454c797565..2cbc97bd8a574 100644
--- a/clang/lib/AST/ByteCode/Compiler.cpp
+++ b/clang/lib/AST/ByteCode/Compiler.cpp
@@ -7406,13 +7406,8 @@ bool Compiler<Emitter>::emitComplexComparison(const Expr *LHS, const Expr *RHS,
const BinaryOperator *E) {
assert(E->isComparisonOp());
assert(!Initializing);
-if (DiscardResult) {
- if (!this->discard(LHS))
- return false;
- if (!this->discard(RHS))
- return false;
- return true;
-}
+if (DiscardResult)
+ return this->discard(LHS) && this->discard(RHS);
PrimType ElemT;
bool LHSIsComplex;
diff --git a/clang/test/AST/ByteCode/complex.cpp b/clang/test/AST/ByteCode/complex.cpp
index 41e5dc0605c23..9bf000d65466c 100644
--- a/clang/test/AST/ByteCode/complex.cpp
+++ b/clang/test/AST/ByteCode/complex.cpp
@@ -439,4 +439,17 @@ namespace Discard {
}
static_assert((V(), true));
+ void test_discard_complex_comparison() {
+ _Complex int x = 1i;
+ (void)(x == 1i);
+ x == 1i;
+ (void)(x != 1i);
+ }
+
+ constexpr int test_side_effect() {
+ int k = 0;
+ (void)(1i == (++k, 1i));
+ return k;
+ }
+ static_assert(test_side_effect() == 1);
}
diff --git a/clang/test/AST/ByteCode/constant-eval-complex-discard.cpp b/clang/test/AST/ByteCode/constant-eval-complex-discard.cpp
deleted file mode 100644
index 6c2cf5739ba13..0000000000000
--- a/clang/test/AST/ByteCode/constant-eval-complex-discard.cpp
+++ /dev/null
@@ -1,16 +0,0 @@
-// RUN: %clang_cc1 -std=c++20 -fsyntax-only -verify -fexperimental-new-constant-interpreter %s
-// RUN: %clang_cc1 -std=c++20 -fsyntax-only -verify %s
-
-// expected-no-diagnostics
-
-void test_no_crash() {
- _Complex int x = 1i;
- (void)(x == 1i);
-}
-
-constexpr int test_side_effect() {
- int k = 0;
- (void)(1i == (++k, 1i));
- return k;
-}
-static_assert(test_side_effect() == 1);
\ No newline at end of file
More information about the cfe-commits
mailing list