From patchwork Tue May 11 14:27:19 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Joseph Myers X-Patchwork-Id: 1477163 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=2620:52:3:1:0:246e:9693:128c; helo=sourceware.org; envelope-from=gcc-patches-bounces@gcc.gnu.org; receiver=) Received: from sourceware.org (server2.sourceware.org [IPv6:2620:52:3:1:0:246e:9693:128c]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 4FfgHT37ltz9sPf for ; Wed, 12 May 2021 00:27:32 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id C658E388F020; Tue, 11 May 2021 14:27:30 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from esa3.mentor.iphmx.com (esa3.mentor.iphmx.com [68.232.137.180]) by sourceware.org (Postfix) with ESMTPS id AF2E6385E004 for ; Tue, 11 May 2021 14:27:26 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org AF2E6385E004 Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=codesourcery.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=joseph_myers@mentor.com IronPort-SDR: v3FOTh1BJaiE9wkhOaa8KUMKKQn9UmcZfcNb8fLuITuoikirwgnwoez4tqPm8e7rqNnKfxOFLw BJUpGECsM8qXEZDFWX6BDvKCDtGXWH3qXRvjRCzDLaNUuKHLk4jniYtN3hqOx8oyIOFECTt1s3 1iYGvR7WAaSl5aHp4bS+0W5RjrJB5IQf0qeeG87GWIqaTBuA6Gk3e+WZm86fBAMZntB64BcyAU HAv1L9r9i/hDjtJCxrVPgm2IdI07n9TJhtqb31i04gkEv4mSjack5H1jyUlo2gjvuZtnns//Ip jfg= X-IronPort-AV: E=Sophos;i="5.82,290,1613462400"; d="scan'208";a="61077602" Received: from orw-gwy-01-in.mentorg.com ([192.94.38.165]) by esa3.mentor.iphmx.com with ESMTP; 11 May 2021 06:27:25 -0800 IronPort-SDR: aS5UL8mx9+SFCUROrag2ZIXLUl9FCiBgqQz+AAIpHv5JywMst8prGQi8RA+WA5HJTylbYF9EL7 UhYf/Csp+WwAO+CgkttxopVzKDUHaszCJ1WVzycfQGdOAut1OH01HcJyIxSLF41sHdz4W9IJ4L PzigWDMmcfUksQIQyoQ+1JjQDeLURkUtuJY0U+efG2lG6n0asV1aMxTjZpWavPgQqzeJV5MBia qzxqz2w3adTeoXewI5HwrVq6uWizgm9fbukWK1X7Ps2zydeUHtPcI8lHYObHZjRM4Qv7wpxed1 yRU= Date: Tue, 11 May 2021 14:27:19 +0000 From: Joseph Myers X-X-Sender: jsm28@digraph.polyomino.org.uk To: Subject: [committed] preprocessor: Enable digit separators for C2X Message-ID: User-Agent: Alpine 2.22 (DEB 394 2020-01-19) MIME-Version: 1.0 X-Originating-IP: [137.202.0.90] X-ClientProxiedBy: SVR-IES-MBX-04.mgc.mentorg.com (139.181.222.4) To svr-ies-mbx-01.mgc.mentorg.com (139.181.222.1) X-Spam-Status: No, score=-3126.4 required=5.0 tests=BAYES_00, GIT_PATCH_0, HEADER_FROM_DIFFERENT_DOMAINS, KAM_DMARC_STATUS, SCC_10_SHORT_WORD_LINES, SCC_5_SHORT_WORD_LINES, SPF_HELO_PASS, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.2 X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on server2.sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gcc-patches-bounces@gcc.gnu.org Sender: "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 },