From 43e78c798d3ea62de7fc19d90d13d8c7564faa43 Mon Sep 17 00:00:00 2001
From: David Malcolm <dmalcolm@redhat.com>
Date: Fri, 16 Sep 2016 11:16:40 -0400
Subject: [PATCH 07/44] FIXME: WIP on template type diff printing

TODO:
  * a lot
  * invoke.texi for new options
---
 gcc/c-family/c.opt                                 |   8 +
 gcc/cp/call.c                                      |  23 +--
 gcc/cp/cvt.c                                       |   6 +-
 gcc/cp/error.c                                     | 169 ++++++++++++++++++++-
 gcc/cp/typeck.c                                    |   8 +-
 gcc/cp/typeck2.c                                   |   6 +-
 gcc/testsuite/g++.dg/template/show-template-tree.C |  42 +++++
 7 files changed, 240 insertions(+), 22 deletions(-)
 create mode 100644 gcc/testsuite/g++.dg/template/show-template-tree.C

diff --git a/gcc/c-family/c.opt b/gcc/c-family/c.opt
index a5358ed..7ff427f 100644
--- a/gcc/c-family/c.opt
+++ b/gcc/c-family/c.opt
@@ -1194,6 +1194,10 @@ fdefault-inline
 C++ ObjC++ Ignore
 Does nothing.  Preserved for backward compatibility.
 
+fdiagnostics-show-template-tree
+C++ ObjC++ Var(flag_diagnostics_show_template_tree) Init(0)
+FIXME
+
 fdirectives-only
 C ObjC C++ ObjC++
 Preprocess directives only.
@@ -1213,6 +1217,10 @@ Write all declarations as Ada code for the given file only.
 felide-constructors
 C++ ObjC++ Var(flag_elide_constructors) Init(1)
 
+felide-type
+C++ ObjC++ Var(flag_elide_type) Init(1)
+-fno-elide-type Do not elide common elements in template comparisons
+
 fenforce-eh-specs
 C++ ObjC++ Var(flag_enforce_eh_specs) Init(1)
 Generate code to check exception specifications.
diff --git a/gcc/cp/call.c b/gcc/cp/call.c
index 024519d..a42bce5 100644
--- a/gcc/cp/call.c
+++ b/gcc/cp/call.c
@@ -3347,7 +3347,7 @@ print_conversion_rejection (location_t loc, struct conversion_info *info)
 		from);
       else
 	inform (loc, "  no known conversion for implicit "
-		"%<this%> parameter from %qT to %qT",
+		"%<this%> parameter %Z",
 		from, info->to_type);
     }
   else if (!TYPE_P (info->from))
@@ -3360,10 +3360,10 @@ print_conversion_rejection (location_t loc, struct conversion_info *info)
     }
   else if (info->n_arg == -2)
     /* Conversion of conversion function return value failed.  */
