[PATCH] D85545: [Diagnostics] Diagnose missing comma in string array initialization
Dávid Bolvanský via Phabricator via cfe-commits
cfe-commits at lists.llvm.org
Fri Aug 7 12:17:56 PDT 2020
xbolva00 created this revision.
xbolva00 added a reviewer: aaron.ballman.
Herald added a project: clang.
Herald added a subscriber: cfe-commits.
xbolva00 requested review of this revision.
Motivation (from PR37674):
const char *ss[] = {
"foo", "bar",
"baz", "qux" // <-- Missing comma!
"abc", "xyz"
};
This kind of bug was recently also found in LLVM codebase (see PR47038).
Solves PR47038, PR37674
Repository:
rG LLVM Github Monorepo
https://reviews.llvm.org/D85545
Files:
clang/include/clang/Basic/DiagnosticSemaKinds.td
clang/lib/Sema/SemaExpr.cpp
clang/test/Sema/string-concat.c
Index: clang/lib/Sema/SemaExpr.cpp
===================================================================
--- clang/lib/Sema/SemaExpr.cpp
+++ clang/lib/Sema/SemaExpr.cpp
@@ -6907,6 +6907,11 @@
<< DIE->getSourceRange();
Diag(InitArgList[I]->getBeginLoc(), diag::note_designated_init_mixed)
<< InitArgList[I]->getSourceRange();
+ } else if (StringLiteral *SL = dyn_cast<StringLiteral>(InitArgList[I])) {
+ unsigned numConcat = SL->getNumConcatenated();
+ // Diagnose missing comma in string array initialization.
+ if (numConcat > 1)
+ Diag(SL->getBeginLoc(), diag::warn_concatenated_literal_array_init);
}
}
Index: clang/include/clang/Basic/DiagnosticSemaKinds.td
===================================================================
--- clang/include/clang/Basic/DiagnosticSemaKinds.td
+++ clang/include/clang/Basic/DiagnosticSemaKinds.td
@@ -2999,6 +2999,10 @@
def warn_objc_string_literal_comparison : Warning<
"direct comparison of a string literal has undefined behavior">,
InGroup<ObjCStringComparison>;
+def warn_concatenated_literal_array_init : Warning<
+ "concatenated literal in a string array initialization - "
+ "possibly missing a comma">,
+ InGroup<DiagGroup<"string-concatenation">>;
def warn_concatenated_nsarray_literal : Warning<
"concatenated NSString literal for an NSArray expression - "
"possibly missing a comma">,
Index: clang/test/Sema/string-concat.c
===================================================================
--- /dev/null
+++ clang/test/Sema/string-concat.c
@@ -0,0 +1,41 @@
+
+// RUN: %clang_cc1 -x c -fsyntax-only -verify %s
+// RUN: %clang_cc1 -x c++ -fsyntax-only -verify %s
+
+const char *test1[] = {
+ "basic_filebuf",
+ "basic_ios",
+ "future",
+ "optional",
+ "packaged_task" // expected-warning{{concatenated literal in a string array initialization - possibly missing a comma}}
+ "promise",
+ "shared_future",
+ "shared_lock",
+ "shared_ptr",
+ "thread",
+ "unique_ptr",
+ "unique_lock",
+ "weak_ptr",
+};
+
+const char *test2[] = {"basic_filebuf",
+ "basic_ios" // expected-warning{{concatenated literal in a string array initialization - possibly missing a comma}}
+ "future"
+ "optional",
+ "packaged_task"};
+
+const char *test3[] = {"basic_filebuf", "basic_ios",
+ "future" "optional", // expected-warning{{concatenated literal in a string array initialization - possibly missing a comma}}
+ "packaged_task", "promise"};
+
+const char *test4[] = {"basic_filebuf", "basic_ios" // expected-warning{{concatenated literal in a string array initialization - possibly missing a comma}}
+ "future", "optional",
+ "packaged_task", "promise"};
+
+#define BASIC_IOS "basic_ios"
+#define FUTURE "future"
+const char *test5[] = {"basic_filebuf", BASIC_IOS // expected-warning{{concatenated literal in a string array initialization - possibly missing a comma}}
+ FUTURE, "optional",
+ "packaged_task", "promise"};
+
+const char *test6[] = {"basic_filebuf", "future" "optional", "packaged_task"}; // expected-warning{{concatenated literal in a string array initialization - possibly missing a comma}}
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D85545.283981.patch
Type: text/x-patch
Size: 3372 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20200807/76b9065f/attachment.bin>
More information about the cfe-commits
mailing list