Line data Source code
1 : /* Support for -fdiagnostics-add-output= and -fdiagnostics-set-output=.
2 : Copyright (C) 2024-2026 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 :
21 : /* This file implements the options -fdiagnostics-add-output=,
22 : -fdiagnostics-set-output=. Most of the work is done
23 : by diagnostics/output-spec.cc so it can be shared by libgdiagnostics. */
24 :
25 : #include "config.h"
26 : #define INCLUDE_ARRAY
27 : #define INCLUDE_LIST
28 : #define INCLUDE_STRING
29 : #define INCLUDE_VECTOR
30 : #include "system.h"
31 : #include "coretypes.h"
32 : #include "version.h"
33 : #include "intl.h"
34 : #include "diagnostic.h"
35 : #include "diagnostics/output-spec.h"
36 : #include "diagnostics/logging.h"
37 : #include "diagnostics/sarif-sink.h"
38 : #include "opts.h"
39 : #include "options.h"
40 : #include "tree-diagnostic-sink-extensions.h"
41 : #include "opts-diagnostic.h"
42 : #include "pub-sub.h"
43 :
44 : /* Decls. */
45 :
46 : namespace {
47 :
48 : class gcc_extra_keys : public diagnostics::output_spec::key_handler
49 : {
50 : public:
51 51 : gcc_extra_keys ()
52 51 : : m_analyzer_graphs (false),
53 51 : m_cfgs (false)
54 : {
55 : }
56 : enum result
57 3 : maybe_handle_kv (const diagnostics::output_spec::context &ctxt,
58 : const std::string &key,
59 : const std::string &value) final override
60 : {
61 3 : if (key == "analyzer-graphs")
62 1 : return parse_bool_value (ctxt, key, value, m_analyzer_graphs);
63 2 : if (key == "cfgs")
64 2 : return parse_bool_value (ctxt, key, value, m_cfgs);
65 : return result::unrecognized;
66 : }
67 :
68 : void
69 0 : get_keys (auto_vec<const char *> &out_known_keys) const final override
70 : {
71 0 : out_known_keys.safe_push ("analyzer-graphs");
72 0 : out_known_keys.safe_push ("cfgs");
73 0 : }
74 :
75 : bool m_analyzer_graphs;
76 : bool m_cfgs;
77 : };
78 :
79 51 : struct opt_spec_context : public diagnostics::output_spec::dc_spec_context
80 : {
81 : public:
82 51 : opt_spec_context (const gcc_options &opts,
83 : diagnostics::context &dc,
84 : line_maps *location_mgr,
85 : location_t loc,
86 : const char *option_name,
87 : const char *option_value,
88 : diagnostics::output_spec::key_handler *client_keys)
89 : : dc_spec_context (option_name,
90 : option_value,
91 : client_keys,
92 : location_mgr,
93 : dc,
94 : location_mgr,
95 : loc),
96 51 : m_opts (opts)
97 : {}
98 :
99 : const char *
100 37 : get_base_filename () const final override
101 : {
102 37 : return get_diagnostic_file_output_basename (m_opts);
103 : }
104 :
105 : const gcc_options &m_opts;
106 : };
107 :
108 : static void
109 51 : handle_gcc_specific_keys (const gcc_extra_keys &gcc_keys,
110 : diagnostics::sink &sink,
111 : const gcc_extension_factory &ext_factory)
112 : {
113 51 : if (gcc_keys.m_cfgs)
114 2 : sink.add_extension (ext_factory.make_cfg_extension (sink));
115 51 : if (gcc_keys.m_analyzer_graphs)
116 1 : sink.add_extension (ext_factory.make_analyzer_graphs_extension (sink));
117 51 : }
118 :
119 : static std::unique_ptr<diagnostics::sink>
120 51 : try_to_make_sink (const gcc_options &opts,
121 : diagnostics::context &dc,
122 : const char *option_name,
123 : const char *unparsed_spec,
124 : location_t loc)
125 : {
126 51 : gcc_assert (line_table);
127 :
128 51 : gcc_extra_keys gcc_keys;
129 51 : opt_spec_context ctxt (opts, dc, line_table, loc, option_name, unparsed_spec,
130 51 : &gcc_keys);
131 51 : auto sink = ctxt.parse_and_make_sink (dc);
132 51 : if (!sink)
133 0 : return nullptr;
134 :
135 51 : sink->set_main_input_filename (opts.x_main_input_filename);
136 :
137 51 : if (auto ext_factory = gcc_extension_factory::singleton)
138 51 : handle_gcc_specific_keys (gcc_keys, *sink, *ext_factory);
139 :
140 51 : return sink;
141 51 : }
142 :
143 : } // anon namespace
144 :
145 : // class gcc_extension_factory
146 :
147 : const gcc_extension_factory *gcc_extension_factory::singleton;
148 :
149 : void
150 37 : handle_OPT_fdiagnostics_add_output_ (const gcc_options &opts,
151 : diagnostics::context &dc,
152 : const char *unparsed_spec,
153 : location_t loc)
154 : {
155 37 : gcc_assert (unparsed_spec);
156 :
157 37 : const char *const option_name = "-fdiagnostics-add-output=";
158 37 : DIAGNOSTICS_LOG_SCOPE_PRINTF2 (dc.get_logger (),
159 37 : "handling: %s%s", option_name, unparsed_spec);
160 37 : if (auto sink = try_to_make_sink (opts, dc, option_name, unparsed_spec, loc))
161 37 : dc.add_sink (std::move (sink));
162 37 : }
163 :
164 : void
165 14 : handle_OPT_fdiagnostics_set_output_ (const gcc_options &opts,
166 : diagnostics::context &dc,
167 : const char *unparsed_spec,
168 : location_t loc)
169 : {
170 14 : gcc_assert (unparsed_spec);
171 :
172 14 : const char *const option_name = "-fdiagnostics-set-output=";
173 14 : DIAGNOSTICS_LOG_SCOPE_PRINTF2 (dc.get_logger (),
174 14 : "handling: %s%s", option_name, unparsed_spec);
175 14 : if (auto sink = try_to_make_sink (opts, dc, option_name, unparsed_spec, loc))
176 14 : dc.set_sink (std::move (sink));
177 14 : }
178 :
179 : /* Return the base name to use when choosing names for output file for
180 : diagnostic sinks (e.g. BASENAME.sarif or BASENAME.html). */
181 :
182 : const char *
183 127 : get_diagnostic_file_output_basename (const gcc_options &opts)
184 : {
185 : /* This might have been called before finish_options, which prepends
186 : the dump dir to the dump base name. If so, make a prepended copy
187 : now and use it. */
188 127 : if (opts.x_dump_base_name
189 119 : && ! opts.x_dump_base_name_prefixed)
190 238 : if (const char *prepended_dump_base_name
191 119 : = maybe_prepend_dump_dir_name (opts))
192 : /* Allocated in opts_obstack. */
193 : return prepended_dump_base_name;
194 :
195 127 : return (opts.x_dump_base_name
196 : ? opts.x_dump_base_name
197 127 : : opts.x_main_input_basename);
198 : }
|