MATCH: Improve `A CMP 0 ? A : -A` set of patterns to use bitwise_equal_p.
[official-gcc.git] / gcc / diagnostic-format-json.cc
blob346abb31ac4a5f4f566511f451de9a854f141066
1 /* JSON output for diagnostics
2 Copyright (C) 2018-2023 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/>. */
22 #include "config.h"
23 #include "system.h"
24 #include "coretypes.h"
25 #include "diagnostic.h"
26 #include "selftest-diagnostic.h"
27 #include "diagnostic-metadata.h"
28 #include "json.h"
29 #include "selftest.h"
31 /* Subclass of diagnostic_output_format for JSON output. */
33 class json_output_format : public diagnostic_output_format
35 public:
36 void on_begin_group () final override
38 /* No-op. */
40 void on_end_group () final override
42 m_cur_group = nullptr;
43 m_cur_children_array = nullptr;
45 void
46 on_begin_diagnostic (diagnostic_info *) final override
48 /* No-op. */
50 void
51 on_end_diagnostic (diagnostic_info *diagnostic,
52 diagnostic_t orig_diag_kind) final override;
53 void on_diagram (const diagnostic_diagram &) final override
55 /* No-op. */
58 protected:
59 json_output_format (diagnostic_context &context)
60 : diagnostic_output_format (context),
61 m_toplevel_array (new json::array ()),
62 m_cur_group (nullptr),
63 m_cur_children_array (nullptr)
67 /* Flush the top-level array to OUTF. */
68 void
69 flush_to_file (FILE *outf)
71 m_toplevel_array->dump (outf);
72 fprintf (outf, "\n");
73 delete m_toplevel_array;
74 m_toplevel_array = nullptr;
77 private:
78 /* The top-level JSON array of pending diagnostics. */
79 json::array *m_toplevel_array;
81 /* The JSON object for the current diagnostic group. */
82 json::object *m_cur_group;
84 /* The JSON array for the "children" array within the current diagnostic
85 group. */
86 json::array *m_cur_children_array;
89 /* Generate a JSON object for LOC. */
91 json::value *
92 json_from_expanded_location (diagnostic_context *context, location_t loc)
94 expanded_location exploc = expand_location (loc);
95 json::object *result = new json::object ();
96 if (exploc.file)
97 result->set ("file", new json::string (exploc.file));
98 result->set ("line", new json::integer_number (exploc.line));
100 const enum diagnostics_column_unit orig_unit = context->column_unit;
101 struct
103 const char *name;
104 enum diagnostics_column_unit unit;
105 } column_fields[] = {
106 {"display-column", DIAGNOSTICS_COLUMN_UNIT_DISPLAY},
107 {"byte-column", DIAGNOSTICS_COLUMN_UNIT_BYTE}
109 int the_column = INT_MIN;
110 for (int i = 0; i != ARRAY_SIZE (column_fields); ++i)
112 context->column_unit = column_fields[i].unit;
113 const int col = diagnostic_converted_column (context, exploc);
114 result->set (column_fields[i].name, new json::integer_number (col));
115 if (column_fields[i].unit == orig_unit)
116 the_column = col;
118 gcc_assert (the_column != INT_MIN);
119 result->set ("column", new json::integer_number (the_column));
120 context->column_unit = orig_unit;
121 return result;
124 /* Generate a JSON object for LOC_RANGE. */
126 static json::object *
127 json_from_location_range (diagnostic_context *context,
128 const location_range *loc_range, unsigned range_idx)
130 location_t caret_loc = get_pure_location (loc_range->m_loc);
132 if (caret_loc == UNKNOWN_LOCATION)
133 return NULL;
135 location_t start_loc = get_start (loc_range->m_loc);
136 location_t finish_loc = get_finish (loc_range->m_loc);
138 json::object *result = new json::object ();
139 result->set ("caret", json_from_expanded_location (context, caret_loc));
140 if (start_loc != caret_loc
141 && start_loc != UNKNOWN_LOCATION)
142 result->set ("start", json_from_expanded_location (context, start_loc));
143 if (finish_loc != caret_loc
144 && finish_loc != UNKNOWN_LOCATION)
145 result->set ("finish", json_from_expanded_location (context, finish_loc));
147 if (loc_range->m_label)
149 label_text text (loc_range->m_label->get_text (range_idx));
150 if (text.get ())
151 result->set ("label", new json::string (text.get ()));
154 return result;
157 /* Generate a JSON object for HINT. */
159 static json::object *
160 json_from_fixit_hint (diagnostic_context *context, const fixit_hint *hint)
162 json::object *fixit_obj = new json::object ();
164 location_t start_loc = hint->get_start_loc ();
165 fixit_obj->set ("start", json_from_expanded_location (context, start_loc));
166 location_t next_loc = hint->get_next_loc ();
167 fixit_obj->set ("next", json_from_expanded_location (context, next_loc));
168 fixit_obj->set ("string", new json::string (hint->get_string ()));
170 return fixit_obj;
173 /* Generate a JSON object for METADATA. */
175 static json::object *
176 json_from_metadata (const diagnostic_metadata *metadata)
178 json::object *metadata_obj = new json::object ();
180 if (metadata->get_cwe ())
181 metadata_obj->set ("cwe",
182 new json::integer_number (metadata->get_cwe ()));
184 return metadata_obj;
187 /* Implementation of "on_end_diagnostic" vfunc for JSON output.
188 Generate a JSON object for DIAGNOSTIC, and store for output
189 within current diagnostic group. */
191 void
192 json_output_format::on_end_diagnostic (diagnostic_info *diagnostic,
193 diagnostic_t orig_diag_kind)
195 json::object *diag_obj = new json::object ();
197 /* Get "kind" of diagnostic. */
199 static const char *const diagnostic_kind_text[] = {
200 #define DEFINE_DIAGNOSTIC_KIND(K, T, C) (T),
201 #include "diagnostic.def"
202 #undef DEFINE_DIAGNOSTIC_KIND
203 "must-not-happen"
205 /* Lose the trailing ": ". */
206 const char *kind_text = diagnostic_kind_text[diagnostic->kind];
207 size_t len = strlen (kind_text);
208 gcc_assert (len > 2);
209 gcc_assert (kind_text[len - 2] == ':');
210 gcc_assert (kind_text[len - 1] == ' ');
211 char *rstrip = xstrdup (kind_text);
212 rstrip[len - 2] = '\0';
213 diag_obj->set ("kind", new json::string (rstrip));
214 free (rstrip);
217 // FIXME: encoding of the message (json::string requires UTF-8)
218 diag_obj->set ("message",
219 new json::string (pp_formatted_text (m_context.printer)));
220 pp_clear_output_area (m_context.printer);
222 char *option_text;
223 option_text = m_context.option_name (&m_context, diagnostic->option_index,
224 orig_diag_kind, diagnostic->kind);
225 if (option_text)
227 diag_obj->set ("option", new json::string (option_text));
228 free (option_text);
231 if (m_context.get_option_url)
233 char *option_url = m_context.get_option_url (&m_context,
234 diagnostic->option_index);
235 if (option_url)
237 diag_obj->set ("option_url", new json::string (option_url));
238 free (option_url);
242 /* If we've already emitted a diagnostic within this auto_diagnostic_group,
243 then add diag_obj to its "children" array. */
244 if (m_cur_group)
246 gcc_assert (m_cur_children_array);
247 m_cur_children_array->append (diag_obj);
249 else
251 /* Otherwise, make diag_obj be the top-level object within the group;
252 add a "children" array and record the column origin. */
253 m_toplevel_array->append (diag_obj);
254 m_cur_group = diag_obj;
255 m_cur_children_array = new json::array ();
256 diag_obj->set ("children", m_cur_children_array);
257 diag_obj->set ("column-origin",
258 new json::integer_number (m_context.column_origin));
261 const rich_location *richloc = diagnostic->richloc;
263 json::array *loc_array = new json::array ();
264 diag_obj->set ("locations", loc_array);
266 for (unsigned int i = 0; i < richloc->get_num_locations (); i++)
268 const location_range *loc_range = richloc->get_range (i);
269 json::object *loc_obj
270 = json_from_location_range (&m_context, loc_range, i);
271 if (loc_obj)
272 loc_array->append (loc_obj);
275 if (richloc->get_num_fixit_hints ())
277 json::array *fixit_array = new json::array ();
278 diag_obj->set ("fixits", fixit_array);
279 for (unsigned int i = 0; i < richloc->get_num_fixit_hints (); i++)
281 const fixit_hint *hint = richloc->get_fixit_hint (i);
282 json::object *fixit_obj = json_from_fixit_hint (&m_context, hint);
283 fixit_array->append (fixit_obj);
287 /* TODO: tree-ish things:
288 TODO: functions
289 TODO: inlining information
290 TODO: macro expansion information. */
292 if (diagnostic->metadata)
294 json::object *metadata_obj = json_from_metadata (diagnostic->metadata);
295 diag_obj->set ("metadata", metadata_obj);
298 const diagnostic_path *path = richloc->get_path ();
299 if (path && m_context.make_json_for_path)
301 json::value *path_value = m_context.make_json_for_path (&m_context, path);
302 diag_obj->set ("path", path_value);
305 diag_obj->set ("escape-source",
306 new json::literal (richloc->escape_on_output_p ()));
309 class json_stderr_output_format : public json_output_format
311 public:
312 json_stderr_output_format (diagnostic_context &context)
313 : json_output_format (context)
316 ~json_stderr_output_format ()
318 flush_to_file (stderr);
322 class json_file_output_format : public json_output_format
324 public:
325 json_file_output_format (diagnostic_context &context,
326 const char *base_file_name)
327 : json_output_format (context),
328 m_base_file_name (xstrdup (base_file_name))
332 ~json_file_output_format ()
334 char *filename = concat (m_base_file_name, ".gcc.json", NULL);
335 free (m_base_file_name);
336 m_base_file_name = nullptr;
337 FILE *outf = fopen (filename, "w");
338 if (!outf)
340 const char *errstr = xstrerror (errno);
341 fnotice (stderr, "error: unable to open '%s' for writing: %s\n",
342 filename, errstr);
343 free (filename);
344 return;
346 flush_to_file (outf);
347 fclose (outf);
348 free (filename);
351 private:
352 char *m_base_file_name;
355 /* Populate CONTEXT in preparation for JSON output (either to stderr, or
356 to a file). */
358 static void
359 diagnostic_output_format_init_json (diagnostic_context *context)
361 /* Override callbacks. */
362 context->print_path = NULL; /* handled in json_end_diagnostic. */
364 /* The metadata is handled in JSON format, rather than as text. */
365 context->show_cwe = false;
366 context->show_rules = false;
368 /* The option is handled in JSON format, rather than as text. */
369 context->show_option_requested = false;
371 /* Don't colorize the text. */
372 pp_show_color (context->printer) = false;
375 /* Populate CONTEXT in preparation for JSON output to stderr. */
377 void
378 diagnostic_output_format_init_json_stderr (diagnostic_context *context)
380 diagnostic_output_format_init_json (context);
381 delete context->m_output_format;
382 context->m_output_format = new json_stderr_output_format (*context);
385 /* Populate CONTEXT in preparation for JSON output to a file named
386 BASE_FILE_NAME.gcc.json. */
388 void
389 diagnostic_output_format_init_json_file (diagnostic_context *context,
390 const char *base_file_name)
392 diagnostic_output_format_init_json (context);
393 delete context->m_output_format;
394 context->m_output_format = new json_file_output_format (*context,
395 base_file_name);
398 #if CHECKING_P
400 namespace selftest {
402 /* We shouldn't call json_from_expanded_location on UNKNOWN_LOCATION,
403 but verify that we handle this gracefully. */
405 static void
406 test_unknown_location ()
408 test_diagnostic_context dc;
409 delete json_from_expanded_location (&dc, UNKNOWN_LOCATION);
412 /* Verify that we gracefully handle attempts to serialize bad
413 compound locations. */
415 static void
416 test_bad_endpoints ()
418 location_t bad_endpoints
419 = make_location (BUILTINS_LOCATION,
420 UNKNOWN_LOCATION, UNKNOWN_LOCATION);
422 location_range loc_range;
423 loc_range.m_loc = bad_endpoints;
424 loc_range.m_range_display_kind = SHOW_RANGE_WITH_CARET;
425 loc_range.m_label = NULL;
427 test_diagnostic_context dc;
428 json::object *obj = json_from_location_range (&dc, &loc_range, 0);
429 /* We should have a "caret" value, but no "start" or "finish" values. */
430 ASSERT_TRUE (obj != NULL);
431 ASSERT_TRUE (obj->get ("caret") != NULL);
432 ASSERT_TRUE (obj->get ("start") == NULL);
433 ASSERT_TRUE (obj->get ("finish") == NULL);
434 delete obj;
437 /* Run all of the selftests within this file. */
439 void
440 diagnostic_format_json_cc_tests ()
442 test_unknown_location ();
443 test_bad_endpoints ();
446 } // namespace selftest
448 #endif /* #if CHECKING_P */