From patchwork Wed Dec 7 19:19:24 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Joseph Myers X-Patchwork-Id: 61671 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 6FD2B390CEA3 for ; Wed, 7 Dec 2022 19:19:46 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from esa1.mentor.iphmx.com (esa1.mentor.iphmx.com [68.232.129.153]) by sourceware.org (Postfix) with ESMTPS id 68A853833A2B for ; Wed, 7 Dec 2022 19:19:29 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 68A853833A2B Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=codesourcery.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=mentor.com X-IronPort-AV: E=Sophos;i="5.96,225,1665475200"; d="scan'208";a="92017925" Received: from orw-gwy-01-in.mentorg.com ([192.94.38.165]) by esa1.mentor.iphmx.com with ESMTP; 07 Dec 2022 11:19:27 -0800 IronPort-SDR: yq20INAw1JmzoqgqDFfenajAjxLHx6j9abvE06cVgbrHUkLH7GX9LrmtgEEOhL3END2nBliu9p KpQkbKL1mS4ZyjpsckajKmeZY2e6mKJQ/2BfPKPTTHUdSr83qT11JZyPd0y+/zz69Ih3IcC3eD ZlgrUs37CFz34B5dqsZ8AfqrBDQcY0G24iP6QH2epCytXkxMLuyE4y8YylH9wrPWaqFK5+nW3e 0WGrp6gT5j/TLImqF8UfCQb/q8/1R0ol2coFZBW9w6uYry9ojjJjR4eW9i5hiPCCY+8Bm/Q4hn aLo= Date: Wed, 7 Dec 2022 19:19:24 +0000 From: Joseph Myers To: Subject: [committed] preprocessor: Enable __VA_OPT__ for C2x Message-ID: <8479e8c5-897e-b33e-ac6-f8f4e211fc29@codesourcery.com> MIME-Version: 1.0 X-Originating-IP: [137.202.0.90] X-ClientProxiedBy: svr-ies-mbx-15.mgc.mentorg.com (139.181.222.15) To svr-ies-mbx-10.mgc.mentorg.com (139.181.222.10) X-Spam-Status: No, score=-3114.6 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.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) 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+patchwork=sourceware.org@gcc.gnu.org Sender: "Gcc-patches" C2x supports __VA_OPT__, so adjust libcpp not to pedwarn for uses of it (or of not passing any variable arguments to a variable-arguments macro) in standard C2x mode. I didn't try to duplicate existing tests for the details of the feature, just verified -pedantic-errors handling is as expected. And there's a reasonable argument (bug 98859) that __VA_OPT__ shouldn't be diagnosed in older standard modes at all (as opposed to not passing any variable arguments to a variable-arguments macro, for which older versions of the C standard require a diagnostic as a constraint violation); that argument applies to C as much as to C++, but I haven't made any changes in that regard. Bootstrapped with no regressions for x86_64-pc-linux-gnu. libcpp/ * init.cc (lang_defaults): Enable va_opt for STDC2X. * lex.cc (maybe_va_opt_error): Adjust diagnostic message for C. * macro.cc (_cpp_arguments_ok): Update comment. gcc/testsuite/ * gcc.dg/cpp/c11-vararg-1.c, gcc.dg/cpp/c2x-va-opt-1.c: New tests. diff --git a/gcc/testsuite/gcc.dg/cpp/c11-vararg-1.c b/gcc/testsuite/gcc.dg/cpp/c11-vararg-1.c new file mode 100644 index 00000000000..6b1bc38bb2c --- /dev/null +++ b/gcc/testsuite/gcc.dg/cpp/c11-vararg-1.c @@ -0,0 +1,9 @@ +/* Test error in C11 for no arguments passed for variable arguments to a + macro. */ +/* { dg-do preprocess } */ +/* { dg-options "-std=c11 -pedantic-errors" } */ + +#define M(X, ...) X + +M (x); /* { dg-error "requires at least one argument" } */ +M (x, y); diff --git a/gcc/testsuite/gcc.dg/cpp/c2x-va-opt-1.c b/gcc/testsuite/gcc.dg/cpp/c2x-va-opt-1.c new file mode 100644 index 00000000000..bd438f74571 --- /dev/null +++ b/gcc/testsuite/gcc.dg/cpp/c2x-va-opt-1.c @@ -0,0 +1,11 @@ +/* Test __VA_OPT__ and no "..." arguments in a call to a variable-arguments + macro accepted for C2X. */ +/* { dg-do preprocess } */ +/* { dg-options "-std=c2x -pedantic-errors" } */ + +#define CALL(F, ...) F (7 __VA_OPT__(,) __VA_ARGS__) +#define M(X, ...) X + +CALL (a); +CALL (b, 1); +M (x); diff --git a/libcpp/init.cc b/libcpp/init.cc index 5f34e3515d2..ea683f0cfaf 100644 --- a/libcpp/init.cc +++ b/libcpp/init.cc @@ -114,7 +114,7 @@ static const struct lang_flags lang_defaults[] = /* STDC99 */ { 1, 0, 1, 1, 0, 0, 1, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, /* STDC11 */ { 1, 0, 1, 1, 1, 0, 1, 1, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, /* STDC17 */ { 1, 0, 1, 1, 1, 0, 1, 1, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, - /* STDC2X */ { 1, 0, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 0, 1, 0, 1, 1, 0, 1, 1, 0, 1 }, + /* STDC2X */ { 1, 0, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 0, 1, 1, 1, 1, 0, 1, 1, 0, 1 }, /* GNUCXX */ { 0, 1, 1, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 1 }, /* CXX98 */ { 0, 1, 0, 1, 0, 1, 1, 1, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 1 }, /* GNUCXX11 */ { 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 1 }, diff --git a/libcpp/lex.cc b/libcpp/lex.cc index b1107920c94..9a21a3e9ecc 100644 --- a/libcpp/lex.cc +++ b/libcpp/lex.cc @@ -2135,8 +2135,14 @@ maybe_va_opt_error (cpp_reader *pfile) /* __VA_OPT__ should not be accepted at all, but allow it in system headers. */ if (!_cpp_in_system_header (pfile)) - cpp_error (pfile, CPP_DL_PEDWARN, - "__VA_OPT__ is not available until C++20"); + { + if (CPP_OPTION (pfile, cplusplus)) + cpp_error (pfile, CPP_DL_PEDWARN, + "__VA_OPT__ is not available until C++20"); + else + cpp_error (pfile, CPP_DL_PEDWARN, + "__VA_OPT__ is not available until C2X"); + } } else if (!pfile->state.va_args_ok) { diff --git a/libcpp/macro.cc b/libcpp/macro.cc index 7d5a0d0fd2e..452e14a1e66 100644 --- a/libcpp/macro.cc +++ b/libcpp/macro.cc @@ -1093,7 +1093,7 @@ _cpp_arguments_ok (cpp_reader *pfile, cpp_macro *macro, const cpp_hashnode *node if (argc < macro->paramc) { - /* In C++20 (here the va_opt flag is used), and also as a GNU + /* In C++20 and C2X (here the va_opt flag is used), and also as a GNU extension, variadic arguments are allowed to not appear in the invocation at all. e.g. #define debug(format, args...) something