From patchwork Tue Jul 16 08:55:39 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Merrill X-Patchwork-Id: 1132535 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (mailfrom) smtp.mailfrom=gcc.gnu.org (client-ip=209.132.180.131; helo=sourceware.org; envelope-from=gcc-patches-return-505124-incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=) Authentication-Results: ozlabs.org; dmarc=fail (p=none dis=none) header.from=redhat.com Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=gcc.gnu.org header.i=@gcc.gnu.org header.b="eOvhRJnZ"; dkim-atps=neutral Received: from sourceware.org (server1.sourceware.org [209.132.180.131]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 45nvQJ5Szcz9s7T for ; Tue, 16 Jul 2019 18:56:24 +1000 (AEST) DomainKey-Signature: a=rsa-sha1; c=nofws; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender:from :to:subject:date:message-id:mime-version :content-transfer-encoding; q=dns; s=default; b=p84lyj7jKb4RXDoM nHJNraQxccT3AEP26BNCw2r/35OkAROms3mgeCfpDfFWEU/fudAzqw1gAIT5Kfcf Kr4ASQCgwxcR1hAE2BPQ2E0WWulSLFFCRpICk2Zj6ickDVTAaf4HJQwYpjfqDv+g kEGdDZBb+KqBLuGBQ1kJicOXOos= DKIM-Signature: v=1; a=rsa-sha1; c=relaxed; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender:from :to:subject:date:message-id:mime-version :content-transfer-encoding; s=default; bh=VPSXtZlnhGXwp5pAlAb9G1 sbpw4=; b=eOvhRJnZdd/T2+JD6baRf0lEi5dQ/fE21KMYuTEi9eWVsLuHFS4UyO E578D9laBgtPQU55YvpxA99UaAQ89uWjNkOHKkWkFATCjec66V7Vb2/DqT0RoZ1E x0t52jKd98d/uafEos9le2bmwLPu/mLMCDcWGLNbzq+MDCphwQhsY= Received: (qmail 127141 invoked by alias); 16 Jul 2019 08:56:17 -0000 Mailing-List: contact gcc-patches-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Archive: List-Post: List-Help: Sender: gcc-patches-owner@gcc.gnu.org Delivered-To: mailing list gcc-patches@gcc.gnu.org Received: (qmail 127133 invoked by uid 89); 16 Jul 2019 08:56:17 -0000 Authentication-Results: sourceware.org; auth=none X-Spam-SWARE-Status: No, score=-26.5 required=5.0 tests=AWL, BAYES_00, GIT_PATCH_0, GIT_PATCH_1, GIT_PATCH_2, GIT_PATCH_3, KAM_ASCII_DIVIDERS, RCVD_IN_DNSWL_NONE autolearn=ham version=3.3.1 spammy=ranging X-HELO: mail-wm1-f66.google.com Received: from mail-wm1-f66.google.com (HELO mail-wm1-f66.google.com) (209.85.128.66) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Tue, 16 Jul 2019 08:56:15 +0000 Received: by mail-wm1-f66.google.com with SMTP id s15so17818259wmj.3 for ; Tue, 16 Jul 2019 01:56:14 -0700 (PDT) Received: from localhost.localdomain ([62.153.214.124]) by smtp.gmail.com with ESMTPSA id y18sm19420114wmi.23.2019.07.16.01.56.05 for (version=TLS1_3 cipher=AEAD-AES256-GCM-SHA384 bits=256/256); Tue, 16 Jul 2019 01:56:12 -0700 (PDT) From: Jason Merrill To: gcc-patches@gcc.gnu.org Subject: [C++ PATCH] Simplify range location creation in C++ parser. Date: Tue, 16 Jul 2019 10:55:39 +0200 Message-Id: <20190716085539.2485-1-jason@redhat.com> MIME-Version: 1.0 X-IsSubscribed: yes Many places in the parser follow the same pattern of capturing the location of the last lexed token, either before or after lexing it, and then using that as the end of a location range; this can be simplified by passing the lexer to make_location and grabbing the token location there. Tested x86_64-pc-linux-gnu, applying to trunk. * parser.c (make_location): Add overload taking cp_lexer* as last parameter. --- gcc/cp/parser.c | 72 +++++++++++++++++------------------------------- gcc/cp/ChangeLog | 3 ++ 2 files changed, 29 insertions(+), 46 deletions(-) base-commit: 315f00e36893e3346b015f4f37a2dc02ad9e76bd diff --git a/gcc/cp/parser.c b/gcc/cp/parser.c index 5e4b45391d5..1a5da1dd8e8 100644 --- a/gcc/cp/parser.c +++ b/gcc/cp/parser.c @@ -773,6 +773,16 @@ cp_lexer_previous_token (cp_lexer *lexer) return cp_lexer_token_at (lexer, tp); } +/* Overload for make_location, taking the lexer to mean the location of the + previous token. */ + +static inline location_t +make_location (location_t caret, location_t start, cp_lexer *lexer) +{ + cp_token *t = cp_lexer_previous_token (lexer); + return make_location (caret, start, t->location); +} + /* nonzero if we are presently saving tokens. */ static inline int @@ -7774,10 +7784,8 @@ cp_parser_postfix_dot_deref_expression (cp_parser *parser, ~~~^~~~~~~~~~~~~ where the caret is at the deref token, ranging from the start of postfix_expression to the end of the access expr. */ - location_t end_loc - = get_finish (cp_lexer_previous_token (parser->lexer)->location); location_t combined_loc - = make_location (input_location, start_loc, end_loc); + = make_location (input_location, start_loc, parser->lexer); protected_set_expr_location (postfix_expression, combined_loc); } } @@ -8187,10 +8195,8 @@ cp_parser_unary_expression (cp_parser *parser, cp_id_kind * pidk, ^~~~~~~~~~~~~~ with start == caret at the start of the "alignof"/"sizeof" token, with the endpoint at the final closing paren. */ - location_t finish_loc - = cp_lexer_previous_token (parser->lexer)->location; location_t compound_loc - = make_location (start_loc, start_loc, finish_loc); + = make_location (start_loc, start_loc, parser->lexer); cp_expr ret_expr (ret); ret_expr.set_location (compound_loc); @@ -8291,8 +8297,6 @@ cp_parser_unary_expression (cp_parser *parser, cp_id_kind * pidk, parser->type_definition_forbidden_message = saved_message; - location_t finish_loc - = cp_lexer_peek_token (parser->lexer)->location; parens.require_close (parser); /* Construct a location of the form: @@ -8300,7 +8304,7 @@ cp_parser_unary_expression (cp_parser *parser, cp_id_kind * pidk, ^~~~~~~~~~~~~~~ with start == caret, finishing at the close-paren. */ location_t noexcept_loc - = make_location (start_loc, start_loc, finish_loc); + = make_location (start_loc, start_loc, parser->lexer); return cp_expr (finish_noexcept_expr (expr, tf_warning_or_error), noexcept_loc); @@ -8351,15 +8355,13 @@ cp_parser_unary_expression (cp_parser *parser, cp_id_kind * pidk, /* Consume the '&&' token. */ cp_lexer_consume_token (parser->lexer); /* Look for the identifier. */ - location_t finish_loc - = get_finish (cp_lexer_peek_token (parser->lexer)->location); identifier = cp_parser_identifier (parser); /* Construct a location of the form: &&label ^~~~~~~ with caret==start at the "&&", finish at the end of the label. */ location_t combined_loc - = make_location (start_loc, start_loc, finish_loc); + = make_location (start_loc, start_loc, parser->lexer); /* Create an expression representing the address. */ expression = finish_label_address_expr (identifier, combined_loc); if (cp_parser_non_integral_constant_expression (parser, @@ -8607,10 +8609,8 @@ cp_parser_has_attribute_expression (cp_parser *parser) ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ with start == caret at the start of the built-in token, and with the endpoint at the final closing paren. */ - location_t finish_loc - = cp_lexer_previous_token (parser->lexer)->location; location_t compound_loc - = make_location (start_loc, start_loc, finish_loc); + = make_location (start_loc, start_loc, parser->lexer); cp_expr ret_expr (ret ? boolean_true_node : boolean_false_node); ret_expr.set_location (compound_loc); @@ -8732,9 +8732,8 @@ cp_parser_new_expression (cp_parser* parser) ^~~~~~~~~~~~ with caret == start at the start of the "new" token, and the end at the end of the final token we consumed. */ - cp_token *end_tok = cp_lexer_previous_token (parser->lexer); - location_t end_loc = get_finish (end_tok->location); - location_t combined_loc = make_location (start_loc, start_loc, end_loc); + location_t combined_loc = make_location (start_loc, start_loc, + parser->lexer); /* Create a representation of the new-expression. */ ret = build_new (&placement, type, nelts, &initializer, global_scope_p, @@ -10501,10 +10500,9 @@ cp_parser_lambda_expression (cp_parser* parser) insert_pending_capture_proxies (); /* Update the lambda expression to a range. */ - cp_token *end_tok = cp_lexer_previous_token (parser->lexer); LAMBDA_EXPR_LOCATION (lambda_expr) = make_location (token->location, token->location, - end_tok->location); + parser->lexer); if (ok) lambda_expr = build_lambda_object (lambda_expr); @@ -11147,11 +11145,7 @@ cp_parser_statement (cp_parser* parser, tree in_statement_expr, cp_parser_parse_tentatively (parser); std_attrs = cp_parser_std_attribute_spec_seq (parser); if (std_attrs) - { - location_t end_loc - = cp_lexer_previous_token (parser->lexer)->location; - attrs_loc = make_location (attrs_loc, attrs_loc, end_loc); - } + attrs_loc = make_location (attrs_loc, attrs_loc, parser->lexer); if (c_dialect_objc ()) { if (!cp_parser_parse_definitely (parser)) @@ -15529,10 +15523,8 @@ cp_parser_operator (cp_parser* parser, location_t start_loc) ^~~~~~~~~~~~~~~~~~~~~ with caret == start at the start token, finish at the end of the suffix identifier. */ - location_t finish_loc - = get_finish (cp_lexer_previous_token (parser->lexer)->location); location_t combined_loc - = make_location (start_loc, start_loc, finish_loc); + = make_location (start_loc, start_loc, parser->lexer); return cp_expr (id, combined_loc); } @@ -16454,10 +16446,8 @@ cp_parser_template_id (cp_parser *parser, ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ with caret == start at the start of the template-name, ranging until the closing '>'. */ - location_t finish_loc - = get_finish (cp_lexer_previous_token (parser->lexer)->location); location_t combined_loc - = make_location (token->location, token->location, finish_loc); + = make_location (token->location, token->location, parser->lexer); /* Check for concepts autos where they don't belong. We could identify types in some cases of idnetifier TEMPL, looking ahead @@ -28567,10 +28557,8 @@ cp_parser_functional_cast (cp_parser* parser, tree type) ^~~~~~~~~~~~~~~ with caret == start at the start of the type name, finishing at the closing brace. */ - location_t finish_loc - = get_finish (cp_lexer_previous_token (parser->lexer)->location); location_t combined_loc = make_location (start_loc, start_loc, - finish_loc); + parser->lexer); cast.set_location (combined_loc); return cast; } @@ -28605,9 +28593,7 @@ cp_parser_functional_cast (cp_parser* parser, tree type) ^~~~~~~~ with caret == start at the start of the type name, finishing at the closing paren. */ - location_t finish_loc - = get_finish (cp_lexer_previous_token (parser->lexer)->location); - location_t combined_loc = make_location (start_loc, start_loc, finish_loc); + location_t combined_loc = make_location (start_loc, start_loc, parser->lexer); cast.set_location (combined_loc); return cast; } @@ -30652,9 +30638,7 @@ cp_parser_objc_encode_expression (cp_parser* parser) @encode(int) ^~~~~~~~~~~~ with caret==start at the @ token, finishing at the close paren. */ - location_t combined_loc - = make_location (start_loc, start_loc, - cp_lexer_previous_token (parser->lexer)->location); + location_t combined_loc = make_location (start_loc, start_loc, parser->lexer); return cp_expr (objc_build_encode_expr (type), combined_loc); } @@ -30698,9 +30682,7 @@ cp_parser_objc_protocol_expression (cp_parser* parser) @protocol(prot) ^~~~~~~~~~~~~~~ with caret==start at the @ token, finishing at the close paren. */ - location_t combined_loc - = make_location (start_loc, start_loc, - cp_lexer_previous_token (parser->lexer)->location); + location_t combined_loc = make_location (start_loc, start_loc, parser->lexer); tree result = objc_build_protocol_expr (proto); protected_set_expr_location (result, combined_loc); return result; @@ -30785,9 +30767,7 @@ cp_parser_objc_selector_expression (cp_parser* parser) @selector(func) ^~~~~~~~~~~~~~~ with caret==start at the @ token, finishing at the close paren. */ - location_t combined_loc - = make_location (loc, loc, - cp_lexer_previous_token (parser->lexer)->location); + location_t combined_loc = make_location (loc, loc, parser->lexer); tree result = objc_build_selector_expr (combined_loc, sel_seq); /* TODO: objc_build_selector_expr doesn't always honor the location. */ protected_set_expr_location (result, combined_loc); diff --git a/gcc/cp/ChangeLog b/gcc/cp/ChangeLog index ad7f4a3ae1f..c9091f523c5 100644 --- a/gcc/cp/ChangeLog +++ b/gcc/cp/ChangeLog @@ -1,5 +1,8 @@ 2019-07-16 Jason Merrill + * parser.c (make_location): Add overload taking cp_lexer* as last + parameter. + * parser.c (cp_parser_simple_type_specifier): Separate tentative parses for optional type-spec and CTAD.