Skip to content

Commit

Permalink
[Diagnostics] Handle string concat pattern and avoid false positives
Browse files Browse the repository at this point in the history
  • Loading branch information
davidbolvansky committed Aug 9, 2020
1 parent 43bdac2 commit 975467e
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 4 deletions.
9 changes: 6 additions & 3 deletions clang/lib/Sema/SemaExpr.cpp
Expand Up @@ -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(
Expand Down
8 changes: 7 additions & 1 deletion clang/test/Sema/string-concat.c
Expand Up @@ -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.
Expand Down Expand Up @@ -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"};

Expand Down

0 comments on commit 975467e

Please sign in to comment.