[cfe-commits] r146009 - in /cfe/trunk: lib/CodeGen/CGExprConstant.cpp lib/CodeGen/CGRecordLayoutBuilder.cpp test/CodeGenCXX/pointers-to-data-members.cpp

Eli Friedman eli.friedman at gmail.com
Tue Dec 6 17:30:11 PST 2011


Author: efriedma
Date: Tue Dec  6 19:30:11 2011
New Revision: 146009

URL: http://llvm.org/viewvc/llvm-project?rev=146009&view=rev
Log:
Make sure we correctly zero-initialize unions containing a pointer to data member as the first field.  PR11487.


Modified:
    cfe/trunk/lib/CodeGen/CGExprConstant.cpp
    cfe/trunk/lib/CodeGen/CGRecordLayoutBuilder.cpp
    cfe/trunk/test/CodeGenCXX/pointers-to-data-members.cpp

Modified: cfe/trunk/lib/CodeGen/CGExprConstant.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/CodeGen/CGExprConstant.cpp?rev=146009&r1=146008&r2=146009&view=diff
==============================================================================
--- cfe/trunk/lib/CodeGen/CGExprConstant.cpp (original)
+++ cfe/trunk/lib/CodeGen/CGExprConstant.cpp Tue Dec  6 19:30:11 2011
@@ -1243,13 +1243,17 @@
   for (RecordDecl::field_iterator I = record->field_begin(),
          E = record->field_end(); I != E; ++I) {
     const FieldDecl *field = *I;
-    
-    // Ignore bit fields.
-    if (field->isBitField())
-      continue;
-    
-    unsigned fieldIndex = layout.getLLVMFieldNo(field);
-    elements[fieldIndex] = CGM.EmitNullConstant(field->getType());
+
+    // Fill in non-bitfields. (Bitfields always use a zero pattern, which we
+    // will fill in later.)
+    if (!field->isBitField()) {
+      unsigned fieldIndex = layout.getLLVMFieldNo(field);
+      elements[fieldIndex] = CGM.EmitNullConstant(field->getType());
+    }
+
+    // For unions, stop after the first named field.
+    if (record->isUnion() && field->getDeclName())
+      break;
   }
 
   // Fill in the virtual bases, if we're working with the complete object.

Modified: cfe/trunk/lib/CodeGen/CGRecordLayoutBuilder.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/CodeGen/CGRecordLayoutBuilder.cpp?rev=146009&r1=146008&r2=146009&view=diff
==============================================================================
--- cfe/trunk/lib/CodeGen/CGRecordLayoutBuilder.cpp (original)
+++ cfe/trunk/lib/CodeGen/CGRecordLayoutBuilder.cpp Tue Dec  6 19:30:11 2011
@@ -531,6 +531,7 @@
   CharUnits unionAlign = CharUnits::Zero();
 
   bool hasOnlyZeroSizedBitFields = true;
+  bool checkedFirstFieldZeroInit = false;
 
   unsigned fieldNo = 0;
   for (RecordDecl::field_iterator field = D->field_begin(),
@@ -542,6 +543,11 @@
     if (!fieldType)
       continue;
 
+    if (field->getDeclName() && !checkedFirstFieldZeroInit) {
+      CheckZeroInitializable(field->getType());
+      checkedFirstFieldZeroInit = true;
+    }
+
     hasOnlyZeroSizedBitFields = false;
 
     CharUnits fieldAlign = CharUnits::fromQuantity(

Modified: cfe/trunk/test/CodeGenCXX/pointers-to-data-members.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/CodeGenCXX/pointers-to-data-members.cpp?rev=146009&r1=146008&r2=146009&view=diff
==============================================================================
--- cfe/trunk/test/CodeGenCXX/pointers-to-data-members.cpp (original)
+++ cfe/trunk/test/CodeGenCXX/pointers-to-data-members.cpp Tue Dec  6 19:30:11 2011
@@ -230,3 +230,13 @@
   // CHECK-GLOBAL: @_ZN5test41dE = global %"struct.test4::D" { %"struct.test4::C.base" zeroinitializer, i32* null, %"struct.test4::B.base" { i32 (...)** null, i64 -1 }, %"struct.test4::A" zeroinitializer }, align 8
   D d;
 }
+
+namespace PR11487 {
+  union U
+  {
+    int U::* mptr;
+    char x[16];
+  } x;
+  // CHECK-GLOBAL: @_ZN7PR114871xE = global %"union.PR11487::U" { i64 -1, [8 x i8] zeroinitializer }, align 8
+  
+}





More information about the cfe-commits mailing list