[clang] [clang][Interp] Support zero init for complex types (PR #79728)
Timm Baeder via cfe-commits
cfe-commits at lists.llvm.org
Sat Jan 27 22:25:29 PST 2024
https://github.com/tbaederr created https://github.com/llvm/llvm-project/pull/79728
Initialize both elements to 0.
>From 0b6925d01645fbe3af9a1ac51ee6e396cbe67a03 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Timm=20B=C3=A4der?= <tbaeder at redhat.com>
Date: Sun, 28 Jan 2024 07:21:06 +0100
Subject: [PATCH] [clang][Interp] Support zero init for complex types
Initialize both elements to 0.
---
clang/lib/AST/Interp/ByteCodeExprGen.cpp | 24 +++++++++++++++++++++++-
clang/test/AST/Interp/complex.cpp | 13 +++++++++++++
2 files changed, 36 insertions(+), 1 deletion(-)
diff --git a/clang/lib/AST/Interp/ByteCodeExprGen.cpp b/clang/lib/AST/Interp/ByteCodeExprGen.cpp
index d4501cefb2131da..b549e35f208bdfd 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 836ea552adac86d..4e730a117de4bd8 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, "");
+}
More information about the cfe-commits
mailing list