From: Argyrios Kyrtzidis Date: Thu, 28 Apr 2011 18:53:55 +0000 (+0000) Subject: Don't waste memory if the initializer expression is empty. X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=f8b1771677743d24c7391305cad8853c513ec0f8;p=clang Don't waste memory if the initializer expression is empty. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@130420 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/lib/Sema/SemaInit.cpp b/lib/Sema/SemaInit.cpp index 0cf7546466..938ace851e 100644 --- a/lib/Sema/SemaInit.cpp +++ b/lib/Sema/SemaInit.cpp @@ -1795,11 +1795,15 @@ InitListChecker::getStructuredSubobjectInit(InitListExpr *IList, unsigned Index, // Pre-allocate storage for the structured initializer list. unsigned NumElements = 0; unsigned NumInits = 0; - if (!StructuredList) + bool GotNumInits = false; + if (!StructuredList) { NumInits = IList->getNumInits(); - else if (Index < IList->getNumInits()) { - if (InitListExpr *SubList = dyn_cast(IList->getInit(Index))) + GotNumInits = true; + } else if (Index < IList->getNumInits()) { + if (InitListExpr *SubList = dyn_cast(IList->getInit(Index))) { NumInits = SubList->getNumInits(); + GotNumInits = true; + } } if (const ArrayType *AType @@ -1808,7 +1812,7 @@ InitListChecker::getStructuredSubobjectInit(InitListExpr *IList, unsigned Index, NumElements = CAType->getSize().getZExtValue(); // Simple heuristic so that we don't allocate a very large // initializer with many empty entries at the end. - if (NumInits && NumElements > NumInits) + if (GotNumInits && NumElements > NumInits) NumElements = 0; } } else if (const VectorType *VType = CurrentObjectType->getAs()) diff --git a/test/Index/initializer-memory.cpp b/test/Index/initializer-memory.cpp index d0f531fdd2..f085c35624 100644 --- a/test/Index/initializer-memory.cpp +++ b/test/Index/initializer-memory.cpp @@ -3,6 +3,7 @@ // rdar://9275920 - We would create millions of Exprs to fill out the initializer. double data[1000000] = {0}; +double data_empty_init[1000000] = {}; struct S { S(int); @@ -10,5 +11,6 @@ struct S { }; S data2[1000000] = {0}; +S data_empty_init2[1000000] = {}; // CHECK: TOTAL = {{.*}} (0.{{.*}} MBytes)