From: Ivan A. Kosarev Date: Wed, 14 Feb 2018 13:10:35 +0000 (+0000) Subject: [AST] Refine the condition for element-dependent array fillers X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=a8fcfae39a99200f210ae98893421e241dc6a781;p=clang [AST] Refine the condition for element-dependent array fillers This patch fixes clang to not consider braced initializers for aggregate elements of arrays to be potentially dependent on the indices of the initialized elements. Resolves bug 18978: initialize a large static array = clang oom? https://bugs.llvm.org/show_bug.cgi?id=18978 Differential Revision: https://reviews.llvm.org/D43187 git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@325120 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/lib/AST/ExprConstant.cpp b/lib/AST/ExprConstant.cpp index 61bd1e5f12..e56569dc6c 100644 --- a/lib/AST/ExprConstant.cpp +++ b/lib/AST/ExprConstant.cpp @@ -48,6 +48,8 @@ #include #include +#define DEBUG_TYPE "exprconstant" + using namespace clang; using llvm::APSInt; using llvm::APFloat; @@ -6780,6 +6782,22 @@ static bool EvaluateArray(const Expr *E, const LValue &This, return ArrayExprEvaluator(Info, This, Result).Visit(E); } +// Return true iff the given array filler may depend on the element index. +static bool MaybeElementDependentArrayFiller(const Expr *FillerExpr) { + // For now, just whitelist non-class value-initialization and initialization + // lists comprised of them. + if (isa(FillerExpr)) + return false; + if (const InitListExpr *ILE = dyn_cast(FillerExpr)) { + for (unsigned I = 0, E = ILE->getNumInits(); I != E; ++I) { + if (MaybeElementDependentArrayFiller(ILE->getInit(I))) + return true; + } + return false; + } + return true; +} + bool ArrayExprEvaluator::VisitInitListExpr(const InitListExpr *E) { const ConstantArrayType *CAT = Info.Ctx.getAsConstantArrayType(E->getType()); if (!CAT) @@ -6809,10 +6827,13 @@ bool ArrayExprEvaluator::VisitInitListExpr(const InitListExpr *E) { const Expr *FillerExpr = E->hasArrayFiller() ? E->getArrayFiller() : nullptr; // If the initializer might depend on the array index, run it for each - // array element. For now, just whitelist non-class value-initialization. - if (NumEltsToInit != NumElts && !isa(FillerExpr)) + // array element. + if (NumEltsToInit != NumElts && MaybeElementDependentArrayFiller(FillerExpr)) NumEltsToInit = NumElts; + DEBUG(llvm::dbgs() << "The number of elements to initialize: " << + NumEltsToInit << ".\n"); + Result = APValue(APValue::UninitArray(), NumEltsToInit, NumElts); // If the array was previously zero-initialized, preserve the diff --git a/test/SemaCXX/large-array-init.cpp b/test/SemaCXX/large-array-init.cpp new file mode 100644 index 0000000000..6adbd1c03a --- /dev/null +++ b/test/SemaCXX/large-array-init.cpp @@ -0,0 +1,9 @@ +// RUN: %clang_cc1 -S -o %t.ll -mllvm -debug-only=exprconstant %s 2>&1 | \ +// RUN: FileCheck %s + +struct S { int i; }; + +static struct S arr[100000000] = {{ 0 }}; +// CHECK: The number of elements to initialize: 1. + +struct S *foo() { return arr; }