From: Eli Friedman Date: Sun, 25 May 2008 13:49:22 +0000 (+0000) Subject: Diagnose implicit init list for empty aggregate, like struct {}. Fixes X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=402256fc665ba179873ffcb4d630e28cbea42f27;p=clang Diagnose implicit init list for empty aggregate, like struct {}. Fixes PR2151 (by not creating the empty implicit init list). git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@51556 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/include/clang/Basic/DiagnosticKinds.def b/include/clang/Basic/DiagnosticKinds.def index beea258482..ce41cfdcf6 100644 --- a/include/clang/Basic/DiagnosticKinds.def +++ b/include/clang/Basic/DiagnosticKinds.def @@ -751,6 +751,8 @@ DIAG(err_empty_scalar_initializer, ERROR, "scalar initializer cannot be empty") DIAG(err_illegal_initializer, ERROR, "illegal initializer (only variables can be initialized)") +DIAG(err_implicit_empty_initializer, ERROR, + "initializer for aggregate with no elements requires explicit braces") DIAG(err_redefinition_of_label, ERROR, "redefinition of label '%0'") diff --git a/lib/Sema/SemaInit.cpp b/lib/Sema/SemaInit.cpp index 35dda28d4a..6eb072eeba 100644 --- a/lib/Sema/SemaInit.cpp +++ b/lib/Sema/SemaInit.cpp @@ -57,6 +57,13 @@ void InitListChecker::CheckImplicitInitList(InitListExpr *ParentIList, else assert(0 && "CheckImplicitInitList(): Illegal type"); + if (maxElements == 0) { + SemaRef->Diag(ParentIList->getInit(Index)->getLocStart(), + diag::err_implicit_empty_initializer); + hadError = true; + return; + } + // Check the element types *before* we create the implicit init list; // otherwise, we might end up taking the wrong number of elements unsigned NewIndex = Index; diff --git a/test/Sema/array-init.c b/test/Sema/array-init.c index a7f27b68f8..69255569dd 100644 --- a/test/Sema/array-init.c +++ b/test/Sema/array-init.c @@ -212,3 +212,7 @@ int u2 = {{3}}; //expected-error{{too many braces around scalar initializer}} void varArray() { int c[][x] = { 0 }; //expected-error{{variable-sized object may not be initialized}} } + +// PR2151 +int emptyInit() {struct {} x[] = {6};} //expected-warning{{empty struct extension}} expected-error{{initializer for aggregate with no elements}} +