[clang] 52a55a7 - [clang][Interp] Allow zero-init of primitives with an empty init list

Timm Bäder via cfe-commits cfe-commits at lists.llvm.org
Mon Sep 18 02:09:40 PDT 2023


Author: Timm Bäder
Date: 2023-09-18T11:09:30+02:00
New Revision: 52a55a7178381c7ac2b46155f2cc000dac48f2d4

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

LOG: [clang][Interp] Allow zero-init of primitives with an empty init list

Differential Revision: https://reviews.llvm.org/D158595

Added: 
    

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

Removed: 
    


################################################################################
diff  --git a/clang/lib/AST/Interp/ByteCodeExprGen.cpp b/clang/lib/AST/Interp/ByteCodeExprGen.cpp
index 823bef7a8c19e11..c813f9b6d9991c5 100644
--- a/clang/lib/AST/Interp/ByteCodeExprGen.cpp
+++ b/clang/lib/AST/Interp/ByteCodeExprGen.cpp
@@ -571,8 +571,10 @@ bool ByteCodeExprGen<Emitter>::VisitInitListExpr(const InitListExpr *E) {
 
   // Primitive values.
   if (std::optional<PrimType> T = classify(E->getType())) {
-    assert(E->getNumInits() == 1);
     assert(!DiscardResult);
+    if (E->getNumInits() == 0)
+      return this->visitZeroInitializer(E->getType(), E);
+    assert(E->getNumInits() == 1);
     return this->delegate(E->inits()[0]);
   }
 

diff  --git a/clang/test/AST/Interp/literals.cpp b/clang/test/AST/Interp/literals.cpp
index 562090d46b094d9..ceda59405ea9105 100644
--- a/clang/test/AST/Interp/literals.cpp
+++ b/clang/test/AST/Interp/literals.cpp
@@ -91,6 +91,15 @@ static_assert(-(1 << 31), ""); // expected-error {{not an integral constant expr
                                // ref-error {{not an integral constant expression}} \
                                // ref-note {{outside the range of representable values}} \
 
+namespace PrimitiveEmptyInitList {
+  constexpr int a = {};
+  static_assert(a == 0, "");
+  constexpr bool b = {};
+  static_assert(!b, "");
+  constexpr double d = {};
+  static_assert(d == 0.0, "");
+}
+
 
 enum E {};
 constexpr E e = static_cast<E>(0);


        


More information about the cfe-commits mailing list