From 755c72478dc5ba8f6920c9ca27559f5d426a41f5 Mon Sep 17 00:00:00 2001
From: Antoni Boucher <bouanto@zoho.com>
Date: Mon, 12 Feb 2024 19:49:43 -0400
Subject: [PATCH] libgccjit: Do not treat warnings as errors

gcc/jit/ChangeLog:

	* jit-playback.cc (add_error, add_error_va): Send DK_ERROR to
	add_error_va.
	(add_diagnostic): Call add_diagnostic instead of add_error.
	* jit-recording.cc (DEFINE_DIAGNOSTIC_KIND): New define.
	(recording::context::add_diagnostic): New function.
	(recording::context::add_error): Send DK_ERROR to add_error_va.
	(recording::context::add_error_va): New parameter diagnostic_kind.
	* jit-recording.h (add_diagnostic): New function.
	(add_error_va): New parameter diagnostic_kind.
	* libgccjit.cc (jit_error): Send DK_ERROR to add_error_va.

gcc/testsuite/ChangeLog:

	* jit.dg/test-error-array-bounds.c: Fix test.
---
 gcc/jit/jit-playback.cc                       | 13 ++++---
 gcc/jit/jit-recording.cc                      | 35 +++++++++++++++----
 gcc/jit/jit-recording.h                       | 11 ++++--
 gcc/jit/libgccjit.cc                          |  2 +-
 .../jit.dg/test-error-array-bounds.c          | 10 ++----
 5 files changed, 48 insertions(+), 23 deletions(-)

diff --git a/gcc/jit/jit-playback.cc b/gcc/jit/jit-playback.cc
index b49bc94442a..6b0522d6f88 100644
--- a/gcc/jit/jit-playback.cc
+++ b/gcc/jit/jit-playback.cc
@@ -3941,7 +3941,7 @@ add_error (location *loc, const char *fmt, ...)
   va_list ap;
   va_start (ap, fmt);
   m_recording_ctxt->add_error_va (loc ? loc->get_recording_loc () : NULL,
-				  fmt, ap);
+				  DK_ERROR, fmt, ap);
   va_end (ap);
 }
 
@@ -3953,13 +3953,12 @@ playback::context::
 add_error_va (location *loc, const char *fmt, va_list ap)
 {
   m_recording_ctxt->add_error_va (loc ? loc->get_recording_loc () : NULL,
-				  fmt, ap);
+				  DK_ERROR, fmt, ap);
 }
 
-/* Report a diagnostic up to the jit context as an error,
-   so that the compilation is treated as a failure.
-   For now, any kind of diagnostic is treated as an error by the jit
-   API.  */
+/* Report a diagnostic up to the jit context, so that the
+   compilation is treated as a failure if the diagnostic
+   is an error.  */
 
 void
 playback::context::
@@ -3989,7 +3988,7 @@ add_diagnostic (diagnostic_context *diag_context,
 						  false);
     }
 
-  m_recording_ctxt->add_error (rec_loc, "%s", text);
+  m_recording_ctxt->add_diagnostic (rec_loc, diagnostic.kind, "%s", text);
   pp_clear_output_area (pp);
 }
 
diff --git a/gcc/jit/jit-recording.cc b/gcc/jit/jit-recording.cc
index 4b8e40ec64c..83a8b299b91 100644
--- a/gcc/jit/jit-recording.cc
+++ b/gcc/jit/jit-recording.cc
@@ -31,6 +31,14 @@ along with GCC; see the file COPYING3.  If not see
 #include "jit-playback.h"
 #include <sstream>
 
