new file mode 100644
@@ -0,0 +1,35 @@
+/* Test -Wstring-plus-int. */
+
+/* { dg-do compile } */
+/* { dg-options "-Wstring-plus-int" } */
+
+/* From stdint.h. */
+typedef __UINT_LEAST32_TYPE__ uint_least32_t;
+
+/* From wchar.h. */
+#ifndef __cplusplus
+typedef __WCHAR_TYPE__ wchar_t;
+#endif
+
+int getchar(void);
+
+int
+main (void)
+{
+ /* Following code should be warn in both C/C++, for any standards. */
+ const char *a = "aa" + 'a'; /* { dg-warning "does not append" } */
+ const wchar_t *b = L"aa" + L'a'; /* { dg-warning "does not append" } */
+ const char *e = "aa" + getchar(); /* { dg-warning "does not append" } */
+
+ a += 'a'; /* { dg-warning "does not append" } */
+ const char *f = a + 'a'; /* { dg-warning "does not append" } */
+ const char *g = 'a' + a; /* { dg-warning "does not append" } */
+
+ /* Following code should never be warned. */
+ char x = a['x']; /* { dg-bogus "does not append" } */
+ const char *k = a + 1; /* { dg-bogus "does not append" } */
+ a += 1; /* { dg-bogus "does not append" } */
+ a += (uint_least32_t) 1; /* { dg-bogus "does not append" } */
+
+ return 0;
+}
new file mode 100644
@@ -0,0 +1,20 @@
+/* Test -Wstring-plus-int, for C++11 built-in character types. */
+
+/* { dg-do compile } */
+/* { dg-options "-std=c++11 -Wstring-plus-int" } */
+
+int
+main(void)
+{
+ const wchar_t *a = L"aa";
+ const char16_t *b = u"aa" + u'a'; /* { dg-warning "does not append" } */
+ const char32_t *c = U"aa" + U'a'; /* { dg-warning "does not append" } */
+
+ /* In C++11, wchar_t, char16_t and char32_t are built-in.
+ There should be warnings. */
+ const wchar_t *h = a + L'a'; /* { dg-warning "does not append" } */
+ const char16_t *i = b + u'a'; /* { dg-warning "does not append" } */
+ const char32_t *j = c + U'a'; /* { dg-warning "does not append" } */
+
+ return 0;
+}
new file mode 100644
@@ -0,0 +1,32 @@
+/* Test -Wstring-plus-int.
+ This is a more realistic test case. */
+
+/* { dg-do compile } */
+/* { dg-options "-Wstring-plus-int" } */
+
+class good_string
+{
+public:
+ good_string(const char *);
+ good_string &operator=(const char *);
+ good_string operator+(char) const;
+ operator const char *(void) const;
+};
+
+/* Someone has forgotten operator+ overload... */
+class bad_string
+{
+public:
+ bad_string(const char *);
+ bad_string &operator=(const char *);
+ operator const char *(void) const;
+};
+
+int main()
+{
+ good_string good = "aa";
+ good = good + 'a'; /* { dg-bogus "does not append" } */
+ bad_string bad = "bb";
+ bad = bad + 'b'; /* { dg-warning "does not append" } */
+ return 0;
+}
new file mode 100644
@@ -0,0 +1,17 @@
+/* Test -Wstring-plus-int with C11 UTF literals. */
+
+/* { dg-do compile } */
+/* { dg-options "-std=c11 -Wstring-plus-int" } */
+
+/* From wchar.h and uchar.h. */
+typedef __CHAR16_TYPE__ char16_t;
+typedef __CHAR32_TYPE__ char32_t;
+
+int
+main (void)
+{
+ const char16_t *a = u"aa" + u'a'; /* { dg-warning "does not append" } */
+ const char32_t *b = U"aa" + U'a'; /* { dg-warning "does not append" } */
+
+ return 0;
+}