[clang] e524ada - [clang][Interp] Support zero init for complex types (#79728)

via cfe-commits cfe-commits at lists.llvm.org
Mon Feb 5 06:56:10 PST 2024


Author: Timm Baeder
Date: 2024-02-05T15:56:06+01:00
New Revision: e524ada6cbc6912156a713ffa179cb92e5362ebb

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

LOG: [clang][Interp] Support zero init for complex types (#79728)

Initialize both elements to 0.

Added: 
    

Modified: 
    clang/lib/AST/Interp/ByteCodeExprGen.cpp
    clang/test/AST/Interp/complex.cpp

Removed: 
    


################################################################################
diff  --git a/clang/lib/AST/Interp/ByteCodeExprGen.cpp b/clang/lib/AST/Interp/ByteCodeExprGen.cpp
index 74a5284ff812c..cefcebe18c6b6 100644
--- a/clang/lib/AST/Interp/ByteCodeExprGen.cpp
+++ b/clang/lib/AST/Interp/ByteCodeExprGen.cpp
@@ -1939,10 +1939,32 @@ bool ByteCodeExprGen<Emitter>::VisitCXXScalarValueInitExpr(
     const CXXScalarValueInitExpr *E) {
   QualType Ty = E->getType();
 
-  if (Ty->isVoidType())
+  if (DiscardResult || Ty->isVoidType())
     return true;
 
-  return this->visitZeroInitializer(classifyPrim(Ty), Ty, E);
+  if (std::optional<PrimType> T = classify(Ty))
+    return this->visitZeroInitializer(*T, Ty, E);
+
+  assert(Ty->isAnyComplexType());
+  if (!Initializing) {
+    std::optional<unsigned> LocalIndex = allocateLocal(E, /*IsExtended=*/false);
+    if (!LocalIndex)
+      return false;
+    if (!this->emitGetPtrLocal(*LocalIndex, E))
+      return false;
+  }
+
+  // Initialize both fields to 0.
+  QualType ElemQT = Ty->getAs<ComplexType>()->getElementType();
+  PrimType ElemT = classifyPrim(ElemQT);
+
+  for (unsigned I = 0; I != 2; ++I) {
+    if (!this->visitZeroInitializer(ElemT, ElemQT, E))
+      return false;
+    if (!this->emitInitElem(ElemT, I, E))
+      return false;
+  }
+  return true;
 }
 
 template <class Emitter>

diff  --git a/clang/test/AST/Interp/complex.cpp b/clang/test/AST/Interp/complex.cpp
index fdd1d738de828..7f02bfa18bbdb 100644
--- a/clang/test/AST/Interp/complex.cpp
+++ b/clang/test/AST/Interp/complex.cpp
@@ -179,3 +179,18 @@ namespace Sub {
 }
 
 }
+
+namespace ZeroInit {
+  typedef _Complex float fcomplex;
+  typedef _Complex unsigned icomplex;
+
+  constexpr fcomplex test7 = fcomplex();
+  static_assert(__real(test7) == 0.0f, "");
+  static_assert(__imag(test7) == 0.0f, "");
+
+  constexpr icomplex test8 = icomplex();
+  static_assert(__real(test8) == 0, "");
+  static_assert(__imag(test8) == 0, "");
+
+  constexpr int ignored = (fcomplex(), 0);
+}


        


More information about the cfe-commits mailing list