[cfe-commits] r82736 - in /cfe/trunk: lib/AST/RecordLayoutBuilder.cpp lib/AST/RecordLayoutBuilder.h test/SemaCXX/empty-class-layout.cpp

Anders Carlsson andersca at mac.com
Thu Sep 24 17:02:51 PDT 2009


Author: andersca
Date: Thu Sep 24 19:02:51 2009
New Revision: 82736

URL: http://llvm.org/viewvc/llvm-project?rev=82736&view=rev
Log:
More work on empty classes.

Modified:
    cfe/trunk/lib/AST/RecordLayoutBuilder.cpp
    cfe/trunk/lib/AST/RecordLayoutBuilder.h
    cfe/trunk/test/SemaCXX/empty-class-layout.cpp

Modified: cfe/trunk/lib/AST/RecordLayoutBuilder.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/AST/RecordLayoutBuilder.cpp?rev=82736&r1=82735&r2=82736&view=diff

==============================================================================
--- cfe/trunk/lib/AST/RecordLayoutBuilder.cpp (original)
+++ cfe/trunk/lib/AST/RecordLayoutBuilder.cpp Thu Sep 24 19:02:51 2009
@@ -252,6 +252,18 @@
   return true;
 }
 
+bool ASTRecordLayoutBuilder::canPlaceFieldAtOffset(const FieldDecl *FD, 
+                                                   uint64_t Offset) const {
+  if (const RecordType *RT = dyn_cast<RecordType>(FD->getType())) {
+    if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(RT->getDecl()))
+      return canPlaceRecordAtOffset(RD, Offset);
+  }
+  
+  // FIXME: Arrays.
+  
+  return true;
+}
+
 void ASTRecordLayoutBuilder::UpdateEmptyClassOffsets(const CXXRecordDecl *RD,
                                                      uint64_t Offset) {
   if (RD->isEmpty())
@@ -485,6 +497,17 @@
 
     // Round up the current record size to the field's alignment boundary.
     FieldOffset = llvm::RoundUpToAlignment(FieldOffset, FieldAlign);
+    
+    if (!IsUnion) {
+      while (true) {
+        // Check if we can place the field at this offset.
+        if (canPlaceFieldAtOffset(D, FieldOffset))
+          break;
+        
+        // We can't try again.
+        FieldOffset += FieldAlign;
+      }
+    }
   }
 
   // Place this field at the current location.

Modified: cfe/trunk/lib/AST/RecordLayoutBuilder.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/AST/RecordLayoutBuilder.h?rev=82736&r1=82735&r2=82736&view=diff

==============================================================================
--- cfe/trunk/lib/AST/RecordLayoutBuilder.h (original)
+++ cfe/trunk/lib/AST/RecordLayoutBuilder.h Thu Sep 24 19:02:51 2009
@@ -103,6 +103,10 @@
   /// (direct or indirect) of the same type having the same offset.
   bool canPlaceRecordAtOffset(const CXXRecordDecl *RD, uint64_t Offset) const;
 
+  /// canPlaceFieldAtOffset - Return whether a field can be placed at the given
+  /// offset.
+  bool canPlaceFieldAtOffset(const FieldDecl *FD, uint64_t Offset) const;
+
   /// UpdateEmptyClassOffsets - Called after a record (either a base class
   /// or a field) has been placed at the given offset. Will update the
   /// EmptyClassOffsets map if the class is empty or has any empty bases or

Modified: cfe/trunk/test/SemaCXX/empty-class-layout.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/SemaCXX/empty-class-layout.cpp?rev=82736&r1=82735&r2=82736&view=diff

==============================================================================
--- cfe/trunk/test/SemaCXX/empty-class-layout.cpp (original)
+++ cfe/trunk/test/SemaCXX/empty-class-layout.cpp Thu Sep 24 19:02:51 2009
@@ -17,3 +17,10 @@
 
 struct G : E, F { };
 SA(3, sizeof(G) == 2);
+
+struct H { H(); };
+
+struct I : H { 
+  H h;
+};
+SA(4, sizeof(I) == 2);





More information about the cfe-commits mailing list