diff --git a/clang/include/clang/Basic/DiagnosticGroups.td b/clang/include/clang/Basic/DiagnosticGroups.td --- a/clang/include/clang/Basic/DiagnosticGroups.td +++ b/clang/include/clang/Basic/DiagnosticGroups.td @@ -466,6 +466,7 @@ def MismatchedReturnTypes : DiagGroup<"mismatched-return-types">; def MismatchedTags : DiagGroup<"mismatched-tags">; def MissingFieldInitializers : DiagGroup<"missing-field-initializers">; +def MissingArrayInitializers : DiagGroup<"missing-array-initializers">; def ModuleLock : DiagGroup<"module-lock">; def ModuleBuild : DiagGroup<"module-build">; def ModuleImport : DiagGroup<"module-import">; diff --git a/clang/include/clang/Basic/DiagnosticSemaKinds.td b/clang/include/clang/Basic/DiagnosticSemaKinds.td --- a/clang/include/clang/Basic/DiagnosticSemaKinds.td +++ b/clang/include/clang/Basic/DiagnosticSemaKinds.td @@ -5797,6 +5797,9 @@ def warn_missing_field_initializers : Warning< "missing field %0 initializer">, InGroup, DefaultIgnore; +def warn_missing_array_initializers : Warning< + "missing array initializer: expected %0 elements, found %1">, + InGroup, DefaultIgnore; def warn_braces_around_init : Warning< "braces around %select{scalar |}0initializer">, InGroup>; diff --git a/clang/lib/Sema/SemaInit.cpp b/clang/lib/Sema/SemaInit.cpp --- a/clang/lib/Sema/SemaInit.cpp +++ b/clang/lib/Sema/SemaInit.cpp @@ -1991,6 +1991,12 @@ CheckEmptyInitializable( InitializedEntity::InitializeElement(SemaRef.Context, 0, Entity), IList->getEndLoc()); + if (!VerifyOnly && maxElementsKnown && elementIndex > 0 && + elementIndex < maxElements) { + SemaRef.Diag(IList->getEndLoc(), diag::warn_missing_array_initializers) + << (unsigned)maxElements.getExtValue() + << (unsigned)elementIndex.getExtValue(); + } } } diff --git a/clang/test/Sema/missing-array-initializers.c b/clang/test/Sema/missing-array-initializers.c new file mode 100644 --- /dev/null +++ b/clang/test/Sema/missing-array-initializers.c @@ -0,0 +1,24 @@ +// RUN: %clang_cc1 -fsyntax-only -verify -Wmissing-array-initializers %s + +// We should only warn on an explicitly sized initializer for an array +// specifier. This means no warning on arrays without size, and no warning on +// string literal specfiers (even if the string literal has a size). + +// not-expected-warning@+1 {{missing array initializer}} +char Doggo[] = "Doggo"; + +// not-expected-warning@+1 {{missing array initializer}} +char Pupper[16] = "Pupper"; + +// expected-warning@+1 {{missing array initializer: expected 16 elements, found 7}} +char Floofer[16] = {'F', 'L', 'O', 'O', 'F', 'E', 'R'}; + +// not-expected-warning@+1 {{missing array initializer}} +int Array[] = {1, 2, 3, 4, 5}; + +// expected-warning@+1 {{missing array initializer: expected 10 elements, found 5}} +int SizedArray[10] = {1, 2, 3, 4, 5}; + +// Don't fire on zero-initialized shorthand either +// not-expected-warning@+1 {{missing array initializer}} +int ZeroInitialized[10] = {};