<html>
    <head>
      <base href="https://llvm.org/bugs/" />
    </head>
    <body><table border="1" cellspacing="0" cellpadding="8">
        <tr>
          <th>Bug ID</th>
          <td><a class="bz_bug_link 
          bz_status_NEW "
   title="NEW --- - Unnamed struct inside union doesn't initialize entire aggregate"
   href="https://llvm.org/bugs/show_bug.cgi?id=24698">24698</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>Unnamed struct inside union doesn't initialize entire aggregate
          </td>
        </tr>

        <tr>
          <th>Product</th>
          <td>clang
          </td>
        </tr>

        <tr>
          <th>Version</th>
          <td>trunk
          </td>
        </tr>

        <tr>
          <th>Hardware</th>
          <td>PC
          </td>
        </tr>

        <tr>
          <th>OS</th>
          <td>Linux
          </td>
        </tr>

        <tr>
          <th>Status</th>
          <td>NEW
          </td>
        </tr>

        <tr>
          <th>Severity</th>
          <td>normal
          </td>
        </tr>

        <tr>
          <th>Priority</th>
          <td>P
          </td>
        </tr>

        <tr>
          <th>Component</th>
          <td>-New Bugs
          </td>
        </tr>

        <tr>
          <th>Assignee</th>
          <td>unassignedclangbugs@nondot.org
          </td>
        </tr>

        <tr>
          <th>Reporter</th>
          <td>bblanco@gmail.com
          </td>
        </tr>

        <tr>
          <th>CC</th>
          <td>llvm-bugs@lists.llvm.org
          </td>
        </tr>

        <tr>
          <th>Classification</th>
          <td>Unclassified
          </td>
        </tr></table>
      <p>
        <div>
        <pre>I have a piece of code which initializes a struct, nested in a union. However,
the remainder part of the union is left uninitialized. My understanding of the
spec may be wrong, but I would have expected the entire outer union to be
initialized as well.

$ cat init.c
union U {
       struct {
               int a;
               int b;
               int c;
       };
       struct {
               int s1;
               int s2;
               int s3;
               int s4;
       };
};

void foo(union U *);
void bar(int a, int b)
{
       union U u = {
               .a = a,
               .b = b
       };
       foo(&u);
}

$ clang -O2 -S init.c -emit-llvm
$ cat init.ll 
; ModuleID = 'init.c'
target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"
target triple = "x86_64-unknown-linux-gnu"

%union.U = type { %struct.anon.0 }
%struct.anon.0 = type { i32, i32, i32, i32 }

; Function Attrs: nounwind uwtable
define void @bar(i32 %a, i32 %b) #0 {
  %u = alloca %union.U, align 4
  %1 = bitcast %union.U* %u to i8*
  call void @llvm.lifetime.start(i64 16, i8* %1) #1
  %2 = getelementptr inbounds %union.U, %union.U* %u, i64 0, i32 0, i32 0
  store i32 %a, i32* %2, align 4, !tbaa !1
  %3 = getelementptr inbounds %union.U, %union.U* %u, i64 0, i32 0, i32 1
  store i32 %b, i32* %3, align 4, !tbaa !6
  %4 = getelementptr inbounds %union.U, %union.U* %u, i64 0, i32 0, i32 2
  store i32 0, i32* %4, align 4, !tbaa !7
  call void @foo(%union.U* nonnull %u) #1
  call void @llvm.lifetime.end(i64 16, i8* %1) #1
  ret void
}

; Function Attrs: nounwind
declare void @llvm.lifetime.start(i64, i8* nocapture) #1

declare void @foo(%union.U*) #2
...

As the IR shows, only the 3rd struct field is added to the initialized list,
rather than all 4.

Anecdotally, this was tested on clang 3.3 and also trunk. The behavior is the
same in both places. In the case of gcc, the full size of the union is
initialized.</pre>
        </div>
      </p>
      <hr>
      <span>You are receiving this mail because:</span>
      
      <ul>
          <li>You are on the CC list for the bug.</li>
      </ul>
    </body>
</html>