Skip to content

Commit 05c99b1

Browse files
committed
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 <[email protected]>
1 parent 5743e18 commit 05c99b1

File tree

5 files changed

+70
-6
lines changed

5 files changed

+70
-6
lines changed

gcc/analyzer/checker-event.cc

+43
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,8 @@ along with GCC; see the file COPYING3. If not see
5555
#include "analyzer/constraint-manager.h"
5656
#include "analyzer/checker-event.h"
5757
#include "analyzer/exploded-graph.h"
58+
#include "diagnostic-format-sarif.h"
59+
#include "tree-logical-location.h"
5860

5961
#if ENABLE_ANALYZER
6062

@@ -142,6 +144,30 @@ checker_event::get_meaning () const
142144
return meaning ();
143145
}
144146

147+
/* Implementation of diagnostic_event::maybe_add_sarif_properties
148+
for checker_event. */
149+
150+
void
151+
checker_event::
152+
maybe_add_sarif_properties (sarif_object &thread_flow_loc_obj) const
153+
{
154+
sarif_property_bag &props = thread_flow_loc_obj.get_or_create_properties ();
155+
#define PROPERTY_PREFIX "gcc/analyzer/checker_event/"
156+
props.set (PROPERTY_PREFIX "emission_id",
157+
diagnostic_event_id_to_json (m_emission_id));
158+
props.set_string (PROPERTY_PREFIX "kind", event_kind_to_string (m_kind));
159+
160+
if (m_original_fndecl != m_effective_fndecl)
161+
{
162+
tree_logical_location logical_loc (m_original_fndecl);
163+
props.set (PROPERTY_PREFIX "original_fndecl",
164+
make_sarif_logical_location_object (logical_loc));
165+
}
166+
if (m_original_depth != m_effective_depth)
167+
props.set_integer (PROPERTY_PREFIX "original_depth", m_original_depth);
168+
#undef PROPERTY_PREFIX
169+
}
170+
145171
/* Dump this event to PP (for debugging/logging purposes). */
146172

147173
void
@@ -498,6 +524,21 @@ state_change_event::get_meaning () const
498524

499525
/* class superedge_event : public checker_event. */
500526

527+
/* Implementation of diagnostic_event::maybe_add_sarif_properties
528+
for superedge_event. */
529+
530+
void
531+
superedge_event::maybe_add_sarif_properties (sarif_object &thread_flow_loc_obj)
532+
const
533+
{
534+
checker_event::maybe_add_sarif_properties (thread_flow_loc_obj);
535+
sarif_property_bag &props = thread_flow_loc_obj.get_or_create_properties ();
536+
#define PROPERTY_PREFIX "gcc/analyzer/superedge_event/"
537+
if (m_sedge)
538+
props.set (PROPERTY_PREFIX "superedge", m_sedge->to_json ());
539+
#undef PROPERTY_PREFIX
540+
}
541+
501542
/* Get the callgraph_superedge for this superedge_event, which must be
502543
for an interprocedural edge, rather than a CFG edge. */
503544

@@ -548,6 +589,8 @@ superedge_event::superedge_event (enum event_kind kind,
548589
m_eedge (eedge), m_sedge (eedge.m_sedge),
549590
m_var (NULL_TREE), m_critical_state (0)
550591
{
592+
/* Note that m_sedge can be nullptr for e.g. jumps through
593+
function pointers. */
551594
}
552595

553596
/* class cfg_edge_event : public superedge_event. */

gcc/analyzer/checker-event.h

+6
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,9 @@ class checker_event : public diagnostic_event
118118
return 0;
119119
}
120120

121+
void
122+
maybe_add_sarif_properties (sarif_object &thread_flow_loc_obj) const override;
123+
121124
/* Additional functionality. */
122125

