[clang] [clang][bytecode] Fix zero-init of first union member (PR #106962)

via cfe-commits cfe-commits at lists.llvm.org
Mon Sep 2 03:04:52 PDT 2024


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-clang

Author: Timm Baeder (tbaederr)

<details>
<summary>Changes</summary>

... if done via a ImplicitValueInitExpr.
We were already doing this later in visitZeroRecordInitializer().

---
Full diff: https://github.com/llvm/llvm-project/pull/106962.diff


2 Files Affected:

- (modified) clang/lib/AST/ByteCode/Compiler.cpp (+2-6) 
- (modified) clang/test/SemaObjC/non-trivial-c-union.m (+1) 


``````````diff
diff --git a/clang/lib/AST/ByteCode/Compiler.cpp b/clang/lib/AST/ByteCode/Compiler.cpp
index dced9ea3493732..d8b4a554574e0a 100644
--- a/clang/lib/AST/ByteCode/Compiler.cpp
+++ b/clang/lib/AST/ByteCode/Compiler.cpp
@@ -1235,12 +1235,6 @@ bool Compiler<Emitter>::VisitImplicitValueInitExpr(
     assert(RD);
     if (RD->isInvalidDecl())
       return false;
-    if (RD->isUnion()) {
-      // C++11 [dcl.init]p5: If T is a (possibly cv-qualified) union type, the
-      // object's first non-static named data member is zero-initialized
-      // FIXME
-      return false;
-    }
 
     if (const auto *CXXRD = dyn_cast<CXXRecordDecl>(RD);
         CXXRD && CXXRD->getNumVBases() > 0) {
@@ -3434,6 +3428,8 @@ bool Compiler<Emitter>::visitZeroRecordInitializer(const Record *R,
     if (!this->emitFinishInitPop(E))
       return false;
 
+    // C++11 [dcl.init]p5: If T is a (possibly cv-qualified) union type, the
+    // object's first non-static named data member is zero-initialized
     if (R->isUnion())
       break;
   }
diff --git a/clang/test/SemaObjC/non-trivial-c-union.m b/clang/test/SemaObjC/non-trivial-c-union.m
index 4c294764c430d8..34f1caad59df70 100644
--- a/clang/test/SemaObjC/non-trivial-c-union.m
+++ b/clang/test/SemaObjC/non-trivial-c-union.m
@@ -1,4 +1,5 @@
 // RUN: %clang_cc1 -fsyntax-only -fblocks -fobjc-arc -fobjc-runtime-has-weak -I %S/Inputs -verify %s
+// RUN: %clang_cc1 -fsyntax-only -fblocks -fobjc-arc -fobjc-runtime-has-weak -I %S/Inputs -verify -fexperimental-new-constant-interpreter %s
 
 #include "non-trivial-c-union.h"
 

``````````

</details>


https://github.com/llvm/llvm-project/pull/106962


More information about the cfe-commits mailing list