From 943494393ead0abbd8aeb7dc84aa4ba8984ed43b Mon Sep 17 00:00:00 2001
From: David Malcolm <dmalcolm@redhat.com>
Date: Tue, 5 Dec 2017 17:05:49 -0500
Subject: [PATCH 09/29] FIXME: move common inlining-printing into a function

---
 gcc/cp/error.c        | 74 +++++++--------------------------------------------
 gcc/langhooks.c       | 74 +++++++--------------------------------------------
 gcc/tree-diagnostic.c | 73 ++++++++++++++++++++++++++++++++++++++++++++++++++
 gcc/tree-diagnostic.h |  4 +++
 4 files changed, 97 insertions(+), 128 deletions(-)

diff --git a/gcc/cp/error.c b/gcc/cp/error.c
index ee1af5a..4071762 100644
--- a/gcc/cp/error.c
+++ b/gcc/cp/error.c
@@ -3303,6 +3303,14 @@ cp_diagnostic_starter (diagnostic_context *context,
 								 diagnostic));
 }
 
+/* FIXME.  */
+
+static const char *
+print_fn_decl (tree fndecl)
+{
+  return cxx_printable_name_translate (fndecl, 2);
+}
+
 /* Print current function onto BUFFER, in the process of reporting
    a diagnostic message.  Called from cp_diagnostic_starter.  */
 static void
@@ -3345,70 +3353,8 @@ cp_print_error_function (diagnostic_context *context,
 	  pp_printf (context->printer, function_category (fndecl),
 		     cxx_printable_name_translate (fndecl, 2));
 
-	  while (abstract_origin)
-	    {
-	      location_t *locus;
-	      tree block = abstract_origin;
-
-	      locus = &BLOCK_SOURCE_LOCATION (block);
-	      fndecl = NULL;
-	      block = BLOCK_SUPERCONTEXT (block);
-	      while (block && TREE_CODE (block) == BLOCK
-		     && BLOCK_ABSTRACT_ORIGIN (block))
-		{
-		  ao = BLOCK_ABSTRACT_ORIGIN (block);
-
-		  while (TREE_CODE (ao) == BLOCK
-			 && BLOCK_ABSTRACT_ORIGIN (ao)
-			 && BLOCK_ABSTRACT_ORIGIN (ao) != ao)
-		    ao = BLOCK_ABSTRACT_ORIGIN (ao);
-
-		  if (TREE_CODE (ao) == FUNCTION_DECL)
-		    {
-		      fndecl = ao;
-		      break;
-		    }
-		  else if (TREE_CODE (ao) != BLOCK)
-		    break;
-
-		  block = BLOCK_SUPERCONTEXT (block);
-		}
-	      if (fndecl)
-		abstract_origin = block;
-	      else
-		{
-		  while (block && TREE_CODE (block) == BLOCK)
-		    block = BLOCK_SUPERCONTEXT (block);
-
-		  if (block && TREE_CODE (block) == FUNCTION_DECL)
-		    fndecl = block;
-		  abstract_origin = NULL;
-		}
-	      if (fndecl)
-		{
-		  expanded_location s = expand_location (*locus);
-		  pp_comma (context->printer);
-		  pp_newline (context->printer);
-		  if (s.file != NULL)
-		    {
-		      if (context->show_column && s.column != 0)
-			pp_printf (context->printer,
-				   _("    inlined from %qs at %r%s:%d:%d%R"),
-				   cxx_printable_name_translate (fndecl, 2),
-				   "locus", s.file, s.line, s.column);
-		      else
-			pp_printf (context->printer,
-				   _("    inlined from %qs at %r%s:%d%R"),
-				   cxx_printable_name_translate (fndecl, 2),
-				   "locus", s.file, s.line);
-
-		    }
-		  else
-		    pp_printf (context->printer, _("    inlined from %qs"),
-			       cxx_printable_name_translate (fndecl, 2));
-		}
-	    }
-	  pp_colon (context->printer);
+	  diagnostic_unwind_inlining_information (context, abstract_origin,
+						  print_fn_decl);
 	}
       pp_newline (context->printer);
 
diff --git a/gcc/langhooks.c b/gcc/langhooks.c
index 130d74b..472900a 100644
--- a/gcc/langhooks.c
+++ b/gcc/langhooks.c
@@ -360,6 +360,14 @@ lhd_handle_option (size_t code ATTRIBUTE_UNUSED,
   return false;
 }
 
+/* FIXME.  */
+
+static const char *
+print_fn_decl (tree fndecl)
+{
+  return identifier_to_locale (lang_hooks.decl_printable_name (fndecl, 2));
+}
+
 /* The default function to print out name of current function that caused
    an error.  */
 void
