[clang] c9e403d - [clang][Interp] Fix zero-init of float and pointer arrays

Timm Bäder via cfe-commits cfe-commits at lists.llvm.org
Tue Apr 25 00:01:06 PDT 2023


Author: Timm Bäder
Date: 2023-04-25T09:00:47+02:00
New Revision: c9e403d1992b064e9cd5b94749fb3f00fa0c0910

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

LOG: [clang][Interp] Fix zero-init of float and pointer arrays

Our Zero opcode only exists for integer types. Use
visitZeroInitializer() here as well so it works for floats and pointers.

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

Added: 
    

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

Removed: 
    


################################################################################
diff  --git a/clang/lib/AST/Interp/ByteCodeExprGen.cpp b/clang/lib/AST/Interp/ByteCodeExprGen.cpp
index 536438b347a20..9f79a7bcf9d34 100644
--- a/clang/lib/AST/Interp/ByteCodeExprGen.cpp
+++ b/clang/lib/AST/Interp/ByteCodeExprGen.cpp
@@ -1291,7 +1291,7 @@ bool ByteCodeExprGen<Emitter>::visitArrayInitializer(const Expr *Initializer) {
       //   since we memset our Block*s to 0 and so we have the desired value
       //   without this.
       for (size_t I = 0; I != NumElems; ++I) {
-        if (!this->emitZero(*ElemT, Initializer))
+        if (!this->visitZeroInitializer(CAT->getElementType(), Initializer))
           return false;
         if (!this->emitInitElem(*ElemT, I, Initializer))
           return false;

diff  --git a/clang/test/AST/Interp/arrays.cpp b/clang/test/AST/Interp/arrays.cpp
index 413ab2fa45d84..22ccafd579241 100644
--- a/clang/test/AST/Interp/arrays.cpp
+++ b/clang/test/AST/Interp/arrays.cpp
@@ -334,3 +334,19 @@ namespace IncDec {
                                    // ref-error {{not an integral constant expression}} \
                                   // ref-note {{in call to}}
 };
+
+namespace ZeroInit {
+  struct A {
+    int *p[2];
+  };
+  constexpr A a = {};
+  static_assert(a.p[0] == nullptr, "");
+  static_assert(a.p[1] == nullptr, "");
+
+  struct B {
+    double f[2];
+  };
+  constexpr B b = {};
+  static_assert(b.f[0] == 0.0, "");
+  static_assert(b.f[1] == 0.0, "");
+}


        


More information about the cfe-commits mailing list