From 4bc1899b2e883f926dbda02f5b9a2c06ea30090d Mon Sep 17 00:00:00 2001 From: David Malcolm Date: Fri, 10 Jan 2020 21:22:12 +0000 Subject: 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 --- gcc/pretty-print.c | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) (limited to 'gcc/pretty-print.c') diff --git a/gcc/pretty-print.c b/gcc/pretty-print.c index ad8f3ef..817c105 100644 --- a/gcc/pretty-print.c +++ b/gcc/pretty-print.c @@ -24,6 +24,7 @@ along with GCC; see the file COPYING3. If not see #include "intl.h" #include "pretty-print.h" #include "diagnostic-color.h" +#include "diagnostic-event-id.h" #include "selftest.h" #if HAVE_ICONV @@ -1039,6 +1040,7 @@ pp_indent (pretty_printer *pp) %>: closing quote. %': apostrophe (should only be used in untranslated messages; translations should use appropriate punctuation directly). + %@: diagnostic_event_id_ptr, for which event_id->known_p () must be true. %.*s: a substring the length of which is specified by an argument integer. %Ns: likewise, but length specified as constant in the format string. @@ -1428,6 +1430,21 @@ pp_format (pretty_printer *pp, text_info *text) } break; + case '@': + { + /* diagnostic_event_id_t *. */ + diagnostic_event_id_ptr event_id + = va_arg (*text->args_ptr, diagnostic_event_id_ptr); + gcc_assert (event_id->known_p ()); + + pp_string (pp, colorize_start (pp_show_color (pp), "path")); + pp_character (pp, '('); + pp_decimal_int (pp, event_id->one_based ()); + pp_character (pp, ')'); + pp_string (pp, colorize_stop (pp_show_color (pp))); + } + break; + default: { bool ok; @@ -2338,6 +2355,21 @@ test_pp_format () assert_pp_format_colored (SELFTEST_LOCATION, "`\33[01m\33[Kfoo\33[m\33[K' 12345678", "%qs %x", "foo", 0x12345678); + /* Verify "%@". */ + { + diagnostic_event_id_t first (2); + diagnostic_event_id_t second (7); + + ASSERT_PP_FORMAT_2 ("first `free' at (3); second `free' at (8)", + "first % at %@; second % at %@", + &first, &second); + assert_pp_format_colored + (SELFTEST_LOCATION, + "first `free' at (3);" + " second `free' at (8)", + "first % at %@; second % at %@", + &first, &second); + } /* Verify %Z. */ int v[] = { 1, 2, 3 }; -- cgit v1.1 From 458c8d6459c4005fc9886b6e25d168a6535ac415 Mon Sep 17 00:00:00 2001 From: Bernd Edlinger Date: Wed, 29 Jan 2020 15:31:10 +0100 Subject: PR 87488: Add --with-diagnostics-urls configuration option 2020-02-15 David Malcolm Bernd Edlinger PR 87488 PR other/93168 * config.in (DIAGNOSTICS_URLS_DEFAULT): New define. * configure.ac (--with-diagnostics-urls): New configuration option, based on --with-diagnostics-color. (DIAGNOSTICS_URLS_DEFAULT): New define. * config.h: Regenerate. * configure: Regenerate. * diagnostic.c (diagnostic_urls_init): Handle -1 for DIAGNOSTICS_URLS_DEFAULT from configure-time --with-diagnostics-urls=auto-if-env by querying for a GCC_URLS and TERM_URLS environment variable. * diagnostic-url.h (diagnostic_url_format): New enum type. (diagnostic_urls_enabled_p): rename to... (determine_url_format): ... this, and change return type. * diagnostic-color.c (parse_env_vars_for_urls): New helper function. (auto_enable_urls): Disable URLs on xfce4-terminal, gnome-terminal, the linux console, and mingw. (diagnostic_urls_enabled_p): rename to... (determine_url_format): ... this, and adjust. * pretty-print.h (pretty_printer::show_urls): rename to... (pretty_printer::url_format): ... this, and change to enum. * pretty-print.c (pretty_printer::pretty_printer, pp_begin_url, pp_end_url, test_urls): Adjust. * doc/install.texi (--with-diagnostics-urls): Document the new configuration option. (--with-diagnostics-color): Document the existing interaction with GCC_COLORS better. * doc/invoke.texi (-fdiagnostics-urls): Add GCC_URLS and TERM_URLS vindex reference. Update description of defaults based on the above. (-fdiagnostics-color): Update description of how -fdiagnostics-color interacts with GCC_COLORS. --- gcc/pretty-print.c | 44 ++++++++++++++++++++++++++++++++++++++------ 1 file changed, 38 insertions(+), 6 deletions(-) (limited to 'gcc/pretty-print.c') diff --git a/gcc/pretty-print.c b/gcc/pretty-print.c index 817c105..dde138b 100644 --- a/gcc/pretty-print.c +++ b/gcc/pretty-print.c @@ -1647,7 +1647,7 @@ pretty_printer::pretty_printer (int maximum_length) need_newline (), translate_identifiers (true), show_color (), - show_urls (false) + url_format (URL_FORMAT_NONE) { pp_line_cutoff (this) = maximum_length; /* By default, we emit prefixes once per message. */ @@ -1670,7 +1670,7 @@ pretty_printer::pretty_printer (const pretty_printer &other) need_newline (other.need_newline), translate_identifiers (other.translate_identifiers), show_color (other.show_color), - show_urls (other.show_urls) + url_format (other.url_format) { pp_line_cutoff (this) = maximum_length; /* By default, we emit prefixes once per message. */ @@ -2171,8 +2171,19 @@ identifier_to_locale (const char *ident) void pp_begin_url (pretty_printer *pp, const char *url) { - if (pp->show_urls) + switch (pp->url_format) + { + case URL_FORMAT_NONE: + break; + case URL_FORMAT_ST: + pp_printf (pp, "\33]8;;%s\33\\", url); + break; + case URL_FORMAT_BEL: pp_printf (pp, "\33]8;;%s\a", url); + break; + default: + gcc_unreachable (); + } } /* If URL-printing is enabled, write a "close URL" escape sequence to PP. */ @@ -2180,8 +2191,19 @@ pp_begin_url (pretty_printer *pp, const char *url) void pp_end_url (pretty_printer *pp) { - if (pp->show_urls) + switch (pp->url_format) + { + case URL_FORMAT_NONE: + break; + case URL_FORMAT_ST: + pp_string (pp, "\33]8;;\33\\"); + break; + case URL_FORMAT_BEL: pp_string (pp, "\33]8;;\a"); + break; + default: + gcc_unreachable (); + } } #if CHECKING_P @@ -2490,7 +2512,7 @@ test_urls () { { pretty_printer pp; - pp.show_urls = false; + pp.url_format = URL_FORMAT_NONE; pp_begin_url (&pp, "http://example.com"); pp_string (&pp, "This is a link"); pp_end_url (&pp); @@ -2500,7 +2522,17 @@ test_urls () { pretty_printer pp; - pp.show_urls = true; + pp.url_format = URL_FORMAT_ST; + pp_begin_url (&pp, "http://example.com"); + pp_string (&pp, "This is a link"); + pp_end_url (&pp); + ASSERT_STREQ ("\33]8;;http://example.com\33\\This is a link\33]8;;\33\\", + pp_formatted_text (&pp)); + } + + { + pretty_printer pp; + pp.url_format = URL_FORMAT_BEL; pp_begin_url (&pp, "http://example.com"); pp_string (&pp, "This is a link"); pp_end_url (&pp); -- cgit v1.1 From 691eeb65a01dab3084b6ce381737adf097bd2e65 Mon Sep 17 00:00:00 2001 From: Jakub Jelinek Date: Wed, 29 Apr 2020 22:41:47 +0200 Subject: diagnostics: Add %{...%} pretty-format support for URLs and use it in -Wpsabi diagnostics The following patch attempts to use the diagnostics URL support if available to provide more information about the C++17 empty base and C++20 [[no_unique_address]] empty class ABI changes in -Wpsabi diagnostics. in GCC 10.1 at the end of the diagnostics is then in some terminals underlined with a dotted line and points to a (to be written) anchor in gcc-10/changes.html which we need to write anyway. 2020-04-29 Jakub Jelinek * configure.ac (-with-changes-root-url): New configure option, defaulting to https://gcc.gnu.org/. * Makefile.in (CFLAGS-opts.o): Define CHANGES_ROOT_URL for opts.c. * pretty-print.c (get_end_url_string): New function. (pp_format): Handle %{ and %} for URLs. (pp_begin_url): Use pp_string instead of pp_printf. (pp_end_url): Use get_end_url_string. * opts.h (get_changes_url): Declare. * opts.c (get_changes_url): New function. * config/rs6000/rs6000-call.c: Include opts.h. (rs6000_discover_homogeneous_aggregate): Use %{in GCC 10.1%} instead of just in GCC 10.1 in diagnostics and add URL. * config/arm/arm.c (aapcs_vfp_is_call_or_return_candidate): Likewise. * config/aarch64/aarch64.c (aarch64_vfp_is_call_or_return_candidate): Likewise. * config/s390/s390.c (s390_function_arg_vector, s390_function_arg_float): Likewise. * configure: Regenerated. * c-format.c (PP_FORMAT_CHAR_TABLE): Add %{ and %}. --- gcc/pretty-print.c | 83 ++++++++++++++++++++++++++++++++++++------------------ 1 file changed, 56 insertions(+), 27 deletions(-) (limited to 'gcc/pretty-print.c') diff --git a/gcc/pretty-print.c b/gcc/pretty-print.c index dde138b..d0dd9cb 100644 --- a/gcc/pretty-print.c +++ b/gcc/pretty-print.c @@ -1020,6 +1020,8 @@ pp_indent (pretty_printer *pp) pp_space (pp); } +static const char *get_end_url_string (pretty_printer *); + /* The following format specifiers are recognized as being client independent: %d, %i: (signed) integer in base ten. %u: unsigned integer in base ten. @@ -1038,6 +1040,8 @@ pp_indent (pretty_printer *pp) %%: '%'. %<: opening quote. %>: closing quote. + %{: URL start. Consumes a const char * argument for the URL. + %}: URL end. Does not consume any arguments. %': apostrophe (should only be used in untranslated messages; translations should use appropriate punctuation directly). %@: diagnostic_event_id_ptr, for which event_id->known_p () must be true. @@ -1051,7 +1055,7 @@ pp_indent (pretty_printer *pp) Arguments can be used sequentially, or through %N$ resp. *N$ notation Nth argument after the format string. If %N$ / *N$ notation is used, it must be used for all arguments, except %m, %%, - %<, %> and %', which may not have a number, as they do not consume + %<, %>, %} and %', which may not have a number, as they do not consume an argument. When %M$.*N$s is used, M must be N + 1. (This may also be written %M$.*s, provided N is not otherwise used.) The format string must have conversion specifiers with argument numbers @@ -1084,7 +1088,7 @@ pp_format (pretty_printer *pp, text_info *text) /* Formatting phase 1: split up TEXT->format_spec into chunks in pp_buffer (PP)->args[]. Even-numbered chunks are to be output verbatim, odd-numbered chunks are format specifiers. - %m, %%, %<, %>, and %' are replaced with the appropriate text at + %m, %%, %<, %>, %} and %' are replaced with the appropriate text at this point. */ memset (formatters, 0, sizeof formatters); @@ -1133,6 +1137,15 @@ pp_format (pretty_printer *pp, text_info *text) p++; continue; + case '}': + { + const char *endurlstr = get_end_url_string (pp); + obstack_grow (&buffer->chunk_obstack, endurlstr, + strlen (endurlstr)); + } + p++; + continue; + case 'R': { const char *colorstr = colorize_stop (pp_show_color (pp)); @@ -1445,6 +1458,10 @@ pp_format (pretty_printer *pp, text_info *text) } break; + case '{': + pp_begin_url (pp, va_arg (*text->args_ptr, const char *)); + break; + default: { bool ok; @@ -2172,18 +2189,41 @@ void pp_begin_url (pretty_printer *pp, const char *url) { switch (pp->url_format) - { - case URL_FORMAT_NONE: - break; - case URL_FORMAT_ST: - pp_printf (pp, "\33]8;;%s\33\\", url); - break; - case URL_FORMAT_BEL: - pp_printf (pp, "\33]8;;%s\a", url); - break; - default: - gcc_unreachable (); - } + { + case URL_FORMAT_NONE: + break; + case URL_FORMAT_ST: + pp_string (pp, "\33]8;;"); + pp_string (pp, url); + pp_string (pp, "\33\\"); + break; + case URL_FORMAT_BEL: + pp_string (pp, "\33]8;;"); + pp_string (pp, url); + pp_string (pp, "\a"); + break; + default: + gcc_unreachable (); + } +} + +/* Helper function for pp_end_url and pp_format, return the "close URL" escape + sequence string. */ + +static const char * +get_end_url_string (pretty_printer *pp) +{ + switch (pp->url_format) + { + case URL_FORMAT_NONE: + return ""; + case URL_FORMAT_ST: + return "\33]8;;\33\\"; + case URL_FORMAT_BEL: + return "\33]8;;\a"; + default: + gcc_unreachable (); + } } /* If URL-printing is enabled, write a "close URL" escape sequence to PP. */ @@ -2191,19 +2231,8 @@ pp_begin_url (pretty_printer *pp, const char *url) void pp_end_url (pretty_printer *pp) { - switch (pp->url_format) - { - case URL_FORMAT_NONE: - break; - case URL_FORMAT_ST: - pp_string (pp, "\33]8;;\33\\"); - break; - case URL_FORMAT_BEL: - pp_string (pp, "\33]8;;\a"); - break; - default: - gcc_unreachable (); - } + if (pp->url_format != URL_FORMAT_NONE) + pp_string (pp, get_end_url_string (pp)); } #if CHECKING_P -- cgit v1.1 From 04e88369a7d95492efccf8f527d27cca74664ea7 Mon Sep 17 00:00:00 2001 From: Jonathan Wakely Date: Thu, 30 Apr 2020 14:42:24 +0100 Subject: diagnostics: Fix spelling in comment gcc/ChangeLog: * pretty-print.c (pp_take_prefix): Fix spelling in comment. --- gcc/pretty-print.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'gcc/pretty-print.c') diff --git a/gcc/pretty-print.c b/gcc/pretty-print.c index d0dd9cb..407f730 100644 --- a/gcc/pretty-print.c +++ b/gcc/pretty-print.c @@ -1595,7 +1595,7 @@ pp_set_prefix (pretty_printer *pp, char *prefix) } /* Take ownership of PP's prefix, setting it to NULL. - This allows clients to save, overide, and then restore an existing + This allows clients to save, override, and then restore an existing prefix, without it being free-ed. */ char * -- cgit v1.1