[Diagnostics] Ignore structs and long text for -Wstring-concatenation

This commit is contained in:
Dávid Bolvanský 2020-08-11 00:49:47 +02:00
parent 0de60b550b
commit 4b59dc77dc
2 changed files with 54 additions and 21 deletions

View File

@ -6865,6 +6865,7 @@ Sema::ActOnInitList(SourceLocation LBraceLoc, MultiExprArg InitArgList,
bool DiagnosedArrayDesignator = false;
bool DiagnosedNestedDesignator = false;
bool DiagnosedMixedDesignator = false;
bool DiagnosedMissingComma = false;
// Check that any designated initializers are syntactically valid in the
// current language mode.
@ -6908,22 +6909,34 @@ 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 *SLPrev =
dyn_cast<StringLiteral>(InitArgList[I == 0 ? E - 1 : I - 1]);
// 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() &&
isa<StringLiteral>(InitArgList[0]) && SLPrev &&
NumConcat != SLPrev->getNumConcatenated()) {
SmallVector<FixItHint, 1> Hints;
for (unsigned i = 0; i < NumConcat - 1; ++i)
Hints.push_back(FixItHint::CreateInsertion(
PP.getLocForEndOfToken(SL->getStrTokenLoc(i)), ","));
if (!DiagnosedMissingComma && NumConcat == 2 && E > 2 && !SL->getBeginLoc().isMacroID()) {
bool OnlyOneMissingComma = true;
for (unsigned J = 0; J < E; ++J) {
if (J == I)
continue;
const auto *SLJ = dyn_cast<StringLiteral>(InitArgList[J]);
if (!SLJ || SLJ->getNumConcatenated() > 1) {
OnlyOneMissingComma = false;
break;
}
}
Diag(SL->getStrTokenLoc(1), diag::warn_concatenated_literal_array_init)
<< Hints;
Diag(SL->getBeginLoc(), diag::note_concatenated_string_literal_silence);
if (OnlyOneMissingComma) {
SmallVector<FixItHint, 1> Hints;
for (unsigned i = 0; i < NumConcat - 1; ++i)
Hints.push_back(FixItHint::CreateInsertion(
PP.getLocForEndOfToken(SL->getStrTokenLoc(i)), ","));
Diag(SL->getStrTokenLoc(1),
diag::warn_concatenated_literal_array_init)
<< Hints;
Diag(SL->getBeginLoc(),
diag::note_concatenated_string_literal_silence);
DiagnosedMissingComma = true;
}
}
}
}

View File

@ -32,12 +32,6 @@ const char *missing_comma_u8[] = {
};
#endif
const char *missing_two_commas[] = {"basic_filebuf",
"basic_ios" // expected-note{{place parentheses around the string literal to silence warning}}
"future" // expected-warning{{suspicious concatenation of string literals in an array initialization; did you mean to separate the elements with a comma?}}
"optional",
"packaged_task"};
const char *missing_comma_same_line[] = {"basic_filebuf", "basic_ios",
"future" "optional", // expected-note{{place parentheses around the string literal to silence warning}}
"packaged_task", "promise"}; // expected-warning@-1{{suspicious concatenation of string literals in an array initialization; did you mean to separate the elements with a comma?}}
@ -56,12 +50,20 @@ char missing_comma_inner[][5] = {
"d" // expected-warning{{suspicious concatenation of string literals in an array initialization; did you mean to separate the elements with a comma?}}
};
const char *warn[] = { "cpll", "gpll", "hdmiphy" "usb480m" }; // 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?}}
const char *missing_two_commas_ignore[] = {"basic_filebuf",
"basic_ios"
"future"
"optional",
"packaged_task"};
#define ONE(x) x
#define TWO "foo"
const char *macro_test[] = { ONE("foo") "bar",
TWO "bar",
"foo" "bar" TWO // expected-note{{place parentheses around the string literal to silence warning}}
const char *macro_test[] = { ONE("foo"),
TWO,
"foo" 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.
@ -110,6 +112,24 @@ const char *not_warn2[] = {
"// Aaa\\\r" " Bbb\\ \r" " Ccc?" "?/\r"
};
const char *not_warn3[] = {
"// \\tparam aaa Bbb\n",
"// \\tparam\n"
"// aaa Bbb\n",
"// \\tparam \n"
"// aaa Bbb\n",
"// \\tparam aaa\n"
"// Bbb\n"
};
const char *not_warn4[] = {"title",
"aaaa "
"bbbb "
"cccc "
"ddd.",
"url"
};
// Do not warn when all the elements in the initializer are concatenated together.
const char *all_elems_in_init_concatenated[] = {"a" "b" "c"};