| 1 |
/* A library for re-emitting diagnostics saved in SARIF form |
| 2 |
via libgdiagnostics. |
| 3 |
Copyright (C) 2022-2025 Free Software Foundation, Inc. |
| 4 |
Contributed by David Malcolm <[email protected]>. |
| 5 |
|
| 6 |
This file is part of GCC. |
| 7 |
|
| 8 |
GCC is free software; you can redistribute it and/or modify it under |
| 9 |
the terms of the GNU General Public License as published by the Free |
| 10 |
Software Foundation; either version 3, or (at your option) any later |
| 11 |
version. |
| 12 |
|
| 13 |
GCC is distributed in the hope that it will be useful, but WITHOUT ANY |
| 14 |
WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 15 |
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 16 |
for more details. |
| 17 |
|
| 18 |
You should have received a copy of the GNU General Public License |
| 19 |
along with GCC; see the file COPYING3. If not see |
| 20 |
<http://www.gnu.org/licenses/>. */ |
| 21 |
|
| 22 |
#include "config.h" |
| 23 |
#define INCLUDE_VECTOR |
| 24 |
#define INCLUDE_MAP |
| 25 |
#define INCLUDE_STRING |
| 26 |
#include "system.h" |
| 27 |
#include "coretypes.h" |
| 28 |
#include "make-unique.h" |
| 29 |
#include "libgdiagnostics++.h" |
| 30 |
#include "json-parsing.h" |
| 31 |
#include "intl.h" |
| 32 |
#include "sarif-spec-urls.def" |
| 33 |
#include "libsarifreplay.h" |
| 34 |
#include "label-text.h" |
| 35 |
|
| 36 |
namespace { |
| 37 |
|
| 38 |
/* Read the contents of PATH into memory. |
| 39 |
Issue an error to MGR and return nullptr if there are any problems. */ |
| 40 |
|
| 41 |
static std::unique_ptr<std::vector<char>> |
| 42 |
read_file (const char *path, libgdiagnostics::manager &mgr) |
| 43 |
{ |
| 44 |
FILE *f_in = fopen (path, "r"); |
| 45 |
if (!f_in) |
| 46 |
{ |
| 47 |
char *errmsg = xstrerror (errno); |
| 48 |
auto err (mgr.begin_diagnostic (DIAGNOSTIC_LEVEL_ERROR)); |
| 49 |
err.finish ("cannot open %qs: %s", path, errmsg); |
| 50 |
return nullptr; |
| 51 |
} |
| 52 |
|
| 53 |
/* Read content, allocating a buffer for it. */ |
| 54 |
auto result = ::make_unique<std::vector<char>> (); |
| 55 |
char buf[4096]; |
| 56 |
size_t iter_sz_in; |
| 57 |
|
| 58 |
while ( (iter_sz_in = fread (buf, 1, sizeof (buf), f_in)) ) |
| 59 |
{ |
| 60 |
size_t old_total_sz = result->size (); |
| 61 |
size_t new_total_sz = old_total_sz + iter_sz_in; |
| 62 |
size_t old_alloc_sz = result->capacity (); |
| 63 |
if (new_total_sz > old_alloc_sz) |
| 64 |
{ |
| 65 |
size_t new_alloc_sz = std::max (old_alloc_sz * 2, new_total_sz); |
| 66 |
result->reserve (new_alloc_sz); |
| 67 |
} |
| 68 |
gcc_assert (result->capacity () >= new_total_sz); |
| 69 |
result->resize (new_total_sz); |
| 70 |
memcpy (result->data () + old_total_sz, buf, iter_sz_in); |
| 71 |
} |
| 72 |
|
| 73 |
if (!feof (f_in)) |
| 74 |
{ |
| 75 |
char *errmsg = xstrerror (errno); |
| 76 |
auto err (mgr.begin_diagnostic (DIAGNOSTIC_LEVEL_ERROR)); |
| 77 |
err.finish ("error reading from %qs: %s", path, errmsg); |
| 78 |
return nullptr; |
| 79 |
} |
| 80 |
|
| 81 |
fclose (f_in); |
| 82 |
|
| 83 |
return result; |
| 84 |
} |
| 85 |
|
| 86 |
static libgdiagnostics::physical_location |
| 87 |
make_physical_location (libgdiagnostics::manager &mgr, |
| 88 |
libgdiagnostics::file f, |
| 89 |
const json::location_map::point &point) |
| 90 |
{ |
| 91 |
/* json::location_map::point uses 0-based columns, |
| 92 |
whereas libgdiagnostics uses 1-based columns. */ |
| 93 |
return mgr.new_location_from_file_line_column (f, |
| 94 |
point.m_line, |
| 95 |
point.m_column + 1); |
| 96 |
} |
| 97 |
|
| 98 |
static libgdiagnostics::physical_location |
| 99 |
make_physical_location (libgdiagnostics::manager &mgr, |
| 100 |
libgdiagnostics::file f, |
| 101 |
const json::location_map::range &range) |
| 102 |
{ |
| 103 |
libgdiagnostics::physical_location start |
| 104 |
= make_physical_location (mgr, f, range.m_start); |
| 105 |
libgdiagnostics::physical_location end |
| 106 |
= make_physical_location (mgr, f, range.m_end); |
| 107 |
return mgr.new_location_from_range (start, start, end); |
| 108 |
} |
| 109 |
|
| 110 |
enum class status |
| 111 |
{ |
| 112 |
ok, |
| 113 |
err_reading_file, |
| 114 |
err_malformed_json, |
| 115 |
err_invalid_sarif, |
| 116 |
err_unhandled_sarif |
| 117 |
}; |
| 118 |
|
| 119 |
/* A reference to the SARIF specification. */ |
| 120 |
|
| 121 |
class spec_ref |
| 122 |
{ |
| 123 |
public: |
| 124 |
spec_ref (const char *section) |
| 125 |
: m_section (section) |
| 126 |
{} |
| 127 |
|
| 128 |
virtual char *make_description () const |
| 129 |
{ |
| 130 |
/* 'SECTION SIGN' (U+00A7). */ |
| 131 |
#define SECTION_SIGN_UTF8 "\xC2\xA7" |
| 132 |
return xasprintf ("SARIF v2.1.0 " SECTION_SIGN_UTF8 "%s", m_section); |
| 133 |
} |
| 134 |
|
| 135 |
char *make_url () const |
| 136 |
{ |
| 137 |
const char *anchor = get_anchor_for_section (m_section); |
| 138 |
if (!anchor) |
| 139 |
return nullptr; |
| 140 |
return xasprintf ("%s#%s", sarif_spec_base_url, anchor); |
| 141 |
} |
| 142 |
|
| 143 |
private: |
| 144 |
static const char * |
| 145 |
get_anchor_for_section (const char *section) |
| 146 |
{ |
| 147 |
/* Linear search, but the array is only a few hundred entries . */ |
| 148 |
for (size_t i = 0; i < ARRAY_SIZE (sarif_spec_anchor_arr); i++) |
| 149 |
{ |
| 150 |
if (strcmp (sarif_spec_anchor_arr[i].m_ref, section) == 0) |
| 151 |
return sarif_spec_anchor_arr[i].m_anchor; |
| 152 |
} |
| 153 |
return nullptr; |
| 154 |
} |
| 155 |
|
| 156 |
/* e.g. "3.1" for section 3.1 of the spec. */ |
| 157 |
const char *m_section; |
| 158 |
}; |
| 159 |
|
| 160 |
/* A reference to the SARIF specification for a particular kind of object. */ |
| 161 |
|
| 162 |
class object_spec_ref : public spec_ref |
| 163 |
{ |
| 164 |
public: |
| 165 |
object_spec_ref (const char *obj_name, const char *section) |
| 166 |
: spec_ref (section), |
| 167 |
m_obj_name (obj_name) |
| 168 |
{} |
| 169 |
|
| 170 |
const char *get_obj_name () const { return m_obj_name; } |
| 171 |
|
| 172 |
private: |
| 173 |
const char *m_obj_name; |
| 174 |
}; |
| 175 |
|
| 176 |
/* A reference to the SARIF specification for a particular property |
| 177 |
of a particular kind of object. */ |
| 178 |
|
| 179 |
class property_spec_ref : public object_spec_ref |
| 180 |
{ |
| 181 |
public: |
| 182 |
property_spec_ref (const char *obj_name, |
| 183 |
const char *property_name, |
| 184 |
const char *section) |
| 185 |
: object_spec_ref (obj_name, section), |
| 186 |
m_property_name (property_name) |
| 187 |
{} |
| 188 |
|
| 189 |
const char *get_property_name () const { return m_property_name; } |
| 190 |
|
| 191 |
private: |
| 192 |
const char *m_property_name; |
| 193 |
}; |
| 194 |
|
| 195 |
template <typename ValueType> |
| 196 |
struct string_property_value |
| 197 |
{ |
| 198 |
const char *m_string; |
| 199 |
ValueType m_value; |
| 200 |
}; |
| 201 |
|
| 202 |
/* A class for recording annotations seen in locations (§3.28.6) that |
| 203 |
should be emitted as secondary locations on diagnostics. */ |
| 204 |
|
| 205 |
class annotation |
| 206 |
{ |
| 207 |
public: |
| 208 |
annotation (libgdiagnostics::physical_location phys_loc, |
| 209 |
label_text label) |
| 210 |
: m_phys_loc (phys_loc), |
| 211 |
m_label (std::move (label)) |
| 212 |
{ |
| 213 |
} |
| 214 |
|
| 215 |
libgdiagnostics::physical_location m_phys_loc; |
| 216 |
label_text m_label; |
| 217 |
}; |
| 218 |
|
| 219 |
class sarif_replayer |
| 220 |
{ |
| 221 |
public: |
| 222 |
sarif_replayer (libgdiagnostics::manager &&output_manager, |
| 223 |
libgdiagnostics::manager &&control_manager) |
| 224 |
: m_output_mgr (std::move (output_manager)), |
| 225 |
m_control_mgr (std::move (control_manager)), |
| 226 |
m_driver_obj (nullptr), |
| 227 |
m_artifacts_arr (nullptr) |
| 228 |
{ |
| 229 |
} |
| 230 |
|
| 231 |
enum status replay_file (const char *filename, |
| 232 |
const replay_options &replay_opts); |
| 233 |
|
| 234 |
private: |
| 235 |
class replayer_location_map : public json::location_map |
| 236 |
{ |
| 237 |
public: |
| 238 |
void record_range_for_value (json::value *jv, |
| 239 |
const range &r) final override |
| 240 |
{ |
| 241 |
m_map_jv_to_range[jv] = r; |
| 242 |
} |
| 243 |
|
| 244 |
const json::location_map::range & |
| 245 |
get_range_for_value (const json::value &jv) const |
| 246 |
{ |
| 247 |
auto iter = m_map_jv_to_range.find (&jv); |
| 248 |
gcc_assert (iter != m_map_jv_to_range.end ()); |
| 249 |
return iter->second; |
| 250 |
} |
| 251 |
|
| 252 |
private: |
| 253 |
std::map<const json::value *, range> m_map_jv_to_range; |
| 254 |
}; |
| 255 |
|
| 256 |
enum status emit_sarif_as_diagnostics (const json::value &jv); |
| 257 |
|
| 258 |
label_text |
| 259 |
make_plain_text_within_result_message (const json::object *tool_component_obj, |
| 260 |
const json::object &message_obj, |
| 261 |
const json::object *rule_obj); |
| 262 |
|
| 263 |
/* Handlers for specific parts of the SARIF spec. |
| 264 |
Keep this in the same order as the spec. */ |
| 265 |
|
| 266 |
// "artifactLocation" object (§3.4) |
| 267 |
enum status |
| 268 |
handle_artifact_location_object (const json::object &artifact_loc, |
| 269 |
libgdiagnostics::file &out); |
| 270 |
|
| 271 |
// Message string lookup algorithm (§3.11.7) |
| 272 |
const char * |
| 273 |
lookup_plain_text_within_result_message (const json::object *tool_component_obj, |
| 274 |
const json::object &message_obj, |
| 275 |
const json::object *rule_obj, |
| 276 |
const json::string *&out_js_str); |
| 277 |
|
| 278 |
// "multiformatMessageString" object (§3.12). |
| 279 |
const char * |
| 280 |
get_plain_text_from_mfms (json::value &mfms_val, |
| 281 |
const property_spec_ref &prop, |
| 282 |
const json::string *&out_js_str); |
| 283 |
|
| 284 |
// "run" object (§3.14) |
| 285 |
enum status |
| 286 |
handle_run_obj (const json::object &run_obj); |
| 287 |
|
| 288 |
// "tool" object (§3.18) |
| 289 |
enum status |
| 290 |
handle_tool_obj (const json::object &tool_obj); |
| 291 |
|
| 292 |
// "artifact" object (§3.24). */ |
| 293 |
void |
| 294 |
handle_artifact_obj (const json::object &artifact_obj); |
| 295 |
|
| 296 |
// "result" object (§3.27) |
| 297 |
enum status |
| 298 |
handle_result_obj (const json::object &result_obj, |
| 299 |
const json::object &run_obj, |
| 300 |
const json::object &tool_obj); |
| 301 |
json::result<enum diagnostic_level, enum status> |
| 302 |
get_level_from_level_str (const json::string &level_str); |
| 303 |
|
| 304 |
// "location" object (§3.28) |
| 305 |
enum status |
| 306 |
handle_location_object (const json::object &location_obj, |
| 307 |
const json::object &run_obj, |
| 308 |
libgdiagnostics::physical_location &out_physical_loc, |
| 309 |
libgdiagnostics::logical_location &out_logical_loc, |
| 310 |
std::vector<annotation> *out_annotations); |
| 311 |
|
| 312 |
// "physicalLocation" object (§3.29) |
| 313 |
enum status |
| 314 |
handle_physical_location_object (const json::object &phys_loc_obj, |
| 315 |
libgdiagnostics::physical_location &out); |
| 316 |
|
| 317 |
// "region" object (§3.30) |
| 318 |
enum status |
| 319 |
handle_region_object (const json::object ®ion_obj, |
| 320 |
libgdiagnostics::file file, |
| 321 |
libgdiagnostics::physical_location &out); |
| 322 |
|
| 323 |
// "logicalLocation" object (§3.33) |
| 324 |
enum status |
| 325 |
handle_logical_location_object (const json::object &logical_loc_obj, |
| 326 |
const json::object *run_obj, |
| 327 |
libgdiagnostics::logical_location &out); |
| 328 |
|
| 329 |
// "threadFlow" object (§3.37) |
| 330 |
enum status |
| 331 |
handle_thread_flow_object (const json::object &thread_flow_obj, |
| 332 |
const json::object &run_obj, |
| 333 |
libgdiagnostics::execution_path &out); |
| 334 |
|
| 335 |
// "threadFlowLocation" object (§3.38) |
| 336 |
enum status |
| 337 |
handle_thread_flow_location_object (const json::object &tflow_loc_obj, |
| 338 |
const json::object &run_obj, |
| 339 |
libgdiagnostics::execution_path &out); |
| 340 |
|
| 341 |
// reportingDescriptor lookup (§3.52.3) |
| 342 |
const json::object * |
| 343 |
lookup_rule_by_id_in_tool (const char *rule_id, |
| 344 |
const json::object &tool_obj, |
| 345 |
const json::object *&tool_component_obj); |
| 346 |
|
| 347 |
const json::object * |
| 348 |
lookup_rule_by_id_in_component (const char *rule_id, |
| 349 |
const json::object &tool_component_obj); |
| 350 |
|
| 351 |
// "fix" object (§3.55) |
| 352 |
enum status |
| 353 |
handle_fix_object (libgdiagnostics::diagnostic &diag, |
| 354 |
const json::object &fix_obj); |
| 355 |
|
| 356 |
// "artifactChange" object (§3.56) |
| 357 |
enum status |
| 358 |
handle_artifact_change_object (libgdiagnostics::diagnostic &diag, |
| 359 |
const json::object &change_obj); |
| 360 |
|
| 361 |
/* Support functions. */ |
| 362 |
|
| 363 |
/* Report an error to m_control_mgr about JV violating REF, |
| 364 |
and return status::err_invalid_sarif. */ |
| 365 |
|
| 366 |
enum status |
| 367 |
report_invalid_sarif (const json::value &jv, |
| 368 |
const spec_ref &ref, |
| 369 |
const char *gmsgid, ...) |
| 370 |
LIBGDIAGNOSTICS_PARAM_GCC_FORMAT_STRING (4, 5) |
| 371 |
{ |
| 372 |
va_list ap; |
| 373 |
va_start (ap, gmsgid); |
| 374 |
report_problem (jv, ref, gmsgid, &ap, DIAGNOSTIC_LEVEL_ERROR); |
| 375 |
va_end (ap); |
| 376 |
return status::err_invalid_sarif; |
| 377 |
} |
| 378 |
|
| 379 |
/* Report a "sorry" to m_control_mgr inability to handle JV and REF, |
| 380 |
and return status::err_unhandled_sarif. */ |
| 381 |
|
| 382 |
enum status |
| 383 |
report_unhandled_sarif (const json::value &jv, |
| 384 |
const spec_ref &ref, |
| 385 |
const char *gmsgid, ...) |
| 386 |
LIBGDIAGNOSTICS_PARAM_GCC_FORMAT_STRING (4, 5) |
| 387 |
{ |
| 388 |
va_list ap; |
| 389 |
va_start (ap, gmsgid); |
| 390 |
report_problem (jv, ref, gmsgid, &ap, DIAGNOSTIC_LEVEL_SORRY); |
| 391 |
va_end (ap); |
| 392 |
return status::err_unhandled_sarif; |
| 393 |
} |
| 394 |
|
| 395 |
void |
| 396 |
report_problem (const json::value &jv, |
| 397 |
const spec_ref &ref, |
| 398 |
const char *gmsgid, |
| 399 |
va_list *args, |
| 400 |
enum diagnostic_level level) |
| 401 |
LIBGDIAGNOSTICS_PARAM_GCC_FORMAT_STRING (4, 0) |
| 402 |
{ |
| 403 |
auto diag (m_control_mgr.begin_diagnostic (level)); |
| 404 |
|
| 405 |
/* Add rule specifying the pertinent section of the specification. |
| 406 |
There doesn't seem to be a systematic mapping from spec sections to |
| 407 |
HTML anchors, so we can't provide URLs |
| 408 |
(filed as https://github.com/oasis-tcs/sarif-spec/issues/533 ). */ |
| 409 |
char *ref_desc = ref.make_description (); |
| 410 |
char *ref_url = ref.make_url (); |
| 411 |
diag.add_rule (ref_desc, ref_url); |
| 412 |
free (ref_desc); |
| 413 |
free (ref_url); |
| 414 |
|
| 415 |
auto loc_range |
| 416 |
= make_physical_location (m_control_mgr, |
| 417 |
m_loaded_file, |
| 418 |
m_json_location_map.get_range_for_value (jv)); |
| 419 |
diag.set_location (loc_range); |
| 420 |
|
| 421 |
diag.finish_va (gmsgid, args); |
| 422 |
} |
| 423 |
|
| 424 |
/* Require OBJ to have at least one of OBJ_PROP1 or OBJ_PROP2. |
| 425 |
If successful, result status::ok. |
| 426 |
Otherwise, complain about OBJ_CONSTRAINTS and return |
| 427 |
status::invalid_sarif. */ |
| 428 |
enum status |
| 429 |
report_invalid_sarif_at_least_one_of (const json::object &obj, |
| 430 |
const object_spec_ref &obj_constraints, |
| 431 |
const property_spec_ref &obj_prop_1, |
| 432 |
const property_spec_ref &obj_prop_2) |
| 433 |
{ |
| 434 |
return report_invalid_sarif |
| 435 |
(obj, obj_constraints, |
| 436 |
"expected SARIF %qs object to contain at least one of %qs or %qs", |
| 437 |
obj_constraints.get_obj_name (), |
| 438 |
obj_prop_1.get_property_name (), |
| 439 |
obj_prop_2.get_property_name ()); |
| 440 |
} |
| 441 |
|
| 442 |
/* Require VAL to be a json::object. |
| 443 |
If successful, return it as an object. |
| 444 |
Otherwise, complain using REF and return nullptr. */ |
| 445 |
const json::object * |
| 446 |
require_object (const json::value &val, const property_spec_ref &ref) |
| 447 |
{ |
| 448 |
const json::object *obj = dyn_cast <const json::object *> (&val); |
| 449 |
if (!obj) |
| 450 |
{ |
| 451 |
report_invalid_sarif (val, ref, "expected %s.%s to be an object", |
| 452 |
ref.get_obj_name (), ref.get_property_name ()); |
| 453 |
return nullptr; |
| 454 |
} |
| 455 |
return obj; |
| 456 |
} |
| 457 |
|
| 458 |
/* Require VAL to be a json::string |
| 459 |
If successful, return it as an string. |
| 460 |
Otherwise, complain using REF and return nullptr. */ |
| 461 |
const json::string * |
| 462 |
require_string (const json::value &val, const property_spec_ref &ref) |
| 463 |
{ |
| 464 |
const json::string *str = dyn_cast <const json::string *> (&val); |
| 465 |
if (!str) |
| 466 |
{ |
| 467 |
report_invalid_sarif (val, ref, "expected %s.%s to be an string", |
| 468 |
ref.get_obj_name (), ref.get_property_name ()); |
| 469 |
return nullptr; |
| 470 |
} |
| 471 |
return str; |
| 472 |
} |
| 473 |
/* Look for an optional property within OBJ based on REF. */ |
| 474 |
const json::value * |
| 475 |
get_optional_property (const json::object &obj, |
| 476 |
const property_spec_ref &ref) |
| 477 |
{ |
| 478 |
return obj.get (ref.get_property_name ()); |
| 479 |
} |
| 480 |
|
| 481 |
/* Look for a property of VAL based on REF. |
| 482 |
If present, it must be of kind JsonType. |
| 483 |
If found and valid, return the property's value. |
| 484 |
If not found, silently return nullptr. |
| 485 |
Otherwise, complain and return nullptr. */ |
| 486 |
template <typename JsonType> |
| 487 |
const JsonType * |
| 488 |
get_optional_property (const json::object &obj, |
| 489 |
const property_spec_ref &ref) |
| 490 |
{ |
| 491 |
const json::value *property_val = get_optional_property (obj, ref); |
| 492 |
if (!property_val) |
| 493 |
return nullptr; |
| 494 |
const JsonType *sub = dyn_cast<const JsonType *> (property_val); |
| 495 |
if (!sub) |
| 496 |
{ |
| 497 |
/* Property is wrong kind of value. */ |
| 498 |
report_bad_property_kind<JsonType> (obj, ref, *property_val); |
| 499 |
return nullptr; |
| 500 |
} |
| 501 |
return sub; |
| 502 |
} |
| 503 |
|
| 504 |
/* Require VAL to be a json::object. |
| 505 |
Look for a property of VAL based on REF, which must be of |
| 506 |
kind JsonType. |
| 507 |
If successful, return the property's value. |
| 508 |
Otherwise, complain and return nullptr. */ |
| 509 |
template <typename JsonType> |
| 510 |
const JsonType * |
| 511 |
get_required_property (const json::value &val, |
| 512 |
const property_spec_ref &ref) |
| 513 |
{ |
| 514 |
const json::object *obj = require_object (val, ref); |
| 515 |
if (!obj) |
| 516 |
return nullptr; |
| 517 |
return get_required_property<JsonType> (*obj, ref); |
| 518 |
} |
| 519 |
|
| 520 |
/* Look for a property of VAL based on REF, which must be of |
| 521 |
kind JsonType. |
| 522 |
If successful, return the property's value. |
| 523 |
Otherwise, complain and return nullptr. */ |
| 524 |
template <typename JsonType> |
| 525 |
const JsonType * |
| 526 |
get_required_property (const json::object &obj, |
| 527 |
const property_spec_ref &ref) |
| 528 |
{ |
| 529 |
const json::value *property_val = get_optional_property (obj, ref); |
| 530 |
if (!property_val) |
| 531 |
{ |
| 532 |
/* Property not present. */ |
| 533 |
report_invalid_sarif (obj, ref, |
| 534 |
"expected %s object to have a %qs property", |
| 535 |
ref.get_obj_name (), ref.get_property_name ()); |
| 536 |
return nullptr; |
| 537 |
} |
| 538 |
const JsonType *sub = dyn_cast<const JsonType *> (property_val); |
| 539 |
if (!sub) |
| 540 |
{ |
| 541 |
/* Property is wrong kind of value. */ |
| 542 |
report_bad_property_kind<JsonType> (obj, ref, *property_val); |
| 543 |
return nullptr; |
| 544 |
} |
| 545 |
return sub; |
| 546 |
} |
| 547 |
|
| 548 |
template <typename JsonType> |
| 549 |
void |
| 550 |
report_bad_property_kind (const json::object &obj, |
| 551 |
const property_spec_ref &ref, |
| 552 |
const json::value &property_val); |
| 553 |
|
| 554 |
const json::object * |
| 555 |
require_object_for_element (const json::value &jv, |
| 556 |
const property_spec_ref &ref) |
| 557 |
{ |
| 558 |
const json::object *obj = dyn_cast <const json::object *> (&jv); |
| 559 |
if (!obj) |
| 560 |
{ |
| 561 |
report_invalid_sarif (jv, ref, |
| 562 |
"expected element of %s.%s array to be an object", |
| 563 |
ref.get_obj_name (), ref.get_property_name ()); |
| 564 |
return nullptr; |
| 565 |
} |
| 566 |
return obj; |
| 567 |
} |
| 568 |
|
| 569 |
template <typename ValueType> |
| 570 |
json::result<ValueType, enum status> |
| 571 |
get_value_from_json_string (const json::string &json_str, |
| 572 |
const property_spec_ref &prop, |
| 573 |
const string_property_value<ValueType> *value_arr, |
| 574 |
size_t num_values); |
| 575 |
|
| 576 |
/* The manager to replay the SARIF files to. */ |
| 577 |
libgdiagnostics::manager m_output_mgr; |
| 578 |
|
| 579 |
/* The manager for reporting issues loading SARIF files. */ |
| 580 |
libgdiagnostics::manager m_control_mgr; |
| 581 |
|
| 582 |
/* The file within m_control_mgr representing the .sarif file. */ |
| 583 |
libgdiagnostics::file m_loaded_file; |
| 584 |
|
| 585 |
replayer_location_map m_json_location_map; |
| 586 |
|
| 587 |
const json::object *m_driver_obj; |
| 588 |
const json::array *m_artifacts_arr; |
| 589 |
}; |
| 590 |
|
| 591 |
static const char * |
| 592 |
describe_kind (const json::value &val) |
| 593 |
{ |
| 594 |
switch (val.get_kind ()) |
| 595 |
{ |
| 596 |
default: |
| 597 |
gcc_unreachable (); |
| 598 |
case json::JSON_OBJECT: |
| 599 |
return _("JSON object"); |
| 600 |
|
| 601 |
case json::JSON_ARRAY: |
| 602 |
return _("JSON array"); |
| 603 |
|
| 604 |
case json::JSON_INTEGER: |
| 605 |
case json::JSON_FLOAT: |
| 606 |
return _("JSON number"); |
| 607 |
|
| 608 |
case json::JSON_STRING: |
| 609 |
return _("JSON string"); |
| 610 |
|
| 611 |
case json::JSON_TRUE: |
| 612 |
case json::JSON_FALSE: |
| 613 |
case json::JSON_NULL: |
| 614 |
return _("JSON literal"); |
| 615 |
} |
| 616 |
} |
| 617 |
|
| 618 |
/* class sarif_replayer. */ |
| 619 |
|
| 620 |
template <> |
| 621 |
void |
| 622 |
sarif_replayer:: |
| 623 |
report_bad_property_kind<json::value> (const json::object &, |
| 624 |
const property_spec_ref &, |
| 625 |
const json::value &) |
| 626 |
{ |
| 627 |
gcc_unreachable (); |
| 628 |
} |
| 629 |
|
| 630 |
template <> |
| 631 |
void |
| 632 |
sarif_replayer:: |
| 633 |
report_bad_property_kind<json::integer_number> (const json::object &, |
| 634 |
const property_spec_ref &ref, |
| 635 |
const json::value &propval) |
| 636 |
{ |
| 637 |
report_invalid_sarif (propval, ref, "expected %s.%s to be a JSON integer; got %s", |
| 638 |
ref.get_obj_name (), ref.get_property_name (), |
| 639 |
describe_kind (propval)); |
| 640 |
} |
| 641 |
|
| 642 |
template <> |
| 643 |
void |
| 644 |
sarif_replayer:: |
| 645 |
report_bad_property_kind<json::string> (const json::object &, |
| 646 |
const property_spec_ref &ref, |
| 647 |
const json::value &propval) |
| 648 |
{ |
| 649 |
report_invalid_sarif (propval, ref, "expected %s.%s to be a JSON string; got %s", |
| 650 |
ref.get_obj_name (), ref.get_property_name (), |
| 651 |
describe_kind (propval)); |
| 652 |
} |
| 653 |
|
| 654 |
template <> |
| 655 |
void |
| 656 |
sarif_replayer:: |
| 657 |
report_bad_property_kind<json::array> (const json::object &, |
| 658 |
const property_spec_ref &ref, |
| 659 |
const json::value &propval) |
| 660 |
{ |
| 661 |
report_invalid_sarif (propval, ref, "expected %s.%s to be a JSON array; got %s", |
| 662 |
ref.get_obj_name (), ref.get_property_name (), |
| 663 |
describe_kind (propval)); |
| 664 |
} |
| 665 |
|
| 666 |
template <> |
| 667 |
void |
| 668 |
sarif_replayer:: |
| 669 |
report_bad_property_kind<json::object> (const json::object &, |
| 670 |
const property_spec_ref &ref, |
| 671 |
const json::value &propval) |
| 672 |
{ |
| 673 |
report_invalid_sarif (propval, ref, "expected %s.%s to be a JSON object; got %s", |
| 674 |
ref.get_obj_name (), ref.get_property_name (), |
| 675 |
describe_kind (propval)); |
| 676 |
} |
| 677 |
|
| 678 |
enum status |
| 679 |
sarif_replayer::replay_file (const char *filename, |
| 680 |
const replay_options &replay_opts) |
| 681 |
{ |
| 682 |
std::unique_ptr<std::vector<char>> buf = read_file (filename, m_control_mgr); |
| 683 |
if (!buf) |
| 684 |
return status::err_reading_file; |
| 685 |
|
| 686 |
/* Use "sarif" as the sourceLanguage for SARIF itself; see |
| 687 |
https://github.com/oasis-tcs/sarif-spec/issues/654 */ |
| 688 |
const char * const source_language = "sarif"; |
| 689 |
m_loaded_file = m_control_mgr.new_file (filename, source_language); |
| 690 |
|
| 691 |
if (replay_opts.m_echo_file) |
| 692 |
{ |
| 693 |
fprintf (stderr, "%s: (%li bytes)\n", |
| 694 |
filename, (long)buf->size ()); |
| 695 |
for (size_t i = 0; i < buf->size(); i++) |
| 696 |
fputc ((*buf)[i], stderr); |
| 697 |
} |
| 698 |
|
| 699 |
json::parser_result_t result |
| 700 |
(json::parse_utf8_string (buf->size (), |
| 701 |
(const char *)buf->data (), |
| 702 |
replay_opts.m_json_comments, |
| 703 |
&m_json_location_map)); |
| 704 |
|
| 705 |
if (auto json_err = result.m_err.get ()) |
| 706 |
{ |
| 707 |
gcc_assert (!result.m_val.get ()); |
| 708 |
auto file = m_control_mgr.new_file (filename, source_language); |
| 709 |
auto loc_range = make_physical_location (m_control_mgr, |
| 710 |
file, |
| 711 |
json_err->get_range ()); |
| 712 |
auto err (m_control_mgr.begin_diagnostic (DIAGNOSTIC_LEVEL_ERROR)); |
| 713 |
err.set_location (loc_range); |
| 714 |
err.finish ("%s", json_err->get_msg ()); |
| 715 |
|
| 716 |
return status::err_malformed_json; |
| 717 |
} |
| 718 |
|
| 719 |
gcc_assert (result.m_val.get ()); |
| 720 |
return emit_sarif_as_diagnostics (*result.m_val.get ()); |
| 721 |
} |
| 722 |
|
| 723 |
#define PROP_sarifLog_version \ |
| 724 |
property_spec_ref ("sarifLog", "version", "3.13.2") |
| 725 |
|
| 726 |
#define PROP_sarifLog_runs \ |
| 727 |
property_spec_ref ("sarifLog", "runs", "3.13.4") |
| 728 |
|
| 729 |
enum status |
| 730 |
sarif_replayer::emit_sarif_as_diagnostics (const json::value &jv) |
| 731 |
{ |
| 732 |
/* We expect a sarifLog object as the top-level value |
| 733 |
(SARIF v2.1.0 section 3.13). */ |
| 734 |
const json::object *toplev_obj = dyn_cast <const json::object *> (&jv); |
| 735 |
if (!toplev_obj) |
| 736 |
return report_invalid_sarif |
| 737 |
(jv, spec_ref ("3.1"), |
| 738 |
"expected a sarifLog object as the top-level value"); |
| 739 |
|
| 740 |
/* sarifLog objects SHALL have a property named "version" |
| 741 |
(SARIF v2.1.0 section 3.13.2) with a string value. */ |
| 742 |
if (!get_required_property<json::string> (*toplev_obj, |
| 743 |
PROP_sarifLog_version)) |
| 744 |
return status::err_invalid_sarif; |
| 745 |
|
| 746 |
/* sarifLog.runs must be null or be an array. */ |
| 747 |
const property_spec_ref prop_runs (PROP_sarifLog_runs); |
| 748 |
const json::value *runs |
| 749 |
= get_required_property<json::value> (*toplev_obj, prop_runs); |
| 750 |
if (!runs) |
| 751 |
return status::err_invalid_sarif; |
| 752 |
|
| 753 |
switch (runs->get_kind ()) |
| 754 |
{ |
| 755 |
default: |
| 756 |
return report_invalid_sarif (*runs, prop_runs, |
| 757 |
"expected sarifLog.runs to be" |
| 758 |
" %<null%> or an array"); |
| 759 |
|
| 760 |
case json::JSON_NULL: |
| 761 |
/* Nothing to do. */ |
| 762 |
break; |
| 763 |
|
| 764 |
case json::JSON_ARRAY: |
| 765 |
{ |
| 766 |
const json::array &runs_arr = *as_a <const json::array *> (runs); |
| 767 |
for (auto element : runs_arr) |
| 768 |
{ |
| 769 |
const json::object *run_obj |
| 770 |
= require_object_for_element (*element, prop_runs); |
| 771 |
if (!run_obj) |
| 772 |
return status::err_invalid_sarif; |
| 773 |
enum status s = handle_run_obj (*run_obj); |
| 774 |
if (s != status::ok) |
| 775 |
return s; |
| 776 |
} |
| 777 |
} |
| 778 |
break; |
| 779 |
} |
| 780 |
|
| 781 |
return status::ok; |
| 782 |
} |
| 783 |
|
| 784 |
/* Process a run object (SARIF v2.1.0 section 3.14). */ |
| 785 |
|
| 786 |
enum status |
| 787 |
sarif_replayer::handle_run_obj (const json::object &run_obj) |
| 788 |
{ |
| 789 |
const json::object *tool_obj |
| 790 |
= get_required_property<json::object> (run_obj, |
| 791 |
property_spec_ref ("run", "tool", |
| 792 |
"3.14.6")); |
| 793 |
if (!tool_obj) |
| 794 |
return status::err_invalid_sarif; |
| 795 |
{ |
| 796 |
enum status err = handle_tool_obj (*tool_obj); |
| 797 |
if (err != status::ok) |
| 798 |
return err; |
| 799 |
} |
| 800 |
|
| 801 |
m_driver_obj |
| 802 |
= get_required_property<json::object> (*tool_obj, |
| 803 |
property_spec_ref ("tool", "driver", |
| 804 |
"3.18.2")); |
| 805 |
if (!m_driver_obj) |
| 806 |
return status::err_invalid_sarif; |
| 807 |
|
| 808 |
const property_spec_ref prop_artifacts ("run", "artifacts", "3.14.15"); |
| 809 |
m_artifacts_arr |
| 810 |
= get_optional_property<json::array> (run_obj, prop_artifacts); |
| 811 |
if (m_artifacts_arr) |
| 812 |
for (auto element : *m_artifacts_arr) |
| 813 |
{ |
| 814 |
if (const json::object *artifact_obj |
| 815 |
= require_object_for_element (*element, prop_artifacts)) |
| 816 |
handle_artifact_obj (*artifact_obj); |
| 817 |
else |
| 818 |
return status::err_invalid_sarif; |
| 819 |
} |
| 820 |
|
| 821 |
/* If present, run.results must be null or be an array. */ |
| 822 |
const property_spec_ref prop_results ("run", "results", "3.14.23"); |
| 823 |
if (const json::value *results = get_optional_property (run_obj, |
| 824 |
prop_results)) |
| 825 |
switch (results->get_kind ()) |
| 826 |
{ |
| 827 |
default: |
| 828 |
return report_invalid_sarif (*results, prop_results, |
| 829 |
"expected run.results to be" |
| 830 |
" %<null%> or an array"); |
| 831 |
|
| 832 |
case json::JSON_NULL: |
| 833 |
/* Nothing to do. */ |
| 834 |
break; |
| 835 |
case json::JSON_ARRAY: |
| 836 |
{ |
| 837 |
const json::array *results_arr = as_a <const json::array *> (results); |
| 838 |
for (auto element : *results_arr) |
| 839 |
{ |
| 840 |
const json::object *result_obj |
| 841 |
= require_object_for_element (*element, prop_results); |
| 842 |
if (!result_obj) |
| 843 |
return status::err_invalid_sarif; |
| 844 |
enum status s = handle_result_obj (*result_obj, |
| 845 |
run_obj, |
| 846 |
*tool_obj); |
| 847 |
if (s != status::ok) |
| 848 |
return s; |
| 849 |
} |
| 850 |
} |
| 851 |
break; |
| 852 |
} |
| 853 |
|
| 854 |
return status::ok; |
| 855 |
} |
| 856 |
|
| 857 |
/* Process an artifact object (SARIF v2.1.0 section 3.24). |
| 858 |
Create a libgdiagnostics::file for each artifact that has a uri, |
| 859 |
effectively prepopulating a cache with source language and contents. */ |
| 860 |
|
| 861 |
void |
| 862 |
sarif_replayer::handle_artifact_obj (const json::object &artifact_obj) |
| 863 |
{ |
| 864 |
const property_spec_ref location ("artifact", "location", "3.24.2"); |
| 865 |
auto artifact_loc_obj |
| 866 |
= get_optional_property<json::object> (artifact_obj, location); |
| 867 |
if (!artifact_loc_obj) |
| 868 |
return; |
| 869 |
|
| 870 |
// we should now have an artifactLocation object (§3.4) |
| 871 |
|
| 872 |
// 3.4.3 uri property |
| 873 |
const property_spec_ref prop_uri ("artifactLocation", "uri", "3.4.3"); |
| 874 |
auto artifact_loc_uri |
| 875 |
= get_optional_property<json::string> (*artifact_loc_obj, prop_uri); |
| 876 |
if (!artifact_loc_uri) |
| 877 |
return; |
| 878 |
|
| 879 |
const char *sarif_source_language = nullptr; |
| 880 |
const property_spec_ref prop_source_lang |
| 881 |
("artifact", "sourceLanguage", "3.24.10"); |
| 882 |
if (auto source_lang_jstr |
| 883 |
= get_optional_property<json::string> (artifact_obj, |
| 884 |
prop_source_lang)) |
| 885 |
sarif_source_language = source_lang_jstr->get_string (); |
| 886 |
|
| 887 |
/* Create the libgdiagnostics::file. */ |
| 888 |
auto file = m_output_mgr.new_file (artifact_loc_uri->get_string (), |
| 889 |
sarif_source_language); |
| 890 |
|
| 891 |
// Set contents, if available |
| 892 |
const property_spec_ref prop_contents |
| 893 |
("artifact", "contents", "3.24.8"); |
| 894 |
if (auto content_obj |
| 895 |
= get_optional_property<json::object> (artifact_obj, |
| 896 |
prop_contents)) |
| 897 |
{ |
| 898 |
// We should have an artifactContent object (§3.3) |
| 899 |
const property_spec_ref prop_text |
| 900 |
("artifactContent", "text", "3.3.2"); |
| 901 |
if (auto text_jstr |
| 902 |
= get_optional_property<json::string> (*content_obj, |
| 903 |
prop_text)) |
| 904 |
file.set_buffered_content (text_jstr->get_string (), |
| 905 |
text_jstr->get_length ()); |
| 906 |
} |
| 907 |
} |
| 908 |
|
| 909 |
/* Process a tool object (SARIF v2.1.0 section 3.18). */ |
| 910 |
|
| 911 |
enum status |
| 912 |
sarif_replayer::handle_tool_obj (const json::object &tool_obj) |
| 913 |
{ |
| 914 |
auto driver_obj |
| 915 |
= get_required_property<json::object> (tool_obj, |
| 916 |
property_spec_ref ("tool", "driver", |
| 917 |
"3.18.2")); |
| 918 |
if (!driver_obj) |
| 919 |
return status::err_invalid_sarif; |
| 920 |
|
| 921 |
const property_spec_ref name_prop ("toolComponent", "name", "3.19.8"); |
| 922 |
if (auto name_jstr = get_optional_property<json::string> (*driver_obj, |
| 923 |
name_prop)) |
| 924 |
m_output_mgr.set_tool_name (name_jstr->get_string ()); |
| 925 |
|
| 926 |
const property_spec_ref full_name_prop |
| 927 |
("toolComponent", "fullName", "3.19.9"); |
| 928 |
if (auto name_jstr = get_optional_property<json::string> (*driver_obj, |
| 929 |
full_name_prop)) |
| 930 |
m_output_mgr.set_full_name (name_jstr->get_string ()); |
| 931 |
|
| 932 |
const property_spec_ref version_prop ("toolComponent", "version", "3.19.13"); |
| 933 |
if (auto name_jstr = get_optional_property<json::string> (*driver_obj, |
| 934 |
version_prop)) |
| 935 |
m_output_mgr.set_version_string (name_jstr->get_string ()); |
| 936 |
|
| 937 |
const property_spec_ref |
| 938 |
info_uri_prop ("toolComponent", "informationUri", "3.19.17"); |
| 939 |
if (auto name_jstr = get_optional_property<json::string> (*driver_obj, |
| 940 |
info_uri_prop)) |
| 941 |
m_output_mgr.set_version_url (name_jstr->get_string ()); |
| 942 |
|
| 943 |
return status::ok; |
| 944 |
} |
| 945 |
|
| 946 |
/* Compare the value of JSON_STR to the values in VALUE_ARR. |
| 947 |
If found, return it in the result's m_val. |
| 948 |
Otherwise, complain using PROP and return status::invalid_sarif |
| 949 |
in results's m_err. */ |
| 950 |
|
| 951 |
template <typename ValueType> |
| 952 |
json::result<ValueType, enum status> |
| 953 |
sarif_replayer:: |
| 954 |
get_value_from_json_string (const json::string &json_str, |
| 955 |
const property_spec_ref &prop, |
| 956 |
const string_property_value<ValueType> *value_arr, |
| 957 |
size_t num_values) |
| 958 |
{ |
| 959 |
const char *str = json_str.get_string (); |
| 960 |
for (size_t i = 0; i < num_values; i++) |
| 961 |
if (strcmp (str, value_arr[i].m_string) == 0) |
| 962 |
return value_arr[i].m_value; |
| 963 |
return report_invalid_sarif (json_str, prop, |
| 964 |
"unrecognized value for %qs: %qs", |
| 965 |
prop.get_property_name (), |
| 966 |
str); |
| 967 |
} |
| 968 |
|
| 969 |
const property_spec_ref prop_result_level ("result", "level", "3.27.10"); |
| 970 |
|
| 971 |
/* Handle a value for result's "level" property (§3.27.10). |
| 972 |
Limitation: doesn't yet support "none". */ |
| 973 |
json::result<enum diagnostic_level, enum status> |
| 974 |
sarif_replayer::get_level_from_level_str (const json::string &level_str) |
| 975 |
{ |
| 976 |
if (strcmp (level_str.get_string (), "none") == 0) |
| 977 |
return report_unhandled_sarif (level_str, prop_result_level, |
| 978 |
"unable to handle value for %qs: %qs", |
| 979 |
prop_result_level.get_property_name (), |
| 980 |
level_str.get_string ()); |
| 981 |
|
| 982 |
const string_property_value<enum diagnostic_level> level_values[] |
| 983 |
= { {"warning", |
| 984 |
DIAGNOSTIC_LEVEL_WARNING}, |
| 985 |
{"error", |
| 986 |
DIAGNOSTIC_LEVEL_ERROR}, |
| 987 |
{"note", |
| 988 |
DIAGNOSTIC_LEVEL_NOTE} }; |
| 989 |
return get_value_from_json_string<enum diagnostic_level> |
| 990 |
(level_str, |
| 991 |
prop_result_level, |
| 992 |
level_values, ARRAY_SIZE (level_values)); |
| 993 |
} |
| 994 |
|
| 995 |
static void |
| 996 |
add_any_annotations (libgdiagnostics::diagnostic &diag, |
| 997 |
const std::vector<annotation> &annotations) |
| 998 |
{ |
| 999 |
for (auto &annotation : annotations) |
| 1000 |
if (annotation.m_label.get ()) |
| 1001 |
diag.add_location_with_label (annotation.m_phys_loc, |
| 1002 |
annotation.m_label.get ()); |
| 1003 |
else |
| 1004 |
diag.add_location (annotation.m_phys_loc); |
| 1005 |
} |
| 1006 |
|
| 1007 |
static bool |
| 1008 |
should_add_rule_p (const char *rule_id_str, const char *url) |
| 1009 |
{ |
| 1010 |
if (url) |
| 1011 |
return true; |
| 1012 |
|
| 1013 |
/* GCC's sarif output uses "error" for "ruleId", which is already |
| 1014 |
captured in the "level", so don't add a rule for that. */ |
| 1015 |
if (!strcmp (rule_id_str, "error")) |
| 1016 |
return false; |
| 1017 |
|
| 1018 |
return true; |
| 1019 |
} |
| 1020 |
|
| 1021 |
/* Process a result object (SARIF v2.1.0 section 3.27). |
| 1022 |
Known limitations: |
| 1023 |
- doesn't yet handle "ruleIndex" property (§3.27.6) |
| 1024 |
- doesn't yet handle "taxa" property (§3.27.8) |
| 1025 |
- handling of "level" property (§3.27.10) doesn't yet support the |
| 1026 |
full logic for when "level" is absent. |
| 1027 |
- doesn't yet support multithreaded flows (§3.36.3) |
| 1028 |
*/ |
| 1029 |
|
| 1030 |
#define PROP_result_ruleId \ |
| 1031 |
property_spec_ref ("result", "ruleId", "3.27.5") |
| 1032 |
|
| 1033 |
#define PROP_result_message \ |
| 1034 |
property_spec_ref ("result", "message", "3.27.11") |
| 1035 |
|
| 1036 |
enum status |
| 1037 |
sarif_replayer::handle_result_obj (const json::object &result_obj, |
| 1038 |
const json::object &run_obj, |
| 1039 |
const json::object &tool_obj) |
| 1040 |
{ |
| 1041 |
const json::object *rule_obj = nullptr; |
| 1042 |
const json::object *tool_component_obj = nullptr; |
| 1043 |
const json::string *rule_id |
| 1044 |
= get_optional_property<json::string> (result_obj, PROP_result_ruleId); |
| 1045 |
if (rule_id) |
| 1046 |
{ |
| 1047 |
rule_obj = lookup_rule_by_id_in_tool (rule_id->get_string (), |
| 1048 |
tool_obj, |
| 1049 |
tool_component_obj); |
| 1050 |
// TODO: error handling |
| 1051 |
} |
| 1052 |
|
| 1053 |
enum diagnostic_level level = DIAGNOSTIC_LEVEL_WARNING; |
| 1054 |
if (auto level_str |
| 1055 |
= get_optional_property<json::string> (result_obj, |
| 1056 |
prop_result_level)) |
| 1057 |
{ |
| 1058 |
auto result = get_level_from_level_str (*level_str); |
| 1059 |
if (result.m_err != status::ok) |
| 1060 |
return result.m_err; |
| 1061 |
level = result.m_val; |
| 1062 |
} |
| 1063 |
|
| 1064 |
// §3.27.11 "message" property |
| 1065 |
label_text text; |
| 1066 |
if (auto message_obj |
| 1067 |
= get_optional_property<json::object> (result_obj, PROP_result_message)) |
| 1068 |
text = make_plain_text_within_result_message (nullptr, // TODO: tool_component_obj, |
| 1069 |
*message_obj, |
| 1070 |
rule_obj); |
| 1071 |
if (!text.get ()) |
| 1072 |
return status::err_invalid_sarif; |
| 1073 |
|
| 1074 |
// §3.27.12 "locations" property |
| 1075 |
libgdiagnostics::physical_location physical_loc; |
| 1076 |
libgdiagnostics::logical_location logical_loc; |
| 1077 |
const property_spec_ref locations_prop ("result", "locations", "3.27.12"); |
| 1078 |
const json::array *locations_arr |
| 1079 |
= get_required_property<json::array> (result_obj, locations_prop); |
| 1080 |
if (!locations_arr) |
| 1081 |
return status::err_invalid_sarif; |
| 1082 |
std::vector<annotation> annotations; |
| 1083 |
if (locations_arr->length () > 0) |
| 1084 |
{ |
| 1085 |
/* Only look at the first, if there's more than one. */ |
| 1086 |
// location objects (§3.28) |
| 1087 |
const json::object *location_obj |
| 1088 |
= require_object_for_element (*locations_arr->get (0), locations_prop); |
| 1089 |
if (!location_obj) |
| 1090 |
return status::err_invalid_sarif; |
| 1091 |
enum status s = handle_location_object (*location_obj, run_obj, |
| 1092 |
physical_loc, |
| 1093 |
logical_loc, |
| 1094 |
&annotations); |
| 1095 |
if (s != status::ok) |
| 1096 |
return s; |
| 1097 |
} |
| 1098 |
|
| 1099 |
// §3.27.18 "codeFlows" property |
| 1100 |
libgdiagnostics::execution_path path; |
| 1101 |
const property_spec_ref code_flows ("result", "codeFlows", "3.27.18"); |
| 1102 |
if (auto code_flows_arr = get_optional_property<json::array> (result_obj, |
| 1103 |
code_flows)) |
| 1104 |
{ |
| 1105 |
// TODO: what if more than one? |
| 1106 |
if (code_flows_arr->length () == 1) |
| 1107 |
{ |
| 1108 |
const json::object *code_flow_obj |
| 1109 |
= require_object_for_element (*code_flows_arr->get (0), code_flows); |
| 1110 |
if (!code_flow_obj) |
| 1111 |
return status::err_invalid_sarif; |
| 1112 |
|
| 1113 |
const property_spec_ref prop_thread_flows |
| 1114 |
("result", "threadFlows", "3.36.3"); |
| 1115 |
if (auto thread_flows_arr |
| 1116 |
= get_optional_property<json::array> (*code_flow_obj, |
| 1117 |
prop_thread_flows)) |
| 1118 |
{ |
| 1119 |
if (thread_flows_arr->length () == 1) |
| 1120 |
{ |
| 1121 |
const json::object *thread_flow_obj |
| 1122 |
= require_object_for_element (*thread_flows_arr->get (0), |
| 1123 |
prop_thread_flows); |
| 1124 |
if (!thread_flow_obj) |
| 1125 |
return status::err_invalid_sarif; |
| 1126 |
handle_thread_flow_object (*thread_flow_obj, run_obj, path); |
| 1127 |
} |
| 1128 |
} |
| 1129 |
} |
| 1130 |
} |
| 1131 |
|
| 1132 |
libgdiagnostics::group g (m_output_mgr); |
| 1133 |
auto err (m_output_mgr.begin_diagnostic (level)); |
| 1134 |
if (rule_id) |
| 1135 |
{ |
| 1136 |
const char *url = nullptr; |
| 1137 |
if (rule_obj) |
| 1138 |
{ |
| 1139 |
/* rule_obj should be a reportingDescriptor object (3.49). |
| 1140 |
Get any §3.49.12 helpUri property. */ |
| 1141 |
const property_spec_ref prop_help_uri |
| 1142 |
("reportingDescriptor", "helpUri", "3.49.12"); |
| 1143 |
if (auto url_val = get_optional_property<json::string>(*rule_obj, |
| 1144 |
prop_help_uri)) |
| 1145 |
url = url_val->get_string (); |
| 1146 |
} |
| 1147 |
|
| 1148 |
const char *rule_id_str = rule_id->get_string (); |
| 1149 |
if (should_add_rule_p (rule_id_str, url)) |
| 1150 |
err.add_rule (rule_id_str, url); |
| 1151 |
} |
| 1152 |
err.set_location (physical_loc); |
| 1153 |
err.set_logical_location (logical_loc); |
| 1154 |
add_any_annotations (err, annotations); |
| 1155 |
if (path.m_inner) |
| 1156 |
err.take_execution_path (std::move (path)); |
| 1157 |
|
| 1158 |
// §3.27.22 relatedLocations property |
| 1159 |
std::vector<std::pair<libgdiagnostics::diagnostic, label_text>> notes; |
| 1160 |
const property_spec_ref prop_related_locations |
| 1161 |
("result", "relatedLocations", "3.27.22"); |
| 1162 |
if (auto related_locations_arr |
| 1163 |
= get_optional_property<json::array> (result_obj, |
| 1164 |
prop_related_locations)) |
| 1165 |
{ |
| 1166 |
for (auto rel_loc : *related_locations_arr) |
| 1167 |
{ |
| 1168 |
libgdiagnostics::physical_location physical_loc; |
| 1169 |
libgdiagnostics::logical_location logical_loc; |
| 1170 |
const json::object *location_obj |
| 1171 |
= require_object_for_element (*rel_loc, |
| 1172 |
prop_related_locations); |
| 1173 |
if (!location_obj) |
| 1174 |
return status::err_invalid_sarif; |
| 1175 |
std::vector<annotation> annotations; |
| 1176 |
enum status s = handle_location_object (*location_obj, run_obj, |
| 1177 |
physical_loc, |
| 1178 |
logical_loc, |
| 1179 |
&annotations); |
| 1180 |
if (s != status::ok) |
| 1181 |
return s; |
| 1182 |
|
| 1183 |
// §3.28.5 message property |
| 1184 |
const property_spec_ref prop_message |
| 1185 |
("location", "message", "3.28.5"); |
| 1186 |
if (auto message_obj |
| 1187 |
= get_optional_property<json::object> (*location_obj, |
| 1188 |
prop_message)) |
| 1189 |
{ |
| 1190 |
/* Treat related locations with a message as a "note". */ |
| 1191 |
label_text text |
| 1192 |
(make_plain_text_within_result_message |
| 1193 |
(tool_component_obj, |
| 1194 |
*message_obj, |
| 1195 |
rule_obj)); |
| 1196 |
if (!text.get ()) |
| 1197 |
return status::err_invalid_sarif; |
| 1198 |
auto note (m_output_mgr.begin_diagnostic (DIAGNOSTIC_LEVEL_NOTE)); |
| 1199 |
note.set_location (physical_loc); |
| 1200 |
note.set_logical_location (logical_loc); |
| 1201 |
add_any_annotations (note, annotations); |
| 1202 |
notes.push_back ({std::move (note), std::move (text)}); |
| 1203 |
} |
| 1204 |
else |
| 1205 |
{ |
| 1206 |
/* Treat related locations without a message as a secondary |
| 1207 |
range. */ |
| 1208 |
if (physical_loc.m_inner) |
| 1209 |
err.add_location (physical_loc); |
| 1210 |
} |
| 1211 |
} |
| 1212 |
} |
| 1213 |
|
| 1214 |
// §3.27.30 "fixes" property |
| 1215 |
const property_spec_ref prop_fixes ("result", "fixes", "3.27.30"); |
| 1216 |
if (auto fixes_arr |
| 1217 |
= get_optional_property<json::array> (result_obj, prop_fixes)) |
| 1218 |
{ |
| 1219 |
// We only support a single fix |
| 1220 |
if (fixes_arr->length () == 1) |
| 1221 |
if (auto fix_obj = require_object (*fixes_arr->get (0), prop_fixes)) |
| 1222 |
handle_fix_object (err, *fix_obj); |
| 1223 |
} |
| 1224 |
|
| 1225 |
err.finish ("%s", text.get ()); |
| 1226 |
|
| 1227 |
// Flush any notes |
| 1228 |
for (auto &iter : notes) |
| 1229 |
{ |
| 1230 |
auto ¬e = iter.first; |
| 1231 |
auto &text = iter.second; |
| 1232 |
note.finish ("%s", text.get ()); |
| 1233 |
} |
| 1234 |
|
| 1235 |
return status::ok; |
| 1236 |
} |
| 1237 |
|
| 1238 |
/* If ITER_SRC starts with a placeholder as per §3.11.5, advance ITER_SRC |
| 1239 |
to immediately beyond the placeholder, write to *OUT_ARG_IDX, and |
| 1240 |
return true. |
| 1241 |
|
| 1242 |
Otherwise, leave ITER_SRC untouched and return false. */ |
| 1243 |
|
| 1244 |
static bool |
| 1245 |
maybe_consume_placeholder (const char *&iter_src, unsigned *out_arg_idx) |
| 1246 |
{ |
| 1247 |
if (*iter_src != '{') |
| 1248 |
return false; |
| 1249 |
const char *first_digit = iter_src + 1; |
| 1250 |
const char *iter_digit = first_digit; |
| 1251 |
while (char ch = *iter_digit) |
| 1252 |
switch (ch) |
| 1253 |
{ |
| 1254 |
default: |
| 1255 |
return false; |
| 1256 |
|
| 1257 |
case '}': |
| 1258 |
if (iter_digit == first_digit) |
| 1259 |
{ |
| 1260 |
/* No digits, we simply have "{}" which is not a placeholder |
| 1261 |
(and malformed: the braces should have been escaped). */ |
| 1262 |
return false; |
| 1263 |
} |
| 1264 |
*out_arg_idx = atoi (first_digit); |
| 1265 |
iter_src = iter_digit + 1; |
| 1266 |
return true; |
| 1267 |
|
| 1268 |
case '0': case '1': case '2': case '3': case '4': |
| 1269 |
case '5': case '6': case '7': case '8': case '9': |
| 1270 |
/* TODO: what about multiple leading zeroes? */ |
| 1271 |
iter_digit++; |
| 1272 |
continue; |
| 1273 |
} |
| 1274 |
return false; |
| 1275 |
} |
| 1276 |
|
| 1277 |
struct embedded_link |
| 1278 |
{ |
| 1279 |
std::string text; |
| 1280 |
std::string destination; |
| 1281 |
}; |
| 1282 |
|
| 1283 |
/* If ITER_SRC starts with an embedded link as per §3.11.6, advance ITER_SRC |
| 1284 |
to immediately beyond the link, and return the link. |
| 1285 |
|
| 1286 |
Otherwise, leave ITER_SRC untouched and return nullptr. */ |
| 1287 |
|
| 1288 |
static std::unique_ptr<embedded_link> |
| 1289 |
maybe_consume_embedded_link (const char *&iter_src) |
| 1290 |
{ |
| 1291 |
if (*iter_src != '[') |
| 1292 |
return nullptr; |
| 1293 |
|
| 1294 |
/* This *might* be an embedded link. |
| 1295 |
See §3.11.6 ("Messages with embedded links") and |
| 1296 |
https://github.com/oasis-tcs/sarif-spec/issues/657 */ |
| 1297 |
|
| 1298 |
/* embedded link = "[", link text, "](", link destination, ")"; */ |
| 1299 |
|
| 1300 |
embedded_link result; |
| 1301 |
|
| 1302 |
/* Try to get the link text. */ |
| 1303 |
const char *iter = iter_src + 1; |
| 1304 |
while (char ch = *(iter++)) |
| 1305 |
{ |
| 1306 |
if (ch == '\\') |
| 1307 |
{ |
| 1308 |
char next_ch = *iter; |
| 1309 |
switch (next_ch) |
| 1310 |
{ |
| 1311 |
case '\\': |
| 1312 |
case '[': |
| 1313 |
case ']': |
| 1314 |
/* escaped link character = "\" | "[" | "]"; */ |
| 1315 |
result.text += next_ch; |
| 1316 |
iter++; |
| 1317 |
continue; |
| 1318 |
|
| 1319 |
default: |
| 1320 |
/* Malformed link text; assume this is not an |
| 1321 |
embedded link. */ |
| 1322 |
return nullptr; |
| 1323 |
} |
| 1324 |
} |
| 1325 |
else if (ch == ']') |
| 1326 |
/* End of link text. */ |
| 1327 |
break; |
| 1328 |
else |
| 1329 |
result.text += ch; |
| 1330 |
} |
| 1331 |
|
| 1332 |
if (*iter++ != '(') |
| 1333 |
return nullptr; |
| 1334 |
|
| 1335 |
/* Try to get the link destination. */ |
| 1336 |
while (1) |
| 1337 |
{ |
| 1338 |
char ch = *(iter++); |
| 1339 |
if (ch == '\0') |
| 1340 |
{ |
| 1341 |
/* String ended before terminating ')'. |
| 1342 |
Assume this is not an embedded link. */ |
| 1343 |
return nullptr; |
| 1344 |
} |
| 1345 |
else if (ch == ')') |
| 1346 |
/* Terminator. */ |
| 1347 |
break; |
| 1348 |
else |
| 1349 |
result.destination += ch; |
| 1350 |
} |
| 1351 |
|
| 1352 |
iter_src = iter; |
| 1353 |
return ::make_unique<embedded_link> (std::move (result)); |
| 1354 |
} |
| 1355 |
|
| 1356 |
/* Lookup the plain text string within a result.message (§3.27.11), |
| 1357 |
and substitute for any placeholders (§3.11.5) and handle any |
| 1358 |
embedded links (§3.11.6). |
| 1359 |
|
| 1360 |
Limitations: |
| 1361 |
- we don't preserve destinations within embedded links |
| 1362 |
|
| 1363 |
MESSAGE_OBJ is "theMessage" |
| 1364 |
RULE_OBJ is "theRule". */ |
| 1365 |
|
| 1366 |
label_text |
| 1367 |
sarif_replayer:: |
| 1368 |
make_plain_text_within_result_message (const json::object *tool_component_obj, |
| 1369 |
const json::object &message_obj, |
| 1370 |
const json::object *rule_obj) |
| 1371 |
{ |
| 1372 |
const json::string *js_str = nullptr; |
| 1373 |
const char *original_text |
| 1374 |
= lookup_plain_text_within_result_message (tool_component_obj, |
| 1375 |
message_obj, |
| 1376 |
rule_obj, |
| 1377 |
js_str); |
| 1378 |
if (!original_text) |
| 1379 |
return label_text::borrow (nullptr); |
| 1380 |
|
| 1381 |
gcc_assert (js_str); |
| 1382 |
|
| 1383 |
/* Look up any arguments for substituting into placeholders. */ |
| 1384 |
const property_spec_ref arguments_prop ("message", "arguments", "3.11.11"); |
| 1385 |
const json::array *arguments |
| 1386 |
= get_optional_property<json::array> (message_obj, arguments_prop); |
| 1387 |
|
| 1388 |
/* Duplicate original_text, substituting any placeholders. */ |
| 1389 |
std::string accum; |
| 1390 |
|
| 1391 |
const char *iter_src = original_text; |
| 1392 |
while (char ch = *iter_src) |
| 1393 |
{ |
| 1394 |
unsigned arg_idx; |
| 1395 |
if (maybe_consume_placeholder (iter_src, &arg_idx)) |
| 1396 |
{ |
| 1397 |
if (!arguments) |
| 1398 |
{ |
| 1399 |
report_invalid_sarif |
| 1400 |
(message_obj, arguments_prop, |
| 1401 |
"message string contains placeholder %<{%i}%>" |
| 1402 |
" but message object has no %qs property", |
| 1403 |
(int)arg_idx, |
| 1404 |
arguments_prop.get_property_name ()); |
| 1405 |
return label_text::borrow (nullptr); |
| 1406 |
} |
| 1407 |
if (arg_idx >= arguments->length ()) |
| 1408 |
{ |
| 1409 |
report_invalid_sarif |
| 1410 |
(message_obj, arguments_prop, |
| 1411 |
"not enough strings in %qs array for" |
| 1412 |
" placeholder %<{%i}%>", |
| 1413 |
arguments_prop.get_property_name (), |
| 1414 |
(int)arg_idx); |
| 1415 |
// TODO: might be nice to add a note showing the args |
| 1416 |
return label_text::borrow (nullptr); |
| 1417 |
} |
| 1418 |
auto replacement_jstr |
| 1419 |
= require_string (*arguments->get (arg_idx), arguments_prop); |
| 1420 |
if (!replacement_jstr) |
| 1421 |
return label_text::borrow (nullptr); |
| 1422 |
accum += replacement_jstr->get_string (); |
| 1423 |
} |
| 1424 |
else if (ch == '{' || ch == '}') |
| 1425 |
{ |
| 1426 |
/* '{' and '}' are escaped by repeating them. */ |
| 1427 |
if (iter_src[1] == ch) |
| 1428 |
{ |
| 1429 |
accum += ch; |
| 1430 |
iter_src += 2; |
| 1431 |
} |
| 1432 |
else |
| 1433 |
{ |
| 1434 |
const spec_ref msgs_with_placeholders ("3.11.5"); |
| 1435 |
gcc_assert (js_str); |
| 1436 |
report_invalid_sarif (*js_str, msgs_with_placeholders, |
| 1437 |
"unescaped '%c' within message string", |
| 1438 |
ch); |
| 1439 |
return label_text::borrow (nullptr); |
| 1440 |
} |
| 1441 |
} |
| 1442 |
else if (auto link = maybe_consume_embedded_link (iter_src)) |
| 1443 |
{ |
| 1444 |
accum += link->text; |
| 1445 |
/* TODO: use the destination. */ |
| 1446 |
/* TODO: potentially could try to convert |
| 1447 |
intra-sarif links into event ids. */ |
| 1448 |
} |
| 1449 |
else |
| 1450 |
{ |
| 1451 |
accum += ch; |
| 1452 |
iter_src++; |
| 1453 |
} |
| 1454 |
} |
| 1455 |
|
| 1456 |
return label_text::take (xstrdup (accum.c_str ())); |
| 1457 |
} |
| 1458 |
|
| 1459 |
/* Handle a value that should be a multiformatMessageString object (§3.12). |
| 1460 |
Complain using prop if MFMS_VAL is not an object. |
| 1461 |
Return get the "text" value (or nullptr, and complain). |
| 1462 |
If the result is non-null, write the json::string that was actually used |
| 1463 |
to OUT_JS_STR. */ |
| 1464 |
|
| 1465 |
const char * |
| 1466 |
sarif_replayer::get_plain_text_from_mfms (json::value &mfms_val, |
| 1467 |
const property_spec_ref &prop, |
| 1468 |
const json::string *&out_js_str) |
| 1469 |
{ |
| 1470 |
auto mfms_obj = require_object (mfms_val, prop); |
| 1471 |
if (!mfms_obj) |
| 1472 |
return nullptr; |
| 1473 |
|
| 1474 |
const property_spec_ref text_prop |
| 1475 |
("multiformatMessageString", "text", "3.12.3"); |
| 1476 |
auto text_jstr = get_required_property<json::string> (*mfms_obj, text_prop); |
| 1477 |
if (!text_jstr) |
| 1478 |
return nullptr; |
| 1479 |
out_js_str = text_jstr; |
| 1480 |
return text_jstr->get_string (); |
| 1481 |
} |
| 1482 |
|
| 1483 |
#define PROP_message_text \ |
| 1484 |
property_spec_ref ("message", "text", "3.11.8") |
| 1485 |
|
| 1486 |
#define PROP_message_id \ |
| 1487 |
property_spec_ref ("message", "id", "3.11.10") |
| 1488 |
|
| 1489 |
/* Implement the message string lookup algorithm from |
| 1490 |
SARIF v2.1.0 section 3.11.7, for the case where theMessage |
| 1491 |
is the value of result.message (§3.27.11). |
| 1492 |
|
| 1493 |
MESSAGE_OBJ is "theMessage" |
| 1494 |
RULE_OBJ is "theRule". |
| 1495 |
|
| 1496 |
If the result is non-null, write the json::string that was actually used |
| 1497 |
to OUT_JS_STR. */ |
| 1498 |
|
| 1499 |
const char * |
| 1500 |
sarif_replayer:: |
| 1501 |
lookup_plain_text_within_result_message (const json::object *tool_component_obj, |
| 1502 |
const json::object &message_obj, |
| 1503 |
const json::object *rule_obj, |
| 1504 |
const json::string *&out_js_str) |
| 1505 |
{ |
| 1506 |
// rule_obj can be NULL |
| 1507 |
|
| 1508 |
/* IF theMessage.text is present and the desired language is theRun.language THEN |
| 1509 |
Use the text or markdown property of theMessage as appropriate. */ |
| 1510 |
if (const json::string *str |
| 1511 |
= get_optional_property<json::string> (message_obj, PROP_message_text)) |
| 1512 |
{ |
| 1513 |
// TODO: check language |
| 1514 |
out_js_str = str; |
| 1515 |
return str->get_string (); |
| 1516 |
} |
| 1517 |
|
| 1518 |
if (rule_obj) |
| 1519 |
if (auto message_id_jstr |
| 1520 |
= get_optional_property<json::string> (message_obj, PROP_message_id)) |
| 1521 |
{ |
| 1522 |
const char *message_id = message_id_jstr->get_string (); |
| 1523 |
const property_spec_ref message_strings |
| 1524 |
("reportingDescriptor", "messageStrings", "3.49.11"); |
| 1525 |
if (auto message_strings_obj |
| 1526 |
= get_optional_property<json::object> (*rule_obj, |
| 1527 |
message_strings)) |
| 1528 |
if (json::value *mfms = message_strings_obj->get (message_id)) |
| 1529 |
return get_plain_text_from_mfms (*mfms, message_strings, out_js_str); |
| 1530 |
|
| 1531 |
/* Look up by theMessage.id within theComponent.globalMessageStrings |
| 1532 |
(§3.19.22). */ |
| 1533 |
if (tool_component_obj) |
| 1534 |
{ |
| 1535 |
const property_spec_ref prop_gms |
| 1536 |
("toolComponent", "globalMessageStrings", "3.19.22"); |
| 1537 |
if (auto global_message_strings |
| 1538 |
= get_optional_property<json::object> (*tool_component_obj, |
| 1539 |
prop_gms)) |
| 1540 |
if (auto mfms = global_message_strings->get (message_id)) |
| 1541 |
return get_plain_text_from_mfms (*mfms, prop_gms, out_js_str); |
| 1542 |
} |
| 1543 |
} |
| 1544 |
|
| 1545 |
/* Failure. */ |
| 1546 |
report_invalid_sarif (message_obj, spec_ref ("3.11.7"), |
| 1547 |
"could not find string for %<message%> object"); |
| 1548 |
return nullptr; |
| 1549 |
} |
| 1550 |
|
| 1551 |
/* Populate OUT for THREAD_FLOW_OBJ, a |
| 1552 |
SARIF threadFlow object (section 3.37). */ |
| 1553 |
|
| 1554 |
enum status |
| 1555 |
sarif_replayer::handle_thread_flow_object (const json::object &thread_flow_obj, |
| 1556 |
const json::object &run_obj, |
| 1557 |
libgdiagnostics::execution_path &out) |
| 1558 |
{ |
| 1559 |
const property_spec_ref locations ("threadFlow", "locations", "3.37.6"); |
| 1560 |
const json::array *locations_arr |
| 1561 |
= get_required_property<json::array> (thread_flow_obj, locations); |
| 1562 |
if (!locations_arr) |
| 1563 |
return status::err_invalid_sarif; |
| 1564 |
|
| 1565 |
out = m_output_mgr.new_execution_path (); |
| 1566 |
for (auto location : *locations_arr) |
| 1567 |
{ |
| 1568 |
/* threadFlowLocation object (§3.38). */ |
| 1569 |
const json::object *tflow_loc_obj |
| 1570 |
= require_object_for_element (*location, locations); |
| 1571 |
if (!tflow_loc_obj) |
| 1572 |
return status::err_invalid_sarif; |
| 1573 |
handle_thread_flow_location_object (*tflow_loc_obj, run_obj, out); |
| 1574 |
} |
| 1575 |
|
| 1576 |
return status::ok; |
| 1577 |
} |
| 1578 |
|
| 1579 |
/* "threadFlowLocation" object (§3.38). |
| 1580 |
Attempt to add an event for TFLOW_LOC_OBJ to PATH. */ |
| 1581 |
|
| 1582 |
enum status |
| 1583 |
sarif_replayer:: |
| 1584 |
handle_thread_flow_location_object (const json::object &tflow_loc_obj, |
| 1585 |
const json::object &run_obj, |
| 1586 |
libgdiagnostics::execution_path &path) |
| 1587 |
{ |
| 1588 |
libgdiagnostics::physical_location physical_loc; |
| 1589 |
libgdiagnostics::logical_location logical_loc; |
| 1590 |
label_text message; |
| 1591 |
int stack_depth = 0; |
| 1592 |
|
| 1593 |
const property_spec_ref location_prop |
| 1594 |
("threadFlowLocation", "location", "3.38.3"); |
| 1595 |
if (auto location_obj = get_optional_property<json::object> (tflow_loc_obj, |
| 1596 |
location_prop)) |
| 1597 |
{ |
| 1598 |
/* location object (§3.28). */ |
| 1599 |
enum status s = handle_location_object (*location_obj, run_obj, |
| 1600 |
physical_loc, logical_loc, |
| 1601 |
nullptr); |
| 1602 |
if (s != status::ok) |
| 1603 |
return s; |
| 1604 |
|
| 1605 |
/* Get any message from here. */ |
| 1606 |
const property_spec_ref location_message |
| 1607 |
("location", "message", "3.28.5"); |
| 1608 |
if (auto message_obj |
| 1609 |
= get_optional_property<json::object> (*location_obj, |
| 1610 |
location_message)) |
| 1611 |
{ |
| 1612 |
message = make_plain_text_within_result_message |
| 1613 |
(nullptr, |
| 1614 |
*message_obj, |
| 1615 |
nullptr/* TODO. */); |
| 1616 |
} |
| 1617 |
} |
| 1618 |
|
| 1619 |
// §3.38.8 "kinds" property |
| 1620 |
const property_spec_ref kinds ("threadFlowLocation", "kinds", "3.38.8"); |
| 1621 |
if (auto kinds_arr |
| 1622 |
= get_optional_property<json::array> (tflow_loc_obj, kinds)) |
| 1623 |
{ |
| 1624 |
std::vector<const char *> kind_strs; |
| 1625 |
for (auto iter : *kinds_arr) |
| 1626 |
{ |
| 1627 |
const json::string *kind_str = dyn_cast <const json::string *> (iter); |
| 1628 |
if (!kind_str) |
| 1629 |
{ |
| 1630 |
} |
| 1631 |
kind_strs.push_back (kind_str->get_string ()); |
| 1632 |
// TODO: handle meaning? |
| 1633 |
/* TOOD: probably just want to add sarif kinds to |
| 1634 |
the libgdiagnostics event, and have libgdiagnostics |
| 1635 |
turn that back into a "meaning". */ |
| 1636 |
} |
| 1637 |
} |
| 1638 |
|
| 1639 |
/* nestingLevel property (§3.38.10). */ |
| 1640 |
const property_spec_ref nesting_level |
| 1641 |
("threadFlowLocation", "nestingLevel", "3.38.10"); |
| 1642 |
if (auto nesting_level_jv |
| 1643 |
= get_optional_property<json::integer_number> (tflow_loc_obj, |
| 1644 |
nesting_level)) |
| 1645 |
{ |
| 1646 |
stack_depth = nesting_level_jv->get (); |
| 1647 |
if (stack_depth < 0) |
| 1648 |
{ |
| 1649 |
return report_invalid_sarif (tflow_loc_obj, nesting_level, |
| 1650 |
"expected a non-negative integer"); |
| 1651 |
} |
| 1652 |
} |
| 1653 |
|
| 1654 |
if (message.get ()) |
| 1655 |
path.add_event (physical_loc, |
| 1656 |
logical_loc, |
| 1657 |
stack_depth, |
| 1658 |
"%s", message.get ()); |
| 1659 |
else |
| 1660 |
path.add_event (physical_loc, |
| 1661 |
logical_loc, |
| 1662 |
stack_depth, |
| 1663 |
""); |
| 1664 |
|
| 1665 |
return status::ok; |
| 1666 |
} |
| 1667 |
|
| 1668 |
/* Handle LOCATION_OBJ, a "location" (§3.28). */ |
| 1669 |
|
| 1670 |
enum status |
| 1671 |
sarif_replayer:: |
| 1672 |
handle_location_object (const json::object &location_obj, |
| 1673 |
const json::object &run_obj, |
| 1674 |
libgdiagnostics::physical_location &out_physical_loc, |
| 1675 |
libgdiagnostics::logical_location &out_logical_loc, |
| 1676 |
std::vector<annotation> *out_annotations) |
| 1677 |
{ |
| 1678 |
// §3.28.3 "physicalLocation" property |
| 1679 |
{ |
| 1680 |
const property_spec_ref physical_location_prop |
| 1681 |
("location", "physicalLocation", "3.28.3"); |
| 1682 |
if (const json::object *phys_loc_obj |
| 1683 |
= get_optional_property<json::object> (location_obj, |
| 1684 |
physical_location_prop)) |
| 1685 |
{ |
| 1686 |
enum status s = handle_physical_location_object (*phys_loc_obj, |
| 1687 |
out_physical_loc); |
| 1688 |
if (s!= status::ok) |
| 1689 |
return s; |
| 1690 |
} |
| 1691 |
} |
| 1692 |
|
| 1693 |
// §3.28.4 "logicalLocations" property |
| 1694 |
{ |
| 1695 |
const property_spec_ref logical_locations_prop |
| 1696 |
("location", "logicalLocations", "3.28.4"); |
| 1697 |
if (const json::array *logical_loc_arr |
| 1698 |
= get_optional_property<json::array> (location_obj, |
| 1699 |
logical_locations_prop)) |
| 1700 |
if (logical_loc_arr->length () > 0) |
| 1701 |
{ |
| 1702 |
/* Only look at the first, if there's more than one. */ |
| 1703 |
const json::object *logical_loc_obj |
| 1704 |
= require_object_for_element (*logical_loc_arr->get (0), |
| 1705 |
logical_locations_prop); |
| 1706 |
if (!logical_loc_obj) |
| 1707 |
return status::err_invalid_sarif; |
| 1708 |
enum status s = handle_logical_location_object (*logical_loc_obj, |
| 1709 |
&run_obj, |
| 1710 |
out_logical_loc); |
| 1711 |
if (s != status::ok) |
| 1712 |
return s; |
| 1713 |
} |
| 1714 |
} |
| 1715 |
|
| 1716 |
// §3.28.6 "annotations" property |
| 1717 |
{ |
| 1718 |
const property_spec_ref annotations_prop |
| 1719 |
("location", "annotations", "3.28.6"); |
| 1720 |
if (const json::array *annotations_arr |
| 1721 |
= get_optional_property<json::array> (location_obj, |
| 1722 |
annotations_prop)) |
| 1723 |
for (auto element : *annotations_arr) |
| 1724 |
{ |
| 1725 |
const json::object *annotation_obj |
| 1726 |
= require_object_for_element (*element, annotations_prop); |
| 1727 |
if (!annotation_obj) |
| 1728 |
return status::err_invalid_sarif; |
| 1729 |
libgdiagnostics::file file = out_physical_loc.get_file (); |
| 1730 |
if (!file.m_inner) |
| 1731 |
return report_invalid_sarif |
| 1732 |
(*annotation_obj, annotations_prop, |
| 1733 |
"cannot find artifact for %qs property", |
| 1734 |
annotations_prop.get_property_name ()); |
| 1735 |
libgdiagnostics::physical_location phys_loc; |
| 1736 |
enum status s = handle_region_object (*annotation_obj, |
| 1737 |
file, |
| 1738 |
phys_loc); |
| 1739 |
if (s != status::ok) |
| 1740 |
return s; |
| 1741 |
|
| 1742 |
label_text label; |
| 1743 |
|
| 1744 |
// §3.30.14 message property |
| 1745 |
{ |
| 1746 |
const property_spec_ref message_prop |
| 1747 |
("region", "message", "3.30.14"); |
| 1748 |
|
| 1749 |
if (const json::object *message_obj |
| 1750 |
= get_optional_property<json::object> (*annotation_obj, |
| 1751 |
message_prop)) |
| 1752 |
label = make_plain_text_within_result_message (nullptr, |
| 1753 |
*message_obj, |
| 1754 |
nullptr); |
| 1755 |
} |
| 1756 |
|
| 1757 |
if (out_annotations) |
| 1758 |
out_annotations->push_back ({phys_loc, std::move (label)}); |
| 1759 |
} |
| 1760 |
} |
| 1761 |
|
| 1762 |
return status::ok; |
| 1763 |
} |
| 1764 |
|
| 1765 |
/* Handle PHYS_LOC_OBJ, a "physicalLocation" object (§3.29). |
| 1766 |
Limitations: |
| 1767 |
- we don't yet support the "contextRegion" property (§3.29.5) */ |
| 1768 |
|
| 1769 |
enum status |
| 1770 |
sarif_replayer:: |
| 1771 |
handle_physical_location_object (const json::object &phys_loc_obj, |
| 1772 |
libgdiagnostics::physical_location &out) |
| 1773 |
{ |
| 1774 |
libgdiagnostics::file artifact_file; |
| 1775 |
|
| 1776 |
// §3.29.3 "artifactLocation" property |
| 1777 |
const property_spec_ref artifact_location_prop |
| 1778 |
("physicalLocation", "artifactLocation", "3.29.3"); |
| 1779 |
if (const json::object *artifact_loc_obj |
| 1780 |
= get_optional_property<json::object> (phys_loc_obj, |
| 1781 |
artifact_location_prop)) |
| 1782 |
{ |
| 1783 |
enum status s = handle_artifact_location_object (*artifact_loc_obj, |
| 1784 |
artifact_file); |
| 1785 |
if (s != status::ok) |
| 1786 |
return s; |
| 1787 |
} |
| 1788 |
|
| 1789 |
// §3.29.6 "address" property |
| 1790 |
const property_spec_ref artifact_address_prop |
| 1791 |
("physicalLocation", "address", "3.29.6"); |
| 1792 |
|
| 1793 |
if (!artifact_file.m_inner) |
| 1794 |
{ |
| 1795 |
const object_spec_ref constraints ("physicalLocation", "3.29.2"); |
| 1796 |
return report_invalid_sarif_at_least_one_of (phys_loc_obj, |
| 1797 |
constraints, |
| 1798 |
artifact_address_prop, |
| 1799 |
artifact_location_prop); |
| 1800 |
} |
| 1801 |
|
| 1802 |
//3.29.4 region property |
| 1803 |
const property_spec_ref region_prop ("physicalLocation", "region", "3.29.4"); |
| 1804 |
if (const json::object *region_obj |
| 1805 |
= get_optional_property<json::object> (phys_loc_obj, region_prop)) |
| 1806 |
{ |
| 1807 |
enum status s |
| 1808 |
= handle_region_object (*region_obj, artifact_file, out); |
| 1809 |
if (s != status::ok) |
| 1810 |
return s; |
| 1811 |
// TODO: |
| 1812 |
} |
| 1813 |
|
| 1814 |
return status::ok; |
| 1815 |
} |
| 1816 |
|
| 1817 |
/* Handle ARTIFACT_LOC, an "artifactLocation" object (§3.4). */ |
| 1818 |
|
| 1819 |
enum status |
| 1820 |
sarif_replayer::handle_artifact_location_object (const json::object &artifact_loc, |
| 1821 |
libgdiagnostics::file &out) |
| 1822 |
{ |
| 1823 |
// §3.4.3 "uri" property |
| 1824 |
const property_spec_ref uri_prop ("artifactLocation", "uri", "3.4.3"); |
| 1825 |
auto uri = get_optional_property<json::string> (artifact_loc, uri_prop); |
| 1826 |
|
| 1827 |
// §3.4.5 "index" property |
| 1828 |
const property_spec_ref index_prop ("artifactLocation", "index", "3.4.5"); |
| 1829 |
auto index = get_optional_property<json::integer_number> (artifact_loc, |
| 1830 |
index_prop); |
| 1831 |
if (uri == nullptr && index == nullptr) |
| 1832 |
{ |
| 1833 |
object_spec_ref constraints ("artifactLocation", "3.4.2"); |
| 1834 |
return report_invalid_sarif_at_least_one_of (artifact_loc, |
| 1835 |
constraints, |
| 1836 |
uri_prop, |
| 1837 |
index_prop); |
| 1838 |
} |
| 1839 |
|
| 1840 |
if (uri) |
| 1841 |
{ |
| 1842 |
// TODO: source language |
| 1843 |
out = m_output_mgr.new_file (uri->get_string (), nullptr); |
| 1844 |
return status::ok; |
| 1845 |
} |
| 1846 |
|
| 1847 |
return status::ok; |
| 1848 |
} |
| 1849 |
|
| 1850 |
/* Handle a "region" object (§3.30) within FILE, writing to OUT. */ |
| 1851 |
|
| 1852 |
enum status |
| 1853 |
sarif_replayer:: |
| 1854 |
handle_region_object (const json::object ®ion_obj, |
| 1855 |
libgdiagnostics::file file, |
| 1856 |
libgdiagnostics::physical_location &out) |
| 1857 |
{ |
| 1858 |
gcc_assert (file.m_inner); |
| 1859 |
|
| 1860 |
// §3.30.5 "startLine" property |
| 1861 |
const property_spec_ref start_line_prop ("region", "startLine", "3.30.5"); |
| 1862 |
libgdiagnostics::physical_location start; |
| 1863 |
libgdiagnostics::physical_location end; |
| 1864 |
if (auto start_line_jnum |
| 1865 |
= get_optional_property<json::integer_number> (region_obj, |
| 1866 |
start_line_prop)) |
| 1867 |
{ |
| 1868 |
/* Text region defined by line/column properties. */ |
| 1869 |
const property_spec_ref start_column_prop |
| 1870 |
("region", "startColumn", "3.30.6"); |
| 1871 |
if (auto start_column_jnum |
| 1872 |
= get_optional_property<json::integer_number> (region_obj, |
| 1873 |
start_column_prop)) |
| 1874 |
{ |
| 1875 |
start = m_output_mgr.new_location_from_file_line_column |
| 1876 |
(file, start_line_jnum->get (), start_column_jnum->get ()); |
| 1877 |
} |
| 1878 |
else |
| 1879 |
start = m_output_mgr.new_location_from_file_and_line |
| 1880 |
(file, start_line_jnum->get ()); |
| 1881 |
|
| 1882 |
int end_line = start_line_jnum->get (); |
| 1883 |
const property_spec_ref end_line_prop ("region", "endLine", "3.30.7"); |
| 1884 |
if (auto end_line_jnum |
| 1885 |
= get_optional_property<json::integer_number> (region_obj, |
| 1886 |
end_line_prop)) |
| 1887 |
end_line = end_line_jnum->get (); |
| 1888 |
|
| 1889 |
const property_spec_ref end_column_prop ("region", "endColumn", "3.30.8"); |
| 1890 |
if (auto end_column_jnum |
| 1891 |
= get_optional_property<json::integer_number> (region_obj, |
| 1892 |
end_column_prop)) |
| 1893 |
{ |
| 1894 |
/* SARIF's endColumn is 1 beyond the final column in the region, |
| 1895 |
whereas GCC's end columns are inclusive. */ |
| 1896 |
end = m_output_mgr.new_location_from_file_line_column |
| 1897 |
(file, end_line, end_column_jnum->get () - 1); |
| 1898 |
} |
| 1899 |
else |
| 1900 |
{ |
| 1901 |
// missing "endColumn" means the whole of the rest of the row |
| 1902 |
end = m_output_mgr.new_location_from_file_and_line |
| 1903 |
(file, end_line); |
| 1904 |
} |
| 1905 |
|
| 1906 |
out = m_output_mgr.new_location_from_range (start, start, end); |
| 1907 |
} |
| 1908 |
|
| 1909 |
return status::ok; |
| 1910 |
} |
| 1911 |
|
| 1912 |
/* Handle a "logicalLocation" object (§3.33), using it to populate OUT. |
| 1913 |
|
| 1914 |
If RUN_OBJ is non-null, and has "logicalLocations", then use it if we |
| 1915 |
see an "index" property. |
| 1916 |
|
| 1917 |
Known limitations: |
| 1918 |
- doesn't yet handle "parentIndex" property (§3.33.8) |
| 1919 |
*/ |
| 1920 |
|
| 1921 |
enum status |
| 1922 |
sarif_replayer:: |
| 1923 |
handle_logical_location_object (const json::object &logical_loc_obj, |
| 1924 |
const json::object *run_obj, |
| 1925 |
libgdiagnostics::logical_location &out) |
| 1926 |
{ |
| 1927 |
if (run_obj) |
| 1928 |
{ |
| 1929 |
const property_spec_ref name_prop ("logicalLocation", "index", "3.33.3"); |
| 1930 |
if (auto index_js_int |
| 1931 |
= get_optional_property<json::integer_number> (logical_loc_obj, |
| 1932 |
name_prop)) |
| 1933 |
{ |
| 1934 |
const long index = index_js_int->get (); |
| 1935 |
/* If "index" is present, then there must be theRun.logicalLocations |
| 1936 |
and it must have a cached object at that index. */ |
| 1937 |
const property_spec_ref run_logical_locs_prop |
| 1938 |
("run", "logicalLocations", "3.14.17"); |
| 1939 |
if (auto logical_locs_arr |
| 1940 |
= get_required_property<json::array> (*run_obj, |
| 1941 |
run_logical_locs_prop)) |
| 1942 |
{ |
| 1943 |
/* "index" must be in range */ |
| 1944 |
if (index < 0 || index >= (long)logical_locs_arr->length ()) |
| 1945 |
return report_invalid_sarif (*index_js_int, name_prop, |
| 1946 |
"index value %li is out of range" |
| 1947 |
" for theRun.logicalLocations", |
| 1948 |
index); |
| 1949 |
|
| 1950 |
json::value *element = logical_locs_arr->get (index); |
| 1951 |
|
| 1952 |
if (const json::object *obj |
| 1953 |
= require_object (*element,run_logical_locs_prop)) |
| 1954 |
{ |
| 1955 |
/* Use this "cached" object instead. |
| 1956 |
Pass nullptr for the run object so that we |
| 1957 |
don't recurse. */ |
| 1958 |
return handle_logical_location_object (*obj, nullptr, out); |
| 1959 |
} |
| 1960 |
} |
| 1961 |
} |
| 1962 |
} |
| 1963 |
|
| 1964 |
// Otherwise use the properties from LOGICAL_LOC_OBJ. |
| 1965 |
|
| 1966 |
const property_spec_ref name_prop ("logicalLocation", "name", "3.33.4"); |
| 1967 |
const char *short_name = nullptr; |
| 1968 |
if (auto name_jstr = get_optional_property<json::string> (logical_loc_obj, |
| 1969 |
name_prop)) |
| 1970 |
short_name = name_jstr->get_string (); |
| 1971 |
|
| 1972 |
const property_spec_ref fqname_prop |
| 1973 |
("logicalLocation", "fullyQualifiedName", "3.33.5"); |
| 1974 |
const char *fully_qualified_name = nullptr; |
| 1975 |
if (auto fully_qualified_name_jstr |
| 1976 |
= get_optional_property<json::string> (logical_loc_obj, fqname_prop)) |
| 1977 |
fully_qualified_name = fully_qualified_name_jstr->get_string (); |
| 1978 |
|
| 1979 |
const property_spec_ref decorated_name_prop |
| 1980 |
("logicalLocation", "decoratedName", "3.33.6"); |
| 1981 |
const char *decorated_name = nullptr; |
| 1982 |
if (auto decorated_name_jstr |
| 1983 |
= get_optional_property<json::string> (logical_loc_obj, |
| 1984 |
decorated_name_prop)) |
| 1985 |
decorated_name = decorated_name_jstr->get_string (); |
| 1986 |
|
| 1987 |
// §3.33.7 "kind" property |
| 1988 |
const property_spec_ref kind_prop ("logicalLocation", "kind", "3.33.7"); |
| 1989 |
enum diagnostic_logical_location_kind_t kind |
| 1990 |
= DIAGNOSTIC_LOGICAL_LOCATION_KIND_FUNCTION; |
| 1991 |
if (auto kind_str = get_optional_property<json::string> (logical_loc_obj, |
| 1992 |
kind_prop)) |
| 1993 |
{ |
| 1994 |
const string_property_value<enum diagnostic_logical_location_kind_t> |
| 1995 |
kind_values[] |
| 1996 |
= { |
| 1997 |
{ "function", |
| 1998 |
DIAGNOSTIC_LOGICAL_LOCATION_KIND_FUNCTION }, |
| 1999 |
{ "member", |
| 2000 |
DIAGNOSTIC_LOGICAL_LOCATION_KIND_MEMBER }, |
| 2001 |
{ "module", |
| 2002 |
DIAGNOSTIC_LOGICAL_LOCATION_KIND_MODULE }, |
| 2003 |
{ "namespace", |
| 2004 |
DIAGNOSTIC_LOGICAL_LOCATION_KIND_NAMESPACE }, |
| 2005 |
{ "type", |
| 2006 |
DIAGNOSTIC_LOGICAL_LOCATION_KIND_TYPE }, |
| 2007 |
{ "returnType", |
| 2008 |
DIAGNOSTIC_LOGICAL_LOCATION_KIND_RETURN_TYPE }, |
| 2009 |
{ "parameter", |
| 2010 |
DIAGNOSTIC_LOGICAL_LOCATION_KIND_PARAMETER }, |
| 2011 |
{ "variable", |
| 2012 |
DIAGNOSTIC_LOGICAL_LOCATION_KIND_VARIABLE } }; |
| 2013 |
auto result |
| 2014 |
= get_value_from_json_string<enum diagnostic_logical_location_kind_t> |
| 2015 |
(*kind_str, kind_prop, kind_values, ARRAY_SIZE (kind_values)); |
| 2016 |
if (result.m_err != status::ok) |
| 2017 |
return result.m_err; |
| 2018 |
kind = result.m_val; |
| 2019 |
} |
| 2020 |
|
| 2021 |
libgdiagnostics::logical_location parent; |
| 2022 |
out = m_output_mgr.new_logical_location (kind, |
| 2023 |
parent, |
| 2024 |
short_name, |
| 2025 |
fully_qualified_name, |
| 2026 |
decorated_name); |
| 2027 |
|
| 2028 |
return status::ok; |
| 2029 |
} |
| 2030 |
|
| 2031 |
// 3.52.3 reportingDescriptor lookup |
| 2032 |
// "For an example of the interaction between ruleId and rule.id, see §3.52.4." |
| 2033 |
|
| 2034 |
const json::object * |
| 2035 |
sarif_replayer:: |
| 2036 |
lookup_rule_by_id_in_tool (const char *rule_id, |
| 2037 |
const json::object &tool_obj, |
| 2038 |
const json::object *&tool_component_obj) |
| 2039 |
{ |
| 2040 |
auto driver_obj |
| 2041 |
= get_required_property<json::object> (tool_obj, |
| 2042 |
property_spec_ref ("tool", "driver", |
| 2043 |
"3.18.2")); |
| 2044 |
if (!driver_obj) |
| 2045 |
return nullptr; |
| 2046 |
|
| 2047 |
if (auto rule_obj = lookup_rule_by_id_in_component (rule_id, *driver_obj)) |
| 2048 |
{ |
| 2049 |
tool_component_obj = driver_obj; |
| 2050 |
return rule_obj; |
| 2051 |
} |
| 2052 |
|
| 2053 |
// TODO: also handle extensions |
| 2054 |
|
| 2055 |
return NULL; |
| 2056 |
} |
| 2057 |
|
| 2058 |
const json::object * |
| 2059 |
sarif_replayer:: |
| 2060 |
lookup_rule_by_id_in_component (const char *rule_id, |
| 2061 |
const json::object &tool_component_obj) |
| 2062 |
{ |
| 2063 |
const property_spec_ref rules ("toolComponent", "rules", "3.18.2"); |
| 2064 |
|
| 2065 |
auto rules_arr |
| 2066 |
= get_optional_property<json::array> (tool_component_obj, rules); |
| 2067 |
if (!rules_arr) |
| 2068 |
return nullptr; |
| 2069 |
|
| 2070 |
for (auto element : *rules_arr) |
| 2071 |
{ |
| 2072 |
const json::object *reporting_desc_obj |
| 2073 |
= require_object_for_element (*element, rules); |
| 2074 |
|
| 2075 |
/* reportingDescriptor objects (§3.49). */ |
| 2076 |
const property_spec_ref id ("reportingDescriptor", "id", "3.49.3"); |
| 2077 |
auto desc_id_jstr |
| 2078 |
= get_required_property<json::string> (*reporting_desc_obj, id); |
| 2079 |
if (!desc_id_jstr) |
| 2080 |
return nullptr; |
| 2081 |
|
| 2082 |
if (!strcmp (rule_id, desc_id_jstr->get_string ())) |
| 2083 |
return reporting_desc_obj; |
| 2084 |
} |
| 2085 |
|
| 2086 |
/* Not found. */ |
| 2087 |
return nullptr; |
| 2088 |
} |
| 2089 |
|
| 2090 |
// "fix" object (§3.55) |
| 2091 |
|
| 2092 |
enum status |
| 2093 |
sarif_replayer::handle_fix_object (libgdiagnostics::diagnostic &diag, |
| 2094 |
const json::object &fix_obj) |
| 2095 |
{ |
| 2096 |
const property_spec_ref changes ("fix", "artifactChanges", "3.55.3"); |
| 2097 |
auto changes_arr = get_required_property<json::array> (fix_obj, changes); |
| 2098 |
if (!changes_arr) |
| 2099 |
return status::err_invalid_sarif; |
| 2100 |
|
| 2101 |
for (auto element : *changes_arr) |
| 2102 |
{ |
| 2103 |
const json::object *change_obj |
| 2104 |
= require_object_for_element (*element, changes); |
| 2105 |
if (!change_obj) |
| 2106 |
return status::err_invalid_sarif; |
| 2107 |
enum status s = handle_artifact_change_object (diag, *change_obj); |
| 2108 |
if (s != status::ok) |
| 2109 |
return s; |
| 2110 |
} |
| 2111 |
return status::ok; |
| 2112 |
} |
| 2113 |
|
| 2114 |
// "artifactChange" object (§3.56) |
| 2115 |
|
| 2116 |
enum status |
| 2117 |
sarif_replayer:: |
| 2118 |
handle_artifact_change_object (libgdiagnostics::diagnostic &diag, |
| 2119 |
const json::object &change_obj) |
| 2120 |
{ |
| 2121 |
const property_spec_ref location |
| 2122 |
("artifactChange", "artifactLocation", "3.56.2"); |
| 2123 |
auto artifact_loc_obj |
| 2124 |
= get_required_property<json::object> (change_obj, location); |
| 2125 |
if (!artifact_loc_obj) |
| 2126 |
return status::err_invalid_sarif; |
| 2127 |
|
| 2128 |
libgdiagnostics::file file; |
| 2129 |
enum status s = handle_artifact_location_object (*artifact_loc_obj, file); |
| 2130 |
if (s != status::ok) |
| 2131 |
return s; |
| 2132 |
|
| 2133 |
const property_spec_ref replacements |
| 2134 |
("artifactChange", "replacements", "3.56.3"); |
| 2135 |
auto replacements_arr |
| 2136 |
= get_required_property<json::array> (change_obj, replacements); |
| 2137 |
if (!replacements_arr) |
| 2138 |
return status::err_invalid_sarif; |
| 2139 |
for (auto element : *replacements_arr) |
| 2140 |
{ |
| 2141 |
// 3.57 replacement object |
| 2142 |
const json::object *replacement_obj |
| 2143 |
= require_object_for_element (*element, replacements); |
| 2144 |
if (!replacement_obj) |
| 2145 |
return status::err_invalid_sarif; |
| 2146 |
|
| 2147 |
// 3.57.3 deletedRegion property |
| 2148 |
const property_spec_ref deleted_region |
| 2149 |
("replacement", "deletedRegion", "3.57.3"); |
| 2150 |
auto deleted_region_obj |
| 2151 |
= get_required_property<json::object> (*replacement_obj, |
| 2152 |
deleted_region); |
| 2153 |
if (!deleted_region_obj) |
| 2154 |
return status::err_invalid_sarif; |
| 2155 |
|
| 2156 |
libgdiagnostics::physical_location phys_loc; |
| 2157 |
enum status s = handle_region_object (*deleted_region_obj, |
| 2158 |
file, |
| 2159 |
phys_loc); |
| 2160 |
if (s != status::ok) |
| 2161 |
return s; |
| 2162 |
|
| 2163 |
// 3.57.4 insertedContent property |
| 2164 |
const property_spec_ref inserted_content |
| 2165 |
("replacement", "insertedContent", "3.57.4"); |
| 2166 |
const char *inserted_text = ""; |
| 2167 |
if (auto inserted_content_obj |
| 2168 |
= get_optional_property<json::object> (*replacement_obj, |
| 2169 |
inserted_content)) |
| 2170 |
{ |
| 2171 |
const property_spec_ref prop_text |
| 2172 |
("artifactContent", "text", "3.3.2"); |
| 2173 |
if (auto text_jstr |
| 2174 |
= get_optional_property<json::string> (*inserted_content_obj, |
| 2175 |
prop_text)) |
| 2176 |
inserted_text = text_jstr->get_string (); |
| 2177 |
} |
| 2178 |
|
| 2179 |
diag.add_fix_it_hint_replace (phys_loc, inserted_text); |
| 2180 |
} |
| 2181 |
return status::ok; |
| 2182 |
} |
| 2183 |
|
| 2184 |
} // anonymous namespace |
| 2185 |
|
| 2186 |
/* Error-checking at the API boundary. */ |
| 2187 |
|
| 2188 |
#define FAIL_IF_NULL(PTR_ARG) \ |
| 2189 |
do { \ |
| 2190 |
GCC_DIAGNOSTIC_PUSH_IGNORED(-Wnonnull-compare) \ |
| 2191 |
if (!(PTR_ARG)) { \ |
| 2192 |
fprintf (stderr, "%s: %s must be non-NULL\n", \ |
| 2193 |
__func__, #PTR_ARG); \ |
| 2194 |
abort (); \ |
| 2195 |
} \ |
| 2196 |
GCC_DIAGNOSTIC_POP \ |
| 2197 |
} while (0) |
| 2198 |
|
| 2199 |
/* Public entrypoint. */ |
| 2200 |
|
| 2201 |
int |
| 2202 |
sarif_replay_path (const char *sarif_file, |
| 2203 |
diagnostic_manager *output_manager, |
| 2204 |
diagnostic_manager *control_manager, |
| 2205 |
const replay_options *options) |
| 2206 |
{ |
| 2207 |
FAIL_IF_NULL (sarif_file); |
| 2208 |
FAIL_IF_NULL (output_manager); |
| 2209 |
FAIL_IF_NULL (control_manager); |
| 2210 |
FAIL_IF_NULL (options); |
| 2211 |
|
| 2212 |
sarif_replayer r (libgdiagnostics::manager (output_manager, false), |
| 2213 |
libgdiagnostics::manager (control_manager, false)); |
| 2214 |
return (int)r.replay_file (sarif_file, *options); |
| 2215 |
} |