From 1b3df59e03d8cbe29b0cf7f04dcdbb753e67b74d Mon Sep 17 00:00:00 2001
From: David Malcolm <dmalcolm@redhat.com>
Date: Wed, 20 Mar 2019 19:14:26 -0400
Subject: [PATCH 131/217] FIXME: convert plugin pass to be interprocedural

---
 .../gcc.dg/plugin/diagnostic_plugin_test_paths.c   | 64 +++++++++++++++-------
 1 file changed, 43 insertions(+), 21 deletions(-)

diff --git a/gcc/testsuite/gcc.dg/plugin/diagnostic_plugin_test_paths.c b/gcc/testsuite/gcc.dg/plugin/diagnostic_plugin_test_paths.c
index 4a7c0be..eba4441 100644
--- a/gcc/testsuite/gcc.dg/plugin/diagnostic_plugin_test_paths.c
+++ b/gcc/testsuite/gcc.dg/plugin/diagnostic_plugin_test_paths.c
@@ -36,12 +36,13 @@
 #include "context.h"
 #include "print-tree.h"
 #include "gcc-rich-location.h"
+#include "cgraph.h"
 
 int plugin_is_GPL_compatible;
 
 const pass_data pass_data_test_show_path =
 {
-  GIMPLE_PASS, /* type */
+  IPA_PASS, /* type */
   "test_show_path", /* name */
   OPTGROUP_NONE, /* optinfo_flags */
   TV_NONE, /* tv_id */
@@ -52,11 +53,20 @@ const pass_data pass_data_test_show_path =
   0, /* todo_flags_finish */
 };
 
-class pass_test_show_path : public gimple_opt_pass
+class pass_test_show_path : public ipa_opt_pass_d
 {
 public:
   pass_test_show_path(gcc::context *ctxt)
-    : gimple_opt_pass(pass_data_test_show_path, ctxt)
+    : ipa_opt_pass_d (pass_data_test_show_path, ctxt,
+		      NULL, /* generate_summary */
+		      NULL, /* write_summary */
+		      NULL, /* read_summary */
+		      NULL, /* write_optimization_summary */
+		      NULL, /* read_optimization_summary */
+		      NULL, /* stmt_fixup */
+		      0, /* function_transform_todo_flags_start */
+		      NULL, /* function_transform */
+		      NULL) /* variable_transform */
   {}
 
   /* opt_pass methods: */
@@ -99,26 +109,38 @@ check_for_named_call (gimple *stmt,
 /* FIXME.  */
 
 unsigned int
-pass_test_show_path::execute (function *fun)
+pass_test_show_path::execute (function *)
 {
   gimple_stmt_iterator gsi;
   basic_block bb;
 
+  /* Example A: a purely intraprocedural path.  */
   gcall *call_to_PyList_Append = NULL;
   gcall *call_to_PyList_New = NULL;
   gcond *for_cond = NULL;
+  function *example_a_fun = NULL;
 
-  FOR_EACH_BB_FN (bb, fun)
-    for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
-      {
-	gimple *stmt = gsi_stmt (gsi);
-	if (gcall *call = check_for_named_call (stmt, "PyList_New", 1))
-	  call_to_PyList_New = call;
-	if (gcall *call = check_for_named_call (stmt, "PyList_Append", 2))
-	  call_to_PyList_Append = call;
-	if (gcond *cond = dyn_cast <gcond *> (stmt))
-	  for_cond = cond;
-      }
+  cgraph_node *node;
+  FOR_EACH_FUNCTION_WITH_GIMPLE_BODY (node)
+    {
+      function *fun = node->get_fun ();
+      FOR_EACH_BB_FN (bb, fun)
+	{
+	  for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
+	    {
+	      gimple *stmt = gsi_stmt (gsi);
+	      if (gcall *call = check_for_named_call (stmt, "PyList_New", 1))
+		{
+		  call_to_PyList_New = call;
+		  example_a_fun = fun;
+		}
+	      if (gcall *call = check_for_named_call (stmt, "PyList_Append", 2))
+		call_to_PyList_Append = call;
+	      if (gcond *cond = dyn_cast <gcond *> (stmt))
+		for_cond = cond;
+	    }
+	}
+    }
 
   if (call_to_PyList_New && for_cond && call_to_PyList_Append)
     {
@@ -126,14 +148,14 @@ pass_test_show_path::execute (function *fun)
       gcc_rich_location richloc (gimple_location (call_to_PyList_Append));
       simple_diagnostic_path path (global_dc->printer);
       path.add_event (gimple_location (call_to_PyList_New),
-		      fun->decl, 0,
+		      example_a_fun->decl, 0,
 		      "when %qs fails, returning NULL",
 		      "PyList_New");
       path.add_event (gimple_location (for_cond),
-		      fun->decl, 0,
+		      example_a_fun->decl, 0,
 		      "when %qs", "i < count");
       path.add_event (gimple_location (call_to_PyList_Append),
-		      fun->decl, 0,
+		      example_a_fun->decl, 0,
 		      "when calling %qs, passing NULL as argument %i",
 		      "PyList_Append", 1);
       richloc.set_path (&path);
@@ -147,7 +169,7 @@ pass_test_show_path::execute (function *fun)
   return 0;
 }
 
-static gimple_opt_pass *
+static opt_pass *
 make_pass_test_show_path (gcc::context *ctxt)
 {
   return new pass_test_show_path (ctxt);
@@ -166,9 +188,9 @@ plugin_init (struct plugin_name_args *plugin_info,
     return 1;
 
   pass_info.pass = make_pass_test_show_path (g);
-  pass_info.reference_pass_name = "ssa";
+  pass_info.reference_pass_name = "whole-program";
   pass_info.ref_pass_instance_number = 1;
-  pass_info.pos_op = PASS_POS_INSERT_AFTER;
+  pass_info.pos_op = PASS_POS_INSERT_BEFORE;
   register_callback (plugin_name, PLUGIN_PASS_MANAGER_SETUP, NULL,
 		     &pass_info);
 
-- 
1.8.5.3

