GCC Middle and Back End API Reference
diagnostic-format-sarif.cc File Reference
#include "config.h"
#include "system.h"
#include "coretypes.h"
#include "diagnostic.h"
#include "diagnostic-metadata.h"
#include "diagnostic-path.h"
#include "diagnostic-format.h"
#include "diagnostic-buffer.h"
#include "json.h"
#include "cpplib.h"
#include "logical-location.h"
#include "diagnostic-client-data-hooks.h"
#include "diagnostic-diagram.h"
#include "text-art/canvas.h"
#include "diagnostic-format-sarif.h"
#include "diagnostic-format-text.h"
#include "ordered-hash-map.h"
#include "sbitmap.h"
#include "make-unique.h"
#include "selftest.h"
#include "selftest-diagnostic.h"
#include "selftest-diagnostic-show-locus.h"
#include "selftest-json.h"
#include "text-range-label.h"
#include "pretty-print-format-impl.h"
#include "pretty-print-urlifier.h"
#include "demangle.h"
#include "backtrace.h"
Include dependency graph for diagnostic-format-sarif.cc:

Data Structures

class  sarif_artifact_content
 
class  sarif_artifact_location
 
class  sarif_message
 
class  sarif_multiformat_message_string
 
class  sarif_log
 
class  sarif_run
 
class  sarif_tool
 
class  sarif_tool_component
 
class  sarif_invocation
 
class  sarif_artifact
 
class  sarif_location_manager
 
struct  sarif_location_manager::worklist_item
 
class  sarif_result
 
class  sarif_location
 
class  sarif_physical_location
 
class  sarif_region
 
class  sarif_location_relationship
 
class  sarif_code_flow
 
class  sarif_thread_flow
 
class  sarif_thread_flow_location
 
class  sarif_reporting_descriptor
 
class  sarif_reporting_descriptor_reference
 
class  sarif_tool_component_reference
 
class  sarif_fix
 
class  sarif_artifact_change
 
class  sarif_replacement
 
class  sarif_ice_notification
 
class  content_renderer
 
class  diagnostic_sarif_format_buffer
 
class  sarif_builder
 
class  sarif_builder::sarif_token_printer
 
struct  bt_closure
 
class  sarif_output_format
 
class  sarif_stream_output_format
 
class  sarif_file_output_format
 

Macros

#define INCLUDE_LIST
 
#define INCLUDE_MAP
 
#define INCLUDE_STRING
 
#define INCLUDE_VECTOR
 
#define PWD_PROPERTY_NAME   ("PWD")
 
#define FILE_PREFIX   ("file://")
 

Enumerations

enum class  location_relationship_kind { includes , is_included_by , relevant , NUM_KINDS }
 
enum class  diagnostic_artifact_role {
  analysis_target , debug_output_file , result_file , scanned_file ,
  traced_file , NUM_ROLES
}
 

Functions

static std::unique_ptr< json::stringmake_date_time_string_for_current_time ()
 
static const char * get_artifact_role_string (enum diagnostic_artifact_role role)
 
static const char * get_string_for_location_relationship_kind (enum location_relationship_kind kind)
 
static int bt_callback (void *data, uintptr_t pc, const char *filename, int lineno, const char *function)
 
static const char * maybe_get_sarif_level (diagnostic_t diag_kind)
 
static char * make_rule_id_for_diagnostic_kind (diagnostic_t diag_kind)
 
static char * make_pwd_uri_str ()
 
static const char * maybe_get_sarif_kind (enum logical_location_kind kind)
 
std::unique_ptr< sarif_logical_locationmake_sarif_logical_location_object (const logical_location &logical_loc)
 
label_text make_sarif_url_for_event (const sarif_code_flow *code_flow, diagnostic_event_id_t event_id)
 
static std::string escape_braces (const char *text)
 
static void set_string_property_escaping_braces (json::object &obj, const char *property_name, const char *value)
 
static const char * sarif_version_to_url (enum sarif_version version)
 
static const char * sarif_version_to_property (enum sarif_version version)
 
static void sarif_begin_embedded_link (pretty_printer *pp)
 
static void sarif_end_embedded_link (pretty_printer *pp, const char *url)
 
static void diagnostic_output_format_init_sarif (diagnostic_context &context, std::unique_ptr< sarif_output_format > fmt)
 
void diagnostic_output_format_init_sarif_stderr (diagnostic_context &context, const line_maps *line_maps, const char *main_input_filename_, bool formatted, enum sarif_version version)
 
diagnostic_output_file diagnostic_output_format_open_sarif_file (diagnostic_context &context, line_maps *line_maps, const char *base_file_name)
 
void diagnostic_output_format_init_sarif_file (diagnostic_context &context, line_maps *line_maps, const char *main_input_filename_, bool formatted, enum sarif_version version, const char *base_file_name)
 
