[Diagnostics] Avoid false positives with -Wstring-concatenation

This commit is contained in:
Dávid Bolvanský 2020-08-10 23:04:20 +02:00
parent 2c48e3629c
commit 62223ff137
2 changed files with 20 additions and 5 deletions

View File

@ -6908,13 +6908,15 @@ 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]);
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() && SLNext &&
NumConcat != SLNext->getNumConcatenated()) {
if (NumConcat > 1 && E > 2 && !SL->getBeginLoc().isMacroID() &&
SL->getString().find(" ") == llvm::StringRef::npos &&
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(

View File

@ -108,7 +108,20 @@ 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"
};
};
const char *not_warn3[] = {
"// \\param [in,out] aaa Bbb\n",
"// \\param[in,out] aaa Bbb\n",
"// \\param [in, out] aaa Bbb\n",
"// \\param [in,\n"
"// out] aaa Bbb\n",
"// \\param [in,out]\n"
"// aaa Bbb\n",
"// \\param [in,out] aaa\n"
"// Bbb\n"
};
// Do not warn when all the elements in the initializer are concatenated together.
const char *all_elems_in_init_concatenated[] = {"a" "b" "c"};