/* Generate a JSON object for LOC. */
-json::value *
-json_from_expanded_location (diagnostic_context *context, location_t loc)
+static json::value *
+json_from_expanded_location (diagnostic_context &context, location_t loc)
{
expanded_location exploc = expand_location (loc);
json::object *result = new json::object ();
result->set_string ("file", exploc.file);
result->set_integer ("line", exploc.line);
- const enum diagnostics_column_unit orig_unit = context->m_column_unit;
+ const enum diagnostics_column_unit orig_unit = context.m_column_unit;
struct
{
const char *name;
int the_column = INT_MIN;
for (int i = 0; i != ARRAY_SIZE (column_fields); ++i)
{
- context->m_column_unit = column_fields[i].unit;
- const int col = context->converted_column (exploc);
+ context.m_column_unit = column_fields[i].unit;
+ const int col = context.converted_column (exploc);
result->set_integer (column_fields[i].name, col);
if (column_fields[i].unit == orig_unit)
the_column = col;
}
gcc_assert (the_column != INT_MIN);
result->set_integer ("column", the_column);
- context->m_column_unit = orig_unit;
+ context.m_column_unit = orig_unit;
return result;
}
/* Generate a JSON object for LOC_RANGE. */
static json::object *
-json_from_location_range (diagnostic_context *context,
+json_from_location_range (diagnostic_context &context,
const location_range *loc_range, unsigned range_idx)
{
location_t caret_loc = get_pure_location (loc_range->m_loc);
/* Generate a JSON object for HINT. */
static json::object *
-json_from_fixit_hint (diagnostic_context *context, const fixit_hint *hint)
+json_from_fixit_hint (diagnostic_context &context, const fixit_hint *hint)
{
json::object *fixit_obj = new json::object ();
/* Make a JSON value for PATH. */
static json::value *
-make_json_for_path (diagnostic_context *context,
+make_json_for_path (diagnostic_context &context,
const diagnostic_path *path)
{
json::array *path_array = new json::array ();
{
const location_range *loc_range = richloc->get_range (i);
json::object *loc_obj
- = json_from_location_range (&m_context, loc_range, i);
+ = json_from_location_range (m_context, loc_range, i);
if (loc_obj)
loc_array->append (loc_obj);
}
for (unsigned int i = 0; i < richloc->get_num_fixit_hints (); i++)
{
const fixit_hint *hint = richloc->get_fixit_hint (i);
- json::object *fixit_obj = json_from_fixit_hint (&m_context, hint);
+ json::object *fixit_obj = json_from_fixit_hint (m_context, hint);
fixit_array->append (fixit_obj);
}
}
const diagnostic_path *path = richloc->get_path ();
if (path)
{
- json::value *path_value = make_json_for_path (&m_context, path);
+ json::value *path_value = make_json_for_path (m_context, path);
diag_obj->set ("path", path_value);
}
to a file). */
static void
-diagnostic_output_format_init_json (diagnostic_context *context)
+diagnostic_output_format_init_json (diagnostic_context &context)
{
/* Suppress normal textual path output. */
- context->set_path_format (DPF_NONE);
+ context.set_path_format (DPF_NONE);
/* The metadata is handled in JSON format, rather than as text. */
- context->set_show_cwe (false);
- context->set_show_rules (false);
+ context.set_show_cwe (false);
+ context.set_show_rules (false);
/* The option is handled in JSON format, rather than as text. */
- context->set_show_option_requested (false);
+ context.set_show_option_requested (false);
/* Don't colorize the text. */
- pp_show_color (context->printer) = false;
- context->set_show_highlight_colors (false);
+ pp_show_color (context.printer) = false;
+ context.set_show_highlight_colors (false);
}
/* Populate CONTEXT in preparation for JSON output to stderr. */
void
-diagnostic_output_format_init_json_stderr (diagnostic_context *context,
+diagnostic_output_format_init_json_stderr (diagnostic_context &context,
bool formatted)
{
diagnostic_output_format_init_json (context);
- context->set_output_format (new json_stderr_output_format (*context,
- formatted));
+ context.set_output_format (new json_stderr_output_format (context,
+ formatted));
}
/* Populate CONTEXT in preparation for JSON output to a file named
BASE_FILE_NAME.gcc.json. */
void
-diagnostic_output_format_init_json_file (diagnostic_context *context,
+diagnostic_output_format_init_json_file (diagnostic_context &context,
bool formatted,
const char *base_file_name)
{
diagnostic_output_format_init_json (context);
- context->set_output_format (new json_file_output_format (*context,
- formatted,
- base_file_name));
+ context.set_output_format (new json_file_output_format (context,
+ formatted,
+ base_file_name));
}
#if CHECKING_P
test_unknown_location ()
{
test_diagnostic_context dc;
- delete json_from_expanded_location (&dc, UNKNOWN_LOCATION);
+ delete json_from_expanded_location (dc, UNKNOWN_LOCATION);
}
/* Verify that we gracefully handle attempts to serialize bad
loc_range.m_label = NULL;
test_diagnostic_context dc;
- json::object *obj = json_from_location_range (&dc, &loc_range, 0);
+ json::object *obj = json_from_location_range (dc, &loc_range, 0);
/* We should have a "caret" value, but no "start" or "finish" values. */
ASSERT_TRUE (obj != NULL);
ASSERT_TRUE (obj->get ("caret") != NULL);
public:
sarif_invocation ();
- void add_notification_for_ice (diagnostic_context *context,
+ void add_notification_for_ice (diagnostic_context &context,
const diagnostic_info &diagnostic,
- sarif_builder *builder);
- void prepare_to_flush (diagnostic_context *context);
+ sarif_builder &builder);
+ void prepare_to_flush (diagnostic_context &context);
private:
json::array *m_notifications_arr;
sarif_result () : m_related_locations_arr (NULL) {}
void
- on_nested_diagnostic (diagnostic_context *context,
+ on_nested_diagnostic (diagnostic_context &context,
const diagnostic_info &diagnostic,
diagnostic_t orig_diag_kind,
- sarif_builder *builder);
- void on_diagram (diagnostic_context *context,
+ sarif_builder &builder);
+ void on_diagram (diagnostic_context &context,
const diagnostic_diagram &diagram,
- sarif_builder *builder);
+ sarif_builder &builder);
private:
void
class sarif_ice_notification : public sarif_object
{
public:
- sarif_ice_notification (diagnostic_context *context,
+ sarif_ice_notification (diagnostic_context &context,
const diagnostic_info &diagnostic,
- sarif_builder *builder);
+ sarif_builder &builder);
};
/* A class for managing SARIF output (for -fdiagnostics-format=sarif-stderr
class sarif_builder
{
public:
- sarif_builder (diagnostic_context *context,
+ sarif_builder (diagnostic_context &context,
const char *main_input_filename_,
bool formatted);
- void end_diagnostic (diagnostic_context *context,
+ void end_diagnostic (diagnostic_context &context,
const diagnostic_info &diagnostic,
diagnostic_t orig_diag_kind);
- void emit_diagram (diagnostic_context *context,
+ void emit_diagram (diagnostic_context &context,
const diagnostic_diagram &diagram);
void end_group ();
sarif_message *
make_message_object (const char *msg) const;
sarif_message *
- make_message_object_for_diagram (diagnostic_context *context,
+ make_message_object_for_diagram (diagnostic_context &context,
const diagnostic_diagram &diagram);
sarif_artifact_content *
maybe_make_artifact_content_object (const char *filename) const;
private:
sarif_result *
- make_result_object (diagnostic_context *context,
+ make_result_object (diagnostic_context &context,
const diagnostic_info &diagnostic,
diagnostic_t orig_diag_kind);
void
sarif_tool_component_reference *
make_tool_component_reference_object_for_cwe () const;
sarif_reporting_descriptor *
- make_reporting_descriptor_object_for_warning (diagnostic_context *context,
+ make_reporting_descriptor_object_for_warning (diagnostic_context &context,
const diagnostic_info &diagnostic,
diagnostic_t orig_diag_kind,
const char *option_text);
make_artifact_content_object (const char *text) const;
int get_sarif_column (expanded_location exploc) const;
- diagnostic_context *m_context;
+ diagnostic_context &m_context;
/* The JSON object for the invocation object. */
sarif_invocation *m_invocation_obj;
Add an object representing the ICE to the notifications array. */
void
-sarif_invocation::add_notification_for_ice (diagnostic_context *context,
+sarif_invocation::add_notification_for_ice (diagnostic_context &context,
const diagnostic_info &diagnostic,
- sarif_builder *builder)
+ sarif_builder &builder)
{
m_success = false;
}
void
-sarif_invocation::prepare_to_flush (diagnostic_context *context)
+sarif_invocation::prepare_to_flush (diagnostic_context &context)
{
/* "executionSuccessful" property (SARIF v2.1.0 section 3.20.14). */
set_bool ("executionSuccessful", m_success);
/* Call client hook, allowing it to create a custom property bag for
this object (SARIF v2.1.0 section 3.8) e.g. for recording time vars. */
- if (auto client_data_hooks = context->get_client_data_hooks ())
+ if (auto client_data_hooks = context.get_client_data_hooks ())
client_data_hooks->add_sarif_invocation_properties (*this);
}
secondary diagnostics occur (such as notes to a warning). */
void
-sarif_result::on_nested_diagnostic (diagnostic_context *context,
+sarif_result::on_nested_diagnostic (diagnostic_context &context,
const diagnostic_info &diagnostic,
diagnostic_t /*orig_diag_kind*/,
- sarif_builder *builder)
+ sarif_builder &builder)
{
/* We don't yet generate meaningful logical locations for notes;
sometimes these will related to current_function_decl, but
often they won't. */
sarif_location *location_obj
- = builder->make_location_object (*diagnostic.richloc, NULL,
- diagnostic_artifact_role::result_file);
+ = builder.make_location_object (*diagnostic.richloc, NULL,
+ diagnostic_artifact_role::result_file);
sarif_message *message_obj
- = builder->make_message_object (pp_formatted_text (context->printer));
- pp_clear_output_area (context->printer);
+ = builder.make_message_object (pp_formatted_text (context.printer));
+ pp_clear_output_area (context.printer);
location_obj->set ("message", message_obj);
add_related_location (location_obj);
(SARIF v2.1.0 section 3.28.5). */
void
-sarif_result::on_diagram (diagnostic_context *context,
+sarif_result::on_diagram (diagnostic_context &context,
const diagnostic_diagram &diagram,
- sarif_builder *builder)
+ sarif_builder &builder)
{
sarif_location *location_obj = new sarif_location ();
sarif_message *message_obj
- = builder->make_message_object_for_diagram (context, diagram);
+ = builder.make_message_object_for_diagram (context, diagram);
location_obj->set ("message", message_obj);
add_related_location (location_obj);
/* sarif_ice_notification's ctor.
DIAGNOSTIC is an internal compiler error. */
-sarif_ice_notification::sarif_ice_notification (diagnostic_context *context,
+sarif_ice_notification::sarif_ice_notification (diagnostic_context &context,
const diagnostic_info &diagnostic,
- sarif_builder *builder)
+ sarif_builder &builder)
{
/* "locations" property (SARIF v2.1.0 section 3.58.4). */
json::array *locations_arr
- = builder->make_locations_arr (diagnostic,
- diagnostic_artifact_role::result_file);
+ = builder.make_locations_arr (diagnostic,
+ diagnostic_artifact_role::result_file);
set ("locations", locations_arr);
/* "message" property (SARIF v2.1.0 section 3.85.5). */
sarif_message *message_obj
- = builder->make_message_object (pp_formatted_text (context->printer));
- pp_clear_output_area (context->printer);
+ = builder.make_message_object (pp_formatted_text (context.printer));
+ pp_clear_output_area (context.printer);
set ("message", message_obj);
/* "level" property (SARIF v2.1.0 section 3.58.6). */
/* sarif_builder's ctor. */
-sarif_builder::sarif_builder (diagnostic_context *context,
+sarif_builder::sarif_builder (diagnostic_context &context,
const char *main_input_filename_,
bool formatted)
: m_context (context),
m_seen_any_relative_paths (false),
m_rule_id_set (),
m_rules_arr (new json::array ()),
- m_tabstop (context->m_tabstop),
+ m_tabstop (context.m_tabstop),
m_formatted (formatted)
{
/* Mark MAIN_INPUT_FILENAME_ as the artifact that the tool was
/* Implementation of "end_diagnostic" for SARIF output. */
void
-sarif_builder::end_diagnostic (diagnostic_context *context,
+sarif_builder::end_diagnostic (diagnostic_context &context,
const diagnostic_info &diagnostic,
diagnostic_t orig_diag_kind)
{
if (diagnostic.kind == DK_ICE || diagnostic.kind == DK_ICE_NOBT)
{
- m_invocation_obj->add_notification_for_ice (context, diagnostic, this);
+ m_invocation_obj->add_notification_for_ice (context, diagnostic, *this);
return;
}
m_cur_group_result->on_nested_diagnostic (context,
diagnostic,
orig_diag_kind,
- this);
+ *this);
else
{
/* Top-level diagnostic. */
for SARIF output. */
void
-sarif_builder::emit_diagram (diagnostic_context *context,
+sarif_builder::emit_diagram (diagnostic_context &context,
const diagnostic_diagram &diagram)
{
/* We must be within the emission of a top-level diagnostic. */
gcc_assert (m_cur_group_result);
- m_cur_group_result->on_diagram (context, diagram, this);
+ m_cur_group_result->on_diagram (context, diagram, *this);
}
/* Implementation of "end_group_cb" for SARIF output. */
/* Make a "result" object (SARIF v2.1.0 section 3.27) for DIAGNOSTIC. */
sarif_result *
-sarif_builder::make_result_object (diagnostic_context *context,
+sarif_builder::make_result_object (diagnostic_context &context,
const diagnostic_info &diagnostic,
diagnostic_t orig_diag_kind)
{
/* "ruleId" property (SARIF v2.1.0 section 3.27.5). */
/* Ideally we'd have an option_name for these. */
if (char *option_text
- = context->make_option_name (diagnostic.option_index,
- orig_diag_kind, diagnostic.kind))
+ = context.make_option_name (diagnostic.option_index,
+ orig_diag_kind, diagnostic.kind))
{
/* Lazily create reportingDescriptor objects for and add to m_rules_arr.
Set ruleId referencing them. */
/* "message" property (SARIF v2.1.0 section 3.27.11). */
sarif_message *message_obj
- = make_message_object (pp_formatted_text (context->printer));
- pp_clear_output_area (context->printer);
+ = make_message_object (pp_formatted_text (context.printer));
+ pp_clear_output_area (context.printer);
result_obj->set ("message", message_obj);
/* "locations" property (SARIF v2.1.0 section 3.27.12). */
sarif_reporting_descriptor *
sarif_builder::
-make_reporting_descriptor_object_for_warning (diagnostic_context *context,
+make_reporting_descriptor_object_for_warning (diagnostic_context &context,
const diagnostic_info &diagnostic,
diagnostic_t /*orig_diag_kind*/,
const char *option_text)
it seems redundant compared to "id". */
/* "helpUri" property (SARIF v2.1.0 section 3.49.12). */
- if (char *option_url = context->make_option_url (diagnostic.option_index))
+ if (char *option_url = context.make_option_url (diagnostic.option_index))
{
reporting_desc->set_string ("helpUri", option_url);
free (option_url);
{
json::array *locations_arr = new json::array ();
const logical_location *logical_loc = NULL;
- if (auto client_data_hooks = m_context->get_client_data_hooks ())
+ if (auto client_data_hooks = m_context.get_client_data_hooks ())
logical_loc = client_data_hooks->get_current_logical_location ();
sarif_location *location_obj
sarif_builder::get_sarif_column (expanded_location exploc) const
{
cpp_char_column_policy policy (m_tabstop, cpp_wcwidth);
- return location_compute_display_column (m_context->get_file_cache (),
+ return location_compute_display_column (m_context.get_file_cache (),
exploc, policy);
}
of the message. */
sarif_message *
-sarif_builder::make_message_object_for_diagram (diagnostic_context *context,
+sarif_builder::make_message_object_for_diagram (diagnostic_context &context,
const diagnostic_diagram &diagram)
{
sarif_message *message_obj = new sarif_message ();
/* "text" property (SARIF v2.1.0 section 3.11.8). */
message_obj->set_string ("text", diagram.get_alt_text ());
- char *saved_prefix = pp_take_prefix (context->printer);
- pp_set_prefix (context->printer, NULL);
+ char *saved_prefix = pp_take_prefix (context.printer);
+ pp_set_prefix (context.printer, NULL);
/* "To produce a code block in Markdown, simply indent every line of
the block by at least 4 spaces or 1 tab."
Here we use 4 spaces. */
- diagram.get_canvas ().print_to_pp (context->printer, " ");
- pp_set_prefix (context->printer, saved_prefix);
+ diagram.get_canvas ().print_to_pp (context.printer, " ");
+ pp_set_prefix (context.printer, saved_prefix);
/* "markdown" property (SARIF v2.1.0 section 3.11.9). */
- message_obj->set_string ("markdown", pp_formatted_text (context->printer));
+ message_obj->set_string ("markdown", pp_formatted_text (context.printer));
- pp_clear_output_area (context->printer);
+ pp_clear_output_area (context.printer);
return message_obj;
}
/* Report plugins via the "extensions" property
(SARIF v2.1.0 section 3.18.3). */
- if (auto client_data_hooks = m_context->get_client_data_hooks ())
+ if (auto client_data_hooks = m_context.get_client_data_hooks ())
if (const client_version_info *vinfo
= client_data_hooks->get_any_version_info ())
{
{
sarif_tool_component *driver_obj = new sarif_tool_component ();
- if (auto client_data_hooks = m_context->get_client_data_hooks ())
+ if (auto client_data_hooks = m_context.get_client_data_hooks ())
if (const client_version_info *vinfo
= client_data_hooks->get_any_version_info ())
{
case diagnostic_artifact_role::result_file:
case diagnostic_artifact_role::traced_file:
/* Assume that these are in the source language. */
- if (auto client_data_hooks = m_context->get_client_data_hooks ())
+ if (auto client_data_hooks = m_context.get_client_data_hooks ())
if (const char *source_lang
= client_data_hooks->maybe_get_sarif_source_language (filename))
artifact_obj->set_string ("sourceLanguage", source_lang);
{
/* Let input.cc handle any charset conversion. */
char_span utf8_content
- = m_context->get_file_cache ().get_source_file_content (filename);
+ = m_context.get_file_cache ().get_source_file_content (filename);
if (!utf8_content)
return NULL;
for (int line = start_line; line <= end_line; line++)
{
char_span line_content
- = m_context->get_file_cache ().get_source_line (filename, line);
+ = m_context.get_file_cache ().get_source_line (filename, line);
if (!line_content.get_buffer ())
return NULL;
result.reserve (line_content.length () + 1);
on_end_diagnostic (const diagnostic_info &diagnostic,
diagnostic_t orig_diag_kind) final override
{
- m_builder.end_diagnostic (&m_context, diagnostic, orig_diag_kind);
+ m_builder.end_diagnostic (m_context, diagnostic, orig_diag_kind);
}
void on_diagram (const diagnostic_diagram &diagram) final override
{
- m_builder.emit_diagram (&m_context, diagram);
+ m_builder.emit_diagram (m_context, diagram);
}
protected:
const char *main_input_filename_,
bool formatted)
: diagnostic_output_format (context),
- m_builder (&context, main_input_filename_, formatted)
+ m_builder (context, main_input_filename_, formatted)
{}
sarif_builder m_builder;
to a file). */
static void
-diagnostic_output_format_init_sarif (diagnostic_context *context)
+diagnostic_output_format_init_sarif (diagnostic_context &context)
{
/* Suppress normal textual path output. */
- context->set_path_format (DPF_NONE);
+ context.set_path_format (DPF_NONE);
/* Override callbacks. */
- context->set_ice_handler_callback (sarif_ice_handler);
+ context.set_ice_handler_callback (sarif_ice_handler);
/* The metadata is handled in SARIF format, rather than as text. */
- context->set_show_cwe (false);
- context->set_show_rules (false);
+ context.set_show_cwe (false);
+ context.set_show_rules (false);
/* The option is handled in SARIF format, rather than as text. */
- context->set_show_option_requested (false);
+ context.set_show_option_requested (false);
/* Don't colorize the text. */
- pp_show_color (context->printer) = false;
- context->set_show_highlight_colors (false);
+ pp_show_color (context.printer) = false;
+ context.set_show_highlight_colors (false);
}
/* Populate CONTEXT in preparation for SARIF output to stderr. */
void
-diagnostic_output_format_init_sarif_stderr (diagnostic_context *context,
+diagnostic_output_format_init_sarif_stderr (diagnostic_context &context,
const char *main_input_filename_,
bool formatted)
{
diagnostic_output_format_init_sarif (context);
- context->set_output_format
- (new sarif_stream_output_format (*context,
+ context.set_output_format
+ (new sarif_stream_output_format (context,
main_input_filename_,
formatted,
stderr));
BASE_FILE_NAME.sarif. */
void
-diagnostic_output_format_init_sarif_file (diagnostic_context *context,
+diagnostic_output_format_init_sarif_file (diagnostic_context &context,
const char *main_input_filename_,
bool formatted,
const char *base_file_name)
{
diagnostic_output_format_init_sarif (context);
- context->set_output_format
- (new sarif_file_output_format (*context,
+ context.set_output_format
+ (new sarif_file_output_format (context,
main_input_filename_,
formatted,
base_file_name));
/* Populate CONTEXT in preparation for SARIF output to STREAM. */
void
-diagnostic_output_format_init_sarif_stream (diagnostic_context *context,
+diagnostic_output_format_init_sarif_stream (diagnostic_context &context,
const char *main_input_filename_,
bool formatted,
FILE *stream)
{
diagnostic_output_format_init_sarif (context);
- context->set_output_format
- (new sarif_stream_output_format (*context,
+ context.set_output_format
+ (new sarif_stream_output_format (context,
main_input_filename_,
formatted,
stream));