void diagnostic_output_format_init_sarif_stream (diagnostic_context &context, const line_maps *line_maps, const char *main_input_filename_, bool formatted, enum sarif_version version, FILE *stream)
 
std::unique_ptr< diagnostic_output_formatmake_sarif_sink (diagnostic_context &context, const line_maps &line_maps, const char *main_input_filename_, enum sarif_version version, diagnostic_output_file output_file)
 

Variables

static const char *const bt_stop []
 

Macro Definition Documentation

◆ FILE_PREFIX

#define FILE_PREFIX   ("file://")

Referenced by make_pwd_uri_str().

◆ INCLUDE_LIST

#define INCLUDE_LIST
SARIF output for diagnostics
   Copyright (C) 2018-2025 Free Software Foundation, Inc.
   Contributed by David Malcolm <dmalcolm@redhat.com>.

This file is part of GCC.

GCC is free software; you can redistribute it and/or modify it under
the terms of the GNU General Public License as published by the Free
Software Foundation; either version 3, or (at your option) any later
version.

GCC is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
for more details.

You should have received a copy of the GNU General Public License
along with GCC; see the file COPYING3.  If not see
<http://www.gnu.org/licenses/>.   

◆ INCLUDE_MAP

#define INCLUDE_MAP

◆ INCLUDE_STRING

#define INCLUDE_STRING

◆ INCLUDE_VECTOR

#define INCLUDE_VECTOR

◆ PWD_PROPERTY_NAME

#define PWD_PROPERTY_NAME   ("PWD")
The ID value for use in "uriBaseId" properties (SARIF v2.1.0 section 3.4.4)
for when we need to express paths relative to PWD.   

Referenced by sarif_builder::make_artifact_location_object(), and sarif_builder::make_run_object().

Enumeration Type Documentation

◆ diagnostic_artifact_role

enum class diagnostic_artifact_role
strong
Corresponds to values for the SARIF artifact objects "roles" property.
(SARIF v2.1.0 section 3.24.6).   
Enumerator
analysis_target 
debug_output_file 
result_file 
scanned_file 
traced_file 
NUM_ROLES 

◆ location_relationship_kind

enum class location_relationship_kind
strong
Enumerator
includes 
is_included_by 
relevant 
NUM_KINDS 

Function Documentation

◆ bt_callback()

static int bt_callback ( void * data,
uintptr_t pc,
const char * filename,
int lineno,
const char * function )
static

◆ diagnostic_output_format_init_sarif()

static void diagnostic_output_format_init_sarif ( diagnostic_context & context,
std::unique_ptr< sarif_output_format > fmt )
static
Populate CONTEXT in preparation for SARIF output (either to stderr, or
to a file).   

References diagnostic_context::set_output_format().

Referenced by diagnostic_output_format_init_sarif_file(), diagnostic_output_format_init_sarif_stderr(), and diagnostic_output_format_init_sarif_stream().

◆ diagnostic_output_format_init_sarif_file()

void diagnostic_output_format_init_sarif_file ( diagnostic_context & context,
line_maps * line_maps,
const char * main_input_filename_,
bool formatted,
enum sarif_version version,
const char * base_file_name )
Populate CONTEXT in preparation for SARIF output to a file named
BASE_FILE_NAME.sarif.   

References base_file_name(), diagnostic_output_format_init_sarif(), diagnostic_output_format_open_sarif_file(), gcc_assert, make_unique(), and output_file.

Referenced by diagnostic_output_format_init().

◆ diagnostic_output_format_init_sarif_stderr()

void diagnostic_output_format_init_sarif_stderr ( diagnostic_context & context,
const line_maps * line_maps,
const char * main_input_filename_,
bool formatted,
enum sarif_version version )
Populate CONTEXT in preparation for SARIF output to stderr.   

References diagnostic_output_format_init_sarif(), gcc_assert, and make_unique().

Referenced by diagnostic_output_format_init().

◆ diagnostic_output_format_init_sarif_stream()

void diagnostic_output_format_init_sarif_stream ( diagnostic_context & context,
const line_maps * line_maps,
const char * main_input_filename_,
bool formatted,
enum sarif_version version,
FILE * stream )
Populate CONTEXT in preparation for SARIF output to STREAM.   

References diagnostic_output_format_init_sarif(), gcc_assert, and make_unique().

◆ diagnostic_output_format_open_sarif_file()

diagnostic_output_file diagnostic_output_format_open_sarif_file ( diagnostic_context & context,
line_maps * line_maps,
const char * base_file_name )
Attempt to open BASE_FILE_NAME.sarif for writing.
Return a non-null diagnostic_output_file,
or return a null diagnostic_output_file and complain to CONTEXT
using LINE_MAPS.   

References base_file_name(), diagnostic_context::emit_diagnostic_with_group(), fopen, and UNKNOWN_LOCATION.

