public inbox for gcc-cvs@sourceware.org
help / color / mirror / Atom feed
From: Jakub Jelinek <jakub@gcc.gnu.org>
To: gcc-cvs@gcc.gnu.org
Subject: [gcc r13-2221] libcpp: Implement P2362R3 - Remove non-encodable wide character literals and multicharacter [PR10664
Date: Fri, 26 Aug 2022 14:07:02 +0000 (GMT)	[thread overview]
Message-ID: <20220826140702.71F9C3858403@sourceware.org> (raw)

https://gcc.gnu.org/g:dad2d3e003f1a9885cb1fa0f67baf50f62d57b06

commit r13-2221-gdad2d3e003f1a9885cb1fa0f67baf50f62d57b06
Author: Jakub Jelinek <jakub@redhat.com>
Date:   Fri Aug 26 16:06:20 2022 +0200

    libcpp: Implement P2362R3 - Remove non-encodable wide character literals and multicharacter [PR106647]
    
    My understanding of the paper is that we just want to promote the CPP_WCHAR
    "character constant too long for its type" warning to error as it is already
    error for u8, u and U literals.
    
    2022-08-26  Jakub Jelinek  <jakub@redhat.com>
    
            PR c++/106647
            * charset.cc (wide_str_to_charconst): Implement P2362R3 - Remove
            non-encodable wide character literals and multicharacter.  For
            C++23 use CPP_DL_ERROR instead of CPP_DL_WARNING for
            "character constant too long for its type" diagnostics on CPP_WCHAR
            literals.
    
            * g++.dg/cpp23/wchar-multi1.C: New test.
            * g++.dg/cpp23/wchar-multi2.C: New test.

Diff:
---
 gcc/testsuite/g++.dg/cpp23/wchar-multi1.C | 42 ++++++++++++++++++++++++++++++
 gcc/testsuite/g++.dg/cpp23/wchar-multi2.C | 43 +++++++++++++++++++++++++++++++
 libcpp/charset.cc                         |  6 ++++-
 3 files changed, 90 insertions(+), 1 deletion(-)

diff --git a/gcc/testsuite/g++.dg/cpp23/wchar-multi1.C b/gcc/testsuite/g++.dg/cpp23/wchar-multi1.C
new file mode 100644
index 00000000000..77fa3a606dc
--- /dev/null
+++ b/gcc/testsuite/g++.dg/cpp23/wchar-multi1.C
@@ -0,0 +1,42 @@
+// P2362R3 - Remove non-encodable wide character literals and multicharacter
+// wide character literals.
+// { dg-do compile }
+
+char a = 'a';
+int b = 'ab';			// { dg-warning "multi-character character constant" }
+int c = '\u05D9';		// { dg-warning "multi-character character constant" }
+#if __SIZEOF_INT__ > 2
+int d = '\U0001F525';		// { dg-warning "multi-character character constant" "" { target int32 } }
+#endif
+int e = 'abcd';			// { dg-warning "multi-character character constant" }
+wchar_t f = L'f';
+wchar_t g = L'gh';		// { dg-error "character constant too long for its type" "" { target c++23 } }
+				// { dg-warning "character constant too long for its type" "" { target c++20_down } .-1 }
+wchar_t h = L'ijkl';		// { dg-error "character constant too long for its type" "" { target c++23 } }
+				// { dg-warning "character constant too long for its type" "" { target c++20_down } .-1 }
+wchar_t i = L'\U0001F525';	// { dg-error "character constant too long for its type" "" { target { c++23 && { ! 4byte_wchar_t } } } }
+				// { dg-warning "character constant too long for its type" "" { target { c++20_down && { ! 4byte_wchar_t } } } .-1 }
+#ifdef __cpp_char8_t
+typedef char8_t u8;
+#else
+typedef char u8;
+#endif
+#if __cpp_unicode_characters >= 201411 
+u8 j = u8'j';
+u8 k = u8'kl';			// { dg-error "character constant too long for its type" "" { target c++17 } }
+u8 l = u8'\U0001F525';		// { dg-error "character constant too long for its type" "" { target c++17 }  }
+#endif
+#if __cpp_unicode_characters >= 200704
+char16_t m = u'm';
+char16_t n = u'no';		// { dg-error "character constant too long for its type" "" { target c++11 } }
+char16_t o = u'\u05D9';
+char16_t p = u'\U0001F525';	// { dg-error "character constant too long for its type" "" { target c++11 } }
+char32_t q = U'm';
+char32_t r = U'no';		// { dg-error "character constant too long for its type" "" { target c++11 } }
+char32_t s = U'\u05D9';
+char32_t t = U'\U0001F525';
+#endif
+wchar_t u = L'\u0065\u0301';		// { dg-error "character constant too long for its type" "" { target c++23 } }
+				// { dg-warning "character constant too long for its type" "" { target c++20_down } .-1 }
+wchar_t v = L'é';		// { dg-error "character constant too long for its type" "" { target c++23 } }
+				// { dg-warning "character constant too long for its type" "" { target c++20_down } .-1 }
diff --git a/gcc/testsuite/g++.dg/cpp23/wchar-multi2.C b/gcc/testsuite/g++.dg/cpp23/wchar-multi2.C
new file mode 100644
index 00000000000..a63e0fffe60
--- /dev/null
+++ b/gcc/testsuite/g++.dg/cpp23/wchar-multi2.C
@@ -0,0 +1,43 @@
+// P2362R3 - Remove non-encodable wide character literals and multicharacter
+// wide character literals.
+// { dg-do compile }
+// { dg-options "-fshort-wchar" }
+
+char a = 'a';
+int b = 'ab';			// { dg-warning "multi-character character constant" }
+int c = '\u05D9';		// { dg-warning "multi-character character constant" }
+#if __SIZEOF_INT__ > 2
+int d = '\U0001F525';		// { dg-warning "multi-character character constant" "" { target int32 } }
+#endif
+int e = 'abcd';			// { dg-warning "multi-character character constant" }
+wchar_t f = L'f';
+wchar_t g = L'gh';		// { dg-error "character constant too long for its type" "" { target c++23 } }
+				// { dg-warning "character constant too long for its type" "" { target c++20_down } .-1 }
+wchar_t h = L'ijkl';		// { dg-error "character constant too long for its type" "" { target c++23 } }
+				// { dg-warning "character constant too long for its type" "" { target c++20_down } .-1 }
+wchar_t i = L'\U0001F525';	// { dg-error "character constant too long for its type" "" { target { c++23 } } }
+				// { dg-warning "character constant too long for its type" "" { target { c++20_down } } .-1 }
+#ifdef __cpp_char8_t
+typedef char8_t u8;
+#else
+typedef char u8;
+#endif
+#if __cpp_unicode_characters >= 201411 
+u8 j = u8'j';
+u8 k = u8'kl';			// { dg-error "character constant too long for its type" "" { target c++17 } }
+u8 l = u8'\U0001F525';		// { dg-error "character constant too long for its type" "" { target c++17 }  }
+#endif
+#if __cpp_unicode_characters >= 200704
+char16_t m = u'm';
+char16_t n = u'no';		// { dg-error "character constant too long for its type" "" { target c++11 } }
+char16_t o = u'\u05D9';
+char16_t p = u'\U0001F525';	// { dg-error "character constant too long for its type" "" { target c++11 } }
+char32_t q = U'm';
+char32_t r = U'no';		// { dg-error "character constant too long for its type" "" { target c++11 } }
+char32_t s = U'\u05D9';
+char32_t t = U'\U0001F525';
+#endif
+wchar_t u = L'\u0065\u0301';		// { dg-error "character constant too long for its type" "" { target c++23 } }
+				// { dg-warning "character constant too long for its type" "" { target c++20_down } .-1 }
+wchar_t v = L'é';		// { dg-error "character constant too long for its type" "" { target c++23 } }
+				// { dg-warning "character constant too long for its type" "" { target c++20_down } .-1 }
diff --git a/libcpp/charset.cc b/libcpp/charset.cc
index 47a36d8b01e..d3c07d6c82d 100644
--- a/libcpp/charset.cc
+++ b/libcpp/charset.cc
@@ -2593,7 +2593,11 @@ wide_str_to_charconst (cpp_reader *pfile, cpp_string str,
      character constant is guaranteed to overflow.  */
   if (str.len > nbwc * 2)
     cpp_error (pfile, (CPP_OPTION (pfile, cplusplus)
-		       && (type == CPP_CHAR16 || type == CPP_CHAR32))
+		       && (type == CPP_CHAR16
+			   || type == CPP_CHAR32
+			   /* In C++23 this is error even for L'ab'.  */
+			   || (type == CPP_WCHAR
+			       && CPP_OPTION (pfile, size_t_literals))))
 		      ? CPP_DL_ERROR : CPP_DL_WARNING,
 	       "character constant too long for its type");

                 reply	other threads:[~2022-08-26 14:07 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

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=20220826140702.71F9C3858403@sourceware.org \
    --to=jakub@gcc.gnu.org \
    --cc=gcc-cvs@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).