Option to warn on comma typo/string concatenation

In the following code, there is a subtle bug because the first entry of the array is missing a comma, so the string literals get implicitly concatenated together.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>
#include <array>
int main()
{
    const char* exts[] = {
        "*.exe"
        "*.msi",
        "*.dll"
    };
    
    for (std::size_t i = 0; i < std::size(exts); i++)
    {
        std::cout << exts[i] << '\n';
    }
}


*.exe*.msi
*.dll


Does anyone know of a way to force GCC or another popular compiler to warn here? If not, does anyone know of an existing code analysis tool that can warn here? Or do I have to write my own tool to do it?
Last edited on
There is currently brand new (i.e., 8-days old) work in Clang on a warning flag -Wstring-concatenation.
https://github.com/llvm/llvm-project/commit/b9af72bffe5f2769f3a7858a785981f89137a0ce

https://clang.llvm.org/docs/DiagnosticsReference.html#wstring-concatenation

Playing around on Compiler explorer, the code you posted doesn't seem to warn unless you slightly change the list, e.g.,
1
2
3
4
5
6
7
8
9
int main()
{
    const char* exts[] = {
        "*.exe"
        "*.msi",
        "*.dll",
        "*.foo" // comment this line and the warning disappears
    };
}

results in a warning, but the code you posted does not.
https://godbolt.org/z/hevvvo

Don't know if this feature is still work-in-progress, but apparently the diagnostic needs a little help.
Last edited on
Thanks mbozzi, I'll try it out.
Topic archived. No new replies allowed.