[PATCH] RISC-V: Move UNSPEC_SSP_SET and UNSPEC_SSP_TEST to correct enum
[gcc.git] / gcc / sarif-replay.cc
blob1523d875e908eaa2e72a8754e21abdd4e0f631a0
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 <dmalcolm@redhat.com>.
5 This file is part of GCC.
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.
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.
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/>. */
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"
30 static const char *progname;
32 static void
33 set_defaults (replay_options &replay_opts)
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;
42 struct options
44 options ()
46 set_defaults (m_replay_opts);
49 replay_options m_replay_opts;
50 std::vector<const char *> m_sarif_filenames;
53 static void
54 print_version ()
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);
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");
92 static void
93 print_usage ()
95 fprintf (stderr, usage_msg);
98 static bool
99 parse_options (int argc, char **argv,
100 options &opts,
101 libgdiagnostics::text_sink control_text_sink)
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*/);
107 for (int i = 1; i < argc; ++i)
109 const char *option = argv[i];
110 bool handled = false;
111 if (strcmp (option, "-fjson-comments") == 0)
113 opts.m_replay_opts.m_json_comments = true;
114 handled = true;
116 else if (strcmp (option, "--verbose") == 0)
118 opts.m_replay_opts.m_verbose = true;
119 handled = true;
121 else if (strcmp (option, "--usage") == 0)
123 print_usage ();
124 exit (0);
126 else if (strcmp (option, "--echo-file") == 0)
128 opts.m_replay_opts.m_echo_file = true;
129 handled = true;
131 else if (strcmp (option, "-fdiagnostics-color=never") == 0)
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;
138 else if (strcmp (option, "-fdiagnostics-color=always") == 0)
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;
145 else if (strcmp (option, "-fdiagnostics-color=auto") == 0)
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;
153 else if (strcmp (option, "-v") == 0
154 || strcmp (option, "--version") == 0)
156 print_version ();
157 exit (0);
160 if (!handled)
162 if (option[0] == '-')
164 auto err = options_mgr.begin_diagnostic (DIAGNOSTIC_LEVEL_ERROR);
165 err.finish ("unrecognized option: %qs", option);
166 return false;
168 else
169 opts.m_sarif_filenames.push_back (option);
173 if (opts.m_sarif_filenames.size () == 0)
175 auto err = options_mgr.begin_diagnostic (DIAGNOSTIC_LEVEL_ERROR);
176 err.finish ("need at least one .sarif file to dump");
177 return false;
179 return true;
182 static const char *
183 get_progname (const char *argv0)
185 const char *p = argv0 + strlen (argv0);
186 while (p != argv0 && !IS_DIR_SEPARATOR (p[-1]))
187 --p;
188 return p;
191 /* Entrypoint to sarif-replay command-line tool. */
194 main (int argc, char **argv)
196 progname = get_progname (argv[0]);
197 xmalloc_set_program_name (progname);
199 libgdiagnostics::manager control_mgr;
201 control_mgr.set_tool_name (progname);
203 libgdiagnostics::text_sink control_text_sink
204 = control_mgr.add_text_sink (stderr, DIAGNOSTIC_COLORIZE_IF_TTY);
206 options opts;
207 if (!parse_options (argc, argv, opts, control_text_sink))
209 print_usage ();
210 return -1;
213 int failures = 0;
214 for (auto filename : opts.m_sarif_filenames)
216 if (opts.m_replay_opts.m_verbose)
218 auto note = control_mgr.begin_diagnostic (DIAGNOSTIC_LEVEL_NOTE);
219 note.finish ("about to replay %qs...", filename);
221 libgdiagnostics::manager playback_mgr;
222 playback_mgr.add_text_sink (stderr,
223 opts.m_replay_opts.m_diagnostics_colorize);
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;
232 if (opts.m_replay_opts.m_verbose)
234 auto note = control_mgr.begin_diagnostic (DIAGNOSTIC_LEVEL_NOTE);
235 note.finish ("...finished replaying %qs", filename);
238 return failures;