From 9dff9c65351394fbafa7ab682139bd3d3f4323e1 Mon Sep 17 00:00:00 2001
From: David Malcolm <dmalcolm@redhat.com>
Date: Thu, 12 Jul 2018 17:03:57 -0400
Subject: [PATCH 09/17] FIXME: WIP on grouping relating diagnostics

---
 gcc/c-family/c-indentation.c | 15 ++++++-----
 gcc/c/c-typeck.c             |  3 ++-
 gcc/diagnostic-core.h        | 25 +++++++++++++++++-
 gcc/diagnostic.c             | 63 +++++++++++++++++++++++++++++++++++++++++---
 gcc/diagnostic.h             |  3 +++
 5 files changed, 96 insertions(+), 13 deletions(-)

diff --git a/gcc/c-family/c-indentation.c b/gcc/c-family/c-indentation.c
index 44b1e1e..499af09 100644
--- a/gcc/c-family/c-indentation.c
+++ b/gcc/c-family/c-indentation.c
@@ -602,12 +602,13 @@ warn_for_misleading_indentation (const token_indent_info &guard_tinfo,
 					      body_tinfo,
 					      next_tinfo))
     {
-      if (warning_at (guard_tinfo.location, OPT_Wmisleading_indentation,
-		      "this %qs clause does not guard...",
-		      guard_tinfo_to_string (guard_tinfo.keyword)))
-	inform (next_tinfo.location,
-		"...this statement, but the latter is misleadingly indented"
-		" as if it were guarded by the %qs",
-		guard_tinfo_to_string (guard_tinfo.keyword));
+      diagnostic_group d;
+      if (d.warning_at (guard_tinfo.location, OPT_Wmisleading_indentation,
+			"this %qs clause does not guard...",
+			guard_tinfo_to_string (guard_tinfo.keyword)))
+	d.inform (next_tinfo.location,
+		  "...this statement, but the latter is misleadingly indented"
+		  " as if it were guarded by the %qs",
+		  guard_tinfo_to_string (guard_tinfo.keyword));
     }
 }
diff --git a/gcc/c/c-typeck.c b/gcc/c/c-typeck.c
index 2e9338e..46b2e68 100644
--- a/gcc/c/c-typeck.c
+++ b/gcc/c/c-typeck.c
@@ -6219,7 +6219,8 @@ pedwarn_init (location_t loc, int opt, const char *gmsgid, ...)
 
   va_list ap;
   va_start (ap, gmsgid);
-  bool warned = emit_diagnostic_valist (DK_PEDWARN, exploc, opt, gmsgid, &ap);
+  bool warned = emit_diagnostic_valist (DK_PEDWARN, exploc, opt, gmsgid, &ap,
+					NULL);
   va_end (ap);
   char *ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
   if (*ofwhat && warned)
diff --git a/gcc/diagnostic-core.h b/gcc/diagnostic-core.h
index aa5807e..79b84a7 100644
--- a/gcc/diagnostic-core.h
+++ b/gcc/diagnostic-core.h
@@ -53,6 +53,28 @@ extern const char *trim_filename (const char *);
 #else
 #define ATTRIBUTE_GCC_DIAG(m, n) ATTRIBUTE_NONNULL(m)
 #endif
+
+/* FIXME.  */
+
+class diagnostic_group
+{
+ public:
+  diagnostic_group () : m_emitted_header (false) {}
+  ~diagnostic_group ();
+
+  void maybe_emit_header ();
+  void emit_footer ();
+
+  bool warning_at (location_t loc, int opt, const char *gmsgid, ...)
+    ATTRIBUTE_GCC_DIAG(4,5);
+
+  void inform (location_t loc, const char *gmsgid, ...)
+    ATTRIBUTE_GCC_DIAG(3,4);
+
+ private:
+  bool m_emitted_header;
+};
+
 extern void internal_error (const char *, ...) ATTRIBUTE_GCC_DIAG(1,2)
      ATTRIBUTE_NORETURN;
 extern void internal_error_no_backtrace (const char *, ...)
@@ -96,7 +118,8 @@ extern void verbatim (const char *, ...) ATTRIBUTE_GCC_DIAG(1,2);
 extern bool emit_diagnostic (diagnostic_t, location_t, int,
 			     const char *, ...) ATTRIBUTE_GCC_DIAG(4,5);
 extern bool emit_diagnostic_valist (diagnostic_t, location_t, int, const char *,
-				    va_list *) ATTRIBUTE_GCC_DIAG (4,0);
+				    va_list *, diagnostic_group *)
+  ATTRIBUTE_GCC_DIAG (4,0);
 extern bool seen_error (void);
 
 #ifdef BUFSIZ
