Branch data Line data Source code
1 : : /* Support for the DSL of -fdiagnostics-add-output= and
2 : : -fdiagnostics-set-output=.
3 : : Copyright (C) 2024-2025 Free Software Foundation, Inc.
4 : :
5 : : This file is part of GCC.
6 : :
7 : : GCC is free software; you can redistribute it and/or modify it under
8 : : the terms of the GNU General Public License as published by the Free
9 : : Software Foundation; either version 3, or (at your option) any later
10 : : version.
11 : :
12 : : GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 : : WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 : : FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 : : for more details.
16 : :
17 : : You should have received a copy of the GNU General Public License
18 : : along with GCC; see the file COPYING3. If not see
19 : : <http://www.gnu.org/licenses/>. */
20 : :
21 : : #ifndef GCC_DIAGNOSTICS_OUTPUT_SPEC_H
22 : : #define GCC_DIAGNOSTICS_OUTPUT_SPEC_H
23 : :
24 : : #include "diagnostics/sink.h"
25 : : #include "diagnostics/output-file.h"
26 : :
27 : : namespace diagnostics {
28 : : namespace output_spec {
29 : :
30 : : /* An abstract base class for handling the DSL of -fdiagnostics-add-output=
31 : : and -fdiagnostics-set-output=. */
32 : :
33 : : class context
34 : : {
35 : : public:
36 : : std::unique_ptr<sink>
37 : : parse_and_make_sink (const char *,
38 : : diagnostics::context &dc);
39 : :
40 : : void
41 : : report_error (const char *gmsgid, ...) const
42 : : ATTRIBUTE_GCC_DIAG(2,3);
43 : :
44 : : void
45 : : report_unknown_key (const char *unparsed_arg,
46 : : const std::string &key,
47 : : const std::string &scheme_name,
48 : : auto_vec<const char *> &known_keys) const;
49 : :
50 : : void
51 : : report_missing_key (const char *unparsed_arg,
52 : : const std::string &key,
53 : : const std::string &scheme_name,
54 : : const char *metavar) const;
55 : :
56 : : output_file
57 : : open_output_file (label_text &&filename) const;
58 : :
59 : : const char *
60 : 16 : get_option_name () const { return m_option_name; }
61 : :
62 : : line_maps *
63 : 48 : get_affected_location_mgr () const { return m_affected_location_mgr; }
64 : :
65 : 65 : virtual ~context () {}
66 : :
67 : : virtual void
68 : : report_error_va (const char *gmsgid, va_list *ap) const = 0;
69 : :
70 : : virtual const char *
71 : : get_base_filename () const = 0;
72 : :
73 : : protected:
74 : 65 : context (const char *option_name,
75 : : line_maps *affected_location_mgr)
76 : 65 : : m_option_name (option_name),
77 : 65 : m_affected_location_mgr (affected_location_mgr)
78 : : {
79 : : }
80 : :
81 : : const char *m_option_name;
82 : : line_maps *m_affected_location_mgr;
83 : : };
84 : :
85 : : /* A subclass that implements reporting errors via a diagnostics::context. */
86 : :
87 : 65 : struct dc_spec_context : public output_spec::context
88 : : {
89 : : public:
90 : 65 : dc_spec_context (diagnostics::context &dc,
91 : : line_maps *affected_location_mgr,
92 : : line_maps *control_location_mgr,
93 : : location_t loc,
94 : : const char *option_name)
95 : 65 : : context (option_name, affected_location_mgr),
96 : 65 : m_dc (dc),
97 : 65 : m_control_location_mgr (control_location_mgr),
98 : 65 : m_loc (loc)
99 : : {}
100 : :
101 : 16 : void report_error_va (const char *gmsgid, va_list *ap) const final override
102 : : ATTRIBUTE_GCC_DIAG(2, 0)
103 : : {
104 : 16 : m_dc.begin_group ();
105 : 16 : rich_location richloc (m_control_location_mgr, m_loc);
106 : 16 : m_dc.diagnostic_impl (&richloc, nullptr, -1, gmsgid, ap, kind::error);
107 : 16 : m_dc.end_group ();
108 : 16 : }
109 : :
110 : : diagnostics::context &m_dc;
111 : : line_maps *m_control_location_mgr;
112 : : location_t m_loc;
113 : : };
114 : :
115 : : } // namespace output_spec
116 : : } // namespace diagnostics
117 : :
118 : : #endif // #ifndef GCC_DIAGNOSTICS_OUTPUT_SPEC_H
|