]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
diagnostics: fix corrupt json/SARIF on stderr [PR114348]
authorDavid Malcolm <dmalcolm@redhat.com>
Tue, 19 Mar 2024 17:57:35 +0000 (13:57 -0400)
committerDavid Malcolm <dmalcolm@redhat.com>
Tue, 19 Mar 2024 17:57:35 +0000 (13:57 -0400)
Various values of -fdiagnostics-format= request machine-readable output
on stderr, using JSON, but in various places we use fnotice to write
free-form text to stderr, such as "compilation terminated", leading to
corrupt JSON.

Fix by having fnotice skip the output for such cases.

gcc/ChangeLog:
PR middle-end/114348
* diagnostic-format-json.cc
(json_stderr_output_format::machine_readable_stderr_p): New.
(json_file_output_format::machine_readable_stderr_p): New.
* diagnostic-format-sarif.cc
(sarif_stream_output_format::machine_readable_stderr_p): New.
(sarif_file_output_format::machine_readable_stderr_p): New.
* diagnostic.cc (diagnostic_context::action_after_output): Move
"fnotice" to before "finish" call, so that we still have the
diagnostic_context.
(fnotice): Bail out if the user requested one of the
machine-readable diagnostic output formats on stderr.
* diagnostic.h
(diagnostic_output_format::machine_readable_stderr_p): New pure
virtual function.
(diagnostic_text_output_format::machine_readable_stderr_p): New.
(diagnostic_context::get_output_format): New accessor.

Signed-off-by: David Malcolm <dmalcolm@redhat.com>
gcc/diagnostic-format-json.cc
gcc/diagnostic-format-sarif.cc
gcc/diagnostic.cc
gcc/diagnostic.h

index 51e016b64638d019ba2c596439ba0e12285be4d6..0782ae831ebaaef41ede2f2771df7acba568f8ff 100644 (file)
@@ -314,6 +314,10 @@ public:
   {
     flush_to_file (stderr);
   }
+  bool machine_readable_stderr_p () const final override
+  {
+    return true;
+  }
 };
 
 class json_file_output_format : public json_output_format
@@ -345,6 +349,10 @@ public:
     fclose (outf);
     free (filename);
   }
+  bool machine_readable_stderr_p () const final override
+  {
+    return false;
+  }
 
 private:
   char *m_base_file_name;
index 307b2f56c2801053a898960d5011b44e75a3cebb..97c5943cd3397e45ad075f7d2638e88dfc5bdd5f 100644 (file)
@@ -1750,6 +1750,10 @@ public:
   {
     m_builder.flush_to_file (m_stream);
   }
+  bool machine_readable_stderr_p () const final override
+  {
+    return m_stream == stderr;
+  }
 private:
   FILE *m_stream;
 };
@@ -1782,6 +1786,10 @@ public:
     fclose (outf);
     free (filename);
   }
+  bool machine_readable_stderr_p () const final override
+  {
+    return false;
+  }
 
 private:
   char *m_base_file_name;
index 1d143fa7498083d7f1435462cdf2dbeb513fe1a1..8e4621f8031aa799d497575f149b618ff4f7f807 100644 (file)
@@ -802,8 +802,8 @@ diagnostic_context::action_after_output (diagnostic_t diag_kind)
     case DK_FATAL:
       if (m_abort_on_error)
        real_abort ();
-      finish ();
       fnotice (stderr, "compilation terminated.\n");
+      finish ();
       exit (FATAL_EXIT_CODE);
 
     default:
@@ -2264,6 +2264,16 @@ diagnostic_context::emit_diagram (const diagnostic_diagram &diagram)
 void
 fnotice (FILE *file, const char *cmsgid, ...)
 {
+  /* If the user requested one of the machine-readable diagnostic output
+     formats on stderr (e.g. -fdiagnostics-format=sarif-stderr), then
+     emitting free-form text on stderr will lead to corrupt output.
+     Skip the message for such cases.  */
+  if (file == stderr && global_dc)
+    if (const diagnostic_output_format *output_format
+         = global_dc->get_output_format ())
+      if (output_format->machine_readable_stderr_p ())
+       return;
+
   va_list ap;
 
   va_start (ap, cmsgid);
index 0a7c7e02b37c1eec096b2d97ca7793c0193b4cf6..065ac784e2587155840b26e2e786832f2c1ff1e2 100644 (file)
@@ -210,6 +210,7 @@ public:
   virtual void on_end_diagnostic (const diagnostic_info &,
                                  diagnostic_t orig_diag_kind) = 0;
   virtual void on_diagram (const diagnostic_diagram &diagram) = 0;
+  virtual bool machine_readable_stderr_p () const = 0;
 
 protected:
   diagnostic_output_format (diagnostic_context &context)
@@ -238,6 +239,10 @@ public:
   void on_end_diagnostic (const diagnostic_info &,
                          diagnostic_t orig_diag_kind) override;
   void on_diagram (const diagnostic_diagram &diagram) override;
+  bool machine_readable_stderr_p () const final override
+  {
+    return false;
+  }
 };
 
 /* A stack of sets of classifications: each entry in the stack is
@@ -432,6 +437,11 @@ public:
 
   void emit_diagram (const diagnostic_diagram &diagram);
 
+  const diagnostic_output_format *get_output_format () const
+  {
+    return m_output_format;
+  }
+
   /* Various setters for use by option-handling logic.  */
   void set_output_format (diagnostic_output_format *output_format);
   void set_text_art_charset (enum diagnostic_text_art_charset charset);