[clang] 4b59dc7 - [Diagnostics] Ignore structs and long text for -Wstring-concatenation
Dávid Bolvanský via cfe-commits
cfe-commits at lists.llvm.org
Mon Aug 10 15:50:13 PDT 2020
Author: Dávid Bolvanský
Date: 2020-08-11T00:49:58+02:00
New Revision: 4b59dc77dc473bba849c1b08f3a1ab7be5733ad1
URL: https://github.com/llvm/llvm-project/commit/4b59dc77dc473bba849c1b08f3a1ab7be5733ad1
DIFF: https://github.com/llvm/llvm-project/commit/4b59dc77dc473bba849c1b08f3a1ab7be5733ad1.diff
LOG: [Diagnostics] Ignore structs and long text for -Wstring-concatenation
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 28ac1dfeb082..0d7f81e0d01a 100644
--- a/clang/lib/Sema/SemaExpr.cpp
+++ b/clang/lib/Sema/SemaExpr.cpp
@@ -6865,6 +6865,7 @@ Sema::ActOnInitList(SourceLocation LBraceLoc, MultiExprArg InitArgList,
bool DiagnosedArrayDesignator = false;
bool DiagnosedNestedDesignator = false;
bool DiagnosedMixedDesignator = false;
+ bool DiagnosedMissingComma = false;
// Check that any designated initializers are syntactically valid in the
// current language mode.
@@ -6908,22 +6909,34 @@ 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 *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() &&
- 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(
- PP.getLocForEndOfToken(SL->getStrTokenLoc(i)), ","));
-
- Diag(SL->getStrTokenLoc(1), diag::warn_concatenated_literal_array_init)
- << Hints;
- Diag(SL->getBeginLoc(), diag::note_concatenated_string_literal_silence);
+ if (!DiagnosedMissingComma && NumConcat == 2 && E > 2 && !SL->getBeginLoc().isMacroID()) {
+ bool OnlyOneMissingComma = true;
+ for (unsigned J = 0; J < E; ++J) {
+ if (J == I)
+ continue;
+ const auto *SLJ = dyn_cast<StringLiteral>(InitArgList[J]);
+ if (!SLJ || SLJ->getNumConcatenated() > 1) {
+ OnlyOneMissingComma = false;
+ break;
+ }
+ }
+
+ if (OnlyOneMissingComma) {
+ SmallVector<FixItHint, 1> Hints;
+ for (unsigned i = 0; i < NumConcat - 1; ++i)
+ Hints.push_back(FixItHint::CreateInsertion(
+ PP.getLocForEndOfToken(SL->getStrTokenLoc(i)), ","));
+
+ Diag(SL->getStrTokenLoc(1),
+ diag::warn_concatenated_literal_array_init)
+ << Hints;
+ Diag(SL->getBeginLoc(),
+ diag::note_concatenated_string_literal_silence);
+ DiagnosedMissingComma = true;
+ }
}
}
}
diff --git a/clang/test/Sema/string-concat.c b/clang/test/Sema/string-concat.c
index 4e5ed4424e7c..3dcde8844dff 100644
--- a/clang/test/Sema/string-concat.c
+++ b/clang/test/Sema/string-concat.c
@@ -32,12 +32,6 @@ const char *missing_comma_u8[] = {
};
#endif
-const char *missing_two_commas[] = {"basic_filebuf",
- "basic_ios" // expected-note{{place parentheses around the string literal to silence warning}}
- "future" // expected-warning{{suspicious concatenation of string literals in an array initialization; did you mean to separate the elements with a comma?}}
- "optional",
- "packaged_task"};
-
const char *missing_comma_same_line[] = {"basic_filebuf", "basic_ios",
"future" "optional", // expected-note{{place parentheses around the string literal to silence warning}}
"packaged_task", "promise"}; // expected-warning at -1{{suspicious concatenation of string literals in an array initialization; did you mean to separate the elements with a comma?}}
@@ -56,12 +50,20 @@ char missing_comma_inner[][5] = {
"d" // expected-warning{{suspicious concatenation of string literals in an array initialization; did you mean to separate the elements with a comma?}}
};
+const char *warn[] = { "cpll", "gpll", "hdmiphy" "usb480m" }; // 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?}}
+
+const char *missing_two_commas_ignore[] = {"basic_filebuf",
+ "basic_ios"
+ "future"
+ "optional",
+ "packaged_task"};
#define ONE(x) x
#define TWO "foo"
-const char *macro_test[] = { ONE("foo") "bar",
- TWO "bar",
- "foo" "bar" TWO // expected-note{{place parentheses around the string literal to silence warning}}
+const char *macro_test[] = { ONE("foo"),
+ TWO,
+ "foo" TWO // 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?}}
// Do not warn for macros.
@@ -110,6 +112,24 @@ const char *not_warn2[] = {
"// Aaa\\\r" " Bbb\\ \r" " Ccc?" "?/\r"
};
+const char *not_warn3[] = {
+ "// \\tparam aaa Bbb\n",
+ "// \\tparam\n"
+ "// aaa Bbb\n",
+ "// \\tparam \n"
+ "// aaa Bbb\n",
+ "// \\tparam aaa\n"
+ "// Bbb\n"
+};
+
+const char *not_warn4[] = {"title",
+ "aaaa "
+ "bbbb "
+ "cccc "
+ "ddd.",
+ "url"
+};
+
// 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