public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
* [committed] preprocessor: Enable digit separators for C2X
@ 2021-05-11 14:27 Joseph Myers
  0 siblings, 0 replies; only message in thread
From: Joseph Myers @ 2021-05-11 14:27 UTC (permalink / raw)
  To: gcc-patches

C2X adds digit separators, as in C++.  Enable them accordingly in
libcpp and c-lex.c.  Some basic tests are added that digit separators
behave as expected for C2X and are properly disabled for C11; further
test coverage is included in the existing g++.dg/cpp1y/digit-sep*.C
tests.

Bootstrapped with no regressions for x86_64-pc-linux-gnu.  Applied to 
mainline.

gcc/c-family/
	* c-lex.c (interpret_float): Handle digit separators for C2X.

libcpp/
	* init.c (lang_defaults): Enable digit separators for GNUC2X and
	STDC2X.

gcc/testsuite/
	* gcc.dg/c11-digit-separators-1.c,
	gcc.dg/c2x-digit-separators-1.c, gcc.dg/c2x-digit-separators-2.c:
	New tests.

diff --git a/gcc/c-family/c-lex.c b/gcc/c-family/c-lex.c
index 6374b72ed2d..1c66ecd8fc4 100644
--- a/gcc/c-family/c-lex.c
+++ b/gcc/c-family/c-lex.c
@@ -1001,7 +1001,7 @@ interpret_float (const cpp_token *token, unsigned int flags,
     }
 
   copy = (char *) alloca (copylen + 1);
