libcpp, c, middle-end: Optimize initializers using #embed in C
[official-gcc.git] / gcc / selftest-diagnostic.cc
blob4e9ee921b0442d55cc183e4ea0ba1bdeb471f154
1 /* Selftest support for diagnostics.
2 Copyright (C) 2016-2024 Free Software Foundation, Inc.
4 This file is part of GCC.
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.
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.
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/>. */
20 #include "config.h"
21 #include "system.h"
22 #include "coretypes.h"
23 #include "diagnostic.h"
24 #include "selftest.h"
25 #include "selftest-diagnostic.h"
27 /* The selftest code should entirely disappear in a production
28 configuration, hence we guard all of it with #if CHECKING_P. */
30 #if CHECKING_P
32 namespace selftest {
34 /* Implementation of class selftest::test_diagnostic_context. */
36 test_diagnostic_context::test_diagnostic_context ()
38 diagnostic_initialize (this, 0);
39 pp_show_color (m_printer) = false;
40 m_source_printing.enabled = true;
41 m_source_printing.show_labels_p = true;
42 m_show_column = true;
43 diagnostic_start_span (this) = start_span_cb;
44 m_source_printing.min_margin_width = 6;
45 m_source_printing.max_width = 80;
48 test_diagnostic_context::~test_diagnostic_context ()
50 diagnostic_finish (this);
53 /* Implementation of diagnostic_start_span_fn, hiding the
54 real filename (to avoid printing the names of tempfiles). */
56 void
57 test_diagnostic_context::
58 start_span_cb (const diagnostic_location_print_policy &loc_policy,
59 pretty_printer *pp,
60 expanded_location exploc)
62 exploc.file = "FILENAME";
63 default_diagnostic_start_span_fn (loc_policy, pp, exploc);
66 bool
67 test_diagnostic_context::report (diagnostic_t kind,
68 rich_location &richloc,
69 const diagnostic_metadata *metadata,
70 int option,
71 const char * fmt, ...)
73 va_list ap;
74 va_start (ap, fmt);
75 begin_group ();
76 bool result = diagnostic_impl (&richloc, metadata, option, fmt, &ap, kind);
77 end_group ();
78 va_end (ap);
79 return result;
82 /* Print RICHLOC's source and annotations to this context's m_printer.
83 Return the text buffer from the printer. */
85 const char *
86 test_diagnostic_context::test_show_locus (rich_location &richloc)
88 gcc_assert (m_printer);
89 diagnostic_source_print_policy source_policy (*this);
90 source_policy.print (*m_printer, richloc, DK_ERROR, nullptr);
91 return pp_formatted_text (m_printer);
94 } // namespace selftest
96 #endif /* #if CHECKING_P */