]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
diagnostics: fix build on hosts where unsigned == size_t
authorDavid Malcolm <dmalcolm@redhat.com>
Sat, 9 Aug 2025 13:15:04 +0000 (09:15 -0400)
committerDavid Malcolm <dmalcolm@redhat.com>
Sat, 9 Aug 2025 13:15:04 +0000 (09:15 -0400)
Looks like I broke the build with r16-3091-gac4e7455a33237 on hosts
where unsigned == size_t.

../../gcc/gcc/diagnostics/dumping.cc:98:1: error: redefinition of ‘void diagnostics::dumping::emit_field(FILE*, int, const char*, T) [with T = unsigned int; FILE = FILE]’
   98 | emit_field<unsigned> (FILE *outfile, int indent,
      | ^~~~~~~~~~~~~~~~~~~~
../../gcc/gcc/diagnostics/dumping.cc:80:1: note: ‘void diagnostics::dumping::emit_field(FILE*, int, const char*, T) [with T = unsigned int; FILE = FILE]’ previously declared here
   80 | emit_field<size_t> (FILE *outfile, int indent,
      | ^~~~~~~~~~~~~~~~~~

Sorry about this.

Should be fixed by the following patch, which avoids templates here
in favor of being explicit about types, avoids the use of "%zi" with
fprintf in various places, and fixes some other minor issues in the
dumping logic that I noticed whilst testing the patch.

gcc/ChangeLog:
* diagnostics/context.cc (context::dump): Bulletproof against
m_reference_printer being null.
* diagnostics/dumping.cc (emit_field<const char *>): Replace
with...
(emit_string_field): ...this.
(emit_field<char *>): Eliminate.
(emit_field<bool>): Replace with...
(emit_bool_field): ...this.
(emit_field<size_t>): Replace with...
(emit_size_t_field): ...this, and use HOST_SIZE_T_PRINT_DEC rather
than %zi in fprintf call.
(emit_field<int>): Replace with...
(emit_int_field): ...this.
(emit_field<unsigned>): Replace with...
(emit_unsigned_field): ...this.
* diagnostics/dumping.h (emit_field): Replace this template decl
with...
(emit_string_field): ...this,
(emit_bool_field): ...this,
(emit_size_t_field): ...this,
(emit_int_field): ...this,
(emit_unsigned_field): ... and this.
(DIAGNOSTICS_DUMPING_EMIT_FIELD): Rename to...
(DIAGNOSTICS_DUMPING_EMIT_BOOL_FIELD): ...this and update for
above change.
* diagnostics/file-cache.cc (file_cache_slot::dump): Replace
emit_field calls with calls that explicitly state the type.  Fix
type of dump of m_missing_trailing_newline to use bool.
(file_cache_slot::dump): Use HOST_SIZE_T_PRINT_DEC rather than
%zi in fprintf call.
* diagnostics/html-sink.cc (html_generation_options::dump): Update
for macro renaming.
* diagnostics/sarif-sink.cc
(sarif_serialization_format_json::dump): Likewise.
(sarif_generation_options::dump): Likewise, and for function
renaming.
* diagnostics/text-sink.cc (text_sink::dump): Update for macro
renaming.
* libgdiagnostics.cc (diagnostic_manager_debug_dump_file): Use
HOST_SIZE_T_PRINT_DEC rather than %zi in fprintf call.
* pretty-print.cc: Include "diagnostics/dumping.h".
(pp_formatted_chunks::dump): Use it.
(get_url_format_as_string): New.
(pretty_printer::dump): Use diagnostics::dumping.  Bulletproof
against m_buffer being null.

Signed-off-by: David Malcolm <dmalcolm@redhat.com>
gcc/diagnostics/context.cc
gcc/diagnostics/dumping.cc
gcc/diagnostics/dumping.h
gcc/diagnostics/file-cache.cc
gcc/diagnostics/html-sink.cc
gcc/diagnostics/sarif-sink.cc
gcc/diagnostics/text-sink.cc
gcc/libgdiagnostics.cc
gcc/pretty-print.cc

index c948246e55db65b48ccf08d3e03e36c28f23e8db..3668958355f4c1d80c601656455791b017cf93e6 100644 (file)
@@ -391,7 +391,10 @@ context::dump (FILE *outfile) const
   dumping::emit_heading (outfile, 0, "diagnostics::context");
   m_diagnostic_counters.dump (outfile, 2);
   dumping::emit_heading (outfile, 2, "reference printer");
-  m_reference_printer->dump (outfile, 4);
+  if (m_reference_printer)
+    m_reference_printer->dump (outfile, 4);
+  else
+    dumping::emit_none (outfile, 4);
   dumping::emit_heading (outfile, 2, "output sinks");
   if (m_sinks.length () > 0)
     {
index f0366a525c916e32f9ff38ddb5290547e8f93082..1dbecf47733ffeb5d49a6917a4de15588c6b21c7 100644 (file)
@@ -45,58 +45,44 @@ emit_heading (FILE *outfile, int indent,
   fprintf (outfile, "%s:\n", text);
 }
 
-/* Various specializattions that emit an indented line to OUTFILE
+/* Various functions that emit an indented line to OUTFILE
    showing "label: value".  */
 
-template <>
 void
-emit_field<const char *> (FILE *outfile, int indent,
-                         const char *label, const char *value)
+emit_string_field (FILE *outfile, int indent,
+                     const char *label, const char *value)
 {
   emit_indent (outfile, indent);
   fprintf (outfile, "%s: %s\n", label, value);
 }
 
-template <>
 void
-emit_field<char *> (FILE *outfile, int indent,
-                   const char *label, char *value)
+emit_bool_field (FILE *outfile, int indent,
+                const char *label, bool value)
 {
-  emit_indent (outfile, indent);
-  fprintf (outfile, "%s: %s\n", label, value);
-}
-
-template <>
-void
-emit_field<bool> (FILE *outfile, int indent,
-                 const char *label, bool value)
-{
-  emit_field<const char *> (outfile, indent, label,
-                           value ? "true" : "false");
+  emit_string_field (outfile, indent, label,
+                    value ? "true" : "false");
 }
 
-template <>
 void
-emit_field<size_t> (FILE *outfile, int indent,
-                   const char *label, size_t value)
+emit_size_t_field (FILE *outfile, int indent,
+                  const char *label, size_t value)
 {
   emit_indent (outfile, indent);
-  fprintf (outfile, "%s: %zi\n", label, value);
+  fprintf (outfile, "%s: " HOST_SIZE_T_PRINT_DEC "\n", label, value);
 }
 
-template <>
 void
-emit_field<int> (FILE *outfile, int indent,
-                const char *label, int value)
+emit_int_field (FILE *outfile, int indent,
+               const char *label, int value)
 {
   emit_indent (outfile, indent);
   fprintf (outfile, "%s: %i\n", label, value);
 }
 
-template <>
 void
-emit_field<unsigned> (FILE *outfile, int indent,
-                     const char *label, unsigned value)
+emit_unsigned_field (FILE *outfile, int indent,
+                    const char *label, unsigned value)
 {
   emit_indent (outfile, indent);
   fprintf (outfile, "%s: %u\n", label, value);
index 08c7ee41b64b4435615b4b13a44b819be20f718f..02f648548a5cc36b124f3fb5e74c4cceac8e45e1 100644 (file)
@@ -28,14 +28,21 @@ extern void emit_indent (FILE *outfile, int indent);
 extern void emit_heading (FILE *outfile, int indent,
                          const char *text);
 
-template <typename T>
-extern void emit_field (FILE *outfile, int indent,
-                       const char *label, T value);
+extern void emit_string_field (FILE *outfile, int indent,
+                              const char *label, const char *value);
+extern void emit_bool_field (FILE *outfile, int indent,
+                               const char *label, bool value);
+extern void emit_size_t_field (FILE *outfile, int indent,
+                              const char *label, size_t value);
+extern void emit_int_field (FILE *outfile, int indent,
+                           const char *label, int value);
+extern void emit_unsigned_field (FILE *outfile, int indent,
+                                const char *label, unsigned value);
 
 extern void emit_none (FILE *outfile, int indent);
 
-#define DIAGNOSTICS_DUMPING_EMIT_FIELD(FLAG) \
-  dumping::emit_field (outfile, indent, #FLAG, FLAG)
+#define DIAGNOSTICS_DUMPING_EMIT_BOOL_FIELD(FLAG) \
+  dumping::emit_bool_field (outfile, indent, #FLAG, FLAG)
 
 } // namespace dumping
 } // namespace diagnostics
index 9acf82ebbbc32dfb8bf492548f8ea099d46bee5e..0ec06798aeaba20ee6bf76aef58e433ad5894e6b 100644 (file)
@@ -547,20 +547,20 @@ file_cache_slot::dump (FILE *out, int indent) const
       fprintf (out, "(unused)\n");
       return;
     }
-  dumping::emit_field (out, indent, "file_path", m_file_path);
+  dumping::emit_string_field (out, indent, "file_path", m_file_path);
   {
     dumping::emit_indent (out, indent);
     fprintf (out, "fp: %p\n", (void *)m_fp);
   }
-  dumping::emit_field (out, indent, "needs_read_p", needs_read_p ());
-  dumping::emit_field (out, indent, "needs_grow_p", needs_grow_p ());
-  dumping::emit_field (out, indent, "use_count", m_use_count);
-  dumping::emit_field (out, indent, "size", m_size);
-  dumping::emit_field (out, indent, "nb_read", m_nb_read);
-  dumping::emit_field (out, indent, "start_line_idx", m_line_start_idx);
-  dumping::emit_field (out, indent, "line_num", m_line_num);
-  dumping::emit_field (out, indent, "missing_trailing_newline",
-                      (int)m_missing_trailing_newline);
+  dumping::emit_bool_field (out, indent, "needs_read_p", needs_read_p ());
+  dumping::emit_bool_field (out, indent, "needs_grow_p", needs_grow_p ());
+  dumping::emit_unsigned_field (out, indent, "use_count", m_use_count);
+  dumping::emit_size_t_field (out, indent, "size", m_size);
+  dumping::emit_size_t_field (out, indent, "nb_read", m_nb_read);
+  dumping::emit_size_t_field (out, indent, "start_line_idx", m_line_start_idx);
+  dumping::emit_size_t_field (out, indent, "line_num", m_line_num);
+  dumping::emit_bool_field (out, indent, "missing_trailing_newline",
+                           m_missing_trailing_newline);
   {
     dumping::emit_indent (out, indent);
     fprintf (out, "line records (%i):\n", m_line_record.length ());
@@ -569,7 +569,10 @@ file_cache_slot::dump (FILE *out, int indent) const
   for (auto &line : m_line_record)
     {
       dumping::emit_indent (out, indent);
-      fprintf (out, "[%i]: line %zi: byte offsets: %zi-%zi\n",
+      fprintf (out, ("[%i]:"
+                    " line " HOST_SIZE_T_PRINT_DEC ":"
+                    " byte offsets: " HOST_SIZE_T_PRINT_DEC
+                    "-" HOST_SIZE_T_PRINT_DEC "\n"),
               idx++, line.line_num, line.start_pos, line.end_pos);
     }
 }
index 448d4613e9054ba1c8e1f29cd23aa7cce8123a4c..1fd317a8b808bb450474348830c80ba9584e491a 100644 (file)
@@ -65,11 +65,11 @@ html_generation_options::html_generation_options ()
 void
 html_generation_options::dump (FILE *outfile, int indent) const
 {
-  DIAGNOSTICS_DUMPING_EMIT_FIELD (m_css);
-  DIAGNOSTICS_DUMPING_EMIT_FIELD (m_javascript);
-  DIAGNOSTICS_DUMPING_EMIT_FIELD (m_show_state_diagrams);
-  DIAGNOSTICS_DUMPING_EMIT_FIELD (m_show_state_diagrams_sarif);
-  DIAGNOSTICS_DUMPING_EMIT_FIELD (m_show_state_diagrams_dot_src);
+  DIAGNOSTICS_DUMPING_EMIT_BOOL_FIELD (m_css);
+  DIAGNOSTICS_DUMPING_EMIT_BOOL_FIELD (m_javascript);
+  DIAGNOSTICS_DUMPING_EMIT_BOOL_FIELD (m_show_state_diagrams);
+  DIAGNOSTICS_DUMPING_EMIT_BOOL_FIELD (m_show_state_diagrams_sarif);
+  DIAGNOSTICS_DUMPING_EMIT_BOOL_FIELD (m_show_state_diagrams_dot_src);
 }
 
 class html_builder;
index 0c4d77a7947ea3b69baeded705541e060c704833..7526c16be08b1838ad9db7b9574ac1e6d428d8f5 100644 (file)
@@ -710,7 +710,7 @@ sarif_serialization_format_json::dump (FILE *outfile, int indent) const
 {
   dumping::emit_indent (outfile, indent);
   fprintf (outfile, "json\n");
-  DIAGNOSTICS_DUMPING_EMIT_FIELD (m_formatted);
+  DIAGNOSTICS_DUMPING_EMIT_BOOL_FIELD (m_formatted);
 }
 
 /* A class for managing SARIF output (for -fdiagnostics-format=sarif-stderr
@@ -4360,10 +4360,10 @@ get_dump_string_for_sarif_version (enum sarif_version version)
 void
 sarif_generation_options::dump (FILE *outfile, int indent) const
 {
-  dumping::emit_field (outfile, indent,
-                      "m_version",
-                      get_dump_string_for_sarif_version (m_version));
-  DIAGNOSTICS_DUMPING_EMIT_FIELD (m_state_graph);
+  dumping::emit_string_field (outfile, indent,
+                             "m_version",
+                             get_dump_string_for_sarif_version (m_version));
+  DIAGNOSTICS_DUMPING_EMIT_BOOL_FIELD (m_state_graph);
 }
 
 #if CHECKING_P
index a7ebb305cefaad4c7bd2fe6a7275bc12ad101162..48b369c8af6307cbe475c50c9abea118c30e3f44 100644 (file)
@@ -159,10 +159,10 @@ text_sink::~text_sink ()
 void
 text_sink::dump (FILE *outfile, int indent) const
 {
-  DIAGNOSTICS_DUMPING_EMIT_FIELD (m_follows_reference_printer);
-  DIAGNOSTICS_DUMPING_EMIT_FIELD (m_show_nesting);
-  DIAGNOSTICS_DUMPING_EMIT_FIELD (m_show_locations_in_nesting);
-  DIAGNOSTICS_DUMPING_EMIT_FIELD (m_show_nesting_levels);
+  DIAGNOSTICS_DUMPING_EMIT_BOOL_FIELD (m_follows_reference_printer);
+  DIAGNOSTICS_DUMPING_EMIT_BOOL_FIELD (m_show_nesting);
+  DIAGNOSTICS_DUMPING_EMIT_BOOL_FIELD (m_show_locations_in_nesting);
+  DIAGNOSTICS_DUMPING_EMIT_BOOL_FIELD (m_show_nesting_levels);
 
   sink::dump (outfile, indent);
   dumping::emit_heading (outfile, indent, "saved_output_buffer");
index 784e281a4902f2abac155273b05a1624f3e21d34..d87dd46cb5c19d64651ca47e0ab9805f9fe2e31c 100644 (file)
@@ -1939,7 +1939,8 @@ diagnostic_manager_debug_dump_file (diagnostic_manager *,
        fprintf (out, ", sarif_source_language=\"%s\"",
                 file->get_sarif_source_language ());
       if (const content_buffer *buf = file->get_content ())
-       fprintf (out, ", content=(size=%zi)", buf->m_sz);
+       fprintf (out, ", content=(size=" HOST_SIZE_T_PRINT_DEC ")",
+                buf->m_sz);
       fprintf (out, ")");
     }
   else
index 77d40ecd07f46ffa76bff7e26401e314364f5512..d79a8282cfbba9e250be807b8918b40cb8c3d7b9 100644 (file)
@@ -29,6 +29,7 @@ along with GCC; see the file COPYING3.  If not see
 #include "pretty-print-urlifier.h"
 #include "diagnostics/color.h"
 #include "diagnostics/event-id.h"
+#include "diagnostics/dumping.h"
 #include "diagnostic-highlight-colors.h"
 #include "auto-obstack.h"
 #include "selftest.h"
@@ -1600,9 +1601,8 @@ pp_formatted_chunks::dump (FILE *out, int indent) const
 {
   for (size_t idx = 0; m_args[idx]; ++idx)
     {
-      fprintf (out, "%*s%i: ",
-              indent, "",
-              (int)idx);
+      diagnostics::dumping::emit_indent (out, indent);
+      fprintf (out, "%i: ", (int)idx);
       m_args[idx]->dump (out);
     }
 }
@@ -3100,34 +3100,39 @@ pretty_printer::end_url ()
     pp_string (this, get_end_url_string (this));
 }
 
-/* Dump state of this pretty_printer to OUT, for debugging.  */
-
-void
-pretty_printer::dump (FILE *out, int indent) const
+static const char *
+get_url_format_as_string (diagnostic_url_format url_format)
 {
-  fprintf (out, "%*sm_show_color: %s\n",
-          indent, "",
-          m_show_color ? "true" : "false");
-
-  fprintf (out, "%*sm_url_format: ", indent, "");
-  switch (m_url_format)
+  switch (url_format)
     {
     case URL_FORMAT_NONE:
-      fprintf (out, "none");
-      break;
+      return "none";
     case URL_FORMAT_ST:
-      fprintf (out, "st");
-      break;
+      return "st";
     case URL_FORMAT_BEL:
-      fprintf (out, "bel");
-      break;
+      return "bel";
     default:
       gcc_unreachable ();
     }
-  fprintf (out, "\n");
+}
+
+/* Dump state of this pretty_printer to OUT, for debugging.  */
 
-  fprintf (out, "%*sm_buffer:\n", indent, "");
-  m_buffer->dump (out, indent + 2);
+void
+pretty_printer::dump (FILE *outfile, int indent) const
+{
+  namespace dumping = diagnostics::dumping;
+
+  DIAGNOSTICS_DUMPING_EMIT_BOOL_FIELD (m_show_color);
+  dumping::emit_string_field
+    (outfile, indent,
+     "m_url_format",
+     get_url_format_as_string (m_url_format));
+  dumping::emit_heading (outfile, indent, "m_buffer");
+  if (m_buffer)
+    m_buffer->dump (outfile, indent + 2);
+  else
+    dumping::emit_none (outfile, indent + 2);
 }
 
 /* class pp_markup::context.  */