GCC Middle and Back End API Reference
|
Data Structures | |
class | sarif_token_printer |
Private Attributes | |
diagnostic_context & | m_context |
pretty_printer * | m_printer |
const line_maps * | m_line_maps |
sarif_token_printer | m_token_printer |
enum sarif_version | m_version |
std::unique_ptr< sarif_invocation > | m_invocation_obj |
std::unique_ptr< json::array > | m_results_array |
std::unique_ptr< sarif_result > | m_cur_group_result |
ordered_hash_map< nofree_string_hash, sarif_artifact * > | m_filename_to_artifact_map |
bool | m_seen_any_relative_paths |
hash_set< free_string_hash > | m_rule_id_set |
std::unique_ptr< json::array > | m_rules_arr |
hash_set< int_hash< int, 0, 1 > > | m_cwe_id_set |
int | m_tabstop |
bool | m_formatted |
unsigned | m_next_result_idx |
sarif_code_flow * | m_current_code_flow |
Friends | |
class | diagnostic_sarif_format_buffer |
A class for managing SARIF output (for -fdiagnostics-format=sarif-stderr and -fdiagnostics-format=sarif-file). As diagnostics occur, we build "result" JSON objects, and accumulate state: - which source files are referenced - which warnings are emitted - which CWEs are used At the end of the compile, we use the above to build the full SARIF object tree, adding the result objects to the correct place, and creating objects for the various source files, warnings and CWEs referenced. Implemented: - fix-it hints - CWE metadata - diagnostic groups (see limitations below) - logical locations (e.g. cfun) - labelled ranges (as annotations) - secondary ranges without labels (as related locations) Known limitations: - GCC supports nesting of diagnostics (one-deep nesting via auto_diagnostic_group, and arbitrary nesting via auto_diagnostic_nesting_level). These are captured in the SARIF as related locations, and so we only capture location and message information from such nested diagnostics (e.g. we ignore fix-it hints on them). Diagnostics within an auto_diagnostic_nesting_level have their nesting level captured as a property. - although we capture command-line arguments (section 3.20.2), we don't yet capture response files. - doesn't capture "artifact.encoding" property (SARIF v2.1.0 section 3.24.9). - doesn't capture hashes of the source files ("artifact.hashes" property (SARIF v2.1.0 section 3.24.11). - doesn't capture the "analysisTarget" property (SARIF v2.1.0 section 3.27.13). - doesn't capture -Werror cleanly - doesn't capture inlining information (can SARIF handle this?) - doesn't capture macro expansion information (can SARIF handle this?). - doesn't capture any diagnostic_metadata::rules associated with a diagnostic.
sarif_builder::sarif_builder | ( | diagnostic_context & | context, |
pretty_printer & | printer, | ||
const line_maps * | line_maps, | ||
const char * | main_input_filename_, | ||
bool | formatted, | ||
enum sarif_version | version ) |
class sarif_builder.
sarif_builder's ctor.
References analysis_target, gcc_assert, get_or_create_artifact(), and m_line_maps.
sarif_builder::~sarif_builder | ( | ) |
References m_filename_to_artifact_map.
|
private |
If WHERE was #included from somewhere, add a worklist item to LOC_MGR to lazily add a location for the #include location, and relationships between it and the LOCATION_OBJ. Compare with diagnostic_context::report_current_module, but rather than iterating the current chain, we add the next edge and iterate in the worklist, so that edges are only added once.
References sarif_location_manager::add_relationship_to_worklist(), BUILTINS_LOCATION, m_line_maps, and map.
Referenced by make_location_object(), make_location_object(), and make_location_object().
void sarif_builder::emit_diagram | ( | const diagnostic_diagram & | diagram | ) |
Implementation of diagnostic_context::m_diagrams.m_emission_cb for SARIF output.
References gcc_assert, and m_cur_group_result.
Referenced by sarif_output_format::on_diagram().
void sarif_builder::end_group | ( | ) |
Implementation of "end_group_cb" for SARIF output.
References m_cur_group_result, and m_results_array.
Referenced by sarif_output_format::on_end_group().
void sarif_builder::flush_to_file | ( | FILE * | outf | ) |
Create a top-level object, and add it to all the results (and other entities) we've seen so far. Flush it all to OUTF.
References flush_to_object(), and m_formatted.
Referenced by sarif_file_output_format::~sarif_file_output_format(), and sarif_stream_output_format::~sarif_stream_output_format().
std::unique_ptr< sarif_log > sarif_builder::flush_to_object | ( | ) |
Create a top-level object, and add it to all the results (and other entities) we've seen so far, moving ownership to the object.
References m_invocation_obj, m_results_array, and make_top_level_object().
Referenced by flush_to_file().
|
inline |
References m_current_code_flow.
Referenced by sarif_builder::sarif_token_printer::print_tokens().
|
inline |
References m_context.
Referenced by sarif_result::on_nested_diagnostic(), and sarif_invocation::prepare_to_flush().
|
private |
Ensure that we have an "artifact" object (SARIF v2.1.0 section 3.24) for FILENAME, adding it to m_filename_to_artifact_map if not already found, and adding ROLE to it. If EMBED_CONTENTS is true, then flag that we will attempt to embed the contents of this artifact when writing it out.
References sarif_artifact::add_role(), analysis_target, debug_output_file, gcc_unreachable, ordered_hash_map< KeyId, Value, Traits >::get(), diagnostic_context::get_client_data_hooks(), m_context, m_filename_to_artifact_map, make_artifact_location_object(), ordered_hash_map< KeyId, Value, Traits >::put(), result_file, scanned_file, json::object::set(), json::object::set_string(), and traced_file.
Referenced by maybe_make_physical_location_object(), and sarif_builder().
|
inline |
References m_printer.
Referenced by make_location_object(), sarif_result::on_nested_diagnostic(), and sarif_ice_notification::sarif_ice_notification().
|
inline |
References gcc_assert.
Referenced by sarif_output_format::get_result().
|
private |
Get the column number within EXPLOC.
References diagnostic_context::get_file_cache(), location_compute_display_column(), m_context, and m_tabstop.
Referenced by make_region_object_for_hint(), and maybe_make_region_object().
|
private |
Attempt to read the given range of lines from FILENAME; return a freshly-allocated 0-terminated buffer containing them, or nullptr.
References char_span::get_buffer(), diagnostic_context::get_file_cache(), file_cache::get_source_line(), i, char_span::length(), and m_context.
Referenced by maybe_make_artifact_content_object().
|
inline |
References m_token_printer.
Referenced by sarif_output_format::update_printer().
|
inline |
References m_version.
Referenced by sarif_invocation::add_notification_for_ice(), and sarif_ice_notification::add_related_location().
|
private |
Make an "artifactChange" object (SARIF v2.1.0 section 3.56) for RICHLOC.
References i, make_artifact_location_object(), make_replacement_object(), and make_unique().
Referenced by make_fix_object().
|
private |
Make an "artifactContent" object (SARIF v2.1.0 section 3.3) for TEXT.
References make_unique().
Referenced by make_replacement_object().
std::unique_ptr< sarif_artifact_location > sarif_builder::make_artifact_location_object | ( | const char * | filename | ) |
Make an "artifactLocation" object (SARIF v2.1.0 section 3.4) for FILENAME, or return nullptr.
References m_seen_any_relative_paths, make_unique(), and PWD_PROPERTY_NAME.
Referenced by get_or_create_artifact(), make_artifact_change_object(), make_artifact_location_object(), maybe_make_physical_location_object(), and sarif_invocation::sarif_invocation().
|
private |
Make an "artifactLocation" object (SARIF v2.1.0 section 3.4) for LOC, or return nullptr.
References LOCATION_FILE, and make_artifact_location_object().
|
private |
Make an "artifactLocation" object (SARIF v2.1.0 section 3.4) for the pwd, for use in the "run.originalUriBaseIds" property (SARIF v2.1.0 section 3.14.14) when we have any relative paths.
References free(), gcc_assert, make_pwd_uri_str(), and make_unique().
Referenced by make_run_object().
|
private |
Make a "codeFlow" object (SARIF v2.1.0 section 3.36) for PATH.
References sarif_thread_flow::add_location(), json::object::get(), diagnostic_event::get_thread_id(), i, m_current_code_flow, make_unique(), and populate_thread_flow_location_object().
Referenced by make_result_object().
|
private |
Make a "toolComponent" object (SARIF v2.1.0 section 3.19) for what SARIF calls the "driver" (see SARIF v2.1.0 section 3.18.1).
References free(), diagnostic_context::get_client_data_hooks(), m_context, m_rules_arr, and make_unique().
Referenced by make_tool_object().
|
private |
Make a "fix" object (SARIF v2.1.0 section 3.55) for RICHLOC.
References make_artifact_change_object(), make_unique(), and json::object::set().
Referenced by make_result_object().
|
private |
Make a "location" object (SARIF v2.1.0 section 3.28) for EVENT within a diagnostic_path.
References add_any_include_chain(), pretty_printer::clone(), json::object::get(), get_printer(), make_message_object(), make_unique(), maybe_make_physical_location_object(), pp_formatted_text(), and set_any_logical_locs_arr().
std::unique_ptr< sarif_location > sarif_builder::make_location_object | ( | sarif_location_manager & | loc_mgr, |
const rich_location & | rich_loc, | ||
const logical_location * | logical_loc, | ||
enum diagnostic_artifact_role | role ) |
Make a "location" object (SARIF v2.1.0 section 3.28) for RICH_LOC and LOGICAL_LOC. Use LOC_MGR for any locations that need "id" values, and for any worklist items.
References add_any_include_chain(), sarif_location_manager::add_relationship_to_worklist(), diagnostic_source_printing_options::colorize_source_p, diagnostic_finish(), diagnostic_initialize(), diagnostic_source_printing_options::enabled, json::object::get(), diagnostic_context::get_escape_format(), diagnostic_output_format::get_printer(), i, m_context, diagnostic_context::m_source_printing, make_message_object(), make_multiformat_message_string(), make_unique(), maybe_make_physical_location_object(), maybe_make_region_object(), pp_formatted_text(), diagnostic_source_print_policy::print(), set_any_logical_locs_arr(), json::object::set_bool(), diagnostic_context::set_escape_format(), diagnostic_source_printing_options::show_labels_p, diagnostic_source_printing_options::show_line_numbers_p, and sarif_location_manager::worklist_item::unlabelled_secondary_location.
Referenced by make_locations_arr(), sarif_result::on_nested_diagnostic(), populate_thread_flow_location_object(), and sarif_location_manager::process_worklist_item().
std::unique_ptr< sarif_location > sarif_builder::make_location_object | ( | sarif_location_manager & | loc_mgr, |
location_t | loc, | ||
enum diagnostic_artifact_role | role ) |
Make a "location" object (SARIF v2.1.0 section 3.28) for WHERE within an include chain.
References add_any_include_chain(), json::object::get(), make_unique(), and maybe_make_physical_location_object().
std::unique_ptr< json::array > sarif_builder::make_locations_arr | ( | sarif_location_manager & | loc_mgr, |
const diagnostic_info & | diagnostic, | ||
enum diagnostic_artifact_role | role ) |
Make an array suitable for use as the "locations" property of: - a "result" object (SARIF v2.1.0 section 3.27.12), or - a "notification" object (SARIF v2.1.0 section 3.58.4). Use LOC_MGR for any locations that need "id" values.
References diagnostic_context::get_client_data_hooks(), m_context, make_location_object(), and make_unique().
Referenced by make_result_object(), and sarif_ice_notification::sarif_ice_notification().
std::unique_ptr< sarif_message > sarif_builder::make_message_object | ( | const char * | msg | ) | const |
Make a "message" object (SARIF v2.1.0 section 3.11) for MSG.
References make_unique(), and msg.
Referenced by make_location_object(), make_location_object(), make_result_object(), sarif_result::on_nested_diagnostic(), and sarif_ice_notification::sarif_ice_notification().
std::unique_ptr< sarif_message > sarif_builder::make_message_object_for_diagram | ( | const diagnostic_diagram & | diagram | ) |
Make a "message" object (SARIF v2.1.0 section 3.11) for DIAGRAM. We emit the diagram as a code block within the Markdown part of the message.
References diagnostic_diagram::get_alt_text(), diagnostic_diagram::get_canvas(), m_printer, make_unique(), pp_clear_output_area(), pp_formatted_text(), pp_set_prefix(), and pp_take_prefix().
Referenced by sarif_result::on_diagram().
|
private |
Make a "multiformatMessageString object" (SARIF v2.1.0 section 3.12) for MSG.
References make_unique(), and msg.
Referenced by make_location_object(), and maybe_make_cwe_taxonomy_object().
|
private |
Make a "region" object (SARIF v2.1.0 section 3.30) for the deletion region of HINT (as per SARIF v2.1.0 section 3.57.3).
References expand_location(), get_sarif_column(), and make_unique().
Referenced by make_replacement_object().
|
private |
Make a "replacement" object (SARIF v2.1.0 section 3.57) for HINT.
References make_artifact_content_object(), make_region_object_for_hint(), make_unique(), and json::object::set().
Referenced by make_artifact_change_object().
|
private |
Make a "reportingDescriptor" object (SARIF v2.1.0 section 3.49) for CWE_ID, for use within the CWE taxa array.
References free(), get_cwe_url(), make_unique(), pp_formatted_text(), and pp_printf().
Referenced by maybe_make_cwe_taxonomy_object().
|
private |
Make a "reportingDescriptor" object (SARIF v2.1.0 section 3.49) for a GCC warning.
References free(), m_context, diagnostic_context::make_option_url(), and make_unique().
Referenced by make_result_object().
|
private |
Make a "reportingDescriptorReference" object (SARIF v2.1.0 section 3.52) referencing CWE_ID, for use within a result object. Also, add CWE_ID to m_cwe_id_set.
References hash_set< KeyId, Lazy, Traits >::add(), gcc_assert, m_cwe_id_set, make_tool_component_reference_object_for_cwe(), make_unique(), pp_formatted_text(), and pp_printf().
Referenced by make_result_object().
|
private |
Make a "result" object (SARIF v2.1.0 section 3.27) for DIAGNOSTIC.
References hash_set< KeyId, Lazy, Traits >::add(), hash_set< KeyId, Lazy, Traits >::contains(), free(), m_context, m_printer, m_rule_id_set, m_rules_arr, make_code_flow_object(), make_fix_object(), make_locations_arr(), make_message_object(), diagnostic_context::make_option_name(), make_reporting_descriptor_object_for_warning(), make_reporting_descriptor_reference_object_for_cwe_id(), make_rule_id_for_diagnostic_kind(), make_unique(), maybe_get_sarif_level(), pp_clear_output_area(), pp_formatted_text(), result_file, and json::object::set().
Referenced by on_report_diagnostic().
|
private |
Make a "run" object (SARIF v2.1.0 section 3.14).
References sarif_artifact::embed_contents_p(), ordered_hash_map< KeyId, Value, Traits >::empty(), m_filename_to_artifact_map, m_seen_any_relative_paths, make_artifact_location_object_for_pwd(), make_tool_object(), make_unique(), maybe_make_taxonomies_array(), sarif_artifact::populate_contents(), sarif_artifact::populate_roles(), PWD_PROPERTY_NAME, and json::object::set().
Referenced by make_top_level_object().
|
private |
Attempt to generate a JSON object representing a backtrace, for adding to ICE notifications.
References bt_callback(), and make_unique().
Referenced by on_report_diagnostic().
|
private |
Make a "toolComponentReference" object (SARIF v2.1.0 section 3.54) that references the CWE taxonomy.
References make_unique().
Referenced by make_reporting_descriptor_reference_object_for_cwe_id().
|
private |
Make a "tool" object (SARIF v2.1.0 section 3.18).
References diagnostic_context::get_client_data_hooks(), diagnostic_client_plugin_info::get_full_name(), diagnostic_client_plugin_info::get_short_name(), diagnostic_client_plugin_info::get_version(), m_context, make_driver_tool_component_object(), make_unique(), and json::object::set().
Referenced by make_run_object().
|
private |
Make a top-level "sarifLog" object (SARIF v2.1.0 section 3.13).
References m_version, make_run_object(), make_unique(), sarif_version_to_property(), sarif_version_to_url(), and json::object::set().
Referenced by flush_to_object().
std::unique_ptr< sarif_artifact_content > sarif_builder::maybe_make_artifact_content_object | ( | const char * | filename | ) | const |
Make an "artifactContent" object (SARIF v2.1.0 section 3.3) for the full contents of FILENAME.
References char_span::get_buffer(), diagnostic_context::get_file_cache(), file_cache::get_source_file_content(), char_span::length(), m_context, and make_unique().
Referenced by maybe_make_region_object_for_context(), and sarif_artifact::populate_contents().
|
private |
Make an "artifactContent" object (SARIF v2.1.0 section 3.3) for the given run of lines within FILENAME (including the endpoints). If R is non-NULL, use it to potentially set the "rendered" property (3.3.4).
References free(), get_source_lines(), make_unique(), and r.
|
private |
If we've seen any CWE IDs, make a "toolComponent" object (SARIF v2.1.0 section 3.19) representing the CWE taxonomy, as per 3.19.3. Populate the "taxa" property with all of the CWE IDs in m_cwe_id_set. Otherwise return nullptr.
References hash_set< KeyId, Lazy, Traits >::is_empty(), m_cwe_id_set, make_multiformat_message_string(), make_reporting_descriptor_object_for_cwe_id(), make_unique(), and json::object::set().
Referenced by maybe_make_taxonomies_array().
|
private |
If M has any known meaning, make a json array suitable for the "kinds" property of a "threadFlowLocation" object (SARIF v2.1.0 section 3.38.8). Otherwise, return nullptr.
References diagnostic_event::meaning::m_noun, diagnostic_event::meaning::m_property, diagnostic_event::meaning::m_verb, make_unique(), diagnostic_event::meaning::maybe_get_noun_str(), diagnostic_event::meaning::maybe_get_property_str(), diagnostic_event::meaning::maybe_get_verb_str(), diagnostic_event::NOUN_unknown, diagnostic_event::PROPERTY_unknown, and diagnostic_event::VERB_unknown.
Referenced by populate_thread_flow_location_object().
|
private |
Make a "physicalLocation" object (SARIF v2.1.0 section 3.29) for LOC. If COLUMN_OVERRIDE is non-zero, then use it as the column number if LOC has no column information. Ensure that we have an artifact object for the file, adding ROLE to it, and flagging that we will attempt to embed the contents of the artifact when writing it out.
References BUILTINS_LOCATION, get_or_create_artifact(), LOCATION_FILE, make_artifact_location_object(), make_unique(), maybe_make_region_object(), maybe_make_region_object_for_context(), and json::object::set().
Referenced by make_location_object(), make_location_object(), and make_location_object().
|
private |
Make a "region" object (SARIF v2.1.0 section 3.30) for LOC, or return nullptr. If COLUMN_OVERRIDE is non-zero, then use it as the column number if LOC has no column information. We only support text properties of regions ("text regions"), not binary properties ("binary regions"); see 3.30.1.
References BUILTINS_LOCATION, expand_location(), get_finish(), get_pure_location(), get_sarif_column(), get_start(), and make_unique().
Referenced by make_location_object(), and maybe_make_physical_location_object().
|
private |
Make a "region" object (SARIF v2.1.0 section 3.30) for the "contextRegion" property (SARIF v2.1.0 section 3.29.5) of a "physicalLocation". This is similar to maybe_make_region_object, but ignores column numbers, covering the line(s) as a whole, and including a "snippet" property embedding those source lines, making it easier for consumers to show the pertinent source.
References BUILTINS_LOCATION, expand_location(), get_finish(), get_pure_location(), get_start(), make_unique(), and maybe_make_artifact_content_object().
Referenced by maybe_make_physical_location_object().
|
private |
If we've seen any CWE IDs, make an array for the "taxonomies" property (SARIF v2.1.0 section 3.14.8) of a run object, containing a single "toolComponent" (3.19) as per 3.19.3, representing the CWE. Otherwise return nullptr.
References make_unique(), and maybe_make_cwe_taxonomy_object().
Referenced by make_run_object().
|
inline |
References m_results_array.
Referenced by sarif_output_format::num_results().
void sarif_builder::on_report_diagnostic | ( | const diagnostic_info & | diagnostic, |
diagnostic_t | orig_diag_kind, | ||
diagnostic_sarif_format_buffer * | buffer ) |
Implementation of "on_report_diagnostic" for SARIF output.
References diagnostic_sarif_format_buffer::add_result(), fnotice(), gcc_assert, diagnostic_context::get_urlifier(), m_context, m_cur_group_result, m_invocation_obj, m_next_result_idx, m_printer, make_result_object(), make_stack_from_backtrace(), and pp_output_formatted_text().
Referenced by sarif_output_format::on_report_diagnostic().
|
private |
Populate TFL_OBJ, a "threadFlowLocation" object (SARIF v2.1.0 section 3.38) based on EVENT.
References diagnostic_event::get_meaning(), diagnostic_event::get_stack_depth(), make_location_object(), diagnostic_event::maybe_add_sarif_properties(), maybe_make_kinds_array(), json::object::set(), json::object::set_integer(), and traced_file.
Referenced by make_code_flow_object().
|
private |
If LOGICAL_LOC is non-null, use it to create a "logicalLocations" property within LOCATION_OBJ (SARIF v2.1.0 section 3.28.4).
References make_sarif_logical_location_object(), make_unique(), and json::object::set().
Referenced by make_location_object(), and make_location_object().
|
inline |
References m_printer.
Referenced by sarif_output_format::update_printer().
|
inline |
References m_cur_group_result.
Referenced by sarif_output_format::~sarif_output_format().
|
friend |
|
private |
Referenced by get_context(), get_or_create_artifact(), get_sarif_column(), get_source_lines(), make_driver_tool_component_object(), make_location_object(), make_locations_arr(), make_reporting_descriptor_object_for_warning(), make_result_object(), make_tool_object(), maybe_make_artifact_content_object(), and on_report_diagnostic().
|
private |
Referenced by emit_diagram(), end_group(), on_report_diagnostic(), and take_current_result().
|
private |
Referenced by get_code_flow_for_event_ids(), and make_code_flow_object().
|
private |
Referenced by get_or_create_artifact(), make_run_object(), and ~sarif_builder().
|
private |
Referenced by flush_to_file().
|
private |
Referenced by flush_to_object(), and on_report_diagnostic().
|
private |
Referenced by add_any_include_chain(), and sarif_builder().
|
private |
Referenced by on_report_diagnostic().
|
private |
Referenced by get_printer(), make_message_object_for_diagram(), make_result_object(), on_report_diagnostic(), and set_printer().
|
private |
Referenced by end_group(), diagnostic_sarif_format_buffer::flush(), flush_to_object(), and num_results().
|
private |
Referenced by make_result_object().
|
private |
Referenced by make_driver_tool_component_object(), and make_result_object().
|
private |
Referenced by make_artifact_location_object(), and make_run_object().
|
private |
Referenced by get_sarif_column().
|
private |
Referenced by get_token_printer().
|
private |
Referenced by get_version(), and make_top_level_object().