[PATCH] D135013: [clang][Interp] Array initialization via ImplicitValueInitExpr

Timm Bäder via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Sat Oct 1 00:34:37 PDT 2022


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

Unfortunately, I have //no idea// how to test this, the one and only reproducer I have so far is:

  #include <array>
  constexpr std::array<int, 10> IntArray = {};

which results in a `InitListExpr` containing a `ImplicitValueInitExpr` with an array type.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D135013

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


Index: clang/lib/AST/Interp/ByteCodeExprGen.cpp
===================================================================
--- clang/lib/AST/Interp/ByteCodeExprGen.cpp
+++ clang/lib/AST/Interp/ByteCodeExprGen.cpp
@@ -732,6 +732,23 @@
         return false;
     }
     return true;
+  } else if (const auto *IVIE = dyn_cast<ImplicitValueInitExpr>(Initializer)) {
+    auto ArrayType = IVIE->getType()->getAsArrayTypeUnsafe();
+    const auto *CAT = dyn_cast<ConstantArrayType>(ArrayType);
+    const size_t NumElems = CAT->getSize().getZExtValue();
+    Optional<PrimType> ElemT = classify(CAT->getElementType());
+
+    for (size_t I = 0; I != NumElems; ++I) {
+      if (ElemT) {
+        if (!this->emitZero(*ElemT, Initializer))
+          return false;
+        if (!this->emitInitElem(*ElemT, I, Initializer))
+          return false;
+      } else {
+        assert(false && "default initializer for non-primitive type");
+      }
+    }
+    return true;
   }
 
   assert(false && "Unknown expression for array initialization");


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D135013.464479.patch
Type: text/x-patch
Size: 1030 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20221001/18e147e0/attachment-0001.bin>


More information about the cfe-commits mailing list