Branch data Line data Source code
1 : : /* Support for -fdiagnostics-add-output= and -fdiagnostics-set-output=.
2 : : Copyright (C) 2024-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 : :
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_STRING
28 : : #define INCLUDE_VECTOR
29 : : #include "system.h"
30 : : #include "coretypes.h"
31 : : #include "version.h"
32 : : #include "intl.h"
33 : : #include "diagnostic.h"
34 : : #include "diagnostics/output-spec.h"
35 : : #include "diagnostics/logging.h"
36 : : #include "opts.h"
37 : : #include "options.h"
38 : :
39 : : /* Decls. */
40 : :
41 : : namespace {
42 : :
43 : 37 : struct opt_spec_context : public diagnostics::output_spec::dc_spec_context
44 : : {
45 : : public:
46 : 37 : opt_spec_context (const gcc_options &opts,
47 : : diagnostics::context &dc,
48 : : line_maps *location_mgr,
49 : : location_t loc,
50 : : const char *option_name,
51 : : const char *option_value)
52 : 37 : : dc_spec_context (option_name,
53 : : option_value,
54 : : nullptr,
55 : : location_mgr,
56 : : dc,
57 : : location_mgr,
58 : : loc),
59 : 37 : m_opts (opts)
60 : : {}
61 : :
62 : : const char *
63 : 23 : get_base_filename () const final override
64 : : {
65 : 23 : return (m_opts.x_dump_base_name
66 : : ? m_opts.x_dump_base_name
67 : 23 : : m_opts.x_main_input_basename);
68 : : }
69 : :
70 : : const gcc_options &m_opts;
71 : : };
72 : :
73 : : } // anon namespace
74 : :
75 : : void
76 : 23 : handle_OPT_fdiagnostics_add_output_ (const gcc_options &opts,
77 : : diagnostics::context &dc,
78 : : const char *unparsed_spec,
79 : : location_t loc)
80 : : {
81 : 23 : gcc_assert (unparsed_spec);
82 : 23 : gcc_assert (line_table);
83 : :
84 : 23 : const char *const option_name = "-fdiagnostics-add-output=";
85 : 23 : DIAGNOSTICS_LOG_SCOPE_PRINTF2 (dc.get_logger (),
86 : 23 : "handling: %s%s", option_name, unparsed_spec);
87 : 23 : opt_spec_context ctxt (opts, dc, line_table, loc, option_name, unparsed_spec);
88 : 23 : auto sink = ctxt.parse_and_make_sink (dc);
89 : 23 : if (!sink)
90 : 0 : return;
91 : :
92 : 23 : sink->set_main_input_filename (opts.x_main_input_filename);
93 : 23 : dc.add_sink (std::move (sink));
94 : 23 : }
95 : :
96 : : void
97 : 14 : handle_OPT_fdiagnostics_set_output_ (const gcc_options &opts,
98 : : diagnostics::context &dc,
99 : : const char *unparsed_spec,
100 : : location_t loc)
101 : : {
102 : 14 : gcc_assert (unparsed_spec);
103 : 14 : gcc_assert (line_table);
104 : :
105 : 14 : const char *const option_name = "-fdiagnostics-set-output=";
106 : 14 : DIAGNOSTICS_LOG_SCOPE_PRINTF2 (dc.get_logger (),
107 : 14 : "handling: %s%s", option_name, unparsed_spec);
108 : 14 : opt_spec_context ctxt (opts, dc, line_table, loc, option_name, unparsed_spec);
109 : 14 : auto sink = ctxt.parse_and_make_sink (dc);
110 : 14 : if (!sink)
111 : 0 : return;
112 : :
113 : 14 : sink->set_main_input_filename (opts.x_main_input_filename);
114 : 14 : dc.set_sink (std::move (sink));
115 : 14 : }
|