Referenced by diagnostic_output_format_init_sarif_file(), and gcc::diagnostics_output_spec::sarif_scheme_handler::make_sink().

◆ escape_braces()

static std::string escape_braces ( const char * text)
static
In "3.11.5 Messages with placeholders":
"Within both plain text and formatted message strings, the characters
"{" and "}" SHALL be represented by the character sequences
"{{" and "}}" respectively."   

Referenced by set_string_property_escaping_braces().

◆ get_artifact_role_string()

static const char * get_artifact_role_string ( enum diagnostic_artifact_role role)
static
Get a string for ROLE corresponding to the
SARIF v2.1.0 section 3.24.6 "roles" property.   

References analysis_target, debug_output_file, gcc_unreachable, result_file, scanned_file, and traced_file.

Referenced by sarif_artifact::populate_roles().

◆ get_string_for_location_relationship_kind()

static const char * get_string_for_location_relationship_kind ( enum location_relationship_kind kind)
static

◆ make_date_time_string_for_current_time()

static std::unique_ptr< json::string > make_date_time_string_for_current_time ( )
static
Make a JSON string for the current date and time.
See SARIF v2.1.0 section 3.9 "Date/time properties".
Given that we don't run at the very beginning/end of the
process, it doesn't make sense to be more accurate than
the current second.   

Referenced by sarif_invocation::prepare_to_flush(), and sarif_invocation::sarif_invocation().

◆ make_pwd_uri_str()

static char * make_pwd_uri_str ( )
static
Get the PWD, or nullptr, as an absolute file-based URI,
adding a trailing forward slash (as required by SARIF v2.1.0
section 3.14.14).   

References FILE_PREFIX, and gcc_assert.

Referenced by sarif_builder::make_artifact_location_object_for_pwd().

◆ make_rule_id_for_diagnostic_kind()

static char * make_rule_id_for_diagnostic_kind ( diagnostic_t diag_kind)
static
Make a string for DIAG_KIND suitable for use a ruleId
(SARIF v2.1.0 section 3.27.5) as a fallback for when we don't
have anything better to use.   

References gcc_assert, and get_diagnostic_kind_text().

Referenced by sarif_builder::make_result_object().

◆ make_sarif_logical_location_object()

std::unique_ptr< sarif_logical_location > make_sarif_logical_location_object ( const logical_location & logical_loc)

◆ make_sarif_sink()

std::unique_ptr< diagnostic_output_format > make_sarif_sink ( diagnostic_context & context,
const line_maps & line_maps,
const char * main_input_filename_,
enum sarif_version version,
diagnostic_output_file output_file )

◆ make_sarif_url_for_event()

◆ maybe_get_sarif_kind()

static const char * maybe_get_sarif_kind ( enum logical_location_kind kind)
static

◆ maybe_get_sarif_level()

static const char * maybe_get_sarif_level ( diagnostic_t diag_kind)
static
Attempt to convert DIAG_KIND to a suitable value for the "level"
property (SARIF v2.1.0 section 3.27.10).

Return nullptr if there isn't one.   

Referenced by sarif_builder::make_result_object().

◆ sarif_begin_embedded_link()

static void sarif_begin_embedded_link ( pretty_printer * pp)
static
Print the start of an embedded link to PP, as per 3.11.6.   

References pp_character().

Referenced by sarif_builder::sarif_token_printer::print_tokens().

◆ sarif_end_embedded_link()

static void sarif_end_embedded_link ( pretty_printer * pp,
const char * url )
static
Print the end of an embedded link to PP, as per 3.11.6.   

References pp_character(), and pp_string().

Referenced by sarif_builder::sarif_token_printer::print_tokens().

◆ sarif_version_to_property()

static const char * sarif_version_to_property ( enum sarif_version version)
static
Convert VERSION to a value for the "version" property
of a "sarifLog" object (SARIF v2.1.0 section 3.13.2).   

References gcc_unreachable, v2_1_0, and v2_2_prerelease_2024_08_08.

Referenced by sarif_builder::make_top_level_object().

◆ sarif_version_to_url()

static const char * sarif_version_to_url ( enum sarif_version version)
static
Convert VERSION to a value for the "$schema" property
of a "sarifLog" object (SARIF v2.1.0 section 3.13.3).   

References gcc_unreachable, v2_1_0, and v2_2_prerelease_2024_08_08.

Referenced by sarif_builder::make_top_level_object().

◆ set_string_property_escaping_braces()

static void set_string_property_escaping_braces ( json::object & obj,
const char * property_name,
const char * value )
static

Variable Documentation

◆ bt_stop

const char* const bt_stop[]
static
Initial value:
=
{
"main",
"toplev::main",
"execute_one_pass",
"compile_file",
}
Functions at which to stop the backtrace print.  It's not
particularly helpful to print the callers of these functions.   

Referenced by bt_callback(), and bt_callback().