From patchwork Tue Jan 2 14:43:12 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrew Burgess X-Patchwork-Id: 83147 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 995D53858281 for ; Tue, 2 Jan 2024 14:44:20 +0000 (GMT) X-Original-To: gdb-patches@sourceware.org Delivered-To: gdb-patches@sourceware.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 7212E3858C2C for ; Tue, 2 Jan 2024 14:43:29 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 7212E3858C2C Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 7212E3858C2C Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=170.10.129.124 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1704206612; cv=none; b=MHEpZU2Oq6m5ShbxVG8J5tgwaqpXQlcEdBLxJz2StSZdTBUuRTEJSpwud4gpgLQKjNeniSOaRVxFLxa0ARtpAttGxM3GCErKn7WThmBgKzcV402K0VAMVEu3zd0YDACzfYg0H3NUCy5VyChapTT09tgB5RCysLJnlb1GeZ0GHfQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1704206612; c=relaxed/simple; bh=zu1COLfIyqHoc8ZowpE32S5ZrxJYjp4F51pqmyh4ybw=; h=DKIM-Signature:From:To:Subject:Date:Message-Id:MIME-Version; b=Bq5pw7C2YweX089NUhzSzN3a+nTijFssvgpRpmazbWGoYMU0wjiSU+yRyZkBFYVruf+WfV1FHaUDEzbLf3w8WCOT2KsoaoHYElD1inbaOt0RP8wVZ/D8IxBM+IhS2weqs+E+WAa+HnKrc9c7LzFglt+17BUlc6tSCcBjbl+Zt7s= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1704206609; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=LOjZaQPn0ySzom4HBtEHVIY0W1hJuQWSRt78s1u9JVg=; b=Od2l0UZ4xQXYEBE0PkCc6PBkDt0fNnNtIKLiwZvdNWwIxqjNyz36Pdf8r5GRtuQ4AqpyOq TUwCCOKRWSrByPoogylpuZ1hefTUeavcfn/1ujBAycFxDPVsU7rL7sa0GGo+p6hgm7gotA KaWCnr08igj6vyXGD9rUxGu1SaEPMPs= Received: from mail-wm1-f69.google.com (mail-wm1-f69.google.com [209.85.128.69]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-217-JdQXooG0PBG33E1LwS9UKQ-1; Tue, 02 Jan 2024 09:43:23 -0500 X-MC-Unique: JdQXooG0PBG33E1LwS9UKQ-1 Received: by mail-wm1-f69.google.com with SMTP id 5b1f17b1804b1-40d3b4606b3so86616235e9.2 for ; Tue, 02 Jan 2024 06:43:22 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1704206601; x=1704811401; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=KlFyQiiqU7ZKcfuCelqJCh+DGwgBZC2WN5R36VeJL4k=; b=QIx6X27PE5N570VgzR+eDxHl0m13XBWo1Es8nAJ42g8/lKiFIWRW5qIlcAS0KNpWSB Vf1muHB9eWQOb01LiXB6RwsG0cuGmkiROPbt5tep7wqaSvpB8+L1EYBcydNwrrh4ZO+b uVQhZiPETORG8cmKtm2vpnoMG1yEhXeu6jv7942sFEPVKAWj2PXUZuBydc56hfdH6b0V SB0h8/U19UigZpxDLVz5PjCCkKP3Oe9hhonqhKeXde0+yn30vb4yCqbkEKPgf7CoPt4N 7ggH478G4p/zIN9nTvgUZYxkZmRfm1gXBlOOptX8hEdadeiH9UfYwUAGpBQfYqaPksal j4IQ== X-Gm-Message-State: AOJu0YwvqsqNvm2M6tjOc17BwNvzzdloI7G6R8APd5yudbpS41YZebvI c3mirQv6BfDt9Ya3nscW5r50y6mL9aN9h6wopOgOEkRIwCRukbQKwUyNK2I/+yCJRHY741B+aOz 8uGmeWH66YvYKskkzKFvO6C1v2bFPshglvbuBaZOUWMem2zK8TYWFF09D+gnX8lATlQSBfOW5Qi K4vJj0TjEjo0AEDg== X-Received: by 2002:a05:600c:19cc:b0:40d:5f8a:5b13 with SMTP id u12-20020a05600c19cc00b0040d5f8a5b13mr4946486wmq.65.1704206601083; Tue, 02 Jan 2024 06:43:21 -0800 (PST) X-Google-Smtp-Source: AGHT+IF95eTB7gewn1Y2jutjvEU7BsL9DEJaSziBhmWrX8aid4KhjRPs/xIh4TwQQa0lpdV+GGe8YA== X-Received: by 2002:a05:600c:19cc:b0:40d:5f8a:5b13 with SMTP id u12-20020a05600c19cc00b0040d5f8a5b13mr4946479wmq.65.1704206600815; Tue, 02 Jan 2024 06:43:20 -0800 (PST) Received: from localhost (185.223.159.143.dyn.plus.net. [143.159.223.185]) by smtp.gmail.com with ESMTPSA id r20-20020a05600c459400b0040c3dcc36e6sm44560675wmo.47.2024.01.02.06.43.20 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 02 Jan 2024 06:43:20 -0800 (PST) From: Andrew Burgess To: gdb-patches@sourceware.org Cc: Andrew Burgess Subject: [PATCHv2 2/3] gdb: merge error handling from different expression parsers Date: Tue, 2 Jan 2024 14:43:12 +0000 Message-Id: <06889e172a22e80af44b30b1e70f1f06ecb3ba96.1704206350.git.aburgess@redhat.com> X-Mailer: git-send-email 2.25.4 In-Reply-To: References: MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-11.7 required=5.0 tests=BAYES_00, DKIM_INVALID, DKIM_SIGNED, GIT_PATCH_0, KAM_DMARC_NONE, KAM_DMARC_STATUS, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H4, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_NONE, TXREP, T_SCC_BODY_TEXT_LINE 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: gdb-patches@sourceware.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Gdb-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gdb-patches-bounces+patchwork=sourceware.org@sourceware.org Many (all?) of the expression parsers implement yyerror to handle parser errors, and all of these functions are basically identical. This commit adds a new parser_state::parse_error() function, which implements the common error handling code, this function can then be called from all the different yyerror functions. The benefit of this is that (in a future commit) I can improve the error output, and all the expression parsers will benefit. This commit is pure refactoring though, and so, there should be no user visible changes after this commit. --- gdb/ada-exp.y | 2 +- gdb/c-exp.y | 5 +---- gdb/d-exp.y | 5 +---- gdb/f-exp.y | 5 +---- gdb/go-exp.y | 5 +---- gdb/m2-exp.y | 5 +---- gdb/p-exp.y | 5 +---- gdb/parse.c | 11 +++++++++++ gdb/parser-defs.h | 5 +++++ 9 files changed, 23 insertions(+), 25 deletions(-) diff --git a/gdb/ada-exp.y b/gdb/ada-exp.y index fcb5aa4379b..2a1cff50887 100644 --- a/gdb/ada-exp.y +++ b/gdb/ada-exp.y @@ -1212,7 +1212,7 @@ ada_parse (struct parser_state *par_state) static void yyerror (const char *msg) { - error (_("Error in expression, near `%s'."), pstate->lexptr); + pstate->parse_error (msg); } /* Emit expression to access an instance of SYM, in block BLOCK (if diff --git a/gdb/c-exp.y b/gdb/c-exp.y index 2b4c21850d3..6697b3b2278 100644 --- a/gdb/c-exp.y +++ b/gdb/c-exp.y @@ -3482,8 +3482,5 @@ c_print_token (FILE *file, int type, YYSTYPE value) static void yyerror (const char *msg) { - if (pstate->prev_lexptr) - pstate->lexptr = pstate->prev_lexptr; - - error (_("A %s in expression, near `%s'."), msg, pstate->lexptr); + pstate->parse_error (msg); } diff --git a/gdb/d-exp.y b/gdb/d-exp.y index e2507982d50..627c681d895 100644 --- a/gdb/d-exp.y +++ b/gdb/d-exp.y @@ -1631,9 +1631,6 @@ d_parse (struct parser_state *par_state) static void yyerror (const char *msg) { - if (pstate->prev_lexptr) - pstate->lexptr = pstate->prev_lexptr; - - error (_("A %s in expression, near `%s'."), msg, pstate->lexptr); + pstate->parse_error (msg); } diff --git a/gdb/f-exp.y b/gdb/f-exp.y index e4e2171d641..88a95bccb11 100644 --- a/gdb/f-exp.y +++ b/gdb/f-exp.y @@ -1736,8 +1736,5 @@ f_language::parser (struct parser_state *par_state) const static void yyerror (const char *msg) { - if (pstate->prev_lexptr) - pstate->lexptr = pstate->prev_lexptr; - - error (_("A %s in expression, near `%s'."), msg, pstate->lexptr); + pstate->parse_error (msg); } diff --git a/gdb/go-exp.y b/gdb/go-exp.y index c9b9c0b1ab7..561a3bef1b0 100644 --- a/gdb/go-exp.y +++ b/gdb/go-exp.y @@ -1545,8 +1545,5 @@ go_language::parser (struct parser_state *par_state) const static void yyerror (const char *msg) { - if (pstate->prev_lexptr) - pstate->lexptr = pstate->prev_lexptr; - - error (_("A %s in expression, near `%s'."), msg, pstate->lexptr); + pstate->parse_error (msg); } diff --git a/gdb/m2-exp.y b/gdb/m2-exp.y index 092a8be248d..9a8767f5ac7 100644 --- a/gdb/m2-exp.y +++ b/gdb/m2-exp.y @@ -1006,8 +1006,5 @@ m2_language::parser (struct parser_state *par_state) const static void yyerror (const char *msg) { - if (pstate->prev_lexptr) - pstate->lexptr = pstate->prev_lexptr; - - error (_("A %s in expression, near `%s'."), msg, pstate->lexptr); + pstate->parse_error (msg); } diff --git a/gdb/p-exp.y b/gdb/p-exp.y index b0f334897ad..9dfa8c5fd4f 100644 --- a/gdb/p-exp.y +++ b/gdb/p-exp.y @@ -1660,8 +1660,5 @@ pascal_language::parser (struct parser_state *par_state) const static void yyerror (const char *msg) { - if (pstate->prev_lexptr) - pstate->lexptr = pstate->prev_lexptr; - - error (_("A %s in expression, near `%s'."), msg, pstate->lexptr); + pstate->parse_error (msg); } diff --git a/gdb/parse.c b/gdb/parse.c index b57d112fafd..efac0dee1af 100644 --- a/gdb/parse.c +++ b/gdb/parse.c @@ -244,6 +244,17 @@ parser_state::push_dollar (struct stoken str) (create_internalvar (copy.c_str () + 1)); } +/* See parser-defs.h. */ + +void +parser_state::parse_error (const char *msg) +{ + if (this->prev_lexptr) + this->lexptr = this->prev_lexptr; + + error (_("A %s in expression, near `%s'."), msg, this->lexptr); +} + const char * diff --git a/gdb/parser-defs.h b/gdb/parser-defs.h index 93ebdf5c061..4d40245228b 100644 --- a/gdb/parser-defs.h +++ b/gdb/parser-defs.h @@ -262,6 +262,11 @@ struct parser_state : public expr_builder push (expr::make_operation (std::move (lhs), std::move (rhs))); } + /* Function called from various the various parsers yyerror functions to + throw an error. The error will include a message identifying the + location of the error within the current expression. */ + void parse_error (const char *msg); + /* If this is nonzero, this block is used as the lexical context for symbol names. */