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
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
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/>. */
22 #define INCLUDE_VECTOR
24 #include "coretypes.h"
27 #include "libgdiagnostics++.h"
28 #include "libsarifreplay.h"
30 static const char *progname
;
33 set_defaults (replay_options
&replay_opts
)
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
;
46 set_defaults (m_replay_opts
);
49 replay_options m_replay_opts
;
50 std::vector
<const char *> m_sarif_filenames
;
56 printf (_("%s %s%s\n"), progname
, pkgversion_string
,
58 printf ("Copyright %s 2024-2025 Free Software Foundation, Inc.\n",
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"),
65 static const char *const usage_msg
= (
66 "sarif-replay [OPTIONS] FILE+\n"
68 " \"Replay\" results from one or more .sarif files as if they were\n"
73 " -fdiagnostics-color={never|always|auto}\n"
74 " Control colorization of diagnostics. Default: auto.\n"
77 " Support C and C++ style comments in .sarif JSON files\n"
80 " Print notes about each .sarif file before and after replaying it.\n"
83 " Print the filename and file contents to stderr before replaying it.\n"
86 " Print version and exit.\n"
89 " Print this message and exit.\n"
95 fprintf (stderr
, usage_msg
);
99 parse_options (int argc
, char **argv
,
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;
116 else if (strcmp (option
, "--verbose") == 0)
118 opts
.m_replay_opts
.m_verbose
= true;
121 else if (strcmp (option
, "--usage") == 0)
126 else if (strcmp (option
, "--echo-file") == 0)
128 opts
.m_replay_opts
.m_echo_file
= 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
);
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
);
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
);
153 else if (strcmp (option
, "-v") == 0
154 || strcmp (option
, "--version") == 0)
162 if (option
[0] == '-')
164 auto err
= options_mgr
.begin_diagnostic (DIAGNOSTIC_LEVEL_ERROR
);
165 err
.finish ("unrecognized option: %qs", option
);
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");
183 get_progname (const char *argv0
)
185 const char *p
= argv0
+ strlen (argv0
);
186 while (p
!= argv0
&& !IS_DIR_SEPARATOR (p
[-1]))
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
);
207 if (!parse_options (argc
, argv
, opts
, control_text_sink
))
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
,
228 &opts
.m_replay_opts
);
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
);