@@ -403,70 +411,8 @@ lhd_print_error_function (diagnostic_context *context, const char *file,
 	      (context->printer, _("In function %qs"),
 	       identifier_to_locale (lang_hooks.decl_printable_name (fndecl, 2)));
 
-	  while (abstract_origin)
-	    {
-	      location_t *locus;
-	      tree block = abstract_origin;
-
-	      locus = &BLOCK_SOURCE_LOCATION (block);
-	      fndecl = NULL;
-	      block = BLOCK_SUPERCONTEXT (block);
-	      while (block && TREE_CODE (block) == BLOCK
-		     && BLOCK_ABSTRACT_ORIGIN (block))
-		{
-		  ao = BLOCK_ABSTRACT_ORIGIN (block);
-
-		  while (TREE_CODE (ao) == BLOCK
-			 && BLOCK_ABSTRACT_ORIGIN (ao)
-			 && BLOCK_ABSTRACT_ORIGIN (ao) != ao)
-		    ao = BLOCK_ABSTRACT_ORIGIN (ao);
-
-		  if (TREE_CODE (ao) == FUNCTION_DECL)
-		    {
-		      fndecl = ao;
-		      break;
-		    }
-		  else if (TREE_CODE (ao) != BLOCK)
-		    break;
-
-		  block = BLOCK_SUPERCONTEXT (block);
-		}
-	      if (fndecl)
-		abstract_origin = block;
-	      else
-		{
-		  while (block && TREE_CODE (block) == BLOCK)
-		    block = BLOCK_SUPERCONTEXT (block);
-
-		  if (block && TREE_CODE (block) == FUNCTION_DECL)
-		    fndecl = block;
-		  abstract_origin = NULL;
-		}
-	      if (fndecl)
-		{
-		  expanded_location s = expand_location (*locus);
-		  pp_comma (context->printer);
-		  pp_newline (context->printer);
-		  if (s.file != NULL)
-		    {
-		      if (context->show_column && s.column != 0)
-			pp_printf (context->printer,
-				   _("    inlined from %qs at %r%s:%d:%d%R"),
-				   identifier_to_locale (lang_hooks.decl_printable_name (fndecl, 2)),
-				   "locus", s.file, s.line, s.column);
-		      else
-			pp_printf (context->printer,
-				   _("    inlined from %qs at %r%s:%d%R"),
-				   identifier_to_locale (lang_hooks.decl_printable_name (fndecl, 2)),
-				   "locus", s.file, s.line);
-
-		    }
-		  else
-		    pp_printf (context->printer, _("    inlined from %qs"),
-			       identifier_to_locale (lang_hooks.decl_printable_name (fndecl, 2)));
-		}
-	    }
-	  pp_colon (context->printer);
+	  diagnostic_unwind_inlining_information (context, abstract_origin,
+						  print_fn_decl);
 	}
 
       diagnostic_set_last_function (context, diagnostic);
diff --git a/gcc/tree-diagnostic.c b/gcc/tree-diagnostic.c
index c80ae54..a4b66b4 100644
--- a/gcc/tree-diagnostic.c
+++ b/gcc/tree-diagnostic.c
@@ -313,3 +313,76 @@ tree_diagnostics_defaults (diagnostic_context *context)
   diagnostic_finalizer (context) = default_diagnostic_finalizer;
   diagnostic_format_decoder (context) = default_tree_printer;
 }
+
+/* FIXME.  */
+
+void
+diagnostic_unwind_inlining_information (diagnostic_context *context,
+					tree abstract_origin,
+					const char * (*print_fn_decl_cb) (tree))
+{
+  while (abstract_origin)
+    {
+      location_t *locus;
+      tree block = abstract_origin;
+      tree fndecl = NULL;
+
+      locus = &BLOCK_SOURCE_LOCATION (block);
+      block = BLOCK_SUPERCONTEXT (block);
+      while (block && TREE_CODE (block) == BLOCK
+	     && BLOCK_ABSTRACT_ORIGIN (block))
+	{
+	  tree ao = BLOCK_ABSTRACT_ORIGIN (block);
+
+	  while (TREE_CODE (ao) == BLOCK
+		 && BLOCK_ABSTRACT_ORIGIN (ao)
+		 && BLOCK_ABSTRACT_ORIGIN (ao) != ao)
+	    ao = BLOCK_ABSTRACT_ORIGIN (ao);
+
+	  if (TREE_CODE (ao) == FUNCTION_DECL)
+	    {
+	      fndecl = ao;
+	      break;
+	    }
+	  else if (TREE_CODE (ao) != BLOCK)
+	    break;
+
+	  block = BLOCK_SUPERCONTEXT (block);
+	}
+      if (fndecl)
+	abstract_origin = block;
+      else
+	{
+	  while (block && TREE_CODE (block) == BLOCK)
+	    block = BLOCK_SUPERCONTEXT (block);
+
+	  if (block && TREE_CODE (block) == FUNCTION_DECL)
+	    fndecl = block;
+	  abstract_origin = NULL;
+	}
+      if (fndecl)
+	{
+	  expanded_location s = expand_location (*locus);
+	  pp_comma (context->printer);
+	  pp_newline (context->printer);
+	  if (s.file != NULL)
+	    {
+	      if (context->show_column && s.column != 0)
+		pp_printf (context->printer,
+			   _("    inlined from %qs at %r%s:%d:%d%R"),
+			   print_fn_decl_cb (fndecl),
+			   "locus", s.file, s.line, s.column);
+	      else
+		pp_printf (context->printer,
+			   _("    inlined from %qs at %r%s:%d%R"),
+			   print_fn_decl_cb (fndecl),
+			   "locus", s.file, s.line);
+
+	    }
+	  else
+	    pp_printf (context->printer, _("    inlined from %qs"),
+		       print_fn_decl_cb (fndecl));
+	}
+    }
+  pp_colon (context->printer);
+}
diff --git a/gcc/tree-diagnostic.h b/gcc/tree-diagnostic.h
index 6c2a60d..47368a0 100644
--- a/gcc/tree-diagnostic.h
+++ b/gcc/tree-diagnostic.h
@@ -65,4 +65,8 @@ void tree_diagnostics_defaults (diagnostic_context *context);
 bool default_tree_printer (pretty_printer *, text_info *, const char *,
 			   int, bool, bool, bool, bool *, const char **);
 
+extern void diagnostic_unwind_inlining_information (diagnostic_context *context,
+						    tree abstract_origin,
+						    const char * (*print_fn_decl_cb) (tree));
+
 #endif /* ! GCC_TREE_DIAGNOSTIC_H */
-- 
1.8.5.3

