From patchwork Mon Sep 9 23:48:34 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Malcolm X-Patchwork-Id: 1982870 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=in9XN/5a; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=8.43.85.97; helo=server2.sourceware.org; envelope-from=gcc-patches-bounces~incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=patchwork.ozlabs.org) Received: from server2.sourceware.org (server2.sourceware.org [8.43.85.97]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4X2k8l42ZGz1y1l for ; Tue, 10 Sep 2024 09:49:59 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id F1B603858435 for ; Mon, 9 Sep 2024 23:49:54 +0000 (GMT) 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.133.124]) by sourceware.org (Postfix) with ESMTP id DDBA2385C6D1 for ; Mon, 9 Sep 2024 23:48:40 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org DDBA2385C6D1 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 DDBA2385C6D1 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1725925724; cv=none; b=T9T5HVVZTYmuAJ6kNFywNFfLoMP3b/vQDH1f/hiVS5eR/+AC90Aer6DM0KpQiQ4AJJlWQ16wTrYnAxVkRM8jCQQ6K2c7sQhx96HGlKOesW6V4sRXd2swbsLMEg9K2t8It2vsT34InDOkm2G1068MDN0oXKa5I6PlaBsbI9IaSMo= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1725925724; c=relaxed/simple; bh=8HbBamxQXU4ubKMNEeoH99cMdeCElLbKmzhTfl5t7BI=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=gVE/RFyCe9Yy4Q5myXlp1A2gyH9KDHPDp4Ej9oGlG1Sqcsj88ObNBTJeO3pWrYINu2zLVDIEJLPyTE9xQg6EQ64ffRyTld8WVmgVZyUcBjmVn8vlnFZEzkuRqUXFuow2UnYmiVbTvtDy6EMlyh0DtlnK1P29fKyx+6tcRjOIus8= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1725925720; 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; bh=csM69K+AG9Kf+tm5pokt1MQlZBpvjZN3AZ2neXGpe/M=; b=in9XN/5aIzcmrvypw2EKhLriIjiXJGHs5UL7tar8PKwkoPRLNU2Nll0oG/yrMhmKmIGui3 Iq6gaZAUy1jF6FBXNk15A0ytqtgZRmS/RnZ6xQ6l5qpnw0EPHkzSJmfdmKMIREkiXcjqBP qnfaESUK08esCmlRj7YHLSXHPO9wBGk= Received: from mx-prod-mc-05.mail-002.prod.us-west-2.aws.redhat.com (ec2-54-186-198-63.us-west-2.compute.amazonaws.com [54.186.198.63]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-628-kEMUn4azNdixZ-W9GP5FEw-1; Mon, 09 Sep 2024 19:48:39 -0400 X-MC-Unique: kEMUn4azNdixZ-W9GP5FEw-1 Received: from mx-prod-int-02.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-02.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.15]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-05.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 219801956080 for ; Mon, 9 Sep 2024 23:48:37 +0000 (UTC) Received: from t14s.localdomain.com (unknown [10.22.8.230]) by mx-prod-int-02.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id 080221956086; Mon, 9 Sep 2024 23:48:35 +0000 (UTC) From: David Malcolm To: gcc-patches@gcc.gnu.org Cc: David Malcolm Subject: [pushed: r15-3555] diagnostics: replace option_hooks with a diagnostic_option_manager class Date: Mon, 9 Sep 2024 19:48:34 -0400 Message-ID: <20240909234834.586922-1-dmalcolm@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.0 on 10.30.177.15 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-11.7 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_H4, RCVD_IN_MSPIKE_WL, 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.30 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gcc-patches-bounces~incoming=patchwork.ozlabs.org@gcc.gnu.org Introduce a diagnostic_option_manager class to help isolate the diagnostics subsystem from GCC's option handling. No functional change intended. Successfully bootstrapped & regrtested on x86_64-pc-linux-gnu. Pushed to trunk as r15-3555-ga97448e92eb76a. gcc/ChangeLog: * diagnostic.cc (diagnostic_context::initialize): Replace m_options_callbacks with m_option_mgr. (diagnostic_context::set_option_hooks): Replace with... (diagnostic_context::set_option_manager): ...this. * diagnostic.h (diagnostic_option_enabled_cb): Delete. (diagnostic_make_option_name_cb): Delete. (diagnostic_make_option_url_cb): Delete. (class diagnostic_option_manager): New. (diagnostic_manager::option_enabled_p): Convert from using m_option_callbacks to m_option_mgr. (diagnostic_manager::make_option_name): Likewise. (diagnostic_manager::make_option_url): Likewise. (diagnostic_manager::set_option_hooks): Replace with... (diagnostic_manager::set_option_manager): ...this. (diagnostic_manager::get_lang_mask): Update for field changes. (diagnostic_manager::m_option_callbacks): Replace with... (diagnostic_manager::m_option_mgr): ...this and... (diagnostic_manager::m_lang_mask): ...this. * lto-wrapper.cc (class lto_diagnostic_option_manager): New. (main): Port from option hooks to diagnostic_option_manager. * opts-common.cc: Include "opts-diagnostic.h". (compiler_diagnostic_option_manager::option_enabled_p): New. * opts-diagnostic.h (option_name): Drop decl. (get_option_url): Drop decl. (class gcc_diagnostic_option_manager): New. (class compiler_diagnostic_option_manager): New. * opts.cc (option_name): Convert to... (compiler_diagnostic_option_manager::make_option_name): ...this. (get_option_url): Convert to... (gcc_diagnostic_option_manager::make_option_url): ...this. * toplev.cc (general_init): Port from option hooks to diagnostic_option_manager. Signed-off-by: David Malcolm --- gcc/diagnostic.cc | 23 ++++------- gcc/diagnostic.h | 94 +++++++++++++++++++------------------------ gcc/lto-wrapper.cc | 24 ++++++++--- gcc/opts-common.cc | 7 ++++ gcc/opts-diagnostic.h | 44 +++++++++++++++++--- gcc/opts.cc | 18 ++++----- gcc/toplev.cc | 10 ++--- 7 files changed, 126 insertions(+), 94 deletions(-) diff --git a/gcc/diagnostic.cc b/gcc/diagnostic.cc index 32eab7d5407a..0e0ab7aeb838 100644 --- a/gcc/diagnostic.cc +++ b/gcc/diagnostic.cc @@ -226,10 +226,7 @@ diagnostic_context::initialize (int n_opts) m_text_callbacks.m_begin_diagnostic = default_diagnostic_starter; m_text_callbacks.m_start_span = default_diagnostic_start_span_fn; m_text_callbacks.m_end_diagnostic = default_diagnostic_finalizer; - m_option_callbacks.m_option_enabled_cb = nullptr; - m_option_callbacks.m_option_state = nullptr; - m_option_callbacks.m_make_option_name_cb = nullptr; - m_option_callbacks.m_make_option_url_cb = nullptr; + m_option_mgr = nullptr; m_urlifier = nullptr; m_last_location = UNKNOWN_LOCATION; m_last_module = nullptr; @@ -446,18 +443,12 @@ diagnostic_context::set_original_argv (unique_argv original_argv) } void -diagnostic_context:: -set_option_hooks (diagnostic_option_enabled_cb option_enabled_cb, - void *option_state, - diagnostic_make_option_name_cb make_option_name_cb, - diagnostic_make_option_url_cb make_option_url_cb, - unsigned lang_mask) -{ - m_option_callbacks.m_option_enabled_cb = option_enabled_cb; - m_option_callbacks.m_option_state = option_state; - m_option_callbacks.m_make_option_name_cb = make_option_name_cb; - m_option_callbacks.m_make_option_url_cb = make_option_url_cb; - m_option_callbacks.m_lang_mask = lang_mask; +diagnostic_context::set_option_manager (diagnostic_option_manager *mgr, + unsigned lang_mask) +{ + delete m_option_mgr; + m_option_mgr = mgr; + m_lang_mask = lang_mask; } void diff --git a/gcc/diagnostic.h b/gcc/diagnostic.h index 4d6147b87458..7244f425936c 100644 --- a/gcc/diagnostic.h +++ b/gcc/diagnostic.h @@ -181,14 +181,34 @@ typedef void (*diagnostic_finalizer_fn) (diagnostic_context *, const diagnostic_info *, diagnostic_t); -typedef int (*diagnostic_option_enabled_cb) (int, unsigned, void *); -typedef char *(*diagnostic_make_option_name_cb) (const diagnostic_context *, - int, - diagnostic_t, - diagnostic_t); -typedef char *(*diagnostic_make_option_url_cb) (const diagnostic_context *, - int, - unsigned); +/* Abstract base class for the diagnostic subsystem to make queries + about command-line options. */ + +class diagnostic_option_manager +{ +public: + virtual ~diagnostic_option_manager () {} + + /* Return 1 if option OPT_IDX is enabled, 0 if it is disabled, + or -1 if it isn't a simple on-off switch + (or if the value is unknown, typically set later in target). */ + virtual int option_enabled_p (int opt_idx) const = 0; + + /* Return malloced memory for the name of the option OPT_IDX + which enabled a diagnostic, originally of type ORIG_DIAG_KIND but + possibly converted to DIAG_KIND by options such as -Werror. + May return NULL if no name is to be printed. + May be passed 0 as well as the index of a particular option. */ + virtual char *make_option_name (int opt_idx, + diagnostic_t orig_diag_kind, + diagnostic_t diag_kind) const = 0; + + /* Return malloced memory for a URL describing the option that controls + a diagnostic. + May return NULL if no URL is available. + May be passed 0 as well as the index of a particular option. */ + virtual char *make_option_url (int opt_idx) const = 0; +}; class edit_context; namespace json { class value; } @@ -489,43 +509,36 @@ public: /* Option-related member functions. */ inline bool option_enabled_p (int option_index) const { - if (!m_option_callbacks.m_option_enabled_cb) + if (!m_option_mgr) return true; - return m_option_callbacks.m_option_enabled_cb - (option_index, - m_option_callbacks.m_lang_mask, - m_option_callbacks.m_option_state); + return m_option_mgr->option_enabled_p (option_index); } inline char *make_option_name (int option_index, diagnostic_t orig_diag_kind, diagnostic_t diag_kind) const { - if (!m_option_callbacks.m_make_option_name_cb) + if (!m_option_mgr) return nullptr; - return m_option_callbacks.m_make_option_name_cb (this, option_index, - orig_diag_kind, - diag_kind); + return m_option_mgr->make_option_name (option_index, + orig_diag_kind, + diag_kind); } inline char *make_option_url (int option_index) const { - if (!m_option_callbacks.m_make_option_url_cb) + if (!m_option_mgr) return nullptr; - return m_option_callbacks.m_make_option_url_cb (this, option_index, - get_lang_mask ()); + return m_option_mgr->make_option_url (option_index); } void - set_option_hooks (diagnostic_option_enabled_cb option_enabled_cb, - void *option_state, - diagnostic_make_option_name_cb make_option_name_cb, - diagnostic_make_option_url_cb make_option_url_cb, - unsigned lang_mask); + set_option_manager (diagnostic_option_manager *mgr, + unsigned lang_mask); unsigned get_lang_mask () const { - return m_option_callbacks.m_lang_mask; + return m_lang_mask; } label_text get_location_text (const expanded_location &s) const; @@ -656,33 +669,8 @@ public: void (*m_adjust_diagnostic_info)(diagnostic_context *, diagnostic_info *); private: - /* Client-supplied callbacks for working with options. */ - struct { - /* Client hook to say whether the option controlling a diagnostic is - enabled. Returns nonzero if enabled, zero if disabled. */ - diagnostic_option_enabled_cb m_option_enabled_cb; - - /* Client information to pass as second argument to - m_option_enabled_cb. */ - void *m_option_state; - - /* Client hook to return the name of an option that controls a - diagnostic. Returns malloced memory. The first diagnostic_t - argument is the kind of diagnostic before any reclassification - (of warnings as errors, etc.); the second is the kind after any - reclassification. May return NULL if no name is to be printed. - May be passed 0 as well as the index of a particular option. */ - diagnostic_make_option_name_cb m_make_option_name_cb; - - /* Client hook to return a URL describing the option that controls - a diagnostic. Returns malloced memory. May return NULL if no URL - is available. May be passed 0 as well as the index of a - particular option. */ - diagnostic_make_option_url_cb m_make_option_url_cb; - - /* A copy of lang_hooks.option_lang_mask (). */ - unsigned m_lang_mask; - } m_option_callbacks; + diagnostic_option_manager *m_option_mgr; + unsigned m_lang_mask; /* An optional hook for adding URLs to quoted text strings in diagnostics. Only used for the main diagnostic message. */ diff --git a/gcc/lto-wrapper.cc b/gcc/lto-wrapper.cc index 5b6f16b9a035..11eaa1f5f789 100644 --- a/gcc/lto-wrapper.cc +++ b/gcc/lto-wrapper.cc @@ -2139,6 +2139,24 @@ cont: obstack_free (&argv_obstack, NULL); } +/* Concrete implementation of diagnostic_option_manager for LTO. */ + +class lto_diagnostic_option_manager : public gcc_diagnostic_option_manager +{ +public: + lto_diagnostic_option_manager () + : gcc_diagnostic_option_manager (0 /* lang_mask */) + { + } + int option_enabled_p (int) const final override + { + return true; + } + char *make_option_name (int, diagnostic_t, diagnostic_t) const final override + { + return nullptr; + } +}; /* Entry point. */ @@ -2161,11 +2179,7 @@ main (int argc, char *argv[]) diagnostic_initialize (global_dc, 0); diagnostic_color_init (global_dc); diagnostic_urls_init (global_dc); - global_dc->set_option_hooks (nullptr, - nullptr, - nullptr, - get_option_url, - 0); + global_dc->set_option_manager (new lto_diagnostic_option_manager (), 0); if (atexit (lto_wrapper_cleanup) != 0) fatal_error (input_location, "% failed"); diff --git a/gcc/opts-common.cc b/gcc/opts-common.cc index 3b37de80ca6a..62d7bef552e0 100644 --- a/gcc/opts-common.cc +++ b/gcc/opts-common.cc @@ -25,6 +25,7 @@ along with GCC; see the file COPYING3. If not see #include "opts.h" #include "options.h" #include "diagnostic.h" +#include "opts-diagnostic.h" #include "spellcheck.h" #include "opts-jobserver.h" @@ -1869,6 +1870,12 @@ option_enabled (int opt_idx, unsigned lang_mask, void *opts) return -1; } +int +compiler_diagnostic_option_manager::option_enabled_p (int opt_idx) const +{ + return option_enabled (opt_idx, m_lang_mask, m_opts); +} + /* Fill STATE with the current state of option OPTION in OPTS. Return true if there is some state to store. */ diff --git a/gcc/opts-diagnostic.h b/gcc/opts-diagnostic.h index a18c4c17f607..2b78ce73b1b3 100644 --- a/gcc/opts-diagnostic.h +++ b/gcc/opts-diagnostic.h @@ -20,11 +20,43 @@ along with GCC; see the file COPYING3. If not see #ifndef GCC_OPTS_DIAGNOSTIC_H #define GCC_OPTS_DIAGNOSTIC_H -extern char *option_name (const diagnostic_context *context, int option_index, - diagnostic_t orig_diag_kind, diagnostic_t diag_kind); - -extern char *get_option_url (const diagnostic_context *context, - int option_index, - unsigned lang_mask); +/* Abstract subclass of diagnostic_option_manager for gcc options. */ + +class gcc_diagnostic_option_manager : public diagnostic_option_manager +{ +public: + char *make_option_url (int opt) const final override; + +protected: + gcc_diagnostic_option_manager (unsigned lang_mask) + : m_lang_mask (lang_mask) + {} + + unsigned m_lang_mask; +}; + +/* Concrete implementation of diagnostic_option_manager for compiler. */ + +class compiler_diagnostic_option_manager : public gcc_diagnostic_option_manager +{ +public: + compiler_diagnostic_option_manager (const diagnostic_context &context, + unsigned lang_mask, + void *opts) + : gcc_diagnostic_option_manager (lang_mask), + m_context (context), + m_opts (opts) + { + } + + int option_enabled_p (int opt_idx) const final override; + char *make_option_name (int opt_idx, + diagnostic_t orig_diag_kind, + diagnostic_t diag_kind) const final override; + +private: + const diagnostic_context &m_context; + void *m_opts; +}; #endif diff --git a/gcc/opts.cc b/gcc/opts.cc index 7a1531834b0b..3e50933a0a94 100644 --- a/gcc/opts.cc +++ b/gcc/opts.cc @@ -3699,13 +3699,15 @@ enable_warning_as_error (const char *arg, int value, unsigned int lang_mask, } /* Return malloced memory for the name of the option OPTION_INDEX - which enabled a diagnostic (context CONTEXT), originally of type + which enabled a diagnostic, originally of type ORIG_DIAG_KIND but possibly converted to DIAG_KIND by options such as -Werror. */ char * -option_name (const diagnostic_context *context, int option_index, - diagnostic_t orig_diag_kind, diagnostic_t diag_kind) +compiler_diagnostic_option_manager:: +make_option_name (int option_index, + diagnostic_t orig_diag_kind, + diagnostic_t diag_kind) const { if (option_index) { @@ -3723,7 +3725,7 @@ option_name (const diagnostic_context *context, int option_index, /* A warning without option classified as an error. */ else if ((orig_diag_kind == DK_WARNING || orig_diag_kind == DK_PEDWARN || diag_kind == DK_WARNING) - && context->warning_as_error_requested_p ()) + && m_context.warning_as_error_requested_p ()) return xstrdup (cl_options[OPT_Werror].opt_text); else return NULL; @@ -3776,16 +3778,14 @@ get_option_url_suffix (int option_index, unsigned lang_mask) } /* Return malloced memory for a URL describing the option OPTION_INDEX - which enabled a diagnostic (context CONTEXT). */ + which enabled a diagnostic. */ char * -get_option_url (const diagnostic_context *, - int option_index, - unsigned lang_mask) +gcc_diagnostic_option_manager::make_option_url (int option_index) const { if (option_index) { - label_text url_suffix = get_option_url_suffix (option_index, lang_mask); + label_text url_suffix = get_option_url_suffix (option_index, m_lang_mask); if (url_suffix.get ()) return concat (DOCUMENTATION_ROOT_URL, url_suffix.get (), nullptr); } diff --git a/gcc/toplev.cc b/gcc/toplev.cc index abc66c170a55..5df59b79c803 100644 --- a/gcc/toplev.cc +++ b/gcc/toplev.cc @@ -1093,11 +1093,11 @@ general_init (const char *argv0, bool init_signals, unique_argv original_argv) (global_options_init.x_flag_diagnostics_show_highlight_colors); global_dc->m_internal_error = internal_error_function; const unsigned lang_mask = lang_hooks.option_lang_mask (); - global_dc->set_option_hooks (option_enabled, - &global_options, - option_name, - get_option_url, - lang_mask); + global_dc->set_option_manager + (new compiler_diagnostic_option_manager (*global_dc, + lang_mask, + &global_options), + lang_mask); global_dc->set_urlifier (make_gcc_urlifier (lang_mask)); if (init_signals)