Branch data Line data Source code
1 : : /* Selftest support for diagnostics.
2 : : Copyright (C) 2016-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 : : #define INCLUDE_VECTOR
21 : : #include "config.h"
22 : : #include "system.h"
23 : : #include "coretypes.h"
24 : : #include "diagnostics/sink.h"
25 : : #include "selftest.h"
26 : : #include "diagnostics/selftest-context.h"
27 : :
28 : : /* The selftest code should entirely disappear in a production
29 : : configuration, hence we guard all of it with #if CHECKING_P. */
30 : :
31 : : #if CHECKING_P
32 : :
33 : : namespace diagnostics {
34 : : namespace selftest {
35 : :
36 : : /* Implementation of class diagnostics::selftest::test_context. */
37 : :
38 : 18813 : test_context::test_context ()
39 : : {
40 : 18813 : diagnostic_initialize (this, 0);
41 : 18813 : pp_show_color (get_reference_printer ()) = false;
42 : :
43 : 18813 : auto &source_printing_opts = get_source_printing_options ();
44 : 18813 : source_printing_opts.enabled = true;
45 : 18813 : source_printing_opts.show_labels_p = true;
46 : 18813 : m_show_column = true;
47 : 18813 : start_span (this) = start_span_cb;
48 : 18813 : source_printing_opts.min_margin_width = 6;
49 : 18813 : source_printing_opts.max_width = 80;
50 : 18813 : pp_buffer (get_sink (0).get_printer ())->m_flush_p = false;
51 : 18813 : }
52 : :
53 : 18813 : test_context::~test_context ()
54 : : {
55 : 18813 : diagnostic_finish (this);
56 : 18813 : }
57 : :
58 : : /* Implementation of diagnostics::start_span_fn, hiding the
59 : : real filename (to avoid printing the names of tempfiles). */
60 : :
61 : : void
62 : 1712 : test_context::
63 : : start_span_cb (const location_print_policy &loc_policy,
64 : : to_text &html_or_text,
65 : : expanded_location exploc)
66 : : {
67 : 1712 : exploc.file = "FILENAME";
68 : 1712 : default_start_span_fn<to_text>
69 : 1712 : (loc_policy, html_or_text, exploc);
70 : 1712 : }
71 : :
72 : : bool
73 : 196 : test_context::report (enum kind kind,
74 : : rich_location &richloc,
75 : : const metadata *metadata_,
76 : : option_id opt_id,
77 : : const char * fmt, ...)
78 : : {
79 : 196 : va_list ap;
80 : 196 : va_start (ap, fmt);
81 : 196 : begin_group ();
82 : 196 : bool result = diagnostic_impl (&richloc, metadata_, opt_id, fmt, &ap, kind);
83 : 196 : end_group ();
84 : 196 : va_end (ap);
85 : 196 : return result;
86 : : }
87 : :
88 : : /* Print RICHLOC's source and annotations to this context's m_printer.
89 : : Return the text buffer from the printer. */
90 : :
91 : : const char *
92 : 4992 : test_context::test_show_locus (rich_location &richloc)
93 : : {
94 : 4992 : pretty_printer *pp = get_reference_printer ();
95 : 4992 : gcc_assert (pp);
96 : 4992 : source_print_policy source_policy (*this);
97 : 4992 : source_policy.print (*pp, richloc, kind::error, nullptr);
98 : 4992 : return pp_formatted_text (pp);
99 : : }
100 : :
101 : : } // namespace selftest
102 : : } // namespace diagnostics
103 : :
104 : : #endif /* #if CHECKING_P */
|