[Diagnostics] Handle string concat pattern and avoid false positives

This commit is contained in:
Dávid Bolvanský 2020-08-09 16:00:01 +02:00
parent 43bdac2906
commit 975467e4aa
2 changed files with 13 additions and 4 deletions

View File

@ -6908,10 +6908,13 @@ Sema::ActOnInitList(SourceLocation LBraceLoc, MultiExprArg InitArgList,
<< InitArgList[I]->getSourceRange();
} else if (const auto *SL = dyn_cast<StringLiteral>(InitArgList[I])) {
unsigned NumConcat = SL->getNumConcatenated();
const auto *SLNext =
dyn_cast<StringLiteral>(InitArgList[I + 1 < E ? I + 1 : 0]);
// 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 > 2 && !SL->getBeginLoc().isMacroID()) {
// Do not warn when all the elements in the initializer are concatenated
// together. Do not warn for macros too.
if (NumConcat > 1 && E > 2 && !SL->getBeginLoc().isMacroID() && SLNext &&
NumConcat != SLNext->getNumConcatenated()) {
SmallVector<FixItHint, 1> Hints;
for (unsigned i = 0; i < NumConcat - 1; ++i)
Hints.push_back(FixItHint::CreateInsertion(

View File

@ -61,7 +61,7 @@ char missing_comma_inner[][5] = {
#define TWO "foo"
const char *macro_test[] = { ONE("foo") "bar",
TWO "bar",
"foo" TWO // expected-note{{place parentheses around the string literal to silence warning}}
"foo" "bar" TWO // 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?}}
// Do not warn for macros.
@ -104,6 +104,12 @@ const char *not_warn[] = {
"world", "test"
};
const char *not_warn2[] = {
"// Aaa\\\n" " Bbb\\ \n" " Ccc?" "?/\n",
"// Aaa\\\r\n" " Bbb\\ \r\n" " Ccc?" "?/\r\n",
"// Aaa\\\r" " Bbb\\ \r" " Ccc?" "?/\r"
};
// Do not warn when all the elements in the initializer are concatenated together.
const char *all_elems_in_init_concatenated[] = {"a" "b" "c"};