diff options
author | David Malcolm <dmalcolm@redhat.com> | 2019-12-18 23:58:49 +0000 |
---|---|---|
committer | David Malcolm <dmalcolm@gcc.gnu.org> | 2019-12-18 23:58:49 +0000 |
commit | 6d4a35ca57b1af5a7a97faedb8a17c0a00890ce4 (patch) | |
tree | 16f8736949f2ebe5bba7113247da859b5ee624d2 /gcc/diagnostic.c | |
parent | a7a09efa24289b93d127ec41d5bec873d9fe000d (diff) | |
download | gcc-6d4a35ca57b1af5a7a97faedb8a17c0a00890ce4.zip gcc-6d4a35ca57b1af5a7a97faedb8a17c0a00890ce4.tar.gz gcc-6d4a35ca57b1af5a7a97faedb8a17c0a00890ce4.tar.bz2 |
Add diagnostic_metadata and CWE support
This patch adds support for associating a diagnostic message with an
optional diagnostic_metadata object, so that plugins can add extra data
to their diagnostics (e.g. mapping a diagnostic to a taxonomy or coding
standard such as from CERT or MISRA).
Currently this only supports associating a CWE identifier with a
diagnostic (which is what I'm using for the warnings in the analyzer
patch kit), but adding a diagnostic_metadata class allows for future
growth in this area without an explosion of further "warning_at"
overloads for all of the different kinds of custom data that a plugin
might want to add.
This version of the patch renames the overly-general
-fdiagnostics-show-metadata to -fdiagnostics-show-cwe and adds test
coverage for it via a plugin.
It also adds a note to the documentation that no GCC diagnostics
currently use this; it's a feature for plugins (and, at some point,
I hope, the analyzer).
gcc/ChangeLog:
* common.opt (fdiagnostics-show-cwe): Add.
* diagnostic-core.h (class diagnostic_metadata): New forward decl.
(warning_at): Add overload taking a const diagnostic_metadata &.
(emit_diagnostic_valist): Add overload taking a
const diagnostic_metadata *.
* diagnostic-format-json.cc: Include "diagnostic-metadata.h".
(json_from_metadata): New function.
(json_end_diagnostic): Call it to add "metadata" child for
diagnostics with metadata.
(diagnostic_output_format_init): Clear context->show_cwe.
* diagnostic-metadata.h: New file.
* diagnostic.c: Include "diagnostic-metadata.h".
(diagnostic_impl): Add const diagnostic_metadata * param.
(diagnostic_n_impl): Likewise.
(diagnostic_initialize): Initialize context->show_cwe.
(diagnostic_set_info_translated): Initialize diagnostic->metadata.
(get_cwe_url): New function.
(print_any_cwe): New function.
(diagnostic_report_diagnostic): Call print_any_cwe if the
diagnostic has non-NULL metadata.
(emit_diagnostic): Pass NULL as the metadata in the call to
diagnostic_impl.
(emit_diagnostic_valist): Likewise.
(emit_diagnostic_valist): New overload taking a
const diagnostic_metadata *.
(inform): Pass NULL as the metadata in the call to
diagnostic_impl.
(inform_n): Likewise for diagnostic_n_impl.
(warning): Likewise.
(warning_at): Likewise. Add overload that takes a
const diagnostic_metadata &.
(warning_n): Pass NULL as the metadata in the call to
diagnostic_n_impl.
(pedwarn): Likewise for diagnostic_impl.
(permerror): Likewise.
(error): Likewise.
(error_n): Likewise.
(error_at): Likewise.
(sorry): Likewise.
(sorry_at): Likewise.
(fatal_error): Likewise.
(internal_error): Likewise.
(internal_error_no_backtrace): Likewise.
* diagnostic.h (diagnostic_info::metadata): New field.
(diagnostic_context::show_cwe): New field.
* doc/invoke.texi (-fno-diagnostics-show-cwe): New option.
* opts.c (common_handle_option): Handle OPT_fdiagnostics_show_cwe.
* toplev.c (general_init): Initialize global_dc->show_cwe.
gcc/testsuite/ChangeLog:
* gcc.dg/plugin/diagnostic-test-metadata.c: New test.
* gcc.dg/plugin/diagnostic_plugin_test_metadata.c: New test plugin.
* gcc.dg/plugin/plugin.exp (plugin_test_list): Add them.
From-SVN: r279556
Diffstat (limited to 'gcc/diagnostic.c')
-rw-r--r-- | gcc/diagnostic.c | 142 |
1 files changed, 111 insertions, 31 deletions
diff --git a/gcc/diagnostic.c b/gcc/diagnostic.c index d6604e6..95cfb6e 100644 --- a/gcc/diagnostic.c +++ b/gcc/diagnostic.c @@ -32,6 +32,7 @@ along with GCC; see the file COPYING3. If not see #include "diagnostic.h" #include "diagnostic-color.h" #include "diagnostic-url.h" +#include "diagnostic-metadata.h" #include "edit-context.h" #include "selftest.h" #include "selftest-diagnostic.h" @@ -58,11 +59,13 @@ along with GCC; see the file COPYING3. If not see #define permissive_error_option(DC) ((DC)->opt_permissive) /* Prototypes. */ -static bool diagnostic_impl (rich_location *, int, const char *, - va_list *, diagnostic_t) ATTRIBUTE_GCC_DIAG(3,0); -static bool diagnostic_n_impl (rich_location *, int, unsigned HOST_WIDE_INT, +static bool diagnostic_impl (rich_location *, const diagnostic_metadata *, + int, const char *, + va_list *, diagnostic_t) ATTRIBUTE_GCC_DIAG(4,0); +static bool diagnostic_n_impl (rich_location *, const diagnostic_metadata *, + int, unsigned HOST_WIDE_INT, const char *, const char *, va_list *, - diagnostic_t) ATTRIBUTE_GCC_DIAG(5,0); + diagnostic_t) ATTRIBUTE_GCC_DIAG(6,0); static void error_recursion (diagnostic_context *) ATTRIBUTE_NORETURN; static void real_abort (void) ATTRIBUTE_NORETURN; @@ -183,6 +186,7 @@ diagnostic_initialize (diagnostic_context *context, int n_opts) diagnostic_set_caret_max_width (context, pp_line_cutoff (context->printer)); for (i = 0; i < rich_location::STATICALLY_ALLOCATED_RANGES; i++) context->caret_chars[i] = '^'; + context->show_cwe = false; context->show_option_requested = false; context->abort_on_error = false; context->show_column = false; @@ -299,6 +303,7 @@ diagnostic_set_info_translated (diagnostic_info *diagnostic, const char *msg, diagnostic->message.format_spec = msg; diagnostic->message.m_richloc = richloc; diagnostic->richloc = richloc; + diagnostic->metadata = NULL; diagnostic->kind = kind; diagnostic->option_index = 0; } @@ -898,6 +903,47 @@ update_effective_level_from_pragmas (diagnostic_context *context, return diag_class; } +/* Generate a URL string describing CWE. The caller is responsible for + freeing the string. */ + +static char * +get_cwe_url (int cwe) +{ + return xasprintf ("https://cwe.mitre.org/data/definitions/%i.html", cwe); +} + +/* If DIAGNOSTIC has a CWE identifier, print it. + + For example, if the diagnostic metadata associates it with CWE-119, + " [CWE-119]" will be printed, suitably colorized, and with a URL of a + description of the security issue. */ + +static void +print_any_cwe (diagnostic_context *context, + const diagnostic_info *diagnostic) +{ + if (diagnostic->metadata == NULL) + return; + + int cwe = diagnostic->metadata->get_cwe (); + if (cwe) + { + pretty_printer *pp = context->printer; + char *saved_prefix = pp_take_prefix (context->printer); + pp_string (pp, " ["); + pp_string (pp, colorize_start (pp_show_color (pp), + diagnostic_kind_color[diagnostic->kind])); + char *cwe_url = get_cwe_url (cwe); + pp_begin_url (pp, cwe_url); + free (cwe_url); + pp_printf (pp, "CWE-%i", cwe); + pp_set_prefix (context->printer, saved_prefix); + pp_end_url (pp); + pp_string (pp, colorize_stop (pp_show_color (pp))); + pp_character (pp, ']'); + } +} + /* Print any metadata about the option used to control DIAGNOSTIC to CONTEXT's printer, e.g. " [-Werror=uninitialized]". Subroutine of diagnostic_report_diagnostic. */ @@ -1058,6 +1104,8 @@ diagnostic_report_diagnostic (diagnostic_context *context, pp_format (context->printer, &diagnostic->message); (*diagnostic_starter (context)) (context, diagnostic); pp_output_formatted_text (context->printer); + if (context->show_cwe) + print_any_cwe (context, diagnostic); if (context->show_option_requested) print_option_information (context, diagnostic, orig_diag_kind); (*diagnostic_finalizer (context)) (context, diagnostic, orig_diag_kind); @@ -1183,8 +1231,8 @@ diagnostic_append_note (diagnostic_context *context, permerror, error, error_at, error_at, sorry, fatal_error, internal_error, and internal_error_no_backtrace, as documented and defined below. */ static bool -diagnostic_impl (rich_location *richloc, int opt, - const char *gmsgid, +diagnostic_impl (rich_location *richloc, const diagnostic_metadata *metadata, + int opt, const char *gmsgid, va_list *ap, diagnostic_t kind) { diagnostic_info diagnostic; @@ -1200,13 +1248,15 @@ diagnostic_impl (rich_location *richloc, int opt, if (kind == DK_WARNING || kind == DK_PEDWARN) diagnostic.option_index = opt; } + diagnostic.metadata = metadata; return diagnostic_report_diagnostic (global_dc, &diagnostic); } /* Implement inform_n, warning_n, and error_n, as documented and defined below. */ static bool -diagnostic_n_impl (rich_location *richloc, int opt, unsigned HOST_WIDE_INT n, +diagnostic_n_impl (rich_location *richloc, const diagnostic_metadata *metadata, + int opt, unsigned HOST_WIDE_INT n, const char *singular_gmsgid, const char *plural_gmsgid, va_list *ap, diagnostic_t kind) @@ -1226,6 +1276,7 @@ diagnostic_n_impl (rich_location *richloc, int opt, unsigned HOST_WIDE_INT n, diagnostic_set_info_translated (&diagnostic, text, ap, richloc, kind); if (kind == DK_WARNING) diagnostic.option_index = opt; + diagnostic.metadata = metadata; return diagnostic_report_diagnostic (global_dc, &diagnostic); } @@ -1239,7 +1290,7 @@ emit_diagnostic (diagnostic_t kind, location_t location, int opt, va_list ap; va_start (ap, gmsgid); rich_location richloc (line_table, location); - bool ret = diagnostic_impl (&richloc, opt, gmsgid, &ap, kind); + bool ret = diagnostic_impl (&richloc, NULL, opt, gmsgid, &ap, kind); va_end (ap); return ret; } @@ -1253,7 +1304,7 @@ emit_diagnostic (diagnostic_t kind, rich_location *richloc, int opt, auto_diagnostic_group d; va_list ap; va_start (ap, gmsgid); - bool ret = diagnostic_impl (richloc, opt, gmsgid, &ap, kind); + bool ret = diagnostic_impl (richloc, NULL, opt, gmsgid, &ap, kind); va_end (ap); return ret; } @@ -1265,7 +1316,18 @@ emit_diagnostic_valist (diagnostic_t kind, location_t location, int opt, const char *gmsgid, va_list *ap) { rich_location richloc (line_table, location); - return diagnostic_impl (&richloc, opt, gmsgid, ap, kind); + return diagnostic_impl (&richloc, NULL, opt, gmsgid, ap, kind); +} + +/* Wrapper around diagnostic_impl taking a va_list parameter. */ + +bool +emit_diagnostic_valist (diagnostic_t kind, rich_location *richloc, + const diagnostic_metadata *metadata, + int opt, + const char *gmsgid, va_list *ap) +{ + return diagnostic_impl (richloc, metadata, opt, gmsgid, ap, kind); } /* An informative note at LOCATION. Use this for additional details on an error @@ -1277,7 +1339,7 @@ inform (location_t location, const char *gmsgid, ...) va_list ap; va_start (ap, gmsgid); rich_location richloc (line_table, location); - diagnostic_impl (&richloc, -1, gmsgid, &ap, DK_NOTE); + diagnostic_impl (&richloc, NULL, -1, gmsgid, &ap, DK_NOTE); va_end (ap); } @@ -1290,7 +1352,7 @@ inform (rich_location *richloc, const char *gmsgid, ...) auto_diagnostic_group d; va_list ap; va_start (ap, gmsgid); - diagnostic_impl (richloc, -1, gmsgid, &ap, DK_NOTE); + diagnostic_impl (richloc, NULL, -1, gmsgid, &ap, DK_NOTE); va_end (ap); } @@ -1304,7 +1366,7 @@ inform_n (location_t location, unsigned HOST_WIDE_INT n, va_start (ap, plural_gmsgid); auto_diagnostic_group d; rich_location richloc (line_table, location); - diagnostic_n_impl (&richloc, -1, n, singular_gmsgid, plural_gmsgid, + diagnostic_n_impl (&richloc, NULL, -1, n, singular_gmsgid, plural_gmsgid, &ap, DK_NOTE); va_end (ap); } @@ -1319,7 +1381,7 @@ warning (int opt, const char *gmsgid, ...) va_list ap; va_start (ap, gmsgid); rich_location richloc (line_table, input_location); - bool ret = diagnostic_impl (&richloc, opt, gmsgid, &ap, DK_WARNING); + bool ret = diagnostic_impl (&richloc, NULL, opt, gmsgid, &ap, DK_WARNING); va_end (ap); return ret; } @@ -1335,7 +1397,7 @@ warning_at (location_t location, int opt, const char *gmsgid, ...) va_list ap; va_start (ap, gmsgid); rich_location richloc (line_table, location); - bool ret = diagnostic_impl (&richloc, opt, gmsgid, &ap, DK_WARNING); + bool ret = diagnostic_impl (&richloc, NULL, opt, gmsgid, &ap, DK_WARNING); va_end (ap); return ret; } @@ -1350,7 +1412,25 @@ warning_at (rich_location *richloc, int opt, const char *gmsgid, ...) auto_diagnostic_group d; va_list ap; va_start (ap, gmsgid); - bool ret = diagnostic_impl (richloc, opt, gmsgid, &ap, DK_WARNING); + bool ret = diagnostic_impl (richloc, NULL, opt, gmsgid, &ap, DK_WARNING); + va_end (ap); + return ret; +} + +/* Same as "warning at" above, but using METADATA. */ + +bool +warning_at (rich_location *richloc, const diagnostic_metadata &metadata, + int opt, const char *gmsgid, ...) +{ + gcc_assert (richloc); + + auto_diagnostic_group d; + va_list ap; + va_start (ap, gmsgid); + bool ret + = diagnostic_impl (richloc, &metadata, opt, gmsgid, &ap, + DK_WARNING); va_end (ap); return ret; } @@ -1366,7 +1446,7 @@ warning_n (rich_location *richloc, int opt, unsigned HOST_WIDE_INT n, auto_diagnostic_group d; va_list ap; va_start (ap, plural_gmsgid); - bool ret = diagnostic_n_impl (richloc, opt, n, + bool ret = diagnostic_n_impl (richloc, NULL, opt, n, singular_gmsgid, plural_gmsgid, &ap, DK_WARNING); va_end (ap); @@ -1385,7 +1465,7 @@ warning_n (location_t location, int opt, unsigned HOST_WIDE_INT n, va_list ap; va_start (ap, plural_gmsgid); rich_location richloc (line_table, location); - bool ret = diagnostic_n_impl (&richloc, opt, n, + bool ret = diagnostic_n_impl (&richloc, NULL, opt, n, singular_gmsgid, plural_gmsgid, &ap, DK_WARNING); va_end (ap); @@ -1412,7 +1492,7 @@ pedwarn (location_t location, int opt, const char *gmsgid, ...) va_list ap; va_start (ap, gmsgid); rich_location richloc (line_table, location); - bool ret = diagnostic_impl (&richloc, opt, gmsgid, &ap, DK_PEDWARN); + bool ret = diagnostic_impl (&richloc, NULL, opt, gmsgid, &ap, DK_PEDWARN); va_end (ap); return ret; } @@ -1427,7 +1507,7 @@ pedwarn (rich_location *richloc, int opt, const char *gmsgid, ...) auto_diagnostic_group d; va_list ap; va_start (ap, gmsgid); - bool ret = diagnostic_impl (richloc, opt, gmsgid, &ap, DK_PEDWARN); + bool ret = diagnostic_impl (richloc, NULL, opt, gmsgid, &ap, DK_PEDWARN); va_end (ap); return ret; } @@ -1446,7 +1526,7 @@ permerror (location_t location, const char *gmsgid, ...) va_list ap; va_start (ap, gmsgid); rich_location richloc (line_table, location); - bool ret = diagnostic_impl (&richloc, -1, gmsgid, &ap, DK_PERMERROR); + bool ret = diagnostic_impl (&richloc, NULL, -1, gmsgid, &ap, DK_PERMERROR); va_end (ap); return ret; } @@ -1461,7 +1541,7 @@ permerror (rich_location *richloc, const char *gmsgid, ...) auto_diagnostic_group d; va_list ap; va_start (ap, gmsgid); - bool ret = diagnostic_impl (richloc, -1, gmsgid, &ap, DK_PERMERROR); + bool ret = diagnostic_impl (richloc, NULL, -1, gmsgid, &ap, DK_PERMERROR); va_end (ap); return ret; } @@ -1475,7 +1555,7 @@ error (const char *gmsgid, ...) va_list ap; va_start (ap, gmsgid); rich_location richloc (line_table, input_location); - diagnostic_impl (&richloc, -1, gmsgid, &ap, DK_ERROR); + diagnostic_impl (&richloc, NULL, -1, gmsgid, &ap, DK_ERROR); va_end (ap); } @@ -1489,7 +1569,7 @@ error_n (location_t location, unsigned HOST_WIDE_INT n, va_list ap; va_start (ap, plural_gmsgid); rich_location richloc (line_table, location); - diagnostic_n_impl (&richloc, -1, n, singular_gmsgid, plural_gmsgid, + diagnostic_n_impl (&richloc, NULL, -1, n, singular_gmsgid, plural_gmsgid, &ap, DK_ERROR); va_end (ap); } @@ -1502,7 +1582,7 @@ error_at (location_t loc, const char *gmsgid, ...) va_list ap; va_start (ap, gmsgid); rich_location richloc (line_table, loc); - diagnostic_impl (&richloc, -1, gmsgid, &ap, DK_ERROR); + diagnostic_impl (&richloc, NULL, -1, gmsgid, &ap, DK_ERROR); va_end (ap); } @@ -1516,7 +1596,7 @@ error_at (rich_location *richloc, const char *gmsgid, ...) auto_diagnostic_group d; va_list ap; va_start (ap, gmsgid); - diagnostic_impl (richloc, -1, gmsgid, &ap, DK_ERROR); + diagnostic_impl (richloc, NULL, -1, gmsgid, &ap, DK_ERROR); va_end (ap); } @@ -1530,7 +1610,7 @@ sorry (const char *gmsgid, ...) va_list ap; va_start (ap, gmsgid); rich_location richloc (line_table, input_location); - diagnostic_impl (&richloc, -1, gmsgid, &ap, DK_SORRY); + diagnostic_impl (&richloc, NULL, -1, gmsgid, &ap, DK_SORRY); va_end (ap); } @@ -1542,7 +1622,7 @@ sorry_at (location_t loc, const char *gmsgid, ...) va_list ap; va_start (ap, gmsgid); rich_location richloc (line_table, loc); - diagnostic_impl (&richloc, -1, gmsgid, &ap, DK_SORRY); + diagnostic_impl (&richloc, NULL, -1, gmsgid, &ap, DK_SORRY); va_end (ap); } @@ -1564,7 +1644,7 @@ fatal_error (location_t loc, const char *gmsgid, ...) va_list ap; va_start (ap, gmsgid); rich_location richloc (line_table, loc); - diagnostic_impl (&richloc, -1, gmsgid, &ap, DK_FATAL); + diagnostic_impl (&richloc, NULL, -1, gmsgid, &ap, DK_FATAL); va_end (ap); gcc_unreachable (); @@ -1581,7 +1661,7 @@ internal_error (const char *gmsgid, ...) va_list ap; va_start (ap, gmsgid); rich_location richloc (line_table, input_location); - diagnostic_impl (&richloc, -1, gmsgid, &ap, DK_ICE); + diagnostic_impl (&richloc, NULL, -1, gmsgid, &ap, DK_ICE); va_end (ap); gcc_unreachable (); @@ -1597,7 +1677,7 @@ internal_error_no_backtrace (const char *gmsgid, ...) va_list ap; va_start (ap, gmsgid); rich_location richloc (line_table, input_location); - diagnostic_impl (&richloc, -1, gmsgid, &ap, DK_ICE_NOBT); + diagnostic_impl (&richloc, NULL, -1, gmsgid, &ap, DK_ICE_NOBT); va_end (ap); gcc_unreachable (); |