Debian Codegraph @

gcc/libgdiagnostics.cc

raw
1 /* C++ implementation of a pure C API for emitting diagnostics.
2 Copyright (C) 2023-2025 Free Software Foundation, Inc.
3
4 This file is part of GCC.
5
6 GCC is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 3, or (at your option) any later
9 version.
10
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
19
20 #include "config.h"
21 #define INCLUDE_MAP
22 #define INCLUDE_VECTOR
23 #include "system.h"
24 #include "coretypes.h"
25 #include "intl.h"
26 #include "diagnostic.h"
27 #include "diagnostic-color.h"
28 #include "diagnostic-url.h"
29 #include "diagnostic-metadata.h"
30 #include "diagnostic-path.h"
31 #include "diagnostic-client-data-hooks.h"
32 #include "diagnostic-format-sarif.h"
33 #include "diagnostic-format-text.h"
34 #include "logical-location.h"
35 #include "edit-context.h"
36 #include "make-unique.h"
37 #include "libgdiagnostics.h"
38
39 class owned_nullable_string
40 {
41 public:
42 owned_nullable_string () : m_str (nullptr) {}
43 owned_nullable_string (const char *str)
44 : m_str (str ? ::xstrdup (str) : nullptr)
45 {
46 }
47 owned_nullable_string (const owned_nullable_string &other)
48 : m_str (other.xstrdup ())
49 {
50 }
51 owned_nullable_string (owned_nullable_string &&other)
52 {
53 m_str = other.m_str;
54 other.m_str = nullptr;
55 }
56
57 ~owned_nullable_string ()
58 {
59 free (m_str);
60 }
61
62 void set (const char *str)
63 {
64 free (m_str);
65 m_str = str ? ::xstrdup (str) : nullptr;
66 }
67
68 const char *get_str () const { return m_str; }
69
70 char *xstrdup () const
71 {
72 return m_str ? ::xstrdup (m_str) : nullptr;
73 }
74
75 bool
76 operator< (const owned_nullable_string &other) const
77 {
78 if (m_str && other.m_str)
79 return strcmp (m_str, other.m_str) < 0;
80 if (m_str == nullptr && other.m_str != nullptr)
81 return true;
82 return false;
83 }
84
85 private:
86 char *m_str;
87 };
88
89 class content_buffer
90 {
91 public:
92 content_buffer (const char *data, size_t sz)
93 : m_data (xmalloc (sz)),
94 m_sz (sz)
95 {
96 memcpy (m_data, data, sz);
97 }
98 ~content_buffer ()
99 {
100 free (m_data);
101 }
102
103 void *m_data;
104 size_t m_sz;
105 };
106
107 /* This has to be a "struct" as it is exposed in the C API. */
108
109 struct diagnostic_file
110 {
111 diagnostic_file (diagnostic_manager &mgr,
112 const char *name,
113 const char *sarif_source_language)
114 : m_mgr (mgr),
115 m_name (name),
116 m_sarif_source_language (sarif_source_language)
117 {
118 }
119
120 const char *get_name () const { return m_name.get_str (); }
121 const char *get_sarif_source_language () const
122 {
123 return m_sarif_source_language.get_str ();
124 }
125
126 const content_buffer *
127 get_content () const
128 {
129 return m_content.get ();
130 }
131 void set_buffered_content (const char *buf, size_t sz);
132
133 private:
134 diagnostic_manager &m_mgr;
135 owned_nullable_string m_name;
136 owned_nullable_string m_sarif_source_language;
137 std::unique_ptr<content_buffer> m_content;
138 };
139
140 /* This has to be a "struct" as it is exposed in the C API. */
141
142 struct diagnostic_physical_location
143 {
144 diagnostic_physical_location (diagnostic_manager *mgr,
145 location_t inner)
146 : m_mgr (mgr),
147 m_inner (inner)
148 {}
149
150 diagnostic_file *get_file () const;
151
152 diagnostic_manager *m_mgr;
153 location_t m_inner;
154 };
155
156 static location_t
157 as_location_t (const diagnostic_physical_location *loc)
158 {
159 if (!loc)
160 return UNKNOWN_LOCATION;
161 return loc->m_inner;
162 }
163
164 /* This has to be a "struct" as it is exposed in the C API. */
165
166 struct diagnostic_logical_location : public logical_location
167 {
168 diagnostic_logical_location (enum diagnostic_logical_location_kind_t kind,
169 const diagnostic_logical_location *parent,
170 const char *short_name,
171 const char *fully_qualified_name,
172 const char *decorated_name)
173 : m_kind (kind),
174 m_parent (parent),
175 m_short_name (short_name),
176 m_fully_qualified_name (fully_qualified_name),
177 m_decorated_name (decorated_name)
178 {
179 }
180
181 const char *get_short_name () const final override
182 {
183 return m_short_name.get_str ();
184 }
185 const char *get_name_with_scope () const final override
186 {
187 return m_fully_qualified_name.get_str ();
188 }
189 const char *get_internal_name () const final override
190 {
191 return m_decorated_name.get_str ();
192 }
193 enum logical_location_kind get_kind () const final override
194 {
195 switch (m_kind)
196 {
197 default:
198 gcc_unreachable ();
199 case DIAGNOSTIC_LOGICAL_LOCATION_KIND_FUNCTION:
200 return LOGICAL_LOCATION_KIND_FUNCTION;
201 case DIAGNOSTIC_LOGICAL_LOCATION_KIND_MEMBER:
202 return LOGICAL_LOCATION_KIND_MEMBER;
203 case DIAGNOSTIC_LOGICAL_LOCATION_KIND_MODULE:
204 return LOGICAL_LOCATION_KIND_MODULE;
205 case DIAGNOSTIC_LOGICAL_LOCATION_KIND_NAMESPACE:
206 return LOGICAL_LOCATION_KIND_NAMESPACE;
207 case DIAGNOSTIC_LOGICAL_LOCATION_KIND_TYPE:
208 return LOGICAL_LOCATION_KIND_TYPE;
209 case DIAGNOSTIC_LOGICAL_LOCATION_KIND_RETURN_TYPE:
210 return LOGICAL_LOCATION_KIND_RETURN_TYPE;
211 case DIAGNOSTIC_LOGICAL_LOCATION_KIND_PARAMETER:
212 return LOGICAL_LOCATION_KIND_PARAMETER;
213 case DIAGNOSTIC_LOGICAL_LOCATION_KIND_VARIABLE:
214 return LOGICAL_LOCATION_KIND_VARIABLE;
215 }
216 }
217
218 enum diagnostic_logical_location_kind_t get_external_kind () const
219 {
220 return m_kind;
221 }
222
223 const diagnostic_logical_location *get_parent () const { return m_parent; }
224
225 label_text get_name_for_path_output () const
226 {
227 return label_text::borrow (m_short_name.get_str ());
228 }
229
230 bool
231 operator< (const diagnostic_logical_location &other) const
232 {
233 if (m_kind < other.m_kind)
234 return true;
235 if (m_parent < other.m_parent)
236 return true;
237 if (m_short_name < other.m_short_name)
238 return true;
239 if (m_fully_qualified_name < other.m_fully_qualified_name)
240 return true;
241 if (m_decorated_name < other.m_decorated_name)
242 return true;
243
244 return false;
245 }
246
247 private:
248 enum diagnostic_logical_location_kind_t m_kind;
249 const diagnostic_logical_location *m_parent;
250 owned_nullable_string m_short_name;
251 owned_nullable_string m_fully_qualified_name;
252 owned_nullable_string m_decorated_name;
253 };
254
255 static diagnostic_event_id
256 as_diagnostic_event_id (diagnostic_event_id_t id)
257 {
258 return id.zero_based ();
259 }
260
261 class sink
262 {
263 protected:
264 sink (diagnostic_manager &mgr) : m_mgr (mgr) {}
265
266 diagnostic_manager &m_mgr;
267 };
268
269 /* This has to be a "struct" as it is exposed in the C API. */
270
271 struct diagnostic_text_sink : public sink
272 {
273 public:
274 diagnostic_text_sink (diagnostic_manager &mgr,
275 FILE *dst_stream,
276 enum diagnostic_colorize colorize);
277
278 diagnostic_source_printing_options &get_source_printing_options ()
279 {
280 return m_source_printing;
281 }
282
283 void
284 set_colorize (enum diagnostic_colorize colorize);
285
286 static void
287 text_starter (diagnostic_text_output_format &text_output,
288 const diagnostic_info *diagnostic);
289
290 private:
291 diagnostic_text_output_format *m_inner_sink; // borrowed from dc
292 diagnostic_source_printing_options m_source_printing;
293 };
294
295 class sarif_sink : public sink
296 {
297 public:
298 sarif_sink (diagnostic_manager &mgr,
299 FILE *dst_stream,
300 const diagnostic_file *main_input_file,
301 enum sarif_version version);
302 };
303
304 /* Helper for the linemap code. */
305
306 static size_t
307 round_alloc_size (size_t s)
308 {
309 return s;
310 }
311
312 class impl_diagnostic_client_data_hooks : public diagnostic_client_data_hooks
313 {
314 public:
315 impl_diagnostic_client_data_hooks (diagnostic_manager &mgr)
316 : m_mgr (mgr)
317 {}
318
319 const client_version_info *get_any_version_info () const final override;
320 const logical_location *get_current_logical_location () const final override;
321 const char * maybe_get_sarif_source_language (const char *filename)
322 const final override;
323 void add_sarif_invocation_properties (sarif_object &invocation_obj)
324 const final override;
325
326 private:
327 diagnostic_manager &m_mgr;
328 };
329
330 class impl_client_version_info : public client_version_info
331 {
332 public:
333 const char *get_tool_name () const final override
334 {
335 return m_name.get_str ();
336 }
337
338 char *maybe_make_full_name () const final override
339 {
340 return m_full_name.xstrdup ();
341 }
342
343 const char *get_version_string () const final override
344 {
345 return m_version.get_str ();
346 }
347
348 char *maybe_make_version_url () const final override
349 {
350 return m_version_url.xstrdup ();
351 }
352
353 void for_each_plugin (plugin_visitor &) const final override
354 {
355 // No-op.
356 }
357
358 owned_nullable_string m_name;
359 owned_nullable_string m_full_name;
360 owned_nullable_string m_version;
361 owned_nullable_string m_version_url;
362 };
363
364 /* This has to be a "struct" as it is exposed in the C API. */
365
366 struct diagnostic_manager
367 {
368 public:
369 diagnostic_manager ()
370 : m_current_diag (nullptr),
371 m_prev_diag_logical_loc (nullptr)
372 {
373 linemap_init (&m_line_table, BUILTINS_LOCATION);
374 m_line_table.m_reallocator = xrealloc;
375 m_line_table.m_round_alloc_size = round_alloc_size;
376 m_line_table.default_range_bits = line_map_suggested_range_bits;
377
378 diagnostic_initialize (&m_dc, 0);
379 m_dc.remove_all_output_sinks ();
380
381 /* Get defaults from environemt. These might be
382 overridden by individual sinks. */
383 diagnostic_color_init (&m_dc, DIAGNOSTICS_COLOR_AUTO);
384 diagnostic_urls_init (&m_dc);
385
386 m_dc.set_show_cwe (true);
387 m_dc.set_show_rules (true);
388 m_dc.m_show_column = true;
389 m_dc.m_source_printing.enabled = true;
390 m_dc.m_source_printing.colorize_source_p = true;
391
392 /* We don't currently expose a way for clients to manipulate the
393 following. */
394 m_dc.m_source_printing.show_labels_p = true;
395 m_dc.m_source_printing.show_line_numbers_p = true;
396 m_dc.m_source_printing.min_margin_width = 6;
397 m_dc.set_path_format (DPF_INLINE_EVENTS);
398
399 m_dc.m_client_aux_data = this;
400 m_dc.set_client_data_hooks
401 (::make_unique<impl_diagnostic_client_data_hooks> (*this));
402
403 diagnostic_text_starter (&m_dc) = diagnostic_text_sink::text_starter;
404
405 m_edit_context = ::make_unique <edit_context> (m_dc.get_file_cache ());
406 }
407
408 ~diagnostic_manager ()
409 {
410 diagnostic_finish (&m_dc);
411
412 for (size_t i = 0; i < m_sinks.size (); i++)
413 m_sinks[i] = nullptr;
414
415 for (auto iter : m_str_to_file_map)
416 delete iter.second;
417
418 for (auto iter :m_location_t_map)
419 delete iter.second;
420
421 free (m_line_table.m_location_adhoc_data_map.data);
422 free (m_line_table.info_ordinary.maps);
423 }
424
425 line_maps *get_line_table () { return &m_line_table; }
426 diagnostic_context &get_dc () { return m_dc; }
427
428 void write_patch (FILE *dst_stream);
429
430 void add_sink (std::unique_ptr<sink> sink)
431 {
432 m_sinks.push_back (std::move (sink));
433 }
434
435 void emit (diagnostic &diag, const char *msgid, va_list *args)
436 LIBGDIAGNOSTICS_PARAM_GCC_FORMAT_STRING(3, 0);
437
438 diagnostic_file *
439 new_file (const char *name,
440 const char *sarif_source_language)
441 {
442 if (diagnostic_file **slot = m_str_to_file_map.get (name))
443 return *slot;
444 diagnostic_file *file
445 = new diagnostic_file (*this, name, sarif_source_language);
446 m_str_to_file_map.put (file->get_name (), file);
447 return file;
448 }
449
450 diagnostic_file *
451 get_file_by_name (const char *name)
452 {
453 if (diagnostic_file **slot = m_str_to_file_map.get (name))
454 return *slot;
455 return nullptr;
456 }
457
458 const diagnostic_physical_location *
459 new_location_from_file_and_line (const diagnostic_file *file,
460 diagnostic_line_num_t line_num)
461 {
462 ensure_linemap_for_file_and_line (file, line_num);
463 location_t loc = linemap_position_for_column (&m_line_table, 0);
464 return new_location (loc);
465 }
466
467 const diagnostic_physical_location *
468 new_location_from_file_line_column (const diagnostic_file *file,
469 diagnostic_line_num_t line_num,
470 diagnostic_column_num_t column_num)
471 {
472 ensure_linemap_for_file_and_line (file, line_num);
473 location_t loc = linemap_position_for_column (&m_line_table, column_num);
474 return new_location (loc);
475 }
476
477 const diagnostic_physical_location *
478 new_location_from_range (const diagnostic_physical_location *loc_caret,
479 const diagnostic_physical_location *loc_start,
480 const diagnostic_physical_location *loc_end)
481 {
482 return new_location
483 (m_line_table.make_location (as_location_t (loc_caret),
484 as_location_t (loc_start),
485 as_location_t (loc_end)));
486 }
487
488 const diagnostic_logical_location *
489 new_logical_location (enum diagnostic_logical_location_kind_t kind,
490 const diagnostic_logical_location *parent,
491 const char *short_name,
492 const char *fully_qualified_name,
493 const char *decorated_name)
494 {
495 /* Use m_logical_locs to "uniquify" instances. */
496 diagnostic_logical_location key (kind,
497 parent,
498 short_name,
499 fully_qualified_name,
500 decorated_name);
501 auto iter = m_logical_locs.find (key);
502 if (iter != m_logical_locs.end ())
503 return (*iter).second.get ();
504
505 std::unique_ptr<diagnostic_logical_location> logical_loc
506 = ::make_unique<diagnostic_logical_location> (kind,
507 parent,
508 short_name,
509 fully_qualified_name,
510 decorated_name);
511 const diagnostic_logical_location *result = logical_loc.get ();
512 m_logical_locs.insert
513 (logical_locs_map_t::value_type (std::move (key),
514 std::move (logical_loc)));
515 return result;
516 }
517
518 diagnostic_execution_path *
519 new_execution_path ();
520
521 void begin_group ()
522 {
523 m_dc.begin_group ();
524 }
525
526 void end_group ()
527 {
528 m_dc.end_group ();
529 }
530
531 const char *
532 maybe_get_sarif_source_language (const char *filename)
533 {
534 if (diagnostic_file **slot = m_str_to_file_map.get (filename))
535 {
536 gcc_assert (*slot);
537 return (*slot)->get_sarif_source_language ();
538 }
539 return nullptr;
540 }
541
542 const diagnostic *get_current_diag () { return m_current_diag; }
543
544 const client_version_info *get_client_version_info () const
545 {
546 return &m_client_version_info;
547 }
548 impl_client_version_info *get_client_version_info ()
549 {
550 return &m_client_version_info;
551 }
552
553 void
554 assert_valid_diagnostic_physical_location (const diagnostic_physical_location *loc) const
555 {
556 if (!loc)
557 return;
558 gcc_assert (loc->m_mgr == this);
559 }
560
561 /* TODO: Various things still use the "line_table" global variable.
562 Set it to be this diagnostic_manager's m_line_table.
563 Ideally we should eliminate this global (and this function). */
564 void set_line_table_global () const
565 {
566 line_table = const_cast<line_maps *> (&m_line_table);
567 }
568
569 const diagnostic_logical_location *
570 get_prev_diag_logical_loc () const
571 {
572 return m_prev_diag_logical_loc;
573 }
574
575 private:
576 void
577 ensure_linemap_for_file_and_line (const diagnostic_file *file,
578 diagnostic_line_num_t linenum)
579 {
580 /* Build a simple linemap describing some locations. */
581 if (LINEMAPS_ORDINARY_USED (&m_line_table) == 0)
582 linemap_add (&m_line_table, LC_ENTER, false, file->get_name (), 0);
583 else
584 {
585 line_map *map
586 = const_cast<line_map *>
587 (linemap_add (&m_line_table, LC_RENAME_VERBATIM, false,
588 file->get_name (), 0));
589 ((line_map_ordinary *)map)->included_from = UNKNOWN_LOCATION;
590 }
591 linemap_line_start (&m_line_table, linenum, 100);
592 }
593
594 const diagnostic_physical_location *
595 new_location (location_t loc)
596 {
597 if (loc == UNKNOWN_LOCATION)
598 return nullptr;
599 if (diagnostic_physical_location **slot = m_location_t_map.get (loc))
600 return *slot;
601 diagnostic_physical_location *phys_loc
602 = new diagnostic_physical_location (this, loc);
603 m_location_t_map.put (loc, phys_loc);
604 return phys_loc;
605 }
606
607 diagnostic_context m_dc;
608 line_maps m_line_table;
609 impl_client_version_info m_client_version_info;
610 std::vector<std::unique_ptr<sink>> m_sinks;
611 hash_map<nofree_string_hash, diagnostic_file *> m_str_to_file_map;
612 hash_map<int_hash<location_t, UNKNOWN_LOCATION, location_t (-1)>,
613 diagnostic_physical_location *> m_location_t_map;
614 typedef std::map<diagnostic_logical_location,
615 std::unique_ptr<diagnostic_logical_location>> logical_locs_map_t;
616 logical_locs_map_t m_logical_locs;
617 const diagnostic *m_current_diag;
618 const diagnostic_logical_location *m_prev_diag_logical_loc;
619 std::unique_ptr<edit_context> m_edit_context;
620 };
621
622 class impl_rich_location : public rich_location
623 {
624 public:
625 impl_rich_location (line_maps *set)
626 : rich_location (set, UNKNOWN_LOCATION)
627 {}
628 };
629
630 class impl_range_label : public range_label
631 {
632 public:
633 impl_range_label (const char *text)
634 : m_text (xstrdup (text))
635 {}
636
637 ~impl_range_label () { free (m_text); }
638
639 label_text get_text (unsigned) const final override
640 {
641 return label_text::borrow (m_text);
642 }
643
644 private:
645 char *m_text;
646 };
647
648 class impl_rule : public diagnostic_metadata::rule
649 {
650 public:
651 impl_rule (const char *title, const char *url)
652 : m_title (title),
653 m_url (url)
654 {
655 }
656
657 virtual ~impl_rule () {}
658
659 char *make_description () const final override
660 {
661 return m_title.xstrdup ();
662 }
663
664 char *make_url () const final override
665 {
666 return m_url.xstrdup ();
667 }
668
669 private:
670 owned_nullable_string m_title;
671 owned_nullable_string m_url;
672 };
673
674 class libgdiagnostics_path_event : public diagnostic_event
675 {
676 public:
677 libgdiagnostics_path_event (const diagnostic_physical_location *physical_loc,
678 const diagnostic_logical_location *logical_loc,
679 unsigned stack_depth,
680 const char *gmsgid,
681 va_list *args)
682 : m_physical_loc (physical_loc),
683 m_logical_loc (logical_loc),
684 m_stack_depth (stack_depth)
685 {
686 m_desc_uncolored = make_desc (gmsgid, args, false);
687 m_desc_colored = make_desc (gmsgid, args, true);
688 }
689
690 /* diagnostic_event vfunc implementations. */
691
692 location_t get_location () const final override
693 {
694 return as_location_t (m_physical_loc);
695 }
696
697 int get_stack_depth () const final override
698 {
699 return m_stack_depth;
700 }
701
702 void print_desc (pretty_printer &pp) const final override
703 {
704 if (pp_show_color (&pp))
705 pp_string (&pp, m_desc_colored.get ());
706 else
707 pp_string (&pp, m_desc_uncolored.get ());
708 }
709
710 const logical_location *get_logical_location () const
711 {
712 return m_logical_loc;
713 }
714
715 meaning get_meaning () const final override
716 {
717 return meaning ();
718 }
719
720 bool connect_to_next_event_p () const final override
721 {
722 return false; // TODO
723 }
724
725 diagnostic_thread_id_t get_thread_id () const final override
726 {
727 return 0;
728 }
729
730 private:
731 static label_text make_desc (const char *gmsgid,
732 va_list *args,
733 bool colorize)
734 {
735 va_list copy_of_args;
736 va_copy (copy_of_args, *args);
737
738 // TODO: when should localization happen?
739 text_info text (gmsgid, &copy_of_args, errno);
740 pretty_printer pp;
741 pp_show_color (&pp) = colorize;
742 pp.set_output_stream (nullptr);
743 pp_format (&pp, &text);
744 pp_output_formatted_text (&pp, nullptr);
745 label_text result = label_text::take (xstrdup (pp_formatted_text (&pp)));
746
747 va_end (copy_of_args);
748
749 return result;
750 }
751
752 const diagnostic_physical_location *m_physical_loc;
753 const diagnostic_logical_location *m_logical_loc;
754 unsigned m_stack_depth;
755 label_text m_desc_uncolored;
756 label_text m_desc_colored;
757 };
758
759 class libgdiagnostics_path_thread : public diagnostic_thread
760 {
761 public:
762 libgdiagnostics_path_thread (const char *name) : m_name (name) {}
763 label_text get_name (bool) const final override
764 {
765 return label_text::borrow (m_name);
766 }
767
768 private:
769 const char *m_name; // has been i18n-ed and formatted
770 };
771
772 /* This has to be a "struct" as it is exposed in the C API. */
773
774 struct diagnostic_execution_path : public diagnostic_path
775 {
776 diagnostic_execution_path ()
777 : m_thread ("")
778 {
779 }
780
781 diagnostic_event_id_t
782 add_event_va (const diagnostic_physical_location *physical_loc,
783 const diagnostic_logical_location *logical_loc,
784 unsigned stack_depth,
785 const char *gmsgid,
786 va_list *args)
787 {
788 m_events.push_back (::make_unique<libgdiagnostics_path_event> (physical_loc,
789 logical_loc,
790 stack_depth,
791 gmsgid,
792 args));
793 return m_events.size () - 1;
794 }
795
796 /* diagnostic_path vfunc implementations. */
797
798 unsigned num_events () const final override
799 {
800 return m_events.size ();
801 }
802 const diagnostic_event & get_event (int idx) const final override
803 {
804 return *m_events[idx];
805 }
806 unsigned num_threads () const final override { return 1; }
807 const diagnostic_thread &
808 get_thread (diagnostic_thread_id_t) const final override
809 {
810 return m_thread;
811 }
812
813 bool
814 same_function_p (int event_idx_a,
815 int event_idx_b) const final override
816 {
817 const logical_location *logical_loc_a
818 = m_events[event_idx_a]->get_logical_location ();
819 const logical_location *logical_loc_b
820 = m_events[event_idx_b]->get_logical_location ();
821
822 /* Pointer equality, as we uniqify logical location instances. */
823 return logical_loc_a == logical_loc_b;
824 }
825
826 private:
827 libgdiagnostics_path_thread m_thread;
828 std::vector<std::unique_ptr<libgdiagnostics_path_event>> m_events;
829 };
830
831 /* This has to be a "struct" as it is exposed in the C API. */
832
833 struct diagnostic
834 {
835 public:
836 diagnostic (diagnostic_manager &diag_mgr,
837 enum diagnostic_level level)
838 : m_diag_mgr (diag_mgr),
839 m_level (level),
840 m_rich_loc (diag_mgr.get_line_table ()),
841 m_logical_loc (nullptr),
842 m_path (nullptr)
843 {}
844
845 diagnostic_manager &get_manager () const
846 {
847 return m_diag_mgr;
848 }
849
850 enum diagnostic_level get_level () const { return m_level; }
851
852 rich_location *get_rich_location () { return &m_rich_loc; }
853 const diagnostic_metadata *get_metadata () { return &m_metadata; }
854
855 void set_cwe (unsigned cwe_id)
856 {
857 m_metadata.add_cwe (cwe_id);
858 }
859
860 void add_rule (const char *title,
861 const char *url)
862 {
863 std::unique_ptr<impl_rule> rule = ::make_unique<impl_rule> (title, url);
864 m_metadata.add_rule (*rule.get ());
865 m_rules.push_back (std::move (rule));
866 }
867
868 void set_location (const diagnostic_physical_location *loc)
869 {
870 m_rich_loc.set_range (0, as_location_t (loc), SHOW_RANGE_WITH_CARET);
871 }
872
873 void
874 add_location (const diagnostic_physical_location *loc)
875 {
876 m_rich_loc.add_range (as_location_t (loc),
877 SHOW_RANGE_WITHOUT_CARET);
878 }
879
880 void
881 add_location_with_label (const diagnostic_physical_location *loc,
882 const char *text)
883 {
884 std::unique_ptr<range_label> label
885 = ::make_unique <impl_range_label> (text);
886 m_rich_loc.add_range (as_location_t (loc),
887 SHOW_RANGE_WITHOUT_CARET,
888 label.get ());
889 m_labels.push_back (std::move (label));
890 }
891
892 void
893 set_logical_location (const diagnostic_logical_location *logical_loc)
894 {
895 m_logical_loc = logical_loc;
896 }
897 const diagnostic_logical_location *get_logical_location () const
898 {
899 return m_logical_loc;
900 }
901
902 diagnostic_execution_path *
903 add_execution_path ()
904 {
905 m_path = ::make_unique<diagnostic_execution_path> ();
906 m_rich_loc.set_path (m_path.get ());
907 return m_path.get ();
908 }
909
910 void
911 take_execution_path (diagnostic_execution_path *path)
912 {
913 m_path = std::unique_ptr<diagnostic_execution_path> (path);
914 m_rich_loc.set_path (path);
915 }
916
917 private:
918 diagnostic_manager &m_diag_mgr;
919 enum diagnostic_level m_level;
920 impl_rich_location m_rich_loc;
921 const diagnostic_logical_location *m_logical_loc;
922 diagnostic_metadata m_metadata;
923 std::vector<std::unique_ptr<range_label>> m_labels;
924 std::vector<std::unique_ptr<impl_rule>> m_rules;
925 std::unique_ptr<diagnostic_execution_path> m_path;
926 };
927
928 static diagnostic_t
929 diagnostic_t_from_diagnostic_level (enum diagnostic_level level)
930 {
931 switch (level)
932 {
933 default:
934 gcc_unreachable ();
935 case DIAGNOSTIC_LEVEL_ERROR:
936 return DK_ERROR;
937 case DIAGNOSTIC_LEVEL_WARNING:
938 return DK_WARNING;
939 case DIAGNOSTIC_LEVEL_NOTE:
940 return DK_NOTE;
941 case DIAGNOSTIC_LEVEL_SORRY:
942 return DK_SORRY;
943 }
944 }
945
946 void
947 diagnostic_file::set_buffered_content (const char *buf, size_t sz)
948 {
949 m_content = ::make_unique<content_buffer> (buf, sz);
950
951 // Populate file_cache:
952 file_cache &fc = m_mgr.get_dc ().get_file_cache ();
953 fc.add_buffered_content (m_name.get_str (), buf, sz);
954 }
955
956 // struct diagnostic_physical_location
957
958 diagnostic_file *
959 diagnostic_physical_location::get_file () const
960 {
961 m_mgr->set_line_table_global ();
962 const char *filename = LOCATION_FILE (m_inner);
963 if (!filename)
964 return nullptr;
965 return m_mgr->get_file_by_name (filename);
966 }
967
968 /* class impl_diagnostic_client_data_hooks. */
969
970 const client_version_info *
971 impl_diagnostic_client_data_hooks::get_any_version_info () const
972 {
973 return m_mgr.get_client_version_info ();
974 }
975
976 const logical_location *
977 impl_diagnostic_client_data_hooks::get_current_logical_location () const
978 {
979 gcc_assert (m_mgr.get_current_diag ());
980
981 return m_mgr.get_current_diag ()->get_logical_location ();
982 }
983
984 const char *
985 impl_diagnostic_client_data_hooks::
986 maybe_get_sarif_source_language (const char *filename) const
987 {
988 return m_mgr.maybe_get_sarif_source_language (filename);
989 }
990
991 void
992 impl_diagnostic_client_data_hooks::
993 add_sarif_invocation_properties (sarif_object &) const
994 {
995 // No-op.
996 }
997
998 /* struct diagnostic_text_sink : public sink. */
999
1000 diagnostic_text_sink::diagnostic_text_sink (diagnostic_manager &mgr,
1001 FILE *dst_stream,
1002 enum diagnostic_colorize colorize)
1003 : sink (mgr),
1004 m_source_printing (mgr.get_dc ().m_source_printing)
1005 {
1006 auto inner_sink
1007 = ::make_unique<diagnostic_text_output_format> (mgr.get_dc (),
1008 &m_source_printing);
1009 inner_sink->get_printer ()->set_output_stream (dst_stream);
1010 m_inner_sink = inner_sink.get ();
1011 set_colorize (colorize);
1012 mgr.get_dc ().add_sink (std::move (inner_sink));
1013 }
1014
1015 void
1016 diagnostic_text_sink::set_colorize (enum diagnostic_colorize colorize)
1017 {
1018 pretty_printer *const pp = m_inner_sink->get_printer ();
1019 switch (colorize)
1020 {
1021 default:
1022 gcc_unreachable ();
1023 case DIAGNOSTIC_COLORIZE_IF_TTY:
1024 pp_show_color (pp)
1025 = pp_show_color (m_mgr.get_dc ().get_reference_printer ());
1026 break;
1027 case DIAGNOSTIC_COLORIZE_NO:
1028 pp_show_color (pp) = false;
1029 break;
1030 case DIAGNOSTIC_COLORIZE_YES:
1031 pp_show_color (pp) = true;
1032 break;
1033 }
1034 }
1035
1036 void
1037 diagnostic_text_sink::text_starter (diagnostic_text_output_format &text_output,
1038 const diagnostic_info *info)
1039 {
1040 gcc_assert (info->x_data);
1041 const diagnostic &diag = *static_cast<const diagnostic *> (info->x_data);
1042 pretty_printer *pp = text_output.get_printer ();
1043 const diagnostic_logical_location *diag_logical_loc
1044 = diag.get_logical_location ();
1045 diagnostic_manager &mgr = diag.get_manager ();
1046 if (diag_logical_loc && diag_logical_loc != mgr.get_prev_diag_logical_loc ())
1047 {
1048 pp_set_prefix (pp, nullptr);
1049 switch (diag_logical_loc->get_kind ())
1050 {
1051 default:
1052 break;
1053 case LOGICAL_LOCATION_KIND_FUNCTION:
1054 if (const char *name
1055 = diag_logical_loc->get_name_with_scope ())
1056 {
1057 pp_printf (pp, _("In function %qs"), name);
1058 pp_character (pp, ':');
1059 pp_newline (pp);
1060 }
1061 break;
1062 // TODO: handle other cases
1063 }
1064 }
1065 pp_set_prefix (pp,
1066 text_output.build_prefix (*info));
1067 }
1068
1069 /* class sarif_sink : public sink. */
1070
1071 sarif_sink::sarif_sink (diagnostic_manager &mgr,
1072 FILE *dst_stream,
1073 const diagnostic_file *main_input_file,
1074 enum sarif_version version)
1075 : sink (mgr)
1076 {
1077 diagnostic_output_file output_file (dst_stream, false,
1078 label_text::borrow ("sarif_sink"));
1079 auto inner_sink = make_sarif_sink (mgr.get_dc (),
1080 *mgr.get_line_table (),
1081 main_input_file->get_name (),
1082 version,
1083 std::move (output_file));
1084 mgr.get_dc ().add_sink (std::move (inner_sink));
1085 }
1086
1087 /* struct diagnostic_manager. */
1088
1089 void
1090 diagnostic_manager::write_patch (FILE *dst_stream)
1091 {
1092 pretty_printer pp;
1093 pp.set_output_stream (dst_stream);
1094 m_edit_context->print_diff (&pp, true);
1095 pp_flush (&pp);
1096 }
1097
1098 void
1099 diagnostic_manager::emit (diagnostic &diag, const char *msgid, va_list *args)
1100 {
1101 set_line_table_global ();
1102
1103 m_current_diag = &diag;
1104
1105 {
1106 m_dc.begin_group ();
1107
1108 diagnostic_info info;
1109 GCC_DIAGNOSTIC_PUSH_IGNORED(-Wsuggest-attribute=format)
1110 diagnostic_set_info (&info, msgid, args, diag.get_rich_location (),
1111 diagnostic_t_from_diagnostic_level (diag.get_level ()));
1112 GCC_DIAGNOSTIC_POP
1113 info.metadata = diag.get_metadata ();
1114 info.x_data = &diag;
1115 diagnostic_report_diagnostic (&m_dc, &info);
1116
1117 m_dc.end_group ();
1118 }
1119
1120 rich_location *rich_loc = diag.get_rich_location ();
1121 if (rich_loc->fixits_can_be_auto_applied_p ())
1122 m_edit_context->add_fixits (rich_loc);
1123
1124 m_prev_diag_logical_loc = diag.get_logical_location ();
1125 m_current_diag = nullptr;
1126 }
1127
1128 diagnostic_execution_path *
1129 diagnostic_manager::new_execution_path ()
1130 {
1131 return new diagnostic_execution_path ();
1132 }
1133
1134 /* Error-checking at the API boundary. */
1135
1136 #define FAIL_IF_NULL(PTR_ARG) \
1137 do { \
1138 volatile const void *p = (PTR_ARG); \
1139 if (!p) { \
1140 fprintf (stderr, "%s: %s must be non-NULL\n", \
1141 __func__, #PTR_ARG); \
1142 abort (); \
1143 } \
1144 } while (0)
1145
1146 /* Public entrypoints. */
1147
1148 /* Public entrypoint for clients to acquire a diagnostic_manager. */
1149
1150 diagnostic_manager *
1151 diagnostic_manager_new (void)
1152 {
1153 return new diagnostic_manager ();
1154 }
1155
1156 /* Public entrypoint for clients to release a diagnostic_manager. */
1157
1158 void
1159 diagnostic_manager_release (diagnostic_manager *diag_mgr)
1160 {
1161 delete diag_mgr;
1162 }
1163
1164 /* Public entrypoint. */
1165
1166 void
1167 diagnostic_manager_set_tool_name (diagnostic_manager *diag_mgr,
1168 const char *value)
1169 {
1170 FAIL_IF_NULL (diag_mgr);
1171 FAIL_IF_NULL (value);
1172
1173 diag_mgr->get_client_version_info ()->m_name.set (value);
1174 }
1175
1176 /* Public entrypoint. */
1177
1178 void
1179 diagnostic_manager_set_full_name (diagnostic_manager *diag_mgr,
1180 const char *value)
1181 {
1182 FAIL_IF_NULL (diag_mgr);
1183 FAIL_IF_NULL (value);
1184
1185 diag_mgr->get_client_version_info ()->m_full_name.set (value);
1186 }
1187
1188 /* Public entrypoint. */
1189
1190 void
1191 diagnostic_manager_set_version_string (diagnostic_manager *diag_mgr,
1192 const char *value)
1193 {
1194 FAIL_IF_NULL (diag_mgr);
1195 FAIL_IF_NULL (value);
1196
1197 diag_mgr->get_client_version_info ()->m_version.set (value);
1198 }
1199
1200 /* Public entrypoint. */
1201
1202 void
1203 diagnostic_manager_set_version_url (diagnostic_manager *diag_mgr,
1204 const char *value)
1205 {
1206 FAIL_IF_NULL (diag_mgr);
1207 FAIL_IF_NULL (value);
1208
1209 diag_mgr->get_client_version_info ()->m_version_url.set (value);
1210 }
1211
1212 /* Public entrypoint. */
1213
1214 diagnostic_text_sink *
1215 diagnostic_manager_add_text_sink (diagnostic_manager *diag_mgr,
1216 FILE *dst_stream,
1217 enum diagnostic_colorize colorize)
1218 {
1219 FAIL_IF_NULL (diag_mgr);
1220 FAIL_IF_NULL (dst_stream);
1221
1222 diagnostic_text_sink *result
1223 = new diagnostic_text_sink (*diag_mgr, dst_stream, colorize);
1224 diag_mgr->add_sink (std::unique_ptr<sink> (result));
1225 return result;
1226 }
1227
1228 /* Public entrypoint. */
1229
1230 void
1231 diagnostic_text_sink_set_source_printing_enabled (diagnostic_text_sink *text_sink,
1232 int value)
1233 {
1234 FAIL_IF_NULL (text_sink);
1235
1236 text_sink->get_source_printing_options ().enabled = value;
1237 }
1238
1239 /* Public entrypoint. */
1240
1241 void
1242 diagnostic_text_sink_set_colorize (diagnostic_text_sink *text_sink,
1243 enum diagnostic_colorize colorize)
1244 {
1245 FAIL_IF_NULL (text_sink);
1246
1247 text_sink->set_colorize (colorize);
1248 }
1249
1250 /* Public entrypoint. */
1251
1252 void
1253 diagnostic_text_sink_set_labelled_source_colorization_enabled (diagnostic_text_sink *text_sink,
1254 int value)
1255 {
1256 FAIL_IF_NULL (text_sink);
1257
1258 text_sink->get_source_printing_options ().colorize_source_p = value;
1259 }
1260
1261
1262 /* Public entrypoint. */
1263
1264 void
1265 diagnostic_manager_add_sarif_sink (diagnostic_manager *diag_mgr,
1266 FILE *dst_stream,
1267 const diagnostic_file *main_input_file,
1268 enum diagnostic_sarif_version version)
1269 {
1270 FAIL_IF_NULL (diag_mgr);
1271 FAIL_IF_NULL (dst_stream);
1272 FAIL_IF_NULL (main_input_file);
1273
1274 enum sarif_version internal_version;
1275 switch (version)
1276 {
1277 default:
1278 fprintf (stderr, "%s: unrecognized value for version: %i\n",
1279 __func__, (int)version);
1280 abort ();
1281 case DIAGNOSTIC_SARIF_VERSION_2_1_0:
1282 internal_version = sarif_version::v2_1_0;
1283 break;
1284 case DIAGNOSTIC_SARIF_VERSION_2_2_PRERELEASE:
1285 internal_version = sarif_version::v2_2_prerelease_2024_08_08;
1286 break;
1287 }
1288
1289 diag_mgr->add_sink (make_unique<sarif_sink> (*diag_mgr,
1290 dst_stream,
1291 main_input_file,
1292 internal_version));
1293 }
1294
1295 /* Public entrypoint. */
1296
1297 void
1298 diagnostic_manager_write_patch (diagnostic_manager *diag_mgr,
1299 FILE *dst_stream)
1300 {
1301 FAIL_IF_NULL (diag_mgr);
1302 FAIL_IF_NULL (dst_stream);
1303
1304 diag_mgr->write_patch (dst_stream);
1305 }
1306
1307 /* Public entrypoint. */
1308
1309 diagnostic_file *
1310 diagnostic_manager_new_file (diagnostic_manager *diag_mgr,
1311 const char *name,
1312 const char *sarif_source_language)
1313 {
1314 FAIL_IF_NULL (diag_mgr);
1315 FAIL_IF_NULL (name);
1316
1317 return diag_mgr->new_file (name, sarif_source_language);
1318 }
1319
1320 /* Public entrypoint. */
1321
1322 void
1323 diagnostic_file_set_buffered_content (diagnostic_file *file,
1324 const char *buf,
1325 size_t sz)
1326 {
1327 FAIL_IF_NULL (file);
1328 FAIL_IF_NULL (buf);
1329
1330 file->set_buffered_content (buf, sz);
1331 }
1332
1333 void
1334 diagnostic_manager_debug_dump_file (diagnostic_manager *,
1335 const diagnostic_file *file,
1336 FILE *out)
1337 {
1338 FAIL_IF_NULL (out);
1339 if (file)
1340 {
1341 fprintf (out, "file(name=\"%s\"",
1342 file->get_name ());
1343 if (file->get_sarif_source_language ())
1344 fprintf (out, ", sarif_source_language=\"%s\"",
1345 file->get_sarif_source_language ());
1346 if (const content_buffer *buf = file->get_content ())
1347 fprintf (out, ", content=(size=%zi)", buf->m_sz);
1348 fprintf (out, ")");
1349 }
1350 else
1351 fprintf (out, "(null)");
1352 }
1353
1354
1355 /* Public entrypoint. */
1356
1357 const diagnostic_physical_location *
1358 diagnostic_manager_new_location_from_file_and_line (diagnostic_manager *diag_mgr,
1359 const diagnostic_file *file,
1360 diagnostic_line_num_t linenum)
1361 {
1362 FAIL_IF_NULL (diag_mgr);
1363 FAIL_IF_NULL (file);
1364
1365 return diag_mgr->new_location_from_file_and_line (file, linenum);
1366 }
1367
1368 /* Public entrypoint. */
1369
1370 const diagnostic_physical_location *
1371 diagnostic_manager_new_location_from_file_line_column (diagnostic_manager *diag_mgr,
1372 const diagnostic_file *file,
1373 diagnostic_line_num_t line_num,
1374 diagnostic_column_num_t column_num)
1375 {
1376 FAIL_IF_NULL (diag_mgr);
1377 FAIL_IF_NULL (file);
1378
1379 return diag_mgr->new_location_from_file_line_column (file,
1380 line_num,
1381 column_num);
1382 }
1383
1384 /* Public entrypoint. */
1385
1386 const diagnostic_physical_location *
1387 diagnostic_manager_new_location_from_range (diagnostic_manager *diag_mgr,
1388 const diagnostic_physical_location *loc_caret,
1389 const diagnostic_physical_location *loc_start,
1390 const diagnostic_physical_location *loc_end)
1391 {
1392 FAIL_IF_NULL (diag_mgr);
1393
1394 return diag_mgr->new_location_from_range (loc_caret,
1395 loc_start,
1396 loc_end);
1397 }
1398
1399 /* Public entrypoint. */
1400
1401 void
1402 diagnostic_manager_debug_dump_location (const diagnostic_manager *diag_mgr,
1403 const diagnostic_physical_location *loc,
1404 FILE *out)
1405 {
1406 FAIL_IF_NULL (diag_mgr);
1407 FAIL_IF_NULL (out);
1408
1409 if (loc)
1410 {
1411 const location_t cpplib_loc = as_location_t (loc);
1412 diag_mgr->set_line_table_global ();
1413 const expanded_location exp_loc (expand_location (cpplib_loc));
1414
1415 diagnostic_context dc;
1416 diagnostic_initialize (&dc, 0);
1417 dc.m_show_column = true;
1418
1419 diagnostic_text_output_format text_format (dc);
1420 label_text loc_text = text_format.get_location_text (exp_loc);
1421 fprintf (out, "%s", loc_text.get ());
1422
1423 diagnostic_finish (&dc);
1424 }
1425 else
1426 fprintf (out, "(null)");
1427 }
1428
1429 /* Public entrypoint. */
1430
1431 const diagnostic_logical_location *
1432 diagnostic_manager_new_logical_location (diagnostic_manager *diag_mgr,
1433 enum diagnostic_logical_location_kind_t kind,
1434 const diagnostic_logical_location *parent,
1435 const char *short_name,
1436 const char *fully_qualified_name,
1437 const char *decorated_name)
1438 {
1439 FAIL_IF_NULL (diag_mgr);
1440
1441 return diag_mgr->new_logical_location (kind,
1442 parent,
1443 short_name,
1444 fully_qualified_name,
1445 decorated_name);
1446 }
1447
1448 void
1449 diagnostic_manager_debug_dump_logical_location (const diagnostic_manager *diag_mgr,
1450 const diagnostic_logical_location *loc,
1451 FILE *out)
1452 {
1453 FAIL_IF_NULL (diag_mgr);
1454 FAIL_IF_NULL (out);
1455
1456 if (loc)
1457 {
1458 fprintf (out, "logical_location(kind=");
1459 switch (loc->get_external_kind ())
1460 {
1461 default:
1462 gcc_unreachable ();
1463 case DIAGNOSTIC_LOGICAL_LOCATION_KIND_FUNCTION:
1464 fprintf (out, "function");
1465 break;
1466 case DIAGNOSTIC_LOGICAL_LOCATION_KIND_MEMBER:
1467 fprintf (out, "member");
1468 break;
1469 case DIAGNOSTIC_LOGICAL_LOCATION_KIND_MODULE:
1470 fprintf (out, "module");
1471 break;
1472 case DIAGNOSTIC_LOGICAL_LOCATION_KIND_NAMESPACE:
1473 fprintf (out, "namespace");
1474 break;
1475 case DIAGNOSTIC_LOGICAL_LOCATION_KIND_TYPE:
1476 fprintf (out, "file");
1477 break;
1478 case DIAGNOSTIC_LOGICAL_LOCATION_KIND_RETURN_TYPE:
1479 fprintf (out, "return_type");
1480 break;
1481 case DIAGNOSTIC_LOGICAL_LOCATION_KIND_PARAMETER:
1482 fprintf (out, "parameter");
1483 break;
1484 case DIAGNOSTIC_LOGICAL_LOCATION_KIND_VARIABLE:
1485 fprintf (out, "variable");
1486 break;
1487 }
1488 if (const diagnostic_logical_location *parent = loc->get_parent ())
1489 diagnostic_manager_debug_dump_logical_location (diag_mgr,
1490 parent,
1491 out);
1492 if (const char *val = loc->get_short_name ())
1493 fprintf (out, ", short_name=\"%s\"", val);
1494 if (const char *val = loc->get_name_with_scope ())
1495 fprintf (out, ", fully_qualified_name=\"%s\"", val);
1496 if (const char *val = loc->get_internal_name ())
1497 fprintf (out, ", decorated_name=\"%s\"", val);
1498 fprintf (out, ")");
1499 }
1500 else
1501 fprintf (out, "(null)");
1502 }
1503
1504 /* Public entrypoint. */
1505
1506 void
1507 diagnostic_manager_begin_group (diagnostic_manager *diag_mgr)
1508 {
1509 FAIL_IF_NULL (diag_mgr);
1510 diag_mgr->begin_group ();
1511 }
1512
1513 /* Public entrypoint. */
1514
1515 extern void
1516 diagnostic_manager_end_group (diagnostic_manager *diag_mgr)
1517 {
1518 FAIL_IF_NULL (diag_mgr);
1519 diag_mgr->end_group ();
1520 }
1521
1522 /* Public entrypoint. */
1523
1524 diagnostic *
1525 diagnostic_begin (diagnostic_manager *diag_mgr,
1526 enum diagnostic_level level)
1527 {
1528 FAIL_IF_NULL (diag_mgr);
1529
1530 return new diagnostic (*diag_mgr, level);
1531 }
1532
1533 /* Public entrypoint. */
1534
1535 void
1536 diagnostic_set_cwe (diagnostic *diag,
1537 unsigned cwe_id)
1538 {
1539 FAIL_IF_NULL (diag);
1540
1541 diag->set_cwe (cwe_id);
1542 }
1543
1544 /* Public entrypoint. */
1545
1546 void
1547 diagnostic_add_rule (diagnostic *diag,
1548 const char *title,
1549 const char *url)
1550 {
1551 FAIL_IF_NULL (diag);
1552
1553 diag->add_rule (title, url);
1554 }
1555
1556 /* Public entrypoint. */
1557
1558 void
1559 diagnostic_set_location (diagnostic *diag,
1560 const diagnostic_physical_location *loc)
1561 {
1562 FAIL_IF_NULL (diag);
1563 diag->get_manager ().assert_valid_diagnostic_physical_location (loc);
1564
1565 diag->set_location (loc);
1566 }
1567
1568 /* Public entrypoint. */
1569
1570 void
1571 diagnostic_add_location (diagnostic *diag,
1572 const diagnostic_physical_location *loc)
1573 {
1574 FAIL_IF_NULL (diag);
1575 diag->get_manager ().assert_valid_diagnostic_physical_location (loc);
1576
1577 diag->add_location (loc);
1578 }
1579
1580 /* Public entrypoint. */
1581
1582 void
1583 diagnostic_add_location_with_label (diagnostic *diag,
1584 const diagnostic_physical_location *loc,
1585 const char *text)
1586 {
1587 FAIL_IF_NULL (diag);
1588 diag->get_manager ().assert_valid_diagnostic_physical_location (loc);
1589 FAIL_IF_NULL (text);
1590
1591 diag->add_location_with_label (loc, text);
1592 }
1593
1594 /* Public entrypoint. */
1595
1596 void
1597 diagnostic_set_logical_location (diagnostic *diag,
1598 const diagnostic_logical_location *logical_loc)
1599 {
1600 FAIL_IF_NULL (diag);
1601
1602 diag->set_logical_location (logical_loc);
1603 }
1604
1605 /* Public entrypoint. */
1606
1607 void
1608 diagnostic_add_fix_it_hint_insert_before (diagnostic *diag,
1609 const diagnostic_physical_location *loc,
1610 const char *addition)
1611 {
1612 FAIL_IF_NULL (diag);
1613 diag->get_manager ().assert_valid_diagnostic_physical_location (loc);
1614 FAIL_IF_NULL (addition);
1615
1616 diag->get_manager ().set_line_table_global ();
1617 diag->get_rich_location ()->add_fixit_insert_before (as_location_t (loc),
1618 addition);
1619 }
1620
1621 /* Public entrypoint. */
1622
1623 void
1624 diagnostic_add_fix_it_hint_insert_after (diagnostic *diag,
1625 const diagnostic_physical_location *loc,
1626 const char *addition)
1627 {
1628 FAIL_IF_NULL (diag);
1629 diag->get_manager ().assert_valid_diagnostic_physical_location (loc);
1630 FAIL_IF_NULL (addition);
1631
1632 diag->get_manager ().set_line_table_global ();
1633 diag->get_rich_location ()->add_fixit_insert_after (as_location_t (loc),
1634 addition);
1635 }
1636
1637 /* Public entrypoint. */
1638
1639 void
1640 diagnostic_add_fix_it_hint_replace (diagnostic *diag,
1641 const diagnostic_physical_location *loc,
1642 const char *replacement)
1643 {
1644 FAIL_IF_NULL (diag);
1645 diag->get_manager ().assert_valid_diagnostic_physical_location (loc);
1646 FAIL_IF_NULL (replacement);
1647
1648 diag->get_manager ().set_line_table_global ();
1649 diag->get_rich_location ()->add_fixit_replace (as_location_t (loc),
1650 replacement);
1651 }
1652
1653 /* Public entrypoint. */
1654
1655 void
1656 diagnostic_add_fix_it_hint_delete (diagnostic *diag,
1657 const diagnostic_physical_location *loc)
1658 {
1659 FAIL_IF_NULL (diag);
1660 diag->get_manager ().assert_valid_diagnostic_physical_location (loc);
1661
1662 diag->get_manager ().set_line_table_global ();
1663 diag->get_rich_location ()->add_fixit_remove (as_location_t (loc));
1664 }
1665
1666 /* Public entrypoint. */
1667
1668 diagnostic_execution_path *
1669 diagnostic_add_execution_path (diagnostic *diag)
1670 {
1671 FAIL_IF_NULL (diag);
1672
1673 return diag->add_execution_path ();
1674 }
1675
1676 /* Public entrypoint. */
1677
1678 diagnostic_execution_path *
1679 diagnostic_manager_new_execution_path (diagnostic_manager *manager)
1680 {
1681 FAIL_IF_NULL (manager);
1682
1683 return manager->new_execution_path ();
1684 }
1685
1686 /* Public entrypoint. */
1687
1688 extern void
1689 diagnostic_take_execution_path (diagnostic *diag,
1690 diagnostic_execution_path *path)
1691 {
1692 FAIL_IF_NULL (diag);
1693 FAIL_IF_NULL (path);
1694
1695 return diag->take_execution_path (path);
1696 }
1697
1698 /* Public entrypoint. */
1699
1700 void
1701 diagnostic_execution_path_release (diagnostic_execution_path *path)
1702 {
1703 delete path;
1704 }
1705
1706 /* Public entrypoint. */
1707
1708 diagnostic_event_id
1709 diagnostic_execution_path_add_event (diagnostic_execution_path *path,
1710 const diagnostic_physical_location *physical_loc,
1711 const diagnostic_logical_location *logical_loc,
1712 unsigned stack_depth,
1713 const char *gmsgid, ...)
1714 {
1715 FAIL_IF_NULL (path);
1716 FAIL_IF_NULL (gmsgid);
1717
1718 va_list args;
1719 va_start (args, gmsgid);
1720 diagnostic_event_id_t result = path->add_event_va (physical_loc,
1721 logical_loc,
1722 stack_depth,
1723 gmsgid, &args);
1724 va_end (args);
1725
1726 return as_diagnostic_event_id (result);
1727 }
1728
1729 /* Public entrypoint. */
1730
1731 diagnostic_event_id
1732 diagnostic_execution_path_add_event_va (diagnostic_execution_path *path,
1733 const diagnostic_physical_location *physical_loc,
1734 const diagnostic_logical_location *logical_loc,
1735 unsigned stack_depth,
1736 const char *gmsgid,
1737 va_list *args)
1738 {
1739 FAIL_IF_NULL (path);
1740 FAIL_IF_NULL (gmsgid);
1741
1742 diagnostic_event_id_t result = path->add_event_va (physical_loc,
1743 logical_loc,
1744 stack_depth,
1745 gmsgid, args);
1746 return as_diagnostic_event_id (result);
1747 }
1748
1749 /* Public entrypoint. */
1750
1751 void
1752 diagnostic_finish (diagnostic *diag, const char *gmsgid, ...)
1753 {
1754 FAIL_IF_NULL (diag);
1755
1756 va_list args;
1757 va_start (args, gmsgid);
1758 diagnostic_finish_va (diag, gmsgid, &args);
1759 va_end (args);
1760 }
1761
1762 /* Public entrypoint. */
1763
1764 void
1765 diagnostic_finish_va (diagnostic *diag, const char *gmsgid, va_list *args)
1766 {
1767 FAIL_IF_NULL (diag);
1768
1769 if (const char *tool_name
1770 = diag->get_manager ().get_client_version_info ()->m_name.get_str ())
1771 progname = tool_name;
1772 else
1773 progname = "progname";
1774 auto_diagnostic_group d;
1775 diag->get_manager ().emit (*diag, gmsgid, args);
1776 delete diag;
1777 }
1778
1779 /* Public entrypoint. */
1780
1781 diagnostic_file *
1782 diagnostic_physical_location_get_file (const diagnostic_physical_location *physical_loc)
1783 {
1784 if (!physical_loc)
1785 return nullptr;
1786
1787 return physical_loc->get_file ();
1788 }