[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:53:49 PDT 2020
xbolva00 updated this revision to Diff 284002.
xbolva00 added a comment.
Do not warn for macros - found false positives when compiling linux kernel.
CHANGES SINCE LAST ACTION
https://reviews.llvm.org/D85545/new/
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,19 @@
<< 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) {
+ bool hasMacro = false;
+ for (unsigned i = 0; i < numConcat; ++i)
+ if (SL->getStrTokenLoc(i).isMacroID()) {
+ hasMacro = true;
+ break;
+ }
+ if (!hasMacro)
+ 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
@@ -9,13 +9,7 @@
"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",
+ "shared_future"
};
const char *test2[] = {"basic_filebuf",
@@ -32,10 +26,30 @@
"future", "optional",
"packaged_task", "promise"};
+const char *test5[] = {"basic_filebuf", "future" "optional", "packaged_task"}; // expected-warning{{concatenated literal in a string array initialization - possibly missing a comma}}
+
+
+// Do not warn for macros.
+
#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}}
+const char *test6[] = {"basic_filebuf", BASIC_IOS
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}}
+#define FOO(xx) xx "_normal", \
+ xx "_movable",
+
+const char *test7[] = {"basic_filebuf",
+ "basic_ios"
+ FOO("future")
+ "optional",
+ "packaged_task"};
+
+#define BAR(name) #name "_normal"
+
+const char *test8[] = {"basic_filebuf",
+ "basic_ios"
+ BAR(future),
+ "optional",
+ "packaged_task"};
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D85545.284002.patch
Type: text/x-patch
Size: 3553 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20200807/b316ee1d/attachment-0001.bin>
More information about the cfe-commits
mailing list