Skip to content

Commit

Permalink
[Diagnostics] Turn string concat warning to avoid false positives
Browse files Browse the repository at this point in the history
  • Loading branch information
davidbolvansky committed Aug 9, 2020
1 parent 186a7f8 commit 04a23f1
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 6 deletions.
2 changes: 1 addition & 1 deletion clang/lib/Sema/SemaExpr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6911,7 +6911,7 @@ Sema::ActOnInitList(SourceLocation LBraceLoc, MultiExprArg InitArgList,
// Diagnose missing comma in string array initialization.
// Do not warn when all the elements in the initializer are concatenated together.
// Do not warn for macros too.
if (NumConcat > 1 && E > 1 && !SL->getBeginLoc().isMacroID()) {
if (NumConcat > 1 && E > 2 && !SL->getBeginLoc().isMacroID()) {
SmallVector<FixItHint, 1> Hints;
for (unsigned i = 0; i < NumConcat - 1; ++i)
Hints.push_back(FixItHint::CreateInsertion(
Expand Down
25 changes: 20 additions & 5 deletions clang/test/Sema/string-concat.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,16 @@ typedef __WCHAR_TYPE__ wchar_t;
const wchar_t *missing_comma_wchar[] = {
L"basic_filebuf",
L"packaged_task" // expected-note{{place parentheses around the string literal to silence warning}}
L"promise" // expected-warning{{suspicious concatenation of string literals in an array initialization; did you mean to separate the elements with a comma?}}
L"promise", // expected-warning{{suspicious concatenation of string literals in an array initialization; did you mean to separate the elements with a comma?}}
L"shared_future"
};

#if __cplusplus >= 201103L
const char *missing_comma_u8[] = {
u8"basic_filebuf",
u8"packaged_task" // expected-note{{place parentheses around the string literal to silence warning}}
u8"promise" // expected-warning{{suspicious concatenation of string literals in an array initialization; did you mean to separate the elements with a comma?}}
u8"promise", // expected-warning{{suspicious concatenation of string literals in an array initialization; did you mean to separate the elements with a comma?}}
u8"shared_future"
};
#endif

Expand All @@ -47,10 +49,11 @@ const char *missing_comma_different_lines[] = {"basic_filebuf", "basic_ios" // e
const char *missing_comma_same_line_all_literals[] = {"basic_filebuf", "future" "optional", "packaged_task"}; // expected-note{{place parentheses around the string literal to silence warning}}
// expected-warning@-1{{suspicious concatenation of string literals in an array initialization; did you mean to separate the elements with a comma?}}

char missing_comma_inner[][4] = {
char missing_comma_inner[][5] = {
"a",
"b" // expected-note{{place parentheses around the string literal to silence warning}}
"c" // expected-warning{{suspicious concatenation of string literals in an array initialization; did you mean to separate the elements with a comma?}}
"b",
"c" // expected-note{{place parentheses around the string literal to silence warning}}
"d" // expected-warning{{suspicious concatenation of string literals in an array initialization; did you mean to separate the elements with a comma?}}
};


Expand Down Expand Up @@ -89,6 +92,18 @@ const char *macro_test4[] = {"basic_filebuf",
#define SUPPRESS(x) x
const char *macro_test5[] = { SUPPRESS("foo" "bar"), "baz" };

typedef struct {
int i;
const char s[11];
} S;

S s = {1, "hello" "world"};

const char *not_warn[] = {
"hello"
"world", "test"
};

// Do not warn when all the elements in the initializer are concatenated together.
const char *all_elems_in_init_concatenated[] = {"a" "b" "c"};

Expand Down

0 comments on commit 04a23f1

Please sign in to comment.