GCC Middle and Back End API Reference
diagnostics::sarif_location_manager Class Reference
Inheritance diagram for diagnostics::sarif_location_manager:
Collaboration diagram for diagnostics::sarif_location_manager:

Data Structures

struct  worklist_item

Public Member Functions

 sarif_location_manager ()
unsigned allocate_location_id ()
virtual void add_related_location (std::unique_ptr< sarif_location > location_obj, sarif_builder &builder)
void add_relationship_to_worklist (sarif_location &location_obj, enum worklist_item::kind kind, location_t where)
void process_worklist (sarif_builder &builder)
void process_worklist_item (sarif_builder &builder, const worklist_item &item)
sarif_property_bagget_or_create_properties ()
enum kind get_kind () const final override
void print (pretty_printer *pp, bool formatted) const final override
std::unique_ptr< valueclone () const final override
object * dyn_cast_object () final override
bool is_empty () const
void set (const char *key, value *v)
template<typename JsonType>
void set (const char *key, std::unique_ptr< JsonType > v)
valueget (const char *key) const
void set_string (const char *key, const char *utf8_value)
void set_integer (const char *key, long v)
void set_float (const char *key, double v)
void set_bool (const char *key, bool v)
size_t get_num_keys () const
const char * get_key (size_t i) const
std::unique_ptr< objectclone_as_object () const
void dump (FILE *, bool formatted) const
void DEBUG_FUNCTION dump () const
virtual string * dyn_cast_string ()
const pointer::token & get_pointer_token () const

Static Public Member Functions

static int compare (const json::object &obj_a, const json::object &obj_b)
static int compare (const json::value &val_a, const json::value &val_b)

Data Fields

pointer::token m_pointer_token

Private Types

typedef hash_map< char *, value *, simple_hashmap_traits< nofree_string_hash, value * > > map_t

Private Attributes

json::arraym_related_locations_arr
unsigned m_next_location_id
std::list< worklist_itemm_worklist
std::map< location_t, sarif_location * > m_included_from_locations
std::map< location_t, sarif_location * > m_unlabelled_secondary_locations
map_t m_map
auto_vec< const char * > m_keys

Detailed Description

A class for sarif_objects that own a "namespace" of numeric IDs for
 managing location objects within them.  Currently (SARIF v2.1.0)
 this is just for sarif_result (section 3.28.2), but it will likely
 eventually also be for notification objects; see
 https://github.com/oasis-tcs/sarif-spec/issues/540

 Consider locations with chains of include information e.g.

 > include-chain-1.c:
 >   #include "include-chain-1.h"

 include-chain-1.h:
   | // First set of decls, which will be referenced in notes
   | #include "include-chain-1-1.h"
   |
   | // Second set of decls, which will trigger the errors
   | #include "include-chain-1-2.h"

 include-chain-1-1.h:
   | int p;
   | int q;

 include-chain-1-1.h:
   | char p;
   | char q;

 GCC's textual output emits:
   |   In file included from PATH/include-chain-1.h:5,
   |                    from PATH/include-chain-1.c:30:
   |   PATH/include-chain-1-2.h:1:6: error: conflicting types for 'p'; have 'char'
   |       1 | char p;
   |         |      ^
   |   In file included from PATH/include-chain-1.h:2:
   |   PATH/include-chain-1-1.h:1:5: note: previous declaration of 'p' with type 'int'
   |       1 | int p;
   |         |     ^
   |   PATH/include-chain-1-2.h:2:6: error: conflicting types for 'q'; have 'char'
   |       2 | char q;
   |         |      ^
   |   PATH/include-chain-1-1.h:2:5: note: previous declaration of 'q' with type 'int'
   |       2 | int q;
   |         |     ^

 Whenever a SARIF location is added for a location_t that
 was #included from somewhere, we queue up the creation of a SARIF
 location for the location of the #include.  The worklist of queued
 locations is flushed when the result is finished, which lazily creates
 any additional related locations for the include chain, and the
 relationships between the locations.  Doing so can lead to further
 include locations being processed.  The worklist approach allows us
 to lazily explore the relevant part of the directed graph of location_t
 values implicit in our line_maps structure, replicating it as a directed
 graph of SARIF locations within the SARIF result object, like this:

 [0]: error in include-chain-1-2.h ("conflicting types for 'p'; have 'char'")
 [1]: #include "include-chain-1-2.h" in include-chain-1.h
 [2]: note in include-chain-1-2.h ("previous declaration of 'p' with type 'int'")
 [3]: #include "include-chain-1-1.h" in include-chain-1.h
 [4]: #include "include-chain-1.h" in include-chain-1.c

 where we want to capture this "includes" graph in SARIF form:
 . +-----------------------------------+ +----------------------------------+
 . |"id": 0                            | |"id": 2                           |
 . | error: "conflicting types for 'p';| | note: previous declaration of 'p'|
 . |  have 'char'"|                    | | with type 'int'")                |
 . | in include-chain-1-2.h            | | in include-chain-1-1.h           |
 . +-----------------------------------+ +----------------------------------+
 .            ^         |                            ^         |
 .   includes |         | included-by       includes |         | included-by
 .            |         V                            |         V
 .  +--------------------------------+    +--------------------------------+
 .  |"id": 1                         |    |"id": 3                         |
 .  | #include "include-chain-1-2.h" |    | #include "include-chain-1-1.h" |
 .  | in include-chain-1.h           |    | in include-chain-1.h           |
 .  +--------------------------------+    +--------------------------------+
 .                   ^     |                       ^    |
 .          includes |     | included-by  includes |    | included-by
 .                   |     V                       |    V
 .                  +------------------------------------+
 .                  |"id": 4                             |
 .                  | The  #include "include-chain-1.h"  |
 .                  | in include-chain-1.c               |
 .                  +------------------------------------+

