Bug 99131 - gcc doesn't detect missing comma in array initialisation [-Wstring-concatenation]
Summary: gcc doesn't detect missing comma in array initialisation [-Wstring-concatenat...
Status: NEW
Alias: None
Product: gcc
Classification: Unclassified
Component: c (show other bugs)
Version: unknown
: P3 enhancement
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords: diagnostic
Depends on:
Blocks: new-warning, new_warning
  Show dependency treegraph
 
Reported: 2021-02-17 10:35 UTC by David Binderman
Modified: 2024-06-15 02:17 UTC (History)
4 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2021-02-17 00:00:00


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description David Binderman 2021-02-17 10:35:26 UTC
For this C code:

const char * a[ 4] = {
	" fred ",
	" bert "
	" harry ",
	" eric "
};

Note missing "," on the bert line. gcc doesn't say very much:

$ /home/dcb/gcc/results/bin/gcc -c -g -O2 -Wall -Wextra feb17f.cc
$

Here is a recent clang doing something more useful:

$ /home/dcb/llvm/clang1200rc1/bin/clang++ -c -g -O2 -Wall -Wextra feb17f.cc
feb17f.cc:7:2: warning: suspicious concatenation of string literals in an array initialization; did you mean to separate the elements with a comma? [-Wstring-concatenation]
        " harry ",
        ^
feb17f.cc:6:2: note: place parentheses around the string literal to silence warning
        " bert "
        ^
1 warning generated.
Comment 1 Eric Gallager 2021-02-17 15:25:24 UTC
Having this warning would have saved me a lot of grief trying to hunt down a particular bug I remember having to deal with in the past; confirmed.
Comment 2 David Binderman 2021-02-17 16:33:34 UTC
I usually write code that compiles warning free on both gcc and clang.

I only noticed this difference between gcc and clang as a result
of compiling the latest release of the tor browser. I thought it
would be good to generalise it.

I guess there must be some way to get both gcc and clang to emit
all possible warning messages they can, then diff the two lists,
to find out where one compiler could be enhanced. The strings 
command might be the way forward on this.

I'd be happy to help with testing any prototype implementation.

Interesting, my usual static analyser, cppcheck, has nothing to say
also, so that's probably worth a bug report too.
Comment 3 Eric Gallager 2021-03-15 20:45:10 UTC
Adding clang's name for this warning to the title for easier findability. Also this warning has found a few issues in GCC's own code; see here:
https://gcc.gnu.org/pipermail/gcc-patches/2021-March/566749.html
...and here:
https://gcc.gnu.org/pipermail/gcc-patches/2021-March/566750.html
Comment 4 David Binderman 2022-01-11 21:04:18 UTC
Interestingly, I compiled fedora rawhide with clang and found
a whopping 247 cases of this warning across 11 packages.