From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 2153) id 870943856255; Wed, 11 May 2022 06:23:44 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 870943856255 MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset="utf-8" From: Jakub Jelinek To: gcc-cvs@gcc.gnu.org Subject: [gcc r9-10116] libcpp: Fix up ##__VA_OPT__ handling [PR89971] X-Act-Checkin: gcc X-Git-Author: Jakub Jelinek X-Git-Refname: refs/heads/releases/gcc-9 X-Git-Oldrev: 5d96fb401e1c03b6b6a6ec2c5276dfdc479bb3e4 X-Git-Newrev: 86b98701bf927ea438354723932e623557c04e42 Message-Id: <20220511062344.870943856255@sourceware.org> Date: Wed, 11 May 2022 06:23:44 +0000 (GMT) X-BeenThere: gcc-cvs@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-cvs mailing list List-Unsubscribe: , List-Archive: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 11 May 2022 06:23:44 -0000 https://gcc.gnu.org/g:86b98701bf927ea438354723932e623557c04e42 commit r9-10116-g86b98701bf927ea438354723932e623557c04e42 Author: Jakub Jelinek Date: Thu Dec 30 22:23:58 2021 +0100 libcpp: Fix up ##__VA_OPT__ handling [PR89971] In the following testcase we incorrectly error about pasting / token with padding token (which is a result of __VA_OPT__); instead we should like e.g. for ##arg where arg is empty macro argument clear PASTE_LEFT flag of the previous token if __VA_OPT__ doesn't add any real tokens (which can happen either because the macro doesn't have any tokens passed to ... (i.e. __VA_ARGS__ expands to empty) or when __VA_OPT__ doesn't have any tokens in between ()s). 2021-12-30 Jakub Jelinek PR preprocessor/89971 libcpp/ * macro.c (replace_args): For ##__VA_OPT__, if __VA_OPT__ expands to no tokens at all, drop PASTE_LEFT flag from the previous token. gcc/testsuite/ * c-c++-common/cpp/va-opt-9.c: New test. (cherry picked from commit 5545d1edcbdb1701443f94dde7ec97c5ce3e1a6c) Diff: --- gcc/testsuite/c-c++-common/cpp/va-opt-9.c | 20 ++++++++++++++++++++ libcpp/macro.c | 5 +++++ 2 files changed, 25 insertions(+) diff --git a/gcc/testsuite/c-c++-common/cpp/va-opt-9.c b/gcc/testsuite/c-c++-common/cpp/va-opt-9.c new file mode 100644 index 00000000000..3f1d42b352f --- /dev/null +++ b/gcc/testsuite/c-c++-common/cpp/va-opt-9.c @@ -0,0 +1,20 @@ +/* PR preprocessor/89971 */ +/* { dg-do compile } */ +/* { dg-options "-std=gnu99" { target c } } */ +/* { dg-options "-std=c++2a" { target c++ } } */ + +int a, c; +#define m1(...) a /##__VA_OPT__(b) c +#define m2(...) a /##__VA_OPT__() c +#define m3(...) a##__VA_OPT__()##b = 1 +#define m4(...) a##__VA_OPT__(b c d)##e = 2 + +int +foo (void) +{ + int d = m1(); + int e = m2(1); + int m3(1 2 3); + int m4(); + return d + e + ab + ae; +} diff --git a/libcpp/macro.c b/libcpp/macro.c index 30d3686451c..8dcc21de10a 100644 --- a/libcpp/macro.c +++ b/libcpp/macro.c @@ -1886,6 +1886,11 @@ replace_args (cpp_reader *pfile, cpp_hashnode *node, cpp_macro *macro, paste_flag = tokens_buff_last_token_ptr (buff); } + if (start && paste_flag == start && (*start)->flags & PASTE_LEFT) + /* If __VA_OPT__ expands to nothing (either because __VA_ARGS__ + is empty or because it is __VA_OPT__() ), drop PASTE_LEFT + flag from previous token. */ + copy_paste_flag (pfile, start, &pfile->avoid_paste); if (src->flags & PASTE_LEFT) { /* With a non-empty __VA_OPT__ on the LHS of ##, the last