From 05c99b1c7965f46f0ff17d5e8f4020a62c643ae5 Mon Sep 17 00:00:00 2001 From: David Malcolm Date: Thu, 4 Jan 2024 09:19:06 -0500 Subject: analyzer: add sarif properties for checker events As another followup to r14-6057-g12b67d1e13b3cf, optionally add SARIF property bags to threadFlowLocation objects when writing out diagnostic paths, and add analyzer-specific properties to them. This was useful for debugging PR analyzer/112790. gcc/analyzer/ChangeLog: * checker-event.cc: Include "diagnostic-format-sarif.h" and "tree-logical-location.h". (checker_event::maybe_add_sarif_properties): New. (superedge_event::maybe_add_sarif_properties): New. (superedge_event::superedge_event): Add comment. * checker-event.h (checker_event::maybe_add_sarif_properties): New decl. (superedge_event::maybe_add_sarif_properties): New decl. gcc/ChangeLog: * diagnostic-format-sarif.cc (sarif_builder::make_logical_location_object): Convert to... (make_sarif_logical_location_object): ...this. (sarif_builder::set_any_logical_locs_arr): Update for above change. (sarif_builder::make_thread_flow_location_object): Call maybe_add_sarif_properties on each diagnostic_event. * diagnostic-format-sarif.h (class logical_location): New forward decl. (make_sarif_logical_location_object): New decl. * diagnostic-path.h (class sarif_object): New forward decl. (diagnostic_event::maybe_add_sarif_properties): New vfunc. Signed-off-by: David Malcolm --- gcc/analyzer/checker-event.cc | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) (limited to 'gcc/analyzer/checker-event.cc') diff --git a/gcc/analyzer/checker-event.cc b/gcc/analyzer/checker-event.cc index 2000db9..3ff3aea 100644 --- a/gcc/analyzer/checker-event.cc +++ b/gcc/analyzer/checker-event.cc @@ -55,6 +55,8 @@ along with GCC; see the file COPYING3. If not see #include "analyzer/constraint-manager.h" #include "analyzer/checker-event.h" #include "analyzer/exploded-graph.h" +#include "diagnostic-format-sarif.h" +#include "tree-logical-location.h" #if ENABLE_ANALYZER @@ -142,6 +144,30 @@ checker_event::get_meaning () const return meaning (); } +/* Implementation of diagnostic_event::maybe_add_sarif_properties + for checker_event. */ + +void +checker_event:: +maybe_add_sarif_properties (sarif_object &thread_flow_loc_obj) const +{ + sarif_property_bag &props = thread_flow_loc_obj.get_or_create_properties (); +#define PROPERTY_PREFIX "gcc/analyzer/checker_event/" + props.set (PROPERTY_PREFIX "emission_id", + diagnostic_event_id_to_json (m_emission_id)); + props.set_string (PROPERTY_PREFIX "kind", event_kind_to_string (m_kind)); + + if (m_original_fndecl != m_effective_fndecl) + { + tree_logical_location logical_loc (m_original_fndecl); + props.set (PROPERTY_PREFIX "original_fndecl", + make_sarif_logical_location_object (logical_loc)); + } + if (m_original_depth != m_effective_depth) + props.set_integer (PROPERTY_PREFIX "original_depth", m_original_depth); +#undef PROPERTY_PREFIX +} + /* Dump this event to PP (for debugging/logging purposes). */ void @@ -498,6 +524,21 @@ state_change_event::get_meaning () const /* class superedge_event : public checker_event. */ +/* Implementation of diagnostic_event::maybe_add_sarif_properties + for superedge_event. */ + +void +superedge_event::maybe_add_sarif_properties (sarif_object &thread_flow_loc_obj) + const +{ + checker_event::maybe_add_sarif_properties (thread_flow_loc_obj); + sarif_property_bag &props = thread_flow_loc_obj.get_or_create_properties (); +#define PROPERTY_PREFIX "gcc/analyzer/superedge_event/" + if (m_sedge) + props.set (PROPERTY_PREFIX "superedge", m_sedge->to_json ()); +#undef PROPERTY_PREFIX +} + /* Get the callgraph_superedge for this superedge_event, which must be for an interprocedural edge, rather than a CFG edge. */ @@ -548,6 +589,8 @@ superedge_event::superedge_event (enum event_kind kind, m_eedge (eedge), m_sedge (eedge.m_sedge), m_var (NULL_TREE), m_critical_state (0) { + /* Note that m_sedge can be nullptr for e.g. jumps through + function pointers. */ } /* class cfg_edge_event : public superedge_event. */ -- cgit v1.1