From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-wm1-x32c.google.com (mail-wm1-x32c.google.com [IPv6:2a00:1450:4864:20::32c]) by sourceware.org (Postfix) with ESMTPS id 63DEC385696B for ; Wed, 5 Apr 2023 14:06:28 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 63DEC385696B Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=embecosm.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=embecosm.com Received: by mail-wm1-x32c.google.com with SMTP id s13so20940932wmr.4 for ; Wed, 05 Apr 2023 07:06:28 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=embecosm.com; s=google; t=1680703587; h=content-transfer-encoding:mime-version:reply-to:references :in-reply-to:message-id:date:subject:cc:to:from:from:to:cc:subject :date:message-id:reply-to; bh=yjv8uVmOGlt5rngZpVbTyR3eaIs7Cu+1AFc8HkUT1Tw=; b=cgGNe94tMRc0xrEXt9xDJwwEmLfWzYO91+0R9yFPeQB3PwIp46jzL7sL92X6L2uS+e 7dHIEylRHlqOq6sHn+U3yxaZ285qD1+i39sHw73ZYeTvm32xoLevjIRMxoLqKGa8nX+F Ur5I/2wfmHVa7yqhFmZaStr+uNUa01S68HtwISE03FXjlEGgF+llJWe1ftuvrHN6WCH4 50WqPBHYT9do6b3sfToX9qeofJMBHn2skiGltuKAVW2+/yCM2ynCQjPgtop4TwFbXFxz w9TA3EjF/4M521IBQBsVjMXDsGDrfrz7HWYHyh+KIyumALU3OJktzUkU6pq8F1s3BvVc WlEQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1680703587; h=content-transfer-encoding:mime-version:reply-to: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=yjv8uVmOGlt5rngZpVbTyR3eaIs7Cu+1AFc8HkUT1Tw=; b=w1VU5+P2NPOaFmxTkyFduT+8dPoY15s1zjim4G427wrdqj4wNrZWNRDxXWPzFGaLW4 vVZUlErJQpl1gv8Acz9kR5jdcYbUGQWMtjZO9Z1jDKrEPgbgOfH0zX2Nmu3cTdmhXSet /DhlvBzhAj5fUG5kmCtTQ+OmYaGGvtw7O3EPn/foQgeFeMm4pNp4itRFs+s+6YO4AAXB vg0jCNO4AskdAhhYSmzQ/qEQrz1qOS8qTTrlYAbBGQfItODKqGg88J5T3ELRjg/SFug6 HS9zVtGKMVq3epK4nNxfoOjPb6W/h3zRqX9rGThdCUeq4npSU/mNwAtp0MFkouFPYFvQ fNfw== X-Gm-Message-State: AAQBX9fJpKxxaE79cKT2aYABb4x2Ry2VxZ035Nv4QFunEMdG+iZaZsot yu/mY7ZRPm40j2ql1mUmwn1LcygIUjeyhUmi6g== X-Google-Smtp-Source: AKy350YB7KNF7EpqwUeFv9chIrXMQW38FyiVbUOW305Q8Kzmm3PWqV0NGxXaA1Tjjsorpe5NoL+clA== X-Received: by 2002:a05:600c:2154:b0:3f0:49b5:f0ce with SMTP id v20-20020a05600c215400b003f049b5f0cemr4770135wml.12.1680703587653; Wed, 05 Apr 2023 07:06:27 -0700 (PDT) Received: from platypus.localdomain ([62.23.166.218]) by smtp.gmail.com with ESMTPSA id ay8-20020a05600c1e0800b003edddae1068sm2330150wmb.9.2023.04.05.07.06.26 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 05 Apr 2023 07:06:27 -0700 (PDT) From: arthur.cohen@embecosm.com To: gcc-patches@gcc.gnu.org Cc: gcc-rust@gcc.gnu.org, Arthur Cohen Subject: [committed 85/88] gccrs: diagnostic: Refactor Error class Date: Wed, 5 Apr 2023 16:04:09 +0200 Message-Id: <20230405140411.3016563-86-arthur.cohen@embecosm.com> X-Mailer: git-send-email 2.40.0 In-Reply-To: <20230405140411.3016563-1-arthur.cohen@embecosm.com> References: <20230405140411.3016563-1-arthur.cohen@embecosm.com> Reply-To: arthur.cohen@embecosm.com MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-14.2 required=5.0 tests=BAYES_00,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,GIT_PATCH_0,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS,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 List-Id: From: Arthur Cohen The class now allows for more variants including a `Hint` one which then gets emitted by calling `rust_inform`. This allows us to display hints/tips/notes in backtracking contexts such as the parser. gcc/rust/ChangeLog: * rust-diagnostics.h (struct Error): Add new Kind enum and various new static constructors to allow for hints as well. * rust-diagnostics.cc (Error::Error): Use new `kind` field properly. * checks/errors/privacy/rust-visibility-resolver.cc (VisibilityResolver::resolve_module_path): Use new Error API. * expand/rust-macro-builtins.cc (MacroBuiltin::include_handler): Likewise. * expand/rust-macro-expand.cc (parse_many): Likewise. (transcribe_type): Likewise. * parse/rust-parse-impl.h (Parser::parse_crate): Likewise. * rust-session-manager.cc (Session::handle_crate_name): Likewise. * ast/rust-ast.cc (Module::load_items): Likewise. --- gcc/rust/ast/rust-ast.cc | 2 +- .../privacy/rust-visibility-resolver.cc | 4 +- gcc/rust/expand/rust-macro-builtins.cc | 2 +- gcc/rust/expand/rust-macro-expand.cc | 4 +- gcc/rust/parse/rust-parse-impl.h | 2 +- gcc/rust/rust-diagnostics.cc | 43 ++++++++++++-- gcc/rust/rust-diagnostics.h | 56 ++++++++++++++++--- gcc/rust/rust-session-manager.cc | 6 +- 8 files changed, 98 insertions(+), 21 deletions(-) diff --git a/gcc/rust/ast/rust-ast.cc b/gcc/rust/ast/rust-ast.cc index 07983b7da06..68a7dfdc662 100644 --- a/gcc/rust/ast/rust-ast.cc +++ b/gcc/rust/ast/rust-ast.cc @@ -4084,7 +4084,7 @@ Module::load_items () inner_attrs = parser.parse_inner_attributes (); auto parsed_items = parser.parse_items (); for (const auto &error : parser.get_errors ()) - error.emit_error (); + error.emit (); items = std::move (parsed_items); kind = ModuleKind::LOADED; diff --git a/gcc/rust/checks/errors/privacy/rust-visibility-resolver.cc b/gcc/rust/checks/errors/privacy/rust-visibility-resolver.cc index becb9fd93a7..30afaf9174e 100644 --- a/gcc/rust/checks/errors/privacy/rust-visibility-resolver.cc +++ b/gcc/rust/checks/errors/privacy/rust-visibility-resolver.cc @@ -63,7 +63,7 @@ VisibilityResolver::resolve_module_path (const HIR::SimplePath &restriction, NodeId ref_node_id = UNKNOWN_NODEID; if (!resolver.lookup_resolved_name (ast_node_id, &ref_node_id)) { - invalid_path.emit_error (); + invalid_path.emit (); return false; } // FIXME: Add a hint here if we can find the path in another scope, such as @@ -77,7 +77,7 @@ VisibilityResolver::resolve_module_path (const HIR::SimplePath &restriction, auto module = mappings.lookup_module (ref); if (!module) { - invalid_path.emit_error (); + invalid_path.emit (); return false; } diff --git a/gcc/rust/expand/rust-macro-builtins.cc b/gcc/rust/expand/rust-macro-builtins.cc index 97594921dec..cf90b384cb5 100644 --- a/gcc/rust/expand/rust-macro-builtins.cc +++ b/gcc/rust/expand/rust-macro-builtins.cc @@ -736,7 +736,7 @@ MacroBuiltin::include_handler (Location invoc_locus, AST::MacroInvocData &invoc) bool has_error = !parser.get_errors ().empty (); for (const auto &error : parser.get_errors ()) - error.emit_error (); + error.emit (); if (has_error) { diff --git a/gcc/rust/expand/rust-macro-expand.cc b/gcc/rust/expand/rust-macro-expand.cc index 4b494490424..71aafef85b0 100644 --- a/gcc/rust/expand/rust-macro-expand.cc +++ b/gcc/rust/expand/rust-macro-expand.cc @@ -848,7 +848,7 @@ parse_many (Parser &parser, TokenId &delimiter, if (node.is_error ()) { for (auto err : parser.get_errors ()) - err.emit_error (); + err.emit (); return AST::Fragment::create_error (); } @@ -991,7 +991,7 @@ transcribe_type (Parser &parser) auto type = parser.parse_type (true); for (auto err : parser.get_errors ()) - err.emit_error (); + err.emit (); auto end = lexer.get_offs (); diff --git a/gcc/rust/parse/rust-parse-impl.h b/gcc/rust/parse/rust-parse-impl.h index cc5436cff63..6a98bcce654 100644 --- a/gcc/rust/parse/rust-parse-impl.h +++ b/gcc/rust/parse/rust-parse-impl.h @@ -444,7 +444,7 @@ Parser::parse_crate () // emit all errors for (const auto &error : error_table) - error.emit_error (); + error.emit (); return std::unique_ptr ( new AST::Crate (std::move (items), std::move (inner_attrs))); diff --git a/gcc/rust/rust-diagnostics.cc b/gcc/rust/rust-diagnostics.cc index c0f02c2831e..4e5c2ececd4 100644 --- a/gcc/rust/rust-diagnostics.cc +++ b/gcc/rust/rust-diagnostics.cc @@ -231,14 +231,49 @@ rust_debug_loc (const Location location, const char *fmt, ...) } namespace Rust { -Error::Error (const Location location, const char *fmt, ...) : locus (location) + +/** + * This function takes ownership of `args` and calls `va_end` on it + */ +static Error +va_constructor (Error::Kind kind, Location locus, const char *fmt, va_list args) + RUST_ATTRIBUTE_GCC_DIAG (3, 0); + +static Error +va_constructor (Error::Kind kind, Location locus, const char *fmt, va_list args) +{ + std::string message = expand_message (fmt, args); + message.shrink_to_fit (); + va_end (args); + + return Error (kind, locus, message); +} + +Error::Error (const Location location, const char *fmt, ...) + : kind (Kind::Err), locus (location) { va_list ap; + va_start (ap, fmt); + *this = va_constructor (Kind::Err, location, fmt, ap); +} + +Error +Error::Hint (const Location location, const char *fmt, ...) +{ + va_list ap; va_start (ap, fmt); - message = expand_message (fmt, ap); - va_end (ap); - message.shrink_to_fit (); + return va_constructor (Kind::Hint, location, fmt, ap); +} + +Error +Error::Fatal (const Location location, const char *fmt, ...) +{ + va_list ap; + va_start (ap, fmt); + + return va_constructor (Kind::FatalErr, location, fmt, ap); } + } // namespace Rust diff --git a/gcc/rust/rust-diagnostics.h b/gcc/rust/rust-diagnostics.h index 900198e72ca..43fee8baf4f 100644 --- a/gcc/rust/rust-diagnostics.h +++ b/gcc/rust/rust-diagnostics.h @@ -114,27 +114,69 @@ namespace Rust { * errors to be ignored, e.g. if backtracking. */ struct Error { + enum class Kind + { + Hint, + Err, + FatalErr, + }; + + Kind kind; Location locus; std::string message; // TODO: store more stuff? e.g. node id? - Error (Location locus, std::string message) - : locus (locus), message (std::move (message)) + Error (Kind kind, Location locus, std::string message) + : kind (kind), locus (locus), message (std::move (message)) { message.shrink_to_fit (); } + Error (Location locus, std::string message) + { + Error (Kind::Err, locus, std::move (message)); + } + + static Error Hint (Location locus, std::string message) + { + return Error (Kind::Hint, locus, std::move (message)); + } + + static Error Fatal (Location locus, std::string message) + { + return Error (Kind::FatalErr, locus, std::move (message)); + } + // TODO: the attribute part might be incorrect Error (Location locus, const char *fmt, ...) /*RUST_ATTRIBUTE_GCC_DIAG (2, 3)*/ RUST_ATTRIBUTE_GCC_DIAG (3, 4); - // Irreversibly emits the error as an error. - void emit_error () const { rust_error_at (locus, "%s", message.c_str ()); } + /** + * printf-like overload of Error::Hint + */ + static Error Hint (Location locus, const char *fmt, ...) + RUST_ATTRIBUTE_GCC_DIAG (2, 3); + + /** + * printf-like overload of Error::Fatal + */ + static Error Fatal (Location locus, const char *fmt, ...) + RUST_ATTRIBUTE_GCC_DIAG (2, 3); - // Irreversibly emits the error as a fatal error. - void emit_fatal_error () const + void emit () const { - rust_fatal_error (locus, "%s", message.c_str ()); + switch (kind) + { + case Kind::Hint: + rust_inform (locus, "%s", message.c_str ()); + break; + case Kind::Err: + rust_error_at (locus, "%s", message.c_str ()); + break; + case Kind::FatalErr: + rust_fatal_error (locus, "%s", message.c_str ()); + break; + } } }; } // namespace Rust diff --git a/gcc/rust/rust-session-manager.cc b/gcc/rust/rust-session-manager.cc index a086f83a3d0..074bad9c5d7 100644 --- a/gcc/rust/rust-session-manager.cc +++ b/gcc/rust/rust-session-manager.cc @@ -193,7 +193,7 @@ Session::handle_option ( else { rust_assert (!error.message.empty ()); - error.emit_error (); + error.emit (); } } else @@ -391,7 +391,7 @@ Session::handle_crate_name (const AST::Crate &parsed_crate) if (!validate_crate_name (msg_str, error)) { error.locus = attr.get_locus (); - error.emit_error (); + error.emit (); continue; } @@ -412,7 +412,7 @@ Session::handle_crate_name (const AST::Crate &parsed_crate) if (!options.crate_name_set_manually && !validate_crate_name (options.crate_name, error)) { - error.emit_error (); + error.emit (); rust_inform (linemap->get_location (0), "crate name inferred from this file"); } -- 2.40.0