[cfe-commits] r112188 - in /cfe/trunk: lib/CodeGen/CGExprCXX.cpp test/CodeGenCXX/new.cpp

Argyrios Kyrtzidis akyrtzi at gmail.com
Thu Aug 26 08:23:38 PDT 2010


Author: akirtzidis
Date: Thu Aug 26 10:23:38 2010
New Revision: 112188

URL: http://llvm.org/viewvc/llvm-project?rev=112188&view=rev
Log:
Fix miscompilation. The cookie was not used when new'ing arrays with multiple dimensions.

Modified:
    cfe/trunk/lib/CodeGen/CGExprCXX.cpp
    cfe/trunk/test/CodeGenCXX/new.cpp

Modified: cfe/trunk/lib/CodeGen/CGExprCXX.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/CodeGen/CGExprCXX.cpp?rev=112188&r1=112187&r2=112188&view=diff
==============================================================================
--- cfe/trunk/lib/CodeGen/CGExprCXX.cpp (original)
+++ cfe/trunk/lib/CodeGen/CGExprCXX.cpp Thu Aug 26 10:23:38 2010
@@ -296,6 +296,7 @@
 }
 
 static CharUnits CalculateCookiePadding(ASTContext &Ctx, QualType ElementType) {
+  ElementType = Ctx.getBaseElementType(ElementType);
   const RecordType *RT = ElementType->getAs<RecordType>();
   if (!RT)
     return CharUnits::Zero();
@@ -376,18 +377,29 @@
                                         const CXXNewExpr *E,
                                         llvm::Value *&NumElements,
                                         llvm::Value *&SizeWithoutCookie) {
-  QualType Type = E->getAllocatedType();
-  CharUnits TypeSize = CGF.getContext().getTypeSizeInChars(Type);
-  const llvm::Type *SizeTy = CGF.ConvertType(CGF.getContext().getSizeType());
+  QualType ElemType = E->getAllocatedType();
   
   if (!E->isArray()) {
+    CharUnits TypeSize = CGF.getContext().getTypeSizeInChars(ElemType);
+    const llvm::Type *SizeTy = CGF.ConvertType(CGF.getContext().getSizeType());
     SizeWithoutCookie = llvm::ConstantInt::get(SizeTy, TypeSize.getQuantity());
     return SizeWithoutCookie;
   }
 
   // Emit the array size expression.
+  // We multiply the size of all dimensions for NumElements.
+  // e.g for 'int[2][3]', ElemType is 'int' and NumElements is 6.
   NumElements = CGF.EmitScalarExpr(E->getArraySize());
-  
+  while (const ConstantArrayType *CAT
+             = CGF.getContext().getAsConstantArrayType(ElemType)) {
+    ElemType = CAT->getElementType();
+    llvm::Value *ArraySize
+        = llvm::ConstantInt::get(CGF.CGM.getLLVMContext(), CAT->getSize());
+    NumElements = CGF.Builder.CreateMul(NumElements, ArraySize);
+  }
+
+  CharUnits TypeSize = CGF.getContext().getTypeSizeInChars(ElemType);
+  const llvm::Type *SizeTy = CGF.ConvertType(CGF.getContext().getSizeType());
   llvm::Value *Size = llvm::ConstantInt::get(SizeTy, TypeSize.getQuantity());
   
   // If someone is doing 'new int[42]' there is no need to do a dynamic check.

Modified: cfe/trunk/test/CodeGenCXX/new.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/CodeGenCXX/new.cpp?rev=112188&r1=112187&r2=112188&view=diff
==============================================================================
--- cfe/trunk/test/CodeGenCXX/new.cpp (original)
+++ cfe/trunk/test/CodeGenCXX/new.cpp Thu Aug 26 10:23:38 2010
@@ -146,12 +146,15 @@
 }
 
 struct Alloc{
+  int x;
   void* operator new[](size_t size);
   void operator delete[](void* p);
+  ~Alloc();
 };
 
 void f() {
-  // CHECK: call i8* @_ZN5AllocnaEm(i64 200)
+  // CHECK: call i8* @_ZN5AllocnaEm(i64 808)
+  // CHECK: store i64 200
   // CHECK: call void @_ZN5AllocdaEPv(i8*
   delete[] new Alloc[10][20];
 }





More information about the cfe-commits mailing list