-  if (cxx_dialect > cxx11)
+  if (c_dialect_cxx () ? cxx_dialect > cxx11 : flag_isoc2x)
     {
       size_t maxlen = 0;
       for (size_t i = 0; i < copylen; ++i)
diff --git a/gcc/testsuite/gcc.dg/c11-digit-separators-1.c b/gcc/testsuite/gcc.dg/c11-digit-separators-1.c
new file mode 100644
index 00000000000..fc832260acb
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/c11-digit-separators-1.c
@@ -0,0 +1,7 @@
+/* Test C2x digit separators not in C11.  */
+/* { dg-do compile } */
+/* { dg-options "-std=c11 -pedantic-errors" } */
+
+#define m(x) 0
+
+_Static_assert (m(1'2)+(3'4) == 0, "digit separators");
diff --git a/gcc/testsuite/gcc.dg/c2x-digit-separators-1.c b/gcc/testsuite/gcc.dg/c2x-digit-separators-1.c
new file mode 100644
index 00000000000..6eadf2ea87f
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/c2x-digit-separators-1.c
@@ -0,0 +1,39 @@
+/* Test C2x digit separators.  Valid usages.  */
+/* { dg-do run } */
+/* { dg-options "-std=c2x -pedantic-errors" } */
+
+_Static_assert (123'45'6 == 123456);
+_Static_assert (0'123 == 0123);
+_Static_assert (0x1'23 == 0x123);
+
+#define m(x) 0
+
+_Static_assert (m(1'2)+(3'4) == 34);
+
+_Static_assert (0x0'e-0xe == 0);
+
+#define a0 '.' -
+#define acat(x) a ## x
+_Static_assert (acat (0'.') == 0);
+
+#define c0(x) 0
+#define b0 c0 (
+#define bcat(x) b ## x
+_Static_assert (bcat (0'\u00c0')) == 0);
+
+extern void exit (int);
+extern void abort (void);
+
+int
+main (void)
+{
+  if (314'159e-0'5f != 3.14159f)
+    abort ();
+  exit (0);
+}
+
+#line 0'123
+_Static_assert (__LINE__ == 123);
+
+#line 4'56'7'8'9
+_Static_assert (__LINE__ == 456789);
diff --git a/gcc/testsuite/gcc.dg/c2x-digit-separators-2.c b/gcc/testsuite/gcc.dg/c2x-digit-separators-2.c
new file mode 100644
index 00000000000..d72f8adc6cb
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/c2x-digit-separators-2.c
@@ -0,0 +1,25 @@
+/* Test C2x digit separators.  Invalid usages.  */
+/* { dg-do compile } */
+/* { dg-options "-std=c2x -pedantic-errors" } */
+
+void
+tf (void)
+{
+  int i;
+  i = 1''2; /* { dg-error "adjacent digit separators" } */
+  i = 0x'0; /* { dg-error "digit separator after base indicator" } */
+  i = 0X'1; /* { dg-error "digit separator after base indicator" } */
+  i = 0b'0; /* { dg-error "digit separator after base indicator" } */
+  i = 0B'1; /* { dg-error "digit separator after base indicator" } */
+  i = 1'u; /* { dg-error "digit separator outside digit sequence" } */
+  float f = 1.2e-3'f; /* { dg-error "digit separator outside digit sequence" } */
+  i = 1'2'3'; /* { dg-error "12:missing terminating" } */
+  ;
+  double d;
+  d = 1'.2'3e-4; /* { dg-warning "multi-character" } */
+  /* { dg-error "expected" "parse error" { target *-*-* } .-1 } */
+  d = 1.2''3; /* { dg-error "adjacent digit separators" } */
+  d = 1.23e-4''5; /* { dg-error "adjacent digit separators" } */
+  d = 1.2'3e-4'5'; /* { dg-error "17:missing terminating" } */
+  /* { dg-error "expected" "parse error" { target *-*-* } .-1 } */
+}
diff --git a/libcpp/init.c b/libcpp/init.c
index 68ed2c761b9..18a2341c2d0 100644
--- a/libcpp/init.c
+++ b/libcpp/init.c
@@ -103,13 +103,13 @@ static const struct lang_flags lang_defaults[] =
   /* GNUC99   */  { 1,  0,  1,  1,  0,  0,  1,   1,   1,   0,    0,     0,     0,   0,      1,   1,     0,   0 },
   /* GNUC11   */  { 1,  0,  1,  1,  1,  0,  1,   1,   1,   0,    0,     0,     0,   0,      1,   1,     0,   0 },
   /* GNUC17   */  { 1,  0,  1,  1,  1,  0,  1,   1,   1,   0,    0,     0,     0,   0,      1,   1,     0,   0 },
-  /* GNUC2X   */  { 1,  0,  1,  1,  1,  0,  1,   1,   1,   0,    1,     0,     0,   1,      1,   1,     1,   0 },
+  /* GNUC2X   */  { 1,  0,  1,  1,  1,  0,  1,   1,   1,   0,    1,     1,     0,   1,      1,   1,     1,   0 },
   /* STDC89   */  { 0,  0,  0,  0,  0,  1,  0,   0,   0,   0,    0,     0,     1,   0,      0,   0,     0,   0 },
   /* STDC94   */  { 0,  0,  0,  0,  0,  1,  1,   0,   0,   0,    0,     0,     1,   0,      0,   0,     0,   0 },
   /* STDC99   */  { 1,  0,  1,  1,  0,  1,  1,   0,   0,   0,    0,     0,     1,   0,      0,   0,     0,   0 },
   /* STDC11   */  { 1,  0,  1,  1,  1,  1,  1,   1,   0,   0,    0,     0,     1,   0,      0,   0,     0,   0 },
   /* STDC17   */  { 1,  0,  1,  1,  1,  1,  1,   1,   0,   0,    0,     0,     1,   0,      0,   0,     0,   0 },
-  /* STDC2X   */  { 1,  0,  1,  1,  1,  1,  1,   1,   0,   0,    1,     0,     1,   1,      0,   1,     1,   0 },
+  /* STDC2X   */  { 1,  0,  1,  1,  1,  1,  1,   1,   0,   0,    1,     1,     1,   1,      0,   1,     1,   0 },
   /* GNUCXX   */  { 0,  1,  1,  1,  0,  0,  1,   0,   0,   0,    0,     0,     0,   0,      1,   1,     0,   0 },
   /* CXX98    */  { 0,  1,  0,  1,  0,  1,  1,   0,   0,   0,    0,     0,     1,   0,      0,   1,     0,   0 },
   /* GNUCXX11 */  { 1,  1,  1,  1,  1,  0,  1,   1,   1,   1,    0,     0,     0,   0,      1,   1,     0,   0 },

-- 
Joseph S. Myers
joseph@codesourcery.com

^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2021-05-11 14:27 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-05-11 14:27 [committed] preprocessor: Enable digit separators for C2X Joseph Myers

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).