[PATCH] D152083: [clang] Warning for uninitialized elements in fixed-size arrays
Louis Burda via Phabricator via cfe-commits
cfe-commits at lists.llvm.org
Sun Jun 4 18:22:38 PDT 2023
Sinitax updated this revision to Diff 528256.
Sinitax added a comment.
Add new warning to its own group.
CHANGES SINCE LAST ACTION
https://reviews.llvm.org/D152083/new/
https://reviews.llvm.org/D152083
Files:
clang/include/clang/Basic/DiagnosticGroups.td
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">,
+ InGroup<UninitializedArrayElements>, DefaultIgnore;
def warn_uninit_var : Warning<
"variable %0 is uninitialized when %select{used here|captured by block}1">,
InGroup<Uninitialized>, DefaultIgnore;
Index: clang/include/clang/Basic/DiagnosticGroups.td
===================================================================
--- clang/include/clang/Basic/DiagnosticGroups.td
+++ clang/include/clang/Basic/DiagnosticGroups.td
@@ -729,6 +729,7 @@
def UninitializedSometimes : DiagGroup<"sometimes-uninitialized">;
def UninitializedStaticSelfInit : DiagGroup<"static-self-init">;
def UninitializedConstReference : DiagGroup<"uninitialized-const-reference">;
+def UninitializedArrayElements : DiagGroup<"uninitialized-array-elements">;
def Uninitialized : DiagGroup<"uninitialized", [UninitializedSometimes,
UninitializedStaticSelfInit,
UninitializedConstReference]>;
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D152083.528256.patch
Type: text/x-patch
Size: 2532 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20230605/b3826bc1/attachment.bin>
More information about the cfe-commits
mailing list