Motivation (from PR37674):
const char *ss[] = {
"foo", "bar", "baz", "qux" // <-- Missing comma! "abc", "xyz" };
This kind of bug was recently also found in LLVM codebase (see PR47030).
Solves PR47038, PR37674
Differential D85545
[Diagnostics] Diagnose missing comma in string array initialization xbolva00 on Aug 7 2020, 12:17 PM. Authored by
Details Motivation (from PR37674): const char *ss[] = { "foo", "bar", "baz", "qux" // <-- Missing comma! "abc", "xyz" }; This kind of bug was recently also found in LLVM codebase (see PR47030). Solves PR47038, PR37674
Diff Detail
Event TimelineComment Actions What if the user did actually want to concatenate the strings? Eg., one of the strings in the list is long and clang-format suggests breaking it up for the 80-column limit which causes the new warning to appear. How would the user suppress the warning in this case? Comment Actions Parentheses could work here: const char *test1[] = { "basic_filebuf", "basic_ios", "future", "optional", ("packaged_task" "promise"), "shared_future" };
Comment Actions New testcases.
Comment Actions LGTM aside from a nit, but you should give other reviewers a chance to comment if they have additional feedback.
Comment Actions And we caught one more bug in LLVM repo (in libcxx) const char* TestCaseSetOne[] = {"", "s", "bac", "bacasf" "lkajseravea", "adsfkajdsfjkas;lnc441324513,34535r34525234", "b*c", "ba?sf" "lka*ea", "adsf*kas;lnc441[0-9]1r34525234"}; Comment Actions Hello, sorry but can you please revert this commit and recommit it when you have a fix or work around that doesn't break our bots: Comment Actions Logs look quite old. http://lab.llvm.org:8011/builders/ppc64le-lld-multistage-test/builds/11291/steps/build-stage2-unified-tree/logs/stdio po 10. 8. 2020 o 21:46 Kamau Bridgeman via Phabricator
Comment Actions In the Firefox repo this warning is firing on a number of strings that were broken up by clang-format (or humans) for line length, for example https://searchfox.org/mozilla-central/rev/ab81b8552f4aa9696a2524f97fdfeb59d4dc31c1/security/certverifier/ExtendedValidation.cpp#176-178 or https://searchfox.org/mozilla-central/rev/ab81b8552f4aa9696a2524f97fdfeb59d4dc31c1/xpcom/tests/gtest/TestEscape.cpp#103-104 or https://searchfox.org/mozilla-central/rev/ab81b8552f4aa9696a2524f97fdfeb59d4dc31c1/js/src/jsapi-tests/testXDR.cpp#115. Do you consider these to be false positives in your view? Should there be some exception for line length, perhaps? Comment Actions I moved it to -Wextra due to false positives.
Yeah, but sure how to define the threshold or so. :/ po 10. 8. 2020 o 23:21 dmajor via Phabricator
Comment Actions It looks to me as if all of the false-positives so far have been *not struct X { int a; const char *b; int c; }; The distinguishing feature here is that if you did insert a comma as Dávid, can you use this in some way? my $.02, Comment Actions For your cases, we currently do not warn. If possible, fetch the po 10. 8. 2020 o 23:46 Arthur O'Dwyer <arthur.j.odwyer@gmail.com> napísal(a):
Comment Actions Something like this: const char *Sources[] = { "// \\tparam aaa Bbb\n", "// \\tparam\n" "// aaa Bbb\n", "// \\tparam \n" "// aaa Bbb\n", "// \\tparam aaa\n" "// Bbb\n" }; annoys me :/ Any idea/heuristic how to avoid warning here? po 10. 8. 2020 o 23:48 Dávid Bolvanský <david.bolvansky@gmail.com> napísal(a):
Comment Actions To decrease the number of false-positives, you could emit the warning only const char *likely_a_bug[] = { "a", "b", "c" "d", "e", "f", "g", "h", "i" }; const char *likely_not_a_bug[] = { "a", "b" "c", "d" "e", "f" "g" }; const char *oops_still_a_bug[] = { "a", "b", "c" "d", "e", "f" "g", "h", "i" }; However, as oops_still_a_bug shows, that tactic would also decrease the I still think it would be appropriate to *stop issuing the warning for –Arthur Comment Actions For your godbolt example, you hit the heuristic to not warn, if
This could work. ut 11. 8. 2020 o 0:04 Arthur O'Dwyer <arthur.j.odwyer@gmail.com> napísal(a):
Comment Actions Pushed fix. It should not warn for structs or long texts. ut 11. 8. 2020 o 0:34 Arthur Eubanks via Phabricator
Comment Actions After not warning on structs, this did find a real missing comma in the Chromium codebase, so thanks! Comment Actions Actually sorry, it does still seem like there are false positives on structs. Reduced: $ cat /tmp/a.cpp struct A { const char* a; const char* b; const char* c; }; static constexpr A foo2 = A{"", "" "", ""}; $ ~/repos/llvm-project/build_cmake/bin/clang /tmp/a.cpp -o /dev/null -c -Wstring-concatenation /tmp/a.cpp:10:29: warning: suspicious concatenation of string literals in an array initialization; did you mean to separate the elements with a comma? [-Wstring-concatenation] "", ^ /tmp/a.cpp:9:29: note: place parentheses around the string literal to silence warning "" ^ 1 warning generated. Comment Actions I check if all elements of init list are strings, so this is not ut 11. 8. 2020 o 19:55 Arthur Eubanks via Phabricator
Comment Actions Yup it's reduced from real world code, same link as before: https://source.chromium.org/chromium/chromium/src/+/master:third_party/dawn/src/dawn_native/Toggles.cpp;drc=80f927d763211ea8e6a6377f86282809c86dc107;l=32. Comment Actions Ok, I will bump that limit + 1. ut 11. 8. 2020 o 20:52 Arthur Eubanks via Phabricator
Comment Actions Dávid: Please just disable it for initializers of structs. That seems to be Comment Actions Reworked ;) Now it should ignore structs properly. ut 11. 8. 2020 o 22:03 Arthur O'Dwyer <arthur.j.odwyer@gmail.com> napísal(a):
Comment Actions It still seems to trigger on structs at head: $ cat /tmp/a.cc struct A { const char *a; const char *b; const char *c; }; static A a = {"", "" "", ""}; $ ./build/bin/clang++ -Wstring-concatenation -o /dev/null -c /tmp/a.cc C:/src/tmp/a.cc:10:15: warning: suspicious concatenation of string literals in an array initialization; did you mean to separate the elements with a comma? [-Wstring-concatenation] "", ^ C:/src/tmp/a.cc:9:15: note: place parentheses around the string literal to silence warning "" ^ 1 warning generated. Comment Actions Done
|
How about: suspicious concatenation of string literals in an array initialization; did you mean to separate the elements with a comma?