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