Skip to content
Snippets Groups Projects
  • David Malcolm's avatar
    6d4a35ca
    Add diagnostic_metadata and CWE support · 6d4a35ca
    David Malcolm authored
    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
    6d4a35ca
    History
    Add diagnostic_metadata and CWE support
    David Malcolm authored
    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
diagnostic-core.h 5.26 KiB