[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 14:16:04 PDT 2021


beanz created this revision.
beanz added reviewers: aaron.ballman, rsmith, RKSimon, dexonsmith.
beanz requested review of this revision.
Herald added a project: clang.

When defining a statically sized array with explicit array
initializers, having a warning for uninitialized members is nice.

This warning will only fire on missing elements in an explicitly sized
array initialized with an array initializer. It will not fire on char
arrays that are initialized using a string literal sequence.

This warning is added under a new warning group
-Wmissing-array-initializers.


Repository:
  rG LLVM Github Monorepo

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,20 @@
+// 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};
Index: clang/lib/Sema/SemaInit.cpp
===================================================================
--- clang/lib/Sema/SemaInit.cpp
+++ clang/lib/Sema/SemaInit.cpp
@@ -1991,6 +1991,11 @@
       CheckEmptyInitializable(
           InitializedEntity::InitializeElement(SemaRef.Context, 0, Entity),
           IList->getEndLoc());
+    if (!VerifyOnly && maxElementsKnown && 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.375699.patch
Type: text/x-patch
Size: 2971 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20210928/4aa0e0f0/attachment.bin>


More information about the cfe-commits mailing list