Make sure adding a field to a struct never reduces its size.  PR11745.



git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@148056 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/include/clang/Sema/Sema.h b/include/clang/Sema/Sema.h
index aa0a7ef..8ee3c68 100644
--- a/include/clang/Sema/Sema.h
+++ b/include/clang/Sema/Sema.h
@@ -5684,6 +5684,8 @@
   // type.
   ExprResult DefaultLvalueConversion(Expr *E);
 
+  void MarkLValueReferenced(Expr *E);
+
   // DefaultArgumentPromotion (C99 6.5.2.2p6). Used for function calls that
   // do not have a prototype. Integer promotions are performed on each
   // argument, and arguments that have type float are promoted to double.
diff --git a/lib/AST/RecordLayoutBuilder.cpp b/lib/AST/RecordLayoutBuilder.cpp
index 43dee10..69e31a3 100644
--- a/lib/AST/RecordLayoutBuilder.cpp
+++ b/lib/AST/RecordLayoutBuilder.cpp
@@ -1867,14 +1867,13 @@
                     Context.toBits(UnpackedFieldAlign), FieldPacked, D);
 
   // Reserve space for this field.
-  uint64_t FieldSizeInBits = Context.toBits(FieldSize);
   if (IsUnion)
-    setSize(std::max(getSizeInBits(), FieldSizeInBits));
+    setDataSize(std::max(getDataSize(), FieldSize));
   else
-    setSize(FieldOffset + FieldSize);
+    setDataSize(FieldOffset + FieldSize);
 
-  // Update the data size.
-  setDataSize(getSizeInBits());
+  // Update the size.
+  setSize(std::max(getSize(), getDataSize()));
 
   // Remember max struct/class alignment.
   UpdateAlignment(FieldAlign, UnpackedFieldAlign);
diff --git a/test/SemaCXX/empty-class-layout.cpp b/test/SemaCXX/empty-class-layout.cpp
index 0b46bf0..c68f2bb 100644
--- a/test/SemaCXX/empty-class-layout.cpp
+++ b/test/SemaCXX/empty-class-layout.cpp
@@ -144,3 +144,14 @@
 SA(0, sizeof(B) == 16);
 
 }
+
+namespace Test7 {
+  // Make sure we reserve enough space for both bases; PR11745.
+  struct Empty { };
+  struct Base1 : Empty { };
+  struct Base2 : Empty { };
+  struct Test : Base1, Base2 {
+    char c;
+  };
+  SA(0, sizeof(Test) == 2);
+}