123126
int get_original_stack_depth () const { return m_original_depth; }
@@ -391,6 +394,9 @@ class state_change_event : public checker_event
391394
class superedge_event : public checker_event
392395
{
393396
public:
397+
void maybe_add_sarif_properties (sarif_object &thread_flow_loc_obj)
398+
const override;
399+
394400
/* Mark this edge event as being either an interprocedural call or
395401
return in which VAR is in STATE, and that this is critical to the
396402
diagnostic (so that get_desc can attempt to get a better description

gcc/diagnostic-format-sarif.cc

+7-6
Original file line numberDiff line numberDiff line change
@@ -184,8 +184,6 @@ class sarif_builder
184184
void set_any_logical_locs_arr (json::object *location_obj,
185185
const logical_location *logical_loc);
186186
json::object *make_location_object (const diagnostic_event &event);
187-
json::object *
188-
make_logical_location_object (const logical_location &logical_loc) const;
189187
json::object *make_code_flow_object (const diagnostic_path &path);
190188
json::object *
191189
make_thread_flow_location_object (const diagnostic_event &event,
@@ -754,7 +752,7 @@ set_any_logical_locs_arr (json::object *location_obj,
754752
{
755753
if (!logical_loc)
756754
return;
757-
json::object *logical_loc_obj = make_logical_location_object (*logical_loc);
755+
json::object *logical_loc_obj = make_sarif_logical_location_object (*logical_loc);
758756
json::array *location_locs_arr = new json::array ();
759757
location_locs_arr->append (logical_loc_obj);
760758
location_obj->set ("logicalLocations", location_locs_arr);
@@ -1092,8 +1090,7 @@ maybe_get_sarif_kind (enum logical_location_kind kind)
10921090
or return NULL. */
10931091

10941092
json::object *
1095-
sarif_builder::
1096-
make_logical_location_object (const logical_location &logical_loc) const
1093+
make_sarif_logical_location_object (const logical_location &logical_loc)
10971094
{
10981095
json::object *logical_loc_obj = new json::object ();
10991096

@@ -1163,7 +1160,11 @@ json::object *
11631160
sarif_builder::make_thread_flow_location_object (const diagnostic_event &ev,
11641161
int path_event_idx)
11651162
{
1166-
json::object *thread_flow_loc_obj = new json::object ();
1163+
sarif_object *thread_flow_loc_obj = new sarif_object ();
1164+
1165+
/* Give diagnostic_event subclasses a chance to add custom properties
1166+
via a property bag. */
1167+
ev.maybe_add_sarif_properties (*thread_flow_loc_obj);
11671168

11681169
/* "location" property (SARIF v2.1.0 section 3.38.3). */
11691170
json::object *location_obj = make_location_object (ev);

gcc/diagnostic-format-sarif.h

+5
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ along with GCC; see the file COPYING3. If not see
2323

2424
#include "json.h"
2525

26+
class logical_location;
27+
2628
/* Concrete subclass of json::object for SARIF property bags
2729
(SARIF v2.1.0 section 3.8). */
2830

@@ -42,4 +44,7 @@ class sarif_object : public json::object
4244
sarif_property_bag &get_or_create_properties ();
4345
};
4446

47+
extern json::object *
48+
make_sarif_logical_location_object (const logical_location &logical_loc);
49+
4550
#endif /* ! GCC_DIAGNOSTIC_FORMAT_SARIF_H */

gcc/diagnostic-path.h

+9
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ along with GCC; see the file COPYING3. If not see
2424
#include "diagnostic.h" /* for ATTRIBUTE_GCC_DIAG. */
2525
#include "diagnostic-event-id.h"
2626

27+
class sarif_object;
28+
2729
/* A diagnostic_path is an optional additional piece of metadata associated
2830
with a diagnostic (via its rich_location).
2931
@@ -157,6 +159,13 @@ class diagnostic_event
157159
virtual meaning get_meaning () const = 0;
158160

159161
virtual diagnostic_thread_id_t get_thread_id () const = 0;
162+
163+
/* Hook for SARIF output to allow for adding diagnostic-specific
164+
properties to the threadFlowLocation object's property bag. */
165+
virtual void
166+
maybe_add_sarif_properties (sarif_object &/*thread_flow_loc_obj*/) const
167+
{
168+
}
160169
};
161170

162171
/* Abstract base class representing a thread of execution within

0 commit comments

Comments
 (0)