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

Anders Carlsson andersca at mac.com
Thu Sep 24 18:23:32 PDT 2009


Author: andersca
Date: Thu Sep 24 20:23:32 2009
New Revision: 82744

URL: http://llvm.org/viewvc/llvm-project?rev=82744&view=rev
Log:
Handle array fields that contain empty structs.

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

Modified: cfe/trunk/include/clang/AST/ASTContext.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/AST/ASTContext.h?rev=82744&r1=82743&r2=82744&view=diff

==============================================================================
--- cfe/trunk/include/clang/AST/ASTContext.h (original)
+++ cfe/trunk/include/clang/AST/ASTContext.h Thu Sep 24 20:23:32 2009
@@ -889,9 +889,9 @@
     return dyn_cast_or_null<IncompleteArrayType>(getAsArrayType(T));
   }
 
-  /// getBaseElementType - Returns the innermost element type of a variable
-  /// length array type. For example, will return "int" for int[m][n]
-  QualType getBaseElementType(const VariableArrayType *VAT);
+  /// getBaseElementType - Returns the innermost element type of an array type.
+  /// For example, will return "int" for int[m][n]
+  QualType getBaseElementType(const ArrayType *VAT);
 
   /// getBaseElementType - Returns the innermost element type of a type
   /// (which needn't actually be an array type).

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

==============================================================================
--- cfe/trunk/lib/AST/ASTContext.cpp (original)
+++ cfe/trunk/lib/AST/ASTContext.cpp Thu Sep 24 20:23:32 2009
@@ -2407,11 +2407,11 @@
   }
 }
 
-QualType ASTContext::getBaseElementType(const VariableArrayType *VAT) {
-  QualType ElemTy = VAT->getElementType();
+QualType ASTContext::getBaseElementType(const ArrayType *AT) {
+  QualType ElemTy = AT->getElementType();
 
-  if (const VariableArrayType *VAT = getAsVariableArrayType(ElemTy))
-    return getBaseElementType(VAT);
+  if (const ArrayType *AT = getAsArrayType(ElemTy))
+    return getBaseElementType(AT);
 
   return ElemTy;
 }

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

==============================================================================
--- cfe/trunk/lib/AST/RecordLayoutBuilder.cpp (original)
+++ cfe/trunk/lib/AST/RecordLayoutBuilder.cpp Thu Sep 24 20:23:32 2009
@@ -254,12 +254,32 @@
 
 bool ASTRecordLayoutBuilder::canPlaceFieldAtOffset(const FieldDecl *FD, 
                                                    uint64_t Offset) const {
-  if (const RecordType *RT = dyn_cast<RecordType>(FD->getType())) {
+  QualType T = FD->getType();
+  if (const RecordType *RT = T->getAs<RecordType>()) {
     if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(RT->getDecl()))
       return canPlaceRecordAtOffset(RD, Offset);
   }
   
-  // FIXME: Arrays.
+  if (const ConstantArrayType *AT = Ctx.getAsConstantArrayType(T)) {
+    QualType ElemTy = Ctx.getBaseElementType(AT);
+    const RecordType *RT = ElemTy->getAs<RecordType>();
+    if (!RT)
+      return true;
+    const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(RT->getDecl());
+    if (!RD)
+      return true;
+    
+    const ASTRecordLayout &Info = Ctx.getASTRecordLayout(RD);
+
+    uint64_t NumElements = Ctx.getConstantArrayElementCount(AT);
+    unsigned ElementOffset = Offset;
+    for (uint64_t I = 0; I != NumElements; ++I) {
+      if (!canPlaceRecordAtOffset(RD, ElementOffset))
+        return false;
+      
+      ElementOffset += Info.getSize();
+    }
+  }
   
   return true;
 }

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=82744&r1=82743&r2=82744&view=diff

==============================================================================
--- cfe/trunk/test/SemaCXX/empty-class-layout.cpp (original)
+++ cfe/trunk/test/SemaCXX/empty-class-layout.cpp Thu Sep 24 20:23:32 2009
@@ -18,9 +18,14 @@
 struct G : E, F { };
 SA(3, sizeof(G) == 2);
 
-struct H { H(); };
+struct Empty { Empty(); };
 
-struct I : H { 
-  H h;
+struct I : Empty { 
+  Empty e;
 };
 SA(4, sizeof(I) == 2);
+
+struct J : Empty { 
+  Empty e[2];
+};
+SA(5, sizeof(J) == 3);
\ No newline at end of file





More information about the cfe-commits mailing list