From patchwork Sat Nov 12 16:53:31 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Marek Polacek X-Patchwork-Id: 19222 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:6687:0:0:0:0:0 with SMTP id l7csp1350136wru; Sat, 12 Nov 2022 08:54:45 -0800 (PST) X-Google-Smtp-Source: AA0mqf6c8QVUzzcLbGQt932w5Qf1ekSIrMAhwSTbQL2YdC7IUmGJ5Do1bGX0oCxHjoQbOkqAAbvJ X-Received: by 2002:a17:906:249a:b0:781:fcf6:e73a with SMTP id e26-20020a170906249a00b00781fcf6e73amr5526714ejb.352.1668272085044; Sat, 12 Nov 2022 08:54:45 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1668272085; cv=none; d=google.com; s=arc-20160816; b=Xl2DbcAmbgAIyVMPV0hkBYNNG64DT3cy9JZGHV/I6Ic0om1IYAmbMaVLCFJhkTDloU yRW9M/Zj5Ll/Ao/BHJ/kApCP2F6LCwFqyh2KjGTBrhkv3XuMti+XZugMwNHexewSZTce Xs0/wxOAINSZAJmwwLTi7ZoWpiwiThicXFacWiyusstI0qbOivUifIWNaIBWbE0ZrDyn 32FMZ+JL1p56gNPRzmlq0htCpoUEhdomsL3oOGhINYk0HoesG1xTGrTcRZKAYeLzPXI1 j5/kbtSHe+IqH27RFbVI8oC8CpDybpE4klUH/UBuBFf7yBTnTC9TtNyfg5W9/bZU45CO 1L9w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:reply-to:from:list-subscribe:list-help:list-post :list-archive:list-unsubscribe:list-id:precedence :content-transfer-encoding:mime-version:message-id:date:subject:to :dmarc-filter:delivered-to:dkim-signature:dkim-filter; bh=0NdvCw6X5QUM+jQhDrk5XQ4maF+uccMYpc5vjk0ZsBw=; b=nasDMKhgc/GZO+A9CeWGv6/lG6WT8Y6zCECJXN8107TR/ljeDYf53/xOBQer3qADOn sRyFcwCGZ+UAgOoWsuRyRVB+eQulTIDbiJkW13lSTbW0d84H1a+0DY46ROV1XvxqZRDx RDcFftHHUh5+PGiB4/XXDHAxdhs6r6pJKBUB9nIxtKl4R6R4SRgwnMbAydiZFTMp+o02 uNCGb/QsOsw+QCsAjvsepMISIam3Kwx2uBWPUZ3/OVw3iNV9NYMEVZ95vDLoIGEtTcAP XKyY2Ty4imlCNbhl7GcsRv8qrPZ1ggmlviFvhmsfdd5nleyvdd2hwS0OjNAFcbE5LIXD rUiw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b="CJKfd6/X"; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gnu.org Received: from sourceware.org (ip-8-43-85-97.sourceware.org. [8.43.85.97]) by mx.google.com with ESMTPS id hr22-20020a1709073f9600b0078d3a35e181si4594487ejc.871.2022.11.12.08.54.44 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 12 Nov 2022 08:54:45 -0800 (PST) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) client-ip=8.43.85.97; Authentication-Results: mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b="CJKfd6/X"; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gnu.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 42B243834692 for ; Sat, 12 Nov 2022 16:54:35 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 42B243834692 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1668272075; bh=0NdvCw6X5QUM+jQhDrk5XQ4maF+uccMYpc5vjk0ZsBw=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=CJKfd6/XBBsf30n//Ulwlz+dZoPyIxw9RaL0P8Y5qS5Wc6ldsNNUmqkCaKIqR1f5t ZAX4mytKHDdXTBnzNqgoAklpBkoLA9/eKZcbZMC7ug8Ck7do3IJ5x1Lz7h1VLPzz87 HBOD8lgVTIaDXD5ujXRWKPEE8BsjLWAFBvhmDe/Y= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by sourceware.org (Postfix) with ESMTPS id 4BF8D38460AD for ; Sat, 12 Nov 2022 16:53:46 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 4BF8D38460AD Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-621-UzsAJ_tcPVeOvHY_S8LQFg-1; Sat, 12 Nov 2022 11:53:44 -0500 X-MC-Unique: UzsAJ_tcPVeOvHY_S8LQFg-1 Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.rdu2.redhat.com [10.11.54.5]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 1CFDE811E75 for ; Sat, 12 Nov 2022 16:53:44 +0000 (UTC) Received: from pdp-11.redhat.com (unknown [10.22.8.18]) by smtp.corp.redhat.com (Postfix) with ESMTP id E73854221F; Sat, 12 Nov 2022 16:53:43 +0000 (UTC) To: GCC Patches , Jason Merrill Subject: [PATCH] c++: Reject UDLs in certain contexts [PR105300] Date: Sat, 12 Nov 2022 11:53:31 -0500 Message-Id: <20221112165331.349041-1-polacek@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.5 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-12.0 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H2, SPF_HELO_NONE, SPF_NONE, 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: , X-Patchwork-Original-From: Marek Polacek via Gcc-patches From: Marek Polacek Reply-To: Marek Polacek Errors-To: gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org Sender: "Gcc-patches" X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1749310069844526409?= X-GMAIL-MSGID: =?utf-8?q?1749310069844526409?= In this PR, we are crashing because we've encountered a UDL where a string-literal is expected. This patch makes the parser reject string and character UDLs in all places where the grammar requires a string-literal and not a user-defined-string-literal. Bootstrapped/regtested on x86_64-pc-linux-gnu, ok for trunk? PR c++/105300 gcc/c-family/ChangeLog: * c-pragma.cc (handle_pragma_message): Warn for CPP_STRING_USERDEF. gcc/cp/ChangeLog: * parser.cc (cp_parser_string_literal): Add a bool parameter. Give an error when UDLs are not permitted. (cp_parser_primary_expression): Adjust the call to cp_parser_string_literal. (cp_parser_linkage_specification): Likewise. (cp_parser_static_assert): Likewise. (cp_parser_operator): Likewise. (cp_parser_asm_definition): Likewise. (cp_parser_asm_specification_opt): Likewise. (cp_parser_asm_operand_list): Likewise. (cp_parser_asm_clobber_list): Likewise. (cp_parser_omp_context_selector): Likewise. (pragma_lex): Likewise. gcc/testsuite/ChangeLog: * g++.dg/cpp0x/udlit-error1.C: New test. --- gcc/c-family/c-pragma.cc | 3 + gcc/cp/parser.cc | 69 ++++++++++++++--------- gcc/testsuite/g++.dg/cpp0x/udlit-error1.C | 21 +++++++ 3 files changed, 65 insertions(+), 28 deletions(-) create mode 100644 gcc/testsuite/g++.dg/cpp0x/udlit-error1.C base-commit: f232715d15618e91c90eb210e23de10909590944 diff --git a/gcc/c-family/c-pragma.cc b/gcc/c-family/c-pragma.cc index 142a46441ac..49f405b605b 100644 --- a/gcc/c-family/c-pragma.cc +++ b/gcc/c-family/c-pragma.cc @@ -1390,6 +1390,9 @@ handle_pragma_message (cpp_reader *) } else if (token == CPP_STRING) message = x; + else if (token == CPP_STRING_USERDEF) + GCC_BAD ("string literal with user-defined suffix is invalid in this " + "context"); else GCC_BAD ("expected a string after %<#pragma message%>"); diff --git a/gcc/cp/parser.cc b/gcc/cp/parser.cc index e4021835ed5..ae2798e2a33 100644 --- a/gcc/cp/parser.cc +++ b/gcc/cp/parser.cc @@ -2226,7 +2226,7 @@ pop_unparsed_function_queues (cp_parser *parser) static cp_expr cp_parser_identifier (cp_parser *); static cp_expr cp_parser_string_literal - (cp_parser *, bool, bool, bool); + (cp_parser *, bool, bool, bool, bool); static cp_expr cp_parser_userdef_char_literal (cp_parser *); static tree cp_parser_userdef_string_literal @@ -4402,7 +4402,8 @@ cp_parser_identifier (cp_parser* parser) TREE_STRING representing the combined, nul-terminated string constant. If TRANSLATE is true, translate the string to the execution character set. If WIDE_OK is true, a wide string is - invalid here. + valid here. If UDL_OK is true, a string literal with user-defined + suffix can be used in this context. C++98 [lex.string] says that if a narrow string literal token is adjacent to a wide string literal token, the behavior is undefined. @@ -4414,7 +4415,7 @@ cp_parser_identifier (cp_parser* parser) FUTURE: ObjC++ will need to handle @-strings here. */ static cp_expr cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok, - bool lookup_udlit = true) + bool udl_ok, bool lookup_udlit = true) { tree value; size_t count; @@ -4439,6 +4440,12 @@ cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok, if (cpp_userdef_string_p (tok->type)) { + if (!udl_ok) + { + error_at (loc, "string literal with user-defined suffix " + "is invalid in this context"); + return error_mark_node; + } string_tree = USERDEF_LITERAL_VALUE (tok->u.value); curr_type = cpp_userdef_string_remove_type (tok->type); curr_tok_is_userdef_p = true; @@ -5655,7 +5662,7 @@ cp_parser_primary_expression (cp_parser *parser, argument to cp_parser_string_literal. */ return (cp_parser_string_literal (parser, parser->translate_strings_p, - true) + /*wide_ok=*/true, /*udl_ok=*/true) .maybe_add_location_wrapper ()); case CPP_OPEN_PAREN: @@ -16161,15 +16168,14 @@ cp_parser_function_specifier_opt (cp_parser* parser, static void cp_parser_linkage_specification (cp_parser* parser, tree prefix_attr) { - tree linkage; - /* Look for the `extern' keyword. */ cp_token *extern_token = cp_parser_require_keyword (parser, RID_EXTERN, RT_EXTERN); /* Look for the string-literal. */ cp_token *string_token = cp_lexer_peek_token (parser->lexer); - linkage = cp_parser_string_literal (parser, false, false); + tree linkage = cp_parser_string_literal (parser, /*translate=*/false, + /*wide_ok=*/false, /*udl_ok=*/false); /* Transform the literal into an identifier. If the literal is a wide-character string, or contains embedded NULs, then we can't @@ -16300,8 +16306,8 @@ cp_parser_static_assert(cp_parser *parser, bool member_p) /* Parse the string-literal message. */ message = cp_parser_string_literal (parser, - /*translate=*/false, - /*wide_ok=*/true); + /*translate=*/false, /*wide_ok=*/true, + /*udl_ok=*/false); /* A `)' completes the static assertion. */ if (!parens.require_close (parser)) @@ -17349,7 +17355,6 @@ cp_parser_operator (cp_parser* parser, location_t start_loc) case CPP_STRING16_USERDEF: case CPP_STRING32_USERDEF: { - cp_expr str; tree string_tree; int sz, len; @@ -17357,8 +17362,10 @@ cp_parser_operator (cp_parser* parser, location_t start_loc) maybe_warn_cpp0x (CPP0X_USER_DEFINED_LITERALS); /* Consume the string. */ - str = cp_parser_string_literal (parser, /*translate=*/true, - /*wide_ok=*/true, /*lookup_udlit=*/false); + cp_expr str = cp_parser_string_literal (parser, /*translate=*/true, + /*wide_ok=*/true, + /*udl_ok=*/true, + /*lookup_udlit=*/false); if (str == error_mark_node) return error_mark_node; else if (TREE_CODE (str) == USERDEF_LITERAL) @@ -21975,7 +21982,6 @@ cp_parser_using_directive (cp_parser* parser) static void cp_parser_asm_definition (cp_parser* parser) { - tree string; tree outputs = NULL_TREE; tree inputs = NULL_TREE; tree clobbers = NULL_TREE; @@ -22083,7 +22089,8 @@ cp_parser_asm_definition (cp_parser* parser) if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN)) return; /* Look for the string. */ - string = cp_parser_string_literal (parser, false, false); + tree string = cp_parser_string_literal (parser, /*translate=*/false, + /*wide_ok=*/false, /*udl_ok=*/false); if (string == error_mark_node) { cp_parser_skip_to_closing_parenthesis (parser, true, false, @@ -28485,11 +28492,8 @@ cp_parser_yield_expression (cp_parser* parser) static tree cp_parser_asm_specification_opt (cp_parser* parser) { - cp_token *token; - tree asm_specification; - /* Peek at the next token. */ - token = cp_lexer_peek_token (parser->lexer); + cp_token *token = cp_lexer_peek_token (parser->lexer); /* If the next token isn't the `asm' keyword, then there's no asm-specification. */ if (!cp_parser_is_keyword (token, RID_ASM)) @@ -28502,7 +28506,10 @@ cp_parser_asm_specification_opt (cp_parser* parser) parens.require_open (parser); /* Look for the string-literal. */ - asm_specification = cp_parser_string_literal (parser, false, false); + tree asm_specification = cp_parser_string_literal (parser, + /*translate=*/false, + /*wide_ok=*/false, + /*udl_ok=*/false); /* Look for the `)'. */ parens.require_close (parser); @@ -28535,8 +28542,6 @@ cp_parser_asm_operand_list (cp_parser* parser) while (true) { - tree string_literal; - tree expression; tree name; if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE)) @@ -28554,13 +28559,16 @@ cp_parser_asm_operand_list (cp_parser* parser) else name = NULL_TREE; /* Look for the string-literal. */ - string_literal = cp_parser_string_literal (parser, false, false); + tree string_literal = cp_parser_string_literal (parser, + /*translate=*/false, + /*wide_ok=*/false, + /*udl_ok=*/false); /* Look for the `('. */ matching_parens parens; parens.require_open (parser); /* Parse the expression. */ - expression = cp_parser_expression (parser); + tree expression = cp_parser_expression (parser); /* Look for the `)'. */ parens.require_close (parser); @@ -28600,10 +28608,11 @@ cp_parser_asm_clobber_list (cp_parser* parser) while (true) { - tree string_literal; - /* Look for the string literal. */ - string_literal = cp_parser_string_literal (parser, false, false); + tree string_literal = cp_parser_string_literal (parser, + /*translate=*/false, + /*wide_ok=*/false, + /*udl_ok=*/false); /* Add it to the list. */ clobbers = tree_cons (NULL_TREE, string_literal, clobbers); /* If the next token is not a `,', then the list is @@ -45761,7 +45770,10 @@ cp_parser_omp_context_selector (cp_parser *parser, tree set, bool has_parms_p) cp_lexer_consume_token (parser->lexer); } else if (cp_lexer_next_token_is (parser->lexer, CPP_STRING)) - value = cp_parser_string_literal (parser, false, false); + value = cp_parser_string_literal (parser, + /*translate=*/false, + /*wide_ok=*/false, + /*udl_ok=*/false); else { cp_parser_error (parser, "expected identifier or " @@ -48783,7 +48795,8 @@ pragma_lex (tree *value, location_t *loc) if (ret == CPP_PRAGMA_EOL) ret = CPP_EOF; else if (ret == CPP_STRING) - *value = cp_parser_string_literal (the_parser, false, false); + *value = cp_parser_string_literal (the_parser, /*translate=*/false, + /*wide_ok=*/false, /*udl_ok=*/false); else { if (ret == CPP_KEYWORD) diff --git a/gcc/testsuite/g++.dg/cpp0x/udlit-error1.C b/gcc/testsuite/g++.dg/cpp0x/udlit-error1.C new file mode 100644 index 00000000000..66e300e350f --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp0x/udlit-error1.C @@ -0,0 +1,21 @@ +// PR c++/105300 +// { dg-do compile { target c++11 } } + +void operator""_x(const char *, decltype(sizeof(0))); + +#include ""_x // { dg-error "include expects" } +#line ""_x // { dg-error "not a positive integer" } +#if __has_include(""_x) // { dg-error "requires a header-name" } +#endif + +#pragma message "hi"_x // { dg-warning "string literal with user-defined suffix is invalid in this context" } + +extern "C"_x { void g(); } // { dg-error "before user-defined string literal" } +static_assert(true, "foo"_x); // { dg-error "string literal with user-defined suffix is invalid in this context|expected" } + +[[deprecated("oof"_x)]] +void +lol () // { dg-error "not a string" } +{ + asm (""_x); // { dg-error "string literal with user-defined suffix is invalid in this context" } +}