public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
From: Xi Ruoyao <ryxi@stu.xidian.edu.cn>
To: gcc-patches@gcc.gnu.org
Cc: ryxi@stu.xidian.edu.cn
Subject: [PATCH 5/5] New tests for -Wstring-plus-int
Date: Wed, 22 Feb 2017 11:01:00 -0000	[thread overview]
Message-ID: <1487761292.2882.68.camel@stu.xidian.edu.cn> (raw)

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

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;
+}

-- 
Xi Ruoyao <ryxi@stu.xidian.edu.cn>
School of Aerospace Science and Technology, Xidian University

             reply	other threads:[~2017-02-22 11:01 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-02-22 11:01 Xi Ruoyao [this message]
2017-04-28 20:41 ` Jeff Law

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1487761292.2882.68.camel@stu.xidian.edu.cn \
    --to=ryxi@stu.xidian.edu.cn \
    --cc=gcc-patches@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).