Branch data Line data Source code
1 : : /* SARIF output for diagnostics
2 : : Copyright (C) 2018-2025 Free Software Foundation, Inc.
3 : : Contributed by David Malcolm <dmalcolm@redhat.com>.
4 : :
5 : : This file is part of GCC.
6 : :
7 : : GCC is free software; you can redistribute it and/or modify it under
8 : : the terms of the GNU General Public License as published by the Free
9 : : Software Foundation; either version 3, or (at your option) any later
10 : : version.
11 : :
12 : : GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 : : WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 : : FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 : : for more details.
16 : :
17 : : You should have received a copy of the GNU General Public License
18 : : along with GCC; see the file COPYING3. If not see
19 : : <http://www.gnu.org/licenses/>. */
20 : :
21 : :
22 : : #include "config.h"
23 : : #define INCLUDE_LIST
24 : : #define INCLUDE_MAP
25 : : #define INCLUDE_STRING
26 : : #define INCLUDE_VECTOR
27 : : #include "system.h"
28 : : #include "coretypes.h"
29 : : #include "diagnostic.h"
30 : : #include "diagnostic-metadata.h"
31 : : #include "diagnostic-path.h"
32 : : #include "diagnostic-format.h"
33 : : #include "diagnostic-buffer.h"
34 : : #include "json.h"
35 : : #include "cpplib.h"
36 : : #include "logical-location.h"
37 : : #include "diagnostic-client-data-hooks.h"
38 : : #include "diagnostic-diagram.h"
39 : : #include "text-art/canvas.h"
40 : : #include "diagnostic-format-sarif.h"
41 : : #include "diagnostic-format-text.h"
42 : : #include "ordered-hash-map.h"
43 : : #include "sbitmap.h"
44 : : #include "selftest.h"
45 : : #include "selftest-diagnostic.h"
46 : : #include "selftest-diagnostic-show-locus.h"
47 : : #include "selftest-json.h"
48 : : #include "text-range-label.h"
49 : : #include "pretty-print-format-impl.h"
50 : : #include "pretty-print-urlifier.h"
51 : : #include "demangle.h"
52 : : #include "backtrace.h"
53 : :
54 : : /* A json::array where the values are "unique" as per
55 : : SARIF v2.1.0 section 3.7.3 ("Array properties with unique values"). */
56 : :
57 : : template <typename JsonElementType>
58 : : class sarif_array_of_unique : public json::array
59 : : {
60 : : public:
61 : 372 : size_t append_uniquely (std::unique_ptr<JsonElementType> val)
62 : : {
63 : : /* This should be O(log(n)) due to the std::map. */
64 : 372 : auto search = m_index_by_value.find (val.get ());
65 : 372 : if (search != m_index_by_value.end())
66 : 93 : return (*search).second;
67 : :
68 : 279 : const size_t insertion_idx = size ();
69 : 279 : m_index_by_value.insert ({val.get (), insertion_idx});
70 : 279 : append (std::move (val));
71 : 279 : return insertion_idx;
72 : : }
73 : :
74 : : /* For ease of reading output, add "index": idx to all
75 : : objects in the array.
76 : : We don't do this until we've added everything, since
77 : : the "index" property would otherwise confuse the
78 : : comparison against new elements. */
79 : 50 : void add_explicit_index_values ()
80 : : {
81 : 321 : for (size_t idx = 0; idx < length (); ++idx)
82 : 271 : if (json::object *obj = get (idx)->dyn_cast_object ())
83 : 271 : obj->set_integer ("index", idx);
84 : 50 : }
85 : :
86 : : private:
87 : : struct comparator_t {
88 : 4819 : bool operator () (const json::value *a, const json::value *b) const
89 : : {
90 : 4819 : gcc_assert (a);
91 : 4819 : gcc_assert (b);
92 : 4819 : return json::value::compare (*a, *b) < 0;
93 : : }
94 : : };
95 : :
96 : : // json::value * here is borrowed from m_elements
97 : : std::map<json::value *, int, comparator_t> m_index_by_value;
98 : : };
99 : :
100 : : /* Forward decls. */
101 : : class sarif_builder;
102 : : class content_renderer;
103 : : class escape_nonascii_renderer;
104 : :
105 : : /* Subclasses of sarif_object.
106 : : Keep these in order of their descriptions in the specification. */
107 : : class sarif_artifact_content; // 3.3
108 : : class sarif_artifact_location; // 3.4
109 : : class sarif_message; // 3.11
110 : : class sarif_multiformat_message_string; // 3.12
111 : : class sarif_log; // 3.13
112 : : class sarif_run; // 3.14
113 : : class sarif_tool; // 3.18
114 : : class sarif_tool_component; // 3.19
115 : : class sarif_invocation; // 3.20
116 : : class sarif_artifact; // 3.24
117 : : class sarif_location_manager; // not in the spec
118 : : class sarif_result; // 3.27
119 : : class sarif_location; // 3.28
120 : : class sarif_physical_location; // 3.29
121 : : class sarif_region; // 3.30
122 : : class sarif_logical_location; // 3.33
123 : : class sarif_location_relationship; // 3.34
124 : : class sarif_code_flow; // 3.36
125 : : class sarif_thread_flow; // 3.37
126 : : class sarif_thread_flow_location; // 3.38
127 : : class sarif_reporting_descriptor; // 3.49
128 : : class sarif_reporting_descriptor_reference; // 3.53
129 : : class sarif_tool_component_reference; // 3.54
130 : : class sarif_fix; // 3.55
131 : : class sarif_artifact_change; // 3.56
132 : : class sarif_replacement; // 3.57
133 : : class sarif_ice_notification; // 3.58
134 : :
135 : : // Valid values for locationRelationship's "kinds" property (3.34.3)
136 : :
137 : : enum class location_relationship_kind
138 : : {
139 : : includes,
140 : : is_included_by,
141 : : relevant,
142 : :
143 : : NUM_KINDS
144 : : };
145 : :
146 : : /* Declarations of subclasses of sarif_object.
147 : : Keep these in order of their descriptions in the specification. */
148 : :
149 : : /* Subclass of sarif_object for SARIF "artifactContent" objects
150 : : (SARIF v2.1.0 section 3.3). */
151 : :
152 : 675 : class sarif_artifact_content : public sarif_object {};
153 : :
154 : : /* Subclass of sarif_object for SARIF "artifactLocation" objects
155 : : (SARIF v2.1.0 section 3.4). */
156 : :
157 : 1504 : class sarif_artifact_location : public sarif_object {};
158 : :
159 : : /* Subclass of sarif_object for SARIF "message" objects
160 : : (SARIF v2.1.0 section 3.11). */
161 : :
162 : 966 : class sarif_message : public sarif_object {};
163 : :
164 : : /* Subclass of sarif_object for SARIF "multiformatMessageString" objects
165 : : (SARIF v2.1.0 section 3.12). */
166 : :
167 : 154 : class sarif_multiformat_message_string : public sarif_object {};
168 : :
169 : : /* Subclass of sarif_object for SARIF "log" objects
170 : : (SARIF v2.1.0 section 3.13). */
171 : :
172 : 263 : class sarif_log : public sarif_object {};
173 : :
174 : : /* Subclass of sarif_object for SARIF "run" objects
175 : : (SARIF v2.1.0 section 3.14). */
176 : :
177 : 263 : class sarif_run : public sarif_object {};
178 : :
179 : : /* Subclass of sarif_object for SARIF "tool" objects
180 : : (SARIF v2.1.0 section 3.18). */
181 : :
182 : 263 : class sarif_tool : public sarif_object {};
183 : :
184 : : /* Subclass of sarif_object for SARIF "toolComponent" objects
185 : : (SARIF v2.1.0 section 3.19). */
186 : :
187 : 289 : class sarif_tool_component : public sarif_object {};
188 : :
189 : : /* Make a JSON string for the current date and time.
190 : : See SARIF v2.1.0 section 3.9 "Date/time properties".
191 : : Given that we don't run at the very beginning/end of the
192 : : process, it doesn't make sense to be more accurate than
193 : : the current second. */
194 : :
195 : : static std::unique_ptr<json::string>
196 : 662 : make_date_time_string_for_current_time ()
197 : : {
198 : 662 : time_t t = time (nullptr);
199 : 662 : struct tm *tm = gmtime (&t);
200 : 662 : char buf[256];
201 : 662 : snprintf (buf, sizeof (buf) - 1,
202 : : ("%04i-%02i-%02iT"
203 : : "%02i:%02i:%02iZ"),
204 : 662 : tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday,
205 : : tm->tm_hour, tm->tm_min, tm->tm_sec);
206 : 662 : return std::make_unique<json::string> (buf);
207 : : }
208 : :
209 : : /* Subclass of sarif_object for SARIF "invocation" objects
210 : : (SARIF v2.1.0 section 3.20). */
211 : :
212 : : class sarif_invocation : public sarif_object
213 : : {
214 : : public:
215 : : sarif_invocation (sarif_builder &builder,
216 : : const char * const *original_argv);
217 : :
218 : : void add_notification_for_ice (const diagnostic_info &diagnostic,
219 : : sarif_builder &builder,
220 : : std::unique_ptr<json::object> backtrace);
221 : : void prepare_to_flush (sarif_builder &builder);
222 : :
223 : : private:
224 : : std::unique_ptr<json::array> m_notifications_arr;
225 : : bool m_success;
226 : : };
227 : :
228 : : /* Corresponds to values for the SARIF artifact objects "roles" property.
229 : : (SARIF v2.1.0 section 3.24.6). */
230 : :
231 : : enum class diagnostic_artifact_role
232 : : {
233 : : analysis_target, /* "analysisTarget". */
234 : : debug_output_file, /* "debugOutputFile". */
235 : : result_file, /* "resultFile". */
236 : :
237 : : /* "scannedFile" added in 2.2;
238 : : see https://github.com/oasis-tcs/sarif-spec/issues/459 */
239 : : scanned_file,
240 : :
241 : : traced_file, /* "tracedFile". */
242 : :
243 : : NUM_ROLES
244 : : };
245 : :
246 : : /* Subclass of sarif_object for SARIF artifact objects
247 : : (SARIF v2.1.0 section 3.24). */
248 : :
249 : : class sarif_artifact : public sarif_object
250 : : {
251 : : public:
252 : 417 : sarif_artifact (const char *filename)
253 : 834 : : m_filename (filename),
254 : 834 : m_roles ((unsigned)diagnostic_artifact_role::NUM_ROLES),
255 : 417 : m_embed_contents (false)
256 : : {
257 : 417 : bitmap_clear (m_roles);
258 : 417 : }
259 : :
260 : : void add_role (enum diagnostic_artifact_role role,
261 : : bool embed_contents);
262 : :
263 : 281 : bool embed_contents_p () const { return m_embed_contents; }
264 : : void populate_contents (sarif_builder &builder);
265 : : void populate_roles ();
266 : :
267 : : private:
268 : : const char *m_filename;
269 : : auto_sbitmap m_roles;
270 : :
271 : : /* Flag to track whether this artifact should have a "contents" property
272 : : (SARIF v2.1.0 section 3.24.8).
273 : : We only add the contents for those artifacts that have a location
274 : : referencing them (so that a consumer might want to quote the source). */
275 : : bool m_embed_contents;
276 : : };
277 : :
278 : : /* A class for sarif_objects that own a "namespace" of numeric IDs for
279 : : managing location objects within them. Currently (SARIF v2.1.0)
280 : : this is just for sarif_result (section 3.28.2), but it will likely
281 : : eventually also be for notification objects; see
282 : : https://github.com/oasis-tcs/sarif-spec/issues/540
283 : :
284 : : Consider locations with chains of include information e.g.
285 : :
286 : : > include-chain-1.c:
287 : : > #include "include-chain-1.h"
288 : :
289 : : include-chain-1.h:
290 : : | // First set of decls, which will be referenced in notes
291 : : | #include "include-chain-1-1.h"
292 : : |
293 : : | // Second set of decls, which will trigger the errors
294 : : | #include "include-chain-1-2.h"
295 : :
296 : : include-chain-1-1.h:
297 : : | int p;
298 : : | int q;
299 : :
300 : : include-chain-1-1.h:
301 : : | char p;
302 : : | char q;
303 : :
304 : : GCC's textual output emits:
305 : : | In file included from PATH/include-chain-1.h:5,
306 : : | from PATH/include-chain-1.c:30:
307 : : | PATH/include-chain-1-2.h:1:6: error: conflicting types for 'p'; have 'char'
308 : : | 1 | char p;
309 : : | | ^
310 : : | In file included from PATH/include-chain-1.h:2:
311 : : | PATH/include-chain-1-1.h:1:5: note: previous declaration of 'p' with type 'int'
312 : : | 1 | int p;
313 : : | | ^
314 : : | PATH/include-chain-1-2.h:2:6: error: conflicting types for 'q'; have 'char'
315 : : | 2 | char q;
316 : : | | ^
317 : : | PATH/include-chain-1-1.h:2:5: note: previous declaration of 'q' with type 'int'
318 : : | 2 | int q;
319 : : | | ^
320 : :
321 : : Whenever a SARIF location is added for a location_t that
322 : : was #included from somewhere, we queue up the creation of a SARIF
323 : : location for the location of the #include. The worklist of queued
324 : : locations is flushed when the result is finished, which lazily creates
325 : : any additional related locations for the include chain, and the
326 : : relationships between the locations. Doing so can lead to further
327 : : include locations being processed. The worklist approach allows us
328 : : to lazily explore the relevant part of the directed graph of location_t
329 : : values implicit in our line_maps structure, replicating it as a directed
330 : : graph of SARIF locations within the SARIF result object, like this:
331 : :
332 : : [0]: error in include-chain-1-2.h ("conflicting types for 'p'; have 'char'")
333 : : [1]: #include "include-chain-1-2.h" in include-chain-1.h
334 : : [2]: note in include-chain-1-2.h ("previous declaration of 'p' with type 'int'")
335 : : [3]: #include "include-chain-1-1.h" in include-chain-1.h
336 : : [4]: #include "include-chain-1.h" in include-chain-1.c
337 : :
338 : : where we want to capture this "includes" graph in SARIF form:
339 : : . +-----------------------------------+ +----------------------------------+
340 : : . |"id": 0 | |"id": 2 |
341 : : . | error: "conflicting types for 'p';| | note: previous declaration of 'p'|
342 : : . | have 'char'"| | | with type 'int'") |
343 : : . | in include-chain-1-2.h | | in include-chain-1-1.h |
344 : : . +-----------------------------------+ +----------------------------------+
345 : : . ^ | ^ |
346 : : . includes | | included-by includes | | included-by
347 : : . | V | V
348 : : . +--------------------------------+ +--------------------------------+
349 : : . |"id": 1 | |"id": 3 |
350 : : . | #include "include-chain-1-2.h" | | #include "include-chain-1-1.h" |
351 : : . | in include-chain-1.h | | in include-chain-1.h |
352 : : . +--------------------------------+ +--------------------------------+
353 : : . ^ | ^ |
354 : : . includes | | included-by includes | | included-by
355 : : . | V | V
356 : : . +------------------------------------+
357 : : . |"id": 4 |
358 : : . | The #include "include-chain-1.h" |
359 : : . | in include-chain-1.c |
360 : : . +------------------------------------+
361 : : */
362 : :
363 : : class sarif_location_manager : public sarif_object
364 : : {
365 : : public:
366 : : /* A worklist of pending actions needed to fully process this object.
367 : :
368 : : This lets us lazily walk our data structures to build the
369 : : directed graph of locations, whilst keeping "notes" at the top
370 : : of the "relatedLocations" array, and avoiding the need for
371 : : recursion. */
372 : : struct worklist_item
373 : : {
374 : : enum class kind
375 : : {
376 : : /* Process a #include relationship where m_location_obj
377 : : was #included-d at m_where. */
378 : : included_from,
379 : :
380 : : /* Process a location_t that was added as a secondary location
381 : : to a rich_location without a label. */
382 : : unlabelled_secondary_location
383 : : };
384 : :
385 : 25 : worklist_item (sarif_location &location_obj,
386 : : enum kind kind,
387 : : location_t where)
388 : 25 : : m_location_obj (location_obj),
389 : 25 : m_kind (kind),
390 : 25 : m_where (where)
391 : : {
392 : : }
393 : :
394 : : sarif_location &m_location_obj;
395 : : enum kind m_kind;
396 : : location_t m_where;
397 : : };
398 : :
399 : 590 : sarif_location_manager ()
400 : 1180 : : m_related_locations_arr (nullptr),
401 : 590 : m_next_location_id (0)
402 : : {
403 : 590 : }
404 : :
405 : 36 : unsigned allocate_location_id ()
406 : : {
407 : 36 : return m_next_location_id++;
408 : : }
409 : :
410 : : virtual void
411 : : add_related_location (std::unique_ptr<sarif_location> location_obj,
412 : : sarif_builder &builder);
413 : :
414 : : void
415 : : add_relationship_to_worklist (sarif_location &location_obj,
416 : : enum worklist_item::kind kind,
417 : : location_t where);
418 : :
419 : : void
420 : : process_worklist (sarif_builder &builder);
421 : :
422 : : void
423 : : process_worklist_item (sarif_builder &builder,
424 : : const worklist_item &item);
425 : : private:
426 : : json::array *m_related_locations_arr; // borrowed
427 : : unsigned m_next_location_id;
428 : :
429 : : std::list<worklist_item> m_worklist;
430 : : std::map<location_t, sarif_location *> m_included_from_locations;
431 : : std::map<location_t, sarif_location *> m_unlabelled_secondary_locations;
432 : : };
433 : :
434 : : /* Subclass of sarif_object for SARIF "result" objects
435 : : (SARIF v2.1.0 section 3.27).
436 : : Each SARIF result object has its own "namespace" of numeric IDs for
437 : : managing location objects (SARIF v2.1.0 section 3.28.2). */
438 : :
439 : 256 : class sarif_result : public sarif_location_manager
440 : : {
441 : : public:
442 : 586 : sarif_result (unsigned idx_within_parent)
443 : 458 : : m_idx_within_parent (idx_within_parent)
444 : : {}
445 : :
446 : 9 : unsigned get_index_within_parent () const { return m_idx_within_parent; }
447 : :
448 : : void
449 : : on_nested_diagnostic (const diagnostic_info &diagnostic,
450 : : diagnostic_t orig_diag_kind,
451 : : sarif_builder &builder);
452 : : void on_diagram (const diagnostic_diagram &diagram,
453 : : sarif_builder &builder);
454 : :
455 : : private:
456 : : const unsigned m_idx_within_parent;
457 : : };
458 : :
459 : : /* Subclass of sarif_object for SARIF "location" objects
460 : : (SARIF v2.1.0 section 3.28).
461 : : A location object can have an "id" which must be unique within
462 : : the enclosing result, if any (see SARIF v2.1.0 section 3.28.2). */
463 : :
464 : 704 : class sarif_location : public sarif_object
465 : : {
466 : : public:
467 : : long lazily_add_id (sarif_location_manager &loc_mgr);
468 : : long get_id () const;
469 : :
470 : : void lazily_add_relationship (sarif_location &target,
471 : : enum location_relationship_kind kind,
472 : : sarif_location_manager &loc_mgr);
473 : :
474 : : private:
475 : : sarif_location_relationship &
476 : : lazily_add_relationship_object (sarif_location &target,
477 : : sarif_location_manager &loc_mgr);
478 : :
479 : : json::array &lazily_add_relationships_array ();
480 : :
481 : : std::map<sarif_location *,
482 : : sarif_location_relationship *> m_relationships_map;
483 : : };
484 : :
485 : : /* Subclass of sarif_object for SARIF "physicalLocation" objects
486 : : (SARIF v2.1.0 section 3.29). */
487 : :
488 : 636 : class sarif_physical_location : public sarif_object {};
489 : :
490 : : /* Subclass of sarif_object for SARIF "region" objects
491 : : (SARIF v2.1.0 section 3.30). */
492 : :
493 : 1682 : class sarif_region : public sarif_object {};
494 : :
495 : : /* Subclass of sarif_object for SARIF "logicalLocation" objects
496 : : (SARIF v2.1.0 section 3.33). */
497 : :
498 : 454 : class sarif_logical_location : public sarif_object
499 : : {
500 : : };
501 : :
502 : : /* Subclass of sarif_object for SARIF "locationRelationship" objects
503 : : (SARIF v2.1.0 section 3.34). */
504 : :
505 : : class sarif_location_relationship : public sarif_object
506 : : {
507 : : public:
508 : : sarif_location_relationship (sarif_location &target,
509 : : sarif_location_manager &loc_mgr);
510 : :
511 : : long get_target_id () const;
512 : :
513 : : void lazily_add_kind (enum location_relationship_kind kind);
514 : :
515 : : private:
516 : : auto_sbitmap m_kinds;
517 : : };
518 : :
519 : : /* Subclass of sarif_object for SARIF "codeFlow" objects
520 : : (SARIF v2.1.0 section 3.36). */
521 : :
522 : : class sarif_code_flow : public sarif_object
523 : : {
524 : : public:
525 : : sarif_code_flow (sarif_result &parent,
526 : : unsigned idx_within_parent);
527 : :
528 : 9 : sarif_result &get_parent () const { return m_parent; }
529 : 9 : unsigned get_index_within_parent () const { return m_idx_within_parent; }
530 : :
531 : : sarif_thread_flow &
532 : : get_or_append_thread_flow (const diagnostic_thread &thread,
533 : : diagnostic_thread_id_t thread_id);
534 : :
535 : : sarif_thread_flow &
536 : : get_thread_flow (diagnostic_thread_id_t thread_id);
537 : :
538 : : void add_location (sarif_thread_flow_location &);
539 : :
540 : : sarif_thread_flow_location &
541 : : get_thread_flow_loc_obj (diagnostic_event_id_t event_id) const;
542 : :
543 : : private:
544 : : sarif_result &m_parent;
545 : : const unsigned m_idx_within_parent;
546 : :
547 : : hash_map<int_hash<diagnostic_thread_id_t, -1, -2>,
548 : : sarif_thread_flow *> m_thread_id_map; // borrowed ptr
549 : : json::array *m_thread_flows_arr; // borrowed
550 : :
551 : : /* Vec of borrowed ptr, allowing for going easily from
552 : : an event_id to the corresponding threadFlowLocation object. */
553 : : std::vector<sarif_thread_flow_location *> m_all_tfl_objs;
554 : : };
555 : :
556 : : /* Subclass of sarif_object for SARIF "threadFlow" objects
557 : : (SARIF v2.1.0 section 3.37). */
558 : :
559 : : class sarif_thread_flow : public sarif_object
560 : : {
561 : : public:
562 : : sarif_thread_flow (sarif_code_flow &parent,
563 : : const diagnostic_thread &thread,
564 : : unsigned idx_within_parent);
565 : :
566 : 9 : sarif_code_flow &get_parent () const { return m_parent; }
567 : 9 : unsigned get_index_within_parent () const { return m_idx_within_parent; }
568 : :
569 : : sarif_thread_flow_location &add_location ();
570 : :
571 : : private:
572 : : sarif_code_flow &m_parent;
573 : : json::array *m_locations_arr; // borrowed
574 : : const unsigned m_idx_within_parent;
575 : : };
576 : :
577 : : /* Subclass of sarif_object for SARIF "threadFlowLocation" objects
578 : : (SARIF v2.1.0 section 3.38). */
579 : :
580 : : class sarif_thread_flow_location : public sarif_object
581 : : {
582 : : public:
583 : 63 : sarif_thread_flow_location (sarif_thread_flow &parent,
584 : : unsigned idx_within_parent)
585 : 126 : : m_parent (parent),
586 : 126 : m_idx_within_parent (idx_within_parent)
587 : : {
588 : : }
589 : :
590 : 9 : sarif_thread_flow &get_parent () const { return m_parent; }
591 : 9 : unsigned get_index_within_parent () const { return m_idx_within_parent; }
592 : :
593 : : private:
594 : : sarif_thread_flow &m_parent;
595 : : const unsigned m_idx_within_parent;
596 : : };
597 : :
598 : : /* Subclass of sarif_object for SARIF "reportingDescriptor" objects
599 : : (SARIF v2.1.0 section 3.49). */
600 : :
601 : 79 : class sarif_reporting_descriptor : public sarif_object {};
602 : :
603 : : /* Subclass of sarif_object for SARIF "reportingDescriptorReference" objects
604 : : (SARIF v2.1.0 section 3.53). */
605 : :
606 : 18 : class sarif_reporting_descriptor_reference : public sarif_object {};
607 : :
608 : : /* Subclass of sarif_object for SARIF "toolComponentReference" objects
609 : : (SARIF v2.1.0 section 3.54). */
610 : :
611 : 18 : class sarif_tool_component_reference : public sarif_object {};
612 : :
613 : : /* Subclass of sarif_object for SARIF "fix" objects
614 : : (SARIF v2.1.0 section 3.55). */
615 : :
616 : 8 : class sarif_fix : public sarif_object {};
617 : :
618 : : /* Subclass of sarif_object for SARIF "artifactChange" objects
619 : : (SARIF v2.1.0 section 3.56). */
620 : :
621 : 8 : class sarif_artifact_change : public sarif_object {};
622 : :
623 : : /* Subclass of sarif_object for SARIF "replacement" objects
624 : : (SARIF v2.1.0 section 3.57). */
625 : :
626 : 8 : class sarif_replacement : public sarif_object {};
627 : :
628 : : /* Subclass of sarif_object for SARIF "notification" objects
629 : : (SARIF v2.1.0 section 3.58).
630 : :
631 : : This subclass is specifically for notifying when an
632 : : internal compiler error occurs. */
633 : :
634 : : class sarif_ice_notification : public sarif_location_manager
635 : : {
636 : : public:
637 : : sarif_ice_notification (const diagnostic_info &diagnostic,
638 : : sarif_builder &builder,
639 : : std::unique_ptr<json::object> backtrace);
640 : :
641 : : void
642 : : add_related_location (std::unique_ptr<sarif_location> location_obj,
643 : : sarif_builder &builder) final override;
644 : : };
645 : :
646 : : /* Abstract base class for use when making an "artifactContent"
647 : : object (SARIF v2.1.0 section 3.3): generate a value for the
648 : : 3.3.4 "rendered" property.
649 : : Can return nullptr, for "no property". */
650 : :
651 : 617 : class content_renderer
652 : : {
653 : : public:
654 : 617 : virtual ~content_renderer () {}
655 : :
656 : : virtual std::unique_ptr<sarif_multiformat_message_string>
657 : : render (const sarif_builder &builder) const = 0;
658 : : };
659 : :
660 : : /* Concrete buffering implementation subclass for JSON output. */
661 : :
662 : : class diagnostic_sarif_format_buffer : public diagnostic_per_format_buffer
663 : : {
664 : : public:
665 : : friend class sarif_output_format;
666 : :
667 : 17 : diagnostic_sarif_format_buffer (sarif_builder &builder)
668 : 17 : : m_builder (builder)
669 : : {}
670 : :
671 : : void dump (FILE *out, int indent) const final override;
672 : : bool empty_p () const final override;
673 : : void move_to (diagnostic_per_format_buffer &dest) final override;
674 : : void clear () final override;
675 : : void flush () final override;
676 : :
677 : 21 : void add_result (std::unique_ptr<sarif_result> result)
678 : : {
679 : 42 : m_results.push_back (std::move (result));
680 : : }
681 : :
682 : : size_t num_results () const { return m_results.size (); }
683 : : sarif_result &get_result (size_t idx) { return *m_results[idx]; }
684 : :
685 : : private:
686 : : sarif_builder &m_builder;
687 : : std::vector<std::unique_ptr<sarif_result>> m_results;
688 : : };
689 : :
690 : : /* Classes for abstracting away JSON vs other serialization formats. */
691 : :
692 : : // class sarif_serialization_format_json : public sarif_serialization_format
693 : :
694 : : void
695 : 95 : sarif_serialization_format_json::write_to_file (FILE *outf,
696 : : const json::value &top)
697 : : {
698 : 95 : top.dump (outf, m_formatted);
699 : 95 : fprintf (outf, "\n");
700 : 95 : }
701 : :
702 : : /* A class for managing SARIF output (for -fdiagnostics-format=sarif-stderr
703 : : and -fdiagnostics-format=sarif-file).
704 : :
705 : : As diagnostics occur, we build "result" JSON objects, and
706 : : accumulate state:
707 : : - which source files are referenced
708 : : - which warnings are emitted
709 : : - which CWEs are used
710 : :
711 : : At the end of the compile, we use the above to build the full SARIF
712 : : object tree, adding the result objects to the correct place, and
713 : : creating objects for the various source files, warnings and CWEs
714 : : referenced.
715 : :
716 : : Implemented:
717 : : - fix-it hints
718 : : - CWE metadata
719 : : - diagnostic groups (see limitations below)
720 : : - logical locations (e.g. cfun)
721 : : - labelled ranges (as annotations)
722 : : - secondary ranges without labels (as related locations)
723 : :
724 : : Known limitations:
725 : : - GCC supports nesting of diagnostics (one-deep nesting via
726 : : auto_diagnostic_group, and arbitrary nesting via
727 : : auto_diagnostic_nesting_level). These are captured in the SARIF
728 : : as related locations, and so we only capture location and message
729 : : information from such nested diagnostics (e.g. we ignore fix-it
730 : : hints on them). Diagnostics within an auto_diagnostic_nesting_level
731 : : have their nesting level captured as a property.
732 : : - although we capture command-line arguments (section 3.20.2), we don't
733 : : yet capture response files.
734 : : - doesn't capture "artifact.encoding" property
735 : : (SARIF v2.1.0 section 3.24.9).
736 : : - doesn't capture hashes of the source files
737 : : ("artifact.hashes" property (SARIF v2.1.0 section 3.24.11).
738 : : - doesn't capture the "analysisTarget" property
739 : : (SARIF v2.1.0 section 3.27.13).
740 : : - doesn't capture -Werror cleanly
741 : : - doesn't capture inlining information (can SARIF handle this?)
742 : : - doesn't capture macro expansion information (can SARIF handle this?).
743 : : - doesn't capture any diagnostic_metadata::rules associated with
744 : : a diagnostic. */
745 : :
746 : : class sarif_builder
747 : : {
748 : : public:
749 : : friend class diagnostic_sarif_format_buffer;
750 : :
751 : : sarif_builder (diagnostic_context &context,
752 : : pretty_printer &printer,
753 : : const line_maps *line_maps,
754 : : std::unique_ptr<sarif_serialization_format> serialization_format,
755 : : const sarif_generation_options &sarif_gen_opts);
756 : : ~sarif_builder ();
757 : :
758 : 271 : void set_printer (pretty_printer &printer)
759 : : {
760 : 271 : m_printer = &printer;
761 : : }
762 : :
763 : : const logical_location_manager *
764 : : get_logical_location_manager () const
765 : : {
766 : : return m_logical_loc_mgr;
767 : : }
768 : :
769 : : void
770 : : set_main_input_filename (const char *name);
771 : :
772 : : void on_report_diagnostic (const diagnostic_info &diagnostic,
773 : : diagnostic_t orig_diag_kind,
774 : : diagnostic_sarif_format_buffer *buffer);
775 : : void emit_diagram (const diagnostic_diagram &diagram);
776 : : void end_group ();
777 : :
778 : 271 : std::unique_ptr<sarif_result> take_current_result ()
779 : : {
780 : 271 : return std::move (m_cur_group_result);
781 : : }
782 : :
783 : : std::unique_ptr<sarif_log> flush_to_object ();
784 : : void flush_to_file (FILE *outf);
785 : :
786 : : std::unique_ptr<json::array>
787 : : make_locations_arr (sarif_location_manager &loc_mgr,
788 : : const diagnostic_info &diagnostic,
789 : : enum diagnostic_artifact_role role);
790 : : std::unique_ptr<sarif_location>
791 : : make_location_object (sarif_location_manager &loc_mgr,
792 : : const rich_location &rich_loc,
793 : : logical_location logical_loc,
794 : : enum diagnostic_artifact_role role);
795 : : std::unique_ptr<sarif_location>
796 : : make_location_object (sarif_location_manager &loc_mgr,
797 : : location_t where,
798 : : enum diagnostic_artifact_role role);
799 : : std::unique_ptr<sarif_message>
800 : : make_message_object (const char *msg) const;
801 : : std::unique_ptr<sarif_message>
802 : : make_message_object_for_diagram (const diagnostic_diagram &diagram);
803 : : std::unique_ptr<sarif_artifact_content>
804 : : maybe_make_artifact_content_object (const char *filename) const;
805 : :
806 : : std::unique_ptr<sarif_artifact_location>
807 : : make_artifact_location_object (const char *filename);
808 : :
809 : : const sarif_code_flow *
810 : 9 : get_code_flow_for_event_ids () const
811 : : {
812 : 9 : return m_current_code_flow;
813 : : }
814 : :
815 : 290 : diagnostic_context &get_context () const { return m_context; }
816 : 125 : pretty_printer *get_printer () const { return m_printer; }
817 : 271 : token_printer &get_token_printer () { return m_token_printer; }
818 : 526 : enum sarif_version get_version () const { return m_sarif_gen_opts.m_version; }
819 : :
820 : 96 : size_t num_results () const { return m_results_array->size (); }
821 : 16 : sarif_result &get_result (size_t idx)
822 : : {
823 : 16 : auto element = (*m_results_array)[idx];
824 : 16 : gcc_assert (element);
825 : 16 : return *static_cast<sarif_result *> (element);
826 : : }
827 : :
828 : : const sarif_generation_options &get_opts () const { return m_sarif_gen_opts; }
829 : :
830 : : std::unique_ptr<sarif_logical_location>
831 : : make_minimal_sarif_logical_location (logical_location);
832 : :
833 : : private:
834 : 399 : class sarif_token_printer : public token_printer
835 : : {
836 : : public:
837 : 399 : sarif_token_printer (sarif_builder &builder)
838 : 399 : : m_builder (builder)
839 : : {
840 : : }
841 : : void print_tokens (pretty_printer *pp,
842 : : const pp_token_list &tokens) final override;
843 : : private:
844 : : sarif_builder &m_builder;
845 : : };
846 : :
847 : : std::unique_ptr<sarif_result>
848 : : make_result_object (const diagnostic_info &diagnostic,
849 : : diagnostic_t orig_diag_kind,
850 : : unsigned idx_within_parent);
851 : : void
852 : : add_any_include_chain (sarif_location_manager &loc_mgr,
853 : : sarif_location &location_obj,
854 : : location_t where);
855 : : void
856 : : set_any_logical_locs_arr (sarif_location &location_obj,
857 : : logical_location logical_loc);
858 : : std::unique_ptr<sarif_location>
859 : : make_location_object (sarif_location_manager &loc_mgr,
860 : : const diagnostic_event &event,
861 : : enum diagnostic_artifact_role role);
862 : : std::unique_ptr<sarif_code_flow>
863 : : make_code_flow_object (sarif_result &result,
864 : : unsigned idx_within_parent,
865 : : const diagnostic_path &path);
866 : : void
867 : : populate_thread_flow_location_object (sarif_result &result,
868 : : sarif_thread_flow_location &thread_flow_loc_obj,
869 : : const diagnostic_event &event,
870 : : int event_execution_idx);
871 : : std::unique_ptr<json::array>
872 : : maybe_make_kinds_array (diagnostic_event::meaning m) const;
873 : : std::unique_ptr<sarif_physical_location>
874 : : maybe_make_physical_location_object (location_t loc,
875 : : enum diagnostic_artifact_role role,
876 : : int column_override,
877 : : const content_renderer *snippet_renderer);
878 : : std::unique_ptr<sarif_artifact_location>
879 : : make_artifact_location_object (location_t loc);
880 : : std::unique_ptr<sarif_artifact_location>
881 : : make_artifact_location_object_for_pwd () const;
882 : : std::unique_ptr<sarif_region>
883 : : maybe_make_region_object (location_t loc,
884 : : int column_override) const;
885 : : std::unique_ptr<sarif_region>
886 : : maybe_make_region_object_for_context (location_t loc,
887 : : const content_renderer *snippet_renderer) const;
888 : : std::unique_ptr<sarif_region>
889 : : make_region_object_for_hint (const fixit_hint &hint) const;
890 : :
891 : : int
892 : : ensure_sarif_logical_location_for (logical_location k);
893 : :
894 : : std::unique_ptr<sarif_multiformat_message_string>
895 : : make_multiformat_message_string (const char *msg) const;
896 : : std::unique_ptr<sarif_log>
897 : : make_top_level_object (std::unique_ptr<sarif_invocation> invocation_obj,
898 : : std::unique_ptr<json::array> results);
899 : : std::unique_ptr<sarif_run>
900 : : make_run_object (std::unique_ptr<sarif_invocation> invocation_obj,
901 : : std::unique_ptr<json::array> results);
902 : : std::unique_ptr<sarif_tool>
903 : : make_tool_object ();
904 : : std::unique_ptr<sarif_tool_component>
905 : : make_driver_tool_component_object ();
906 : : std::unique_ptr<json::array> maybe_make_taxonomies_array () const;
907 : : std::unique_ptr<sarif_tool_component>
908 : : maybe_make_cwe_taxonomy_object () const;
909 : : std::unique_ptr<sarif_tool_component_reference>
910 : : make_tool_component_reference_object_for_cwe () const;
911 : : std::unique_ptr<sarif_reporting_descriptor>
912 : : make_reporting_descriptor_object_for_warning (const diagnostic_info &diagnostic,
913 : : diagnostic_t orig_diag_kind,
914 : : const char *option_text);
915 : : std::unique_ptr<sarif_reporting_descriptor>
916 : : make_reporting_descriptor_object_for_cwe_id (int cwe_id) const;
917 : : std::unique_ptr<sarif_reporting_descriptor_reference>
918 : : make_reporting_descriptor_reference_object_for_cwe_id (int cwe_id);
919 : : sarif_artifact &
920 : : get_or_create_artifact (const char *filename,
921 : : enum diagnostic_artifact_role role,
922 : : bool embed_contents);
923 : : char *
924 : : get_source_lines (const char *filename,
925 : : int start_line,
926 : : int end_line) const;
927 : : std::unique_ptr<sarif_artifact_content>
928 : : maybe_make_artifact_content_object (const char *filename,
929 : : int start_line,
930 : : int end_line,
931 : : const content_renderer *r) const;
932 : : std::unique_ptr<sarif_fix>
933 : : make_fix_object (const rich_location &rich_loc);
934 : : std::unique_ptr<sarif_artifact_change>
935 : : make_artifact_change_object (const rich_location &richloc);
936 : : std::unique_ptr<sarif_replacement>
937 : : make_replacement_object (const fixit_hint &hint) const;
938 : : std::unique_ptr<sarif_artifact_content>
939 : : make_artifact_content_object (const char *text) const;
940 : : int get_sarif_column (expanded_location exploc) const;
941 : :
942 : : std::unique_ptr<json::object>
943 : : make_stack_from_backtrace ();
944 : :
945 : : diagnostic_context &m_context;
946 : : pretty_printer *m_printer;
947 : : const line_maps *m_line_maps;
948 : : sarif_token_printer m_token_printer;
949 : :
950 : : const logical_location_manager *m_logical_loc_mgr;
951 : :
952 : : /* The JSON object for the invocation object. */
953 : : std::unique_ptr<sarif_invocation> m_invocation_obj;
954 : :
955 : : /* The JSON array of pending diagnostics. */
956 : : std::unique_ptr<json::array> m_results_array;
957 : :
958 : : /* The JSON object for the result object (if any) in the current
959 : : diagnostic group. */
960 : : std::unique_ptr<sarif_result> m_cur_group_result;
961 : :
962 : : /* Ideally we'd use std::unique_ptr<sarif_artifact> here, but I had
963 : : trouble getting this to work when building with GCC 4.8. */
964 : : ordered_hash_map <nofree_string_hash,
965 : : sarif_artifact *> m_filename_to_artifact_map;
966 : :
967 : : bool m_seen_any_relative_paths;
968 : : hash_set <free_string_hash> m_rule_id_set;
969 : : std::unique_ptr<json::array> m_rules_arr;
970 : :
971 : : /* The set of all CWE IDs we've seen, if any. */
972 : : hash_set <int_hash <int, 0, 1> > m_cwe_id_set;
973 : :
974 : : std::unique_ptr<sarif_array_of_unique<sarif_logical_location>> m_cached_logical_locs;
975 : :
976 : : int m_tabstop;
977 : :
978 : : std::unique_ptr<sarif_serialization_format> m_serialization_format;
979 : : const sarif_generation_options m_sarif_gen_opts;
980 : :
981 : : unsigned m_next_result_idx;
982 : : sarif_code_flow *m_current_code_flow;
983 : : };
984 : :
985 : : /* class sarif_object : public json::object. */
986 : :
987 : : sarif_property_bag &
988 : 278 : sarif_object::get_or_create_properties ()
989 : : {
990 : 278 : json::value *properties_val = get ("properties");
991 : 278 : if (properties_val)
992 : : {
993 : 28 : if (properties_val->get_kind () == json::JSON_OBJECT)
994 : : return *static_cast <sarif_property_bag *> (properties_val);
995 : : }
996 : :
997 : 250 : sarif_property_bag *bag = new sarif_property_bag ();
998 : 250 : set ("properties", bag);
999 : 250 : return *bag;
1000 : : }
1001 : :
1002 : : /* class sarif_invocation : public sarif_object. */
1003 : :
1004 : 399 : sarif_invocation::sarif_invocation (sarif_builder &builder,
1005 : 399 : const char * const *original_argv)
1006 : 399 : : m_notifications_arr (std::make_unique<json::array> ()),
1007 : 399 : m_success (true)
1008 : : {
1009 : : // "arguments" property (SARIF v2.1.0 section 3.20.2)
1010 : 399 : if (original_argv)
1011 : : {
1012 : 95 : auto arguments_arr = std::make_unique<json::array> ();
1013 : 3522 : for (size_t i = 0; original_argv[i]; ++i)
1014 : 3427 : arguments_arr->append_string (original_argv[i]);
1015 : 95 : set<json::array> ("arguments", std::move (arguments_arr));
1016 : 95 : }
1017 : :
1018 : : // "workingDirectory" property (SARIF v2.1.0 section 3.20.19)
1019 : 399 : if (const char *pwd = getpwd ())
1020 : 798 : set<sarif_artifact_location> ("workingDirectory",
1021 : 399 : builder.make_artifact_location_object (pwd));
1022 : :
1023 : : // "startTimeUtc" property (SARIF v2.1.0 section 3.20.7)
1024 : 798 : set<json::string> ("startTimeUtc",
1025 : 399 : make_date_time_string_for_current_time ());
1026 : 399 : }
1027 : :
1028 : : /* Handle an internal compiler error DIAGNOSTIC.
1029 : : Add an object representing the ICE to the notifications array. */
1030 : :
1031 : : void
1032 : 4 : sarif_invocation::add_notification_for_ice (const diagnostic_info &diagnostic,
1033 : : sarif_builder &builder,
1034 : : std::unique_ptr<json::object> backtrace)
1035 : : {
1036 : 4 : m_success = false;
1037 : :
1038 : 4 : auto notification
1039 : : = std::make_unique<sarif_ice_notification> (diagnostic,
1040 : : builder,
1041 : 4 : std::move (backtrace));
1042 : :
1043 : : /* Support for related locations within a notification was added
1044 : : in SARIF 2.2; see https://github.com/oasis-tcs/sarif-spec/issues/540 */
1045 : 4 : if (builder.get_version () >= sarif_version::v2_2_prerelease_2024_08_08)
1046 : 1 : notification->process_worklist (builder);
1047 : :
1048 : 4 : m_notifications_arr->append<sarif_ice_notification>
1049 : 4 : (std::move (notification));
1050 : 4 : }
1051 : :
1052 : : void
1053 : 263 : sarif_invocation::prepare_to_flush (sarif_builder &builder)
1054 : : {
1055 : 263 : const diagnostic_context &context = builder.get_context ();
1056 : :
1057 : : /* "executionSuccessful" property (SARIF v2.1.0 section 3.20.14). */
1058 : 263 : if (context.execution_failed_p ())
1059 : 197 : m_success = false;
1060 : 263 : set_bool ("executionSuccessful", m_success);
1061 : :
1062 : : /* "toolExecutionNotifications" property (SARIF v2.1.0 section 3.20.21). */
1063 : 263 : set ("toolExecutionNotifications", std::move (m_notifications_arr));
1064 : :
1065 : : /* Call client hook, allowing it to create a custom property bag for
1066 : : this object (SARIF v2.1.0 section 3.8) e.g. for recording time vars. */
1067 : 263 : if (auto client_data_hooks = context.get_client_data_hooks ())
1068 : 95 : client_data_hooks->add_sarif_invocation_properties (*this);
1069 : :
1070 : : // "endTimeUtc" property (SARIF v2.1.0 section 3.20.8);
1071 : 526 : set<json::string> ("endTimeUtc",
1072 : 263 : make_date_time_string_for_current_time ());
1073 : 263 : }
1074 : :
1075 : : /* class sarif_artifact : public sarif_object. */
1076 : :
1077 : : /* Add ROLE to this artifact's roles.
1078 : : If EMBED_CONTENTS is true, then flag that we will attempt to embed the
1079 : : contents of this artifact when writing it out. */
1080 : :
1081 : : void
1082 : 907 : sarif_artifact::add_role (enum diagnostic_artifact_role role,
1083 : : bool embed_contents)
1084 : : {
1085 : : /* TODO(SARIF 2.2): "scannedFile" is to be added as a role in SARIF 2.2;
1086 : : see https://github.com/oasis-tcs/sarif-spec/issues/459
1087 : :
1088 : : For now, skip them.
1089 : : Ultimately, we probably shouldn't bother embedding the contents
1090 : : of such artifacts, just the snippets. */
1091 : 907 : if (role == diagnostic_artifact_role::scanned_file)
1092 : : return;
1093 : :
1094 : 887 : if (embed_contents)
1095 : 616 : m_embed_contents = true;
1096 : :
1097 : : /* In SARIF v2.1.0 section 3.24.6 "roles" property:
1098 : : "resultFile" is for an artifact
1099 : : "which the analysis tool was not explicitly instructed to scan",
1100 : : whereas "analysisTarget" is for one where the
1101 : : "analysis tool was instructed to scan this artifact".
1102 : : Hence the latter excludes the former. */
1103 : 887 : if (role == diagnostic_artifact_role::result_file)
1104 : 425 : if (bitmap_bit_p (m_roles, (int)diagnostic_artifact_role::analysis_target))
1105 : : return;
1106 : :
1107 : 477 : bitmap_set_bit (m_roles, (int)role);
1108 : : }
1109 : :
1110 : : /* Populate the "contents" property (SARIF v2.1.0 section 3.24.8).
1111 : : We do this after initialization to
1112 : : (a) ensure that any charset options have been set
1113 : : (b) only populate it for artifacts that are referenced by a location. */
1114 : :
1115 : : void
1116 : 223 : sarif_artifact::populate_contents (sarif_builder &builder)
1117 : : {
1118 : 223 : if (auto artifact_content_obj
1119 : 223 : = builder.maybe_make_artifact_content_object (m_filename))
1120 : 223 : set<sarif_artifact_content> ("contents", std::move (artifact_content_obj));
1121 : 223 : }
1122 : :
1123 : : /* Get a string for ROLE corresponding to the
1124 : : SARIF v2.1.0 section 3.24.6 "roles" property. */
1125 : :
1126 : : static const char *
1127 : 299 : get_artifact_role_string (enum diagnostic_artifact_role role)
1128 : : {
1129 : 299 : switch (role)
1130 : : {
1131 : 0 : default:
1132 : 0 : gcc_unreachable ();
1133 : : case diagnostic_artifact_role::analysis_target:
1134 : : return "analysisTarget";
1135 : 0 : case diagnostic_artifact_role::debug_output_file:
1136 : 0 : return "debugOutputFile";
1137 : 13 : case diagnostic_artifact_role::result_file:
1138 : 13 : return "resultFile";
1139 : 0 : case diagnostic_artifact_role::scanned_file:
1140 : 0 : return "scannedFile";
1141 : 23 : case diagnostic_artifact_role::traced_file:
1142 : 23 : return "tracedFile";
1143 : : }
1144 : : }
1145 : :
1146 : : /* Populate the "roles" property of this sarif_artifact with a new
1147 : : json::array for the artifact.roles property (SARIF v2.1.0 section 3.24.6)
1148 : : containing strings such as "analysisTarget", "resultFile"
1149 : : and/or "tracedFile". */
1150 : :
1151 : : void
1152 : 281 : sarif_artifact::populate_roles ()
1153 : : {
1154 : 281 : if (bitmap_empty_p (m_roles))
1155 : 1 : return;
1156 : 280 : auto roles_arr (std::make_unique<json::array> ());
1157 : 1680 : for (int i = 0; i < (int)diagnostic_artifact_role::NUM_ROLES; i++)
1158 : 1400 : if (bitmap_bit_p (m_roles, i))
1159 : : {
1160 : 299 : enum diagnostic_artifact_role role = (enum diagnostic_artifact_role)i;
1161 : 299 : roles_arr->append_string (get_artifact_role_string (role));
1162 : : }
1163 : 280 : set<json::array> ("roles", std::move (roles_arr));
1164 : 280 : }
1165 : :
1166 : : /* class sarif_location_manager : public sarif_object. */
1167 : :
1168 : : /* Base implementation of sarif_location_manager::add_related_location vfunc.
1169 : :
1170 : : Add LOCATION_OBJ to this object's "relatedLocations" array,
1171 : : creating it if it doesn't yet exist. */
1172 : :
1173 : : void
1174 : 51 : sarif_location_manager::
1175 : : add_related_location (std::unique_ptr<sarif_location> location_obj,
1176 : : sarif_builder &)
1177 : : {
1178 : 51 : if (!m_related_locations_arr)
1179 : : {
1180 : 26 : m_related_locations_arr = new json::array ();
1181 : : /* Give ownership of m_related_locations_arr to json::object;
1182 : : keep a borrowed ptr. */
1183 : 26 : set ("relatedLocations", m_related_locations_arr);
1184 : : }
1185 : 51 : m_related_locations_arr->append (std::move (location_obj));
1186 : 51 : }
1187 : :
1188 : : void
1189 : 25 : sarif_location_manager::
1190 : : add_relationship_to_worklist (sarif_location &location_obj,
1191 : : enum worklist_item::kind kind,
1192 : : location_t where)
1193 : : {
1194 : 50 : m_worklist.push_back (worklist_item (location_obj,
1195 : : kind,
1196 : 25 : where));
1197 : 25 : }
1198 : :
1199 : : /* Process all items in this result's worklist.
1200 : : Doing so may temporarily add new items to the end
1201 : : of the worklist.
1202 : : Handling any item should be "lazy", and thus we should
1203 : : eventually drain the queue and terminate. */
1204 : :
1205 : : void
1206 : 446 : sarif_location_manager::process_worklist (sarif_builder &builder)
1207 : : {
1208 : 470 : while (!m_worklist.empty ())
1209 : : {
1210 : 24 : const worklist_item &item = m_worklist.front ();
1211 : 24 : process_worklist_item (builder, item);
1212 : 24 : m_worklist.pop_front ();
1213 : : }
1214 : 446 : }
1215 : :
1216 : : /* Process one item in this result's worklist, potentially
1217 : : adding new items to the end of the worklist. */
1218 : :
1219 : : void
1220 : 24 : sarif_location_manager::process_worklist_item (sarif_builder &builder,
1221 : : const worklist_item &item)
1222 : : {
1223 : 24 : switch (item.m_kind)
1224 : : {
1225 : 0 : default:
1226 : 0 : gcc_unreachable ();
1227 : 20 : case worklist_item::kind::included_from:
1228 : 20 : {
1229 : 20 : sarif_location &included_loc_obj = item.m_location_obj;
1230 : 20 : sarif_location *includer_loc_obj = nullptr;
1231 : 20 : auto iter = m_included_from_locations.find (item.m_where);
1232 : 20 : if (iter != m_included_from_locations.end ())
1233 : 4 : includer_loc_obj = iter->second;
1234 : : else
1235 : : {
1236 : 16 : std::unique_ptr<sarif_location> new_loc_obj
1237 : : = builder.make_location_object
1238 : : (*this,
1239 : 16 : item.m_where,
1240 : 16 : diagnostic_artifact_role::scanned_file);
1241 : 16 : includer_loc_obj = new_loc_obj.get ();
1242 : 16 : add_related_location (std::move (new_loc_obj), builder);
1243 : 16 : auto kv
1244 : 16 : = std::pair<location_t, sarif_location *> (item.m_where,
1245 : 16 : includer_loc_obj);
1246 : 16 : m_included_from_locations.insert (kv);
1247 : 16 : }
1248 : :
1249 : 20 : includer_loc_obj->lazily_add_relationship
1250 : 20 : (included_loc_obj,
1251 : : location_relationship_kind::includes,
1252 : : *this);
1253 : 20 : included_loc_obj.lazily_add_relationship
1254 : 20 : (*includer_loc_obj,
1255 : : location_relationship_kind::is_included_by,
1256 : : *this);
1257 : : }
1258 : 20 : break;
1259 : 4 : case worklist_item::kind::unlabelled_secondary_location:
1260 : 4 : {
1261 : 4 : sarif_location &primary_loc_obj = item.m_location_obj;
1262 : 4 : sarif_location *secondary_loc_obj = nullptr;
1263 : 4 : auto iter = m_unlabelled_secondary_locations.find (item.m_where);
1264 : 4 : if (iter != m_unlabelled_secondary_locations.end ())
1265 : 0 : secondary_loc_obj = iter->second;
1266 : : else
1267 : : {
1268 : 4 : std::unique_ptr<sarif_location> new_loc_obj
1269 : : = builder.make_location_object
1270 : : (*this,
1271 : 4 : item.m_where,
1272 : 4 : diagnostic_artifact_role::scanned_file);
1273 : 4 : secondary_loc_obj = new_loc_obj.get ();
1274 : 4 : add_related_location (std::move (new_loc_obj), builder);
1275 : 4 : auto kv
1276 : 4 : = std::pair<location_t, sarif_location *> (item.m_where,
1277 : 4 : secondary_loc_obj);
1278 : 4 : m_unlabelled_secondary_locations.insert (kv);
1279 : 4 : }
1280 : 4 : gcc_assert (secondary_loc_obj);
1281 : 4 : primary_loc_obj.lazily_add_relationship
1282 : 4 : (*secondary_loc_obj,
1283 : : location_relationship_kind::relevant,
1284 : : *this);
1285 : : }
1286 : 4 : break;
1287 : : }
1288 : 24 : }
1289 : :
1290 : : /* class sarif_result : public sarif_location_manager. */
1291 : :
1292 : : /* Handle secondary diagnostics that occur within a diagnostic group.
1293 : : The closest SARIF seems to have to nested diagnostics is the
1294 : : "relatedLocations" property of result objects (SARIF v2.1.0 section 3.27.22),
1295 : : so we lazily set this property and populate the array if and when
1296 : : secondary diagnostics occur (such as notes to a warning). */
1297 : :
1298 : : void
1299 : 27 : sarif_result::on_nested_diagnostic (const diagnostic_info &diagnostic,
1300 : : diagnostic_t /*orig_diag_kind*/,
1301 : : sarif_builder &builder)
1302 : : {
1303 : : /* We don't yet generate meaningful logical locations for notes;
1304 : : sometimes these will related to current_function_decl, but
1305 : : often they won't. */
1306 : 27 : auto location_obj
1307 : 27 : = builder.make_location_object (*this, *diagnostic.richloc,
1308 : 27 : logical_location (),
1309 : 27 : diagnostic_artifact_role::result_file);
1310 : 27 : auto message_obj
1311 : 27 : = builder.make_message_object (pp_formatted_text (builder.get_printer ()));
1312 : 27 : pp_clear_output_area (builder.get_printer ());
1313 : 27 : location_obj->set<sarif_message> ("message", std::move (message_obj));
1314 : :
1315 : : /* Add nesting level, as per "P3358R0 SARIF for Structured Diagnostics"
1316 : : https://wg21.link/P3358R0 */
1317 : 27 : sarif_property_bag &bag = location_obj->get_or_create_properties ();
1318 : 27 : bag.set_integer ("nestingLevel",
1319 : 27 : builder.get_context ().get_diagnostic_nesting_level ());
1320 : :
1321 : 27 : add_related_location (std::move (location_obj), builder);
1322 : 27 : }
1323 : :
1324 : : /* Handle diagrams that occur within a diagnostic group.
1325 : : The closest thing in SARIF seems to be to add a location to the
1326 : : "releatedLocations" property (SARIF v2.1.0 section 3.27.22),
1327 : : and to put the diagram into the "message" property of that location
1328 : : (SARIF v2.1.0 section 3.28.5). */
1329 : :
1330 : : void
1331 : 4 : sarif_result::on_diagram (const diagnostic_diagram &diagram,
1332 : : sarif_builder &builder)
1333 : : {
1334 : 4 : auto location_obj = std::make_unique<sarif_location> ();
1335 : 4 : auto message_obj = builder.make_message_object_for_diagram (diagram);
1336 : 4 : location_obj->set<sarif_message> ("message", std::move (message_obj));
1337 : :
1338 : 4 : add_related_location (std::move (location_obj), builder);
1339 : 4 : }
1340 : :
1341 : : /* class sarif_location : public sarif_object. */
1342 : :
1343 : : /* Ensure this location has an "id" and return it.
1344 : : Use LOC_MGR if an id needs to be allocated.
1345 : :
1346 : : See the "id" property (3.28.2).
1347 : :
1348 : : We use this to only assign ids to locations that are
1349 : : referenced by another sarif object; others have no "id". */
1350 : :
1351 : : long
1352 : 44 : sarif_location::lazily_add_id (sarif_location_manager &loc_mgr)
1353 : : {
1354 : 44 : long id = get_id ();
1355 : 44 : if (id != -1)
1356 : : return id;
1357 : 36 : id = loc_mgr.allocate_location_id ();
1358 : 36 : set_integer ("id", id);
1359 : 36 : gcc_assert (id != -1);
1360 : 36 : return id;
1361 : : }
1362 : :
1363 : : /* Get the id of this location, or -1 if it doesn't have one. */
1364 : :
1365 : : long
1366 : 44 : sarif_location::get_id () const
1367 : : {
1368 : 44 : json::value *id = get ("id");
1369 : 44 : if (!id)
1370 : : return -1;
1371 : 8 : gcc_assert (id->get_kind () == json::JSON_INTEGER);
1372 : 8 : return static_cast <json::integer_number *> (id)->get ();
1373 : : }
1374 : :
1375 : : // 3.34.3 kinds property
1376 : : static const char *
1377 : 44 : get_string_for_location_relationship_kind (enum location_relationship_kind kind)
1378 : : {
1379 : 44 : switch (kind)
1380 : : {
1381 : 0 : default:
1382 : 0 : gcc_unreachable ();
1383 : : case location_relationship_kind::includes:
1384 : : return "includes";
1385 : 20 : case location_relationship_kind::is_included_by:
1386 : 20 : return "isIncludedBy";
1387 : 4 : case location_relationship_kind::relevant:
1388 : 4 : return "relevant";
1389 : : }
1390 : : }
1391 : :
1392 : : /* Lazily populate this location's "relationships" property (3.28.7)
1393 : : with the relationship of KIND to TARGET, creating objects
1394 : : as necessary.
1395 : : Use LOC_MGR for any locations that need "id" values. */
1396 : :
1397 : : void
1398 : 44 : sarif_location::lazily_add_relationship (sarif_location &target,
1399 : : enum location_relationship_kind kind,
1400 : : sarif_location_manager &loc_mgr)
1401 : : {
1402 : 44 : sarif_location_relationship &relationship_obj
1403 : 44 : = lazily_add_relationship_object (target, loc_mgr);
1404 : :
1405 : 44 : relationship_obj.lazily_add_kind (kind);
1406 : 44 : }
1407 : :
1408 : : /* Lazily populate this location's "relationships" property (3.28.7)
1409 : : with a location_relationship to TARGET, creating objects
1410 : : as necessary.
1411 : : Use LOC_MGR for any locations that need "id" values. */
1412 : :
1413 : : sarif_location_relationship &
1414 : 44 : sarif_location::lazily_add_relationship_object (sarif_location &target,
1415 : : sarif_location_manager &loc_mgr)
1416 : : {
1417 : : /* See if THIS already has a locationRelationship referencing TARGET. */
1418 : 44 : auto iter = m_relationships_map.find (&target);
1419 : 44 : if (iter != m_relationships_map.end ())
1420 : : {
1421 : : /* We already have a locationRelationship from THIS to TARGET. */
1422 : 0 : sarif_location_relationship *relationship = iter->second;
1423 : 0 : gcc_assert (relationship->get_target_id() == target.get_id ());
1424 : : return *relationship;
1425 : : }
1426 : :
1427 : : // Ensure that THIS has a "relationships" property (3.28.7).
1428 : 44 : json::array &relationships_arr = lazily_add_relationships_array ();
1429 : :
1430 : : /* No existing locationRelationship from THIS to TARGET; make one,
1431 : : record it, and add it to the "relationships" array. */
1432 : 44 : auto relationship_obj
1433 : 44 : = std::make_unique<sarif_location_relationship> (target, loc_mgr);
1434 : 44 : sarif_location_relationship *relationship = relationship_obj.get ();
1435 : 44 : auto kv
1436 : : = std::pair<sarif_location *,
1437 : 44 : sarif_location_relationship *> (&target, relationship);
1438 : 44 : m_relationships_map.insert (kv);
1439 : :
1440 : 44 : relationships_arr.append (std::move (relationship_obj));
1441 : :
1442 : 44 : return *relationship;
1443 : 44 : }
1444 : :
1445 : : /* Ensure this location has a "relationships" array (3.28.7). */
1446 : :
1447 : : json::array &
1448 : 44 : sarif_location::lazily_add_relationships_array ()
1449 : : {
1450 : 44 : const char *const property_name = "relationships";
1451 : 44 : if (json::value *relationships = get (property_name))
1452 : : {
1453 : 8 : gcc_assert (relationships->get_kind () == json::JSON_ARRAY);
1454 : : return *static_cast <json::array *> (relationships);
1455 : : }
1456 : 36 : json::array *relationships_arr = new json::array ();
1457 : 36 : set (property_name, relationships_arr);
1458 : 36 : return *relationships_arr;
1459 : : }
1460 : :
1461 : : /* class sarif_ice_notification : public sarif_location_manager. */
1462 : :
1463 : : /* sarif_ice_notification's ctor.
1464 : : DIAGNOSTIC is an internal compiler error. */
1465 : :
1466 : 4 : sarif_ice_notification::
1467 : : sarif_ice_notification (const diagnostic_info &diagnostic,
1468 : : sarif_builder &builder,
1469 : 4 : std::unique_ptr<json::object> backtrace)
1470 : : {
1471 : : /* "locations" property (SARIF v2.1.0 section 3.58.4). */
1472 : 4 : auto locations_arr
1473 : : = builder.make_locations_arr (*this,
1474 : : diagnostic,
1475 : 4 : diagnostic_artifact_role::result_file);
1476 : 4 : set<json::array> ("locations", std::move (locations_arr));
1477 : :
1478 : : /* "message" property (SARIF v2.1.0 section 3.85.5). */
1479 : 4 : auto message_obj
1480 : 4 : = builder.make_message_object (pp_formatted_text (builder.get_printer ()));
1481 : 4 : pp_clear_output_area (builder.get_printer ());
1482 : 4 : set<sarif_message> ("message", std::move (message_obj));
1483 : :
1484 : : /* "level" property (SARIF v2.1.0 section 3.58.6). */
1485 : 4 : set_string ("level", "error");
1486 : :
1487 : : /* If we have backtrace information, add it as part of a property bag. */
1488 : 4 : if (backtrace)
1489 : : {
1490 : 4 : sarif_property_bag &bag = get_or_create_properties ();
1491 : 4 : bag.set ("gcc/backtrace", std::move (backtrace));
1492 : : }
1493 : 4 : }
1494 : :
1495 : : /* Implementation of sarif_location_manager::add_related_location vfunc
1496 : : for notifications. */
1497 : :
1498 : : void
1499 : 1 : sarif_ice_notification::
1500 : : add_related_location (std::unique_ptr<sarif_location> location_obj,
1501 : : sarif_builder &builder)
1502 : : {
1503 : : /* Support for related locations within a notification was added
1504 : : in SARIF 2.2; see https://github.com/oasis-tcs/sarif-spec/issues/540 */
1505 : 1 : if (builder.get_version () >= sarif_version::v2_2_prerelease_2024_08_08)
1506 : 1 : sarif_location_manager::add_related_location (std::move (location_obj),
1507 : : builder);
1508 : : /* Otherwise implicitly discard LOCATION_OBJ. */
1509 : 1 : }
1510 : :
1511 : : /* class sarif_location_relationship : public sarif_object. */
1512 : :
1513 : 44 : sarif_location_relationship::
1514 : : sarif_location_relationship (sarif_location &target,
1515 : 44 : sarif_location_manager &loc_mgr)
1516 : 44 : : m_kinds ((unsigned)location_relationship_kind::NUM_KINDS)
1517 : : {
1518 : 44 : bitmap_clear (m_kinds);
1519 : 44 : set_integer ("target", target.lazily_add_id (loc_mgr));
1520 : 44 : }
1521 : :
1522 : : long
1523 : 0 : sarif_location_relationship::get_target_id () const
1524 : : {
1525 : 0 : json::value *id = get ("id");
1526 : 0 : gcc_assert (id);
1527 : 0 : return static_cast <json::integer_number *> (id)->get ();
1528 : : }
1529 : :
1530 : : void
1531 : 44 : sarif_location_relationship::
1532 : : lazily_add_kind (enum location_relationship_kind kind)
1533 : : {
1534 : 44 : if (bitmap_bit_p (m_kinds, (int)kind))
1535 : : return; // already have this kind
1536 : 44 : bitmap_set_bit (m_kinds, (int)kind);
1537 : :
1538 : : // 3.34.3 kinds property
1539 : 44 : json::array *kinds_arr = nullptr;
1540 : 44 : if (json::value *kinds_val = get ("kinds"))
1541 : : {
1542 : 0 : gcc_assert (kinds_val->get_kind () == json::JSON_ARRAY);
1543 : : }
1544 : : else
1545 : : {
1546 : 44 : kinds_arr = new json::array ();
1547 : 44 : set ("kinds", kinds_arr);
1548 : : }
1549 : 44 : const char *kind_str = get_string_for_location_relationship_kind (kind);
1550 : 44 : kinds_arr->append_string (kind_str);
1551 : : }
1552 : :
1553 : : /* class sarif_code_flow : public sarif_object. */
1554 : :
1555 : 19 : sarif_code_flow::sarif_code_flow (sarif_result &parent,
1556 : 19 : unsigned idx_within_parent)
1557 : 19 : : m_parent (parent),
1558 : 19 : m_idx_within_parent (idx_within_parent)
1559 : : {
1560 : : /* "threadFlows" property (SARIF v2.1.0 section 3.36.3). */
1561 : 19 : auto thread_flows_arr = std::make_unique<json::array> ();
1562 : 19 : m_thread_flows_arr = thread_flows_arr.get (); // borrowed
1563 : 19 : set<json::array> ("threadFlows", std::move (thread_flows_arr));
1564 : 19 : }
1565 : :
1566 : : sarif_thread_flow &
1567 : 63 : sarif_code_flow::get_or_append_thread_flow (const diagnostic_thread &thread,
1568 : : diagnostic_thread_id_t thread_id)
1569 : : {
1570 : 63 : sarif_thread_flow **slot = m_thread_id_map.get (thread_id);
1571 : 63 : if (slot)
1572 : 43 : return **slot;
1573 : :
1574 : 20 : unsigned next_thread_flow_idx = m_thread_flows_arr->size ();
1575 : 20 : auto thread_flow_obj
1576 : 20 : = std::make_unique<sarif_thread_flow> (*this, thread, next_thread_flow_idx);
1577 : 20 : m_thread_id_map.put (thread_id, thread_flow_obj.get ()); // borrowed
1578 : 20 : sarif_thread_flow *result = thread_flow_obj.get ();
1579 : 20 : m_thread_flows_arr->append<sarif_thread_flow> (std::move (thread_flow_obj));
1580 : 20 : return *result;
1581 : 20 : }
1582 : :
1583 : : sarif_thread_flow &
1584 : 0 : sarif_code_flow::get_thread_flow (diagnostic_thread_id_t thread_id)
1585 : : {
1586 : 0 : sarif_thread_flow **slot = m_thread_id_map.get (thread_id);
1587 : 0 : gcc_assert (slot); // it must already have one
1588 : 0 : return **slot;
1589 : : }
1590 : :
1591 : : void
1592 : 63 : sarif_code_flow::add_location (sarif_thread_flow_location &tfl_obj)
1593 : : {
1594 : 63 : m_all_tfl_objs.push_back (&tfl_obj);
1595 : 63 : }
1596 : :
1597 : : sarif_thread_flow_location &
1598 : 72 : sarif_code_flow::get_thread_flow_loc_obj (diagnostic_event_id_t event_id) const
1599 : : {
1600 : 72 : gcc_assert (event_id.known_p ());
1601 : 72 : gcc_assert ((size_t)event_id.zero_based () < m_all_tfl_objs.size ());
1602 : 72 : sarif_thread_flow_location *tfl_obj = m_all_tfl_objs[event_id.zero_based ()];
1603 : 72 : gcc_assert (tfl_obj);
1604 : 72 : return *tfl_obj;
1605 : : }
1606 : :
1607 : : /* class sarif_thread_flow : public sarif_object. */
1608 : :
1609 : 20 : sarif_thread_flow::sarif_thread_flow (sarif_code_flow &parent,
1610 : : const diagnostic_thread &thread,
1611 : 20 : unsigned idx_within_parent)
1612 : 20 : : m_parent (parent),
1613 : 20 : m_idx_within_parent (idx_within_parent)
1614 : : {
1615 : : /* "id" property (SARIF v2.1.0 section 3.37.2). */
1616 : 20 : label_text name (thread.get_name (false));
1617 : 20 : set_string ("id", name.get ());
1618 : :
1619 : : /* "locations" property (SARIF v2.1.0 section 3.37.6). */
1620 : 20 : m_locations_arr = new json::array ();
1621 : :
1622 : : /* Give ownership of m_locations_arr to json::object;
1623 : : keep a borrowed ptr. */
1624 : 20 : set ("locations", m_locations_arr);
1625 : 20 : }
1626 : :
1627 : : /* Add a sarif_thread_flow_location to this threadFlow object, but
1628 : : don't populate it yet. */
1629 : :
1630 : : sarif_thread_flow_location &
1631 : 63 : sarif_thread_flow::add_location ()
1632 : : {
1633 : 63 : const unsigned thread_flow_location_idx = m_locations_arr->size ();
1634 : 63 : sarif_thread_flow_location *thread_flow_loc_obj
1635 : 63 : = new sarif_thread_flow_location (*this, thread_flow_location_idx);
1636 : 63 : m_locations_arr->append (thread_flow_loc_obj);
1637 : 63 : m_parent.add_location (*thread_flow_loc_obj);
1638 : 63 : return *thread_flow_loc_obj;
1639 : : }
1640 : :
1641 : : /* class sarif_builder. */
1642 : :
1643 : : /* sarif_builder's ctor. */
1644 : :
1645 : 399 : sarif_builder::sarif_builder (diagnostic_context &context,
1646 : : pretty_printer &printer,
1647 : : const line_maps *line_maps,
1648 : : std::unique_ptr<sarif_serialization_format> serialization_format,
1649 : 399 : const sarif_generation_options &sarif_gen_opts)
1650 : 399 : : m_context (context),
1651 : 399 : m_printer (&printer),
1652 : 399 : m_line_maps (line_maps),
1653 : 399 : m_token_printer (*this),
1654 : 399 : m_logical_loc_mgr (nullptr),
1655 : 399 : m_invocation_obj
1656 : : (std::make_unique<sarif_invocation> (*this,
1657 : 399 : context.get_original_argv ())),
1658 : 399 : m_results_array (new json::array ()),
1659 : 399 : m_cur_group_result (nullptr),
1660 : 399 : m_seen_any_relative_paths (false),
1661 : 399 : m_rule_id_set (),
1662 : 399 : m_rules_arr (new json::array ()),
1663 : 399 : m_cached_logical_locs
1664 : : (std::make_unique<sarif_array_of_unique<sarif_logical_location>> ()),
1665 : 399 : m_tabstop (context.m_tabstop),
1666 : 399 : m_serialization_format (std::move (serialization_format)),
1667 : 399 : m_sarif_gen_opts (sarif_gen_opts),
1668 : 399 : m_next_result_idx (0),
1669 : 798 : m_current_code_flow (nullptr)
1670 : : {
1671 : 399 : gcc_assert (m_line_maps);
1672 : 399 : gcc_assert (m_serialization_format);
1673 : :
1674 : 399 : if (auto client_data_hooks = context.get_client_data_hooks ())
1675 : 95 : m_logical_loc_mgr = client_data_hooks->get_logical_location_manager ();
1676 : 399 : }
1677 : :
1678 : 399 : sarif_builder::~sarif_builder ()
1679 : : {
1680 : : /* Normally m_filename_to_artifact_map will have been emptied as part
1681 : : of make_run_object, but this isn't run by all the selftests.
1682 : : Ensure the artifact objects are cleaned up for such cases. */
1683 : 934 : for (auto iter : m_filename_to_artifact_map)
1684 : : {
1685 : 136 : sarif_artifact *artifact_obj = iter.second;
1686 : 136 : delete artifact_obj;
1687 : : }
1688 : 399 : }
1689 : :
1690 : : /* Functions at which to stop the backtrace print. It's not
1691 : : particularly helpful to print the callers of these functions. */
1692 : :
1693 : : static const char * const bt_stop[] =
1694 : : {
1695 : : "main",
1696 : : "toplev::main",
1697 : : "execute_one_pass",
1698 : : "compile_file",
1699 : : };
1700 : :
1701 : : struct bt_closure
1702 : : {
1703 : 4 : bt_closure (sarif_builder &builder,
1704 : : json::array *frames_arr)
1705 : 4 : : m_builder (builder),
1706 : 4 : m_frames_arr (frames_arr)
1707 : : {
1708 : : }
1709 : :
1710 : : sarif_builder &m_builder;
1711 : : json::array *m_frames_arr;
1712 : : };
1713 : :
1714 : : /* A callback function passed to the backtrace_full function. */
1715 : :
1716 : : static int
1717 : 14 : bt_callback (void *data, uintptr_t pc, const char *filename, int lineno,
1718 : : const char *function)
1719 : : {
1720 : 14 : bt_closure *closure = (bt_closure *)data;
1721 : :
1722 : : /* If we don't have any useful information, don't print
1723 : : anything. */
1724 : 14 : if (filename == NULL && function == NULL)
1725 : : return 0;
1726 : :
1727 : : /* Skip functions in diagnostic.cc or diagnostic-global-context.cc. */
1728 : 13 : if (closure->m_frames_arr->size () == 0
1729 : 8 : && filename != NULL
1730 : 13 : && (strcmp (lbasename (filename), "diagnostic.cc") == 0
1731 : 8 : || strcmp (lbasename (filename),
1732 : : "diagnostic-global-context.cc") == 0))
1733 : 4 : return 0;
1734 : :
1735 : : /* Print up to 20 functions. We could make this a --param, but
1736 : : since this is only for debugging just use a constant for now. */
1737 : 9 : if (closure->m_frames_arr->size () >= 20)
1738 : : {
1739 : : /* Returning a non-zero value stops the backtrace. */
1740 : : return 1;
1741 : : }
1742 : :
1743 : 9 : char *alc = NULL;
1744 : 9 : if (function != NULL)
1745 : : {
1746 : 9 : char *str = cplus_demangle_v3 (function,
1747 : : (DMGL_VERBOSE | DMGL_ANSI
1748 : : | DMGL_GNU_V3 | DMGL_PARAMS));
1749 : 9 : if (str != NULL)
1750 : : {
1751 : 8 : alc = str;
1752 : 8 : function = str;
1753 : : }
1754 : :
1755 : 37 : for (size_t i = 0; i < ARRAY_SIZE (bt_stop); ++i)
1756 : : {
1757 : 32 : size_t len = strlen (bt_stop[i]);
1758 : 32 : if (strncmp (function, bt_stop[i], len) == 0
1759 : 4 : && (function[len] == '\0' || function[len] == '('))
1760 : : {
1761 : 4 : if (alc != NULL)
1762 : 4 : free (alc);
1763 : : /* Returning a non-zero value stops the backtrace. */
1764 : 4 : return 1;
1765 : : }
1766 : : }
1767 : : }
1768 : :
1769 : 5 : auto frame_obj = std::make_unique<json::object> ();
1770 : :
1771 : : /* I tried using sarifStack and sarifStackFrame for this
1772 : : but it's not a good fit e.g. PC information. */
1773 : 5 : char buf[128];
1774 : 5 : snprintf (buf, sizeof (buf) - 1, "0x%lx", (unsigned long)pc);
1775 : 5 : frame_obj->set_string ("pc", buf);
1776 : 5 : if (function)
1777 : 5 : frame_obj->set_string ("function", function);
1778 : 5 : if (filename)
1779 : 5 : frame_obj->set_string ("filename", filename);
1780 : 5 : frame_obj->set_integer ("lineno", lineno);
1781 : 5 : closure->m_frames_arr->append (std::move (frame_obj));
1782 : :
1783 : 5 : if (alc != NULL)
1784 : 4 : free (alc);
1785 : :
1786 : 5 : return 0;
1787 : 5 : }
1788 : :
1789 : : /* Attempt to generate a JSON object representing a backtrace,
1790 : : for adding to ICE notifications. */
1791 : :
1792 : : std::unique_ptr<json::object>
1793 : 4 : sarif_builder::make_stack_from_backtrace ()
1794 : : {
1795 : 4 : auto frames_arr = std::make_unique<json::array> ();
1796 : :
1797 : 4 : backtrace_state *state = nullptr;
1798 : 4 : state = backtrace_create_state (nullptr, 0, nullptr, nullptr);
1799 : 4 : bt_closure closure (*this, frames_arr.get ());
1800 : 4 : const int frames_to_skip = 5;
1801 : 4 : if (state != nullptr)
1802 : 4 : backtrace_full (state, frames_to_skip, bt_callback, nullptr,
1803 : : (void *) &closure);
1804 : :
1805 : 4 : if (frames_arr->size () == 0)
1806 : 0 : return nullptr;
1807 : :
1808 : 4 : auto stack = std::make_unique<json::object> ();
1809 : 4 : stack->set ("frames", std::move (frames_arr));
1810 : 4 : return stack;
1811 : 4 : }
1812 : :
1813 : : void
1814 : 271 : sarif_builder::set_main_input_filename (const char *name)
1815 : : {
1816 : : /* Mark NAME as the artifact that the tool was instructed to scan.
1817 : : Only quote the contents if it gets referenced by physical locations,
1818 : : since otherwise the "no diagnostics" case would quote the main input
1819 : : file, and doing so noticeably bloated the output seen in analyzer
1820 : : integration testing (build directory went from 20G -> 21G). */
1821 : 271 : if (name)
1822 : 271 : get_or_create_artifact (name,
1823 : : diagnostic_artifact_role::analysis_target,
1824 : : false);
1825 : 271 : }
1826 : :
1827 : : /* Implementation of "on_report_diagnostic" for SARIF output. */
1828 : :
1829 : : void
1830 : 489 : sarif_builder::on_report_diagnostic (const diagnostic_info &diagnostic,
1831 : : diagnostic_t orig_diag_kind,
1832 : : diagnostic_sarif_format_buffer *buffer)
1833 : : {
1834 : 489 : pp_output_formatted_text (m_printer, m_context.get_urlifier ());
1835 : :
1836 : 489 : if (diagnostic.kind == DK_ICE || diagnostic.kind == DK_ICE_NOBT)
1837 : : {
1838 : 4 : std::unique_ptr<json::object> stack = make_stack_from_backtrace ();
1839 : 4 : m_invocation_obj->add_notification_for_ice (diagnostic, *this,
1840 : : std::move (stack));
1841 : :
1842 : : /* Print a header for the remaining output to stderr, and
1843 : : return, attempting to print the usual ICE messages to
1844 : : stderr. Hopefully this will be helpful to the user in
1845 : : indicating what's gone wrong (also for DejaGnu, for pruning
1846 : : those messages). */
1847 : 4 : fnotice (stderr, "Internal compiler error:\n");
1848 : :
1849 : 4 : return;
1850 : 4 : }
1851 : :
1852 : 485 : if (buffer)
1853 : : {
1854 : : /* When buffering, we can only handle top-level results. */
1855 : 21 : gcc_assert (!m_cur_group_result);
1856 : 21 : buffer->add_result (make_result_object (diagnostic, orig_diag_kind,
1857 : 21 : m_next_result_idx++));
1858 : 21 : return;
1859 : : }
1860 : :
1861 : 464 : if (m_cur_group_result)
1862 : : /* Nested diagnostic. */
1863 : 27 : m_cur_group_result->on_nested_diagnostic (diagnostic,
1864 : : orig_diag_kind,
1865 : : *this);
1866 : : else
1867 : : {
1868 : : /* Top-level diagnostic. */
1869 : 437 : m_cur_group_result = make_result_object (diagnostic, orig_diag_kind,
1870 : 437 : m_next_result_idx++);
1871 : : }
1872 : : }
1873 : :
1874 : : /* Implementation of diagnostic_context::m_diagrams.m_emission_cb
1875 : : for SARIF output. */
1876 : :
1877 : : void
1878 : 4 : sarif_builder::emit_diagram (const diagnostic_diagram &diagram)
1879 : : {
1880 : : /* We must be within the emission of a top-level diagnostic. */
1881 : 4 : gcc_assert (m_cur_group_result);
1882 : 4 : m_cur_group_result->on_diagram (diagram, *this);
1883 : 4 : }
1884 : :
1885 : : /* Implementation of "end_group_cb" for SARIF output. */
1886 : :
1887 : : void
1888 : 462 : sarif_builder::end_group ()
1889 : : {
1890 : 462 : if (m_cur_group_result)
1891 : : {
1892 : 437 : m_cur_group_result->process_worklist (*this);
1893 : 437 : m_results_array->append<sarif_result> (std::move (m_cur_group_result));
1894 : : }
1895 : 462 : }
1896 : :
1897 : : /* Create a top-level object, and add it to all the results
1898 : : (and other entities) we've seen so far, moving ownership
1899 : : to the object. */
1900 : :
1901 : : std::unique_ptr<sarif_log>
1902 : 263 : sarif_builder::flush_to_object ()
1903 : : {
1904 : 263 : m_invocation_obj->prepare_to_flush (*this);
1905 : 263 : std::unique_ptr<sarif_log> top
1906 : 263 : = make_top_level_object (std::move (m_invocation_obj),
1907 : 263 : std::move (m_results_array));
1908 : 263 : return top;
1909 : : }
1910 : :
1911 : : /* Create a top-level object, and add it to all the results
1912 : : (and other entities) we've seen so far.
1913 : :
1914 : : Flush it all to OUTF. */
1915 : :
1916 : : void
1917 : 95 : sarif_builder::flush_to_file (FILE *outf)
1918 : : {
1919 : 95 : std::unique_ptr<sarif_log> top = flush_to_object ();
1920 : 95 : m_serialization_format->write_to_file (outf, *top);
1921 : 95 : }
1922 : :
1923 : : /* Attempt to convert DIAG_KIND to a suitable value for the "level"
1924 : : property (SARIF v2.1.0 section 3.27.10).
1925 : :
1926 : : Return nullptr if there isn't one. */
1927 : :
1928 : : static const char *
1929 : 458 : maybe_get_sarif_level (diagnostic_t diag_kind)
1930 : : {
1931 : 0 : switch (diag_kind)
1932 : : {
1933 : : case DK_WARNING:
1934 : : return "warning";
1935 : 234 : case DK_ERROR:
1936 : 0 : return "error";
1937 : 0 : case DK_NOTE:
1938 : 0 : case DK_ANACHRONISM:
1939 : 0 : return "note";
1940 : 0 : default:
1941 : 0 : return nullptr;
1942 : : }
1943 : : }
1944 : :
1945 : : /* Make a string for DIAG_KIND suitable for use a ruleId
1946 : : (SARIF v2.1.0 section 3.27.5) as a fallback for when we don't
1947 : : have anything better to use. */
1948 : :
1949 : : static char *
1950 : 226 : make_rule_id_for_diagnostic_kind (diagnostic_t diag_kind)
1951 : : {
1952 : : /* Lose the trailing ": ". */
1953 : 226 : const char *kind_text = get_diagnostic_kind_text (diag_kind);
1954 : 226 : size_t len = strlen (kind_text);
1955 : 226 : gcc_assert (len > 2);
1956 : 226 : gcc_assert (kind_text[len - 2] == ':');
1957 : 226 : gcc_assert (kind_text[len - 1] == ' ');
1958 : 226 : char *rstrip = xstrdup (kind_text);
1959 : 226 : rstrip[len - 2] = '\0';
1960 : 226 : return rstrip;
1961 : : }
1962 : :
1963 : : /* Make a "result" object (SARIF v2.1.0 section 3.27) for DIAGNOSTIC. */
1964 : :
1965 : : std::unique_ptr<sarif_result>
1966 : 458 : sarif_builder::make_result_object (const diagnostic_info &diagnostic,
1967 : : diagnostic_t orig_diag_kind,
1968 : : unsigned idx_within_parent)
1969 : : {
1970 : 458 : auto result_obj = std::make_unique<sarif_result> (idx_within_parent);
1971 : :
1972 : : /* "ruleId" property (SARIF v2.1.0 section 3.27.5). */
1973 : : /* Ideally we'd have an option_name for these. */
1974 : 916 : if (char *option_text
1975 : 458 : = m_context.make_option_name (diagnostic.option_id,
1976 : 458 : orig_diag_kind, diagnostic.kind))
1977 : : {
1978 : : /* Lazily create reportingDescriptor objects for and add to m_rules_arr.
1979 : : Set ruleId referencing them. */
1980 : 232 : result_obj->set_string ("ruleId", option_text);
1981 : 232 : if (m_rule_id_set.contains (option_text))
1982 : 171 : free (option_text);
1983 : : else
1984 : : {
1985 : : /* This is the first time we've seen this ruleId. */
1986 : : /* Add to set, taking ownership. */
1987 : 61 : m_rule_id_set.add (option_text);
1988 : :
1989 : 61 : m_rules_arr->append<sarif_reporting_descriptor>
1990 : 61 : (make_reporting_descriptor_object_for_warning (diagnostic,
1991 : : orig_diag_kind,
1992 : : option_text));
1993 : : }
1994 : : }
1995 : : else
1996 : : {
1997 : : /* Otherwise, we have an "error" or a stray "note"; use the
1998 : : diagnostic kind as the ruleId, so that the result object at least
1999 : : has a ruleId.
2000 : : We don't bother creating reportingDescriptor objects for these. */
2001 : 226 : char *rule_id = make_rule_id_for_diagnostic_kind (orig_diag_kind);
2002 : 226 : result_obj->set_string ("ruleId", rule_id);
2003 : 226 : free (rule_id);
2004 : : }
2005 : :
2006 : 458 : if (diagnostic.metadata)
2007 : : {
2008 : : /* "taxa" property (SARIF v2.1.0 section 3.27.8). */
2009 : 19 : if (int cwe_id = diagnostic.metadata->get_cwe ())
2010 : : {
2011 : 18 : auto taxa_arr = std::make_unique<json::array> ();
2012 : 18 : taxa_arr->append<sarif_reporting_descriptor_reference>
2013 : 18 : (make_reporting_descriptor_reference_object_for_cwe_id (cwe_id));
2014 : 18 : result_obj->set<json::array> ("taxa", std::move (taxa_arr));
2015 : 18 : }
2016 : :
2017 : 19 : diagnostic.metadata->maybe_add_sarif_properties (*result_obj);
2018 : :
2019 : : /* We don't yet support diagnostic_metadata::rule. */
2020 : : }
2021 : :
2022 : : /* "level" property (SARIF v2.1.0 section 3.27.10). */
2023 : 458 : if (const char *sarif_level = maybe_get_sarif_level (diagnostic.kind))
2024 : 458 : result_obj->set_string ("level", sarif_level);
2025 : :
2026 : : /* "message" property (SARIF v2.1.0 section 3.27.11). */
2027 : 458 : auto message_obj
2028 : 458 : = make_message_object (pp_formatted_text (m_printer));
2029 : 458 : pp_clear_output_area (m_printer);
2030 : 458 : result_obj->set<sarif_message> ("message", std::move (message_obj));
2031 : :
2032 : : /* "locations" property (SARIF v2.1.0 section 3.27.12). */
2033 : 458 : result_obj->set<json::array>
2034 : 916 : ("locations",
2035 : 458 : make_locations_arr (*result_obj.get (),
2036 : : diagnostic,
2037 : : diagnostic_artifact_role::result_file));
2038 : :
2039 : : /* "codeFlows" property (SARIF v2.1.0 section 3.27.18). */
2040 : 458 : if (const diagnostic_path *path = diagnostic.richloc->get_path ())
2041 : : {
2042 : 19 : auto code_flows_arr = std::make_unique<json::array> ();
2043 : 19 : const unsigned code_flow_index = 0;
2044 : 19 : code_flows_arr->append<sarif_code_flow>
2045 : 19 : (make_code_flow_object (*result_obj.get (),
2046 : : code_flow_index,
2047 : : *path));
2048 : 19 : result_obj->set<json::array> ("codeFlows", std::move (code_flows_arr));
2049 : 19 : }
2050 : :
2051 : : /* The "relatedLocations" property (SARIF v2.1.0 section 3.27.22) is
2052 : : set up later, if any nested diagnostics occur within this diagnostic
2053 : : group. */
2054 : :
2055 : : /* "fixes" property (SARIF v2.1.0 section 3.27.30). */
2056 : 458 : const rich_location *richloc = diagnostic.richloc;
2057 : 458 : if (richloc->get_num_fixit_hints ())
2058 : : {
2059 : 8 : auto fix_arr = std::make_unique<json::array> ();
2060 : 8 : fix_arr->append<sarif_fix> (make_fix_object (*richloc));
2061 : 8 : result_obj->set<json::array> ("fixes", std::move (fix_arr));
2062 : 8 : }
2063 : :
2064 : 916 : return result_obj;
2065 : 458 : }
2066 : :
2067 : : /* Make a "reportingDescriptor" object (SARIF v2.1.0 section 3.49)
2068 : : for a GCC warning. */
2069 : :
2070 : : std::unique_ptr<sarif_reporting_descriptor>
2071 : 61 : sarif_builder::
2072 : : make_reporting_descriptor_object_for_warning (const diagnostic_info &diagnostic,
2073 : : diagnostic_t /*orig_diag_kind*/,
2074 : : const char *option_text)
2075 : : {
2076 : 61 : auto reporting_desc = std::make_unique<sarif_reporting_descriptor> ();
2077 : :
2078 : : /* "id" property (SARIF v2.1.0 section 3.49.3). */
2079 : 61 : reporting_desc->set_string ("id", option_text);
2080 : :
2081 : : /* We don't implement "name" property (SARIF v2.1.0 section 3.49.7), since
2082 : : it seems redundant compared to "id". */
2083 : :
2084 : : /* "helpUri" property (SARIF v2.1.0 section 3.49.12). */
2085 : 61 : if (char *option_url = m_context.make_option_url (diagnostic.option_id))
2086 : : {
2087 : 61 : reporting_desc->set_string ("helpUri", option_url);
2088 : 61 : free (option_url);
2089 : : }
2090 : :
2091 : 61 : return reporting_desc;
2092 : : }
2093 : :
2094 : : /* Make a "reportingDescriptor" object (SARIF v2.1.0 section 3.49)
2095 : : for CWE_ID, for use within the CWE taxa array. */
2096 : :
2097 : : std::unique_ptr<sarif_reporting_descriptor>
2098 : 18 : sarif_builder::make_reporting_descriptor_object_for_cwe_id (int cwe_id) const
2099 : : {
2100 : 18 : auto reporting_desc = std::make_unique<sarif_reporting_descriptor> ();
2101 : :
2102 : : /* "id" property (SARIF v2.1.0 section 3.49.3). */
2103 : 18 : {
2104 : 18 : pretty_printer pp;
2105 : 18 : pp_printf (&pp, "%i", cwe_id);
2106 : 18 : reporting_desc->set_string ("id", pp_formatted_text (&pp));
2107 : 18 : }
2108 : :
2109 : : /* "helpUri" property (SARIF v2.1.0 section 3.49.12). */
2110 : 18 : {
2111 : 18 : char *url = get_cwe_url (cwe_id);
2112 : 18 : reporting_desc->set_string ("helpUri", url);
2113 : 18 : free (url);
2114 : : }
2115 : :
2116 : 18 : return reporting_desc;
2117 : : }
2118 : :
2119 : : /* Make a "reportingDescriptorReference" object (SARIF v2.1.0 section 3.52)
2120 : : referencing CWE_ID, for use within a result object.
2121 : : Also, add CWE_ID to m_cwe_id_set. */
2122 : :
2123 : : std::unique_ptr<sarif_reporting_descriptor_reference>
2124 : 18 : sarif_builder::
2125 : : make_reporting_descriptor_reference_object_for_cwe_id (int cwe_id)
2126 : : {
2127 : 18 : auto desc_ref_obj = std::make_unique<sarif_reporting_descriptor_reference> ();
2128 : :
2129 : : /* "id" property (SARIF v2.1.0 section 3.52.4). */
2130 : 18 : {
2131 : 18 : pretty_printer pp;
2132 : 18 : pp_printf (&pp, "%i", cwe_id);
2133 : 18 : desc_ref_obj->set_string ("id", pp_formatted_text (&pp));
2134 : 18 : }
2135 : :
2136 : : /* "toolComponent" property (SARIF v2.1.0 section 3.52.7). */
2137 : 18 : desc_ref_obj->set<sarif_tool_component_reference>
2138 : 18 : ("toolComponent", make_tool_component_reference_object_for_cwe ());
2139 : :
2140 : : /* Add CWE_ID to our set. */
2141 : 18 : gcc_assert (cwe_id > 0);
2142 : 18 : m_cwe_id_set.add (cwe_id);
2143 : :
2144 : 18 : return desc_ref_obj;
2145 : : }
2146 : :
2147 : : /* Make a "toolComponentReference" object (SARIF v2.1.0 section 3.54) that
2148 : : references the CWE taxonomy. */
2149 : :
2150 : : std::unique_ptr<sarif_tool_component_reference>
2151 : 18 : sarif_builder::
2152 : : make_tool_component_reference_object_for_cwe () const
2153 : : {
2154 : 18 : auto comp_ref_obj = std::make_unique<sarif_tool_component_reference> ();
2155 : :
2156 : : /* "name" property (SARIF v2.1.0 section 3.54.3). */
2157 : 18 : comp_ref_obj->set_string ("name", "cwe");
2158 : :
2159 : 18 : return comp_ref_obj;
2160 : : }
2161 : :
2162 : : /* Make an array suitable for use as the "locations" property of:
2163 : : - a "result" object (SARIF v2.1.0 section 3.27.12), or
2164 : : - a "notification" object (SARIF v2.1.0 section 3.58.4).
2165 : : Use LOC_MGR for any locations that need "id" values. */
2166 : :
2167 : : std::unique_ptr<json::array>
2168 : 462 : sarif_builder::make_locations_arr (sarif_location_manager &loc_mgr,
2169 : : const diagnostic_info &diagnostic,
2170 : : enum diagnostic_artifact_role role)
2171 : : {
2172 : 462 : auto locations_arr = std::make_unique<json::array> ();
2173 : 462 : logical_location logical_loc;
2174 : 462 : if (auto client_data_hooks = m_context.get_client_data_hooks ())
2175 : 270 : logical_loc = client_data_hooks->get_current_logical_location ();
2176 : :
2177 : 462 : auto location_obj
2178 : 462 : = make_location_object (loc_mgr, *diagnostic.richloc, logical_loc, role);
2179 : : /* Don't add entirely empty location objects to the array. */
2180 : 462 : if (!location_obj->is_empty ())
2181 : 398 : locations_arr->append<sarif_location> (std::move (location_obj));
2182 : :
2183 : 924 : return locations_arr;
2184 : 462 : }
2185 : :
2186 : : /* If LOGICAL_LOC is non-null, use it to create a "logicalLocations" property
2187 : : within LOCATION_OBJ (SARIF v2.1.0 section 3.28.4) with a minimal logical
2188 : : location object referencing theRuns.logicalLocations (3.33.3). */
2189 : :
2190 : : void
2191 : 680 : sarif_builder::
2192 : : set_any_logical_locs_arr (sarif_location &location_obj,
2193 : : logical_location logical_loc)
2194 : : {
2195 : 680 : if (!logical_loc)
2196 : 566 : return;
2197 : 114 : gcc_assert (m_logical_loc_mgr);
2198 : 114 : auto location_locs_arr = std::make_unique<json::array> ();
2199 : :
2200 : 114 : auto logical_loc_obj = make_minimal_sarif_logical_location (logical_loc);
2201 : :
2202 : 114 : location_locs_arr->append<sarif_logical_location>
2203 : 114 : (std::move (logical_loc_obj));
2204 : :
2205 : 114 : location_obj.set<json::array> ("logicalLocations",
2206 : : std::move (location_locs_arr));
2207 : 114 : }
2208 : :
2209 : : /* Make a "location" object (SARIF v2.1.0 section 3.28) for RICH_LOC
2210 : : and LOGICAL_LOC.
2211 : : Use LOC_MGR for any locations that need "id" values, and for
2212 : : any worklist items. */
2213 : :
2214 : : std::unique_ptr<sarif_location>
2215 : 617 : sarif_builder::make_location_object (sarif_location_manager &loc_mgr,
2216 : : const rich_location &rich_loc,
2217 : : logical_location logical_loc,
2218 : : enum diagnostic_artifact_role role)
2219 : : {
2220 : 1234 : class escape_nonascii_renderer : public content_renderer
2221 : : {
2222 : : public:
2223 : 617 : escape_nonascii_renderer (const rich_location &richloc,
2224 : : enum diagnostics_escape_format escape_format)
2225 : 617 : : m_richloc (richloc),
2226 : 617 : m_escape_format (escape_format)
2227 : : {}
2228 : :
2229 : : std::unique_ptr<sarif_multiformat_message_string>
2230 : 136 : render (const sarif_builder &builder) const final override
2231 : : {
2232 : 136 : diagnostic_context dc;
2233 : 136 : diagnostic_initialize (&dc, 0);
2234 : 136 : dc.m_source_printing.enabled = true;
2235 : 136 : dc.m_source_printing.colorize_source_p = false;
2236 : 136 : dc.m_source_printing.show_labels_p = true;
2237 : 136 : dc.m_source_printing.show_line_numbers_p = true;
2238 : :
2239 : 136 : rich_location my_rich_loc (m_richloc);
2240 : 136 : my_rich_loc.set_escape_on_output (true);
2241 : :
2242 : 136 : diagnostic_source_print_policy source_policy (dc);
2243 : 136 : dc.set_escape_format (m_escape_format);
2244 : 136 : diagnostic_text_output_format text_output (dc);
2245 : 136 : source_policy.print (*text_output.get_printer (),
2246 : : my_rich_loc, DK_ERROR, nullptr);
2247 : :
2248 : 136 : const char *buf = pp_formatted_text (text_output.get_printer ());
2249 : 136 : std::unique_ptr<sarif_multiformat_message_string> result
2250 : 136 : = builder.make_multiformat_message_string (buf);
2251 : :
2252 : 136 : diagnostic_finish (&dc);
2253 : :
2254 : 272 : return result;
2255 : 136 : }
2256 : : private:
2257 : : const rich_location &m_richloc;
2258 : : enum diagnostics_escape_format m_escape_format;
2259 : : } the_renderer (rich_loc,
2260 : 617 : m_context.get_escape_format ());
2261 : :
2262 : 617 : auto location_obj = std::make_unique<sarif_location> ();
2263 : :
2264 : : /* Get primary loc from RICH_LOC. */
2265 : 617 : location_t loc = rich_loc.get_loc ();
2266 : :
2267 : : /* "physicalLocation" property (SARIF v2.1.0 section 3.28.3). */
2268 : 617 : const content_renderer *snippet_renderer
2269 : 617 : = rich_loc.escape_on_output_p () ? &the_renderer : nullptr;
2270 : 617 : if (auto phs_loc_obj
2271 : : = maybe_make_physical_location_object (loc, role,
2272 : : rich_loc.get_column_override (),
2273 : 617 : snippet_renderer))
2274 : 553 : location_obj->set<sarif_physical_location> ("physicalLocation",
2275 : 617 : std::move (phs_loc_obj));
2276 : :
2277 : : /* "logicalLocations" property (SARIF v2.1.0 section 3.28.4). */
2278 : 617 : set_any_logical_locs_arr (*location_obj, logical_loc);
2279 : :
2280 : : /* Handle labelled ranges and/or secondary locations. */
2281 : 617 : {
2282 : 617 : std::unique_ptr<json::array> annotations_arr = nullptr;
2283 : 1512 : for (unsigned int i = 0; i < rich_loc.get_num_locations (); i++)
2284 : : {
2285 : 895 : const location_range *range = rich_loc.get_range (i);
2286 : 895 : bool handled = false;
2287 : 895 : if (const range_label *label = range->m_label)
2288 : : {
2289 : 410 : label_text text = label->get_text (i);
2290 : 410 : if (text.get ())
2291 : : {
2292 : : /* Create annotations for any labelled ranges. */
2293 : 410 : location_t range_loc = rich_loc.get_loc (i);
2294 : 410 : auto region
2295 : : = maybe_make_region_object (range_loc,
2296 : 410 : rich_loc.get_column_override ());
2297 : 410 : if (region)
2298 : : {
2299 : 410 : if (!annotations_arr)
2300 : 137 : annotations_arr = std::make_unique<json::array> ();
2301 : 410 : region->set<sarif_message>
2302 : 410 : ("message", make_message_object (text.get ()));
2303 : 410 : annotations_arr->append<sarif_region> (std::move (region));
2304 : 410 : handled = true;
2305 : : }
2306 : 410 : }
2307 : 410 : }
2308 : :
2309 : : /* Add related locations for any secondary locations in RICH_LOC
2310 : : that don't have labels (and thus aren't added to "annotations"). */
2311 : 895 : if (i > 0 && !handled)
2312 : 4 : loc_mgr.add_relationship_to_worklist
2313 : 4 : (*location_obj.get (),
2314 : : sarif_location_manager::worklist_item::kind::unlabelled_secondary_location,
2315 : 4 : range->m_loc);
2316 : : }
2317 : 617 : if (annotations_arr)
2318 : : /* "annotations" property (SARIF v2.1.0 section 3.28.6). */
2319 : 137 : location_obj->set<json::array> ("annotations",
2320 : : std::move (annotations_arr));
2321 : 617 : }
2322 : :
2323 : 617 : add_any_include_chain (loc_mgr, *location_obj.get (), loc);
2324 : :
2325 : : /* A flag for hinting that the diagnostic involves issues at the
2326 : : level of character encodings (such as homoglyphs, or misleading
2327 : : bidirectional control codes), and thus that it will be helpful
2328 : : to the user if we show some representation of
2329 : : how the characters in the pertinent source lines are encoded. */
2330 : 617 : if (rich_loc.escape_on_output_p ())
2331 : : {
2332 : 136 : sarif_property_bag &bag = location_obj->get_or_create_properties ();
2333 : 136 : bag.set_bool ("gcc/escapeNonAscii", rich_loc.escape_on_output_p ());
2334 : : }
2335 : :
2336 : 617 : return location_obj;
2337 : 617 : }
2338 : :
2339 : : /* If WHERE was #included from somewhere, add a worklist item
2340 : : to LOC_MGR to lazily add a location for the #include location,
2341 : : and relationships between it and the LOCATION_OBJ.
2342 : : Compare with diagnostic_context::report_current_module, but rather
2343 : : than iterating the current chain, we add the next edge and iterate
2344 : : in the worklist, so that edges are only added once. */
2345 : :
2346 : : void
2347 : 700 : sarif_builder::add_any_include_chain (sarif_location_manager &loc_mgr,
2348 : : sarif_location &location_obj,
2349 : : location_t where)
2350 : : {
2351 : 700 : if (where <= BUILTINS_LOCATION)
2352 : 679 : return;
2353 : :
2354 : 636 : const line_map_ordinary *map = nullptr;
2355 : 636 : linemap_resolve_location (m_line_maps, where,
2356 : : LRK_MACRO_DEFINITION_LOCATION,
2357 : : &map);
2358 : :
2359 : 636 : if (!map)
2360 : : return;
2361 : :
2362 : 636 : location_t include_loc = linemap_included_from (map);
2363 : 636 : map = linemap_included_from_linemap (m_line_maps, map);
2364 : 636 : if (!map)
2365 : : return;
2366 : 21 : loc_mgr.add_relationship_to_worklist
2367 : 21 : (location_obj,
2368 : : sarif_result::worklist_item::kind::included_from,
2369 : : include_loc);
2370 : : }
2371 : :
2372 : : /* Make a "location" object (SARIF v2.1.0 section 3.28) for WHERE
2373 : : within an include chain. */
2374 : :
2375 : : std::unique_ptr<sarif_location>
2376 : 20 : sarif_builder::make_location_object (sarif_location_manager &loc_mgr,
2377 : : location_t loc,
2378 : : enum diagnostic_artifact_role role)
2379 : : {
2380 : 20 : auto location_obj = std::make_unique<sarif_location> ();
2381 : :
2382 : : /* "physicalLocation" property (SARIF v2.1.0 section 3.28.3). */
2383 : 20 : if (auto phs_loc_obj
2384 : 20 : = maybe_make_physical_location_object (loc, role, 0, nullptr))
2385 : 20 : location_obj->set<sarif_physical_location> ("physicalLocation",
2386 : 20 : std::move (phs_loc_obj));
2387 : :
2388 : 20 : add_any_include_chain (loc_mgr, *location_obj.get (), loc);
2389 : :
2390 : 20 : return location_obj;
2391 : : }
2392 : :
2393 : : /* Make a "location" object (SARIF v2.1.0 section 3.28) for EVENT
2394 : : within a diagnostic_path. */
2395 : :
2396 : : std::unique_ptr<sarif_location>
2397 : 63 : sarif_builder::make_location_object (sarif_location_manager &loc_mgr,
2398 : : const diagnostic_event &event,
2399 : : enum diagnostic_artifact_role role)
2400 : : {
2401 : 63 : auto location_obj = std::make_unique<sarif_location> ();
2402 : :
2403 : : /* "physicalLocation" property (SARIF v2.1.0 section 3.28.3). */
2404 : 63 : location_t loc = event.get_location ();
2405 : 63 : if (auto phs_loc_obj
2406 : 63 : = maybe_make_physical_location_object (loc, role, 0, nullptr))
2407 : 63 : location_obj->set<sarif_physical_location> ("physicalLocation",
2408 : 63 : std::move (phs_loc_obj));
2409 : :
2410 : : /* "logicalLocations" property (SARIF v2.1.0 section 3.28.4). */
2411 : 63 : logical_location logical_loc = event.get_logical_location ();
2412 : 63 : set_any_logical_locs_arr (*location_obj, logical_loc);
2413 : :
2414 : : /* "message" property (SARIF v2.1.0 section 3.28.5). */
2415 : 63 : std::unique_ptr<pretty_printer> pp = get_printer ()->clone ();
2416 : 63 : event.print_desc (*pp);
2417 : 63 : location_obj->set<sarif_message>
2418 : 126 : ("message",
2419 : 63 : make_message_object (pp_formatted_text (pp.get ())));
2420 : :
2421 : 63 : add_any_include_chain (loc_mgr, *location_obj.get (), loc);
2422 : :
2423 : 126 : return location_obj;
2424 : 63 : }
2425 : :
2426 : : /* Make a "physicalLocation" object (SARIF v2.1.0 section 3.29) for LOC.
2427 : :
2428 : : If COLUMN_OVERRIDE is non-zero, then use it as the column number
2429 : : if LOC has no column information.
2430 : :
2431 : : Ensure that we have an artifact object for the file, adding ROLE to it,
2432 : : and flagging that we will attempt to embed the contents of the artifact
2433 : : when writing it out. */
2434 : :
2435 : : std::unique_ptr<sarif_physical_location>
2436 : 700 : sarif_builder::
2437 : : maybe_make_physical_location_object (location_t loc,
2438 : : enum diagnostic_artifact_role role,
2439 : : int column_override,
2440 : : const content_renderer *snippet_renderer)
2441 : : {
2442 : 700 : if (loc <= BUILTINS_LOCATION || LOCATION_FILE (loc) == nullptr)
2443 : 64 : return nullptr;
2444 : :
2445 : 636 : auto phys_loc_obj = std::make_unique<sarif_physical_location> ();
2446 : :
2447 : : /* "artifactLocation" property (SARIF v2.1.0 section 3.29.3). */
2448 : 636 : phys_loc_obj->set<sarif_artifact_location>
2449 : 636 : ("artifactLocation", make_artifact_location_object (loc));
2450 : 636 : get_or_create_artifact (LOCATION_FILE (loc), role, true);
2451 : :
2452 : : /* "region" property (SARIF v2.1.0 section 3.29.4). */
2453 : 636 : if (auto region_obj = maybe_make_region_object (loc, column_override))
2454 : 636 : phys_loc_obj->set<sarif_region> ("region", std::move (region_obj));
2455 : :
2456 : : /* "contextRegion" property (SARIF v2.1.0 section 3.29.5). */
2457 : 636 : if (auto context_region_obj
2458 : 636 : = maybe_make_region_object_for_context (loc, snippet_renderer))
2459 : 632 : phys_loc_obj->set<sarif_region> ("contextRegion",
2460 : 636 : std::move (context_region_obj));
2461 : :
2462 : : /* Instead, we add artifacts to the run as a whole,
2463 : : with artifact.contents.
2464 : : Could do both, though. */
2465 : :
2466 : 636 : return phys_loc_obj;
2467 : 636 : }
2468 : :
2469 : : /* Make an "artifactLocation" object (SARIF v2.1.0 section 3.4) for LOC,
2470 : : or return nullptr. */
2471 : :
2472 : : std::unique_ptr<sarif_artifact_location>
2473 : 644 : sarif_builder::make_artifact_location_object (location_t loc)
2474 : : {
2475 : 644 : return make_artifact_location_object (LOCATION_FILE (loc));
2476 : : }
2477 : :
2478 : : /* The ID value for use in "uriBaseId" properties (SARIF v2.1.0 section 3.4.4)
2479 : : for when we need to express paths relative to PWD. */
2480 : :
2481 : : #define PWD_PROPERTY_NAME ("PWD")
2482 : :
2483 : : /* Make an "artifactLocation" object (SARIF v2.1.0 section 3.4) for FILENAME,
2484 : : or return nullptr. */
2485 : :
2486 : : std::unique_ptr<sarif_artifact_location>
2487 : 1460 : sarif_builder::make_artifact_location_object (const char *filename)
2488 : : {
2489 : 1460 : auto artifact_loc_obj = std::make_unique<sarif_artifact_location> ();
2490 : :
2491 : : /* "uri" property (SARIF v2.1.0 section 3.4.3). */
2492 : 1460 : artifact_loc_obj->set_string ("uri", filename);
2493 : :
2494 : 1460 : if (filename[0] != '/')
2495 : : {
2496 : : /* If we have a relative path, set the "uriBaseId" property
2497 : : (SARIF v2.1.0 section 3.4.4). */
2498 : 56 : artifact_loc_obj->set_string ("uriBaseId", PWD_PROPERTY_NAME);
2499 : 56 : m_seen_any_relative_paths = true;
2500 : : }
2501 : :
2502 : 1460 : return artifact_loc_obj;
2503 : : }
2504 : :
2505 : : /* Get the PWD, or nullptr, as an absolute file-based URI,
2506 : : adding a trailing forward slash (as required by SARIF v2.1.0
2507 : : section 3.14.14). */
2508 : :
2509 : : static char *
2510 : 44 : make_pwd_uri_str ()
2511 : : {
2512 : : /* The prefix of a file-based URI, up to, but not including the path. */
2513 : : #define FILE_PREFIX ("file://")
2514 : :
2515 : 44 : const char *pwd = getpwd ();
2516 : 44 : if (!pwd)
2517 : : return nullptr;
2518 : 44 : size_t len = strlen (pwd);
2519 : 44 : if (len == 0 || pwd[len - 1] != '/')
2520 : 44 : return concat (FILE_PREFIX, pwd, "/", nullptr);
2521 : : else
2522 : : {
2523 : 0 : gcc_assert (pwd[len - 1] == '/');
2524 : 0 : return concat (FILE_PREFIX, pwd, nullptr);
2525 : : }
2526 : : }
2527 : :
2528 : : /* Make an "artifactLocation" object (SARIF v2.1.0 section 3.4) for the pwd,
2529 : : for use in the "run.originalUriBaseIds" property (SARIF v2.1.0
2530 : : section 3.14.14) when we have any relative paths. */
2531 : :
2532 : : std::unique_ptr<sarif_artifact_location>
2533 : 44 : sarif_builder::make_artifact_location_object_for_pwd () const
2534 : : {
2535 : 44 : auto artifact_loc_obj = std::make_unique<sarif_artifact_location> ();
2536 : :
2537 : : /* "uri" property (SARIF v2.1.0 section 3.4.3). */
2538 : 44 : if (char *pwd = make_pwd_uri_str ())
2539 : : {
2540 : 44 : gcc_assert (strlen (pwd) > 0);
2541 : 44 : gcc_assert (pwd[strlen (pwd) - 1] == '/');
2542 : 44 : artifact_loc_obj->set_string ("uri", pwd);
2543 : 44 : free (pwd);
2544 : : }
2545 : :
2546 : 44 : return artifact_loc_obj;
2547 : : }
2548 : :
2549 : : /* Get the column number within EXPLOC. */
2550 : :
2551 : : int
2552 : 1900 : sarif_builder::get_sarif_column (expanded_location exploc) const
2553 : : {
2554 : 1900 : cpp_char_column_policy policy (m_tabstop, cpp_wcwidth);
2555 : 1900 : return location_compute_display_column (m_context.get_file_cache (),
2556 : 1900 : exploc, policy);
2557 : : }
2558 : :
2559 : : /* Make a "region" object (SARIF v2.1.0 section 3.30) for LOC,
2560 : : or return nullptr.
2561 : :
2562 : : If COLUMN_OVERRIDE is non-zero, then use it as the column number
2563 : : if LOC has no column information.
2564 : :
2565 : : We only support text properties of regions ("text regions"),
2566 : : not binary properties ("binary regions"); see 3.30.1. */
2567 : :
2568 : : std::unique_ptr<sarif_region>
2569 : 1046 : sarif_builder::maybe_make_region_object (location_t loc,
2570 : : int column_override) const
2571 : : {
2572 : 1046 : location_t caret_loc = get_pure_location (loc);
2573 : :
2574 : 1046 : if (caret_loc <= BUILTINS_LOCATION)
2575 : 0 : return nullptr;
2576 : :
2577 : 1046 : location_t start_loc = get_start (loc);
2578 : 1046 : location_t finish_loc = get_finish (loc);
2579 : :
2580 : 1046 : expanded_location exploc_caret = expand_location (caret_loc);
2581 : 1046 : expanded_location exploc_start = expand_location (start_loc);
2582 : 1046 : expanded_location exploc_finish = expand_location (finish_loc);
2583 : :
2584 : 1046 : if (exploc_start.file !=exploc_caret.file)
2585 : 0 : return nullptr;
2586 : 1046 : if (exploc_finish.file !=exploc_caret.file)
2587 : 0 : return nullptr;
2588 : :
2589 : : /* We can have line == 0 in the presence of "#" lines.
2590 : : SARIF requires lines > 0, so if we hit this case we don't have a
2591 : : way of validly representing the region as SARIF; bail out. */
2592 : 1046 : if (exploc_start.line <= 0)
2593 : 4 : return nullptr;
2594 : :
2595 : 1042 : auto region_obj = std::make_unique<sarif_region> ();
2596 : :
2597 : : /* "startLine" property (SARIF v2.1.0 section 3.30.5) */
2598 : 1042 : region_obj->set_integer ("startLine", exploc_start.line);
2599 : :
2600 : : /* "startColumn" property (SARIF v2.1.0 section 3.30.6).
2601 : :
2602 : : We use column == 0 to mean the whole line, so omit the column
2603 : : information for this case, unless COLUMN_OVERRIDE is non-zero,
2604 : : (for handling certain awkward lexer diagnostics) */
2605 : :
2606 : 1042 : if (exploc_start.column == 0 && column_override)
2607 : : /* Use the provided column number. */
2608 : : exploc_start.column = column_override;
2609 : :
2610 : 1042 : if (exploc_start.column > 0)
2611 : : {
2612 : 1026 : int start_column = get_sarif_column (exploc_start);
2613 : 1026 : region_obj->set_integer ("startColumn", start_column);
2614 : : }
2615 : :
2616 : : /* "endLine" property (SARIF v2.1.0 section 3.30.7) */
2617 : 1042 : if (exploc_finish.line != exploc_start.line
2618 : 0 : && exploc_finish.line > 0)
2619 : 0 : region_obj->set_integer ("endLine", exploc_finish.line);
2620 : :
2621 : : /* "endColumn" property (SARIF v2.1.0 section 3.30.8).
2622 : : This expresses the column immediately beyond the range.
2623 : :
2624 : : We use column == 0 to mean the whole line, so omit the column
2625 : : information for this case. */
2626 : 1042 : if (exploc_finish.column > 0)
2627 : : {
2628 : 858 : int next_column = get_sarif_column (exploc_finish) + 1;
2629 : 858 : region_obj->set_integer ("endColumn", next_column);
2630 : : }
2631 : :
2632 : 1042 : return region_obj;
2633 : 1042 : }
2634 : :
2635 : : /* Make a "region" object (SARIF v2.1.0 section 3.30) for the "contextRegion"
2636 : : property (SARIF v2.1.0 section 3.29.5) of a "physicalLocation".
2637 : :
2638 : : This is similar to maybe_make_region_object, but ignores column numbers,
2639 : : covering the line(s) as a whole, and including a "snippet" property
2640 : : embedding those source lines, making it easier for consumers to show
2641 : : the pertinent source. */
2642 : :
2643 : : std::unique_ptr<sarif_region>
2644 : 636 : sarif_builder::
2645 : : maybe_make_region_object_for_context (location_t loc,
2646 : : const content_renderer *snippet_renderer)
2647 : : const
2648 : : {
2649 : 636 : location_t caret_loc = get_pure_location (loc);
2650 : :
2651 : 636 : if (caret_loc <= BUILTINS_LOCATION)
2652 : 0 : return nullptr;
2653 : :
2654 : 636 : location_t start_loc = get_start (loc);
2655 : 636 : location_t finish_loc = get_finish (loc);
2656 : :
2657 : 636 : expanded_location exploc_caret = expand_location (caret_loc);
2658 : 636 : expanded_location exploc_start = expand_location (start_loc);
2659 : 636 : expanded_location exploc_finish = expand_location (finish_loc);
2660 : :
2661 : 636 : if (exploc_start.file !=exploc_caret.file)
2662 : 0 : return nullptr;
2663 : 636 : if (exploc_finish.file !=exploc_caret.file)
2664 : 0 : return nullptr;
2665 : :
2666 : : /* We can have line == 0 in the presence of "#" lines.
2667 : : SARIF requires lines > 0, so if we hit this case we don't have a
2668 : : way of validly representing the region as SARIF; bail out. */
2669 : 636 : if (exploc_start.line <= 0)
2670 : 4 : return nullptr;
2671 : :
2672 : 632 : auto region_obj = std::make_unique<sarif_region> ();
2673 : :
2674 : : /* "startLine" property (SARIF v2.1.0 section 3.30.5) */
2675 : 632 : region_obj->set_integer ("startLine", exploc_start.line);
2676 : :
2677 : : /* "endLine" property (SARIF v2.1.0 section 3.30.7) */
2678 : 632 : if (exploc_finish.line != exploc_start.line
2679 : 0 : && exploc_finish.line > 0)
2680 : 0 : region_obj->set_integer ("endLine", exploc_finish.line);
2681 : :
2682 : : /* "snippet" property (SARIF v2.1.0 section 3.30.13). */
2683 : 632 : if (auto artifact_content_obj
2684 : : = maybe_make_artifact_content_object (exploc_start.file,
2685 : : exploc_start.line,
2686 : : exploc_finish.line,
2687 : 632 : snippet_renderer))
2688 : 460 : region_obj->set<sarif_artifact_content> ("snippet",
2689 : 632 : std::move (artifact_content_obj));
2690 : :
2691 : 632 : return region_obj;
2692 : 632 : }
2693 : :
2694 : : /* Make a "region" object (SARIF v2.1.0 section 3.30) for the deletion region
2695 : : of HINT (as per SARIF v2.1.0 section 3.57.3). */
2696 : :
2697 : : std::unique_ptr<sarif_region>
2698 : 8 : sarif_builder::make_region_object_for_hint (const fixit_hint &hint) const
2699 : : {
2700 : 8 : location_t start_loc = hint.get_start_loc ();
2701 : 8 : location_t next_loc = hint.get_next_loc ();
2702 : :
2703 : 8 : expanded_location exploc_start = expand_location (start_loc);
2704 : 8 : expanded_location exploc_next = expand_location (next_loc);
2705 : :
2706 : 8 : auto region_obj = std::make_unique<sarif_region> ();
2707 : :
2708 : : /* "startLine" property (SARIF v2.1.0 section 3.30.5) */
2709 : 8 : region_obj->set_integer ("startLine", exploc_start.line);
2710 : :
2711 : : /* "startColumn" property (SARIF v2.1.0 section 3.30.6) */
2712 : 8 : int start_col = get_sarif_column (exploc_start);
2713 : 8 : region_obj->set_integer ("startColumn", start_col);
2714 : :
2715 : : /* "endLine" property (SARIF v2.1.0 section 3.30.7) */
2716 : 8 : if (exploc_next.line != exploc_start.line)
2717 : 0 : region_obj->set_integer ("endLine", exploc_next.line);
2718 : :
2719 : : /* "endColumn" property (SARIF v2.1.0 section 3.30.8).
2720 : : This expresses the column immediately beyond the range. */
2721 : 8 : int next_col = get_sarif_column (exploc_next);
2722 : 8 : region_obj->set_integer ("endColumn", next_col);
2723 : :
2724 : 8 : return region_obj;
2725 : : }
2726 : :
2727 : : /* Attempt to get a string for a logicalLocation's "kind" property
2728 : : (SARIF v2.1.0 section 3.33.7).
2729 : : Return nullptr if unknown. */
2730 : :
2731 : : static const char *
2732 : 340 : maybe_get_sarif_kind (enum logical_location_kind kind)
2733 : : {
2734 : 340 : switch (kind)
2735 : : {
2736 : 0 : default:
2737 : 0 : gcc_unreachable ();
2738 : : case logical_location_kind::unknown:
2739 : : return nullptr;
2740 : :
2741 : : /* Kinds within executable code. */
2742 : 114 : case logical_location_kind::function:
2743 : 114 : return "function";
2744 : 0 : case logical_location_kind::member:
2745 : 0 : return "member";
2746 : 0 : case logical_location_kind::module_:
2747 : 0 : return "module";
2748 : 217 : case logical_location_kind::namespace_:
2749 : 217 : return "namespace";
2750 : 9 : case logical_location_kind::type:
2751 : 9 : return "type";
2752 : 0 : case logical_location_kind::return_type:
2753 : 0 : return "returnType";
2754 : 0 : case logical_location_kind::parameter:
2755 : 0 : return "parameter";
2756 : 0 : case logical_location_kind::variable:
2757 : 0 : return "variable";
2758 : :
2759 : : /* Kinds within XML or HTML documents. */
2760 : 0 : case logical_location_kind::element:
2761 : 0 : return "element";
2762 : 0 : case logical_location_kind::attribute:
2763 : 0 : return "attribute";
2764 : 0 : case logical_location_kind::text:
2765 : 0 : return "text";
2766 : 0 : case logical_location_kind::comment:
2767 : 0 : return "comment";
2768 : 0 : case logical_location_kind::processing_instruction:
2769 : 0 : return "processingInstruction";
2770 : 0 : case logical_location_kind::dtd:
2771 : 0 : return "dtd";
2772 : 0 : case logical_location_kind::declaration:
2773 : 0 : return "declaration";
2774 : :
2775 : : /* Kinds within JSON documents. */
2776 : 0 : case logical_location_kind::object:
2777 : 0 : return "object";
2778 : 0 : case logical_location_kind::array:
2779 : 0 : return "array";
2780 : 0 : case logical_location_kind::property:
2781 : 0 : return "property";
2782 : 0 : case logical_location_kind::value:
2783 : 0 : return "value";
2784 : : }
2785 : : }
2786 : :
2787 : : /* Set PROPERTY_NAME within this bag to a "logicalLocation" object (SARIF v2.1.0
2788 : : section 3.33) for LOGICAL_LOC. The object has an "index" property to refer to
2789 : : theRuns.logicalLocations (3.33.3). */
2790 : :
2791 : : void
2792 : 0 : sarif_property_bag::set_logical_location (const char *property_name,
2793 : : sarif_builder &builder,
2794 : : logical_location logical_loc)
2795 : : {
2796 : 0 : set<sarif_logical_location>
2797 : 0 : (property_name,
2798 : 0 : builder.make_minimal_sarif_logical_location (logical_loc));
2799 : 0 : }
2800 : :
2801 : : /* Ensure that m_cached_logical_locs has a "logicalLocation" object
2802 : : (SARIF v2.1.0 section 3.33) for K, and return its index within the
2803 : : array. */
2804 : :
2805 : : int
2806 : 340 : sarif_builder::
2807 : : ensure_sarif_logical_location_for (logical_location k)
2808 : : {
2809 : 340 : gcc_assert (m_logical_loc_mgr);
2810 : :
2811 : 340 : auto sarif_logical_loc = std::make_unique<sarif_logical_location> ();
2812 : :
2813 : 340 : if (const char *short_name = m_logical_loc_mgr->get_short_name (k))
2814 : 340 : sarif_logical_loc->set_string ("name", short_name);
2815 : :
2816 : : /* "fullyQualifiedName" property (SARIF v2.1.0 section 3.33.5). */
2817 : 340 : if (const char *name_with_scope = m_logical_loc_mgr->get_name_with_scope (k))
2818 : 331 : sarif_logical_loc->set_string ("fullyQualifiedName", name_with_scope);
2819 : :
2820 : : /* "decoratedName" property (SARIF v2.1.0 section 3.33.6). */
2821 : 340 : if (const char *internal_name = m_logical_loc_mgr->get_internal_name (k))
2822 : 114 : sarif_logical_loc->set_string ("decoratedName", internal_name);
2823 : :
2824 : : /* "kind" property (SARIF v2.1.0 section 3.33.7). */
2825 : 340 : enum logical_location_kind kind = m_logical_loc_mgr->get_kind (k);
2826 : 340 : if (const char *sarif_kind_str = maybe_get_sarif_kind (kind))
2827 : 340 : sarif_logical_loc->set_string ("kind", sarif_kind_str);
2828 : :
2829 : : /* "parentIndex" property (SARIF v2.1.0 section 3.33.8). */
2830 : 340 : if (auto parent_key = m_logical_loc_mgr->get_parent (k))
2831 : : {
2832 : : /* Recurse upwards. */
2833 : 226 : int parent_index = ensure_sarif_logical_location_for (parent_key);
2834 : 226 : sarif_logical_loc->set_integer ("parentIndex", parent_index);
2835 : : }
2836 : :
2837 : : /* Consolidate if this logical location already exists. */
2838 : 340 : int index
2839 : 340 : = m_cached_logical_locs->append_uniquely (std::move (sarif_logical_loc));
2840 : :
2841 : 680 : return index;
2842 : 340 : }
2843 : :
2844 : : /* Ensure that theRuns.logicalLocations (3.14.17) has a "logicalLocation" object
2845 : : (SARIF v2.1.0 section 3.33) for LOGICAL_LOC.
2846 : : Create and return a minimal logicalLocation object referring to the
2847 : : full object by index. */
2848 : :
2849 : : std::unique_ptr<sarif_logical_location>
2850 : 114 : sarif_builder::
2851 : : make_minimal_sarif_logical_location (logical_location logical_loc)
2852 : : {
2853 : 114 : gcc_assert (m_logical_loc_mgr);
2854 : :
2855 : : /* Ensure that m_cached_logical_locs has a "logicalLocation" object
2856 : : (SARIF v2.1.0 section 3.33) for LOGICAL_LOC, and return its index within
2857 : : the array. */
2858 : :
2859 : 114 : auto sarif_logical_loc = std::make_unique <sarif_logical_location> ();
2860 : :
2861 : 114 : int index = ensure_sarif_logical_location_for (logical_loc);
2862 : :
2863 : : // 3.33.3 index property
2864 : 114 : sarif_logical_loc->set_integer ("index", index);
2865 : :
2866 : : /* "fullyQualifiedName" property (SARIF v2.1.0 section 3.33.5). */
2867 : 228 : if (const char *name_with_scope
2868 : 114 : = m_logical_loc_mgr->get_name_with_scope (logical_loc))
2869 : 114 : sarif_logical_loc->set_string ("fullyQualifiedName", name_with_scope);
2870 : :
2871 : 114 : return sarif_logical_loc;
2872 : : }
2873 : :
2874 : : label_text
2875 : 9 : make_sarif_url_for_event (const sarif_code_flow *code_flow,
2876 : : diagnostic_event_id_t event_id)
2877 : : {
2878 : 9 : gcc_assert (event_id.known_p ());
2879 : :
2880 : 9 : if (!code_flow)
2881 : 0 : return label_text ();
2882 : :
2883 : 9 : const sarif_thread_flow_location &tfl_obj
2884 : 9 : = code_flow->get_thread_flow_loc_obj (event_id);
2885 : 9 : const int location_idx = tfl_obj.get_index_within_parent ();
2886 : :
2887 : 9 : const sarif_thread_flow &thread_flow_obj = tfl_obj.get_parent ();
2888 : 9 : const int thread_flow_idx = thread_flow_obj.get_index_within_parent ();
2889 : :
2890 : 9 : const sarif_code_flow &code_flow_obj = thread_flow_obj.get_parent ();
2891 : 9 : const int code_flow_idx = code_flow_obj.get_index_within_parent ();
2892 : :
2893 : 9 : const sarif_result &result_obj = code_flow_obj.get_parent ();
2894 : 9 : const int result_idx = result_obj.get_index_within_parent ();
2895 : :
2896 : : /* We only support a single run object in the log. */
2897 : 9 : const int run_idx = 0;
2898 : :
2899 : 9 : char *buf = xasprintf
2900 : 9 : ("sarif:/runs/%i/results/%i/codeFlows/%i/threadFlows/%i/locations/%i",
2901 : : run_idx, result_idx, code_flow_idx, thread_flow_idx, location_idx);
2902 : 9 : return label_text::take (buf);
2903 : : }
2904 : :
2905 : : /* Make a "codeFlow" object (SARIF v2.1.0 section 3.36) for PATH. */
2906 : :
2907 : : std::unique_ptr<sarif_code_flow>
2908 : 19 : sarif_builder::make_code_flow_object (sarif_result &result,
2909 : : unsigned idx_within_parent,
2910 : : const diagnostic_path &path)
2911 : : {
2912 : 19 : auto code_flow_obj
2913 : 19 : = std::make_unique <sarif_code_flow> (result, idx_within_parent);
2914 : :
2915 : : /* First pass:
2916 : : Create threadFlows and threadFlowLocation objects within them,
2917 : : effectively recording a mapping from event_id to threadFlowLocation
2918 : : so that we can later go from an event_id to a URI within the
2919 : : SARIF file. */
2920 : 82 : for (unsigned i = 0; i < path.num_events (); i++)
2921 : : {
2922 : 63 : const diagnostic_event &event = path.get_event (i);
2923 : 63 : const diagnostic_thread_id_t thread_id = event.get_thread_id ();
2924 : :
2925 : 63 : sarif_thread_flow &thread_flow_obj
2926 : 63 : = code_flow_obj->get_or_append_thread_flow (path.get_thread (thread_id),
2927 : : thread_id);
2928 : 63 : thread_flow_obj.add_location ();
2929 : : }
2930 : :
2931 : : /* Second pass: walk the events, populating the tfl objs. */
2932 : 19 : m_current_code_flow = code_flow_obj.get ();
2933 : 82 : for (unsigned i = 0; i < path.num_events (); i++)
2934 : : {
2935 : 63 : const diagnostic_event &event = path.get_event (i);
2936 : 63 : sarif_thread_flow_location &thread_flow_loc_obj
2937 : 63 : = code_flow_obj->get_thread_flow_loc_obj (i);
2938 : 63 : populate_thread_flow_location_object (result,
2939 : : thread_flow_loc_obj,
2940 : : event,
2941 : : i);
2942 : : }
2943 : 19 : m_current_code_flow = nullptr;
2944 : :
2945 : 19 : return code_flow_obj;
2946 : : }
2947 : :
2948 : : /* Populate TFL_OBJ, a "threadFlowLocation" object (SARIF v2.1.0 section 3.38)
2949 : : based on EVENT. */
2950 : :
2951 : : void
2952 : 63 : sarif_builder::
2953 : : populate_thread_flow_location_object (sarif_result &result,
2954 : : sarif_thread_flow_location &tfl_obj,
2955 : : const diagnostic_event &ev,
2956 : : int event_execution_idx)
2957 : : {
2958 : : /* Give diagnostic_event subclasses a chance to add custom properties
2959 : : via a property bag. */
2960 : 63 : ev.maybe_add_sarif_properties (*this, tfl_obj);
2961 : :
2962 : : /* "location" property (SARIF v2.1.0 section 3.38.3). */
2963 : 63 : tfl_obj.set<sarif_location>
2964 : 126 : ("location",
2965 : 63 : make_location_object (result, ev, diagnostic_artifact_role::traced_file));
2966 : :
2967 : : /* "kinds" property (SARIF v2.1.0 section 3.38.8). */
2968 : 63 : diagnostic_event::meaning m = ev.get_meaning ();
2969 : 63 : if (auto kinds_arr = maybe_make_kinds_array (m))
2970 : 63 : tfl_obj.set<json::array> ("kinds", std::move (kinds_arr));
2971 : :
2972 : : /* "nestingLevel" property (SARIF v2.1.0 section 3.38.10). */
2973 : 63 : tfl_obj.set_integer ("nestingLevel", ev.get_stack_depth ());
2974 : :
2975 : : /* "executionOrder" property (SARIF v2.1.0 3.38.11).
2976 : : Offset by 1 to match the human-readable values emitted by %@. */
2977 : 63 : tfl_obj.set_integer ("executionOrder", event_execution_idx + 1);
2978 : :
2979 : : /* It might be nice to eventually implement the following for -fanalyzer:
2980 : : - the "stack" property (SARIF v2.1.0 section 3.38.5)
2981 : : - the "state" property (SARIF v2.1.0 section 3.38.9)
2982 : : - the "importance" property (SARIF v2.1.0 section 3.38.13). */
2983 : 63 : }
2984 : :
2985 : : /* If M has any known meaning, make a json array suitable for the "kinds"
2986 : : property of a "threadFlowLocation" object (SARIF v2.1.0 section 3.38.8).
2987 : :
2988 : : Otherwise, return nullptr. */
2989 : :
2990 : : std::unique_ptr<json::array>
2991 : 63 : sarif_builder::maybe_make_kinds_array (diagnostic_event::meaning m) const
2992 : : {
2993 : 63 : if (m.m_verb == diagnostic_event::VERB_unknown
2994 : 21 : && m.m_noun == diagnostic_event::NOUN_unknown
2995 : 21 : && m.m_property == diagnostic_event::PROPERTY_unknown)
2996 : 21 : return nullptr;
2997 : :
2998 : 42 : auto kinds_arr = std::make_unique<json::array> ();
2999 : 84 : if (const char *verb_str
3000 : 42 : = diagnostic_event::meaning::maybe_get_verb_str (m.m_verb))
3001 : 42 : kinds_arr->append_string (verb_str);
3002 : 84 : if (const char *noun_str
3003 : 42 : = diagnostic_event::meaning::maybe_get_noun_str (m.m_noun))
3004 : 17 : kinds_arr->append_string (noun_str);
3005 : 84 : if (const char *property_str
3006 : 42 : = diagnostic_event::meaning::maybe_get_property_str (m.m_property))
3007 : 8 : kinds_arr->append_string (property_str);
3008 : 42 : return kinds_arr;
3009 : 42 : }
3010 : :
3011 : : /* In "3.11.5 Messages with placeholders":
3012 : : "Within both plain text and formatted message strings, the characters
3013 : : "{" and "}" SHALL be represented by the character sequences
3014 : : "{{" and "}}" respectively." */
3015 : :
3016 : : static std::string
3017 : 1124 : escape_braces (const char *text)
3018 : : {
3019 : 1124 : std::string result;
3020 : 57091 : while (char ch = *text++)
3021 : 55967 : switch (ch)
3022 : : {
3023 : 36 : case '{':
3024 : 36 : case '}':
3025 : 36 : result += ch;
3026 : : /* Fall through. */
3027 : 55967 : default:
3028 : 55967 : result += ch;
3029 : 55967 : break;
3030 : : }
3031 : 1124 : return result;
3032 : : }
3033 : :
3034 : : static void
3035 : 1124 : set_string_property_escaping_braces (json::object &obj,
3036 : : const char *property_name,
3037 : : const char *value)
3038 : : {
3039 : 1124 : std::string escaped (escape_braces (value));
3040 : 1124 : obj.set_string (property_name, escaped.c_str ());
3041 : 1124 : }
3042 : :
3043 : : /* Make a "message" object (SARIF v2.1.0 section 3.11) for MSG. */
3044 : :
3045 : : std::unique_ptr<sarif_message>
3046 : 962 : sarif_builder::make_message_object (const char *msg) const
3047 : : {
3048 : 962 : auto message_obj = std::make_unique<sarif_message> ();
3049 : :
3050 : : /* "text" property (SARIF v2.1.0 section 3.11.8). */
3051 : 962 : set_string_property_escaping_braces (*message_obj,
3052 : : "text", msg);
3053 : :
3054 : 962 : return message_obj;
3055 : : }
3056 : :
3057 : : /* Make a "message" object (SARIF v2.1.0 section 3.11) for DIAGRAM.
3058 : : We emit the diagram as a code block within the Markdown part
3059 : : of the message. */
3060 : :
3061 : : std::unique_ptr<sarif_message>
3062 : 4 : sarif_builder::make_message_object_for_diagram (const diagnostic_diagram &diagram)
3063 : : {
3064 : 4 : auto message_obj = std::make_unique<sarif_message> ();
3065 : :
3066 : : /* "text" property (SARIF v2.1.0 section 3.11.8). */
3067 : 4 : set_string_property_escaping_braces (*message_obj,
3068 : : "text", diagram.get_alt_text ());
3069 : :
3070 : 4 : pretty_printer *const pp = m_printer;
3071 : 4 : char *saved_prefix = pp_take_prefix (pp);
3072 : 4 : pp_set_prefix (pp, nullptr);
3073 : :
3074 : : /* "To produce a code block in Markdown, simply indent every line of
3075 : : the block by at least 4 spaces or 1 tab."
3076 : : Here we use 4 spaces. */
3077 : 4 : diagram.get_canvas ().print_to_pp (pp, " ");
3078 : 4 : pp_set_prefix (pp, saved_prefix);
3079 : :
3080 : : /* "markdown" property (SARIF v2.1.0 section 3.11.9). */
3081 : 4 : set_string_property_escaping_braces (*message_obj,
3082 : : "markdown", pp_formatted_text (pp));
3083 : :
3084 : 4 : pp_clear_output_area (pp);
3085 : :
3086 : 4 : return message_obj;
3087 : : }
3088 : :
3089 : : /* Make a "multiformatMessageString object" (SARIF v2.1.0 section 3.12)
3090 : : for MSG. */
3091 : :
3092 : : std::unique_ptr<sarif_multiformat_message_string>
3093 : 154 : sarif_builder::make_multiformat_message_string (const char *msg) const
3094 : : {
3095 : 154 : auto message_obj = std::make_unique<sarif_multiformat_message_string> ();
3096 : :
3097 : : /* "text" property (SARIF v2.1.0 section 3.12.3). */
3098 : 154 : set_string_property_escaping_braces (*message_obj,
3099 : : "text", msg);
3100 : :
3101 : 154 : return message_obj;
3102 : : }
3103 : :
3104 : : /* Convert VERSION to a value for the "$schema" property
3105 : : of a "sarifLog" object (SARIF v2.1.0 section 3.13.3). */
3106 : :
3107 : : static const char *
3108 : 271 : sarif_version_to_url (enum sarif_version version)
3109 : : {
3110 : 271 : switch (version)
3111 : : {
3112 : 0 : default:
3113 : 0 : gcc_unreachable ();
3114 : : case sarif_version::v2_1_0:
3115 : : return "https://docs.oasis-open.org/sarif/sarif/v2.1.0/errata01/os/schemas/sarif-schema-2.1.0.json";
3116 : 90 : case sarif_version::v2_2_prerelease_2024_08_08:
3117 : 90 : return "https://raw.githubusercontent.com/oasis-tcs/sarif-spec/refs/tags/2.2-prerelease-2024-08-08/sarif-2.2/schema/sarif-2-2.schema.json";
3118 : : }
3119 : : }
3120 : :
3121 : : /* Convert VERSION to a value for the "version" property
3122 : : of a "sarifLog" object (SARIF v2.1.0 section 3.13.2). */
3123 : :
3124 : : static const char *
3125 : 271 : sarif_version_to_property (enum sarif_version version)
3126 : : {
3127 : 271 : switch (version)
3128 : : {
3129 : 0 : default:
3130 : 0 : gcc_unreachable ();
3131 : : case sarif_version::v2_1_0:
3132 : : return "2.1.0";
3133 : 90 : case sarif_version::v2_2_prerelease_2024_08_08:
3134 : : /* I would have used "2.2-prerelease-2024-08-08",
3135 : : but the schema only accepts "2.2". */
3136 : 90 : return "2.2";
3137 : : }
3138 : : }
3139 : :
3140 : : /* Make a top-level "sarifLog" object (SARIF v2.1.0 section 3.13). */
3141 : :
3142 : : std::unique_ptr<sarif_log>
3143 : 263 : sarif_builder::
3144 : : make_top_level_object (std::unique_ptr<sarif_invocation> invocation_obj,
3145 : : std::unique_ptr<json::array> results)
3146 : : {
3147 : 263 : auto log_obj = std::make_unique<sarif_log> ();
3148 : :
3149 : : /* "$schema" property (SARIF v2.1.0 section 3.13.3) . */
3150 : 263 : log_obj->set_string ("$schema", sarif_version_to_url (get_version ()));
3151 : :
3152 : : /* "version" property (SARIF v2.1.0 section 3.13.2). */
3153 : 263 : log_obj->set_string ("version", sarif_version_to_property (get_version ()));
3154 : :
3155 : : /* "runs" property (SARIF v2.1.0 section 3.13.4). */
3156 : 263 : auto run_arr = std::make_unique<json::array> ();
3157 : 263 : auto run_obj = make_run_object (std::move (invocation_obj),
3158 : 263 : std::move (results));
3159 : 263 : run_arr->append<sarif_run> (std::move (run_obj));
3160 : 263 : log_obj->set<json::array> ("runs", std::move (run_arr));
3161 : :
3162 : 526 : return log_obj;
3163 : 263 : }
3164 : :
3165 : : /* Make a "run" object (SARIF v2.1.0 section 3.14). */
3166 : :
3167 : : std::unique_ptr<sarif_run>
3168 : 263 : sarif_builder::
3169 : : make_run_object (std::unique_ptr<sarif_invocation> invocation_obj,
3170 : : std::unique_ptr<json::array> results)
3171 : : {
3172 : 263 : auto run_obj = std::make_unique<sarif_run> ();
3173 : :
3174 : : /* "tool" property (SARIF v2.1.0 section 3.14.6). */
3175 : 263 : run_obj->set<sarif_tool> ("tool", make_tool_object ());
3176 : :
3177 : : /* "taxonomies" property (SARIF v2.1.0 section 3.14.8). */
3178 : 263 : if (auto taxonomies_arr = maybe_make_taxonomies_array ())
3179 : 263 : run_obj->set<json::array> ("taxonomies", std::move (taxonomies_arr));
3180 : :
3181 : : /* "invocations" property (SARIF v2.1.0 section 3.14.11). */
3182 : 263 : {
3183 : 263 : auto invocations_arr = std::make_unique<json::array> ();
3184 : 263 : invocations_arr->append (std::move (invocation_obj));
3185 : 263 : run_obj->set<json::array> ("invocations", std::move (invocations_arr));
3186 : 263 : }
3187 : :
3188 : : /* "originalUriBaseIds (SARIF v2.1.0 section 3.14.14). */
3189 : 263 : if (m_seen_any_relative_paths)
3190 : : {
3191 : 44 : auto orig_uri_base_ids = std::make_unique<json::object> ();
3192 : 44 : orig_uri_base_ids->set<sarif_artifact_location>
3193 : 44 : (PWD_PROPERTY_NAME, make_artifact_location_object_for_pwd ());
3194 : 44 : run_obj->set<json::object> ("originalUriBaseIds",
3195 : : std::move (orig_uri_base_ids));
3196 : 44 : }
3197 : :
3198 : : /* "artifacts" property (SARIF v2.1.0 section 3.14.15). */
3199 : 263 : auto artifacts_arr = std::make_unique<json::array> ();
3200 : 807 : for (auto iter : m_filename_to_artifact_map)
3201 : : {
3202 : 281 : sarif_artifact *artifact_obj = iter.second;
3203 : 281 : if (artifact_obj->embed_contents_p ())
3204 : 223 : artifact_obj->populate_contents (*this);
3205 : 281 : artifact_obj->populate_roles ();
3206 : 281 : artifacts_arr->append (artifact_obj);
3207 : : }
3208 : 263 : run_obj->set<json::array> ("artifacts", std::move (artifacts_arr));
3209 : 263 : m_filename_to_artifact_map.empty ();
3210 : :
3211 : : /* "results" property (SARIF v2.1.0 section 3.14.23). */
3212 : 263 : run_obj->set<json::array> ("results", std::move (results));
3213 : :
3214 : : /* "logicalLocations" property (SARIF v2.1.0 3.14.17). */
3215 : 263 : if (m_cached_logical_locs->size () > 0)
3216 : : {
3217 : 46 : m_cached_logical_locs->add_explicit_index_values ();
3218 : 46 : run_obj->set<json::array> ("logicalLocations",
3219 : 46 : std::move (m_cached_logical_locs));
3220 : : }
3221 : :
3222 : 526 : return run_obj;
3223 : 263 : }
3224 : :
3225 : : /* Make a "tool" object (SARIF v2.1.0 section 3.18). */
3226 : :
3227 : : std::unique_ptr<sarif_tool>
3228 : 263 : sarif_builder::make_tool_object ()
3229 : : {
3230 : 263 : auto tool_obj = std::make_unique<sarif_tool> ();
3231 : :
3232 : : /* "driver" property (SARIF v2.1.0 section 3.18.2). */
3233 : 263 : tool_obj->set<sarif_tool_component> ("driver",
3234 : 263 : make_driver_tool_component_object ());
3235 : :
3236 : : /* Report plugins via the "extensions" property
3237 : : (SARIF v2.1.0 section 3.18.3). */
3238 : 263 : if (auto client_data_hooks = m_context.get_client_data_hooks ())
3239 : 190 : if (const client_version_info *vinfo
3240 : 95 : = client_data_hooks->get_any_version_info ())
3241 : : {
3242 : 95 : class my_plugin_visitor : public client_version_info :: plugin_visitor
3243 : : {
3244 : : public:
3245 : 8 : void on_plugin (const diagnostic_client_plugin_info &p) final override
3246 : : {
3247 : : /* Create a "toolComponent" object (SARIF v2.1.0 section 3.19)
3248 : : for the plugin. */
3249 : 8 : auto plugin_obj = std::make_unique<sarif_tool_component> ();
3250 : :
3251 : : /* "name" property (SARIF v2.1.0 section 3.19.8). */
3252 : 8 : if (const char *short_name = p.get_short_name ())
3253 : 8 : plugin_obj->set_string ("name", short_name);
3254 : :
3255 : : /* "fullName" property (SARIF v2.1.0 section 3.19.9). */
3256 : 8 : if (const char *full_name = p.get_full_name ())
3257 : 8 : plugin_obj->set_string ("fullName", full_name);
3258 : :
3259 : : /* "version" property (SARIF v2.1.0 section 3.19.13). */
3260 : 8 : if (const char *version = p.get_version ())
3261 : 0 : plugin_obj->set_string ("version", version);
3262 : :
3263 : 8 : m_plugin_objs.push_back (std::move (plugin_obj));
3264 : 8 : }
3265 : : std::vector<std::unique_ptr<sarif_tool_component>> m_plugin_objs;
3266 : : };
3267 : 95 : my_plugin_visitor v;
3268 : 95 : vinfo->for_each_plugin (v);
3269 : 95 : if (v.m_plugin_objs.size () > 0)
3270 : : {
3271 : 8 : auto extensions_arr = std::make_unique<json::array> ();
3272 : 16 : for (auto &iter : v.m_plugin_objs)
3273 : 8 : extensions_arr->append<sarif_tool_component> (std::move (iter));
3274 : 8 : tool_obj->set<json::array> ("extensions",
3275 : : std::move (extensions_arr));
3276 : 8 : }
3277 : 95 : }
3278 : :
3279 : : /* Perhaps we could also show GMP, MPFR, MPC, isl versions as other
3280 : : "extensions" (see toplev.cc: print_version). */
3281 : :
3282 : 263 : return tool_obj;
3283 : : }
3284 : :
3285 : : /* Make a "toolComponent" object (SARIF v2.1.0 section 3.19) for what SARIF
3286 : : calls the "driver" (see SARIF v2.1.0 section 3.18.1). */
3287 : :
3288 : : std::unique_ptr<sarif_tool_component>
3289 : 263 : sarif_builder::make_driver_tool_component_object ()
3290 : : {
3291 : 263 : auto driver_obj = std::make_unique<sarif_tool_component> ();
3292 : :
3293 : 263 : if (auto client_data_hooks = m_context.get_client_data_hooks ())
3294 : 190 : if (const client_version_info *vinfo
3295 : 95 : = client_data_hooks->get_any_version_info ())
3296 : : {
3297 : : /* "name" property (SARIF v2.1.0 section 3.19.8). */
3298 : 95 : if (const char *name = vinfo->get_tool_name ())
3299 : 95 : driver_obj->set_string ("name", name);
3300 : :
3301 : : /* "fullName" property (SARIF v2.1.0 section 3.19.9). */
3302 : 95 : if (char *full_name = vinfo->maybe_make_full_name ())
3303 : : {
3304 : 95 : driver_obj->set_string ("fullName", full_name);
3305 : 95 : free (full_name);
3306 : : }
3307 : :
3308 : : /* "version" property (SARIF v2.1.0 section 3.19.13). */
3309 : 95 : if (const char *version = vinfo->get_version_string ())
3310 : 95 : driver_obj->set_string ("version", version);
3311 : :
3312 : : /* "informationUri" property (SARIF v2.1.0 section 3.19.17). */
3313 : 95 : if (char *version_url = vinfo->maybe_make_version_url ())
3314 : : {
3315 : 95 : driver_obj->set_string ("informationUri", version_url);
3316 : 95 : free (version_url);
3317 : : }
3318 : : }
3319 : :
3320 : : /* "rules" property (SARIF v2.1.0 section 3.19.23). */
3321 : 263 : driver_obj->set<json::array> ("rules", std::move (m_rules_arr));
3322 : :
3323 : 263 : return driver_obj;
3324 : : }
3325 : :
3326 : : /* If we've seen any CWE IDs, make an array for the "taxonomies" property
3327 : : (SARIF v2.1.0 section 3.14.8) of a run object, containing a single
3328 : : "toolComponent" (3.19) as per 3.19.3, representing the CWE.
3329 : :
3330 : : Otherwise return nullptr. */
3331 : :
3332 : : std::unique_ptr<json::array>
3333 : 263 : sarif_builder::maybe_make_taxonomies_array () const
3334 : : {
3335 : 263 : auto cwe_obj = maybe_make_cwe_taxonomy_object ();
3336 : 263 : if (!cwe_obj)
3337 : 245 : return nullptr;
3338 : :
3339 : : /* "taxonomies" property (SARIF v2.1.0 section 3.14.8). */
3340 : 18 : auto taxonomies_arr = std::make_unique<json::array> ();
3341 : 18 : taxonomies_arr->append<sarif_tool_component> (std::move (cwe_obj));
3342 : 18 : return taxonomies_arr;
3343 : 263 : }
3344 : :
3345 : : /* If we've seen any CWE IDs, make a "toolComponent" object
3346 : : (SARIF v2.1.0 section 3.19) representing the CWE taxonomy, as per 3.19.3.
3347 : : Populate the "taxa" property with all of the CWE IDs in m_cwe_id_set.
3348 : :
3349 : : Otherwise return nullptr. */
3350 : :
3351 : : std::unique_ptr<sarif_tool_component>
3352 : 263 : sarif_builder::maybe_make_cwe_taxonomy_object () const
3353 : : {
3354 : 263 : if (m_cwe_id_set.is_empty ())
3355 : 245 : return nullptr;
3356 : :
3357 : 18 : auto taxonomy_obj = std::make_unique<sarif_tool_component> ();
3358 : :
3359 : : /* "name" property (SARIF v2.1.0 section 3.19.8). */
3360 : 18 : taxonomy_obj->set_string ("name", "CWE");
3361 : :
3362 : : /* "version" property (SARIF v2.1.0 section 3.19.13). */
3363 : 18 : taxonomy_obj->set_string ("version", "4.7");
3364 : :
3365 : : /* "organization" property (SARIF v2.1.0 section 3.19.18). */
3366 : 18 : taxonomy_obj->set_string ("organization", "MITRE");
3367 : :
3368 : : /* "shortDescription" property (SARIF v2.1.0 section 3.19.19). */
3369 : 18 : taxonomy_obj->set<sarif_multiformat_message_string>
3370 : 18 : ("shortDescription",
3371 : 18 : make_multiformat_message_string ("The MITRE"
3372 : : " Common Weakness Enumeration"));
3373 : :
3374 : : /* "taxa" property (SARIF v2.1.0 3.section 3.19.25). */
3375 : 18 : auto taxa_arr = std::make_unique<json::array> ();
3376 : 36 : for (auto cwe_id : m_cwe_id_set)
3377 : 18 : taxa_arr->append<sarif_reporting_descriptor>
3378 : 18 : (make_reporting_descriptor_object_for_cwe_id (cwe_id));
3379 : 18 : taxonomy_obj->set<json::array> ("taxa", std::move (taxa_arr));
3380 : :
3381 : 18 : return taxonomy_obj;
3382 : 18 : }
3383 : :
3384 : : /* Ensure that we have an "artifact" object (SARIF v2.1.0 section 3.24)
3385 : : for FILENAME, adding it to m_filename_to_artifact_map if not already
3386 : : found, and adding ROLE to it.
3387 : : If EMBED_CONTENTS is true, then flag that we will attempt to embed the
3388 : : contents of this artifact when writing it out. */
3389 : :
3390 : : sarif_artifact &
3391 : 907 : sarif_builder::get_or_create_artifact (const char *filename,
3392 : : enum diagnostic_artifact_role role,
3393 : : bool embed_contents)
3394 : : {
3395 : 907 : if (auto *slot = m_filename_to_artifact_map.get (filename))
3396 : : {
3397 : 490 : (*slot)->add_role (role, embed_contents);
3398 : 490 : return **slot;
3399 : : }
3400 : :
3401 : 417 : sarif_artifact *artifact_obj = new sarif_artifact (filename);
3402 : 417 : artifact_obj->add_role (role, embed_contents);
3403 : 417 : m_filename_to_artifact_map.put (filename, artifact_obj);
3404 : :
3405 : : /* "location" property (SARIF v2.1.0 section 3.24.2). */
3406 : 417 : artifact_obj->set<sarif_artifact_location>
3407 : 417 : ("location", make_artifact_location_object (filename));
3408 : :
3409 : : /* "sourceLanguage" property (SARIF v2.1.0 section 3.24.10). */
3410 : 417 : switch (role)
3411 : : {
3412 : 0 : default:
3413 : 0 : gcc_unreachable ();
3414 : 417 : case diagnostic_artifact_role::analysis_target:
3415 : 417 : case diagnostic_artifact_role::result_file:
3416 : 417 : case diagnostic_artifact_role::scanned_file:
3417 : 417 : case diagnostic_artifact_role::traced_file:
3418 : : /* Assume that these are in the source language. */
3419 : 417 : if (auto client_data_hooks = m_context.get_client_data_hooks ())
3420 : 226 : if (const char *source_lang
3421 : 113 : = client_data_hooks->maybe_get_sarif_source_language (filename))
3422 : 113 : artifact_obj->set_string ("sourceLanguage", source_lang);
3423 : : break;
3424 : :
3425 : : case diagnostic_artifact_role::debug_output_file:
3426 : : /* Assume that these are not in the source language. */
3427 : : break;
3428 : : }
3429 : :
3430 : : return *artifact_obj;
3431 : : }
3432 : :
3433 : : /* Make an "artifactContent" object (SARIF v2.1.0 section 3.3) for the
3434 : : full contents of FILENAME. */
3435 : :
3436 : : std::unique_ptr<sarif_artifact_content>
3437 : 223 : sarif_builder::maybe_make_artifact_content_object (const char *filename) const
3438 : : {
3439 : : /* Let input.cc handle any charset conversion. */
3440 : 223 : char_span utf8_content
3441 : 223 : = m_context.get_file_cache ().get_source_file_content (filename);
3442 : 223 : if (!utf8_content)
3443 : 4 : return nullptr;
3444 : :
3445 : : /* Don't add it if it's not valid UTF-8. */
3446 : 219 : if (!cpp_valid_utf8_p(utf8_content.get_buffer (), utf8_content.length ()))
3447 : 12 : return nullptr;
3448 : :
3449 : 207 : auto artifact_content_obj = std::make_unique<sarif_artifact_content> ();
3450 : 207 : artifact_content_obj->set<json::string>
3451 : 414 : ("text",
3452 : 207 : std::make_unique <json::string> (utf8_content.get_buffer (),
3453 : 207 : utf8_content.length ()));
3454 : 207 : return artifact_content_obj;
3455 : 207 : }
3456 : :
3457 : : /* Attempt to read the given range of lines from FILENAME; return
3458 : : a freshly-allocated 0-terminated buffer containing them, or nullptr. */
3459 : :
3460 : : char *
3461 : 632 : sarif_builder::get_source_lines (const char *filename,
3462 : : int start_line,
3463 : : int end_line) const
3464 : : {
3465 : 632 : auto_vec<char> result;
3466 : :
3467 : 1264 : for (int line = start_line; line <= end_line; line++)
3468 : : {
3469 : 632 : char_span line_content
3470 : 632 : = m_context.get_file_cache ().get_source_line (filename, line);
3471 : 632 : if (!line_content.get_buffer ())
3472 : 0 : return nullptr;
3473 : 632 : result.reserve (line_content.length () + 1);
3474 : 13939 : for (size_t i = 0; i < line_content.length (); i++)
3475 : 13307 : result.quick_push (line_content[i]);
3476 : 632 : result.quick_push ('\n');
3477 : : }
3478 : 632 : result.safe_push ('\0');
3479 : :
3480 : 1264 : return xstrdup (result.address ());
3481 : 632 : }
3482 : :
3483 : : /* Make an "artifactContent" object (SARIF v2.1.0 section 3.3) for the given
3484 : : run of lines within FILENAME (including the endpoints).
3485 : : If R is non-NULL, use it to potentially set the "rendered"
3486 : : property (3.3.4). */
3487 : :
3488 : : std::unique_ptr<sarif_artifact_content>
3489 : 632 : sarif_builder::
3490 : : maybe_make_artifact_content_object (const char *filename,
3491 : : int start_line,
3492 : : int end_line,
3493 : : const content_renderer *r) const
3494 : : {
3495 : 632 : char *text_utf8 = get_source_lines (filename, start_line, end_line);
3496 : :
3497 : 632 : if (!text_utf8)
3498 : 0 : return nullptr;
3499 : :
3500 : : /* Don't add it if it's not valid UTF-8. */
3501 : 632 : if (!cpp_valid_utf8_p(text_utf8, strlen(text_utf8)))
3502 : : {
3503 : 172 : free (text_utf8);
3504 : 172 : return nullptr;
3505 : : }
3506 : :
3507 : 460 : auto artifact_content_obj = std::make_unique<sarif_artifact_content> ();
3508 : 460 : artifact_content_obj->set_string ("text", text_utf8);
3509 : 460 : free (text_utf8);
3510 : :
3511 : : /* 3.3.4 "rendered" property. */
3512 : 460 : if (r)
3513 : 136 : if (std::unique_ptr<sarif_multiformat_message_string> rendered
3514 : 136 : = r->render (*this))
3515 : 136 : artifact_content_obj->set ("rendered", std::move (rendered));
3516 : :
3517 : 460 : return artifact_content_obj;
3518 : 460 : }
3519 : :
3520 : : /* Make a "fix" object (SARIF v2.1.0 section 3.55) for RICHLOC. */
3521 : :
3522 : : std::unique_ptr<sarif_fix>
3523 : 8 : sarif_builder::make_fix_object (const rich_location &richloc)
3524 : : {
3525 : 8 : auto fix_obj = std::make_unique<sarif_fix> ();
3526 : :
3527 : : /* "artifactChanges" property (SARIF v2.1.0 section 3.55.3). */
3528 : : /* We assume that all fix-it hints in RICHLOC affect the same file. */
3529 : 8 : auto artifact_change_arr = std::make_unique<json::array> ();
3530 : 8 : artifact_change_arr->append<sarif_artifact_change>
3531 : 8 : (make_artifact_change_object (richloc));
3532 : 8 : fix_obj->set<json::array> ("artifactChanges",
3533 : : std::move (artifact_change_arr));
3534 : :
3535 : 16 : return fix_obj;
3536 : 8 : }
3537 : :
3538 : : /* Make an "artifactChange" object (SARIF v2.1.0 section 3.56) for RICHLOC. */
3539 : :
3540 : : std::unique_ptr<sarif_artifact_change>
3541 : 8 : sarif_builder::make_artifact_change_object (const rich_location &richloc)
3542 : : {
3543 : 8 : auto artifact_change_obj = std::make_unique<sarif_artifact_change> ();
3544 : :
3545 : : /* "artifactLocation" property (SARIF v2.1.0 section 3.56.2). */
3546 : 8 : artifact_change_obj->set<sarif_artifact_location>
3547 : 8 : ("artifactLocation",
3548 : 8 : make_artifact_location_object (richloc.get_loc ()));
3549 : :
3550 : : /* "replacements" property (SARIF v2.1.0 section 3.56.3). */
3551 : 8 : auto replacement_arr = std::make_unique<json::array> ();
3552 : 16 : for (unsigned int i = 0; i < richloc.get_num_fixit_hints (); i++)
3553 : : {
3554 : 8 : const fixit_hint *hint = richloc.get_fixit_hint (i);
3555 : 8 : replacement_arr->append<sarif_replacement>
3556 : 8 : (make_replacement_object (*hint));
3557 : : }
3558 : 8 : artifact_change_obj->set<json::array> ("replacements",
3559 : : std::move (replacement_arr));
3560 : :
3561 : 16 : return artifact_change_obj;
3562 : 8 : }
3563 : :
3564 : : /* Make a "replacement" object (SARIF v2.1.0 section 3.57) for HINT. */
3565 : :
3566 : : std::unique_ptr<sarif_replacement>
3567 : 8 : sarif_builder::make_replacement_object (const fixit_hint &hint) const
3568 : : {
3569 : 8 : auto replacement_obj = std::make_unique<sarif_replacement> ();
3570 : :
3571 : : /* "deletedRegion" property (SARIF v2.1.0 section 3.57.3). */
3572 : 8 : replacement_obj->set<sarif_region> ("deletedRegion",
3573 : 8 : make_region_object_for_hint (hint));
3574 : :
3575 : : /* "insertedContent" property (SARIF v2.1.0 section 3.57.4). */
3576 : 8 : replacement_obj->set<sarif_artifact_content>
3577 : 8 : ("insertedContent",
3578 : 8 : make_artifact_content_object (hint.get_string ()));
3579 : :
3580 : 8 : return replacement_obj;
3581 : : }
3582 : :
3583 : : /* Make an "artifactContent" object (SARIF v2.1.0 section 3.3) for TEXT. */
3584 : :
3585 : : std::unique_ptr<sarif_artifact_content>
3586 : 8 : sarif_builder::make_artifact_content_object (const char *text) const
3587 : : {
3588 : 8 : auto content_obj = std::make_unique<sarif_artifact_content> ();
3589 : :
3590 : : /* "text" property (SARIF v2.1.0 section 3.3.2). */
3591 : 8 : content_obj->set_string ("text", text);
3592 : :
3593 : 8 : return content_obj;
3594 : : }
3595 : :
3596 : : /* class diagnostic_sarif_format_buffer : public diagnostic_per_format_buffer. */
3597 : :
3598 : : void
3599 : 0 : diagnostic_sarif_format_buffer::dump (FILE *out, int indent) const
3600 : : {
3601 : 0 : fprintf (out, "%*sdiagnostic_sarif_format_buffer:\n", indent, "");
3602 : 0 : int idx = 0;
3603 : 0 : for (auto &result : m_results)
3604 : : {
3605 : 0 : fprintf (out, "%*sresult[%i]:\n", indent + 2, "", idx);
3606 : 0 : result->dump (out, true);
3607 : 0 : fprintf (out, "\n");
3608 : 0 : ++idx;
3609 : : }
3610 : 0 : }
3611 : :
3612 : : bool
3613 : 61 : diagnostic_sarif_format_buffer::empty_p () const
3614 : : {
3615 : 61 : return m_results.empty ();
3616 : : }
3617 : :
3618 : : void
3619 : 0 : diagnostic_sarif_format_buffer::move_to (diagnostic_per_format_buffer &base)
3620 : : {
3621 : 0 : diagnostic_sarif_format_buffer &dest
3622 : : = static_cast<diagnostic_sarif_format_buffer &> (base);
3623 : 0 : for (auto &&result : m_results)
3624 : 0 : dest.m_results.push_back (std::move (result));
3625 : 0 : m_results.clear ();
3626 : 0 : }
3627 : :
3628 : : void
3629 : 22 : diagnostic_sarif_format_buffer::clear ()
3630 : : {
3631 : 22 : m_results.clear ();
3632 : 22 : }
3633 : :
3634 : : void
3635 : 8 : diagnostic_sarif_format_buffer::flush ()
3636 : : {
3637 : 16 : for (auto &&result : m_results)
3638 : : {
3639 : 8 : result->process_worklist (m_builder);
3640 : 8 : m_builder.m_results_array->append<sarif_result> (std::move (result));
3641 : : }
3642 : 8 : m_results.clear ();
3643 : 8 : }
3644 : :
3645 : : class sarif_output_format : public diagnostic_output_format
3646 : : {
3647 : : public:
3648 : 271 : ~sarif_output_format ()
3649 : 271 : {
3650 : : /* Any sarifResult objects should have been handled by now.
3651 : : If not, then something's gone wrong with diagnostic
3652 : : groupings. */
3653 : 271 : std::unique_ptr<sarif_result> pending_result
3654 : 271 : = m_builder.take_current_result ();
3655 : 271 : gcc_assert (!pending_result);
3656 : 271 : }
3657 : :
3658 : 0 : void dump (FILE *out, int indent) const override
3659 : : {
3660 : 0 : fprintf (out, "%*ssarif_output_format\n", indent, "");
3661 : 0 : diagnostic_output_format::dump (out, indent);
3662 : 0 : }
3663 : :
3664 : : void
3665 : 271 : set_main_input_filename (const char *name) final override
3666 : : {
3667 : 95 : m_builder.set_main_input_filename (name);
3668 : 95 : }
3669 : :
3670 : : std::unique_ptr<diagnostic_per_format_buffer>
3671 : 17 : make_per_format_buffer () final override
3672 : : {
3673 : 17 : return std::make_unique<diagnostic_sarif_format_buffer> (m_builder);
3674 : : }
3675 : 297 : void set_buffer (diagnostic_per_format_buffer *base_buffer) final override
3676 : : {
3677 : 297 : diagnostic_sarif_format_buffer *buffer
3678 : : = static_cast<diagnostic_sarif_format_buffer *> (base_buffer);
3679 : 297 : m_buffer = buffer;
3680 : 297 : }
3681 : :
3682 : 0 : bool follows_reference_printer_p () const final override
3683 : : {
3684 : 0 : return false;
3685 : : }
3686 : :
3687 : 271 : void update_printer () final override
3688 : : {
3689 : 271 : m_printer = m_context.clone_printer ();
3690 : :
3691 : : /* Don't colorize the text. */
3692 : 271 : pp_show_color (m_printer.get ()) = false;
3693 : :
3694 : : /* No textual URLs. */
3695 : 271 : m_printer->set_url_format (URL_FORMAT_NONE);
3696 : :
3697 : : /* Use builder's token printer. */
3698 : 271 : get_printer ()->set_token_printer (&m_builder.get_token_printer ());
3699 : :
3700 : : /* Update the builder to use the new printer. */
3701 : 271 : m_builder.set_printer (*get_printer ());
3702 : 271 : }
3703 : :
3704 : 462 : void on_begin_group () final override
3705 : : {
3706 : : /* No-op, */
3707 : 462 : }
3708 : 462 : void on_end_group () final override
3709 : : {
3710 : 462 : m_builder.end_group ();
3711 : 462 : }
3712 : : void
3713 : 489 : on_report_diagnostic (const diagnostic_info &diagnostic,
3714 : : diagnostic_t orig_diag_kind) final override
3715 : : {
3716 : 489 : m_builder.on_report_diagnostic (diagnostic, orig_diag_kind, m_buffer);
3717 : 489 : }
3718 : 4 : void on_diagram (const diagnostic_diagram &diagram) final override
3719 : : {
3720 : 4 : m_builder.emit_diagram (diagram);
3721 : 4 : }
3722 : 464 : void after_diagnostic (const diagnostic_info &) final override
3723 : : {
3724 : : /* No-op. */
3725 : 464 : }
3726 : :
3727 : : sarif_builder &get_builder () { return m_builder; }
3728 : :
3729 : 96 : size_t num_results () const { return m_builder.num_results (); }
3730 : 16 : sarif_result &get_result (size_t idx) { return m_builder.get_result (idx); }
3731 : :
3732 : : protected:
3733 : 271 : sarif_output_format (diagnostic_context &context,
3734 : : const line_maps *line_maps,
3735 : : std::unique_ptr<sarif_serialization_format> serialization_format,
3736 : : const sarif_generation_options &sarif_gen_opts)
3737 : 271 : : diagnostic_output_format (context),
3738 : 271 : m_builder (context, *get_printer (), line_maps,
3739 : : std::move (serialization_format), sarif_gen_opts),
3740 : 271 : m_buffer (nullptr)
3741 : 271 : {}
3742 : :
3743 : : sarif_builder m_builder;
3744 : : diagnostic_sarif_format_buffer *m_buffer;
3745 : : };
3746 : :
3747 : : class sarif_stream_output_format : public sarif_output_format
3748 : : {
3749 : : public:
3750 : 0 : sarif_stream_output_format (diagnostic_context &context,
3751 : : const line_maps *line_maps,
3752 : : std::unique_ptr<sarif_serialization_format> serialization_format,
3753 : : const sarif_generation_options &sarif_gen_opts,
3754 : : FILE *stream)
3755 : 0 : : sarif_output_format (context, line_maps,
3756 : : std::move (serialization_format), sarif_gen_opts),
3757 : 0 : m_stream (stream)
3758 : : {
3759 : 0 : }
3760 : 0 : ~sarif_stream_output_format ()
3761 : 0 : {
3762 : 0 : m_builder.flush_to_file (m_stream);
3763 : 0 : }
3764 : 0 : bool machine_readable_stderr_p () const final override
3765 : : {
3766 : 0 : return m_stream == stderr;
3767 : : }
3768 : : private:
3769 : : FILE *m_stream;
3770 : : };
3771 : :
3772 : : class sarif_file_output_format : public sarif_output_format
3773 : : {
3774 : : public:
3775 : 95 : sarif_file_output_format (diagnostic_context &context,
3776 : : const line_maps *line_maps,
3777 : : std::unique_ptr<sarif_serialization_format> serialization_format,
3778 : : const sarif_generation_options &sarif_gen_opts,
3779 : : diagnostic_output_file output_file)
3780 : 95 : : sarif_output_format (context, line_maps,
3781 : : std::move (serialization_format), sarif_gen_opts),
3782 : 95 : m_output_file (std::move (output_file))
3783 : : {
3784 : 95 : gcc_assert (m_output_file.get_open_file ());
3785 : 95 : gcc_assert (m_output_file.get_filename ());
3786 : 95 : }
3787 : 190 : ~sarif_file_output_format ()
3788 : 95 : {
3789 : 95 : m_builder.flush_to_file (m_output_file.get_open_file ());
3790 : 190 : }
3791 : 0 : void dump (FILE *out, int indent) const override
3792 : : {
3793 : 0 : fprintf (out, "%*ssarif_file_output_format: %s\n",
3794 : : indent, "",
3795 : : m_output_file.get_filename ());
3796 : 0 : diagnostic_output_format::dump (out, indent);
3797 : 0 : }
3798 : 8 : bool machine_readable_stderr_p () const final override
3799 : : {
3800 : 8 : return false;
3801 : : }
3802 : :
3803 : : private:
3804 : : diagnostic_output_file m_output_file;
3805 : : };
3806 : :
3807 : : /* Print the start of an embedded link to PP, as per 3.11.6. */
3808 : :
3809 : : static void
3810 : 35 : sarif_begin_embedded_link (pretty_printer *pp)
3811 : : {
3812 : 0 : pp_character (pp, '[');
3813 : 9 : }
3814 : :
3815 : : /* Print the end of an embedded link to PP, as per 3.11.6. */
3816 : :
3817 : : static void
3818 : 35 : sarif_end_embedded_link (pretty_printer *pp,
3819 : : const char *url)
3820 : : {
3821 : 35 : pp_string (pp, "](");
3822 : : /* TODO: does the URI need escaping?
3823 : : See https://github.com/oasis-tcs/sarif-spec/issues/657 */
3824 : 35 : pp_string (pp, url);
3825 : 35 : pp_character (pp, ')');
3826 : 35 : }
3827 : :
3828 : : /* class sarif_token_printer : public token_printer. */
3829 : :
3830 : : /* Implementation of pretty_printer::token_printer for SARIF output.
3831 : : Emit URLs as per 3.11.6 ("Messages with embedded links"). */
3832 : :
3833 : : void
3834 : 527 : sarif_builder::sarif_token_printer::print_tokens (pretty_printer *pp,
3835 : : const pp_token_list &tokens)
3836 : : {
3837 : : /* Convert to text, possibly with colorization, URLs, etc. */
3838 : 527 : label_text current_url;
3839 : 2854 : for (auto iter = tokens.m_first; iter; iter = iter->m_next)
3840 : 2327 : switch (iter->m_kind)
3841 : : {
3842 : 0 : default:
3843 : 0 : gcc_unreachable ();
3844 : :
3845 : 1306 : case pp_token::kind::text:
3846 : 1306 : {
3847 : 1306 : const pp_token_text *sub = as_a <const pp_token_text *> (iter);
3848 : 1306 : const char * const str = sub->m_value.get ();
3849 : 1306 : if (current_url.get ())
3850 : : {
3851 : : /* Write iter->m_value, but escaping any
3852 : : escaped link characters as per 3.11.6. */
3853 : 296 : for (const char *ptr = str; *ptr; ptr++)
3854 : : {
3855 : 270 : const char ch = *ptr;
3856 : 270 : switch (ch)
3857 : : {
3858 : 230 : default:
3859 : 230 : pp_character (pp, ch);
3860 : 230 : break;
3861 : 40 : case '\\':
3862 : 40 : case '[':
3863 : 40 : case ']':
3864 : 40 : pp_character (pp, '\\');
3865 : 40 : pp_character (pp, ch);
3866 : 40 : break;
3867 : : }
3868 : : }
3869 : : }
3870 : : else
3871 : : /* TODO: is other escaping needed? (e.g. of '[')
3872 : : See https://github.com/oasis-tcs/sarif-spec/issues/658 */
3873 : 1280 : pp_string (pp, str);
3874 : : }
3875 : : break;
3876 : :
3877 : : case pp_token::kind::begin_color:
3878 : : case pp_token::kind::end_color:
3879 : : /* These are no-ops. */
3880 : : break;
3881 : :
3882 : 487 : case pp_token::kind::begin_quote:
3883 : 487 : pp_begin_quote (pp, pp_show_color (pp));
3884 : 487 : break;
3885 : 469 : case pp_token::kind::end_quote:
3886 : 469 : pp_end_quote (pp, pp_show_color (pp));
3887 : 469 : break;
3888 : :
3889 : : /* Emit URLs as per 3.11.6 ("Messages with embedded links"). */
3890 : 26 : case pp_token::kind::begin_url:
3891 : 26 : {
3892 : 26 : pp_token_begin_url *sub = as_a <pp_token_begin_url *> (iter);
3893 : 26 : sarif_begin_embedded_link (pp);
3894 : 26 : current_url = std::move (sub->m_value);
3895 : : }
3896 : 26 : break;
3897 : 26 : case pp_token::kind::end_url:
3898 : 26 : gcc_assert (current_url.get ());
3899 : 26 : sarif_end_embedded_link (pp, current_url.get ());
3900 : 26 : current_url = label_text::borrow (nullptr);
3901 : 26 : break;
3902 : :
3903 : 9 : case pp_token::kind::event_id:
3904 : 9 : {
3905 : 9 : pp_token_event_id *sub = as_a <pp_token_event_id *> (iter);
3906 : 9 : gcc_assert (sub->m_event_id.known_p ());
3907 : 9 : const sarif_code_flow *code_flow
3908 : 9 : = m_builder.get_code_flow_for_event_ids ();
3909 : 9 : label_text url = make_sarif_url_for_event (code_flow,
3910 : 9 : sub->m_event_id);
3911 : 9 : if (url.get ())
3912 : 9 : sarif_begin_embedded_link (pp);
3913 : 9 : pp_character (pp, '(');
3914 : 9 : pp_decimal_int (pp, sub->m_event_id.one_based ());
3915 : 9 : pp_character (pp, ')');
3916 : 9 : if (url.get ())
3917 : 9 : sarif_end_embedded_link (pp, url.get ());
3918 : 9 : }
3919 : 9 : break;
3920 : : }
3921 : 527 : }
3922 : :
3923 : : /* Populate CONTEXT in preparation for SARIF output (either to stderr, or
3924 : : to a file).
3925 : : Return a reference to *FMT. */
3926 : :
3927 : : static diagnostic_output_format &
3928 : 265 : diagnostic_output_format_init_sarif (diagnostic_context &context,
3929 : : std::unique_ptr<sarif_output_format> fmt)
3930 : : {
3931 : 265 : gcc_assert (fmt);
3932 : 265 : diagnostic_output_format &out = *fmt;
3933 : :
3934 : 265 : fmt->update_printer ();
3935 : :
3936 : 265 : context.set_output_format (std::move (fmt));
3937 : :
3938 : 265 : return out;
3939 : : }
3940 : :
3941 : : /* Populate CONTEXT in preparation for SARIF output to stderr.
3942 : : Return a reference to the new sink. */
3943 : :
3944 : : diagnostic_output_format &
3945 : 0 : diagnostic_output_format_init_sarif_stderr (diagnostic_context &context,
3946 : : const line_maps *line_maps,
3947 : : bool formatted)
3948 : : {
3949 : 0 : gcc_assert (line_maps);
3950 : 0 : const sarif_generation_options sarif_gen_opts;
3951 : 0 : auto serialization
3952 : 0 : = std::make_unique<sarif_serialization_format_json> (formatted);
3953 : 0 : return diagnostic_output_format_init_sarif
3954 : 0 : (context,
3955 : 0 : std::make_unique<sarif_stream_output_format> (context,
3956 : : line_maps,
3957 : : std::move (serialization),
3958 : : sarif_gen_opts,
3959 : 0 : stderr));
3960 : 0 : }
3961 : :
3962 : : /* Attempt to open "BASE_FILE_NAME""EXTENSION" for writing.
3963 : : Return a non-null diagnostic_output_file,
3964 : : or return a null diagnostic_output_file and complain to CONTEXT
3965 : : using LINE_MAPS. */
3966 : :
3967 : : diagnostic_output_file
3968 : 93 : diagnostic_output_file::try_to_open (diagnostic_context &context,
3969 : : line_maps *line_maps,
3970 : : const char *base_file_name,
3971 : : const char *extension,
3972 : : bool is_binary)
3973 : : {
3974 : 93 : gcc_assert (extension);
3975 : 93 : gcc_assert (extension[0] == '.');
3976 : :
3977 : 93 : if (!base_file_name)
3978 : : {
3979 : 0 : rich_location richloc (line_maps, UNKNOWN_LOCATION);
3980 : 0 : context.emit_diagnostic_with_group
3981 : 0 : (DK_ERROR, richloc, nullptr, 0,
3982 : : "unable to determine filename for SARIF output");
3983 : 0 : return diagnostic_output_file ();
3984 : 0 : }
3985 : :
3986 : 93 : label_text filename = label_text::take (concat (base_file_name,
3987 : : extension,
3988 : 93 : nullptr));
3989 : 186 : FILE *outf = fopen (filename.get (), is_binary ? "wb" : "w");
3990 : 93 : if (!outf)
3991 : : {
3992 : 0 : rich_location richloc (line_maps, UNKNOWN_LOCATION);
3993 : 0 : context.emit_diagnostic_with_group
3994 : 0 : (DK_ERROR, richloc, nullptr, 0,
3995 : : "unable to open %qs for diagnostic output: %m",
3996 : : filename.get ());
3997 : 0 : return diagnostic_output_file ();
3998 : 0 : }
3999 : 93 : return diagnostic_output_file (outf, true, std::move (filename));
4000 : 93 : }
4001 : :
4002 : : /* Attempt to open BASE_FILE_NAME.sarif for writing JSON.
4003 : : Return a non-null diagnostic_output_file,
4004 : : or return a null diagnostic_output_file and complain to CONTEXT
4005 : : using LINE_MAPS. */
4006 : :
4007 : : diagnostic_output_file
4008 : 93 : diagnostic_output_format_open_sarif_file (diagnostic_context &context,
4009 : : line_maps *line_maps,
4010 : : const char *base_file_name,
4011 : : enum sarif_serialization_kind serialization_kind)
4012 : : {
4013 : 93 : const char *suffix;
4014 : 93 : bool is_binary;
4015 : 93 : switch (serialization_kind)
4016 : : {
4017 : 0 : default:
4018 : 0 : gcc_unreachable ();
4019 : 93 : case sarif_serialization_kind::json:
4020 : 93 : suffix = ".sarif";
4021 : 93 : is_binary = false;
4022 : 93 : break;
4023 : : }
4024 : :
4025 : 93 : return diagnostic_output_file::try_to_open (context,
4026 : : line_maps,
4027 : : base_file_name,
4028 : : suffix,
4029 : 93 : is_binary);
4030 : : }
4031 : :
4032 : : /* Populate CONTEXT in preparation for SARIF output to a file named
4033 : : BASE_FILE_NAME.sarif.
4034 : : Return a reference to the new sink. */
4035 : :
4036 : : diagnostic_output_format &
4037 : 89 : diagnostic_output_format_init_sarif_file (diagnostic_context &context,
4038 : : line_maps *line_maps,
4039 : : bool formatted,
4040 : : const char *base_file_name)
4041 : : {
4042 : 89 : gcc_assert (line_maps);
4043 : :
4044 : 89 : diagnostic_output_file output_file
4045 : : = diagnostic_output_format_open_sarif_file (context,
4046 : : line_maps,
4047 : : base_file_name,
4048 : 89 : sarif_serialization_kind::json);
4049 : 89 : auto serialization
4050 : 89 : = std::make_unique<sarif_serialization_format_json> (formatted);
4051 : :
4052 : 89 : const sarif_generation_options sarif_gen_opts;
4053 : 89 : return diagnostic_output_format_init_sarif
4054 : 89 : (context,
4055 : 89 : std::make_unique<sarif_file_output_format> (context,
4056 : : line_maps,
4057 : : std::move (serialization),
4058 : : sarif_gen_opts,
4059 : 89 : std::move (output_file)));
4060 : 89 : }
4061 : :
4062 : : /* Populate CONTEXT in preparation for SARIF output to STREAM.
4063 : : Return a reference to the new sink. */
4064 : :
4065 : : diagnostic_output_format &
4066 : 0 : diagnostic_output_format_init_sarif_stream (diagnostic_context &context,
4067 : : const line_maps *line_maps,
4068 : : bool formatted,
4069 : : FILE *stream)
4070 : : {
4071 : 0 : gcc_assert (line_maps);
4072 : 0 : const sarif_generation_options sarif_gen_opts;
4073 : 0 : auto serialization
4074 : 0 : = std::make_unique<sarif_serialization_format_json> (formatted);
4075 : 0 : return diagnostic_output_format_init_sarif
4076 : 0 : (context,
4077 : 0 : std::make_unique<sarif_stream_output_format> (context,
4078 : : line_maps,
4079 : : std::move (serialization),
4080 : : sarif_gen_opts,
4081 : 0 : stream));
4082 : 0 : }
4083 : :
4084 : : std::unique_ptr<diagnostic_output_format>
4085 : 6 : make_sarif_sink (diagnostic_context &context,
4086 : : const line_maps &line_maps,
4087 : : std::unique_ptr<sarif_serialization_format> serialization,
4088 : : const sarif_generation_options &sarif_gen_opts,
4089 : : diagnostic_output_file output_file)
4090 : : {
4091 : 6 : auto sink
4092 : : = std::make_unique<sarif_file_output_format> (context,
4093 : 12 : &line_maps,
4094 : : std::move (serialization),
4095 : : sarif_gen_opts,
4096 : 6 : std::move (output_file));
4097 : 6 : sink->update_printer ();
4098 : 6 : return sink;
4099 : 6 : }
4100 : :
4101 : : // struct sarif_generation_options
4102 : :
4103 : 303 : sarif_generation_options::sarif_generation_options ()
4104 : 303 : : m_version (sarif_version::v2_1_0)
4105 : : {
4106 : 303 : }
4107 : :
4108 : : #if CHECKING_P
4109 : :
4110 : : namespace selftest {
4111 : :
4112 : : static void
4113 : 4 : test_sarif_array_of_unique_1 ()
4114 : : {
4115 : 4 : sarif_array_of_unique<json::string> arr;
4116 : :
4117 : 4 : ASSERT_EQ (arr.length (), 0);
4118 : :
4119 : 4 : {
4120 : 4 : size_t idx = arr.append_uniquely (std::make_unique<json::string> ("foo"));
4121 : 4 : ASSERT_EQ (idx, 0);
4122 : 4 : ASSERT_EQ (arr.length (), 1);
4123 : : }
4124 : 4 : {
4125 : 4 : size_t idx = arr.append_uniquely (std::make_unique<json::string> ("bar"));
4126 : 4 : ASSERT_EQ (idx, 1);
4127 : 4 : ASSERT_EQ (arr.length (), 2);
4128 : : }
4129 : :
4130 : : /* Try adding them again, should be idempotent. */
4131 : 4 : {
4132 : 4 : size_t idx = arr.append_uniquely (std::make_unique<json::string> ("foo"));
4133 : 4 : ASSERT_EQ (idx, 0);
4134 : 4 : ASSERT_EQ (arr.length (), 2);
4135 : : }
4136 : 4 : {
4137 : 4 : size_t idx = arr.append_uniquely (std::make_unique<json::string> ("bar"));
4138 : 4 : ASSERT_EQ (idx, 1);
4139 : 4 : ASSERT_EQ (arr.length (), 2);
4140 : : }
4141 : 4 : }
4142 : :
4143 : : static void
4144 : 4 : test_sarif_array_of_unique_2 ()
4145 : : {
4146 : 4 : sarif_array_of_unique<json::object> arr;
4147 : :
4148 : 4 : ASSERT_EQ (arr.length (), 0);
4149 : :
4150 : 4 : {
4151 : 4 : auto obj0 = std::make_unique<json::object> ();
4152 : 4 : size_t idx = arr.append_uniquely (std::move (obj0));
4153 : 4 : ASSERT_EQ (idx, 0);
4154 : 4 : ASSERT_EQ (arr.length (), 1);
4155 : :
4156 : : // Attempting to add another empty objects should be idempotent.
4157 : 4 : idx = arr.append_uniquely (std::make_unique<json::object> ());
4158 : 4 : ASSERT_EQ (idx, 0);
4159 : 4 : ASSERT_EQ (arr.length (), 1);
4160 : 4 : }
4161 : 4 : {
4162 : 4 : auto obj1 = std::make_unique<json::object> ();
4163 : 4 : obj1->set_string ("foo", "bar");
4164 : 4 : size_t idx = arr.append_uniquely (std::move (obj1));
4165 : 4 : ASSERT_EQ (idx, 1);
4166 : 4 : ASSERT_EQ (arr.length (), 2);
4167 : :
4168 : : // Attempting to add an equivalent object should be idempotent.
4169 : 4 : auto other = std::make_unique<json::object> ();
4170 : 4 : other->set_string ("foo", "bar");
4171 : 4 : idx = arr.append_uniquely (std::move (other));
4172 : 4 : ASSERT_EQ (idx, 1);
4173 : 4 : ASSERT_EQ (arr.length (), 2);
4174 : 4 : }
4175 : :
4176 : : // Verify behavior of add_explicit_index_values.
4177 : 4 : arr.add_explicit_index_values ();
4178 : 4 : ASSERT_JSON_INT_PROPERTY_EQ (arr[0], "index", 0);
4179 : 4 : ASSERT_JSON_INT_PROPERTY_EQ (arr[1], "index", 1);
4180 : 4 : }
4181 : :
4182 : : /* A subclass of sarif_output_format for writing selftests.
4183 : : The JSON output is cached internally, rather than written
4184 : : out to a file. */
4185 : :
4186 : 352 : class test_sarif_diagnostic_context : public test_diagnostic_context
4187 : : {
4188 : : public:
4189 : 176 : test_sarif_diagnostic_context (const char *main_input_filename,
4190 : : const sarif_generation_options &sarif_gen_opts)
4191 : 176 : {
4192 : 176 : auto format = std::make_unique<buffered_output_format> (*this,
4193 : : line_table,
4194 : 352 : true,
4195 : 176 : sarif_gen_opts);
4196 : 176 : m_format = format.get (); // borrowed
4197 : 176 : diagnostic_output_format_init_sarif (*this, std::move (format));
4198 : 176 : m_format->set_main_input_filename (main_input_filename);
4199 : 176 : }
4200 : :
4201 : 168 : std::unique_ptr<sarif_log> flush_to_object ()
4202 : : {
4203 : 336 : return m_format->flush_to_object ();
4204 : : }
4205 : :
4206 : 96 : size_t num_results () const { return m_format->num_results (); }
4207 : 32 : sarif_result &get_result (size_t idx) { return m_format->get_result (idx); }
4208 : :
4209 : : private:
4210 : : class buffered_output_format : public sarif_output_format
4211 : : {
4212 : : public:
4213 : 176 : buffered_output_format (diagnostic_context &context,
4214 : : const line_maps *line_maps,
4215 : : bool formatted,
4216 : : const sarif_generation_options &sarif_gen_opts)
4217 : 176 : : sarif_output_format (context, line_maps,
4218 : : std::make_unique<sarif_serialization_format_json>
4219 : 176 : (formatted),
4220 : 176 : sarif_gen_opts)
4221 : : {
4222 : 176 : }
4223 : 0 : bool machine_readable_stderr_p () const final override
4224 : : {
4225 : 0 : return false;
4226 : : }
4227 : 168 : std::unique_ptr<sarif_log> flush_to_object ()
4228 : : {
4229 : 168 : return m_builder.flush_to_object ();
4230 : : }
4231 : : };
4232 : :
4233 : : buffered_output_format *m_format; // borrowed
4234 : : };
4235 : :
4236 : : /* Test making a sarif_location for a complex rich_location
4237 : : with labels and escape-on-output. */
4238 : :
4239 : : static void
4240 : 192 : test_make_location_object (const sarif_generation_options &sarif_gen_opts,
4241 : : const line_table_case &case_)
4242 : : {
4243 : 192 : diagnostic_show_locus_fixture_one_liner_utf8 f (case_);
4244 : 192 : location_t line_end = linemap_position_for_column (line_table, 31);
4245 : :
4246 : : /* Don't attempt to run the tests if column data might be unavailable. */
4247 : 192 : if (line_end > LINE_MAP_MAX_LOCATION_WITH_COLS)
4248 : 64 : return;
4249 : :
4250 : 128 : test_diagnostic_context dc;
4251 : 128 : pretty_printer pp;
4252 : 128 : sarif_builder builder
4253 : : (dc, pp, line_table,
4254 : 128 : std::make_unique<sarif_serialization_format_json> (true),
4255 : 128 : sarif_gen_opts);
4256 : :
4257 : : /* These "columns" are byte offsets, whereas later on the columns
4258 : : in the generated SARIF use sarif_builder::get_sarif_column and
4259 : : thus respect tabs, encoding. */
4260 : 128 : const location_t foo
4261 : 128 : = make_location (linemap_position_for_column (line_table, 1),
4262 : : linemap_position_for_column (line_table, 1),
4263 : : linemap_position_for_column (line_table, 8));
4264 : 128 : const location_t bar
4265 : 128 : = make_location (linemap_position_for_column (line_table, 12),
4266 : : linemap_position_for_column (line_table, 12),
4267 : : linemap_position_for_column (line_table, 17));
4268 : 128 : const location_t field
4269 : 128 : = make_location (linemap_position_for_column (line_table, 19),
4270 : : linemap_position_for_column (line_table, 19),
4271 : : linemap_position_for_column (line_table, 30));
4272 : :
4273 : 128 : text_range_label label0 ("label0");
4274 : 128 : text_range_label label1 ("label1");
4275 : 128 : text_range_label label2 ("label2");
4276 : :
4277 : 128 : rich_location richloc (line_table, foo, &label0, nullptr);
4278 : 128 : richloc.add_range (bar, SHOW_RANGE_WITHOUT_CARET, &label1);
4279 : 128 : richloc.add_range (field, SHOW_RANGE_WITHOUT_CARET, &label2);
4280 : 128 : richloc.set_escape_on_output (true);
4281 : :
4282 : 128 : sarif_result result (0);
4283 : :
4284 : 128 : std::unique_ptr<sarif_location> location_obj
4285 : : = builder.make_location_object
4286 : 128 : (result, richloc, logical_location (),
4287 : 128 : diagnostic_artifact_role::analysis_target);
4288 : 128 : ASSERT_NE (location_obj, nullptr);
4289 : :
4290 : 128 : auto physical_location
4291 : 128 : = EXPECT_JSON_OBJECT_WITH_OBJECT_PROPERTY (location_obj.get (),
4292 : : "physicalLocation");
4293 : 128 : {
4294 : 128 : auto region
4295 : 128 : = EXPECT_JSON_OBJECT_WITH_OBJECT_PROPERTY (physical_location, "region");
4296 : 128 : ASSERT_JSON_INT_PROPERTY_EQ (region, "startLine", 1);
4297 : 128 : ASSERT_JSON_INT_PROPERTY_EQ (region, "startColumn", 1);
4298 : 128 : ASSERT_JSON_INT_PROPERTY_EQ (region, "endColumn", 7);
4299 : : }
4300 : 128 : {
4301 : 128 : auto context_region
4302 : 128 : = EXPECT_JSON_OBJECT_WITH_OBJECT_PROPERTY (physical_location,
4303 : : "contextRegion");
4304 : 128 : ASSERT_JSON_INT_PROPERTY_EQ (context_region, "startLine", 1);
4305 : :
4306 : 128 : {
4307 : 128 : auto snippet
4308 : 128 : = EXPECT_JSON_OBJECT_WITH_OBJECT_PROPERTY (context_region, "snippet");
4309 : :
4310 : : /* We expect the snippet's "text" to be a copy of the content. */
4311 : 128 : ASSERT_JSON_STRING_PROPERTY_EQ (snippet, "text", f.m_content);
4312 : :
4313 : : /* We expect the snippet to have a "rendered" whose "text" has a
4314 : : pure ASCII escaped copy of the line (with labels, etc). */
4315 : 128 : {
4316 : 128 : auto rendered
4317 : 128 : = EXPECT_JSON_OBJECT_WITH_OBJECT_PROPERTY (snippet, "rendered");
4318 : 128 : ASSERT_JSON_STRING_PROPERTY_EQ
4319 : : (rendered, "text",
4320 : : "1 | <U+1F602>_foo = <U+03C0>_bar.<U+1F602>_field<U+03C0>;\n"
4321 : : " | ^~~~~~~~~~~~~ ~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~\n"
4322 : : " | | | |\n"
4323 : : " | label0 label1 label2\n");
4324 : : }
4325 : : }
4326 : : }
4327 : 128 : auto annotations
4328 : 128 : = EXPECT_JSON_OBJECT_WITH_ARRAY_PROPERTY (location_obj.get (),
4329 : : "annotations");
4330 : 128 : ASSERT_EQ (annotations->size (), 3);
4331 : 128 : {
4332 : 128 : {
4333 : 128 : auto a0 = (*annotations)[0];
4334 : 128 : ASSERT_JSON_INT_PROPERTY_EQ (a0, "startLine", 1);
4335 : 128 : ASSERT_JSON_INT_PROPERTY_EQ (a0, "startColumn", 1);
4336 : 128 : ASSERT_JSON_INT_PROPERTY_EQ (a0, "endColumn", 7);
4337 : 128 : auto message
4338 : 128 : = EXPECT_JSON_OBJECT_WITH_OBJECT_PROPERTY (a0, "message");
4339 : 128 : ASSERT_JSON_STRING_PROPERTY_EQ (message, "text", "label0");
4340 : : }
4341 : 128 : {
4342 : 128 : auto a1 = (*annotations)[1];
4343 : 128 : ASSERT_JSON_INT_PROPERTY_EQ (a1, "startLine", 1);
4344 : 128 : ASSERT_JSON_INT_PROPERTY_EQ (a1, "startColumn", 10);
4345 : 128 : ASSERT_JSON_INT_PROPERTY_EQ (a1, "endColumn", 15);
4346 : 128 : auto message
4347 : 128 : = EXPECT_JSON_OBJECT_WITH_OBJECT_PROPERTY (a1, "message");
4348 : 128 : ASSERT_JSON_STRING_PROPERTY_EQ (message, "text", "label1");
4349 : : }
4350 : 128 : {
4351 : 128 : auto a2 = (*annotations)[2];
4352 : 128 : ASSERT_JSON_INT_PROPERTY_EQ (a2, "startLine", 1);
4353 : 128 : ASSERT_JSON_INT_PROPERTY_EQ (a2, "startColumn", 16);
4354 : 128 : ASSERT_JSON_INT_PROPERTY_EQ (a2, "endColumn", 25);
4355 : 128 : auto message
4356 : 128 : = EXPECT_JSON_OBJECT_WITH_OBJECT_PROPERTY (a2, "message");
4357 : 128 : ASSERT_JSON_STRING_PROPERTY_EQ (message, "text", "label2");
4358 : : }
4359 : : }
4360 : 192 : }
4361 : :
4362 : : /* Test of reporting a diagnostic at UNKNOWN_LOCATION to a
4363 : : diagnostic_context and examining the generated sarif_log.
4364 : : Verify various basic properties. */
4365 : :
4366 : : static void
4367 : 8 : test_simple_log (const sarif_generation_options &sarif_gen_opts)
4368 : : {
4369 : 8 : test_sarif_diagnostic_context dc ("MAIN_INPUT_FILENAME", sarif_gen_opts);
4370 : :
4371 : 8 : rich_location richloc (line_table, UNKNOWN_LOCATION);
4372 : 8 : dc.report (DK_ERROR, richloc, nullptr, 0, "this is a test: %i", 42);
4373 : :
4374 : 8 : auto log_ptr = dc.flush_to_object ();
4375 : :
4376 : : // 3.13 sarifLog:
4377 : 8 : auto log = log_ptr.get ();
4378 : 8 : const enum sarif_version version = sarif_gen_opts.m_version;
4379 : 8 : ASSERT_JSON_STRING_PROPERTY_EQ (log, "$schema",
4380 : : sarif_version_to_url (version));
4381 : 8 : ASSERT_JSON_STRING_PROPERTY_EQ (log, "version",
4382 : : sarif_version_to_property (version));
4383 : :
4384 : 8 : auto runs = EXPECT_JSON_OBJECT_WITH_ARRAY_PROPERTY (log, "runs"); // 3.13.4
4385 : 8 : ASSERT_EQ (runs->size (), 1);
4386 : :
4387 : : // 3.14 "run" object:
4388 : 8 : auto run = (*runs)[0];
4389 : :
4390 : 8 : {
4391 : : // 3.14.6:
4392 : 8 : auto tool = EXPECT_JSON_OBJECT_WITH_OBJECT_PROPERTY (run, "tool");
4393 : :
4394 : 8 : EXPECT_JSON_OBJECT_WITH_OBJECT_PROPERTY (tool, "driver"); // 3.18.2
4395 : : }
4396 : :
4397 : 8 : {
4398 : : // 3.14.11
4399 : 8 : auto invocations
4400 : 8 : = EXPECT_JSON_OBJECT_WITH_ARRAY_PROPERTY (run, "invocations");
4401 : 8 : ASSERT_EQ (invocations->size (), 1);
4402 : :
4403 : 8 : {
4404 : : // 3.20 "invocation" object:
4405 : 8 : auto invocation = (*invocations)[0];
4406 : :
4407 : : // 3.20.3 arguments property
4408 : :
4409 : : // 3.20.7 startTimeUtc property
4410 : 8 : EXPECT_JSON_OBJECT_WITH_STRING_PROPERTY (invocation, "startTimeUtc");
4411 : :
4412 : : // 3.20.8 endTimeUtc property
4413 : 8 : EXPECT_JSON_OBJECT_WITH_STRING_PROPERTY (invocation, "endTimeUtc");
4414 : :
4415 : : // 3.20.19 workingDirectory property
4416 : 8 : {
4417 : 8 : auto wd_obj
4418 : 8 : = EXPECT_JSON_OBJECT_WITH_OBJECT_PROPERTY (invocation,
4419 : : "workingDirectory");
4420 : 8 : EXPECT_JSON_OBJECT_WITH_STRING_PROPERTY (wd_obj, "uri");
4421 : : }
4422 : :
4423 : : // 3.20.21 toolExecutionNotifications property
4424 : 8 : auto notifications
4425 : 8 : = EXPECT_JSON_OBJECT_WITH_ARRAY_PROPERTY
4426 : : (invocation, "toolExecutionNotifications");
4427 : 8 : ASSERT_EQ (notifications->size (), 0);
4428 : : }
4429 : : }
4430 : :
4431 : 8 : {
4432 : : // 3.14.15:
4433 : 8 : auto artifacts = EXPECT_JSON_OBJECT_WITH_ARRAY_PROPERTY (run, "artifacts");
4434 : 8 : ASSERT_EQ (artifacts->size (), 1);
4435 : :
4436 : 8 : {
4437 : : // 3.24 "artifact" object:
4438 : 8 : auto artifact = (*artifacts)[0];
4439 : :
4440 : : // 3.24.2:
4441 : 8 : auto location
4442 : 8 : = EXPECT_JSON_OBJECT_WITH_OBJECT_PROPERTY (artifact, "location");
4443 : 8 : ASSERT_JSON_STRING_PROPERTY_EQ (location, "uri", "MAIN_INPUT_FILENAME");
4444 : :
4445 : : // 3.24.6:
4446 : 8 : auto roles = EXPECT_JSON_OBJECT_WITH_ARRAY_PROPERTY (artifact, "roles");
4447 : 8 : ASSERT_EQ (roles->size (), 1);
4448 : 8 : {
4449 : 8 : auto role = (*roles)[0];
4450 : 8 : ASSERT_JSON_STRING_EQ (role, "analysisTarget");
4451 : : }
4452 : : }
4453 : : }
4454 : :
4455 : 8 : {
4456 : : // 3.14.23:
4457 : 8 : auto results = EXPECT_JSON_OBJECT_WITH_ARRAY_PROPERTY (run, "results");
4458 : 8 : ASSERT_EQ (results->size (), 1);
4459 : :
4460 : 8 : {
4461 : : // 3.27 "result" object:
4462 : 8 : auto result = (*results)[0];
4463 : 8 : ASSERT_JSON_STRING_PROPERTY_EQ (result, "ruleId", "error");
4464 : 8 : ASSERT_JSON_STRING_PROPERTY_EQ (result, "level", "error"); // 3.27.10
4465 : :
4466 : 8 : {
4467 : : // 3.27.11:
4468 : 8 : auto message
4469 : 8 : = EXPECT_JSON_OBJECT_WITH_OBJECT_PROPERTY (result, "message");
4470 : 8 : ASSERT_JSON_STRING_PROPERTY_EQ (message, "text",
4471 : : "this is a test: 42");
4472 : : }
4473 : :
4474 : : // 3.27.12:
4475 : 8 : auto locations
4476 : 8 : = EXPECT_JSON_OBJECT_WITH_ARRAY_PROPERTY (result, "locations");
4477 : 8 : ASSERT_EQ (locations->size (), 0);
4478 : : }
4479 : : }
4480 : 8 : }
4481 : :
4482 : : /* As above, but with a "real" location_t. */
4483 : :
4484 : : static void
4485 : 192 : test_simple_log_2 (const sarif_generation_options &sarif_gen_opts,
4486 : : const line_table_case &case_)
4487 : : {
4488 : 192 : auto_fix_quotes fix_quotes;
4489 : :
4490 : 192 : const char *const content
4491 : : /* 000000000111111
4492 : : 123456789012345. */
4493 : : = "unsinged int i;\n";
4494 : 192 : diagnostic_show_locus_fixture f (case_, content);
4495 : 192 : location_t line_end = linemap_position_for_column (line_table, 31);
4496 : :
4497 : : /* Don't attempt to run the tests if column data might be unavailable. */
4498 : 192 : if (line_end > LINE_MAP_MAX_LOCATION_WITH_COLS)
4499 : 64 : return;
4500 : :
4501 : 128 : test_sarif_diagnostic_context dc (f.get_filename (), sarif_gen_opts);
4502 : :
4503 : 128 : const location_t typo_loc
4504 : 128 : = make_location (linemap_position_for_column (line_table, 1),
4505 : : linemap_position_for_column (line_table, 1),
4506 : : linemap_position_for_column (line_table, 8));
4507 : :
4508 : 128 : rich_location richloc (line_table, typo_loc);
4509 : 128 : dc.report (DK_ERROR, richloc, nullptr, 0,
4510 : : "did you misspell %qs again?",
4511 : : "unsigned");
4512 : :
4513 : 128 : auto log_ptr = dc.flush_to_object ();
4514 : :
4515 : : // 3.13 sarifLog:
4516 : 128 : auto log = log_ptr.get ();
4517 : :
4518 : 128 : auto runs = EXPECT_JSON_OBJECT_WITH_ARRAY_PROPERTY (log, "runs"); // 3.13.4
4519 : 128 : ASSERT_EQ (runs->size (), 1);
4520 : :
4521 : : // 3.14 "run" object:
4522 : 128 : auto run = (*runs)[0];
4523 : :
4524 : 128 : {
4525 : : // 3.14.23:
4526 : 128 : auto results = EXPECT_JSON_OBJECT_WITH_ARRAY_PROPERTY (run, "results");
4527 : 128 : ASSERT_EQ (results->size (), 1);
4528 : :
4529 : 128 : {
4530 : : // 3.27 "result" object:
4531 : 128 : auto result = (*results)[0];
4532 : 128 : ASSERT_JSON_STRING_PROPERTY_EQ (result, "ruleId", "error");
4533 : 128 : ASSERT_JSON_STRING_PROPERTY_EQ (result, "level", "error"); // 3.27.10
4534 : :
4535 : 128 : {
4536 : : // 3.27.11:
4537 : 128 : auto message
4538 : 128 : = EXPECT_JSON_OBJECT_WITH_OBJECT_PROPERTY (result, "message");
4539 : 128 : ASSERT_JSON_STRING_PROPERTY_EQ (message, "text",
4540 : : "did you misspell `unsigned' again?");
4541 : : }
4542 : :
4543 : : // 3.27.12:
4544 : 128 : auto locations
4545 : 128 : = EXPECT_JSON_OBJECT_WITH_ARRAY_PROPERTY (result, "locations");
4546 : 128 : ASSERT_EQ (locations->size (), 1);
4547 : :
4548 : 128 : {
4549 : : // 3.28 "location" object:
4550 : 128 : auto location = (*locations)[0];
4551 : :
4552 : 128 : auto physical_location
4553 : 128 : = EXPECT_JSON_OBJECT_WITH_OBJECT_PROPERTY (location,
4554 : : "physicalLocation");
4555 : 128 : {
4556 : 128 : auto region
4557 : 128 : = EXPECT_JSON_OBJECT_WITH_OBJECT_PROPERTY (physical_location,
4558 : : "region");
4559 : 128 : ASSERT_JSON_INT_PROPERTY_EQ (region, "startLine", 1);
4560 : 128 : ASSERT_JSON_INT_PROPERTY_EQ (region, "startColumn", 1);
4561 : 128 : ASSERT_JSON_INT_PROPERTY_EQ (region, "endColumn", 9);
4562 : : }
4563 : 128 : {
4564 : 128 : auto context_region
4565 : 128 : = EXPECT_JSON_OBJECT_WITH_OBJECT_PROPERTY (physical_location,
4566 : : "contextRegion");
4567 : 128 : ASSERT_JSON_INT_PROPERTY_EQ (context_region, "startLine", 1);
4568 : :
4569 : 128 : {
4570 : 128 : auto snippet
4571 : 128 : = EXPECT_JSON_OBJECT_WITH_OBJECT_PROPERTY (context_region,
4572 : : "snippet");
4573 : :
4574 : : /* We expect the snippet's "text" to be a copy of the content. */
4575 : 128 : ASSERT_JSON_STRING_PROPERTY_EQ (snippet, "text", f.m_content);
4576 : : }
4577 : : }
4578 : : }
4579 : : }
4580 : : }
4581 : 192 : }
4582 : :
4583 : : /* Assuming that a single diagnostic has been emitted within
4584 : : LOG, get a json::object for the result object. */
4585 : :
4586 : : static const json::object *
4587 : 32 : get_result_from_log (const sarif_log *log)
4588 : : {
4589 : 32 : auto runs = EXPECT_JSON_OBJECT_WITH_ARRAY_PROPERTY (log, "runs"); // 3.13.4
4590 : 32 : ASSERT_EQ (runs->size (), 1);
4591 : :
4592 : : // 3.14 "run" object:
4593 : 32 : auto run = (*runs)[0];
4594 : :
4595 : : // 3.14.23:
4596 : 32 : auto results = EXPECT_JSON_OBJECT_WITH_ARRAY_PROPERTY (run, "results");
4597 : 32 : ASSERT_EQ (results->size (), 1);
4598 : :
4599 : : // 3.27 "result" object:
4600 : 32 : auto result = (*results)[0];
4601 : 32 : return expect_json_object (SELFTEST_LOCATION, result);
4602 : : }
4603 : :
4604 : : static const json::object *
4605 : 16 : get_message_from_result (const sarif_result &result)
4606 : : {
4607 : : // 3.27.11:
4608 : 16 : auto message_obj
4609 : 16 : = EXPECT_JSON_OBJECT_WITH_OBJECT_PROPERTY (&result, "message");
4610 : 16 : return message_obj;
4611 : : }
4612 : :
4613 : : /* Assuming that a single diagnostic has been emitted to
4614 : : DC, get a json::object for the messsage object within
4615 : : the result. */
4616 : :
4617 : : static const json::object *
4618 : 32 : get_message_from_log (const sarif_log *log)
4619 : : {
4620 : 32 : auto result_obj = get_result_from_log (log);
4621 : :
4622 : : // 3.27.11:
4623 : 32 : auto message_obj
4624 : 32 : = EXPECT_JSON_OBJECT_WITH_OBJECT_PROPERTY (result_obj, "message");
4625 : 32 : return message_obj;
4626 : : }
4627 : :
4628 : : /* Tests of messages with embedded links; see SARIF v2.1.0 3.11.6. */
4629 : :
4630 : : static void
4631 : 8 : test_message_with_embedded_link (const sarif_generation_options &sarif_gen_opts)
4632 : : {
4633 : 8 : auto_fix_quotes fix_quotes;
4634 : 8 : {
4635 : 8 : test_sarif_diagnostic_context dc ("test.c", sarif_gen_opts);
4636 : 8 : rich_location richloc (line_table, UNKNOWN_LOCATION);
4637 : 8 : dc.report (DK_ERROR, richloc, nullptr, 0,
4638 : : "before %{text%} after",
4639 : : "http://example.com");
4640 : 8 : std::unique_ptr<sarif_log> log = dc.flush_to_object ();
4641 : :
4642 : 8 : auto message_obj = get_message_from_log (log.get ());
4643 : 8 : ASSERT_JSON_STRING_PROPERTY_EQ
4644 : : (message_obj, "text",
4645 : : "before [text](http://example.com) after");
4646 : 8 : }
4647 : :
4648 : : /* Escaping in message text.
4649 : : This is "EXAMPLE 1" from 3.11.6. */
4650 : 8 : {
4651 : 8 : test_sarif_diagnostic_context dc ("test.c", sarif_gen_opts);
4652 : 8 : rich_location richloc (line_table, UNKNOWN_LOCATION);
4653 : :
4654 : : /* Disable "unquoted sequence of 2 consecutive punctuation
4655 : : characters `]\' in format" warning. */
4656 : : #if __GNUC__ >= 10
4657 : 8 : # pragma GCC diagnostic push
4658 : 8 : # pragma GCC diagnostic ignored "-Wformat-diag"
4659 : : #endif
4660 : 8 : dc.report (DK_ERROR, richloc, nullptr, 0,
4661 : : "Prohibited term used in %{para[0]\\spans[2]%}.",
4662 : : "1");
4663 : : #if __GNUC__ >= 10
4664 : 8 : # pragma GCC diagnostic pop
4665 : : #endif
4666 : :
4667 : 8 : std::unique_ptr<sarif_log> log = dc.flush_to_object ();
4668 : :
4669 : 8 : auto message_obj = get_message_from_log (log.get ());
4670 : 8 : ASSERT_JSON_STRING_PROPERTY_EQ
4671 : : (message_obj, "text",
4672 : : "Prohibited term used in [para\\[0\\]\\\\spans\\[2\\]](1).");
4673 : : /* This isn't exactly what EXAMPLE 1 of the spec has; reported as
4674 : : https://github.com/oasis-tcs/sarif-spec/issues/656 */
4675 : 8 : }
4676 : :
4677 : : /* Urlifier. */
4678 : 8 : {
4679 : 8 : class test_urlifier : public urlifier
4680 : : {
4681 : : public:
4682 : : char *
4683 : 16 : get_url_for_quoted_text (const char *p, size_t sz) const final override
4684 : : {
4685 : 16 : if (!strncmp (p, "-foption", sz))
4686 : 8 : return xstrdup ("http://example.com");
4687 : : return nullptr;
4688 : : }
4689 : : };
4690 : :
4691 : 8 : test_sarif_diagnostic_context dc ("test.c", sarif_gen_opts);
4692 : 8 : dc.push_owned_urlifier (std::make_unique<test_urlifier> ());
4693 : 8 : rich_location richloc (line_table, UNKNOWN_LOCATION);
4694 : 8 : dc.report (DK_ERROR, richloc, nullptr, 0,
4695 : : "foo %<-foption%> %<unrecognized%> bar");
4696 : 8 : std::unique_ptr<sarif_log> log = dc.flush_to_object ();
4697 : :
4698 : 8 : auto message_obj = get_message_from_log (log.get ());
4699 : 8 : ASSERT_JSON_STRING_PROPERTY_EQ
4700 : : (message_obj, "text",
4701 : : "foo `[-foption](http://example.com)' `unrecognized' bar");
4702 : 8 : }
4703 : 8 : }
4704 : :
4705 : : /* Verify that braces in messages get escaped, as per
4706 : : 3.11.5 ("Messages with placeholders"). */
4707 : :
4708 : : static void
4709 : 8 : test_message_with_braces (const sarif_generation_options &sarif_gen_opts)
4710 : : {
4711 : 8 : auto_fix_quotes fix_quotes;
4712 : 8 : {
4713 : 8 : test_sarif_diagnostic_context dc ("test.c", sarif_gen_opts);
4714 : 8 : rich_location richloc (line_table, UNKNOWN_LOCATION);
4715 : 8 : dc.report (DK_ERROR, richloc, nullptr, 0,
4716 : : "open brace: %qs close brace: %qs",
4717 : : "{", "}");
4718 : 8 : std::unique_ptr<sarif_log> log = dc.flush_to_object ();
4719 : :
4720 : 8 : auto message_obj = get_message_from_log (log.get ());
4721 : 8 : ASSERT_JSON_STRING_PROPERTY_EQ
4722 : : (message_obj, "text",
4723 : : "open brace: `{{' close brace: `}}'");
4724 : 8 : }
4725 : 8 : }
4726 : :
4727 : : static void
4728 : 8 : test_buffering (const sarif_generation_options &sarif_gen_opts)
4729 : : {
4730 : 8 : test_sarif_diagnostic_context dc ("test.c", sarif_gen_opts);
4731 : :
4732 : 8 : diagnostic_buffer buf_a (dc);
4733 : 8 : diagnostic_buffer buf_b (dc);
4734 : :
4735 : 8 : rich_location rich_loc (line_table, UNKNOWN_LOCATION);
4736 : :
4737 : 8 : ASSERT_EQ (dc.diagnostic_count (DK_ERROR), 0);
4738 : 8 : ASSERT_EQ (buf_a.diagnostic_count (DK_ERROR), 0);
4739 : 8 : ASSERT_EQ (buf_b.diagnostic_count (DK_ERROR), 0);
4740 : 8 : ASSERT_EQ (dc.num_results (), 0);
4741 : 8 : ASSERT_TRUE (buf_a.empty_p ());
4742 : 8 : ASSERT_TRUE (buf_b.empty_p ());
4743 : :
4744 : : /* Unbuffered diagnostic. */
4745 : 8 : {
4746 : 8 : dc.report (DK_ERROR, rich_loc, nullptr, 0,
4747 : : "message 1");
4748 : :
4749 : 8 : ASSERT_EQ (dc.diagnostic_count (DK_ERROR), 1);
4750 : 8 : ASSERT_EQ (buf_a.diagnostic_count (DK_ERROR), 0);
4751 : 8 : ASSERT_EQ (buf_b.diagnostic_count (DK_ERROR), 0);
4752 : 8 : ASSERT_EQ (dc.num_results (), 1);
4753 : 8 : sarif_result &result_obj = dc.get_result (0);
4754 : 8 : auto message_obj = get_message_from_result (result_obj);
4755 : 8 : ASSERT_JSON_STRING_PROPERTY_EQ (message_obj, "text",
4756 : : "message 1");
4757 : 8 : ASSERT_TRUE (buf_a.empty_p ());
4758 : 8 : ASSERT_TRUE (buf_b.empty_p ());
4759 : : }
4760 : :
4761 : : /* Buffer diagnostic into buffer A. */
4762 : 8 : {
4763 : 8 : dc.set_diagnostic_buffer (&buf_a);
4764 : 8 : dc.report (DK_ERROR, rich_loc, nullptr, 0,
4765 : : "message in buffer a");
4766 : 8 : ASSERT_EQ (dc.diagnostic_count (DK_ERROR), 1);
4767 : 8 : ASSERT_EQ (buf_a.diagnostic_count (DK_ERROR), 1);
4768 : 8 : ASSERT_EQ (buf_b.diagnostic_count (DK_ERROR), 0);
4769 : 8 : ASSERT_EQ (dc.num_results (), 1);
4770 : 8 : ASSERT_FALSE (buf_a.empty_p ());
4771 : 8 : ASSERT_TRUE (buf_b.empty_p ());
4772 : : }
4773 : :
4774 : : /* Buffer diagnostic into buffer B. */
4775 : 8 : {
4776 : 8 : dc.set_diagnostic_buffer (&buf_b);
4777 : 8 : dc.report (DK_ERROR, rich_loc, nullptr, 0,
4778 : : "message in buffer b");
4779 : 8 : ASSERT_EQ (dc.diagnostic_count (DK_ERROR), 1);
4780 : 8 : ASSERT_EQ (buf_a.diagnostic_count (DK_ERROR), 1);
4781 : 8 : ASSERT_EQ (buf_b.diagnostic_count (DK_ERROR), 1);
4782 : 8 : ASSERT_EQ (dc.num_results (), 1);
4783 : 8 : ASSERT_FALSE (buf_a.empty_p ());
4784 : 8 : ASSERT_FALSE (buf_b.empty_p ());
4785 : : }
4786 : :
4787 : : /* Flush buffer B to dc. */
4788 : 8 : {
4789 : 8 : dc.flush_diagnostic_buffer (buf_b);
4790 : 8 : ASSERT_EQ (dc.diagnostic_count (DK_ERROR), 2);
4791 : 8 : ASSERT_EQ (buf_a.diagnostic_count (DK_ERROR), 1);
4792 : 8 : ASSERT_EQ (buf_b.diagnostic_count (DK_ERROR), 0);
4793 : 8 : ASSERT_EQ (dc.num_results (), 2);
4794 : 8 : sarif_result &result_1_obj = dc.get_result (1);
4795 : 8 : auto message_1_obj = get_message_from_result (result_1_obj);
4796 : 8 : ASSERT_JSON_STRING_PROPERTY_EQ (message_1_obj, "text",
4797 : : "message in buffer b");
4798 : 8 : ASSERT_FALSE (buf_a.empty_p ());
4799 : 8 : ASSERT_TRUE (buf_b.empty_p ());
4800 : : }
4801 : :
4802 : : /* Clear buffer A. */
4803 : 8 : {
4804 : 8 : dc.clear_diagnostic_buffer (buf_a);
4805 : 8 : ASSERT_EQ (dc.diagnostic_count (DK_ERROR), 2);
4806 : 8 : ASSERT_EQ (buf_a.diagnostic_count (DK_ERROR), 0);
4807 : 8 : ASSERT_EQ (buf_b.diagnostic_count (DK_ERROR), 0);
4808 : 8 : ASSERT_EQ (dc.num_results (), 2);
4809 : 8 : ASSERT_TRUE (buf_a.empty_p ());
4810 : 8 : ASSERT_TRUE (buf_b.empty_p ());
4811 : : }
4812 : 8 : }
4813 : :
4814 : : template <class ...ArgTypes>
4815 : : static void
4816 : 208 : for_each_sarif_gen_option (void (*callback) (const sarif_generation_options &,
4817 : : ArgTypes ...),
4818 : : ArgTypes ...args)
4819 : : {
4820 : 208 : sarif_generation_options sarif_gen_opts;
4821 : 624 : for (int version_idx = 0;
4822 : 624 : version_idx < (int)sarif_version::num_versions;
4823 : : ++version_idx)
4824 : : {
4825 : 416 : sarif_gen_opts.m_version = static_cast<enum sarif_version> (version_idx);
4826 : :
4827 : 416 : callback (sarif_gen_opts, args...);
4828 : : }
4829 : 208 : }
4830 : :
4831 : : static void
4832 : 96 : run_line_table_case_tests_per_version (const line_table_case &case_)
4833 : : {
4834 : 96 : for_each_sarif_gen_option<const line_table_case &>
4835 : 96 : (test_make_location_object, case_);
4836 : :
4837 : 96 : for_each_sarif_gen_option<const line_table_case &>
4838 : 96 : (test_simple_log_2, case_);
4839 : 96 : }
4840 : :
4841 : : /* Run all of the selftests within this file. */
4842 : :
4843 : : void
4844 : 4 : diagnostic_format_sarif_cc_tests ()
4845 : : {
4846 : 4 : test_sarif_array_of_unique_1 ();
4847 : 4 : test_sarif_array_of_unique_2 ();
4848 : :
4849 : 4 : for_each_sarif_gen_option (test_simple_log);
4850 : 4 : for_each_sarif_gen_option (test_message_with_embedded_link);
4851 : 4 : for_each_sarif_gen_option (test_message_with_braces);
4852 : 4 : for_each_sarif_gen_option (test_buffering);
4853 : :
4854 : : /* Run tests per (SARIF gen-option, line-table-case) pair. */
4855 : 4 : for_each_line_table_case (run_line_table_case_tests_per_version);
4856 : 4 : }
4857 : :
4858 : : } // namespace selftest
4859 : :
4860 : : #endif /* CHECKING_P */
|