diff --git a/gcc/diagnostic.c b/gcc/diagnostic.c
index b021fe8..8d28f91 100644
--- a/gcc/diagnostic.c
+++ b/gcc/diagnostic.c
@@ -50,7 +50,8 @@ along with GCC; see the file COPYING3.  If not see
 
 /* Prototypes.  */
 static bool diagnostic_impl (rich_location *, int, const char *,
-			     va_list *, diagnostic_t) ATTRIBUTE_GCC_DIAG(3,0);
+			     va_list *, diagnostic_t, diagnostic_group * = NULL)
+  ATTRIBUTE_GCC_DIAG(3,0);
 static bool diagnostic_n_impl (rich_location *, int, unsigned HOST_WIDE_INT,
 			       const char *, const char *, va_list *,
 			       diagnostic_t) ATTRIBUTE_GCC_DIAG(5,0);
@@ -65,6 +66,53 @@ const char *progname;
 /* A diagnostic_context surrogate for stderr.  */
 static diagnostic_context global_diagnostic_context;
 diagnostic_context *global_dc = &global_diagnostic_context;
+
+
+
+diagnostic_group::~diagnostic_group ()
+{
+  if (m_emitted_header)
+    emit_footer ();
+}
+
+void
+diagnostic_group::maybe_emit_header ()
+{
+  if (m_emitted_header)
+    return;
+
+  m_emitted_header = true;
+  // FIXME:
+  fprintf (stderr, "--------------------------------------------------------\n");
+}
+
+void
+diagnostic_group::emit_footer ()
+{
+  fprintf (stderr, "\n");
+}
+
+bool
+diagnostic_group::warning_at (location_t loc, int opt, const char *gmsgid, ...)
+{
+  va_list ap;
+  va_start (ap, gmsgid);
+  /* Pass in "this", so that we can group things.  */
+  bool ret = emit_diagnostic_valist (DK_WARNING, loc, opt, gmsgid, &ap, this);
+  va_end (ap);
+  return ret;
+}
+
+void
+diagnostic_group::inform (location_t loc, const char *gmsgid, ...)
+{
+  va_list ap;
+  va_start (ap, gmsgid);
+  /* Pass in "this", so that we can group things.  */
+  emit_diagnostic_valist (DK_NOTE, loc, -1, gmsgid, &ap, this);
+  va_end (ap);
+}
+
 
 /* Return a malloc'd string containing MSG formatted a la printf.  The
    caller is responsible for freeing the memory.  */
@@ -265,6 +313,7 @@ diagnostic_set_info_translated (diagnostic_info *diagnostic, const char *msg,
   diagnostic->richloc = richloc;
   diagnostic->kind = kind;
   diagnostic->option_index = 0;
+  diagnostic->group = NULL;
 }
 
 /* Initialize DIAGNOSTIC, where the message GMSGID has not yet been
@@ -1049,6 +1098,9 @@ diagnostic_report_diagnostic (diagnostic_context *context,
   else
     ++diagnostic_kind_count (context, diagnostic->kind);
 
+  if (diagnostic->group)
+    diagnostic->group->maybe_emit_header ();
+
   diagnostic->message.x_data = &diagnostic->x_data;
   diagnostic->x_data = NULL;
   pp_format (context->printer, &diagnostic->message);
@@ -1159,7 +1211,8 @@ diagnostic_append_note (diagnostic_context *context,
 static bool
 diagnostic_impl (rich_location *richloc, int opt,
 		 const char *gmsgid,
-		 va_list *ap, diagnostic_t kind)
+		 va_list *ap, diagnostic_t kind,
+		 diagnostic_group *group)
 {
   diagnostic_info diagnostic;
   if (kind == DK_PERMERROR)
@@ -1174,6 +1227,7 @@ diagnostic_impl (rich_location *richloc, int opt,
       if (kind == DK_WARNING || kind == DK_PEDWARN)
 	diagnostic.option_index = opt;
     }
+  diagnostic.group = group;
   return diagnostic_report_diagnostic (global_dc, &diagnostic);
 }
 
@@ -1221,10 +1275,11 @@ emit_diagnostic (diagnostic_t kind, location_t location, int opt,
 
 bool
 emit_diagnostic_valist (diagnostic_t kind, location_t location, int opt,
-			const char *gmsgid, va_list *ap)
+			const char *gmsgid, va_list *ap,
+			diagnostic_group *group)
 {
   rich_location richloc (line_table, location);
-  return diagnostic_impl (&richloc, opt, gmsgid, ap, kind);
+  return diagnostic_impl (&richloc, opt, gmsgid, ap, kind, group);
 }
 
 /* An informative note at LOCATION.  Use this for additional details on an error
diff --git a/gcc/diagnostic.h b/gcc/diagnostic.h
index dcb8e2d..f9c0873 100644
--- a/gcc/diagnostic.h
+++ b/gcc/diagnostic.h
@@ -41,6 +41,9 @@ struct diagnostic_info
   diagnostic_t kind;
   /* Which OPT_* directly controls this diagnostic.  */
   int option_index;
+
+  /* FIXME.  */
+  diagnostic_group *group;
 };
 
 /* Each time a diagnostic's classification is changed with a pragma,
-- 
1.8.5.3

