diff mbox

[4/6] New warnings -Wstring-plus-{char, int} (PR c++/62181)

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

Commit Message

Xi Ruoyao June 12, 2017, 1:36 a.m. UTC
This patch adds tests for -Wstring-plus-int.

gcc/ChangeLog:

2017-06-12  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: Ditto.
	* testsuite/g++.dg/Wstring-plus-int-2.C: Ditto.
---
 gcc/testsuite/c-c++-common/Wstring-plus-int.c | 26 ++++++++++++++++++++++++++
 gcc/testsuite/g++.dg/Wstring-plus-int-1.C     |  9 +++++++++
 gcc/testsuite/g++.dg/Wstring-plus-int-2.C     | 10 ++++++++++
 3 files changed, 45 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
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..6172bd0
--- /dev/null
+++ b/gcc/testsuite/c-c++-common/Wstring-plus-int.c
@@ -0,0 +1,26 @@ 
+/* Test -Wstring-plus-int.  */
+
+/* { dg-do compile } */
+/* { dg-options "-Wstring-plus-int" } */
+
+extern int getchar();
+extern int offset;
+
+int main(void)
+{
+  const char *a = "aa" + 'a'; /* { dg-warning "does not append" } */
+  const char *b = "aa" + getchar(); /* { dg-warning "does not append" } */
+  const char *c = "aa" + 4; /* { dg-warning "does not append" } */
+  const char *d = "aa" + -1; /* { dg-warning "does not append" } */
+  const char *e = 'x' + "aa"; /* { dg-warning "does not append" } */
+  const char *f = "aa" + offset; /* { dg-warning "does not append" } */
+
+  /* This is legal (at least Clang think it is).  */
+  const char *g = "aa" + 3; /* { dg-bogus "does not append" } */
+
+  /* Although they are strange, still shouldn't
+     be warned by this warning.  Maybe -Warray-bounds.  */
+  const char (*h)[3] = &"aa" + 1; /* { dg-bogus "does not append" } */
+  char i = "aa"[4]; /* { dg-bogus "does not append" } */
+  const char *j = "aa" - 1; /* { dg-bogus "does not append" } */
+}
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..fc74428
--- /dev/null
+++ b/gcc/testsuite/g++.dg/Wstring-plus-int-1.C
@@ -0,0 +1,9 @@ 
+/* Test -Wstring-plus-int for C++ wide char types.  */
+
+/* { dg-do compile } */
+/* { dg-options "-Wstring-plus-int" } */
+
+int main(void)
+{
+  const wchar_t *a = L"aa" + L'a'; /* { dg-warning "does not append" } */
+}
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..b69da41
--- /dev/null
+++ b/gcc/testsuite/g++.dg/Wstring-plus-int-2.C
@@ -0,0 +1,10 @@ 
+/* Test -Wstring-plus-int for C++ 2011 unicode char types.  */
+
+/* { dg-do compile } */
+/* { dg-options "-std=c++11 -Wstring-plus-int" } */
+
+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" } */
+}
-- 
2.7.1