[PATCH] D110656: [clang][Sema] Warn on uninitialized array elments

Chris Bieneman via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Tue Sep 28 15:00:44 PDT 2021


beanz updated this revision to Diff 375711.
beanz added a comment.

Updating so that the warning doesn't fire on an empty initializer list.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D110656/new/

https://reviews.llvm.org/D110656

Files:
  clang/include/clang/Basic/DiagnosticGroups.td
  clang/include/clang/Basic/DiagnosticSemaKinds.td
  clang/lib/Sema/SemaInit.cpp
  clang/test/Sema/missing-array-initializers.c


Index: clang/test/Sema/missing-array-initializers.c
===================================================================
--- /dev/null
+++ 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 at +1 {{missing array initializer}}
+char Doggo[] = "Doggo";
+
+// not-expected-warning at +1 {{missing array initializer}}
+char Pupper[16] = "Pupper";
+
+// expected-warning at +1 {{missing array initializer: expected 16 elements, found 7}}
+char Floofer[16] = {'F', 'L', 'O', 'O', 'F', 'E', 'R'};
+
+// not-expected-warning at +1 {{missing array initializer}}
+int Array[] = {1, 2, 3, 4, 5};
+
+// expected-warning at +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 at +1 {{missing array initializer}}
+int ZeroInitialized[10] = {};
Index: clang/lib/Sema/SemaInit.cpp
===================================================================
--- clang/lib/Sema/SemaInit.cpp
+++ 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();
+    }
   }
 }
 
Index: clang/include/clang/Basic/DiagnosticSemaKinds.td
===================================================================
--- clang/include/clang/Basic/DiagnosticSemaKinds.td
+++ clang/include/clang/Basic/DiagnosticSemaKinds.td
@@ -5797,6 +5797,9 @@
 def warn_missing_field_initializers : Warning<
   "missing field %0 initializer">,
   InGroup<MissingFieldInitializers>, DefaultIgnore;
+def warn_missing_array_initializers : Warning<
+  "missing array initializer: expected %0 elements, found %1">,
+  InGroup<MissingArrayInitializers>, DefaultIgnore;
 def warn_braces_around_init : Warning<
   "braces around %select{scalar |}0initializer">,
   InGroup<DiagGroup<"braced-scalar-init">>;
Index: clang/include/clang/Basic/DiagnosticGroups.td
===================================================================
--- clang/include/clang/Basic/DiagnosticGroups.td
+++ 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">;


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D110656.375711.patch
Type: text/x-patch
Size: 3143 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20210928/ee4ba37f/attachment-0001.bin>


More information about the cfe-commits mailing list