diff mbox series

[pushed:,r15-3555] diagnostics: replace option_hooks with a diagnostic_option_manager class

Message ID 20240909234834.586922-1-dmalcolm@redhat.com
State New
Headers show
Series [pushed:,r15-3555] diagnostics: replace option_hooks with a diagnostic_option_manager class | expand

Commit Message

David Malcolm Sept. 9, 2024, 11:48 p.m. UTC
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 <dmalcolm@redhat.com>
---
 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 mbox series

Patch

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, "%<atexit%> 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)