From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 2108) id EB5D5385843E; Mon, 21 Nov 2022 23:04:09 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org EB5D5385843E DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1669071849; bh=AkLfDcSCOqoGoH7L2A5oGhrTtj0wBOEVjuEHOxE5Epc=; h=From:To:Subject:Date:From; b=oVpNl9RBANJBvS4ttooqevYmjDDN8XyHI+Wn4OokYsE6/TaNcgPe6acRhyEAqorD/ CPwmf9rZOTXIN2BAhFnNNlnwowcdRCKQ3zzUTpOmmo7qedPdj5PypBUJv6Q9VYMnZa vKqaX1jQ2239xTwpdPhc3PWuO0/pmWBh4e0uoTCc= MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset="utf-8" From: Lewis Hyatt To: gcc-cvs@gcc.gnu.org Subject: [gcc r13-4217] libcpp: Fix paste error with unknown pragma after macro expansion X-Act-Checkin: gcc X-Git-Author: Lewis Hyatt X-Git-Refname: refs/heads/master X-Git-Oldrev: 5c0d171f67d082c353ddc319859111d3b9126c17 X-Git-Newrev: 6f46d14d4989b2711379807e4565585123c48118 Message-Id: <20221121230409.EB5D5385843E@sourceware.org> Date: Mon, 21 Nov 2022 23:04:09 +0000 (GMT) List-Id: https://gcc.gnu.org/g:6f46d14d4989b2711379807e4565585123c48118 commit r13-4217-g6f46d14d4989b2711379807e4565585123c48118 Author: Lewis Hyatt Date: Wed Nov 2 16:04:36 2022 -0400 libcpp: Fix paste error with unknown pragma after macro expansion In directives.cc, do_pragma() contains logic to handle a case such as the new testcase pragma-omp-unknown.c, where an unknown pragma was the result of macro expansion (for pragma namespaces that permit expansion). This no longer works correctly as shown by the testcase, fixed by adding PREV_WHITE to the flags on the second token to prevent an unwanted paste. Also fixed the memory leak, since the temporary tokens are pushed on their own context, nothing prevents freeing of the buffer that holds them when the context is eventually popped. libcpp/ChangeLog: * directives.cc (do_pragma): Fix memory leak in token buffer. Fix unwanted paste between two tokens. gcc/testsuite/ChangeLog: * c-c++-common/gomp/pragma-omp-unknown.c: New test. Diff: --- gcc/testsuite/c-c++-common/gomp/pragma-omp-unknown.c | 10 ++++++++++ libcpp/directives.cc | 10 +++++----- 2 files changed, 15 insertions(+), 5 deletions(-) diff --git a/gcc/testsuite/c-c++-common/gomp/pragma-omp-unknown.c b/gcc/testsuite/c-c++-common/gomp/pragma-omp-unknown.c new file mode 100644 index 00000000000..04881f786ab --- /dev/null +++ b/gcc/testsuite/c-c++-common/gomp/pragma-omp-unknown.c @@ -0,0 +1,10 @@ +/* { dg-do preprocess } */ +/* { dg-options "-fopenmp" } */ + +#define X UNKNOWN1 +#pragma omp X +/* { dg-final { scan-file pragma-omp-unknown.i "#pragma omp UNKNOWN1" } } */ + +#define Y UNKNOWN2 +_Pragma("omp Y") +/* { dg-final { scan-file pragma-omp-unknown.i "#pragma omp UNKNOWN2" } } */ diff --git a/libcpp/directives.cc b/libcpp/directives.cc index 918752f6b1f..9dc4363c65a 100644 --- a/libcpp/directives.cc +++ b/libcpp/directives.cc @@ -1565,15 +1565,15 @@ do_pragma (cpp_reader *pfile) { /* Invalid name comes from macro expansion, _cpp_backup_tokens won't allow backing 2 tokens. */ - /* ??? The token buffer is leaked. Perhaps if def_pragma hook - reads both tokens, we could perhaps free it, but if it doesn't, - we don't know the exact lifespan. */ - cpp_token *toks = XNEWVEC (cpp_token, 2); + const auto tok_buff = _cpp_get_buff (pfile, 2 * sizeof (cpp_token)); + const auto toks = (cpp_token *)tok_buff->base; toks[0] = ns_token; toks[0].flags |= NO_EXPAND; toks[1] = *token; - toks[1].flags |= NO_EXPAND; + toks[1].flags |= NO_EXPAND | PREV_WHITE; _cpp_push_token_context (pfile, NULL, toks, 2); + /* Arrange to free this buffer when no longer needed. */ + pfile->context->buff = tok_buff; } pfile->cb.def_pragma (pfile, pfile->directive_line); }