[clang] 04a23f1 - [Diagnostics] Turn string concat warning to avoid false positives

Dávid Bolvanský via cfe-commits cfe-commits at lists.llvm.org
Sun Aug 9 03:22:45 PDT 2020


Author: Dávid Bolvanský
Date: 2020-08-09T12:22:29+02:00
New Revision: 04a23f1fe08a6ad0baf1305d7308231d2cb4843b

URL: https://github.com/llvm/llvm-project/commit/04a23f1fe08a6ad0baf1305d7308231d2cb4843b
DIFF: https://github.com/llvm/llvm-project/commit/04a23f1fe08a6ad0baf1305d7308231d2cb4843b.diff

LOG: [Diagnostics] Turn string concat warning to avoid false positives

Added: 
    

Modified: 
    clang/lib/Sema/SemaExpr.cpp
    clang/test/Sema/string-concat.c

Removed: 
    


################################################################################
diff  --git a/clang/lib/Sema/SemaExpr.cpp b/clang/lib/Sema/SemaExpr.cpp
index 7560dc996b15..35047a7b2b14 100644
--- a/clang/lib/Sema/SemaExpr.cpp
+++ b/clang/lib/Sema/SemaExpr.cpp
@@ -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(

diff  --git a/clang/test/Sema/string-concat.c b/clang/test/Sema/string-concat.c
index 8f087e37d953..c93bbd4eaa00 100644
--- a/clang/test/Sema/string-concat.c
+++ b/clang/test/Sema/string-concat.c
@@ -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_
diff erent_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 at -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"};
 


        


More information about the cfe-commits mailing list