This is an archive of the discontinued LLVM Phabricator instance.

[clang] Improve diagnostic for "initializer-string for char array is too long"
ClosedPublic

Authored by evansmal on Jan 9 2023, 7:11 AM.

Details

Summary

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.

Fixes #58829

Diff Detail

Event Timeline

evansmal created this revision.Jan 9 2023, 7:11 AM
Herald added a project: Restricted Project. · View Herald TranscriptJan 9 2023, 7:11 AM
evansmal requested review of this revision.Jan 9 2023, 7:11 AM
shafik added a subscriber: shafik.Jan 9 2023, 7:41 AM

Thank you for this diagnostic fix, please make sure you update clang/test/CXX/dcl.decl/dcl.init/dcl.init.string/p2.cpp.

clang/include/clang/Basic/DiagnosticSemaKinds.td
5968

Note. we are allowed to initialize with a string-literal that is smaller than the array size so maybe something along the lines of array size is %0 but initializer has size %1

evansmal added a comment.EditedJan 9 2023, 7:53 AM

Thank you for this diagnostic fix, please make sure you update clang/test/CXX/dcl.decl/dcl.init/dcl.init.string/p2.cpp.

@shafik I noticed that there are also some tests in: clang/test/SemaCXX/pascal-strings.cpp, clang/test/Sema/array-init.c, and clang/test/Sema/format-strings.c that also check for this diagnostic. I assume these should also be updated?

EDIT: It seems that git-clang-format is failing for the build. Running it re-formats most of clang/lib/Sema/SemaInit.cpp - can I commit these changes here or should I open a seperate PR for that?

evansmal updated this revision to Diff 487443.Jan 9 2023, 7:55 AM

Improve specificity of the diagnostic message.

When grepping clang/test/ for "for char array is too long", I get a bunch of other hits for this diagnostic, are the those tests not failing for you (e.g. clang/test/Sema/array-init.c:149)?

evansmal marked an inline comment as done.EditedJan 10 2023, 8:14 AM

When grepping clang/test/ for "for char array is too long", I get a bunch of other hits for this diagnostic, are the those tests not failing for you (e.g. clang/test/Sema/array-init.c:149)?

@tbaeder Yeah, the tests don't fail for me. I can update them, but I'm unsure how to tell if they're working properly.

When grepping clang/test/ for "for char array is too long", I get a bunch of other hits for this diagnostic, are the those tests not failing for you (e.g. clang/test/Sema/array-init.c:149)?

The change is specific to C++, so it should not apply to .c file, right?

When grepping clang/test/ for "for char array is too long", I get a bunch of other hits for this diagnostic, are the those tests not failing for you (e.g. clang/test/Sema/array-init.c:149)?

"expected-error" matches a substring, that's why they are not failing.

gribozavr2 accepted this revision.Jan 10 2023, 8:30 AM
This revision is now accepted and ready to land.Jan 10 2023, 8:30 AM

Thanks all for the review!

shafik accepted this revision.Jan 10 2023, 1:48 PM

LGTM

clang/test/CXX/dcl.decl/dcl.init/dcl.init.string/p2.cpp
2–3

Can you also add:

char test2[1]="";

We expect this to succeed

evansmal updated this revision to Diff 488012.Jan 10 2023, 3:17 PM

Added additional test case

evansmal marked an inline comment as done.Jan 10 2023, 3:17 PM
evansmal edited the summary of this revision. (Show Details)Jan 12 2023, 5:17 PM

Hi @shafik! Thanks for the review. I don't have commit access and am unsure what I should do to get this committed. Can you let me know what I need to do next?

AFAIK we need a name and an email address so the commit can be attributed properly.

AFAIK we need a name and an email address so the commit can be attributed properly.

The commit can be attributed to Evan Smal <evan.smal@hotmail.com>

This revision was landed with ongoing or failed builds.Jan 19 2023, 4:13 AM
This revision was automatically updated to reflect the committed changes.