From 2bde59d072ed74eb1514d84616bfeab9946ba5f2 Mon Sep 17 00:00:00 2001
From: David Malcolm <dmalcolm@redhat.com>
Date: Thu, 16 Aug 2018 10:33:39 -0400
Subject: [PATCH 18/19] FIXME: gimple-ssa-sprintf.c: WIP experiment for labels
 etc

---
 gcc/gimple-ssa-sprintf.c | 54 ++++++++++++++++++++++++++++++++++++++++--------
 1 file changed, 45 insertions(+), 9 deletions(-)

diff --git a/gcc/gimple-ssa-sprintf.c b/gcc/gimple-ssa-sprintf.c
index b5e1a08..0c534ee 100644
--- a/gcc/gimple-ssa-sprintf.c
+++ b/gcc/gimple-ssa-sprintf.c
@@ -78,6 +78,7 @@ along with GCC; see the file COPYING3.  If not see
 #include "input.h"
 #include "toplev.h"
 #include "substring-locations.h"
+#include "gcc-rich-location.h"
 #include "diagnostic.h"
 #include "domwalk.h"
 #include "alloc-pool.h"
@@ -611,14 +612,17 @@ fmtwarn (const substring_loc &fmt_loc, location_t param_loc,
 }
 
 static bool
-ATTRIBUTE_GCC_DIAG (6, 8) ATTRIBUTE_GCC_DIAG (7, 8)
-fmtwarn_n (const substring_loc &fmt_loc, location_t param_loc,
+ATTRIBUTE_GCC_DIAG (8, 10) ATTRIBUTE_GCC_DIAG (9, 10)
+fmtwarn_n (const substring_loc &fmt_loc,
+	   const range_label *fmt_label,
+	   location_t param_loc,
+	   const range_label *param_label,
 	   const char *corrected_substring, int opt, unsigned HOST_WIDE_INT n,
 	   const char *singular_gmsgid, const char *plural_gmsgid, ...)
 {
   va_list ap;
   va_start (ap, plural_gmsgid);
-  bool warned = format_warning_n_va (fmt_loc, NULL, param_loc, NULL,
+  bool warned = format_warning_n_va (fmt_loc, fmt_label, param_loc, param_label,
 				     corrected_substring,
 				     opt, n, singular_gmsgid, plural_gmsgid,
 				     &ap);
@@ -2529,6 +2533,27 @@ should_warn_p (const sprintf_dom_walker::call_info &info,
   return true;
 }
 
+/* FIXME.  */
+
+class buffer_range_label : public range_label
+{
+ public:
+  buffer_range_label (const result_range &range)
+  : m_range (range)
+  {}
+
+  label_text get_text () const FINAL OVERRIDE
+  {
+    pretty_printer pp;
+    // FIXME: various possibilities here:
+    pp_printf (&pp, "%wi", m_range.min);
+    return label_text (xstrdup (pp_formatted_text (&pp)), true);
+  }
+
+ private:
+  const result_range &m_range;
+};
+
 /* At format string location describe by DIRLOC in a call described
    by INFO, issue a warning for a directive DIR whose output may be
    in excess of the available space AVAIL_RANGE in the destination
@@ -2602,23 +2627,29 @@ maybe_warn (substring_loc &dirloc, location_t argloc,
 
       if (res.min == res.max)
 	{
+	  // FIXME: should label the destination
+	  text_range_label fmt_label ("FIXME: fmt_label");
+	  buffer_range_label param_label (res);
 	  const char *d = target_to_host (hostdir, sizeof hostdir, dir.beg);
 	  if (!info.bounded)
-	    return fmtwarn_n (dirloc, argloc, NULL, info.warnopt (), res.min,
+	    return fmtwarn_n (dirloc, &fmt_label, argloc, &param_label,
+			      NULL, info.warnopt (), res.min,
 			      "%<%.*s%> directive writing %wu byte into a "
 			      "region of size %wu",
 			      "%<%.*s%> directive writing %wu bytes into a "
 			      "region of size %wu",
 			      (int) dir.len, d, res.min, navail);
 	  else if (maybe)
-	    return fmtwarn_n (dirloc, argloc, NULL, info.warnopt (), res.min,
+	    return fmtwarn_n (dirloc, &fmt_label, argloc, &param_label,
+			      NULL, info.warnopt (), res.min,
 			      "%<%.*s%> directive output may be truncated "
 			      "writing %wu byte into a region of size %wu",
 			      "%<%.*s%> directive output may be truncated "
 			      "writing %wu bytes into a region of size %wu",
 			      (int) dir.len, d, res.min, navail);
 	  else
-	    return fmtwarn_n (dirloc, argloc, NULL, info.warnopt (), res.min,
+	    return fmtwarn_n (dirloc, &fmt_label, argloc, &param_label,
+			      NULL, info.warnopt (), res.min,
 			      "%<%.*s%> directive output truncated writing "
 			      "%wu byte into a region of size %wu",
 			      "%<%.*s%> directive output truncated writing "
@@ -2721,16 +2752,20 @@ maybe_warn (substring_loc &dirloc, location_t argloc,
 
   if (res.min == res.max)
     {
+      text_range_label fmt_label ("FIXME: fmt_label");
+      text_range_label param_label ("FIXME: param_label");
       const char *d = target_to_host (hostdir, sizeof hostdir, dir.beg);
       if (!info.bounded)
-	return fmtwarn_n (dirloc, argloc, NULL, info.warnopt (), res.min,
+	return fmtwarn_n (dirloc, &fmt_label, argloc, &param_label,
+			  NULL, info.warnopt (), res.min,
 			  "%<%.*s%> directive writing %wu byte into a region "
 			  "of size between %wu and %wu",
 			  "%<%.*s%> directive writing %wu bytes into a region "
 			  "of size between %wu and %wu", (int) dir.len, d,
 			  res.min, avail_range.min, avail_range.max);
       else if (maybe)
-	return fmtwarn_n (dirloc, argloc, NULL, info.warnopt (), res.min,
+	return fmtwarn_n (dirloc, &fmt_label, argloc, &param_label,
+			  NULL, info.warnopt (), res.min,
 			  "%<%.*s%> directive output may be truncated writing "
 			  "%wu byte into a region of size between %wu and %wu",
 			  "%<%.*s%> directive output may be truncated writing "
@@ -2738,7 +2773,8 @@ maybe_warn (substring_loc &dirloc, location_t argloc,
 			  "%wu", (int) dir.len, d, res.min, avail_range.min,
 			  avail_range.max);
       else
-	return fmtwarn_n (dirloc, argloc, NULL, info.warnopt (), res.min,
+	return fmtwarn_n (dirloc, &fmt_label, argloc, &param_label,
+			  NULL, info.warnopt (), res.min,
 			  "%<%.*s%> directive output truncated writing %wu "
 			  "byte into a region of size between %wu and %wu",
 			  "%<%.*s%> directive output truncated writing %wu "
-- 
1.8.5.3

