From 344c0bd4f1ce4e20ba1d322edf9fd67dabda6096 Mon Sep 17 00:00:00 2001
From: David Malcolm <dmalcolm@redhat.com>
Date: Thu, 21 Mar 2019 12:18:35 -0400
Subject: [PATCH 133/138] FIXME: add -fdiagnostics-show-path-depths

---
 gcc/common.opt                                     |  4 ++
 gcc/diagnostic.c                                   |  1 +
 gcc/diagnostic.h                                   |  3 +
 gcc/opts.c                                         |  4 ++
 .../gcc.dg/plugin/diagnostic-test-paths-4.c        | 18 ++---
 .../gcc.dg/plugin/diagnostic-test-paths-5.c        | 76 ++++++++++++++++++++++
 gcc/testsuite/gcc.dg/plugin/plugin.exp             |  3 +-
 gcc/toplev.c                                       |  2 +
 gcc/tree-diagnostic.c                              |  2 +-
 9 files changed, 102 insertions(+), 11 deletions(-)
 create mode 100644 gcc/testsuite/gcc.dg/plugin/diagnostic-test-paths-5.c

diff --git a/gcc/common.opt b/gcc/common.opt
index 8a8fc1f..99218d0 100644
--- a/gcc/common.opt
+++ b/gcc/common.opt
@@ -1329,6 +1329,10 @@ fdiagnostics-show-path-summary
 Common Var(flag_diagnostics_show_path_summary) Init(1)
 Show summary of interprocedural paths. (TODO: add to docs)
 
+fdiagnostics-show-path-depths
+Common Var(flag_diagnostics_show_path_depths) Init(0)
+Show stack depths of events in paths. (TODO: add to docs)
+
 fdiagnostics-minimum-margin-width=
 Common Joined UInteger Var(diagnostics_minimum_margin_width) Init(6)
 Set minimum width of left margin of source code when showing source
diff --git a/gcc/diagnostic.c b/gcc/diagnostic.c
index 3f0dccc..00af760 100644
--- a/gcc/diagnostic.c
+++ b/gcc/diagnostic.c
@@ -180,6 +180,7 @@ diagnostic_initialize (diagnostic_context *context, int n_opts)
     context->caret_chars[i] = '^';
   context->show_metadata = false;
   context->show_path_summary = false;
+  context->show_path_depths = false;
   context->show_option_requested = false;
   context->abort_on_error = false;
   context->show_column = false;
diff --git a/gcc/diagnostic.h b/gcc/diagnostic.h
index 0f8ddf7..ba9dfb2 100644
--- a/gcc/diagnostic.h
+++ b/gcc/diagnostic.h
@@ -136,6 +136,9 @@ struct diagnostic_context
   /* FIXME.  */
   bool show_path_summary;
 
+  /* FIXME.  */
+  bool show_path_depths;
+
   /* True if we should print the command line option which controls
      each diagnostic, if known.  */
   bool show_option_requested;
