diff mbox

[5/5] New tests for -Wstring-plus-int

Message ID 1487761292.2882.68.camel@stu.xidian.edu.cn
State New
Headers show

Commit Message

Xi Ruoyao Feb. 22, 2017, 11:01 a.m. UTC
This patch adds tests for -Wstring-plus-int.

gcc/ChangeLog:

2017-02-22  Xi Ruoyao  <ryxi@stu.xidian.edu.cn>

	* testsuite/c-c++-common/Wstring-plus-int.c: New test
	* testsuite/g++.dg/Wstring-plus-int-1.C: New test
	* testsuite/g++.dg/Wstring-plus-int-2.C: New test
	* testsuite/gcc.dg/Wstring-plus-int.c: New test

---
 gcc/testsuite/c-c++-common/Wstring-plus-int.c | 35 +++++++++++++++++++++++++++
 gcc/testsuite/g++.dg/Wstring-plus-int-1.C     | 20 +++++++++++++++
 gcc/testsuite/g++.dg/Wstring-plus-int-2.C     | 32 ++++++++++++++++++++++++
 gcc/testsuite/gcc.dg/Wstring-plus-int.c       | 17 +++++++++++++
 4 files changed, 104 insertions(+)
 create mode 100644 gcc/testsuite/c-c++-common/Wstring-plus-int.c
 create mode 100644 gcc/testsuite/g++.dg/Wstring-plus-int-1.C
 create mode 100644 gcc/testsuite/g++.dg/Wstring-plus-int-2.C
 create mode 100644 gcc/testsuite/gcc.dg/Wstring-plus-int.c

Comments

Jeff Law April 28, 2017, 8:28 p.m. UTC | #1
On 02/22/2017 04:01 AM, Xi Ruoyao wrote:
> This patch adds tests for -Wstring-plus-int.
> 
> gcc/ChangeLog:
> 
> 2017-02-22  Xi Ruoyao  <ryxi@stu.xidian.edu.cn>
> 
> 	* testsuite/c-c++-common/Wstring-plus-int.c: New test
> 	* testsuite/g++.dg/Wstring-plus-int-1.C: New test
> 	* testsuite/g++.dg/Wstring-plus-int-2.C: New test
> 	* testsuite/gcc.dg/Wstring-plus-int.c: New test
This is OK for the trunk once all 5 are approved.
jeff
diff mbox

Patch

diff --git a/gcc/testsuite/c-c++-common/Wstring-plus-int.c b/gcc/testsuite/c-c++-common/Wstring-plus-int.c
new file mode 100644
index 0000000..f2a10a3
--- /dev/null
+++ b/gcc/testsuite/c-c++-common/Wstring-plus-int.c
@@ -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;
+}
diff --git a/gcc/testsuite/g++.dg/Wstring-plus-int-1.C b/gcc/testsuite/g++.dg/Wstring-plus-int-1.C
new file mode 100644
index 0000000..82631da
--- /dev/null
+++ b/gcc/testsuite/g++.dg/Wstring-plus-int-1.C
@@ -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;
+}
diff --git a/gcc/testsuite/g++.dg/Wstring-plus-int-2.C b/gcc/testsuite/g++.dg/Wstring-plus-int-2.C
new file mode 100644
index 0000000..48fdb4f
--- /dev/null
+++ b/gcc/testsuite/g++.dg/Wstring-plus-int-2.C
@@ -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;
+}
diff --git a/gcc/testsuite/gcc.dg/Wstring-plus-int.c b/gcc/testsuite/gcc.dg/Wstring-plus-int.c
new file mode 100644
index 0000000..bc029b8
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/Wstring-plus-int.c
@@ -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;
+}