-    inform (loc, "  no known conversion from %qT to %qT",
+    inform (loc, "  no known conversion %Z",
 	    from, info->to_type);
   else
-    inform (loc, "  no known conversion for argument %d from %qT to %qT",
+    inform (loc, "  no known conversion for argument %d %Z",
 	    info->n_arg + 1, from, info->to_type);
 }
 
@@ -3843,7 +3843,7 @@ build_user_type_conversion_1 (tree totype, tree expr, int flags,
     {
       if (complain & tf_error)
 	{
-	  error ("conversion from %qT to %qT is ambiguous",
+	  error ("conversion %Z is ambiguous",
 		 fromtype, totype);
 	  print_z_candidates (location_of (expr), candidates);
 	}
@@ -3970,7 +3970,7 @@ build_integral_nontype_arg_conv (tree type, tree expr, tsubst_flags_t complain)
 	  break;
 
 	if (complain & tf_error)
-	  error_at (loc, "conversion from %qT to %qT not considered for "
+	  error_at (loc, "conversion %Z not considered for "
 		    "non-type template argument", t, type);
 	/* fall through.  */
 
@@ -5126,7 +5126,7 @@ build_conditional_expr_1 (location_t loc, tree arg1, tree arg2, tree arg3,
 							     arg3_type);
       if (complain & tf_warning)
 	do_warn_double_promotion (result_type, arg2_type, arg3_type,
-				  "implicit conversion from %qT to %qT to "
+				  "implicit conversion %Z to "
 				  "match other result of conditional",
 				  loc);
 
@@ -6410,7 +6410,7 @@ convert_like_real (conversion *convs, tree expr, tree fn, int argnum,
 	  if (t->kind == ck_user && t->cand->reason)
 	    {
 	      complained = permerror (loc, "invalid user-defined conversion "
-				      "from %qT to %qT", TREE_TYPE (expr),
+				      "%Z", TREE_TYPE (expr),
 				      totype);
 	      if (complained)
 		print_z_candidate (loc, "candidate is:", t->cand);
@@ -6446,7 +6446,7 @@ convert_like_real (conversion *convs, tree expr, tree fn, int argnum,
 	    break;
 	}
       if (!complained)
-	complained = permerror (loc, "invalid conversion from %qT to %qT",
+	complained = permerror (loc, "invalid conversion %Z",
 				TREE_TYPE (expr), totype);
       if (complained && fn)
 	inform (DECL_SOURCE_LOCATION (fn),
@@ -6876,7 +6876,7 @@ convert_arg_to_ellipsis (tree arg, tsubst_flags_t complain)
       if ((complain & tf_warning)
 	  && warn_double_promotion && !c_inhibit_evaluation_warnings)
 	warning_at (loc, OPT_Wdouble_promotion,
-		    "implicit conversion from %qT to %qT when passing "
+		    "implicit conversion %Z when passing "
 		    "argument to function",
 		    arg_type, double_type_node);
       arg = convert_to_real_nofold (double_type_node, arg);
@@ -9370,7 +9370,7 @@ joust (struct z_candidate *cand1, struct z_candidate *cand2, bool warn,
 	  if (! DECL_CONSTRUCTOR_P (w->fn))
 	    source = TREE_TYPE (source);
 	  if (warning (OPT_Wconversion, "choosing %qD over %qD", w->fn, l->fn)
-	      && warning (OPT_Wconversion, "  for conversion from %qT to %qT",
+	      && warning (OPT_Wconversion, "  for conversion %Z",
 			  source, w->second_conv->type)) 
 	    {
 	      inform (input_location, "  because conversion sequence for the argument is better");
@@ -9764,7 +9764,8 @@ perform_implicit_conversion_flags (tree type, tree expr,
 	  else if (invalid_nonstatic_memfn_p (loc, expr, complain))
 	    /* We gave an error.  */;
 	  else
-	    error_at (loc, "could not convert %qE from %qT to %qT", expr,
+            // FIXME: here's the one I'm testing...
+	    error_at (loc, "could not convert %qE %Z", expr,
 		      TREE_TYPE (expr), type);
 	}
       expr = error_mark_node;
diff --git a/gcc/cp/cvt.c b/gcc/cp/cvt.c
index 977c665..2df7b92 100644
--- a/gcc/cp/cvt.c
+++ b/gcc/cp/cvt.c
@@ -96,7 +96,7 @@ cp_convert_to_pointer (tree type, tree expr, bool dofold,
 	{
 	  if ((complain & tf_error)
 	      && rval == error_mark_node)
-	    error_at (loc, "conversion of %qE from %qT to %qT is ambiguous",
+	    error_at (loc, "conversion of %qE %Z is ambiguous",
 		      expr, intype, type);
 	  return rval;
 	}
@@ -221,7 +221,7 @@ cp_convert_to_pointer (tree type, tree expr, bool dofold,
   else if (TYPE_PTRMEM_P (type) && INTEGRAL_CODE_P (form))
     {
       if (complain & tf_error)
-	error_at (loc, "invalid conversion from %qT to %qT", intype, type);
+	error_at (loc, "invalid conversion %Z", intype, type);
       return error_mark_node;
     }
 
@@ -464,7 +464,7 @@ convert_to_reference (tree reftype, tree expr, int convtype,
 	    && !at_least_as_qualified_p (ttl, ttr))
 	  {
 	    if (complain & tf_error)
-	      permerror (loc, "conversion from %qT to %qT discards qualifiers",
+	      permerror (loc, "conversion %Z discards qualifiers",
 			 ttr, reftype);
 	    else
 	      return error_mark_node;
diff --git a/gcc/cp/error.c b/gcc/cp/error.c
index 88049ee..122b24b 100644
--- a/gcc/cp/error.c
+++ b/gcc/cp/error.c
@@ -3510,6 +3510,156 @@ maybe_print_constexpr_context (diagnostic_context *context)
     }
 }
 
+
+/* FIXME. */
+
+static bool
+comparable_template_types_p (tree type_a, tree type_b)
+{
+  if (TREE_CODE (type_a) != RECORD_TYPE)
+    return false;
+  if (TREE_CODE (type_b) != RECORD_TYPE)
+    return false;
+
+  tree tinfo_a = TYPE_TEMPLATE_INFO (type_a);
+  tree tinfo_b = TYPE_TEMPLATE_INFO (type_b);
+  if (!tinfo_a || !tinfo_b)
+    return false;
+
+  return TI_TEMPLATE (tinfo_a) == TI_TEMPLATE (tinfo_b);
+}
+
+/* FIXME. */
+
+static void
+newline_and_indent (pretty_printer *pp, int spc)
+{
+  pp_newline (pp);
+  for (int i = 0; i < spc; i++)
+    pp_space (pp);
+}
+
+/* FIXME.  */
+static const char *
+arg_to_string (tree arg, bool verbose)
+{
+  if (EXPR_P (arg))
+    return expr_to_string (arg);
+  return type_to_string (arg, verbose);
+}
+
+/* As type_to_string, but for a template, potentially colorize/elide the types
+   w.r.t. PEER.  */
+
+static const char *
+type_to_string_with_compare (tree type, tree peer, bool verbose)
+{
+  // FIXME:
+  return type_to_string (type, verbose); // for now
+}
+
+
+/* FIXME.  */
+
+static void
+print_nonequal_arg (pretty_printer *pp, tree arg, bool verbose)
+{
+  pp_printf (pp, "%r%s%R",
+	     // FIXME: do we need another colorization code for this?
+	     "note",
+	     (arg
+	      ? arg_to_string (arg, verbose)
+	      : "(no argument)"));
+}
+
+/* FIXME. */
+
+static void
+print_template_tree_comparison (pretty_printer *pp, tree type_a, tree type_b,
+				bool verbose, int indent)
+{
+  tree tinfo_a = TYPE_TEMPLATE_INFO (type_a);
+  tree tinfo_b = TYPE_TEMPLATE_INFO (type_b);
+
+  newline_and_indent (pp, indent);
+  pp_printf (pp, "%s<",
+	     IDENTIFIER_POINTER (DECL_NAME (TI_TEMPLATE (tinfo_a))));
+  tree args_a = TI_ARGS (tinfo_a);
+  tree args_b = TI_ARGS (tinfo_b);
+  gcc_assert (TREE_CODE (args_a) == TREE_VEC);
+  gcc_assert (TREE_CODE (args_b) == TREE_VEC);
+  int len_a = TREE_VEC_LENGTH (args_a);
+  int len_b = TREE_VEC_LENGTH (args_b);
+  int len_max = MAX (len_a, len_b);
+  for (int idx = 0; idx < len_max; idx++)
+    {
+      tree arg_a = idx < len_a ? TREE_VEC_ELT (args_a, idx) : NULL;
+      tree arg_b = idx < len_b ? TREE_VEC_ELT (args_b, idx) : NULL;
+      if (arg_a == arg_b)
+	{
+	  newline_and_indent (pp, indent + 2);
+	  // Can do elision here, printing "[...]":
+	  if (flag_elide_type)
+	    pp_string (pp, "[...]");
+	  else
+	    pp_string (pp, arg_to_string (arg_a, verbose));
+	}
+      else
+	{
+	  if (comparable_template_types_p (arg_a, arg_b))
+	    print_template_tree_comparison (pp, arg_a, arg_b, verbose,
+					    indent + 2);
+	  else
+	    {
+	      newline_and_indent (pp, indent + 2);
+	      pp_character (pp, '[');
+	      print_nonequal_arg (pp, arg_a, verbose);
+	      pp_string (pp, " != ");
+	      print_nonequal_arg (pp, arg_b, verbose);
+	      pp_character (pp, ']');
+	    }
+	}
+      if (idx + 1 < len_max)
+	pp_character (pp, ',');
+    }
+  // FIXME: surplus elements when len_a != len_b
+  pp_printf (pp, ">");
+}
+
+/* FIXME. */
+
+static void
+print_type_diff (pretty_printer *pp, tree type_a, tree type_b, bool verbose)
+{
+  // FIXME:
+  // expect an open brace/close brace region with a pipe
+  // consume two tree args
+  // looks like we can't consume extra text here
+  // just *spec
+  // for now, only support a hardcoded text
+  // "from %qT to %qT"
+  pretty_printer inner_pp;
+  pp_show_color (&inner_pp) = pp_show_color (pp);
+
+  if (comparable_template_types_p (type_a, type_b))
+    {
+      pp_printf (&inner_pp, "from %qs to %qs",
+		 type_to_string_with_compare (type_a, type_b, verbose),
+		 type_to_string_with_compare (type_b, type_a, verbose));
+      if (flag_diagnostics_show_template_tree)
+	print_template_tree_comparison (&inner_pp, type_a, type_b, verbose, 2);
+    }
+  else
+    {
+      pp_printf (&inner_pp, "from %qs to %qs",
+		 type_to_string (type_a, verbose),
+		 type_to_string (type_b, verbose));
+    }
+
+  pp_string (pp, pp_formatted_text (&inner_pp));
+}
+
+
 /* Called from output_format -- during diagnostic message processing --
    to handle C++ specific format specifier with the following meanings:
    %A   function argument-list.
@@ -3524,7 +3674,8 @@ maybe_print_constexpr_context (diagnostic_context *context)
    %S   substitution (template + args)
    %T   type.
    %V   cv-qualifier.
-   %X   exception-specification.  */
+   %X   exception-specification.
+   %Z   type difference.  */
 static bool
 cp_printer (pretty_printer *pp, text_info *text, const char *spec,
 	    int precision, bool wide, bool set_locus, bool verbose)
@@ -3574,6 +3725,22 @@ cp_printer (pretty_printer *pp, text_info *text, const char *spec,
       percent_K_format (text);
       return true;
 
+    case 'Z':
+      {
+	// FIXME:
+	// expect an open brace/close brace region with a pipe
+	// consume two tree args
+	// looks like we can't consume extra text here
+	// just *spec
+	// for now, only support a hardcoded text
+	// "from %qT to %qT"
+	tree type_a = next_tree;
+	tree type_b = next_tree;
+	print_type_diff (pp, type_a, type_b, verbose);
+	return true;
+      }
+      break;
+
     default:
       return false;
     }
diff --git a/gcc/cp/typeck.c b/gcc/cp/typeck.c
index a591d29..6af58e9 100644
--- a/gcc/cp/typeck.c
+++ b/gcc/cp/typeck.c
@@ -4966,7 +4966,7 @@ cp_build_binary_op (location_t location,
       result_type = cp_common_type (type0, type1);
       if (complain & tf_warning)
 	do_warn_double_promotion (result_type, type0, type1,
-				  "implicit conversion from %qT to %qT "
+				  "implicit conversion %Z "
 				  "to match other operand of binary "
 				  "expression",
 				  location);
@@ -6923,7 +6923,7 @@ convert_member_func_to_ptr (tree type, tree expr, tsubst_flags_t complain)
 
   if (pedantic || warn_pmf2ptr)
     pedwarn (input_location, pedantic ? OPT_Wpedantic : OPT_Wpmf_conversions,
-	     "converting from %qT to %qT", intype, type);
+	     "converting %Z", intype, type);
 
   if (TREE_CODE (intype) == METHOD_TYPE)
     expr = build_addr_func (expr, complain);
@@ -7045,7 +7045,7 @@ build_reinterpret_cast_1 (tree type, tree expr, bool c_cast_p,
       if (TYPE_PRECISION (type) < TYPE_PRECISION (intype))
         {
           if (complain & tf_error)
-            permerror (input_location, "cast from %qT to %qT loses precision",
+            permerror (input_location, "cast %Z loses precision",
                        intype, type);
           else
             return error_mark_node;
@@ -7085,7 +7085,7 @@ build_reinterpret_cast_1 (tree type, tree expr, bool c_cast_p,
 	  && COMPLETE_TYPE_P (TREE_TYPE (type))
 	  && COMPLETE_TYPE_P (TREE_TYPE (intype))
 	  && TYPE_ALIGN (TREE_TYPE (type)) > TYPE_ALIGN (TREE_TYPE (intype)))
-	warning (OPT_Wcast_align, "cast from %qT to %qT "
+	warning (OPT_Wcast_align, "cast %Z "
                  "increases required alignment of target type", intype, type);
 
       /* We need to strip nops here, because the front end likes to
diff --git a/gcc/cp/typeck2.c b/gcc/cp/typeck2.c
index b9dc56d..2908245 100644
--- a/gcc/cp/typeck2.c
+++ b/gcc/cp/typeck2.c
@@ -943,7 +943,7 @@ check_narrowing (tree type, tree init, tsubst_flags_t complain)
 	{
 	  if (complain & tf_warning)
 	    warning_at (loc, OPT_Wnarrowing, "narrowing conversion of %qE "
-			"from %qT to %qT inside { } is ill-formed in C++11",
+			"%Z inside { } is ill-formed in C++11",
 			init, ftype, type);
 	  ok = true;
 	}
@@ -954,7 +954,7 @@ check_narrowing (tree type, tree init, tsubst_flags_t complain)
 	      if ((!almost_ok || pedantic)
 		  && pedwarn (loc, OPT_Wnarrowing,
 			      "narrowing conversion of %qE "
-			      "from %qT to %qT inside { }",
+			      "%Z inside { }",
 			      init, ftype, type)
 		  && almost_ok)
 		inform (loc, " the expression has a constant value but is not "
@@ -967,7 +967,7 @@ check_narrowing (tree type, tree init, tsubst_flags_t complain)
 	  int savederrorcount = errorcount;
 	  global_dc->pedantic_errors = 1;
 	  pedwarn (loc, OPT_Wnarrowing,
-		   "narrowing conversion of %qE from %qT to %qT "
+		   "narrowing conversion of %qE %Z "
 		   "inside { }", init, ftype, type);
 	  if (errorcount == savederrorcount)
 	    ok = true;
diff --git a/gcc/testsuite/g++.dg/template/show-template-tree.C b/gcc/testsuite/g++.dg/template/show-template-tree.C
new file mode 100644
index 0000000..b3ac414
--- /dev/null
+++ b/gcc/testsuite/g++.dg/template/show-template-tree.C
@@ -0,0 +1,42 @@
+// { dg-options "-fdiagnostics-show-template-tree" }
+
+template<typename> struct vector {};
+template<typename, typename> struct map {};
+
+void fn_1(vector<int>);
+void fn_2(map<int, int>);
+void fn_3(vector<map<int, float> >);
+
+void test ()
+{
+  fn_1 (vector<int> ());
+  fn_1 (vector<double> ()); // { dg-error "could not convert 'vector<double>\\(\\)' from 'vector<double>' to 'vector<int>'" }
+  /* { dg-begin-multiline-output "" }
+  vector<
+    [double != int]>
+     { dg-end-multiline-output "" } */
+  fn_1 (map<int, int> ()); // { dg-error "could not convert 'map<int, int>\\(\\)' from 'map<int, int>' to 'vector<int>'" }
+
+  fn_2 (map<int, int>());
+  fn_2 (map<int, double>());  // { dg-error "could not convert 'map<int, double>\\(\\)' from 'map<int, double>' to 'map<int, int>'" }
+  /* { dg-begin-multiline-output "" }
+  map<
+    [...],
+    [double != int]>
+     { dg-end-multiline-output "" } */
+  fn_2 (map<double, double>());  // { dg-error "could not convert 'map<double, double>\\(\\)' from 'map<double, double>' to 'map<int, int>'" }
+  /* { dg-begin-multiline-output "" }
+  map<
+    [double != int],
+    [double != int]>
+     { dg-end-multiline-output "" } */
+
+  fn_3 (vector<map<int, float> >());
+  fn_3 (vector<map<int, double> >());  // { dg-error "could not convert 'vector<map<int, double> >\\(\\)' from 'vector<map<int, double> >' to 'vector<map<int, float> >'" }
+  /* { dg-begin-multiline-output "" }
+  vector<
+    map<
+      [...],
+      [double != float]>>
+     { dg-end-multiline-output "" } */
+}
-- 
1.8.5.3

