Debian Codegraph @

gcc/sarif-replay.cc

raw
1 /* A program for re-emitting diagnostics saved in SARIF form.
2 Copyright (C) 2022-2025 Free Software Foundation, Inc.
3 Contributed by David Malcolm <[email protected]>.
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 #include "config.h"
22 #define INCLUDE_VECTOR
23 #include "system.h"
24 #include "coretypes.h"
25 #include "version.h"
26 #include "intl.h"
27 #include "libgdiagnostics++.h"
28 #include "libsarifreplay.h"
29
30 static const char *progname;
31
32 static void
33 set_defaults (replay_options &replay_opts)
34 {
35 /* Defaults. */
36 replay_opts.m_echo_file = false;
37 replay_opts.m_json_comments = false;
38 replay_opts.m_verbose = false;
39 replay_opts.m_diagnostics_colorize = DIAGNOSTIC_COLORIZE_IF_TTY;
40 }
41
42 struct options
43 {
44 options ()
45 {
46 set_defaults (m_replay_opts);
47 }
48
49 replay_options m_replay_opts;
50 std::vector<const char *> m_sarif_filenames;
51 };
52
53 static void
54 print_version ()
55 {
56 printf (_("%s %s%s\n"), progname, pkgversion_string,
57 version_string);
58 printf ("Copyright %s 2024-2025 Free Software Foundation, Inc.\n",
59 _("(C)"));
60 fputs (_("This is free software; see the source for copying conditions. There is NO\n\
61 warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n\n"),
62 stdout);
63 }
64
65 static const char *const usage_msg = (
66 "sarif-replay [OPTIONS] FILE+\n"
67 "\n"
68 " \"Replay\" results from one or more .sarif files as if they were\n"
69 " GCC diagnostics\n"
70 "\n"
71 "Options:\n"
72 "\n"
73 " -fdiagnostics-color={never|always|auto}\n"
74 " Control colorization of diagnostics. Default: auto.\n"
75 "\n"
76 " -fjson-comments\n"
77 " Support C and C++ style comments in .sarif JSON files\n"
78 "\n"
79 " --verbose\n"
80 " Print notes about each .sarif file before and after replaying it.\n"
81 "\n"
82 " --echo-file\n"
83 " Print the filename and file contents to stderr before replaying it.\n"
84 "\n"
85 " -v, --version\n"
86 " Print version and exit.\n"
87 "\n"
88 " --usage\n"
89 " Print this message and exit.\n"
90 "\n");
91
92 static void
93 print_usage ()
94 {
95 fprintf (stderr, usage_msg);
96 }
97
98 static bool
99 parse_options (int argc, char **argv,
100 options &opts,
101 libgdiagnostics::text_sink control_text_sink)
102 {
103 libgdiagnostics::manager options_mgr;
104 options_mgr.set_tool_name ("sarif-replay");
105 options_mgr.add_text_sink (stderr, DIAGNOSTIC_COLORIZE_NO/*IF_TTY*/);
106
107 for (int i = 1; i < argc; ++i)
108 {
109 const char *option = argv[i];
110 bool handled = false;
111 if (strcmp (option, "-fjson-comments") == 0)
112 {
113 opts.m_replay_opts.m_json_comments = true;
114 handled = true;
115 }
116 else if (strcmp (option, "--verbose") == 0)
117 {
118 opts.m_replay_opts.m_verbose = true;
119 handled = true;
120 }
121 else if (strcmp (option, "--usage") == 0)
122 {
123 print_usage ();
124 exit (0);
125 }
126 else if (strcmp (option, "--echo-file") == 0)
127 {
128 opts.m_replay_opts.m_echo_file = true;
129 handled = true;
130 }
131 else if (strcmp (option, "-fdiagnostics-color=never") == 0)
132 {
133 opts.m_replay_opts.m_diagnostics_colorize = DIAGNOSTIC_COLORIZE_NO;
134 control_text_sink.set_colorize
135 (opts.m_replay_opts.m_diagnostics_colorize);
136 handled = true;
137 }
138 else if (strcmp (option, "-fdiagnostics-color=always") == 0)
139 {
140 opts.m_replay_opts.m_diagnostics_colorize = DIAGNOSTIC_COLORIZE_YES;
141 control_text_sink.set_colorize
142 (opts.m_replay_opts.m_diagnostics_colorize);
143 handled = true;
144 }
145 else if (strcmp (option, "-fdiagnostics-color=auto") == 0)
146 {
147 opts.m_replay_opts.m_diagnostics_colorize
148 = DIAGNOSTIC_COLORIZE_IF_TTY;
149 control_text_sink.set_colorize
150 (opts.m_replay_opts.m_diagnostics_colorize);
151 handled = true;
152 }
153 else if (strcmp (option, "-v") == 0
154 || strcmp (option, "--version") == 0)
155 {
156 print_version ();
157 exit (0);
158 }
159
160 if (!handled)
161 {
162 if (option[0] == '-')
163 {
164 auto err = options_mgr.begin_diagnostic (DIAGNOSTIC_LEVEL_ERROR);
165 err.finish ("unrecognized option: %qs", option);
166 return false;
167 }
168 else
169 opts.m_sarif_filenames.push_back (option);
170 }
171 }
172
173 if (opts.m_sarif_filenames.size () == 0)
174 {
175 auto err = options_mgr.begin_diagnostic (DIAGNOSTIC_LEVEL_ERROR);
176 err.finish ("need at least one .sarif file to dump");
177 return false;
178 }
179 return true;
180 }
181
182 static const char *
183 get_progname (const char *argv0)
184 {
185 const char *p = argv0 + strlen (argv0);
186 while (p != argv0 && !IS_DIR_SEPARATOR (p[-1]))
187 --p;
188 return p;
189 }
190
191 /* Entrypoint to sarif-replay command-line tool. */
192
193 int
194 main (int argc, char **argv)
195 {
196 progname = get_progname (argv[0]);
197 xmalloc_set_program_name (progname);
198
199 libgdiagnostics::manager control_mgr;
200
201 control_mgr.set_tool_name (progname);
202
203 libgdiagnostics::text_sink control_text_sink
204 = control_mgr.add_text_sink (stderr, DIAGNOSTIC_COLORIZE_IF_TTY);
205
206 options opts;
207 if (!parse_options (argc, argv, opts, control_text_sink))
208 {
209 print_usage ();
210 return -1;
211 }
212
213 int failures = 0;
214 for (auto filename : opts.m_sarif_filenames)
215 {
216 if (opts.m_replay_opts.m_verbose)
217 {
218 auto note = control_mgr.begin_diagnostic (DIAGNOSTIC_LEVEL_NOTE);
219 note.finish ("about to replay %qs...", filename);
220 }
221 libgdiagnostics::manager playback_mgr;
222 playback_mgr.add_text_sink (stderr,
223 opts.m_replay_opts.m_diagnostics_colorize);
224
225 int result = sarif_replay_path (filename,
226 playback_mgr.m_inner,
227 control_mgr.m_inner,
228 &opts.m_replay_opts);
229 if (result)
230 ++failures;
231
232 if (opts.m_replay_opts.m_verbose)
233 {
234 auto note = control_mgr.begin_diagnostic (DIAGNOSTIC_LEVEL_NOTE);
235 note.finish ("...finished replaying %qs", filename);
236 }
237 }
238 return failures;
239 }