Member Typedef Documentation

◆ map_t

typedef hash_map<char *, value *, simple_hashmap_traits<nofree_string_hash, value *> > json::object::map_t
privateinherited

Constructor & Destructor Documentation

◆ sarif_location_manager()

diagnostics::sarif_location_manager::sarif_location_manager ( )
inline

Member Function Documentation

◆ add_related_location()

void diagnostics::sarif_location_manager::add_related_location ( std::unique_ptr< sarif_location > location_obj,
sarif_builder & builder )
virtual
class sarif_location_manager : public sarif_object.   
Base implementation of sarif_location_manager::add_related_location vfunc.

Add LOCATION_OBJ to this object's "relatedLocations" array,
creating it if it doesn't yet exist.   

Reimplemented in diagnostics::sarif_ice_notification.

References m_related_locations_arr, and json::object::set().

Referenced by diagnostics::sarif_ice_notification::add_related_location(), diagnostics::sarif_result::on_diagram(), diagnostics::sarif_result::on_nested_diagnostic(), and process_worklist_item().

◆ add_relationship_to_worklist()

void diagnostics::sarif_location_manager::add_relationship_to_worklist ( sarif_location & location_obj,
enum worklist_item::kind kind,
location_t where )

◆ allocate_location_id()

unsigned diagnostics::sarif_location_manager::allocate_location_id ( )
inline

◆ clone()

std::unique_ptr< value > object::clone ( ) const
finaloverridevirtualinherited

Implements json::value.

References clone_as_object().

◆ clone_as_object()

std::unique_ptr< object > object::clone_as_object ( ) const
inherited

◆ compare() [1/2]

int object::compare ( const json::object & obj_a,
const json::object & obj_b )
staticinherited
Subroutine of json::compare for comparing a pairs of objects.   

References json::value::compare(), gcc_assert, get(), m_keys, and m_map.

Referenced by json::value::compare().

◆ compare() [2/2]

int value::compare ( const json::value & val_a,
const json::value & val_b )
staticinherited
A deterministic total ordering for comparing json values, so that we
can e.g. put them in std::map.

This is intended to follow the condition for equality described in
the JSON Schema standard (§4.3, “Instance equality”), as referenced
by SARIF v2.1.0 (§3.7.3 "Array properties with unique values"), but has
the following limitations:
- numbers are supposed to be checked for "the same mathematical value",
but in this implementation int vs float numbers won't compare as equal,
and float number comparison is bitwise
- strings are supposed to be "the same codepoint-for-codepoint", but
this implementation doesn't take into account canonicalization issues.   

References json::object::compare(), compare(), gcc_unreachable, json::float_number::get(), json::integer_number::get(), get_kind(), json::string::get_string(), json::JSON_ARRAY, json::JSON_FALSE, json::JSON_FLOAT, json::JSON_INTEGER, json::JSON_NULL, json::JSON_OBJECT, json::JSON_STRING, json::JSON_TRUE, and json::array::size().

Referenced by json::object::compare(), compare(), and diagnostics::sarif_array_of_unique< JsonElementType >::comparator_t::operator()().

◆ dump() [1/2]

void value::dump ( ) const
inherited
A convenience function for debugging.
Dump to stderr with formatting, and a trailing newline.  

References dump().

◆ dump() [2/2]

void value::dump ( FILE * outf,
bool formatted ) const
inherited
class json::value.   
Dump this json::value tree to OUTF.

The key/value pairs of json::objects are printed in the order
in which the keys were originally inserted.   

References output_buffer::m_stream, pp_buffer(), pp_flush(), and print().

Referenced by dump(), generate_results(), and diagnostics::sarif_serialization_format_json::write_to_file().

◆ dyn_cast_object()

object * json::object::dyn_cast_object ( )
inlinefinaloverridevirtualinherited

Reimplemented from json::value.

References dyn_cast_object(), and final().

Referenced by dyn_cast_object().

◆ dyn_cast_string()

virtual string * json::value::dyn_cast_string ( )
inlinevirtualinherited

Reimplemented in json::string.

◆ get()

◆ get_key()

const char * json::object::get_key ( size_t i) const
inlineinherited

