diff options
Diffstat (limited to 'gcc/diagnostic-format-sarif.cc')
-rw-r--r-- | gcc/diagnostic-format-sarif.cc | 320 |
1 files changed, 168 insertions, 152 deletions
diff --git a/gcc/diagnostic-format-sarif.cc b/gcc/diagnostic-format-sarif.cc index 8dbc91e..f322991 100644 --- a/gcc/diagnostic-format-sarif.cc +++ b/gcc/diagnostic-format-sarif.cc @@ -41,7 +41,6 @@ along with GCC; see the file COPYING3. If not see #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" @@ -158,7 +157,7 @@ make_date_time_string_for_current_time () "%02i:%02i:%02iZ"), tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_sec); - return ::make_unique<json::string> (buf); + return std::make_unique<json::string> (buf); } /* Subclass of sarif_object for SARIF "invocation" objects @@ -689,7 +688,7 @@ public: const line_maps *line_maps, const char *main_input_filename_, bool formatted, - enum sarif_version version); + const sarif_generation_options &sarif_gen_opts); ~sarif_builder (); void set_printer (pretty_printer &printer) @@ -743,7 +742,7 @@ public: diagnostic_context &get_context () const { return m_context; } pretty_printer *get_printer () const { return m_printer; } token_printer &get_token_printer () { return m_token_printer; } - enum sarif_version get_version () const { return m_version; } + enum sarif_version get_version () const { return m_sarif_gen_opts.m_version; } size_t num_results () const { return m_results_array->size (); } sarif_result &get_result (size_t idx) @@ -753,6 +752,8 @@ public: return *static_cast<sarif_result *> (element); } + const sarif_generation_options &get_opts () const { return m_sarif_gen_opts; } + private: class sarif_token_printer : public token_printer { @@ -865,7 +866,6 @@ private: pretty_printer *m_printer; const line_maps *m_line_maps; sarif_token_printer m_token_printer; - enum sarif_version m_version; /* The JSON object for the invocation object. */ std::unique_ptr<sarif_invocation> m_invocation_obj; @@ -892,6 +892,7 @@ private: int m_tabstop; bool m_formatted; + const sarif_generation_options m_sarif_gen_opts; unsigned m_next_result_idx; sarif_code_flow *m_current_code_flow; @@ -918,13 +919,13 @@ sarif_object::get_or_create_properties () sarif_invocation::sarif_invocation (sarif_builder &builder, const char * const *original_argv) -: m_notifications_arr (::make_unique<json::array> ()), +: m_notifications_arr (std::make_unique<json::array> ()), m_success (true) { // "arguments" property (SARIF v2.1.0 section 3.20.2) if (original_argv) { - auto arguments_arr = ::make_unique<json::array> (); + auto arguments_arr = std::make_unique<json::array> (); for (size_t i = 0; original_argv[i]; ++i) arguments_arr->append_string (original_argv[i]); set<json::array> ("arguments", std::move (arguments_arr)); @@ -951,9 +952,9 @@ sarif_invocation::add_notification_for_ice (const diagnostic_info &diagnostic, m_success = false; auto notification - = ::make_unique<sarif_ice_notification> (diagnostic, - builder, - std::move (backtrace)); + = std::make_unique<sarif_ice_notification> (diagnostic, + builder, + std::move (backtrace)); /* Support for related locations within a notification was added in SARIF 2.2; see https://github.com/oasis-tcs/sarif-spec/issues/540 */ @@ -1068,7 +1069,7 @@ sarif_artifact::populate_roles () { if (bitmap_empty_p (m_roles)) return; - auto roles_arr (::make_unique<json::array> ()); + auto roles_arr (std::make_unique<json::array> ()); for (int i = 0; i < (int)diagnostic_artifact_role::NUM_ROLES; i++) if (bitmap_bit_p (m_roles, i)) { @@ -1245,7 +1246,7 @@ void sarif_result::on_diagram (const diagnostic_diagram &diagram, sarif_builder &builder) { - auto location_obj = ::make_unique<sarif_location> (); + auto location_obj = std::make_unique<sarif_location> (); auto message_obj = builder.make_message_object_for_diagram (diagram); location_obj->set<sarif_message> ("message", std::move (message_obj)); @@ -1344,7 +1345,7 @@ sarif_location::lazily_add_relationship_object (sarif_location &target, /* No existing locationRelationship from THIS to TARGET; make one, record it, and add it to the "relationships" array. */ auto relationship_obj - = ::make_unique<sarif_location_relationship> (target, loc_mgr); + = std::make_unique<sarif_location_relationship> (target, loc_mgr); sarif_location_relationship *relationship = relationship_obj.get (); auto kv = std::pair<sarif_location *, @@ -1472,7 +1473,7 @@ sarif_code_flow::sarif_code_flow (sarif_result &parent, m_idx_within_parent (idx_within_parent) { /* "threadFlows" property (SARIF v2.1.0 section 3.36.3). */ - auto thread_flows_arr = ::make_unique<json::array> (); + auto thread_flows_arr = std::make_unique<json::array> (); m_thread_flows_arr = thread_flows_arr.get (); // borrowed set<json::array> ("threadFlows", std::move (thread_flows_arr)); } @@ -1487,7 +1488,7 @@ sarif_code_flow::get_or_append_thread_flow (const diagnostic_thread &thread, unsigned next_thread_flow_idx = m_thread_flows_arr->size (); auto thread_flow_obj - = ::make_unique<sarif_thread_flow> (*this, thread, next_thread_flow_idx); + = std::make_unique<sarif_thread_flow> (*this, thread, next_thread_flow_idx); m_thread_id_map.put (thread_id, thread_flow_obj.get ()); // borrowed sarif_thread_flow *result = thread_flow_obj.get (); m_thread_flows_arr->append<sarif_thread_flow> (std::move (thread_flow_obj)); @@ -1561,15 +1562,14 @@ sarif_builder::sarif_builder (diagnostic_context &context, const line_maps *line_maps, const char *main_input_filename_, bool formatted, - enum sarif_version version) + const sarif_generation_options &sarif_gen_opts) : m_context (context), m_printer (&printer), m_line_maps (line_maps), m_token_printer (*this), - m_version (version), m_invocation_obj - (::make_unique<sarif_invocation> (*this, - context.get_original_argv ())), + (std::make_unique<sarif_invocation> (*this, + context.get_original_argv ())), m_results_array (new json::array ()), m_cur_group_result (nullptr), m_seen_any_relative_paths (false), @@ -1577,6 +1577,7 @@ sarif_builder::sarif_builder (diagnostic_context &context, m_rules_arr (new json::array ()), m_tabstop (context.m_tabstop), m_formatted (formatted), + m_sarif_gen_opts (sarif_gen_opts), m_next_result_idx (0), m_current_code_flow (nullptr) { @@ -1685,7 +1686,7 @@ bt_callback (void *data, uintptr_t pc, const char *filename, int lineno, } } - auto frame_obj = ::make_unique<json::object> (); + auto frame_obj = std::make_unique<json::object> (); /* I tried using sarifStack and sarifStackFrame for this but it's not a good fit e.g. PC information. */ @@ -1711,7 +1712,7 @@ bt_callback (void *data, uintptr_t pc, const char *filename, int lineno, std::unique_ptr<json::object> sarif_builder::make_stack_from_backtrace () { - auto frames_arr = ::make_unique<json::array> (); + auto frames_arr = std::make_unique<json::array> (); backtrace_state *state = nullptr; state = backtrace_create_state (nullptr, 0, nullptr, nullptr); @@ -1724,7 +1725,7 @@ sarif_builder::make_stack_from_backtrace () if (frames_arr->size () == 0) return nullptr; - auto stack = ::make_unique<json::object> (); + auto stack = std::make_unique<json::object> (); stack->set ("frames", std::move (frames_arr)); return stack; } @@ -1873,7 +1874,7 @@ sarif_builder::make_result_object (const diagnostic_info &diagnostic, diagnostic_t orig_diag_kind, unsigned idx_within_parent) { - auto result_obj = ::make_unique<sarif_result> (idx_within_parent); + auto result_obj = std::make_unique<sarif_result> (idx_within_parent); /* "ruleId" property (SARIF v2.1.0 section 3.27.5). */ /* Ideally we'd have an option_name for these. */ @@ -1914,7 +1915,7 @@ sarif_builder::make_result_object (const diagnostic_info &diagnostic, /* "taxa" property (SARIF v2.1.0 section 3.27.8). */ if (int cwe_id = diagnostic.metadata->get_cwe ()) { - auto taxa_arr = ::make_unique<json::array> (); + auto taxa_arr = std::make_unique<json::array> (); taxa_arr->append<sarif_reporting_descriptor_reference> (make_reporting_descriptor_reference_object_for_cwe_id (cwe_id)); result_obj->set<json::array> ("taxa", std::move (taxa_arr)); @@ -1945,7 +1946,7 @@ sarif_builder::make_result_object (const diagnostic_info &diagnostic, /* "codeFlows" property (SARIF v2.1.0 section 3.27.18). */ if (const diagnostic_path *path = diagnostic.richloc->get_path ()) { - auto code_flows_arr = ::make_unique<json::array> (); + auto code_flows_arr = std::make_unique<json::array> (); const unsigned code_flow_index = 0; code_flows_arr->append<sarif_code_flow> (make_code_flow_object (*result_obj.get (), @@ -1962,7 +1963,7 @@ sarif_builder::make_result_object (const diagnostic_info &diagnostic, const rich_location *richloc = diagnostic.richloc; if (richloc->get_num_fixit_hints ()) { - auto fix_arr = ::make_unique<json::array> (); + auto fix_arr = std::make_unique<json::array> (); fix_arr->append<sarif_fix> (make_fix_object (*richloc)); result_obj->set<json::array> ("fixes", std::move (fix_arr)); } @@ -1979,7 +1980,7 @@ make_reporting_descriptor_object_for_warning (const diagnostic_info &diagnostic, diagnostic_t /*orig_diag_kind*/, const char *option_text) { - auto reporting_desc = ::make_unique<sarif_reporting_descriptor> (); + auto reporting_desc = std::make_unique<sarif_reporting_descriptor> (); /* "id" property (SARIF v2.1.0 section 3.49.3). */ reporting_desc->set_string ("id", option_text); @@ -2003,7 +2004,7 @@ make_reporting_descriptor_object_for_warning (const diagnostic_info &diagnostic, std::unique_ptr<sarif_reporting_descriptor> sarif_builder::make_reporting_descriptor_object_for_cwe_id (int cwe_id) const { - auto reporting_desc = ::make_unique<sarif_reporting_descriptor> (); + auto reporting_desc = std::make_unique<sarif_reporting_descriptor> (); /* "id" property (SARIF v2.1.0 section 3.49.3). */ { @@ -2030,7 +2031,7 @@ std::unique_ptr<sarif_reporting_descriptor_reference> sarif_builder:: make_reporting_descriptor_reference_object_for_cwe_id (int cwe_id) { - auto desc_ref_obj = ::make_unique<sarif_reporting_descriptor_reference> (); + auto desc_ref_obj = std::make_unique<sarif_reporting_descriptor_reference> (); /* "id" property (SARIF v2.1.0 section 3.52.4). */ { @@ -2057,7 +2058,7 @@ std::unique_ptr<sarif_tool_component_reference> sarif_builder:: make_tool_component_reference_object_for_cwe () const { - auto comp_ref_obj = ::make_unique<sarif_tool_component_reference> (); + auto comp_ref_obj = std::make_unique<sarif_tool_component_reference> (); /* "name" property (SARIF v2.1.0 section 3.54.3). */ comp_ref_obj->set_string ("name", "cwe"); @@ -2075,7 +2076,7 @@ sarif_builder::make_locations_arr (sarif_location_manager &loc_mgr, const diagnostic_info &diagnostic, enum diagnostic_artifact_role role) { - auto locations_arr = ::make_unique<json::array> (); + auto locations_arr = std::make_unique<json::array> (); const logical_location *logical_loc = nullptr; if (auto client_data_hooks = m_context.get_client_data_hooks ()) logical_loc = client_data_hooks->get_current_logical_location (); @@ -2099,7 +2100,7 @@ set_any_logical_locs_arr (sarif_location &location_obj, { if (!logical_loc) return; - auto location_locs_arr = ::make_unique<json::array> (); + auto location_locs_arr = std::make_unique<json::array> (); location_locs_arr->append<sarif_logical_location> (make_sarif_logical_location_object (*logical_loc)); location_obj.set<json::array> ("logicalLocations", @@ -2159,7 +2160,7 @@ sarif_builder::make_location_object (sarif_location_manager &loc_mgr, } the_renderer (rich_loc, m_context.get_escape_format ()); - auto location_obj = ::make_unique<sarif_location> (); + auto location_obj = std::make_unique<sarif_location> (); /* Get primary loc from RICH_LOC. */ location_t loc = rich_loc.get_loc (); @@ -2197,7 +2198,7 @@ sarif_builder::make_location_object (sarif_location_manager &loc_mgr, if (region) { if (!annotations_arr) - annotations_arr = ::make_unique<json::array> (); + annotations_arr = std::make_unique<json::array> (); region->set<sarif_message> ("message", make_message_object (text.get ())); annotations_arr->append<sarif_region> (std::move (region)); @@ -2277,7 +2278,7 @@ sarif_builder::make_location_object (sarif_location_manager &loc_mgr, location_t loc, enum diagnostic_artifact_role role) { - auto location_obj = ::make_unique<sarif_location> (); + auto location_obj = std::make_unique<sarif_location> (); /* "physicalLocation" property (SARIF v2.1.0 section 3.28.3). */ if (auto phs_loc_obj @@ -2298,7 +2299,7 @@ sarif_builder::make_location_object (sarif_location_manager &loc_mgr, const diagnostic_event &event, enum diagnostic_artifact_role role) { - auto location_obj = ::make_unique<sarif_location> (); + auto location_obj = std::make_unique<sarif_location> (); /* "physicalLocation" property (SARIF v2.1.0 section 3.28.3). */ location_t loc = event.get_location (); @@ -2342,7 +2343,7 @@ maybe_make_physical_location_object (location_t loc, if (loc <= BUILTINS_LOCATION || LOCATION_FILE (loc) == nullptr) return nullptr; - auto phys_loc_obj = ::make_unique<sarif_physical_location> (); + auto phys_loc_obj = std::make_unique<sarif_physical_location> (); /* "artifactLocation" property (SARIF v2.1.0 section 3.29.3). */ phys_loc_obj->set<sarif_artifact_location> @@ -2386,7 +2387,7 @@ sarif_builder::make_artifact_location_object (location_t loc) std::unique_ptr<sarif_artifact_location> sarif_builder::make_artifact_location_object (const char *filename) { - auto artifact_loc_obj = ::make_unique<sarif_artifact_location> (); + auto artifact_loc_obj = std::make_unique<sarif_artifact_location> (); /* "uri" property (SARIF v2.1.0 section 3.4.3). */ artifact_loc_obj->set_string ("uri", filename); @@ -2432,7 +2433,7 @@ make_pwd_uri_str () std::unique_ptr<sarif_artifact_location> sarif_builder::make_artifact_location_object_for_pwd () const { - auto artifact_loc_obj = ::make_unique<sarif_artifact_location> (); + auto artifact_loc_obj = std::make_unique<sarif_artifact_location> (); /* "uri" property (SARIF v2.1.0 section 3.4.3). */ if (char *pwd = make_pwd_uri_str ()) @@ -2492,7 +2493,7 @@ sarif_builder::maybe_make_region_object (location_t loc, if (exploc_start.line <= 0) return nullptr; - auto region_obj = ::make_unique<sarif_region> (); + auto region_obj = std::make_unique<sarif_region> (); /* "startLine" property (SARIF v2.1.0 section 3.30.5) */ region_obj->set_integer ("startLine", exploc_start.line); @@ -2569,7 +2570,7 @@ maybe_make_region_object_for_context (location_t loc, if (exploc_start.line <= 0) return nullptr; - auto region_obj = ::make_unique<sarif_region> (); + auto region_obj = std::make_unique<sarif_region> (); /* "startLine" property (SARIF v2.1.0 section 3.30.5) */ region_obj->set_integer ("startLine", exploc_start.line); @@ -2603,7 +2604,7 @@ sarif_builder::make_region_object_for_hint (const fixit_hint &hint) const expanded_location exploc_start = expand_location (start_loc); expanded_location exploc_next = expand_location (next_loc); - auto region_obj = ::make_unique<sarif_region> (); + auto region_obj = std::make_unique<sarif_region> (); /* "startLine" property (SARIF v2.1.0 section 3.30.5) */ region_obj->set_integer ("startLine", exploc_start.line); @@ -2663,7 +2664,7 @@ maybe_get_sarif_kind (enum logical_location_kind kind) std::unique_ptr<sarif_logical_location> make_sarif_logical_location_object (const logical_location &logical_loc) { - auto logical_loc_obj = ::make_unique<sarif_logical_location> (); + auto logical_loc_obj = std::make_unique<sarif_logical_location> (); /* "name" property (SARIF v2.1.0 section 3.33.4). */ if (const char *short_name = logical_loc.get_short_name ()) @@ -2724,7 +2725,7 @@ sarif_builder::make_code_flow_object (sarif_result &result, const diagnostic_path &path) { auto code_flow_obj - = ::make_unique <sarif_code_flow> (result, idx_within_parent); + = std::make_unique <sarif_code_flow> (result, idx_within_parent); /* First pass: Create threadFlows and threadFlowLocation objects within them, @@ -2809,7 +2810,7 @@ sarif_builder::maybe_make_kinds_array (diagnostic_event::meaning m) const && m.m_property == diagnostic_event::PROPERTY_unknown) return nullptr; - auto kinds_arr = ::make_unique<json::array> (); + auto kinds_arr = std::make_unique<json::array> (); if (const char *verb_str = diagnostic_event::meaning::maybe_get_verb_str (m.m_verb)) kinds_arr->append_string (verb_str); @@ -2859,7 +2860,7 @@ set_string_property_escaping_braces (json::object &obj, std::unique_ptr<sarif_message> sarif_builder::make_message_object (const char *msg) const { - auto message_obj = ::make_unique<sarif_message> (); + auto message_obj = std::make_unique<sarif_message> (); /* "text" property (SARIF v2.1.0 section 3.11.8). */ set_string_property_escaping_braces (*message_obj, @@ -2875,7 +2876,7 @@ sarif_builder::make_message_object (const char *msg) const std::unique_ptr<sarif_message> sarif_builder::make_message_object_for_diagram (const diagnostic_diagram &diagram) { - auto message_obj = ::make_unique<sarif_message> (); + auto message_obj = std::make_unique<sarif_message> (); /* "text" property (SARIF v2.1.0 section 3.11.8). */ set_string_property_escaping_braces (*message_obj, @@ -2906,7 +2907,7 @@ sarif_builder::make_message_object_for_diagram (const diagnostic_diagram &diagra std::unique_ptr<sarif_multiformat_message_string> sarif_builder::make_multiformat_message_string (const char *msg) const { - auto message_obj = ::make_unique<sarif_multiformat_message_string> (); + auto message_obj = std::make_unique<sarif_multiformat_message_string> (); /* "text" property (SARIF v2.1.0 section 3.12.3). */ set_string_property_escaping_braces (*message_obj, @@ -2958,16 +2959,16 @@ sarif_builder:: make_top_level_object (std::unique_ptr<sarif_invocation> invocation_obj, std::unique_ptr<json::array> results) { - auto log_obj = ::make_unique<sarif_log> (); + auto log_obj = std::make_unique<sarif_log> (); /* "$schema" property (SARIF v2.1.0 section 3.13.3) . */ - log_obj->set_string ("$schema", sarif_version_to_url (m_version)); + log_obj->set_string ("$schema", sarif_version_to_url (get_version ())); /* "version" property (SARIF v2.1.0 section 3.13.2). */ - log_obj->set_string ("version", sarif_version_to_property (m_version)); + log_obj->set_string ("version", sarif_version_to_property (get_version ())); /* "runs" property (SARIF v2.1.0 section 3.13.4). */ - auto run_arr = ::make_unique<json::array> (); + auto run_arr = std::make_unique<json::array> (); auto run_obj = make_run_object (std::move (invocation_obj), std::move (results)); run_arr->append<sarif_run> (std::move (run_obj)); @@ -2983,7 +2984,7 @@ sarif_builder:: make_run_object (std::unique_ptr<sarif_invocation> invocation_obj, std::unique_ptr<json::array> results) { - auto run_obj = ::make_unique<sarif_run> (); + auto run_obj = std::make_unique<sarif_run> (); /* "tool" property (SARIF v2.1.0 section 3.14.6). */ run_obj->set<sarif_tool> ("tool", make_tool_object ()); @@ -2994,7 +2995,7 @@ make_run_object (std::unique_ptr<sarif_invocation> invocation_obj, /* "invocations" property (SARIF v2.1.0 section 3.14.11). */ { - auto invocations_arr = ::make_unique<json::array> (); + auto invocations_arr = std::make_unique<json::array> (); invocations_arr->append (std::move (invocation_obj)); run_obj->set<json::array> ("invocations", std::move (invocations_arr)); } @@ -3002,7 +3003,7 @@ make_run_object (std::unique_ptr<sarif_invocation> invocation_obj, /* "originalUriBaseIds (SARIF v2.1.0 section 3.14.14). */ if (m_seen_any_relative_paths) { - auto orig_uri_base_ids = ::make_unique<json::object> (); + auto orig_uri_base_ids = std::make_unique<json::object> (); orig_uri_base_ids->set<sarif_artifact_location> (PWD_PROPERTY_NAME, make_artifact_location_object_for_pwd ()); run_obj->set<json::object> ("originalUriBaseIds", @@ -3010,7 +3011,7 @@ make_run_object (std::unique_ptr<sarif_invocation> invocation_obj, } /* "artifacts" property (SARIF v2.1.0 section 3.14.15). */ - auto artifacts_arr = ::make_unique<json::array> (); + auto artifacts_arr = std::make_unique<json::array> (); for (auto iter : m_filename_to_artifact_map) { sarif_artifact *artifact_obj = iter.second; @@ -3033,7 +3034,7 @@ make_run_object (std::unique_ptr<sarif_invocation> invocation_obj, std::unique_ptr<sarif_tool> sarif_builder::make_tool_object () { - auto tool_obj = ::make_unique<sarif_tool> (); + auto tool_obj = std::make_unique<sarif_tool> (); /* "driver" property (SARIF v2.1.0 section 3.18.2). */ tool_obj->set<sarif_tool_component> ("driver", @@ -3052,7 +3053,7 @@ sarif_builder::make_tool_object () { /* Create a "toolComponent" object (SARIF v2.1.0 section 3.19) for the plugin. */ - auto plugin_obj = ::make_unique<sarif_tool_component> (); + auto plugin_obj = std::make_unique<sarif_tool_component> (); /* "name" property (SARIF v2.1.0 section 3.19.8). */ if (const char *short_name = p.get_short_name ()) @@ -3074,7 +3075,7 @@ sarif_builder::make_tool_object () vinfo->for_each_plugin (v); if (v.m_plugin_objs.size () > 0) { - auto extensions_arr = ::make_unique<json::array> (); + auto extensions_arr = std::make_unique<json::array> (); for (auto &iter : v.m_plugin_objs) extensions_arr->append<sarif_tool_component> (std::move (iter)); tool_obj->set<json::array> ("extensions", @@ -3094,7 +3095,7 @@ sarif_builder::make_tool_object () std::unique_ptr<sarif_tool_component> sarif_builder::make_driver_tool_component_object () { - auto driver_obj = ::make_unique<sarif_tool_component> (); + auto driver_obj = std::make_unique<sarif_tool_component> (); if (auto client_data_hooks = m_context.get_client_data_hooks ()) if (const client_version_info *vinfo @@ -3143,7 +3144,7 @@ sarif_builder::maybe_make_taxonomies_array () const return nullptr; /* "taxonomies" property (SARIF v2.1.0 section 3.14.8). */ - auto taxonomies_arr = ::make_unique<json::array> (); + auto taxonomies_arr = std::make_unique<json::array> (); taxonomies_arr->append<sarif_tool_component> (std::move (cwe_obj)); return taxonomies_arr; } @@ -3160,7 +3161,7 @@ sarif_builder::maybe_make_cwe_taxonomy_object () const if (m_cwe_id_set.is_empty ()) return nullptr; - auto taxonomy_obj = ::make_unique<sarif_tool_component> (); + auto taxonomy_obj = std::make_unique<sarif_tool_component> (); /* "name" property (SARIF v2.1.0 section 3.19.8). */ taxonomy_obj->set_string ("name", "CWE"); @@ -3178,7 +3179,7 @@ sarif_builder::maybe_make_cwe_taxonomy_object () const " Common Weakness Enumeration")); /* "taxa" property (SARIF v2.1.0 3.section 3.19.25). */ - auto taxa_arr = ::make_unique<json::array> (); + auto taxa_arr = std::make_unique<json::array> (); for (auto cwe_id : m_cwe_id_set) taxa_arr->append<sarif_reporting_descriptor> (make_reporting_descriptor_object_for_cwe_id (cwe_id)); @@ -3252,10 +3253,10 @@ sarif_builder::maybe_make_artifact_content_object (const char *filename) const if (!cpp_valid_utf8_p(utf8_content.get_buffer (), utf8_content.length ())) return nullptr; - auto artifact_content_obj = ::make_unique<sarif_artifact_content> (); + auto artifact_content_obj = std::make_unique<sarif_artifact_content> (); artifact_content_obj->set<json::string> ("text", - ::make_unique <json::string> (utf8_content.get_buffer (), + std::make_unique <json::string> (utf8_content.get_buffer (), utf8_content.length ())); return artifact_content_obj; } @@ -3310,7 +3311,7 @@ maybe_make_artifact_content_object (const char *filename, return nullptr; } - auto artifact_content_obj = ::make_unique<sarif_artifact_content> (); + auto artifact_content_obj = std::make_unique<sarif_artifact_content> (); artifact_content_obj->set_string ("text", text_utf8); free (text_utf8); @@ -3328,11 +3329,11 @@ maybe_make_artifact_content_object (const char *filename, std::unique_ptr<sarif_fix> sarif_builder::make_fix_object (const rich_location &richloc) { - auto fix_obj = ::make_unique<sarif_fix> (); + auto fix_obj = std::make_unique<sarif_fix> (); /* "artifactChanges" property (SARIF v2.1.0 section 3.55.3). */ /* We assume that all fix-it hints in RICHLOC affect the same file. */ - auto artifact_change_arr = ::make_unique<json::array> (); + auto artifact_change_arr = std::make_unique<json::array> (); artifact_change_arr->append<sarif_artifact_change> (make_artifact_change_object (richloc)); fix_obj->set<json::array> ("artifactChanges", @@ -3346,7 +3347,7 @@ sarif_builder::make_fix_object (const rich_location &richloc) std::unique_ptr<sarif_artifact_change> sarif_builder::make_artifact_change_object (const rich_location &richloc) { - auto artifact_change_obj = ::make_unique<sarif_artifact_change> (); + auto artifact_change_obj = std::make_unique<sarif_artifact_change> (); /* "artifactLocation" property (SARIF v2.1.0 section 3.56.2). */ artifact_change_obj->set<sarif_artifact_location> @@ -3354,7 +3355,7 @@ sarif_builder::make_artifact_change_object (const rich_location &richloc) make_artifact_location_object (richloc.get_loc ())); /* "replacements" property (SARIF v2.1.0 section 3.56.3). */ - auto replacement_arr = ::make_unique<json::array> (); + auto replacement_arr = std::make_unique<json::array> (); for (unsigned int i = 0; i < richloc.get_num_fixit_hints (); i++) { const fixit_hint *hint = richloc.get_fixit_hint (i); @@ -3372,7 +3373,7 @@ sarif_builder::make_artifact_change_object (const rich_location &richloc) std::unique_ptr<sarif_replacement> sarif_builder::make_replacement_object (const fixit_hint &hint) const { - auto replacement_obj = ::make_unique<sarif_replacement> (); + auto replacement_obj = std::make_unique<sarif_replacement> (); /* "deletedRegion" property (SARIF v2.1.0 section 3.57.3). */ replacement_obj->set<sarif_region> ("deletedRegion", @@ -3391,7 +3392,7 @@ sarif_builder::make_replacement_object (const fixit_hint &hint) const std::unique_ptr<sarif_artifact_content> sarif_builder::make_artifact_content_object (const char *text) const { - auto content_obj = ::make_unique<sarif_artifact_content> (); + auto content_obj = std::make_unique<sarif_artifact_content> (); /* "text" property (SARIF v2.1.0 section 3.3.2). */ content_obj->set_string ("text", text); @@ -3470,7 +3471,7 @@ public: std::unique_ptr<diagnostic_per_format_buffer> make_per_format_buffer () final override { - return ::make_unique<diagnostic_sarif_format_buffer> (m_builder); + return std::make_unique<diagnostic_sarif_format_buffer> (m_builder); } void set_buffer (diagnostic_per_format_buffer *base_buffer) final override { @@ -3534,10 +3535,10 @@ protected: const line_maps *line_maps, const char *main_input_filename_, bool formatted, - enum sarif_version version) + const sarif_generation_options &sarif_gen_opts) : diagnostic_output_format (context), m_builder (context, *get_printer (), line_maps, main_input_filename_, - formatted, version), + formatted, sarif_gen_opts), m_buffer (nullptr) {} @@ -3552,10 +3553,11 @@ public: const line_maps *line_maps, const char *main_input_filename_, bool formatted, - enum sarif_version version, + const sarif_generation_options &sarif_gen_opts, FILE *stream) : sarif_output_format (context, line_maps, main_input_filename_, - formatted, version), + formatted, + sarif_gen_opts), m_stream (stream) { } @@ -3578,10 +3580,10 @@ public: const line_maps *line_maps, const char *main_input_filename_, bool formatted, - enum sarif_version version, + const sarif_generation_options &sarif_gen_opts, diagnostic_output_file output_file) : sarif_output_format (context, line_maps, main_input_filename_, - formatted, version), + formatted, sarif_gen_opts), m_output_file (std::move (output_file)) { gcc_assert (m_output_file.get_open_file ()); @@ -3741,18 +3743,18 @@ 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) + bool formatted) { gcc_assert (line_maps); + const sarif_generation_options sarif_gen_opts; diagnostic_output_format_init_sarif (context, - ::make_unique<sarif_stream_output_format> (context, - line_maps, - main_input_filename_, - formatted, - version, - stderr)); + std::make_unique<sarif_stream_output_format> (context, + line_maps, + main_input_filename_, + formatted, + sarif_gen_opts, + stderr)); } /* Attempt to open BASE_FILE_NAME.sarif for writing. @@ -3798,7 +3800,6 @@ 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) { gcc_assert (line_maps); @@ -3808,14 +3809,15 @@ diagnostic_output_format_init_sarif_file (diagnostic_context &context, line_maps, base_file_name); + const sarif_generation_options sarif_gen_opts; diagnostic_output_format_init_sarif (context, - ::make_unique<sarif_file_output_format> (context, - line_maps, - main_input_filename_, - formatted, - version, - std::move (output_file))); + std::make_unique<sarif_file_output_format> (context, + line_maps, + main_input_filename_, + formatted, + sarif_gen_opts, + std::move (output_file))); } /* Populate CONTEXT in preparation for SARIF output to STREAM. */ @@ -3825,37 +3827,46 @@ 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) { gcc_assert (line_maps); + const sarif_generation_options sarif_gen_opts; diagnostic_output_format_init_sarif (context, - ::make_unique<sarif_stream_output_format> (context, - line_maps, - main_input_filename_, - formatted, - version, - stream)); + std::make_unique<sarif_stream_output_format> (context, + line_maps, + main_input_filename_, + formatted, + sarif_gen_opts, + stream)); } 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, + bool formatted, + const sarif_generation_options &sarif_gen_opts, diagnostic_output_file output_file) { - auto sink = ::make_unique<sarif_file_output_format> (context, - &line_maps, - main_input_filename_, - true, - version, - std::move (output_file)); + auto sink + = std::make_unique<sarif_file_output_format> (context, + &line_maps, + main_input_filename_, + formatted, + sarif_gen_opts, + std::move (output_file)); sink->update_printer (); return sink; } +// struct sarif_generation_options + +sarif_generation_options::sarif_generation_options () +: m_version (sarif_version::v2_1_0) +{ +} + #if CHECKING_P namespace selftest { @@ -3868,13 +3879,13 @@ class test_sarif_diagnostic_context : public test_diagnostic_context { public: test_sarif_diagnostic_context (const char *main_input_filename, - enum sarif_version version) + const sarif_generation_options &sarif_gen_opts) { - auto format = ::make_unique<buffered_output_format> (*this, - line_table, - main_input_filename, - true, - version); + auto format = std::make_unique<buffered_output_format> (*this, + line_table, + main_input_filename, + true, + sarif_gen_opts); m_format = format.get (); // borrowed diagnostic_output_format_init_sarif (*this, std::move (format)); } @@ -3895,9 +3906,9 @@ private: const line_maps *line_maps, const char *main_input_filename_, bool formatted, - enum sarif_version version) + const sarif_generation_options &sarif_gen_opts) : sarif_output_format (context, line_maps, main_input_filename_, - formatted, version) + formatted, sarif_gen_opts) { } bool machine_readable_stderr_p () const final override @@ -3917,8 +3928,8 @@ private: with labels and escape-on-output. */ static void -test_make_location_object (const line_table_case &case_, - enum sarif_version version) +test_make_location_object (const sarif_generation_options &sarif_gen_opts, + const line_table_case &case_) { diagnostic_show_locus_fixture_one_liner_utf8 f (case_); location_t line_end = linemap_position_for_column (line_table, 31); @@ -3930,7 +3941,7 @@ test_make_location_object (const line_table_case &case_, test_diagnostic_context dc; pretty_printer pp; sarif_builder builder (dc, pp, line_table, "MAIN_INPUT_FILENAME", - true, version); + true, sarif_gen_opts); /* These "columns" are byte offsets, whereas later on the columns in the generated SARIF use sarif_builder::get_sarif_column and @@ -4041,9 +4052,9 @@ test_make_location_object (const line_table_case &case_, Verify various basic properties. */ static void -test_simple_log (enum sarif_version version) +test_simple_log (const sarif_generation_options &sarif_gen_opts) { - test_sarif_diagnostic_context dc ("MAIN_INPUT_FILENAME", version); + test_sarif_diagnostic_context dc ("MAIN_INPUT_FILENAME", sarif_gen_opts); rich_location richloc (line_table, UNKNOWN_LOCATION); dc.report (DK_ERROR, richloc, nullptr, 0, "this is a test: %i", 42); @@ -4052,6 +4063,7 @@ test_simple_log (enum sarif_version version) // 3.13 sarifLog: auto log = log_ptr.get (); + const enum sarif_version version = sarif_gen_opts.m_version; ASSERT_JSON_STRING_PROPERTY_EQ (log, "$schema", sarif_version_to_url (version)); ASSERT_JSON_STRING_PROPERTY_EQ (log, "version", @@ -4158,8 +4170,8 @@ test_simple_log (enum sarif_version version) /* As above, but with a "real" location_t. */ static void -test_simple_log_2 (const line_table_case &case_, - enum sarif_version version) +test_simple_log_2 (const sarif_generation_options &sarif_gen_opts, + const line_table_case &case_) { auto_fix_quotes fix_quotes; @@ -4174,7 +4186,7 @@ test_simple_log_2 (const line_table_case &case_, if (line_end > LINE_MAP_MAX_LOCATION_WITH_COLS) return; - test_sarif_diagnostic_context dc (f.get_filename (), version); + test_sarif_diagnostic_context dc (f.get_filename (), sarif_gen_opts); const location_t typo_loc = make_location (linemap_position_for_column (line_table, 1), @@ -4304,11 +4316,11 @@ get_message_from_log (const sarif_log *log) /* Tests of messages with embedded links; see SARIF v2.1.0 3.11.6. */ static void -test_message_with_embedded_link (enum sarif_version version) +test_message_with_embedded_link (const sarif_generation_options &sarif_gen_opts) { auto_fix_quotes fix_quotes; { - test_sarif_diagnostic_context dc ("test.c", version); + test_sarif_diagnostic_context dc ("test.c", sarif_gen_opts); rich_location richloc (line_table, UNKNOWN_LOCATION); dc.report (DK_ERROR, richloc, nullptr, 0, "before %{text%} after", @@ -4324,7 +4336,7 @@ test_message_with_embedded_link (enum sarif_version version) /* Escaping in message text. This is "EXAMPLE 1" from 3.11.6. */ { - test_sarif_diagnostic_context dc ("test.c", version); + test_sarif_diagnostic_context dc ("test.c", sarif_gen_opts); rich_location richloc (line_table, UNKNOWN_LOCATION); /* Disable "unquoted sequence of 2 consecutive punctuation @@ -4364,8 +4376,8 @@ test_message_with_embedded_link (enum sarif_version version) } }; - test_sarif_diagnostic_context dc ("test.c", version); - dc.push_owned_urlifier (::make_unique<test_urlifier> ()); + test_sarif_diagnostic_context dc ("test.c", sarif_gen_opts); + dc.push_owned_urlifier (std::make_unique<test_urlifier> ()); rich_location richloc (line_table, UNKNOWN_LOCATION); dc.report (DK_ERROR, richloc, nullptr, 0, "foo %<-foption%> %<unrecognized%> bar"); @@ -4382,11 +4394,11 @@ test_message_with_embedded_link (enum sarif_version version) 3.11.5 ("Messages with placeholders"). */ static void -test_message_with_braces (enum sarif_version version) +test_message_with_braces (const sarif_generation_options &sarif_gen_opts) { auto_fix_quotes fix_quotes; { - test_sarif_diagnostic_context dc ("test.c", version); + test_sarif_diagnostic_context dc ("test.c", sarif_gen_opts); rich_location richloc (line_table, UNKNOWN_LOCATION); dc.report (DK_ERROR, richloc, nullptr, 0, "open brace: %qs close brace: %qs", @@ -4401,9 +4413,9 @@ test_message_with_braces (enum sarif_version version) } static void -test_buffering (enum sarif_version version) +test_buffering (const sarif_generation_options &sarif_gen_opts) { - test_sarif_diagnostic_context dc ("test.c", version); + test_sarif_diagnostic_context dc ("test.c", sarif_gen_opts); diagnostic_buffer buf_a (dc); diagnostic_buffer buf_b (dc); @@ -4487,41 +4499,45 @@ test_buffering (enum sarif_version version) } } +template <class ...ArgTypes> static void -run_tests_per_version (const line_table_case &case_) +for_each_sarif_gen_option (void (*callback) (const sarif_generation_options &, + ArgTypes ...), + ArgTypes ...args) { + sarif_generation_options sarif_gen_opts; for (int version_idx = 0; version_idx < (int)sarif_version::num_versions; ++version_idx) { - enum sarif_version version - = static_cast<enum sarif_version> (version_idx); + sarif_gen_opts.m_version = static_cast<enum sarif_version> (version_idx); - test_make_location_object (case_, version); - test_simple_log_2 (case_, version); + callback (sarif_gen_opts, args...); } } +static void +run_line_table_case_tests_per_version (const line_table_case &case_) +{ + for_each_sarif_gen_option<const line_table_case &> + (test_make_location_object, case_); + + for_each_sarif_gen_option<const line_table_case &> + (test_simple_log_2, case_); +} + /* Run all of the selftests within this file. */ void diagnostic_format_sarif_cc_tests () { - for (int version_idx = 0; - version_idx < (int)sarif_version::num_versions; - ++version_idx) - { - enum sarif_version version - = static_cast<enum sarif_version> (version_idx); - - test_simple_log (version); - test_message_with_embedded_link (version); - test_message_with_braces (version); - test_buffering (version); - } + for_each_sarif_gen_option (test_simple_log); + for_each_sarif_gen_option (test_message_with_embedded_link); + for_each_sarif_gen_option (test_message_with_braces); + for_each_sarif_gen_option (test_buffering); - /* Run tests per (line-table-case, SARIF version) pair. */ - for_each_line_table_case (run_tests_per_version); + /* Run tests per (SARIF gen-option, line-table-case) pair. */ + for_each_line_table_case (run_line_table_case_tests_per_version); } } // namespace selftest |