]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
diagnostics: make option-handling callbacks private
authorDavid Malcolm <dmalcolm@redhat.com>
Tue, 14 Nov 2023 19:02:10 +0000 (14:02 -0500)
committerDavid Malcolm <dmalcolm@redhat.com>
Tue, 14 Nov 2023 19:02:10 +0000 (14:02 -0500)
No functional change intended.

gcc/c-family/ChangeLog:
* c-warn.cc (conversion_warning): Update call to
global_dc->m_option_enabled to use option_enabled_p.

gcc/cp/ChangeLog:
* decl.cc (finish_function): Update call to
global_dc->m_option_enabled to use option_enabled_p.

gcc/ChangeLog:
* diagnostic-format-json.cc
(json_output_format::on_end_diagnostic): Update calls to m_context
callbacks to use member functions; tighten up scopes.
* diagnostic-format-sarif.cc (sarif_builder::make_result_object):
Likewise.
(sarif_builder::make_reporting_descriptor_object_for_warning):
Likewise.
* diagnostic.cc (diagnostic_context::initialize): Update for
callbacks being moved into m_option_callbacks and being renamed.
(diagnostic_context::set_option_hooks): New.
(diagnostic_option_classifier::classify_diagnostic): Update call
to global_dc->m_option_enabled to use option_enabled_p.
(diagnostic_context::print_option_information): Update calls to
m_context callbacks to use member functions; tighten up scopes.
(diagnostic_context::diagnostic_enabled): Likewise.
* diagnostic.h (diagnostic_option_enabled_cb): New typedef.
(diagnostic_make_option_name_cb): New typedef.
(diagnostic_make_option_url_cb): New typedef.
(diagnostic_context::option_enabled_p): New.
(diagnostic_context::make_option_name): New.
(diagnostic_context::make_option_url): New.
(diagnostic_context::set_option_hooks): New decl.
(diagnostic_context::m_option_enabled): Rename to
m_option_enabled_cb and move within m_option_callbacks, using
typedef.
(diagnostic_context::m_option_state): Move within
m_option_callbacks.
(diagnostic_context::m_option_name): Rename to
m_make_option_name_cb and move within m_option_callbacks, using
typedef.
(diagnostic_context::m_get_option_url): Likewise, renaming to
m_make_option_url_cb.
* lto-wrapper.cc (print_lto_docs_link): Update call to m_context
callback to use member function.
(main): Use diagnostic_context::set_option_hooks.
* opts-diagnostic.h (option_name): Make context param const.
(get_option_url): Likewise.
* opts.cc (option_name): Likewise.
(get_option_url): Likewise.
* toplev.cc (general_init): Use
diagnostic_context::set_option_hooks.

Signed-off-by: David Malcolm <dmalcolm@redhat.com>
gcc/c-family/c-warn.cc
gcc/cp/decl.cc
gcc/diagnostic-format-json.cc
gcc/diagnostic-format-sarif.cc
gcc/diagnostic.cc
gcc/diagnostic.h
gcc/lto-wrapper.cc
gcc/opts-diagnostic.h
gcc/opts.cc
gcc/toplev.cc