References i, and m_keys.

Referenced by diagnostics::copy_any_property_bag().

◆ get_kind()

enum kind json::object::get_kind ( ) const
inlinefinaloverridevirtualinherited

Implements json::value.

References final(), and json::JSON_OBJECT.

◆ get_num_keys()

size_t json::object::get_num_keys ( ) const
inlineinherited

References m_keys.

Referenced by diagnostics::copy_any_property_bag().

◆ get_or_create_properties()

◆ get_pointer_token()

const pointer::token & json::value::get_pointer_token ( ) const
inlineinherited

References m_pointer_token.

◆ is_empty()

bool json::object::is_empty ( ) const
inlineinherited

References m_map.

◆ print()

void object::print ( pretty_printer * pp,
bool formatted ) const
finaloverridevirtualinherited

◆ process_worklist()

void diagnostics::sarif_location_manager::process_worklist ( sarif_builder & builder)
Process all items in this result's worklist.
Doing so may temporarily add new items to the end
of the worklist.
Handling any item should be "lazy", and thus we should
eventually drain the queue and terminate.   

References m_worklist, and process_worklist_item().

◆ process_worklist_item()

◆ set() [1/2]

template<typename JsonType>
void json::object::set ( const char * key,
std::unique_ptr< JsonType > v )
inlineinherited

References set().

◆ set() [2/2]

void object::set ( const char * key,
value * v )
inherited
Set the json::value * for KEY, taking ownership of V
(and taking a copy of KEY if necessary).   

References gcc_assert, m_keys, m_map, and json::value::m_pointer_token.

Referenced by optrecord_json_writer::add_pass_list(), optrecord_json_writer::add_record(), diagnostics::sarif_location_manager::add_related_location(), diagnostics::copy_any_property_bag(), generate_results(), diagnostics::sarif_builder::get_or_create_artifact(), diagnostics::sarif_object::get_or_create_properties(), optrecord_json_writer::inlining_chain_to_json(), json_set_prime_path_coverage(), diagnostics::sarif_location_relationship::lazily_add_kind(), diagnostics::sarif_location::lazily_add_relationships_array(), diagnostics::sarif_builder::make_fix_object(), diagnostics::sarif_builder::make_replacement_object(), diagnostics::sarif_builder::make_result_object(), diagnostics::sarif_builder::make_run_object(), diagnostics::sarif_builder::make_tool_object(), diagnostics::sarif_builder::make_top_level_object(), diagnostics::sarif_builder::maybe_make_cwe_taxonomy_object(), diagnostics::sarif_builder::maybe_make_physical_location_object(), optrecord_json_writer::optinfo_to_json(), optrecord_json_writer::optrecord_json_writer(), output_intermediate_json_line(), output_json_intermediate_file(), optrecord_json_writer::pass_to_json(), diagnostics::sarif_artifact::populate_contents(), diagnostics::sarif_artifact::populate_roles(), diagnostics::sarif_builder::populate_thread_flow_location_object(), diagnostics::sarif_invocation::prepare_to_flush(), diagnostics::sarif_code_flow::sarif_code_flow(), diagnostics::sarif_ice_notification::sarif_ice_notification(), diagnostics::sarif_invocation::sarif_invocation(), diagnostics::sarif_thread_flow::sarif_thread_flow(), set(), diagnostics::sarif_builder::set_any_logical_locs_arr(), set_bool(), set_float(), diagnostics::sarif_property_bag::set_graph(), set_integer(), diagnostics::sarif_property_bag::set_logical_location(), and set_string().

◆ set_bool()

void object::set_bool ( const char * key,
bool v )
inherited
Set value of KEY within this object to the JSON
literal true or false, based on V.   

References set().

Referenced by diagnostics::sarif_builder::make_location_object(), output_intermediate_json_line(), and diagnostics::sarif_invocation::prepare_to_flush().

◆ set_float()

void object::set_float ( const char * key,
double v )
inherited
Set value of KEY within this object to a JSON
floating point value based on V.   

References set().

◆ set_integer()

◆ set_string()

Field Documentation

◆ m_included_from_locations

std::map<location_t, sarif_location *> diagnostics::sarif_location_manager::m_included_from_locations
private

Referenced by process_worklist_item().

◆ m_keys

auto_vec<const char *> json::object::m_keys
privateinherited

◆ m_map

map_t json::object::m_map
privateinherited

◆ m_next_location_id

unsigned diagnostics::sarif_location_manager::m_next_location_id
private

◆ m_pointer_token

pointer::token json::value::m_pointer_token
inherited

◆ m_related_locations_arr

json::array* diagnostics::sarif_location_manager::m_related_locations_arr
private

◆ m_unlabelled_secondary_locations

std::map<location_t, sarif_location *> diagnostics::sarif_location_manager::m_unlabelled_secondary_locations
private

Referenced by process_worklist_item().

◆ m_worklist

std::list<worklist_item> diagnostics::sarif_location_manager::m_worklist
private

The documentation for this class was generated from the following file: