[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
Mon Jan 9 07:11:08 PST 2023


evansmal created this revision.
evansmal added a reviewer: clang.
evansmal added a project: clang.
Herald added a project: All.
evansmal requested review of this revision.
Herald added a subscriber: cfe-commits.

This patch improves the diagnostic message "initializer-string for char array is too long" 
by specifying an expected array length and by indicating that the initializer string implicitly
includes the null terminator.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D141283

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
@@ -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, expected %0 but was %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.487419.patch
Type: text/x-patch
Size: 1195 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20230109/77b9b602/attachment.bin>


More information about the cfe-commits mailing list