[clang] 62223ff - [Diagnostics] Avoid false positives with -Wstring-concatenation
Dávid Bolvanský via cfe-commits
cfe-commits at lists.llvm.org
Mon Aug 10 14:05:23 PDT 2020
Author: Dávid Bolvanský
Date: 2020-08-10T23:05:01+02:00
New Revision: 62223ff1376b540dc9612239fdfb11b376d796d3
URL: https://github.com/llvm/llvm-project/commit/62223ff1376b540dc9612239fdfb11b376d796d3
DIFF: https://github.com/llvm/llvm-project/commit/62223ff1376b540dc9612239fdfb11b376d796d3.diff
LOG: [Diagnostics] Avoid false positives with -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 23b2fbd5cbbf..8f38238401fc 100644
--- a/clang/lib/Sema/SemaExpr.cpp
+++ b/clang/lib/Sema/SemaExpr.cpp
@@ -6908,13 +6908,15 @@ 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]);
+ 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() && SLNext &&
- NumConcat != SLNext->getNumConcatenated()) {
+ if (NumConcat > 1 && E > 2 && !SL->getBeginLoc().isMacroID() &&
+ SL->getString().find(" ") == llvm::StringRef::npos &&
+ 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(
diff --git a/clang/test/Sema/string-concat.c b/clang/test/Sema/string-concat.c
index 13e9656d2536..09de0a9bffbe 100644
--- a/clang/test/Sema/string-concat.c
+++ b/clang/test/Sema/string-concat.c
@@ -108,7 +108,20 @@ 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"
- };
+};
+
+const char *not_warn3[] = {
+ "// \\param [in,out] aaa Bbb\n",
+ "// \\param[in,out] aaa Bbb\n",
+ "// \\param [in, out] aaa Bbb\n",
+ "// \\param [in,\n"
+ "// out] aaa Bbb\n",
+ "// \\param [in,out]\n"
+ "// aaa Bbb\n",
+ "// \\param [in,out] aaa\n"
+ "// Bbb\n"
+};
+
// 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