[PATCH] D141283: [clang] Improve diagnostic for "initializer-string for char array is too long"
Evan Smal via Phabricator via cfe-commits
cfe-commits at lists.llvm.org
Tue Jan 10 15:17:05 PST 2023
evansmal updated this revision to Diff 488012.
evansmal added a comment.
Added additional test case
Repository:
rG LLVM Github Monorepo
CHANGES SINCE LAST ACTION
https://reviews.llvm.org/D141283/new/
https://reviews.llvm.org/D141283
Files:
clang/include/clang/Basic/DiagnosticSemaKinds.td
clang/lib/Sema/SemaInit.cpp
clang/test/CXX/dcl.decl/dcl.init/dcl.init.string/p2.cpp
Index: clang/test/CXX/dcl.decl/dcl.init/dcl.init.string/p2.cpp
===================================================================
--- clang/test/CXX/dcl.decl/dcl.init/dcl.init.string/p2.cpp
+++ clang/test/CXX/dcl.decl/dcl.init/dcl.init.string/p2.cpp
@@ -1,2 +1,3 @@
// RUN: %clang_cc1 -fsyntax-only -verify %s
-char test1[1]="f"; // expected-error {{initializer-string for char array is too long}}
+char test1[1]="f"; // expected-error {{initializer-string for char array is too long, array size is 1 but initializer has size 2 (including the null terminating character)}}
+char test2[1]="";
Index: clang/lib/Sema/SemaInit.cpp
===================================================================
--- clang/lib/Sema/SemaInit.cpp
+++ clang/lib/Sema/SemaInit.cpp
@@ -239,6 +239,8 @@
if (StrLength > CAT->getSize().getZExtValue())
S.Diag(Str->getBeginLoc(),
diag::err_initializer_string_for_char_array_too_long)
+ << CAT->getSize().getZExtValue()
+ << StrLength
<< Str->getSourceRange();
} else {
// C99 6.7.8p14.
Index: clang/include/clang/Basic/DiagnosticSemaKinds.td
===================================================================
--- clang/include/clang/Basic/DiagnosticSemaKinds.td
+++ clang/include/clang/Basic/DiagnosticSemaKinds.td
@@ -5953,7 +5953,7 @@
"excess elements in char array initializer">,
InGroup<ExcessInitializers>;
def err_initializer_string_for_char_array_too_long : Error<
- "initializer-string for char array is too long">;
+ "initializer-string for char array is too long, array size is %0 but initializer has size %1 (including the null terminating character)">;
def ext_initializer_string_for_char_array_too_long : ExtWarn<
"initializer-string for char array is too long">,
InGroup<ExcessInitializers>;
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D141283.488012.patch
Type: text/x-patch
Size: 1811 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20230110/d63174ef/attachment.bin>
More information about the cfe-commits
mailing list