+/* This comes from diagnostic.cc.  */
+static const char *const diagnostic_kind_text[] = {
+#define DEFINE_DIAGNOSTIC_KIND(K, T, C) (T),
+#include "diagnostic.def"
+#undef DEFINE_DIAGNOSTIC_KIND
+  "must-not-happen"
+};
+
 namespace gcc {
 namespace jit {
 
@@ -1664,12 +1672,22 @@ recording::context::get_target_info ()
 /* Format the given error using printf's conventions, print
    it to stderr, and add it to the context.  */
 
+void
+recording::context::add_diagnostic (location *loc, diagnostic_t diagnostic_kind,
+				    const char *fmt, ...)
+{
+  va_list ap;
+  va_start (ap, fmt);
+  add_error_va (loc, diagnostic_kind, fmt, ap);
+  va_end (ap);
+}
+
 void
 recording::context::add_error (location *loc, const char *fmt, ...)
 {
   va_list ap;
   va_start (ap, fmt);
-  add_error_va (loc, fmt, ap);
+  add_error_va (loc, DK_ERROR, fmt, ap);
   va_end (ap);
 }
 
@@ -1677,7 +1695,8 @@ recording::context::add_error (location *loc, const char *fmt, ...)
    it to stderr, and add it to the context.  */
 
 void
-recording::context::add_error_va (location *loc, const char *fmt, va_list ap)
+recording::context::add_error_va (location *loc, diagnostic_t diagnostic_kind,
+				  const char *fmt, va_list ap)
 {
   int len;
   char *malloced_msg;
@@ -1698,7 +1717,8 @@ recording::context::add_error_va (location *loc, const char *fmt, va_list ap)
       has_ownership = true;
     }
   if (get_logger ())
-    get_logger ()->log ("error %i: %s", m_error_count, errmsg);
+    get_logger ()->log ("%s %i: %s", diagnostic_kind_text[diagnostic_kind],
+			m_error_count, errmsg);
 
   const char *ctxt_progname =
     get_str_option (GCC_JIT_STR_OPTION_PROGNAME);
@@ -1710,13 +1730,15 @@ recording::context::add_error_va (location *loc, const char *fmt, va_list ap)
   if (print_errors_to_stderr)
   {
     if (loc)
-      fprintf (stderr, "%s: %s: error: %s\n",
+      fprintf (stderr, "%s: %s: %s: %s\n",
 	       ctxt_progname,
 	       loc->get_debug_string (),
+	       diagnostic_kind_text[diagnostic_kind],
 	       errmsg);
     else
-      fprintf (stderr, "%s: error: %s\n",
+      fprintf (stderr, "%s: %s: %s\n",
 	       ctxt_progname,
+	       diagnostic_kind_text[diagnostic_kind],
 	       errmsg);
   }
 
@@ -1732,7 +1754,8 @@ recording::context::add_error_va (location *loc, const char *fmt, va_list ap)
   m_last_error_str = const_cast <char *> (errmsg);
   m_owns_last_error_str = has_ownership;
 
-  m_error_count++;
+  if (diagnostic_kind == DK_ERROR)
+    m_error_count++;
 }
 
 /* Get the message for the first error that occurred on this context, or
diff --git a/gcc/jit/jit-recording.h b/gcc/jit/jit-recording.h
index c47f485dcdc..4833b2d3f52 100644
--- a/gcc/jit/jit-recording.h
+++ b/gcc/jit/jit-recording.h
@@ -23,6 +23,7 @@ along with GCC; see the file COPYING3.  If not see
 
 #include "jit-common.h"
 #include "jit-logging.h"
+#include "diagnostic-core.h"
 #include "libgccjit.h"
 
 #include <string>
@@ -320,13 +321,19 @@ public:
   void
   get_target_info ();
 
+  void
+  add_diagnostic (location *loc, diagnostic_t diagnostic_kind,
+		  const char *fmt, ...)
+      GNU_PRINTF(4, 5);
+
   void
   add_error (location *loc, const char *fmt, ...)
       GNU_PRINTF(3, 4);
 
   void
-  add_error_va (location *loc, const char *fmt, va_list ap)
-      GNU_PRINTF(3, 0);
+  add_error_va (location *loc, diagnostic_t diagnostic_kind, const char *fmt,
+		va_list ap)
+      GNU_PRINTF(4, 0);
 
   const char *
   get_first_error () const;
diff --git a/gcc/jit/libgccjit.cc b/gcc/jit/libgccjit.cc
index ad029253158..5a1308b2b8c 100644
--- a/gcc/jit/libgccjit.cc
+++ b/gcc/jit/libgccjit.cc
@@ -341,7 +341,7 @@ jit_error (gcc::jit::recording::context *ctxt,
   va_start (ap, fmt);
 
   if (ctxt)
-    ctxt->add_error_va (loc, fmt, ap);
+    ctxt->add_error_va (loc, DK_ERROR, fmt, ap);
   else
     {
       /* No context?  Send to stderr.  */
diff --git a/gcc/testsuite/jit.dg/test-error-array-bounds.c b/gcc/testsuite/jit.dg/test-error-array-bounds.c
index a0dead13cb7..fb5c2064177 100644
--- a/gcc/testsuite/jit.dg/test-error-array-bounds.c
+++ b/gcc/testsuite/jit.dg/test-error-array-bounds.c
@@ -64,11 +64,7 @@ create_code (gcc_jit_context *ctxt, void *user_data)
 void
 verify_code (gcc_jit_context *ctxt, gcc_jit_result *result)
 {
-  /* Verify that the diagnostic led to the context failing... */
-  CHECK_VALUE (result, NULL);
-
-  /* ...and that the message was captured by the API.  */
-  CHECK_STRING_VALUE (gcc_jit_context_get_first_error (ctxt),
-		      "array subscript 10 is above array bounds of"
-		      " 'char[10]' [-Warray-bounds=]");
+  /* Verify that the message was captured by the API.  */
+  CHECK_STRING_VALUE (gcc_jit_context_get_last_error (ctxt),
+		      "while referencing 'buffer'");
 }
-- 
2.43.0

