From 5ea40269a77a3754dd0f610f7c09b1a372e3c7f7 Mon Sep 17 00:00:00 2001 From: Joseph Myers Date: Tue, 11 May 2021 14:25:55 +0000 Subject: [PATCH] preprocessor: Enable digit separators for C2X 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. 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. --- gcc/c-family/c-lex.c | 2 +- gcc/testsuite/gcc.dg/c11-digit-separators-1.c | 7 +++++ gcc/testsuite/gcc.dg/c2x-digit-separators-1.c | 39 +++++++++++++++++++++++++++ gcc/testsuite/gcc.dg/c2x-digit-separators-2.c | 25 +++++++++++++++++ libcpp/init.c | 4 +-- 5 files changed, 74 insertions(+), 3 deletions(-) create mode 100644 gcc/testsuite/gcc.dg/c11-digit-separators-1.c create mode 100644 gcc/testsuite/gcc.dg/c2x-digit-separators-1.c create mode 100644 gcc/testsuite/gcc.dg/c2x-digit-separators-2.c diff --git a/gcc/c-family/c-lex.c b/gcc/c-family/c-lex.c index 6374b72..1c66ecd 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 0000000..fc83226 --- /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 0000000..6eadf2e --- /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 0000000..d72f8ad --- /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 68ed2c7..18a2341 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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