Author: Timm Baeder
Date: 2024-08-28T19:09:12+02:00
New Revision: 40db261551e38c9f9f6b6988a8b756504d16dd58

URL: 
https://github.com/llvm/llvm-project/commit/40db261551e38c9f9f6b6988a8b756504d16dd58
DIFF: 
https://github.com/llvm/llvm-project/commit/40db261551e38c9f9f6b6988a8b756504d16dd58.diff

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

Ignore the multiplication overflow but report the 0 denominator.

Added: 
    

Modified: 
    clang/lib/AST/ByteCode/Interp.h
    clang/test/AST/ByteCode/complex.cpp

Removed: 
    


################################################################################
diff  --git a/clang/lib/AST/ByteCode/Interp.h b/clang/lib/AST/ByteCode/Interp.h
index b98be010b40d4c..cfd9b1843d0407 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, "");


        
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to