aboutsummaryrefslogtreecommitdiff
path: root/gcc/testsuite/gcc.dg/plugin/diagnostic_plugin_test_paths.c
diff options
context:
space:
mode:
authorDavid Malcolm <dmalcolm@redhat.com>2020-01-10 21:22:12 +0000
committerDavid Malcolm <dmalcolm@redhat.com>2020-01-10 21:22:12 +0000
commitc6f0af50e0ab292102a857598bd2d9736e7ff10c (patch)
treeaa499fa988bf6f1172f99666b74375a6647929d9 /gcc/testsuite/gcc.dg/plugin/diagnostic_plugin_test_paths.c
parent71558941711aeee73edc638120c80d8dfa031313 (diff)
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. git-svn-id: https://gcc.gnu.org/svn/gcc/trunk@280142 138bc75d-0d04-0410-961f-82ee72b054a4
Diffstat (limited to 'gcc/testsuite/gcc.dg/plugin/diagnostic_plugin_test_paths.c')
-rw-r--r--gcc/testsuite/gcc.dg/plugin/diagnostic_plugin_test_paths.c460
1 files changed, 460 insertions, 0 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
new file mode 100644
index 000000000000..cf05ca3a5d32
--- /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;
+}