]> git.ipfire.org Git - thirdparty/gcc.git/commit
sarif output: capture nesting of logical locations [PR116176]
authorDavid Malcolm <dmalcolm@redhat.com>
Tue, 6 May 2025 13:26:18 +0000 (09:26 -0400)
committerDavid Malcolm <dmalcolm@redhat.com>
Tue, 6 May 2025 13:26:18 +0000 (09:26 -0400)
commitf25e178b8c2cc868168b0a29ab03260fbefa4ff7
tree966ba7726640d33d80e97356327aca4bea4ca59d
parent8ab6899dce92e6ef8cc55096789fcda60d4129cb
sarif output: capture nesting of logical locations [PR116176]

Previously our SARIF output did not capture nesting of logical
locations: any time a result or event referred to a logical location it
would simply put a copy of the logical location into the pertinent
location object without a "parentIndex" property.

With this patch we instead populate such locations with minimal logical
locations with an "index" that refers to theRuns.logicalLocations,
populating theRuns.logicalLocations with the full logical locations,
including "parentIndex", recursively adding entries for the ancestor
locations as needed, so that the SARIF output captures the hierarchical
structure of the logical locations.

gcc/ChangeLog:
PR other/116176
* diagnostic-format-sarif.cc (class sarif_array_of_unique): New
template.
(class sarif_logical_location): Move here from
diagnostic-format-sarif.h.
(sarif_builder::m_cached_logical_locs): New.
(sarif_builder::sarif_builder): Initialize it.
(sarif_builder::set_any_logical_locs_arr): Call
make_minimal_sarif_logical_location rather than
make_sarif_logical_location_object.
(sarif_property_bag::set_logical_location): Likewise.
(make_sarif_logical_location_object): Replace with...
(sarif_builder::ensure_sarif_logical_location_for): ...this.
Capture "parentIndex" property.  Consolidate into
theRuns.logicalLocations.
(sarif_builder::make_minimal_sarif_logical_location): New.
(sarif_builder::make_run_object): Add "index" properties to
m_cached_logical_locs and move it to theRuns.logicalLocations.
(selftest::test_sarif_array_of_unique_1): New.
(selftest::test_sarif_array_of_unique_2): New.
(selftest::diagnostic_format_sarif_cc_tests): Call the new
selftests.
* diagnostic-format-sarif.h (class sarif_logical_location): Move
to diagnostic-format-sarif.cc.
(make_sarif_logical_location_object): Drop decl.
* json.cc (value::compare): New.
(object::compare): New.
(selftest::fail_comparison): New.
(selftest::assert_json_equal): New.
(ASSERT_JSON_EQ): New.
(selftest::assert_json_non_equal): New.
(ASSERT_JSON_NE): New.
(selftest::test_comparisons): New.
(selftest::json_cc_tests): Call the new selftest.
* json.h (json::value::dyn_cast_object): New vfunc.
(json::object::dyn_cast_object): New vfunc impl.
(json::object::compare): New decl.
* libgdiagnostics.cc
(impl_logical_location_manager::get_parent): New.
* logical-location.h (logical_location_manager::get_parent): New
vfunc impl.
* selftest-logical-location.h
(test_logical_location_manager::get_parent): New vfunc impl.
* tree-logical-location.cc (assert_valid_tree): New.
(tree_logical_location_manager::get_short_name): Support types as
well as decls.
(tree_logical_location_manager::get_name_with_scope): Gracefully
handle non-decl nodes.
(tree_logical_location_manager::get_internal_name): Likewise.
(tree_logical_location_manager::get_kind): Don't attempt to handle
null nodes.  Handle NAMESPACE_DECL and RECORD_TYPE.
(tree_logical_location_manager::get_name_for_path_output):
Gracefully handle non-decl nodes.
(tree_logical_location_manager::get_parent): New.
* tree-logical-location.h
(tree_logical_location_manager::get_parent): New vfunc impl.

gcc/testsuite/ChangeLog:
PR other/116176
* g++.dg/sarif-output/logical-locations-1.C: New test.
* g++.dg/sarif-output/logical-locations-1.py: New test script.
* g++.dg/sarif-output/logical-locations-2.C: New test.
* g++.dg/sarif-output/logical-locations-2.py: New test script.
* g++.dg/sarif-output/logical-locations-3.C: New test.
* g++.dg/sarif-output/logical-locations-3.py: New test script.
* g++.dg/sarif-output/sarif-output.exp: New script, adapted
from gcc.dg/sarif-output/sarif-output.exp.
* libgdiagnostics.dg/test-logical-location-c.py: Update for using
theRun.logicalLocations.
* libgdiagnostics.dg/test-warning-with-path-c.py: Likewise.

Signed-off-by: David Malcolm <dmalcolm@redhat.com>
18 files changed:
gcc/diagnostic-format-sarif.cc
gcc/diagnostic-format-sarif.h
gcc/json.cc
gcc/json.h
gcc/libgdiagnostics.cc
gcc/logical-location.h
gcc/selftest-logical-location.h
gcc/testsuite/g++.dg/sarif-output/logical-locations-1.C [new file with mode: 0644]
gcc/testsuite/g++.dg/sarif-output/logical-locations-1.py [new file with mode: 0644]
gcc/testsuite/g++.dg/sarif-output/logical-locations-2.C [new file with mode: 0644]
gcc/testsuite/g++.dg/sarif-output/logical-locations-2.py [new file with mode: 0644]
gcc/testsuite/g++.dg/sarif-output/logical-locations-3.C [new file with mode: 0644]
gcc/testsuite/g++.dg/sarif-output/logical-locations-3.py [new file with mode: 0644]
gcc/testsuite/g++.dg/sarif-output/sarif-output.exp [new file with mode: 0644]
gcc/testsuite/libgdiagnostics.dg/test-logical-location-c.py
gcc/testsuite/libgdiagnostics.dg/test-warning-with-path-c.py
gcc/tree-logical-location.cc
gcc/tree-logical-location.h