diff --git a/gcc/opts.c b/gcc/opts.c
index aff428a..ef14677 100644
--- a/gcc/opts.c
+++ b/gcc/opts.c
@@ -2408,6 +2408,10 @@ common_handle_option (struct gcc_options *opts,
       dc->show_path_summary = value;
       break;
 
+    case OPT_fdiagnostics_show_path_depths:
+      dc->show_path_depths = value;
+      break;
+
     case OPT_fdiagnostics_show_option:
       dc->show_option_requested = value;
       break;
diff --git a/gcc/testsuite/gcc.dg/plugin/diagnostic-test-paths-4.c b/gcc/testsuite/gcc.dg/plugin/diagnostic-test-paths-4.c
index edd73e5..b7f4c73 100644
--- a/gcc/testsuite/gcc.dg/plugin/diagnostic-test-paths-4.c
+++ b/gcc/testsuite/gcc.dg/plugin/diagnostic-test-paths-4.c
@@ -12,33 +12,33 @@ void wrapped_free (void *ptr)
 {
   free (ptr); /* { dg-warning "double-free of 'ptr' \\\[CWE-415\\]" } */
   /* { dg-begin-multiline-output "" }
-  'test': events 1-2 (depth 0)
+  'test': events 1-2
      | (1): entering 'test'
      | (2): calling 'make_boxed_int'
-     +--> 'make_boxed_int': events 3-4 (depth 1)
+     +--> 'make_boxed_int': events 3-4
                   | (3): entering 'make_boxed_int'
                   | (4): calling 'wrapped_malloc'
-                  +--> 'wrapped_malloc': events 5-6 (depth 2)
+                  +--> 'wrapped_malloc': events 5-6
                                | (5): entering 'wrapped_malloc'
                                | (6): calling 'malloc'
      <-------------------------+
      |
-  'test': event 7 (depth 0)
+  'test': event 7
      | (7): calling 'free_boxed_int'
-     +--> 'free_boxed_int': events 8-9 (depth 1)
+     +--> 'free_boxed_int': events 8-9
                   | (8): entering 'free_boxed_int'
                   | (9): calling 'wrapped_free'
-                  +--> 'wrapped_free': events 10-11 (depth 2)
+                  +--> 'wrapped_free': events 10-11
                               | (10): entering 'wrapped_free'
                               | (11): calling 'free'
      <------------------------+
      |
-  'test': event 12 (depth 0)
+  'test': event 12
      | (12): calling 'free_boxed_int'
-     +--> 'free_boxed_int': events 13-14 (depth 1)
+     +--> 'free_boxed_int': events 13-14
                   | (13): entering 'free_boxed_int'
                   | (14): calling 'wrapped_free'
-                  +--> 'wrapped_free': events 15-16 (depth 2)
+                  +--> 'wrapped_free': events 15-16
                               | (15): entering 'wrapped_free'
                               | (16): calling 'free'
      { dg-end-multiline-output "" } */
diff --git a/gcc/testsuite/gcc.dg/plugin/diagnostic-test-paths-5.c b/gcc/testsuite/gcc.dg/plugin/diagnostic-test-paths-5.c
new file mode 100644
index 0000000..0ccb90e
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/plugin/diagnostic-test-paths-5.c
@@ -0,0 +1,76 @@
+/* As per diagnostics-test-paths-4.c, but with -fdiagnostics-show-path-depths.  */
+
+/* { dg-do compile } */
+/* { dg-options "-fdiagnostics-show-path-depths" } */
+
+#include <stdlib.h>
+
+void *wrapped_malloc (size_t size)
+{
+  return malloc (size);
+}
+
+void wrapped_free (void *ptr)
+{
+  free (ptr); /* { dg-warning "double-free of 'ptr' \\\[CWE-415\\]" } */
+  /* { dg-begin-multiline-output "" }
+  'test': events 1-2 (depth 0)
+     | (1): entering 'test'
+     | (2): calling 'make_boxed_int'
+     +--> 'make_boxed_int': events 3-4 (depth 1)
+                  | (3): entering 'make_boxed_int'
+                  | (4): calling 'wrapped_malloc'
+                  +--> 'wrapped_malloc': events 5-6 (depth 2)
+                               | (5): entering 'wrapped_malloc'
+                               | (6): calling 'malloc'
+     <-------------------------+
+     |
+  'test': event 7 (depth 0)
+     | (7): calling 'free_boxed_int'
+     +--> 'free_boxed_int': events 8-9 (depth 1)
+                  | (8): entering 'free_boxed_int'
+                  | (9): calling 'wrapped_free'
+                  +--> 'wrapped_free': events 10-11 (depth 2)
+                              | (10): entering 'wrapped_free'
+                              | (11): calling 'free'
+     <------------------------+
+     |
+  'test': event 12 (depth 0)
+     | (12): calling 'free_boxed_int'
+     +--> 'free_boxed_int': events 13-14 (depth 1)
+                  | (13): entering 'free_boxed_int'
+                  | (14): calling 'wrapped_free'
+                  +--> 'wrapped_free': events 15-16 (depth 2)
+                              | (15): entering 'wrapped_free'
+                              | (16): calling 'free'
+     { dg-end-multiline-output "" } */
+}
+
+typedef struct boxed_int
+{
+  int i;
+} boxed_int;
+
+boxed_int *
+make_boxed_int (int i)
+{
+  boxed_int *result = (boxed_int *)wrapped_malloc (sizeof (boxed_int));
+  result->i = i;
+  return result;
+}
+
+void
+free_boxed_int (boxed_int *bi)
+{
+  wrapped_free (bi);
+}
+
+void test (int i)
+{
+  boxed_int *obj = make_boxed_int (i);
+
+  free_boxed_int (obj);
+
+  free_boxed_int (obj);
+}
+
diff --git a/gcc/testsuite/gcc.dg/plugin/plugin.exp b/gcc/testsuite/gcc.dg/plugin/plugin.exp
index df7f48d..71e3d22 100644
--- a/gcc/testsuite/gcc.dg/plugin/plugin.exp
+++ b/gcc/testsuite/gcc.dg/plugin/plugin.exp
@@ -60,7 +60,8 @@ set plugin_test_list [list \
 	  diagnostic-test-paths-1.c \
 	  diagnostic-test-paths-2.c \
 	  diagnostic-test-paths-3.c \
-	  diagnostic-test-paths-4.c } \
+	  diagnostic-test-paths-4.c \
+	  diagnostic-test-paths-5.c } \
 ]
 
 foreach plugin_test $plugin_test_list {
diff --git a/gcc/toplev.c b/gcc/toplev.c
index 5b50f15..4a0dc31 100644
--- a/gcc/toplev.c
+++ b/gcc/toplev.c
@@ -1123,6 +1123,8 @@ general_init (const char *argv0, bool init_signals)
     = global_options_init.x_flag_diagnostics_show_metadata;
   global_dc->show_path_summary
     = global_options_init.x_flag_diagnostics_show_path_summary;
+  global_dc->show_path_depths
+    = global_options_init.x_flag_diagnostics_show_path_depths;
   global_dc->show_option_requested
     = global_options_init.x_flag_diagnostics_show_option;
   global_dc->min_margin_width
diff --git a/gcc/tree-diagnostic.c b/gcc/tree-diagnostic.c
index 2ec7720..91d83b0 100644
--- a/gcc/tree-diagnostic.c
+++ b/gcc/tree-diagnostic.c
@@ -654,7 +654,7 @@ tree_print_diagnostic_path (diagnostic_context *context,
       char *saved_prefix = pp_take_prefix (context->printer);
       pp_set_prefix (context->printer, NULL);
       print_path_summary (context->printer,
-			  summary, path, true);
+			  summary, path, context->show_path_depths);
       pp_flush (context->printer);
       pp_set_prefix (context->printer, saved_prefix);
     }
-- 
1.8.5.3

