[clang] 70e5a2a - [clang][Interp] Fix zero-initializing of floating types
Timm Bäder via cfe-commits
cfe-commits at lists.llvm.org
Thu Apr 6 01:52:14 PDT 2023
Author: Timm Bäder
Date: 2023-04-06T10:52:01+02:00
New Revision: 70e5a2a94354fd7cec577d3e0c3892cc5e7ef07a
URL: https://github.com/llvm/llvm-project/commit/70e5a2a94354fd7cec577d3e0c3892cc5e7ef07a
DIFF: https://github.com/llvm/llvm-project/commit/70e5a2a94354fd7cec577d3e0c3892cc5e7ef07a.diff
LOG: [clang][Interp] Fix zero-initializing of floating types
Differential Revision: https://reviews.llvm.org/D146788
Added:
Modified:
clang/lib/AST/Interp/ByteCodeExprGen.cpp
clang/test/AST/Interp/floats.cpp
Removed:
################################################################################
diff --git a/clang/lib/AST/Interp/ByteCodeExprGen.cpp b/clang/lib/AST/Interp/ByteCodeExprGen.cpp
index 3c0992e4fee0..6ced8ca4d07f 100644
--- a/clang/lib/AST/Interp/ByteCodeExprGen.cpp
+++ b/clang/lib/AST/Interp/ByteCodeExprGen.cpp
@@ -953,8 +953,10 @@ bool ByteCodeExprGen<Emitter>::visitZeroInitializer(PrimType T, const Expr *E) {
return this->emitNullPtr(E);
case PT_FnPtr:
return this->emitNullFnPtr(E);
- case PT_Float:
- assert(false);
+ case PT_Float: {
+ return this->emitConstFloat(
+ APFloat::getZero(Ctx.getFloatSemantics(E->getType())), E);
+ }
}
llvm_unreachable("unknown primitive type");
}
diff --git a/clang/test/AST/Interp/floats.cpp b/clang/test/AST/Interp/floats.cpp
index 7b9328c4d118..3b392d3b6727 100644
--- a/clang/test/AST/Interp/floats.cpp
+++ b/clang/test/AST/Interp/floats.cpp
@@ -78,3 +78,17 @@ namespace compound {
}
static_assert(f2() == __FLT_MAX__, "");
}
+
+namespace ZeroInit {
+ template<typename FloatT>
+ struct A {
+ int a;
+ FloatT f;
+ };
+
+ constexpr A<float> a{12};
+ static_assert(a.f == 0.0f);
+
+ constexpr A<double> b{12};
+ static_assert(a.f == 0.0);
+};
More information about the cfe-commits
mailing list