[clang] [clang][bytecode] Fix #55390 here as well (PR #106395)

Timm Baeder via cfe-commits cfe-commits at lists.llvm.org
Wed Aug 28 06:55:07 PDT 2024


https://github.com/tbaederr created https://github.com/llvm/llvm-project/pull/106395

Ignore the multiplication overflow but report the 0 denominator.

>From 7b1cfea20ae76a7b04ae63bf6d6082488c17b2e3 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Timm=20B=C3=A4der?= <tbaeder at redhat.com>
Date: Wed, 28 Aug 2024 15:50:10 +0200
Subject: [PATCH] [clang][bytecode] Fix #55390 here as well

Ignore the multiplication overflow but report the 0 denominator.
---
 clang/lib/AST/ByteCode/Interp.h     | 11 +++++++++--
 clang/test/AST/ByteCode/complex.cpp |  3 +++
 2 files changed, 12 insertions(+), 2 deletions(-)

diff --git a/clang/lib/AST/ByteCode/Interp.h b/clang/lib/AST/ByteCode/Interp.h
index 242532a3f0544e..ad6e585daab9fc 100644
--- a/clang/lib/AST/ByteCode/Interp.h
+++ b/clang/lib/AST/ByteCode/Interp.h
@@ -517,12 +517,19 @@ inline bool Divc(InterpState &S, CodePtr OpPC) {
 
     // Den = real(RHS)² + imag(RHS)²
     T A, B;
-    if (T::mul(RHSR, RHSR, Bits, &A) || T::mul(RHSI, RHSI, Bits, &B))
-      return false;
+    if (T::mul(RHSR, RHSR, Bits, &A) || T::mul(RHSI, RHSI, Bits, &B)) {
+      // Ignore overflow here, because that's what the current interpeter does.
+    }
     T Den;
     if (T::add(A, B, Bits, &Den))
       return false;
 
+    if (Compare(Den, Zero) == ComparisonCategoryResult::Equal) {
+      const SourceInfo &E = S.Current->getSource(OpPC);
+      S.FFDiag(E, diag::note_expr_divide_by_zero);
+      return false;
+    }
+
     // real(Result) = ((real(LHS) * real(RHS)) + (imag(LHS) * imag(RHS))) / Den
     T &ResultR = Result.atIndex(0).deref<T>();
     T &ResultI = Result.atIndex(1).deref<T>();
diff --git a/clang/test/AST/ByteCode/complex.cpp b/clang/test/AST/ByteCode/complex.cpp
index a969aadfdcd088..dc93c786dac7ae 100644
--- a/clang/test/AST/ByteCode/complex.cpp
+++ b/clang/test/AST/ByteCode/complex.cpp
@@ -181,6 +181,9 @@ constexpr _Complex float getComplexFloat() {
 static_assert(__real(getComplexFloat()) == 1, "");
 static_assert(__imag(getComplexFloat()) == 2, "");
 
+constexpr auto GH55390 = 1 / 65536j; // both-note {{division by zero}} \
+                                     // both-error {{constexpr variable 'GH55390' must be initialized by a constant expression}}
+
 namespace CastToBool {
   constexpr _Complex int F = {0, 1};
   static_assert(F, "");



More information about the cfe-commits mailing list