[PATCH] D158595: [clang][Interp] Allow zero-ini of primitives with an empty init list

Timm Bäder via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Wed Aug 23 04:51:07 PDT 2023


tbaeder created this revision.
tbaeder added reviewers: aaron.ballman, erichkeane, shafik, cor3ntin.
Herald added a project: All.
tbaeder requested review of this revision.
Herald added a project: clang.
Herald added a subscriber: cfe-commits.

Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D158595

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


Index: clang/test/AST/Interp/literals.cpp
===================================================================
--- clang/test/AST/Interp/literals.cpp
+++ clang/test/AST/Interp/literals.cpp
@@ -91,6 +91,15 @@
                                // 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);
Index: clang/lib/AST/Interp/ByteCodeExprGen.cpp
===================================================================
--- clang/lib/AST/Interp/ByteCodeExprGen.cpp
+++ clang/lib/AST/Interp/ByteCodeExprGen.cpp
@@ -675,8 +675,10 @@
 
   // 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]);
   }
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D158595.552659.patch
Type: text/x-patch
Size: 1202 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20230823/9cc85e5c/attachment-0001.bin>


More information about the cfe-commits mailing list