Skip to content
Snippets Groups Projects
Commit caef7002 authored by David Malcolm's avatar David Malcolm
Browse files

diagnostics: support SARIF 2.2 output, undocumented for now [PR116301]

GCC currently supports outputting SARIF v2.1.0

Version 2.2 of the SARIF spec is not yet official, but the draft has
already gained features we might might want to use.

This patch extends the SARIF output code to accept a enum sarif_version
parameter internally, representing 2.1.0 or a prerelease of 2.2

The patch updates the SARIF output selftests so that they are run for
all such versions.

I hope to expose this "properly" via the mechanism described
in comment #13 of PR116613.  In the meantime, the patch adds a new
  -fdiagnostics-format=sarif-file-2.2-prerelease
for use by the DejaGnu testsuite, deliberately left undocumented for
now.

The copy of the 2.2 draft schema in the testsuite was downloaded from
https://raw.githubusercontent.com/oasis-tcs/sarif-spec/refs/tags/2.2-prerelease-2024-08-08/sarif-2.2/schema/sarif-2-2.schema.json

The patch adds support for capturing related locations within an ICE
notification for SARIF 2.2 onwards, thus capturing "include chain"
information for SARIF-based reports of ICEs that occur within a
header; see https://github.com/oasis-tcs/sarif-spec/issues/540

The patch does *not* add support for the "scannedFile" role, leaving it
to followup work; see https://github.com/oasis-tcs/sarif-spec/issues/459



gcc/ChangeLog:
	PR other/116301
	* common.opt (sarif-file-2.2-prerelease): New value for
	-fdiagnostics-format=.
	* diagnostic-format-sarif.cc
	(sarif_location_manager::sarif_location_manager): Move
	initialization of m_related_locations_arr here from sarif_result's
	ctor.
	(sarif_location_manager::add_related_location): Implement for
	base class, taking sarif_result's implementation.  Add "builder"
	param.
	(sarif_location_manager::m_related_locations_arr): Move here from
	class sarif_result.
	(class sarif_result): Move m_related_locations_arr field and
	add_related_location vfunc to class sarif_location_manager.
	(sarif_builder::get_version): New accessor.
	(sarif_builder::m_version): New field.
	(sarif_invocation::add_notification_for_ice): Call
	process_worklist on the notification for SARIF 2.2 and later.
	(sarif_location_manager::process_worklist_item): Pass builder to
	calls to add_related_location.
	(sarif_result::on_nested_diagnostic): Likewise.
	(sarif_result::on_diagram): Likewise.
	(sarif_ice_notification::add_related_location): Add builder param.
	For SARIF 2.2 and later chain up to base class impl so that
	notifications get related locations.
	(sarif_builder::sarif_builder): Add "version" param.
	(SARIF_SCHEMA): Delete in favor of...
	(sarif_version_to_url): New function.
	(SARIF_VERSION): Delete in favor of...
	(sarif_version_to_property): New function.
	(make_top_level_object): Update to use m_version for "$schema" and
	"version".
	(sarif_output_format::sarif_output_format): Add "version" param.
	(sarif_stream_output_format::sarif_stream_output_format):
	Likewise.
	(sarif_file_output_format::sarif_file_output_format): Likewise.
	(diagnostic_output_format_init_sarif_stderr): Likewise.
	(diagnostic_output_format_init_sarif_file): Likewise.
	(diagnostic_output_format_init_sarif_stream): Likewise.
	(selftest::test_sarif_diagnostic_context): Likewise.
	(selftest::test_make_location_object): Likewise.
	(selftest::test_simple_log): Likewise.  Update schema and version
	tests accordingly.
	(selftest::test_simple_log_2): Add "version" param.
	(selftest::test_message_with_embedded_link): Likewise.
	(selftest::run_tests_per_version): New, based on the
	for_each_line_table_case calls in...
	(selftest::diagnostic_format_sarif_cc_tests): Add loop over sarif
	versions.  Replace for_each_line_table_case calls with one
	call to run_tests_per_version.
	* diagnostic-format-sarif.h: Include "diagnostic-format.h".
	(enum class sarif_version): New.
	(diagnostic_output_format_init_sarif_stderr): Move to here from
	diagnostic-format.h.  Add "version" param.
	(diagnostic_output_format_init_sarif_file): Likewise.
	(diagnostic_output_format_init_sarif_stream): Likewise.
	* diagnostic-format.h: Include "diagnostic.h".
	(diagnostic_output_format_init_sarif_stderr): Move from here to
	diagnostic-format-sarif.h.
	* diagnostic.cc: Define INCLUDE_MEMORY.
	Include "diagnostic-format-sarif.h".
	(diagnostic_output_format_init): Pass sarif_version::v2_1_0 to
	existing SARIF options.
	Add case DIAGNOSTICS_OUTPUT_FORMAT_SARIF_FILE_2_2_PRERELEASE.
	* diagnostic.h (enum diagnostics_output_format): Add
	DIAGNOSTICS_OUTPUT_FORMAT_SARIF_FILE_2_2_PRERELEASE.

gcc/testsuite/ChangeLog:
	PR other/116301
	* gcc.dg/plugin/crash-test-ice-in-header-sarif-2.1.c: New test.
	* gcc.dg/plugin/crash-test-ice-in-header-sarif-2.2.c: New test.
	* gcc.dg/plugin/crash-test-ice-in-header-sarif-2_1.py: Support
	script for new test.
	* gcc.dg/plugin/crash-test-ice-in-header-sarif-2_2.py: Likewise.
	* gcc.dg/plugin/crash-test-ice-in-header.h: New header.
	* gcc.dg/plugin/plugin.exp: Add the new tests.
	* lib/sarif-schema-2.2-prerelease-2024-08-08.json: New schema
	file.
	* lib/scansarif.exp (verify-sarif-file): Add optional argument for
	specifying which version of the schema to validate against,
	supporting "2.1" and "2.2", defaulting to the former.
	Update the test name to capture the version of the schema tested
	against.

Signed-off-by: default avatarDavid Malcolm <dmalcolm@redhat.com>
parent 1d091178
No related branches found
No related tags found
No related merge requests found
Showing
with 3459 additions and 92 deletions
Loading
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment