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

via cfe-commits cfe-commits at lists.llvm.org
Sat Jan 27 22:25:59 PST 2024


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-clang

Author: Timm Baeder (tbaederr)

<details>
<summary>Changes</summary>

Initialize both elements to 0.

---
Full diff: https://github.com/llvm/llvm-project/pull/79728.diff


2 Files Affected:

- (modified) clang/lib/AST/Interp/ByteCodeExprGen.cpp (+23-1) 
- (modified) clang/test/AST/Interp/complex.cpp (+13) 


``````````diff
diff --git a/clang/lib/AST/Interp/ByteCodeExprGen.cpp b/clang/lib/AST/Interp/ByteCodeExprGen.cpp
index d4501cefb2131d..b549e35f208bdf 100644
--- a/clang/lib/AST/Interp/ByteCodeExprGen.cpp
+++ b/clang/lib/AST/Interp/ByteCodeExprGen.cpp
@@ -1830,7 +1830,29 @@ bool ByteCodeExprGen<Emitter>::VisitCXXScalarValueInitExpr(
   if (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 836ea552adac86..4e730a117de4bd 100644
--- a/clang/test/AST/Interp/complex.cpp
+++ b/clang/test/AST/Interp/complex.cpp
@@ -146,3 +146,16 @@ 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, "");
+}

``````````

</details>


https://github.com/llvm/llvm-project/pull/79728


More information about the cfe-commits mailing list