Debian Codegraph @

gcc/libgdiagnostics++.h

raw
1 /* A C++ wrapper API around libgdiagnostics.h 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
7 under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3, or (at your option)
9 any later version.
10
11 GCC is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License 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 #ifndef LIBGDIAGNOSTICSPP_H
21 #define LIBGDIAGNOSTICSPP_H
22
23 #include "libgdiagnostics.h"
24
25 namespace libgdiagnostics {
26
27 typedef diagnostic_line_num_t line_num_t;
28 typedef diagnostic_column_num_t column_num_t;
29
30 class file;
31 class physical_location;
32 class logical_location;
33 class execution_path;
34 class group;
35 class manager;
36 class diagnostic;
37
38 /* Wrapper around a borrowed diagnostic_text_sink *. */
39
40 class text_sink
41 {
42 public:
43 text_sink (diagnostic_text_sink *inner)
44 : m_inner (inner)
45 {
46 }
47
48 void
49 set_source_printing_enabled (int value)
50 {
51 diagnostic_text_sink_set_source_printing_enabled (m_inner, value);
52 }
53
54 void
55 set_colorize (enum diagnostic_colorize colorize)
56 {
57 diagnostic_text_sink_set_colorize (m_inner, colorize);
58 }
59
60 void
61 set_labelled_source_colorization_enabled (int value)
62 {
63 diagnostic_text_sink_set_labelled_source_colorization_enabled (m_inner,
64 value);
65 }
66
67 diagnostic_text_sink *m_inner;
68 };
69
70 /* Wrapper around a diagnostic_file *. */
71
72 class file
73 {
74 public:
75 file () : m_inner (nullptr) {}
76 file (diagnostic_file *file) : m_inner (file) {}
77 file (const file &other) : m_inner (other.m_inner) {}
78 file &operator= (const file &other) { m_inner = other.m_inner; return *this; }
79
80 void set_buffered_content (const char *data, size_t sz);
81
82 diagnostic_file * m_inner;
83 };
84
85 /* Wrapper around a const diagnostic_physical_location *. */
86
87 class physical_location
88 {
89 public:
90 physical_location () : m_inner (nullptr) {}
91
92 physical_location (const diagnostic_physical_location *location)
93 : m_inner (location)
94 {}
95
96 file get_file () const;
97
98 const diagnostic_physical_location *m_inner;
99 };
100
101 /* Wrapper around a const diagnostic_logical_location *. */
102
103 class logical_location
104 {
105 public:
106 logical_location () : m_inner (nullptr) {}
107
108 logical_location (const diagnostic_logical_location *logical_loc)
109 : m_inner (logical_loc)
110 {}
111
112 const diagnostic_logical_location *m_inner;
113 };
114
115 /* RAII class around a diagnostic_execution_path *. */
116
117 class execution_path
118 {
119 public:
120 execution_path () : m_inner (nullptr), m_owned (false) {}
121
122 execution_path (diagnostic_execution_path *path)
123 : m_inner (path), m_owned (true)
124 {}
125
126 execution_path (const diagnostic_execution_path *path)
127 : m_inner (const_cast<diagnostic_execution_path *> (path)),
128 m_owned (false)
129 {}
130
131 execution_path (const execution_path &other) = delete;
132 execution_path &operator= (const execution_path &other) = delete;
133
134 execution_path (execution_path &&other)
135 : m_inner (other.m_inner),
136 m_owned (other.m_owned)
137 {
138 other.m_inner = nullptr;
139 other.m_owned = false;
140 }
141
142 execution_path &operator= (execution_path &&other)
143 {
144 m_inner = other.m_inner;
145 m_owned = other.m_owned;
146 other.m_inner = nullptr;
147 other.m_owned = false;
148 return *this;
149 }
150
151 ~execution_path ()
152 {
153 if (m_owned)
154 diagnostic_execution_path_release (m_inner);
155 }
156
157 diagnostic_event_id
158 add_event (physical_location physical_loc,
159 logical_location logical_loc,
160 unsigned stack_depth,
161 const char *fmt, ...)
162 LIBGDIAGNOSTICS_PARAM_GCC_FORMAT_STRING (5, 6);
163
164 diagnostic_event_id
165 add_event_va (physical_location physical_loc,
166 logical_location logical_loc,
167 unsigned stack_depth,
168 const char *fmt,
169 va_list *args)
170 LIBGDIAGNOSTICS_PARAM_GCC_FORMAT_STRING (5, 0);
171
172 diagnostic_execution_path *m_inner;
173 bool m_owned;
174 };
175
176 /* RAII class for starting/ending a group within a diagnostic_manager. */
177
178 class group
179 {
180 public:
181 group (manager &mgr);
182 ~group ();
183
184 private:
185 manager &m_mgr;
186 };
187
188 /* Wrapper around a diagnostic *. */
189
190 class diagnostic
191 {
192 public:
193 diagnostic (::diagnostic *d) : m_inner (d) {}
194
195 void
196 set_cwe (unsigned cwe_id);
197
198 void
199 add_rule (const char *title, const char *url);
200
201 void
202 set_location (physical_location loc);
203
204 void
205 add_location (physical_location loc);
206
207 void
208 add_location_with_label (physical_location loc,
209 const char *text);
210
211 void
212 set_logical_location (logical_location loc);
213
214 void
215 add_fix_it_hint_insert_before (physical_location loc,
216 const char *addition);
217 void
218 add_fix_it_hint_insert_after (physical_location loc,
219 const char *addition);
220 void
221 add_fix_it_hint_replace (physical_location loc,
222 const char *replacement);
223 void
224 add_fix_it_hint_delete (physical_location loc);
225
226 void
227 take_execution_path (execution_path path);
228
229 void
230 finish (const char *fmt, ...)
231 LIBGDIAGNOSTICS_PARAM_MUST_BE_NON_NULL (2)
232 LIBGDIAGNOSTICS_PARAM_GCC_FORMAT_STRING (2, 3);
233
234 void
235 finish_va (const char *fmt, va_list *args)
236 LIBGDIAGNOSTICS_PARAM_MUST_BE_NON_NULL (2)
237 LIBGDIAGNOSTICS_PARAM_GCC_FORMAT_STRING (2, 0);
238
239 ::diagnostic * const m_inner;
240 };
241
242 /* Wrapper around a diagnostic_manager *, possibly with ownership. */
243
244 class manager
245 {
246 public:
247 manager ()
248 : m_inner (diagnostic_manager_new ()),
249 m_owned (true)
250 {
251 }
252 manager (diagnostic_manager *inner, bool owned)
253 : m_inner (inner),
254 m_owned (owned)
255 {
256 }
257 ~manager ()
258 {
259 if (m_owned)
260 diagnostic_manager_release (m_inner);
261 }
262
263 manager (const manager &other) = delete;
264 manager (manager &&other)
265 : m_inner (other.m_inner),
266 m_owned (other.m_owned)
267 {
268 other.m_inner = nullptr;
269 }
270
271 void
272 set_tool_name (const char *value)
273 {
274 diagnostic_manager_set_tool_name (m_inner, value);
275 }
276
277 void
278 set_full_name (const char *value)
279 {
280 diagnostic_manager_set_full_name (m_inner, value);
281 }
282
283 void
284 set_version_string (const char *value)
285 {
286 diagnostic_manager_set_version_string (m_inner, value);
287 }
288
289 void
290 set_version_url (const char *value)
291 {
292 diagnostic_manager_set_version_url (m_inner, value);
293 }
294
295 text_sink
296 add_text_sink (FILE *dst_stream,
297 enum diagnostic_colorize colorize)
298 {
299 return text_sink
300 (diagnostic_manager_add_text_sink (m_inner, dst_stream, colorize));
301 }
302
303 void
304 add_sarif_sink (FILE *dst_stream,
305 file main_input_file,
306 enum diagnostic_sarif_version version)
307 {
308 diagnostic_manager_add_sarif_sink (m_inner, dst_stream,
309 main_input_file.m_inner,
310 version);
311 }
312
313 void
314 write_patch (FILE *dst_stream)
315 {
316 diagnostic_manager_write_patch (m_inner, dst_stream);
317 }
318
319 /* Location management. */
320
321 file
322 new_file (const char *name,
323 const char *sarif_source_language)
324 LIBGDIAGNOSTICS_PARAM_MUST_BE_NON_NULL (2)
325 LIBGDIAGNOSTICS_PARAM_CAN_BE_NULL (3);
326
327 void
328 debug_dump (file f,
329 FILE *out);
330
331 physical_location
332 new_location_from_file_and_line (file f, diagnostic_line_num_t line_num);
333
334 physical_location
335 new_location_from_file_line_column (file f,
336 line_num_t line_num,
337 column_num_t column_num);
338
339 physical_location
340 new_location_from_range (physical_location loc_caret,
341 physical_location loc_start,
342 physical_location loc_end);
343
344 void
345 debug_dump (physical_location loc,
346 FILE *out);
347
348 logical_location
349 new_logical_location (enum diagnostic_logical_location_kind_t kind,
350 logical_location parent,
351 const char *short_name,
352 const char *fully_qualified_name,
353 const char *decorated_name);
354
355 void
356 debug_dump (logical_location loc,
357 FILE *out);
358
359 execution_path
360 new_execution_path ();
361
362 diagnostic
363 begin_diagnostic (enum diagnostic_level level);
364
365
366 diagnostic_manager *m_inner;
367 bool m_owned;
368 };
369
370 // Implementation
371
372 // class file
373
374 inline void
375 file::set_buffered_content (const char *data, size_t sz)
376 {
377 diagnostic_file_set_buffered_content (m_inner, data, sz);
378 }
379
380 // class physical_location
381
382 inline file
383 physical_location::get_file () const
384 {
385 return file (diagnostic_physical_location_get_file (m_inner));
386 }
387
388 // class execution_path
389
390 inline diagnostic_event_id
391 execution_path::add_event (physical_location physical_loc,
392 logical_location logical_loc,
393 unsigned stack_depth,
394 const char *fmt, ...)
395 {
396 va_list args;
397 va_start (args, fmt);
398 diagnostic_event_id result = add_event_va (physical_loc,
399 logical_loc,
400 stack_depth,
401 fmt, &args);
402 va_end (args);
403
404 return result;
405 }
406
407 inline diagnostic_event_id
408 execution_path::add_event_va (physical_location physical_loc,
409 logical_location logical_loc,
410 unsigned stack_depth,
411 const char *fmt,
412 va_list *args)
413 {
414 return diagnostic_execution_path_add_event_va (m_inner,
415 physical_loc.m_inner,
416 logical_loc.m_inner,
417 stack_depth,
418 fmt,
419 args);
420 }
421
422 // class group
423
424 inline
425 group::group (manager &mgr)
426 : m_mgr (mgr)
427 {
428 diagnostic_manager_begin_group (m_mgr.m_inner);
429 }
430
431 inline
432 group::~group ()
433 {
434 diagnostic_manager_end_group (m_mgr.m_inner);
435 }
436
437 // class diagnostic
438
439 inline void
440 diagnostic::set_cwe (unsigned cwe_id)
441 {
442 diagnostic_set_cwe (m_inner, cwe_id);
443 }
444
445 inline void
446 diagnostic::add_rule (const char *title, const char *url)
447 {
448 diagnostic_add_rule (m_inner, title, url);
449 }
450
451 inline void
452 diagnostic::set_location (physical_location loc)
453 {
454 diagnostic_set_location (m_inner, loc.m_inner);
455 }
456
457 inline void
458 diagnostic::add_location_with_label (physical_location loc,
459 const char *text)
460 {
461 diagnostic_add_location_with_label (m_inner, loc.m_inner, text);
462 }
463
464 inline void
465 diagnostic::add_location (physical_location loc)
466 {
467 diagnostic_add_location (m_inner, loc.m_inner);
468 }
469
470 inline void
471 diagnostic::set_logical_location (logical_location loc)
472 {
473 diagnostic_set_logical_location (m_inner, loc.m_inner);
474 }
475
476 inline void
477 diagnostic::add_fix_it_hint_insert_before (physical_location loc,
478 const char *addition)
479 {
480 diagnostic_add_fix_it_hint_insert_before (m_inner,
481 loc.m_inner,
482 addition);
483 }
484
485 inline void
486 diagnostic::add_fix_it_hint_insert_after (physical_location loc,
487 const char *addition)
488 {
489 diagnostic_add_fix_it_hint_insert_after (m_inner,
490 loc.m_inner,
491 addition);
492 }
493
494 inline void
495 diagnostic::add_fix_it_hint_replace (physical_location loc,
496 const char *replacement)
497 {
498 diagnostic_add_fix_it_hint_replace (m_inner,
499 loc.m_inner,
500 replacement);
501 }
502
503 inline void
504 diagnostic::add_fix_it_hint_delete (physical_location loc)
505 {
506 diagnostic_add_fix_it_hint_delete (m_inner,
507 loc.m_inner);
508 }
509
510 inline void
511 diagnostic::take_execution_path (execution_path path)
512 {
513 diagnostic_take_execution_path (m_inner,
514 path.m_inner);
515 path.m_owned = false;
516 }
517
518 inline void
519 diagnostic::finish (const char *fmt, ...)
520 {
521 va_list ap;
522 va_start (ap, fmt);
523 diagnostic_finish_va (m_inner, fmt, &ap);
524 va_end (ap);
525 }
526
527 inline void
528 diagnostic::finish_va (const char *fmt, va_list *args)
529 {
530 diagnostic_finish_va (m_inner, fmt, args);
531 }
532
533 // class manager
534
535 inline file
536 manager::new_file (const char *name,
537 const char *sarif_source_language)
538 {
539 return file
540 (diagnostic_manager_new_file (m_inner, name, sarif_source_language));
541 }
542
543 inline physical_location
544 manager::new_location_from_file_and_line (file f,
545 diagnostic_line_num_t line_num)
546 {
547 return physical_location
548 (diagnostic_manager_new_location_from_file_and_line (m_inner,
549 f.m_inner,
550 line_num));
551 }
552
553 inline physical_location
554 manager::new_location_from_file_line_column (file f,
555 line_num_t line_num,
556 column_num_t column_num)
557 {
558 return physical_location
559 (diagnostic_manager_new_location_from_file_line_column (m_inner,
560 f.m_inner,
561 line_num,
562 column_num));
563 }
564
565 inline physical_location
566 manager::new_location_from_range (physical_location loc_caret,
567 physical_location loc_start,
568 physical_location loc_end)
569 {
570 return physical_location
571 (diagnostic_manager_new_location_from_range (m_inner,
572 loc_caret.m_inner,
573 loc_start.m_inner,
574 loc_end.m_inner));
575 }
576
577 inline void
578 manager::debug_dump (physical_location loc,
579 FILE *out)
580 {
581 diagnostic_manager_debug_dump_location (m_inner,
582 loc.m_inner,
583 out);
584 }
585 inline logical_location
586 manager::new_logical_location (enum diagnostic_logical_location_kind_t kind,
587 logical_location parent,
588 const char *short_name,
589 const char *fully_qualified_name,
590 const char *decorated_name)
591 {
592 return logical_location
593 (diagnostic_manager_new_logical_location (m_inner,
594 kind,
595 parent.m_inner,
596 short_name,
597 fully_qualified_name,
598 decorated_name));
599 }
600
601 inline void
602 manager::debug_dump (logical_location loc,
603 FILE *out)
604 {
605 diagnostic_manager_debug_dump_logical_location (m_inner,
606 loc.m_inner,
607 out);
608 }
609
610 inline execution_path
611 manager::new_execution_path ()
612 {
613 return execution_path (diagnostic_manager_new_execution_path (m_inner));
614 }
615
616 inline diagnostic
617 manager::begin_diagnostic (enum diagnostic_level level)
618 {
619 return diagnostic (diagnostic_begin (m_inner, level));
620 }
621
622 } // namespace libgdiagnostics
623
624 #endif // #ifndef LIBGDIAGNOSTICSPP_H