aboutsummaryrefslogtreecommitdiff
path: root/gcc/testsuite
diff options
context:
space:
mode:
authorDavid Malcolm <dmalcolm@redhat.com>2020-01-10 21:22:12 +0000
committerDavid Malcolm <dmalcolm@gcc.gnu.org>2020-01-10 21:22:12 +0000
commit4bc1899b2e883f926dbda02f5b9a2c06ea30090d (patch)
treeaa499fa988bf6f1172f99666b74375a6647929d9 /gcc/testsuite
parentea69031c5facc70e4a96df83cd58702900fd54b6 (diff)
downloadgcc-4bc1899b2e883f926dbda02f5b9a2c06ea30090d.zip
gcc-4bc1899b2e883f926dbda02f5b9a2c06ea30090d.tar.gz
gcc-4bc1899b2e883f926dbda02f5b9a2c06ea30090d.tar.bz2
Add diagnostic paths
This patch adds support for associating a "diagnostic_path" with a diagnostic: a sequence of events predicted by the compiler that leads to the problem occurring, with their locations in the user's source, text descriptions, and stack information (for handling interprocedural paths). For example, the following (hypothetical) error has a 3-event intraprocedural path: test.c: In function 'demo': test.c:29:5: error: passing NULL as argument 1 to 'PyList_Append' which requires a non-NULL parameter 29 | PyList_Append(list, item); | ^~~~~~~~~~~~~~~~~~~~~~~~~ 'demo': events 1-3 | | 25 | list = PyList_New(0); | | ^~~~~~~~~~~~~ | | | | | (1) when 'PyList_New' fails, returning NULL | 26 | | 27 | for (i = 0; i < count; i++) { | | ~~~ | | | | | (2) when 'i < count' | 28 | item = PyLong_FromLong(random()); | 29 | PyList_Append(list, item); | | ~~~~~~~~~~~~~~~~~~~~~~~~~ | | | | | (3) when calling 'PyList_Append', passing NULL from (1) as argument 1 | The patch adds a new "%@" format code for printing event IDs, so that in the above, the description of event (3) mentions event (1), showing the user where the bogus NULL value comes from (the event IDs are colorized to draw the user's attention to them). There is a separation between data vs presentation: the above shows how the diagnostic-printing code has consolidated the path into a single run of events, since all the events are near each other and within the same function; more complicated examples (such as interprocedural paths) might be printed as multiple runs of events. Examples of how interprocedural paths are printed can be seen in the test suite (which uses a plugin to exercise the code without relying on specific warnings using this functionality). Other output formats include - JSON, - printing each event as a separate "note", and - to not emit paths. gcc/ChangeLog: * Makefile.in (OBJS): Add tree-diagnostic-path.o. * common.opt (fdiagnostics-path-format=): New option. (diagnostic_path_format): New enum. (fdiagnostics-show-path-depths): New option. * coretypes.h (diagnostic_event_id_t): New forward decl. * diagnostic-color.c (color_dict): Add "path". * diagnostic-event-id.h: New file. * diagnostic-format-json.cc (json_from_expanded_location): Make non-static. (json_end_diagnostic): Call context->make_json_for_path if it exists and the diagnostic has a path. (diagnostic_output_format_init): Clear context->print_path. * diagnostic-path.h: New file. * diagnostic-show-locus.c (colorizer::set_range): Special-case when printing a run of events in a diagnostic_path so that they all get the same color. (layout::m_diagnostic_path_p): New field. (layout::layout): Initialize it. (layout::print_any_labels): Don't colorize the label text for an event in a diagnostic_path. (gcc_rich_location::add_location_if_nearby): Add "restrict_to_current_line_spans" and "label" params. Pass the former to layout.maybe_add_location_range; pass the latter when calling add_range. * diagnostic.c: Include "diagnostic-path.h". (diagnostic_initialize): Initialize context->path_format and context->show_path_depths. (diagnostic_show_any_path): New function. (diagnostic_path::interprocedural_p): New function. (diagnostic_report_diagnostic): Call diagnostic_show_any_path. (simple_diagnostic_path::num_events): New function. (simple_diagnostic_path::get_event): New function. (simple_diagnostic_path::add_event): New function. (simple_diagnostic_event::simple_diagnostic_event): New ctor. (simple_diagnostic_event::~simple_diagnostic_event): New dtor. (debug): New overload taking a diagnostic_path *. * diagnostic.def (DK_DIAGNOSTIC_PATH): New. * diagnostic.h (enum diagnostic_path_format): New enum. (json::value): New forward decl. (diagnostic_context::path_format): New field. (diagnostic_context::show_path_depths): New field. (diagnostic_context::print_path): New callback field. (diagnostic_context::make_json_for_path): New callback field. (diagnostic_show_any_path): New decl. (json_from_expanded_location): New decl. * doc/invoke.texi (-fdiagnostics-path-format=): New option. (-fdiagnostics-show-path-depths): New option. (-fdiagnostics-color): Add "path" to description of default GCC_COLORS; describe it. (-fdiagnostics-format=json): Document how diagnostic paths are represented in the JSON output format. * gcc-rich-location.h (gcc_rich_location::add_location_if_nearby): Add optional params "restrict_to_current_line_spans" and "label". * opts.c (common_handle_option): Handle OPT_fdiagnostics_path_format_ and OPT_fdiagnostics_show_path_depths. * pretty-print.c: Include "diagnostic-event-id.h". (pp_format): Implement "%@" format code for printing diagnostic_event_id_t *. (selftest::test_pp_format): Add tests for "%@". * selftest-run-tests.c (selftest::run_tests): Call selftest::tree_diagnostic_path_cc_tests. * selftest.h (selftest::tree_diagnostic_path_cc_tests): New decl. * toplev.c (general_init): Initialize global_dc->path_format and global_dc->show_path_depths. * tree-diagnostic-path.cc: New file. * tree-diagnostic.c (maybe_unwind_expanded_macro_loc): Make non-static. Drop "diagnostic" param in favor of storing the original value of "where" and re-using it. (virt_loc_aware_diagnostic_finalizer): Update for dropped param of maybe_unwind_expanded_macro_loc. (tree_diagnostics_defaults): Initialize context->print_path and context->make_json_for_path. * tree-diagnostic.h (default_tree_diagnostic_path_printer): New decl. (default_tree_make_json_for_path): New decl. (maybe_unwind_expanded_macro_loc): New decl. gcc/c-family/ChangeLog: * c-format.c (local_event_ptr_node): New. (PP_FORMAT_CHAR_TABLE): Add entry for "%@". (init_dynamic_diag_info): Initialize local_event_ptr_node. * c-format.h (T_EVENT_PTR): New define. gcc/testsuite/ChangeLog: * gcc.dg/format/gcc_diag-10.c (diagnostic_event_id_t): New typedef. (test_diag): Add coverage of "%@". * gcc.dg/plugin/diagnostic-path-format-default.c: New test. * gcc.dg/plugin/diagnostic-path-format-inline-events-1.c: New test. * gcc.dg/plugin/diagnostic-path-format-inline-events-2.c: New test. * gcc.dg/plugin/diagnostic-path-format-inline-events-3.c: New test. * gcc.dg/plugin/diagnostic-path-format-none.c: New test. * gcc.dg/plugin/diagnostic-test-paths-1.c: New test. * gcc.dg/plugin/diagnostic-test-paths-2.c: New test. * gcc.dg/plugin/diagnostic-test-paths-3.c: New test. * gcc.dg/plugin/diagnostic-test-paths-4.c: New test. * gcc.dg/plugin/diagnostic_plugin_test_paths.c: New. * gcc.dg/plugin/plugin.exp: Add the new plugin and test cases. libcpp/ChangeLog: * include/line-map.h (class diagnostic_path): New forward decl. (rich_location::get_path): New accessor. (rich_location::set_path): New function. (rich_location::m_path): New field. * line-map.c (rich_location::rich_location): Initialize m_path. From-SVN: r280142
Diffstat (limited to 'gcc/testsuite')
-rw-r--r--gcc/testsuite/ChangeLog17
-rw-r--r--gcc/testsuite/gcc.dg/format/gcc_diag-10.c6
-rw-r--r--gcc/testsuite/gcc.dg/plugin/diagnostic-path-format-default.c142
-rw-r--r--gcc/testsuite/gcc.dg/plugin/diagnostic-path-format-inline-events-1.c142
-rw-r--r--gcc/testsuite/gcc.dg/plugin/diagnostic-path-format-inline-events-2.c154
-rw-r--r--gcc/testsuite/gcc.dg/plugin/diagnostic-path-format-inline-events-3.c154
-rw-r--r--gcc/testsuite/gcc.dg/plugin/diagnostic-path-format-none.c43
-rw-r--r--gcc/testsuite/gcc.dg/plugin/diagnostic-path-format-separate-events.c44
-rw-r--r--gcc/testsuite/gcc.dg/plugin/diagnostic-test-paths-1.c38
-rw-r--r--gcc/testsuite/gcc.dg/plugin/diagnostic-test-paths-2.c56
-rw-r--r--gcc/testsuite/gcc.dg/plugin/diagnostic-test-paths-3.c38
-rw-r--r--gcc/testsuite/gcc.dg/plugin/diagnostic-test-paths-4.c84
-rw-r--r--gcc/testsuite/gcc.dg/plugin/diagnostic_plugin_test_paths.c460
-rw-r--r--gcc/testsuite/gcc.dg/plugin/plugin.exp11
14 files changed, 1388 insertions, 1 deletions
diff --git a/gcc/testsuite/ChangeLog b/gcc/testsuite/ChangeLog
index 527d53b..6576aee 100644
--- a/gcc/testsuite/ChangeLog
+++ b/gcc/testsuite/ChangeLog
@@ -21,6 +21,23 @@
2020-01-10 David Malcolm <dmalcolm@redhat.com>
+ * gcc.dg/format/gcc_diag-10.c (diagnostic_event_id_t): New
+ typedef.
+ (test_diag): Add coverage of "%@".
+ * gcc.dg/plugin/diagnostic-path-format-default.c: New test.
+ * gcc.dg/plugin/diagnostic-path-format-inline-events-1.c: New test.
+ * gcc.dg/plugin/diagnostic-path-format-inline-events-2.c: New test.
+ * gcc.dg/plugin/diagnostic-path-format-inline-events-3.c: New test.
+ * gcc.dg/plugin/diagnostic-path-format-none.c: New test.
+ * gcc.dg/plugin/diagnostic-test-paths-1.c: New test.
+ * gcc.dg/plugin/diagnostic-test-paths-2.c: New test.
+ * gcc.dg/plugin/diagnostic-test-paths-3.c: New test.
+ * gcc.dg/plugin/diagnostic-test-paths-4.c: New test.
+ * gcc.dg/plugin/diagnostic_plugin_test_paths.c: New.
+ * gcc.dg/plugin/plugin.exp: Add the new plugin and test cases.
+
+2020-01-10 David Malcolm <dmalcolm@redhat.com>
+
* lib/gcc-dg.exp (cleanup-after-saved-dg-test): Reset global
nn_line_numbers_enabled.
* lib/multiline.exp (nn_line_numbers_enabled): New global.
diff --git a/gcc/testsuite/gcc.dg/format/gcc_diag-10.c b/gcc/testsuite/gcc.dg/format/gcc_diag-10.c
index ba2629b..a2f99fe 100644
--- a/gcc/testsuite/gcc.dg/format/gcc_diag-10.c
+++ b/gcc/testsuite/gcc.dg/format/gcc_diag-10.c
@@ -22,6 +22,9 @@ typedef struct gimple gimple;
/* Likewise for gimple. */
typedef struct cgraph_node cgraph_node;
+/* Likewise for diagnostic_event_id_t. */
+typedef struct diagnostic_event_id_t diagnostic_event_id_t;
+
#define FORMAT(kind) __attribute__ ((format (__gcc_## kind ##__, 1, 2)))
void diag (const char*, ...) FORMAT (diag);
@@ -30,7 +33,7 @@ void tdiag (const char*, ...) FORMAT (tdiag);
void cxxdiag (const char*, ...) FORMAT (cxxdiag);
void dump (const char*, ...) FORMAT (dump_printf);
-void test_diag (tree t, gimple *gc)
+void test_diag (tree t, gimple *gc, diagnostic_event_id_t *event_id_ptr)
{
diag ("%<"); /* { dg-warning "unterminated quoting directive" } */
diag ("%>"); /* { dg-warning "unmatched quoting directive " } */
@@ -38,6 +41,7 @@ void test_diag (tree t, gimple *gc)
diag ("%G", gc); /* { dg-warning "format" } */
diag ("%K", t); /* { dg-warning "format" } */
+ diag ("%@", event_id_ptr);
diag ("%R"); /* { dg-warning "unmatched color reset directive" } */
diag ("%r", ""); /* { dg-warning "unterminated color directive" } */
diff --git a/gcc/testsuite/gcc.dg/plugin/diagnostic-path-format-default.c b/gcc/testsuite/gcc.dg/plugin/diagnostic-path-format-default.c
new file mode 100644
index 0000000..5712dbd
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/plugin/diagnostic-path-format-default.c
@@ -0,0 +1,142 @@
+/* { dg-do compile } */
+/* { dg-options "-fdiagnostics-show-caret" } */
+
+#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 "" }
+ free (ptr);
+ ^~~~~~~~~~
+ 'test': events 1-2
+ |
+ | {
+ | ^
+ | |
+ | (1) entering 'test'
+ | boxed_int *obj = make_boxed_int (i);
+ | ~~~~~~~~~~~~~~~~~~
+ | |
+ | (2) calling 'make_boxed_int'
+ |
+ +--> 'make_boxed_int': events 3-4
+ |
+ | {
+ | ^
+ | |
+ | (3) entering 'make_boxed_int'
+ | boxed_int *result = (boxed_int *)wrapped_malloc (sizeof (boxed_int));
+ | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ | |
+ | (4) calling 'wrapped_malloc'
+ |
+ +--> 'wrapped_malloc': events 5-6
+ |
+ | {
+ | ^
+ | |
+ | (5) entering 'wrapped_malloc'
+ | return malloc (size);
+ | ~~~~~~~~~~~~~
+ | |
+ | (6) calling 'malloc'
+ |
+ <-------------+
+ |
+ 'test': event 7
+ |
+ | free_boxed_int (obj);
+ | ^~~~~~~~~~~~~~~~~~~~
+ | |
+ | (7) calling 'free_boxed_int'
+ |
+ +--> 'free_boxed_int': events 8-9
+ |
+ | {
+ | ^
+ | |
+ | (8) entering 'free_boxed_int'
+ | wrapped_free (bi);
+ | ~~~~~~~~~~~~~~~~~
+ | |
+ | (9) calling 'wrapped_free'
+ |
+ +--> 'wrapped_free': events 10-11
+ |
+ | {
+ | ^
+ | |
+ | (10) entering 'wrapped_free'
+ | free (ptr);
+ | ~~~~~~~~~~
+ | |
+ | (11) calling 'free'
+ |
+ <-------------+
+ |
+ 'test': event 12
+ |
+ | free_boxed_int (obj);
+ | ^~~~~~~~~~~~~~~~~~~~
+ | |
+ | (12) calling 'free_boxed_int'
+ |
+ +--> 'free_boxed_int': events 13-14
+ |
+ | {
+ | ^
+ | |
+ | (13) entering 'free_boxed_int'
+ | wrapped_free (bi);
+ | ~~~~~~~~~~~~~~~~~
+ | |
+ | (14) calling 'wrapped_free'
+ |
+ +--> 'wrapped_free': events 15-16
+ |
+ | {
+ | ^
+ | |
+ | (15) entering 'wrapped_free'
+ | free (ptr);
+ | ~~~~~~~~~~
+ | |
+ | (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/diagnostic-path-format-inline-events-1.c b/gcc/testsuite/gcc.dg/plugin/diagnostic-path-format-inline-events-1.c
new file mode 100644
index 0000000..430d817
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/plugin/diagnostic-path-format-inline-events-1.c
@@ -0,0 +1,142 @@
+/* { dg-do compile } */
+/* { dg-options "-fdiagnostics-path-format=inline-events -fdiagnostics-show-caret" } */
+
+#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 "" }
+ free (ptr);
+ ^~~~~~~~~~
+ 'test': events 1-2
+ |
+ | {
+ | ^
+ | |
+ | (1) entering 'test'
+ | boxed_int *obj = make_boxed_int (i);
+ | ~~~~~~~~~~~~~~~~~~
+ | |
+ | (2) calling 'make_boxed_int'
+ |
+ +--> 'make_boxed_int': events 3-4
+ |
+ | {
+ | ^
+ | |
+ | (3) entering 'make_boxed_int'
+ | boxed_int *result = (boxed_int *)wrapped_malloc (sizeof (boxed_int));
+ | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ | |
+ | (4) calling 'wrapped_malloc'
+ |
+ +--> 'wrapped_malloc': events 5-6
+ |
+ | {
+ | ^
+ | |
+ | (5) entering 'wrapped_malloc'
+ | return malloc (size);
+ | ~~~~~~~~~~~~~
+ | |
+ | (6) calling 'malloc'
+ |
+ <-------------+
+ |
+ 'test': event 7
+ |
+ | free_boxed_int (obj);
+ | ^~~~~~~~~~~~~~~~~~~~
+ | |
+ | (7) calling 'free_boxed_int'
+ |
+ +--> 'free_boxed_int': events 8-9
+ |
+ | {
+ | ^
+ | |
+ | (8) entering 'free_boxed_int'
+ | wrapped_free (bi);
+ | ~~~~~~~~~~~~~~~~~
+ | |
+ | (9) calling 'wrapped_free'
+ |
+ +--> 'wrapped_free': events 10-11
+ |
+ | {
+ | ^
+ | |
+ | (10) entering 'wrapped_free'
+ | free (ptr);
+ | ~~~~~~~~~~
+ | |
+ | (11) calling 'free'
+ |
+ <-------------+
+ |
+ 'test': event 12
+ |
+ | free_boxed_int (obj);
+ | ^~~~~~~~~~~~~~~~~~~~
+ | |
+ | (12) calling 'free_boxed_int'
+ |
+ +--> 'free_boxed_int': events 13-14
+ |
+ | {
+ | ^
+ | |
+ | (13) entering 'free_boxed_int'
+ | wrapped_free (bi);
+ | ~~~~~~~~~~~~~~~~~
+ | |
+ | (14) calling 'wrapped_free'
+ |
+ +--> 'wrapped_free': events 15-16
+ |
+ | {
+ | ^
+ | |
+ | (15) entering 'wrapped_free'
+ | free (ptr);
+ | ~~~~~~~~~~
+ | |
+ | (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/diagnostic-path-format-inline-events-2.c b/gcc/testsuite/gcc.dg/plugin/diagnostic-path-format-inline-events-2.c
new file mode 100644
index 0000000..c2bfabe
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/plugin/diagnostic-path-format-inline-events-2.c
@@ -0,0 +1,154 @@
+/* { dg-do compile } */
+/* { dg-options "-fdiagnostics-path-format=inline-events -fdiagnostics-show-caret" } */
+
+/* Verify that 'inline-events' copes gracefully with events with an
+ unknown location. */
+
+#include <stdlib.h>
+
+extern void missing_location ();
+
+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 "" }
+ free (ptr);
+ ^~~~~~~~~~
+ 'test': events 1-2
+ |
+ | {
+ | ^
+ | |
+ | (1) entering 'test'
+ | boxed_int *obj = make_boxed_int (i);
+ | ~~~~~~~~~~~~~~~~~~
+ | |
+ | (2) calling 'make_boxed_int'
+ |
+ +--> 'make_boxed_int': events 3-4
+ |
+ | {
+ | ^
+ | |
+ | (3) entering 'make_boxed_int'
+ | boxed_int *result = (boxed_int *)wrapped_malloc (sizeof (boxed_int));
+ | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ | |
+ | (4) calling 'wrapped_malloc'
+ |
+ +--> 'wrapped_malloc': events 5-6
+ |
+ | {
+ | ^
+ | |
+ | (5) entering 'wrapped_malloc'
+ | return malloc (size);
+ | ~~~~~~~~~~~~~
+ | |
+ | (6) calling 'malloc'
+ |
+ <-------------+
+ |
+ 'test': event 7
+ |
+ | free_boxed_int (obj);
+ | ^~~~~~~~~~~~~~~~~~~~
+ | |
+ | (7) calling 'free_boxed_int'
+ |
+ +--> 'free_boxed_int': events 8-9
+ |
+ | {
+ | ^
+ | |
+ | (8) entering 'free_boxed_int'
+ | wrapped_free (bi);
+ | ~~~~~~~~~~~~~~~~~
+ | |
+ | (9) calling 'wrapped_free'
+ |
+ +--> 'wrapped_free': events 10-11
+ |
+ | {
+ | ^
+ | |
+ | (10) entering 'wrapped_free'
+ | free (ptr);
+ | ~~~~~~~~~~
+ | |
+ | (11) calling 'free'
+ |
+ <-------------+
+ |
+ 'test': event 12
+ |
+ |cc1:
+ | (12): calling 'missing_location'
+ |
+ 'test': event 13
+ |
+ | free_boxed_int (obj);
+ | ^~~~~~~~~~~~~~~~~~~~
+ | |
+ | (13) calling 'free_boxed_int'
+ |
+ +--> 'free_boxed_int': events 14-15
+ |
+ | {
+ | ^
+ | |
+ | (14) entering 'free_boxed_int'
+ | wrapped_free (bi);
+ | ~~~~~~~~~~~~~~~~~
+ | |
+ | (15) calling 'wrapped_free'
+ |
+ +--> 'wrapped_free': events 16-17
+ |
+ | {
+ | ^
+ | |
+ | (16) entering 'wrapped_free'
+ | free (ptr);
+ | ~~~~~~~~~~
+ | |
+ | (17) 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);
+
+ missing_location ();
+
+ free_boxed_int (obj);
+}
+
diff --git a/gcc/testsuite/gcc.dg/plugin/diagnostic-path-format-inline-events-3.c b/gcc/testsuite/gcc.dg/plugin/diagnostic-path-format-inline-events-3.c
new file mode 100644
index 0000000..386cac9
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/plugin/diagnostic-path-format-inline-events-3.c
@@ -0,0 +1,154 @@
+/* { dg-do compile } */
+/* { dg-options "-fdiagnostics-path-format=inline-events -fdiagnostics-show-line-numbers -fdiagnostics-show-caret" } */
+/* { dg-enable-nn-line-numbers "" } */
+
+/* Verify the interaction of inline-events with line numbers. */
+
+#include <stdlib.h>
+
+extern void missing_location ();
+
+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 "" }
+ NN | free (ptr);
+ | ^~~~~~~~~~
+ 'test': events 1-2
+ |
+ | NN | {
+ | | ^
+ | | |
+ | | (1) entering 'test'
+ | NN | boxed_int *obj = make_boxed_int (i);
+ | | ~~~~~~~~~~~~~~~~~~
+ | | |
+ | | (2) calling 'make_boxed_int'
+ |
+ +--> 'make_boxed_int': events 3-4
+ |
+ | NN | {
+ | | ^
+ | | |
+ | | (3) entering 'make_boxed_int'
+ | NN | boxed_int *result = (boxed_int *)wrapped_malloc (sizeof (boxed_int));
+ | | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ | | |
+ | | (4) calling 'wrapped_malloc'
+ |
+ +--> 'wrapped_malloc': events 5-6
+ |
+ | NN | {
+ | | ^
+ | | |
+ | | (5) entering 'wrapped_malloc'
+ | NN | return malloc (size);
+ | | ~~~~~~~~~~~~~
+ | | |
+ | | (6) calling 'malloc'
+ |
+ <-------------+
+ |
+ 'test': event 7
+ |
+ | NN | free_boxed_int (obj);
+ | | ^~~~~~~~~~~~~~~~~~~~
+ | | |
+ | | (7) calling 'free_boxed_int'
+ |
+ +--> 'free_boxed_int': events 8-9
+ |
+ | NN | {
+ | | ^
+ | | |
+ | | (8) entering 'free_boxed_int'
+ | NN | wrapped_free (bi);
+ | | ~~~~~~~~~~~~~~~~~
+ | | |
+ | | (9) calling 'wrapped_free'
+ |
+ +--> 'wrapped_free': events 10-11
+ |
+ | NN | {
+ | | ^
+ | | |
+ | | (10) entering 'wrapped_free'
+ | NN | free (ptr);
+ | | ~~~~~~~~~~
+ | | |
+ | | (11) calling 'free'
+ |
+ <-------------+
+ |
+ 'test': event 12
+ |
+ |cc1:
+ | (12): calling 'missing_location'
+ |
+ 'test': event 13
+ |
+ | NN | free_boxed_int (obj);
+ | | ^~~~~~~~~~~~~~~~~~~~
+ | | |
+ | | (13) calling 'free_boxed_int'
+ |
+ +--> 'free_boxed_int': events 14-15
+ |
+ | NN | {
+ | | ^
+ | | |
+ | | (14) entering 'free_boxed_int'
+ | NN | wrapped_free (bi);
+ | | ~~~~~~~~~~~~~~~~~
+ | | |
+ | | (15) calling 'wrapped_free'
+ |
+ +--> 'wrapped_free': events 16-17
+ |
+ | NN | {
+ | | ^
+ | | |
+ | | (16) entering 'wrapped_free'
+ | NN | free (ptr);
+ | | ~~~~~~~~~~
+ | | |
+ | | (17) 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);
+
+ missing_location ();
+
+ free_boxed_int (obj);
+}
+
diff --git a/gcc/testsuite/gcc.dg/plugin/diagnostic-path-format-none.c b/gcc/testsuite/gcc.dg/plugin/diagnostic-path-format-none.c
new file mode 100644
index 0000000..0a29f67
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/plugin/diagnostic-path-format-none.c
@@ -0,0 +1,43 @@
+/* { dg-do compile } */
+/* { dg-options "-fdiagnostics-path-format=none" } */
+
+#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\\]" } */
+}
+
+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/diagnostic-path-format-separate-events.c b/gcc/testsuite/gcc.dg/plugin/diagnostic-path-format-separate-events.c
new file mode 100644
index 0000000..dcb72c0
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/plugin/diagnostic-path-format-separate-events.c
@@ -0,0 +1,44 @@
+/* { dg-do compile } */
+/* { dg-options "-fdiagnostics-path-format=separate-events" } */
+
+#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\\]" } */
+}
+
+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)
+{ /* { dg-message "\\(1\\) entering 'test'" } */
+ boxed_int *obj = make_boxed_int (i); /* { dg-message "\\(2\\) calling 'make_boxed_int'" } */
+ /* etc */
+
+ free_boxed_int (obj);
+
+ free_boxed_int (obj);
+}
+
diff --git a/gcc/testsuite/gcc.dg/plugin/diagnostic-test-paths-1.c b/gcc/testsuite/gcc.dg/plugin/diagnostic-test-paths-1.c
new file mode 100644
index 0000000..7b11c90
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/plugin/diagnostic-test-paths-1.c
@@ -0,0 +1,38 @@
+/* { dg-do compile } */
+/* { dg-options "-fdiagnostics-path-format=separate-events" } */
+
+#include <stddef.h>
+#include <stdlib.h>
+
+/* Minimal reimplementation of cpython API. */
+typedef struct PyObject {} PyObject;
+extern int PyArg_ParseTuple (PyObject *args, const char *fmt, ...);
+extern PyObject *PyList_New (int);
+extern PyObject *PyLong_FromLong(long);
+extern void PyList_Append(PyObject *list, PyObject *item);
+
+PyObject *
+make_a_list_of_random_ints_badly(PyObject *self,
+ PyObject *args)
+{
+ PyObject *list, *item;
+ long count, i;
+
+ if (!PyArg_ParseTuple(args, "i", &count)) {
+ return NULL;
+ }
+
+ list = PyList_New(0); /* { dg-line PyList_New } */
+
+ for (i = 0; i < count; i++) { /* { dg-line for } */
+ item = PyLong_FromLong(random());
+ PyList_Append(list, item); /* { dg-line PyList_Append } */
+ }
+
+ return list;
+
+ /* { dg-error "passing NULL as argument 1 to 'PyList_Append' which requires a non-NULL parameter" "" { target *-*-* } PyList_Append } */
+ /* { dg-message "\\(1\\) when 'PyList_New' fails, returning NULL" "" { target *-*-* } PyList_New } */
+ /* { dg-message "\\(2\\) when 'i < count'" "" { target *-*-* } for } */
+ /* { dg-message "\\(3\\) when calling 'PyList_Append', passing NULL from \\(1\\) as argument 1" "" { target *-*-* } PyList_Append } */
+}
diff --git a/gcc/testsuite/gcc.dg/plugin/diagnostic-test-paths-2.c b/gcc/testsuite/gcc.dg/plugin/diagnostic-test-paths-2.c
new file mode 100644
index 0000000..391aeb9
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/plugin/diagnostic-test-paths-2.c
@@ -0,0 +1,56 @@
+/* { dg-do compile } */
+/* { dg-options "-fdiagnostics-show-caret -fdiagnostics-show-line-numbers" } */
+
+#include <stddef.h>
+#include <stdlib.h>
+
+/* Minimal reimplementation of cpython API. */
+typedef struct PyObject {} PyObject;
+extern int PyArg_ParseTuple (PyObject *args, const char *fmt, ...);
+extern PyObject *PyList_New (int);
+extern PyObject *PyLong_FromLong(long);
+extern void PyList_Append(PyObject *list, PyObject *item);
+
+PyObject *
+make_a_list_of_random_ints_badly(PyObject *self,
+ PyObject *args)
+{
+ PyObject *list, *item;
+ long count, i;
+
+ if (!PyArg_ParseTuple(args, "i", &count)) {
+ return NULL;
+ }
+
+ list = PyList_New(0); /* { dg-line PyList_New } */
+
+ for (i = 0; i < count; i++) {
+ item = PyLong_FromLong(random());
+ PyList_Append(list, item); /* { dg-line PyList_Append } */
+ }
+
+ return list;
+
+ /* { dg-error "passing NULL as argument 1 to 'PyList_Append' which requires a non-NULL parameter" "" { target *-*-* } PyList_Append } */
+ /* { dg-begin-multiline-output "" }
+ 29 | PyList_Append(list, item);
+ | ^~~~~~~~~~~~~~~~~~~~~~~~~
+ 'make_a_list_of_random_ints_badly': events 1-3
+ |
+ | 25 | list = PyList_New(0);
+ | | ^~~~~~~~~~~~~
+ | | |
+ | | (1) when 'PyList_New' fails, returning NULL
+ | 26 |
+ | 27 | for (i = 0; i < count; i++) {
+ | | ~~~
+ | | |
+ | | (2) when 'i < count'
+ | 28 | item = PyLong_FromLong(random());
+ | 29 | PyList_Append(list, item);
+ | | ~~~~~~~~~~~~~~~~~~~~~~~~~
+ | | |
+ | | (3) when calling 'PyList_Append', passing NULL from (1) as argument 1
+ |
+ { dg-end-multiline-output "" } */
+}
diff --git a/gcc/testsuite/gcc.dg/plugin/diagnostic-test-paths-3.c b/gcc/testsuite/gcc.dg/plugin/diagnostic-test-paths-3.c
new file mode 100644
index 0000000..6971d7c
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/plugin/diagnostic-test-paths-3.c
@@ -0,0 +1,38 @@
+/* { dg-do compile } */
+/* { dg-options "-fdiagnostics-format=json" } */
+
+#include <stddef.h>
+#include <stdlib.h>
+
+/* Minimal reimplementation of cpython API. */
+typedef struct PyObject {} PyObject;
+extern int PyArg_ParseTuple (PyObject *args, const char *fmt, ...);
+extern PyObject *PyList_New (int);
+extern PyObject *PyLong_FromLong(long);
+extern void PyList_Append(PyObject *list, PyObject *item);
+
+PyObject *
+make_a_list_of_random_ints_badly(PyObject *self,
+ PyObject *args)
+{
+ PyObject *list, *item;
+ long count, i;
+
+ if (!PyArg_ParseTuple(args, "i", &count)) {
+ return NULL;
+ }
+
+ list = PyList_New(0);
+
+ for (i = 0; i < count; i++) {
+ item = PyLong_FromLong(random());
+ PyList_Append(list, item);
+ }
+
+ return list;
+}
+
+/* FIXME: test the events within a path. */
+/* { dg-regexp "\"kind\": \"error\"" } */
+/* { dg-regexp "\"path\": " } */
+/* { dg-regexp ".*" } */
diff --git a/gcc/testsuite/gcc.dg/plugin/diagnostic-test-paths-4.c b/gcc/testsuite/gcc.dg/plugin/diagnostic-test-paths-4.c
new file mode 100644
index 0000000..847b6d4
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/plugin/diagnostic-test-paths-4.c
@@ -0,0 +1,84 @@
+/* { dg-do compile } */
+/* { dg-options "-fdiagnostics-path-format=inline-events -fdiagnostics-show-caret -fdiagnostics-show-line-numbers" } */
+/* { dg-enable-nn-line-numbers "" } */
+
+#include <stdio.h>
+#include <signal.h>
+#include <stdlib.h>
+
+extern void body_of_program(void);
+
+void custom_logger(const char *msg)
+{
+ fprintf(stderr, "LOG: %s", msg); /* { dg-warning "call to 'fprintf' from within signal handler" } */
+}
+
+static void int_handler(int signum)
+{
+ custom_logger("got signal");
+}
+
+static void register_handler ()
+{
+ signal(SIGINT, int_handler);
+}
+
+void test (void)
+{
+ register_handler ();
+ body_of_program();
+}
+
+/* { dg-begin-multiline-output "" }
+ NN | fprintf(stderr, "LOG: %s", msg);
+ | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ 'test': events 1-2
+ |
+ | NN | {
+ | | ^
+ | | |
+ | | (1) entering 'test'
+ | NN | register_handler ();
+ | | ~~~~~~~~~~~~~~~~~~~
+ | | |
+ | | (2) calling 'register_handler'
+ |
+ +--> 'register_handler': events 3-4
+ |
+ | NN | {
+ | | ^
+ | | |
+ | | (3) entering 'register_handler'
+ | NN | signal(SIGINT, int_handler);
+ | | ~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ | | |
+ | | (4) registering 'int_handler' as signal handler
+ |
+ event 5
+ |
+ |cc1:
+ | (5): later on, when the signal is delivered to the process
+ |
+ +--> 'int_handler': events 6-7
+ |
+ | NN | {
+ | | ^
+ | | |
+ | | (6) entering 'int_handler'
+ | NN | custom_logger("got signal");
+ | | ~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ | | |
+ | | (7) calling 'custom_logger'
+ |
+ +--> 'custom_logger': events 8-9
+ |
+ | NN | {
+ | | ^
+ | | |
+ | | (8) entering 'custom_logger'
+ | NN | fprintf(stderr, "LOG: %s", msg);
+ | | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ | | |
+ | | (9) calling 'fprintf'
+ |
+ { dg-end-multiline-output "" } */
diff --git a/gcc/testsuite/gcc.dg/plugin/diagnostic_plugin_test_paths.c b/gcc/testsuite/gcc.dg/plugin/diagnostic_plugin_test_paths.c
new file mode 100644
index 0000000..cf05ca3
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/plugin/diagnostic_plugin_test_paths.c
@@ -0,0 +1,460 @@
+/* { dg-options "-O" } */
+
+/* This plugin exercises the path-printing code.
+
+ The goal is to unit-test the path-printing code without needing any
+ specific tests within the compiler's IR. We can't use any real
+ diagnostics for this, so we have to fake it, hence this plugin. */
+
+#include "gcc-plugin.h"
+#include "config.h"
+#include "system.h"
+#include "coretypes.h"
+#include "tm.h"
+#include "tree.h"
+#include "stringpool.h"
+#include "toplev.h"
+#include "basic-block.h"
+#include "hash-table.h"
+#include "vec.h"
+#include "ggc.h"
+#include "basic-block.h"
+#include "tree-ssa-alias.h"
+#include "internal-fn.h"
+#include "gimple-fold.h"
+#include "tree-eh.h"
+#include "gimple-expr.h"
+#include "is-a.h"
+#include "gimple.h"
+#include "gimple-iterator.h"
+#include "tree.h"
+#include "tree-pass.h"
+#include "intl.h"
+#include "plugin-version.h"
+#include "diagnostic.h"
+#include "diagnostic-path.h"
+#include "diagnostic-metadata.h"
+#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 =
+{
+ IPA_PASS, /* type */
+ "test_show_path", /* name */
+ OPTGROUP_NONE, /* optinfo_flags */
+ TV_NONE, /* tv_id */
+ PROP_ssa, /* properties_required */
+ 0, /* properties_provided */
+ 0, /* properties_destroyed */
+ 0, /* todo_flags_start */
+ 0, /* todo_flags_finish */
+};
+
+class pass_test_show_path : public ipa_opt_pass_d
+{
+public:
+ pass_test_show_path(gcc::context *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: */
+ bool gate (function *) { return true; }
+ virtual unsigned int execute (function *);
+
+}; // class pass_test_show_path
+
+/* Determine if STMT is a call with NUM_ARGS arguments to a function
+ named FUNCNAME.
+ If so, return STMT as a gcall *. Otherwise return NULL. */
+
+static gcall *
+check_for_named_call (gimple *stmt,
+ const char *funcname, unsigned int num_args)
+{
+ gcc_assert (funcname);
+
+ gcall *call = dyn_cast <gcall *> (stmt);
+ if (!call)
+ return NULL;
+
+ tree fndecl = gimple_call_fndecl (call);
+ if (!fndecl)
+ return NULL;
+
+ if (strcmp (IDENTIFIER_POINTER (DECL_NAME (fndecl)), funcname))
+ return NULL;
+
+ if (gimple_call_num_args (call) != num_args)
+ {
+ error_at (stmt->location, "expected number of args: %i (got %i)",
+ num_args, gimple_call_num_args (call));
+ return NULL;
+ }
+
+ return call;
+}
+
+/* Example 1: a purely intraprocedural path. */
+
+static void
+example_1 ()
+{
+ gimple_stmt_iterator gsi;
+ basic_block bb;
+
+ gcall *call_to_PyList_Append = NULL;
+ gcall *call_to_PyList_New = NULL;
+ gcond *for_cond = NULL;
+ function *example_a_fun = NULL;
+
+ 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)
+ {
+ auto_diagnostic_group d;
+ gcc_rich_location richloc (gimple_location (call_to_PyList_Append));
+ simple_diagnostic_path path (global_dc->printer);
+ diagnostic_event_id_t alloc_event_id
+ = path.add_event (gimple_location (call_to_PyList_New),
+ example_a_fun->decl, 0,
+ "when %qs fails, returning NULL",
+ "PyList_New");
+ path.add_event (gimple_location (for_cond),
+ example_a_fun->decl, 0,
+ "when %qs", "i < count");
+ path.add_event (gimple_location (call_to_PyList_Append),
+ example_a_fun->decl, 0,
+ "when calling %qs, passing NULL from %@ as argument %i",
+ "PyList_Append", &alloc_event_id, 1);
+ richloc.set_path (&path);
+ error_at (&richloc,
+ "passing NULL as argument %i to %qs"
+ " which requires a non-NULL parameter",
+ 1, "PyList_Append");
+ }
+}
+
+/* A (function, location_t) pair. */
+
+struct event_location_t
+{
+ event_location_t ()
+ : m_fun (NULL), m_loc (UNKNOWN_LOCATION)
+ {}
+
+ event_location_t (function *fun, location_t loc)
+ : m_fun (fun), m_loc (loc)
+ {}
+
+ void set (const gimple *stmt, function *fun)
+ {
+ m_fun = fun;
+ m_loc = gimple_location (stmt);
+ }
+
+ function *m_fun;
+ location_t m_loc;
+};
+
+/* If FUN's name matches FUNCNAME, write the function and its start location
+ into *OUT_ENTRY. */
+
+static void
+check_for_named_function (function *fun, const char *funcname,
+ event_location_t *out_entry)
+{
+ gcc_assert (fun);
+ gcc_assert (funcname);
+
+ if (strcmp (IDENTIFIER_POINTER (DECL_NAME (fun->decl)), funcname))
+ return;
+
+ *out_entry = event_location_t (fun, fun->function_start_locus);
+}
+
+
+/* Example 2: an interprocedural path. */
+
+class test_diagnostic_path : public simple_diagnostic_path
+{
+ public:
+ test_diagnostic_path (pretty_printer *event_pp)
+ : simple_diagnostic_path (event_pp)
+ {
+ }
+ void add_entry (event_location_t evloc, int stack_depth,
+ const char *funcname)
+ {
+ gcc_assert (evloc.m_fun);
+ add_event (evloc.m_loc, evloc.m_fun->decl, stack_depth,
+ "entering %qs", funcname);
+ }
+
+ void add_call (event_location_t call_evloc, int caller_stack_depth,
+ event_location_t callee_entry_evloc, const char *callee)
+ {
+ gcc_assert (call_evloc.m_fun);
+ add_event (call_evloc.m_loc, call_evloc.m_fun->decl, caller_stack_depth,
+ "calling %qs", callee);
+ add_entry (callee_entry_evloc, caller_stack_depth + 1, callee);
+ }
+
+ void add_leaf_call (event_location_t call_evloc, int caller_stack_depth,
+ const char *callee)
+ {
+ gcc_assert (call_evloc.m_fun);
+ add_event (call_evloc.m_loc, call_evloc.m_fun->decl, caller_stack_depth,
+ "calling %qs", callee);
+ }
+};
+
+static void
+example_2 ()
+{
+ gimple_stmt_iterator gsi;
+ basic_block bb;
+
+ event_location_t entry_to_wrapped_malloc;
+ event_location_t call_to_malloc;
+
+ event_location_t entry_to_wrapped_free;
+ event_location_t call_to_free;
+
+ event_location_t entry_to_make_boxed_int;
+ event_location_t call_to_wrapped_malloc;
+
+ event_location_t entry_to_free_boxed_int;
+ event_location_t call_to_wrapped_free;
+
+ event_location_t entry_to_test;
+ event_location_t call_to_make_boxed_int;
+ event_location_t call_to_free_boxed_int;
+
+ event_location_t call_to_missing_location;
+
+ cgraph_node *node;
+ FOR_EACH_FUNCTION_WITH_GIMPLE_BODY (node)
+ {
+ function *fun = node->get_fun ();
+ FOR_EACH_BB_FN (bb, fun)
+ {
+ check_for_named_function (fun, "wrapped_malloc",
+ &entry_to_wrapped_malloc);
+ check_for_named_function (fun, "wrapped_free",
+ &entry_to_wrapped_free);
+ check_for_named_function (fun, "make_boxed_int",
+ &entry_to_make_boxed_int);
+ check_for_named_function (fun, "free_boxed_int",
+ &entry_to_free_boxed_int);
+ check_for_named_function (fun, "test",
+ &entry_to_test);
+
+ 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, "malloc", 1))
+ call_to_malloc.set (call, fun);
+ if (gcall *call = check_for_named_call (stmt, "free", 1))
+ call_to_free.set (call, fun);
+ if (gcall *call = check_for_named_call (stmt, "wrapped_malloc", 1))
+ call_to_wrapped_malloc.set (call, fun);
+ if (gcall *call = check_for_named_call (stmt, "wrapped_free", 1))
+ call_to_wrapped_free.set (call, fun);
+ if (gcall *call = check_for_named_call (stmt, "make_boxed_int", 1))
+ call_to_make_boxed_int.set (call, fun);
+ if (gcall *call = check_for_named_call (stmt, "free_boxed_int", 1))
+ call_to_free_boxed_int.set (call, fun);
+ if (gcall *call = check_for_named_call (stmt, "missing_location", 0))
+ {
+ call_to_missing_location.set (call, fun);
+ /* Simulate an event that's missing a useful location_t. */
+ call_to_missing_location.m_loc = UNKNOWN_LOCATION;
+ }
+ }
+ }
+ }
+
+ if (call_to_malloc.m_fun)
+ {
+ auto_diagnostic_group d;
+
+ gcc_rich_location richloc (call_to_free.m_loc);
+ test_diagnostic_path path (global_dc->printer);
+ path.add_entry (entry_to_test, 0, "test");
+ path.add_call (call_to_make_boxed_int, 0,
+ entry_to_make_boxed_int, "make_boxed_int");
+ path.add_call (call_to_wrapped_malloc, 1,
+ entry_to_wrapped_malloc, "wrapped_malloc");
+ path.add_leaf_call (call_to_malloc, 2, "malloc");
+
+ for (int i = 0; i < 2; i++)
+ {
+ path.add_call (call_to_free_boxed_int, 0,
+ entry_to_free_boxed_int, "free_boxed_int");
+ path.add_call (call_to_wrapped_free, 1,
+ entry_to_wrapped_free, "wrapped_free");
+ path.add_leaf_call (call_to_free, 2, "free");
+ if (i == 0 && call_to_missing_location.m_fun)
+ path.add_leaf_call (call_to_missing_location, 0, "missing_location");
+ }
+
+ richloc.set_path (&path);
+
+ diagnostic_metadata m;
+ m.add_cwe (415); /* CWE-415: Double Free. */
+
+ warning_at (&richloc, m, 0,
+ "double-free of %qs", "ptr");
+ }
+}
+
+/* Example 3: an interprocedural path with a callback. */
+
+static void
+example_3 ()
+{
+ gimple_stmt_iterator gsi;
+ basic_block bb;
+
+ event_location_t entry_to_custom_logger;
+ event_location_t call_to_fprintf;
+
+ event_location_t entry_to_int_handler;
+ event_location_t call_to_custom_logger;
+
+ event_location_t entry_to_register_handler;
+ event_location_t call_to_signal;
+
+ event_location_t entry_to_test;
+ event_location_t call_to_register_handler;
+
+ cgraph_node *node;
+ FOR_EACH_FUNCTION_WITH_GIMPLE_BODY (node)
+ {
+ function *fun = node->get_fun ();
+ FOR_EACH_BB_FN (bb, fun)
+ {
+ check_for_named_function (fun, "custom_logger",
+ &entry_to_custom_logger);
+ check_for_named_function (fun, "int_handler",
+ &entry_to_int_handler);
+ check_for_named_function (fun, "register_handler",
+ &entry_to_register_handler);
+ check_for_named_function (fun, "test",
+ &entry_to_test);
+ 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, "fprintf", 3))
+ call_to_fprintf.set (call, fun);
+ if (gcall *call = check_for_named_call (stmt, "custom_logger", 1))
+ call_to_custom_logger.set (call, fun);
+ if (gcall *call = check_for_named_call (stmt, "register_handler",
+ 0))
+ call_to_register_handler.set (call, fun);
+ if (gcall *call = check_for_named_call (stmt, "signal", 2))
+ call_to_signal.set (call, fun);
+ }
+ }
+ }
+
+ if (call_to_fprintf.m_fun)
+ {
+ auto_diagnostic_group d;
+
+ gcc_rich_location richloc (call_to_fprintf.m_loc);
+ test_diagnostic_path path (global_dc->printer);
+ path.add_entry (entry_to_test, 1, "test");
+ path.add_call (call_to_register_handler, 1,
+ entry_to_register_handler, "register_handler");
+ path.add_event (call_to_signal.m_loc, call_to_signal.m_fun->decl,
+ 2, "registering 'int_handler' as signal handler");
+ path.add_event (UNKNOWN_LOCATION, NULL_TREE, 0,
+ "later on, when the signal is delivered to the process");
+ path.add_entry (entry_to_int_handler, 1, "int_handler");
+ path.add_call (call_to_custom_logger, 1,
+ entry_to_custom_logger, "custom_logger");
+ path.add_leaf_call (call_to_fprintf, 2, "fprintf");
+
+ richloc.set_path (&path);
+
+ diagnostic_metadata m;
+ /* CWE-479: Signal Handler Use of a Non-reentrant Function. */
+ m.add_cwe (479);
+
+ warning_at (&richloc, m, 0,
+ "call to %qs from within signal handler",
+ "fprintf");
+ }
+}
+
+unsigned int
+pass_test_show_path::execute (function *)
+{
+ example_1 ();
+ example_2 ();
+ example_3 ();
+
+ return 0;
+}
+
+static opt_pass *
+make_pass_test_show_path (gcc::context *ctxt)
+{
+ return new pass_test_show_path (ctxt);
+}
+
+int
+plugin_init (struct plugin_name_args *plugin_info,
+ struct plugin_gcc_version *version)
+{
+ struct register_pass_info pass_info;
+ const char *plugin_name = plugin_info->base_name;
+ int argc = plugin_info->argc;
+ struct plugin_argument *argv = plugin_info->argv;
+
+ if (!plugin_default_version_check (version, &gcc_version))
+ return 1;
+
+ pass_info.pass = make_pass_test_show_path (g);
+ pass_info.reference_pass_name = "whole-program";
+ pass_info.ref_pass_instance_number = 1;
+ pass_info.pos_op = PASS_POS_INSERT_BEFORE;
+ register_callback (plugin_name, PLUGIN_PASS_MANAGER_SETUP, NULL,
+ &pass_info);
+
+ return 0;
+}
diff --git a/gcc/testsuite/gcc.dg/plugin/plugin.exp b/gcc/testsuite/gcc.dg/plugin/plugin.exp
index 7a8dbd2..c02b008 100644
--- a/gcc/testsuite/gcc.dg/plugin/plugin.exp
+++ b/gcc/testsuite/gcc.dg/plugin/plugin.exp
@@ -95,6 +95,17 @@ set plugin_test_list [list \
diagnostic-test-inlining-3.c \
diagnostic-test-inlining-4.c } \
{ diagnostic_plugin_test_metadata.c diagnostic-test-metadata.c } \
+ { diagnostic_plugin_test_paths.c \
+ diagnostic-test-paths-1.c \
+ diagnostic-test-paths-2.c \
+ diagnostic-test-paths-3.c \
+ diagnostic-test-paths-4.c \
+ diagnostic-path-format-default.c \
+ diagnostic-path-format-none.c \
+ diagnostic-path-format-separate-events.c \
+ diagnostic-path-format-inline-events-1.c \
+ diagnostic-path-format-inline-events-2.c \
+ diagnostic-path-format-inline-events-3.c } \
{ location_overflow_plugin.c \
location-overflow-test-1.c \
location-overflow-test-2.c \