[PATCH] D152083: [clang] Warning for uninitialized elements in fixed-size arrays

Louis Burda via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Sat Jun 3 16:28:07 PDT 2023


Sinitax updated this revision to Diff 528162.
Sinitax added a comment.

Remove new warning from -Wuninitialized group for backwards-compatibility.


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

https://reviews.llvm.org/D152083

Files:
  clang/include/clang/Basic/DiagnosticSemaKinds.td
  clang/lib/Sema/SemaInit.cpp


Index: clang/lib/Sema/SemaInit.cpp
===================================================================
--- clang/lib/Sema/SemaInit.cpp
+++ clang/lib/Sema/SemaInit.cpp
@@ -980,6 +980,22 @@
     if (RequiresSecondPass && !hadError)
       FillInEmptyInitializations(Entity, FullyStructuredList,
                                  RequiresSecondPass, nullptr, 0);
+
+    if (const ConstantArrayType *CAType = dyn_cast<ConstantArrayType>(T)) {
+      if (FullyStructuredList->getNumInits() < CAType->getSize().getZExtValue()) {
+        S.Diag(IL->getBeginLoc(), diag::warn_uninit_fixed_size_array)
+          << IL->getSourceRange();
+      } else {
+        Expr **inits = FullyStructuredList->getInits();
+        for (unsigned i = 0, e = FullyStructuredList->getNumInits(); i != e; ++i) {
+          if (inits[i] == nullptr) {
+            S.Diag(IL->getBeginLoc(), diag::warn_uninit_fixed_size_array)
+              << IL->getSourceRange();
+            break;
+          }
+        }
+      }
+    }
   }
   if (hadError && FullyStructuredList)
     FullyStructuredList->markError();
Index: clang/include/clang/Basic/DiagnosticSemaKinds.td
===================================================================
--- clang/include/clang/Basic/DiagnosticSemaKinds.td
+++ clang/include/clang/Basic/DiagnosticSemaKinds.td
@@ -2214,6 +2214,9 @@
   "reference %0 is not yet bound to a value when used within its own"
   " initialization">,
   InGroup<Uninitialized>;
+def warn_uninit_fixed_size_array : Warning<
+  "fixed-size array contains uninitialized elements">,
+  DefaultIgnore;
 def warn_uninit_var : Warning<
   "variable %0 is uninitialized when %select{used here|captured by block}1">,
   InGroup<Uninitialized>, DefaultIgnore;


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D152083.528162.patch
Type: text/x-patch
Size: 1730 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20230603/ef68d78d/attachment.bin>


More information about the cfe-commits mailing list