[Diagnostics] Turn string concat warning to avoid false positives
authorDávid Bolvanský <david.bolvansky@gmail.com>
Sun, 9 Aug 2020 10:22:29 +0000 (12:22 +0200)
committerDávid Bolvanský <david.bolvansky@gmail.com>
Sun, 9 Aug 2020 10:22:29 +0000 (12:22 +0200)
clang/lib/Sema/SemaExpr.cpp
clang/test/Sema/string-concat.c

index 7560dc9..35047a7 100644 (file)
@@ -6911,7 +6911,7 @@ Sema::ActOnInitList(SourceLocation LBraceLoc, MultiExprArg InitArgList,
       // 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 > 1 && !SL->getBeginLoc().isMacroID()) {
+      if (NumConcat > 1 && E > 2 && !SL->getBeginLoc().isMacroID()) {
         SmallVector<FixItHint, 1> Hints;
         for (unsigned i = 0; i < NumConcat - 1; ++i)
           Hints.push_back(FixItHint::CreateInsertion(
index 8f087e3..c93bbd4 100644 (file)
@@ -19,14 +19,16 @@ typedef __WCHAR_TYPE__ wchar_t;
 const wchar_t *missing_comma_wchar[] = {
     L"basic_filebuf",
     L"packaged_task" // expected-note{{place parentheses around the string literal to silence warning}}
-    L"promise"      // expected-warning{{suspicious concatenation of string literals in an array initialization; did you mean to separate the elements with a comma?}}
+    L"promise",      // expected-warning{{suspicious concatenation of string literals in an array initialization; did you mean to separate the elements with a comma?}}
+    L"shared_future"
 };
 
 #if __cplusplus >= 201103L
 const char *missing_comma_u8[] = {
     u8"basic_filebuf",
     u8"packaged_task" // expected-note{{place parentheses around the string literal to silence warning}}
-    u8"promise"      // expected-warning{{suspicious concatenation of string literals in an array initialization; did you mean to separate the elements with a comma?}}
+    u8"promise",      // expected-warning{{suspicious concatenation of string literals in an array initialization; did you mean to separate the elements with a comma?}}
+    u8"shared_future"
 };
 #endif
 
@@ -47,10 +49,11 @@ const char *missing_comma_different_lines[] = {"basic_filebuf", "basic_ios" // e
 const char *missing_comma_same_line_all_literals[] = {"basic_filebuf", "future" "optional", "packaged_task"}; // 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?}}
 
-char missing_comma_inner[][4] = {
+char missing_comma_inner[][5] = {
     "a",
-    "b" // expected-note{{place parentheses around the string literal to silence warning}}
-    "c" // expected-warning{{suspicious concatenation of string literals in an array initialization; did you mean to separate the elements with a comma?}}
+    "b",
+    "c" // expected-note{{place parentheses around the string literal to silence warning}}
+    "d" // expected-warning{{suspicious concatenation of string literals in an array initialization; did you mean to separate the elements with a comma?}}
 };
 
 
@@ -89,6 +92,18 @@ const char *macro_test4[] = {"basic_filebuf",
 #define SUPPRESS(x) x
 const char *macro_test5[] = { SUPPRESS("foo" "bar"), "baz" };
 
+typedef struct {
+    int i;
+    const char s[11];
+} S;
+
+S s = {1, "hello" "world"};
+
+const char *not_warn[] = {
+    "hello"
+    "world", "test"
+};
+
 // Do not warn when all the elements in the initializer are concatenated together.
 const char *all_elems_in_init_concatenated[] = {"a" "b" "c"};