GCC Middle and Back End API Reference
selftest-context.h
Go to the documentation of this file.
1/* Selftest support for diagnostics.
2 Copyright (C) 2016-2025 Free Software Foundation, Inc.
3
4This file is part of GCC.
5
6GCC is free software; you can redistribute it and/or modify it under
7the terms of the GNU General Public License as published by the Free
8Software Foundation; either version 3, or (at your option) any later
9version.
10
11GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12WARRANTY; without even the implied warranty of MERCHANTABILITY or
13FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14for more details.
15
16You should have received a copy of the GNU General Public License
17along with GCC; see the file COPYING3. If not see
18<http://www.gnu.org/licenses/>. */
19
20#ifndef GCC_DIAGNOSTICS_SELFTEST_CONTEXT_H
21#define GCC_DIAGNOSTICS_SELFTEST_CONTEXT_H
22
23/* The selftest code should entirely disappear in a production
24 configuration, hence we guard all of it with #if CHECKING_P. */
25
26#if CHECKING_P
27
28namespace diagnostics {
29namespace selftest {
30
31/* Convenience subclass of diagnostics::context for testing
32 the diagnostic subsystem. */
33
34class test_context : public context
35{
36 public:
37 test_context ();
38 ~test_context ();
39
40 /* Implementation of diagnostics::start_span_fn, hiding the
41 real filename (to avoid printing the names of tempfiles). */
42 static void
43 start_span_cb (const location_print_policy &,
44 to_text &sink,
45 expanded_location exploc);
46
47 /* Report a diagnostic to this context. For a selftest, this
48 should only be called on a context that uses a non-standard formatter
49 that e.g. gathers the results in memory, rather than emits to stderr. */
50 bool
51 report (enum kind kind,
52 rich_location &richloc,
53 const metadata *,
54 option_id opt_id,
55 const char * fmt, ...) ATTRIBUTE_GCC_DIAG(6,7);
56
57 const char *test_show_locus (rich_location &richloc);
58
59 /* Setters for the context's source_printing_options
60 for use in selftests. */
61 void colorize_source (bool val)
62 {
63 get_source_printing_options ().colorize_source_p = val;
64 }
65 void show_labels (bool val)
66 {
67 get_source_printing_options ().show_labels_p = val;
68 }
69 void show_line_numbers (bool val)
70 {
71 get_source_printing_options ().show_line_numbers_p = val;
72 }
73 void show_ruler (bool val)
74 {
75 get_source_printing_options ().show_ruler_p = val;
76 }
77 void show_event_links (bool val)
78 {
79 get_source_printing_options ().show_event_links_p = val;
80 }
81 void set_caret_char (unsigned idx, char ch)
82 {
83 gcc_assert (idx < rich_location::STATICALLY_ALLOCATED_RANGES);
84 get_source_printing_options ().caret_chars[idx] = ch;
85 }
86};
87
88} // namespace selftest
89} // namespace diagnostics
90
91#endif /* #if CHECKING_P */
92
93#endif /* GCC_DIAGNOSTICS_SELFTEST_CONTEXT_H */
#define ATTRIBUTE_GCC_DIAG(m, n)
Definition diagnostic-core.h:71
Definition coretypes.h:167
#define gcc_assert(EXPR)
Definition system.h:814