index b1bd8ba9f4224f25c5f6adb0bb2d33cc036f4f2d..d2938b91043d366b640c5017a43c94e8a69f26f2 100644 (file)
@@ -1350,9 +1350,7 @@ conversion_warning (location_t loc, tree type, tree expr, tree result)
          break;
 
        if (arith_ops
-           && global_dc->m_option_enabled (warnopt,
-                                           global_dc->m_lang_mask,
-                                           global_dc->m_option_state))
+           && global_dc->option_enabled_p (warnopt))
          {
            for (int i = 0; i < arith_ops; ++i)
              {
index 4a07c7e879b1a02f3cf45e1328e0bf052422cb66..d2ed46b145318b1d677550d3fa418b1ed884b6be 100644 (file)
@@ -18308,9 +18308,7 @@ finish_function (bool inline_p)
              && current_class_ref
              && same_type_ignoring_top_level_qualifiers_p
                  (TREE_TYPE (valtype), TREE_TYPE (current_class_ref))
-             && global_dc->m_option_enabled (OPT_Wreturn_type,
-                                             global_dc->m_lang_mask,
-                                             global_dc->m_option_state))
+             && global_dc->option_enabled_p (OPT_Wreturn_type))
            add_return_star_this_fixit (&richloc, fndecl);
        }
       if (cxx_dialect >= cxx14
index 7c5a126786e6e925247bbb60a55dfaeb6d923e0f..141075b7dee8676b38678643798b4b8c9cc151cf 100644 (file)
@@ -217,24 +217,18 @@ json_output_format::on_end_diagnostic (diagnostic_info *diagnostic,
   diag_obj->set_string ("message", pp_formatted_text (m_context.printer));
   pp_clear_output_area (m_context.printer);
 
-  char *option_text;
-  option_text = m_context.m_option_name (&m_context, diagnostic->option_index,
-                                        orig_diag_kind, diagnostic->kind);
-  if (option_text)
+  if (char *option_text = m_context.make_option_name (diagnostic->option_index,
+                                                     orig_diag_kind,
+                                                     diagnostic->kind))
     {
       diag_obj->set_string ("option", option_text);
       free (option_text);
     }
 
-  if (m_context.m_get_option_url)
+  if (char *option_url = m_context.make_option_url (diagnostic->option_index))
     {
-      char *option_url = m_context.m_get_option_url (&m_context,
-                                                    diagnostic->option_index);
-      if (option_url)
-       {
-         diag_obj->set_string ("option_url", option_url);
-         free (option_url);
-       }
+      diag_obj->set_string ("option_url", option_url);
+      free (option_url);
     }
 
   /* If we've already emitted a diagnostic within this auto_diagnostic_group,
index 9510f4add80fcdd111bc2c6a195b3821be9957b6..1bb728654e6cc07690e7917336e9d2e1c28e1cad 100644 (file)
@@ -536,8 +536,8 @@ sarif_builder::make_result_object (diagnostic_context *context,
   /* "ruleId" property (SARIF v2.1.0 section 3.27.5).  */
   /* Ideally we'd have an option_name for these.  */
   if (char *option_text
-       = context->m_option_name (context, diagnostic->option_index,
-                                 orig_diag_kind, diagnostic->kind))
+       = context->make_option_name (diagnostic->option_index,
+                                    orig_diag_kind, diagnostic->kind))
     {
       /* Lazily create reportingDescriptor objects for and add to m_rules_arr.
         Set ruleId referencing them.  */
@@ -639,15 +639,10 @@ make_reporting_descriptor_object_for_warning (diagnostic_context *context,
      it seems redundant compared to "id".  */
 
   /* "helpUri" property (SARIF v2.1.0 section 3.49.12).  */
-  if (context->m_get_option_url)
+  if (char *option_url = context->make_option_url (diagnostic->option_index))
     {
-      char *option_url
-       = context->m_get_option_url (context, diagnostic->option_index);
-      if (option_url)
-       {
-         reporting_desc->set_string ("helpUri", option_url);
-         free (option_url);
-       }
+      reporting_desc->set_string ("helpUri", option_url);
+      free (option_url);
     }
 
   return reporting_desc;
index ccca2d2150f71f9835e129aee69bb38102541372..eb0df6890564685e4e9e1b824eaefc0b1bc2f33b 100644 (file)
@@ -238,10 +238,10 @@ 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_enabled = nullptr;
-  m_option_state = nullptr;
-  m_option_name = nullptr;
-  m_get_option_url = nullptr;
+  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_urlifier = nullptr;
   m_last_location = UNKNOWN_LOCATION;
   m_last_module = nullptr;
@@ -418,6 +418,19 @@ diagnostic_context::set_client_data_hooks (diagnostic_client_data_hooks *hooks)
   m_client_data_hooks = hooks;
 }
 
+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)
+{
+  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;
+}
+
 void
 diagnostic_context::set_urlifier (urlifier *urlifier)
 {
@@ -1124,9 +1137,7 @@ classify_diagnostic (const diagnostic_context *context,
       /* Record the command-line status, so we can reset it back on DK_POP. */
       if (old_kind == DK_UNSPECIFIED)
        {
-         old_kind = !context->m_option_enabled (option_index,
-                                                context->m_lang_mask,
-                                                context->m_option_state)
+         old_kind = !context->option_enabled_p (option_index)
            ? DK_IGNORED : (context->warning_as_error_requested_p ()
                            ? DK_ERROR : DK_WARNING);
          m_classify_diagnostic[option_index] = old_kind;
@@ -1412,18 +1423,12 @@ void
 diagnostic_context::print_option_information (const diagnostic_info &diagnostic,
                                              diagnostic_t orig_diag_kind)
 {
-  char *option_text;
-
-  option_text = m_option_name (this, diagnostic.option_index,
-                              orig_diag_kind, diagnostic.kind);
-
-  if (option_text)
+  if (char *option_text = make_option_name (diagnostic.option_index,
+                                           orig_diag_kind, diagnostic.kind))
     {
-      char *option_url = NULL;
-      if (m_get_option_url
-         && this->printer->url_format != URL_FORMAT_NONE)
-       option_url = m_get_option_url (this,
-                                      diagnostic.option_index);
+      char *option_url = nullptr;
+      if (this->printer->url_format != URL_FORMAT_NONE)
+       option_url = make_option_url (diagnostic.option_index);
       pretty_printer * const pp = this->printer;
       pp_string (pp, " [");
       pp_string (pp, colorize_start (pp_show_color (pp),
@@ -1458,9 +1463,7 @@ diagnostic_context::diagnostic_enabled (diagnostic_info *diagnostic)
 
   /* This tests if the user provided the appropriate -Wfoo or
      -Wno-foo option.  */
-  if (! m_option_enabled (diagnostic->option_index,
-                         m_lang_mask,
-                         m_option_state))
+  if (!option_enabled_p (diagnostic->option_index))
     return false;
 
   /* This tests for #pragma diagnostic changes.  */
index 57c5ed4f582a4657d4e320ebb700521123206560..db61b0e274771d68e27826bfb393914276df3e8c 100644 (file)
@@ -179,6 +179,14 @@ typedef void (*diagnostic_finalizer_fn) (diagnostic_context *,
                                         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);
+
 class edit_context;
 namespace json { class value; }
 class diagnostic_client_data_hooks;
@@ -491,6 +499,41 @@ public:
     return m_diagnostic_count[kind];
   }
 
+  /* Option-related member functions.  */
+  inline bool option_enabled_p (int option_index) const
+  {
+    if (!m_option_callbacks.m_option_enabled_cb)
+      return true;
+    return m_option_callbacks.m_option_enabled_cb
+      (option_index,
+       m_lang_mask,
+       m_option_callbacks.m_option_state);
+  }
+
+  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)
+      return nullptr;
+    return m_option_callbacks.m_make_option_name_cb (this, 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)
+      return nullptr;
+    return m_option_callbacks.m_make_option_url_cb (this, 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);
+
 private:
   bool includes_seen_p (const line_map_ordinary *map);
 
@@ -606,32 +649,32 @@ public:
   /* Client hook to report an internal error.  */
   void (*m_internal_error) (diagnostic_context *, const char *, va_list *);
 
-  /* Client hook to say whether the option controlling a diagnostic is
-     enabled.  Returns nonzero if enabled, zero if disabled.  */
-  int (*m_option_enabled) (int, unsigned, void *);
-
-  /* Client information to pass as second argument to
-     option_enabled.  */
-  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.  */
-  char *(*m_option_name) (diagnostic_context *,
-                         int,
-                         diagnostic_t,
-                         diagnostic_t);
-
-  /* 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.  */
-  char *(*m_get_option_url) (diagnostic_context *, int);
-
 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;
+  } m_option_callbacks;
+
   /* An optional hook for adding URLs to quoted text strings in
      diagnostics.  Only used for the main diagnostic message.  */
   urlifier *m_urlifier;
index a92fed3d25b0556b0e964a217b4d366519fe849b..7c76635ef213e2871b5580f580a0b35373c064db 100644 (file)
@@ -1355,7 +1355,7 @@ void
 print_lto_docs_link ()
 {
   bool print_url = global_dc->printer->url_format != URL_FORMAT_NONE;
-  const char *url = global_dc->m_get_option_url (global_dc, OPT_flto);
+  const char *url = global_dc->make_option_url (OPT_flto);
 
   pretty_printer pp;
   pp.url_format = URL_FORMAT_DEFAULT;
@@ -2146,7 +2146,10 @@ main (int argc, char *argv[])
   diagnostic_initialize (global_dc, 0);
   diagnostic_color_init (global_dc);
   diagnostic_urls_init (global_dc);
-  global_dc->m_get_option_url = get_option_url;
+  global_dc->set_option_hooks (nullptr,
+                              nullptr,
+                              nullptr,
+                              get_option_url);
 
   if (atexit (lto_wrapper_cleanup) != 0)
     fatal_error (input_location, "%<atexit%> failed");
index b36fb46cc929217b3cb390dc44f3e86656cb0cc8..8c3b695f07a234bd579cfcdc4cb6495aeea71110 100644 (file)
@@ -20,9 +20,10 @@ along with GCC; see the file COPYING3.  If not see
 #ifndef GCC_OPTS_DIAGNOSTIC_H
 #define GCC_OPTS_DIAGNOSTIC_H
 
-extern char *option_name (diagnostic_context *context, int option_index,
+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 (diagnostic_context *context, int option_index);
+extern char *get_option_url (const diagnostic_context *context,
+                            int option_index);
 
 #endif
index 32fdfc3e3ce194c787f5151aa57362526d153952..33165c9e74ffa5c643c17ff7223c0d2ac2ff00f9 100644 (file)
@@ -3615,7 +3615,7 @@ enable_warning_as_error (const char *arg, int value, unsigned int lang_mask,
    as -Werror.  */
 
 char *
-option_name (diagnostic_context *context, int option_index,
+option_name (const diagnostic_context *context, int option_index,
             diagnostic_t orig_diag_kind, diagnostic_t diag_kind)
 {
   if (option_index)
@@ -3674,7 +3674,7 @@ get_option_html_page (int option_index)
    which enabled a diagnostic (context CONTEXT).  */
 
 char *
-get_option_url (diagnostic_context *, int option_index)
+get_option_url (const diagnostic_context *, int option_index)
 {
   if (option_index)
     return concat (/* DOCUMENTATION_ROOT_URL should be supplied via
index d8e8978dd5557e7b0e4b4ed8b2ec8b8e8c486d14..ae2f3d52602b4d1fc3cb8643403bb2865cbbe8ce 100644 (file)
@@ -1045,10 +1045,10 @@ general_init (const char *argv0, bool init_signals)
   global_dc->m_show_column
     = global_options_init.x_flag_show_column;
   global_dc->m_internal_error = internal_error_function;
-  global_dc->m_option_enabled = option_enabled;
-  global_dc->m_option_state = &global_options;
-  global_dc->m_option_name = option_name;
-  global_dc->m_get_option_url = get_option_url;
+  global_dc->set_option_hooks (option_enabled,
+                              &global_options,
+                              option_name,
+                              get_option_url);
   global_dc->set_urlifier (make_gcc_urlifier ());
 
   if (init_signals)