diff --git a/clang/lib/Sema/SemaExpr.cpp b/clang/lib/Sema/SemaExpr.cpp index 35047a7b2b145b..74427f8cd7ae8e 100644 --- a/clang/lib/Sema/SemaExpr.cpp +++ b/clang/lib/Sema/SemaExpr.cpp @@ -6908,10 +6908,13 @@ Sema::ActOnInitList(SourceLocation LBraceLoc, MultiExprArg InitArgList, << InitArgList[I]->getSourceRange(); } else if (const auto *SL = dyn_cast(InitArgList[I])) { unsigned NumConcat = SL->getNumConcatenated(); + const auto *SLNext = + dyn_cast(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 Hints; for (unsigned i = 0; i < NumConcat - 1; ++i) Hints.push_back(FixItHint::CreateInsertion( diff --git a/clang/test/Sema/string-concat.c b/clang/test/Sema/string-concat.c index c93bbd4eaa006e..13e9656d2536f3 100644 --- a/clang/test/Sema/string-concat.c +++ b/clang/test/Sema/string-concat.c @@ -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"};