]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/diagnostic-format-json.cc
diagnostics: add ability to associate diagnostics with rules from coding standards
[thirdparty/gcc.git] / gcc / diagnostic-format-json.cc
CommitLineData
478dd60d 1/* JSON output for diagnostics
7adcbafe 2 Copyright (C) 2018-2022 Free Software Foundation, Inc.
478dd60d
DM
3 Contributed by David Malcolm <dmalcolm@redhat.com>.
4
5This file is part of GCC.
6
7GCC is free software; you can redistribute it and/or modify it under
8the terms of the GNU General Public License as published by the Free
9Software Foundation; either version 3, or (at your option) any later
10version.
11
12GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13WARRANTY; without even the implied warranty of MERCHANTABILITY or
14FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15for more details.
16
17You should have received a copy of the GNU General Public License
18along with GCC; see the file COPYING3. If not see
19<http://www.gnu.org/licenses/>. */
20
21
22#include "config.h"
23#include "system.h"
24#include "coretypes.h"
25#include "diagnostic.h"
004bb936 26#include "selftest-diagnostic.h"
6d4a35ca 27#include "diagnostic-metadata.h"
478dd60d 28#include "json.h"
30d3ba51 29#include "selftest.h"
478dd60d
DM
30
31/* The top-level JSON array of pending diagnostics. */
32
33static json::array *toplevel_array;
34
35/* The JSON object for the current diagnostic group. */
36
37static json::object *cur_group;
38
39/* The JSON array for the "children" array within the current diagnostic
40 group. */
41
42static json::array *cur_children_array;
43
44/* Generate a JSON object for LOC. */
45
4bc1899b 46json::value *
004bb936 47json_from_expanded_location (diagnostic_context *context, location_t loc)
478dd60d
DM
48{
49 expanded_location exploc = expand_location (loc);
50 json::object *result = new json::object ();
30d3ba51
DM
51 if (exploc.file)
52 result->set ("file", new json::string (exploc.file));
07622278 53 result->set ("line", new json::integer_number (exploc.line));
004bb936
LH
54
55 const enum diagnostics_column_unit orig_unit = context->column_unit;
56 struct
57 {
58 const char *name;
59 enum diagnostics_column_unit unit;
60 } column_fields[] = {
61 {"display-column", DIAGNOSTICS_COLUMN_UNIT_DISPLAY},
62 {"byte-column", DIAGNOSTICS_COLUMN_UNIT_BYTE}
63 };
64 int the_column = INT_MIN;
ca32b29e 65 for (int i = 0; i != ARRAY_SIZE (column_fields); ++i)
004bb936
LH
66 {
67 context->column_unit = column_fields[i].unit;
68 const int col = diagnostic_converted_column (context, exploc);
69 result->set (column_fields[i].name, new json::integer_number (col));
70 if (column_fields[i].unit == orig_unit)
71 the_column = col;
72 }
73 gcc_assert (the_column != INT_MIN);
74 result->set ("column", new json::integer_number (the_column));
75 context->column_unit = orig_unit;
478dd60d
DM
76 return result;
77}
78
79/* Generate a JSON object for LOC_RANGE. */
80
81static json::object *
004bb936
LH
82json_from_location_range (diagnostic_context *context,
83 const location_range *loc_range, unsigned range_idx)
478dd60d
DM
84{
85 location_t caret_loc = get_pure_location (loc_range->m_loc);
86
87 if (caret_loc == UNKNOWN_LOCATION)
88 return NULL;
89
90 location_t start_loc = get_start (loc_range->m_loc);
91 location_t finish_loc = get_finish (loc_range->m_loc);
92
93 json::object *result = new json::object ();
004bb936 94 result->set ("caret", json_from_expanded_location (context, caret_loc));
30d3ba51
DM
95 if (start_loc != caret_loc
96 && start_loc != UNKNOWN_LOCATION)
004bb936 97 result->set ("start", json_from_expanded_location (context, start_loc));
30d3ba51
DM
98 if (finish_loc != caret_loc
99 && finish_loc != UNKNOWN_LOCATION)
004bb936 100 result->set ("finish", json_from_expanded_location (context, finish_loc));
478dd60d
DM
101
102 if (loc_range->m_label)
103 {
104 label_text text;
105 text = loc_range->m_label->get_text (range_idx);
106 if (text.m_buffer)
107 result->set ("label", new json::string (text.m_buffer));
108 text.maybe_free ();
109 }
110
111 return result;
112}
113
114/* Generate a JSON object for HINT. */
115
116static json::object *
004bb936 117json_from_fixit_hint (diagnostic_context *context, const fixit_hint *hint)
478dd60d
DM
118{
119 json::object *fixit_obj = new json::object ();
120
121 location_t start_loc = hint->get_start_loc ();
004bb936 122 fixit_obj->set ("start", json_from_expanded_location (context, start_loc));
478dd60d 123 location_t next_loc = hint->get_next_loc ();
004bb936 124 fixit_obj->set ("next", json_from_expanded_location (context, next_loc));
478dd60d
DM
125 fixit_obj->set ("string", new json::string (hint->get_string ()));
126
127 return fixit_obj;
128}
129
6d4a35ca
DM
130/* Generate a JSON object for METADATA. */
131
132static json::object *
133json_from_metadata (const diagnostic_metadata *metadata)
134{
135 json::object *metadata_obj = new json::object ();
136
137 if (metadata->get_cwe ())
138 metadata_obj->set ("cwe",
139 new json::integer_number (metadata->get_cwe ()));
140
141 return metadata_obj;
142}
143
478dd60d
DM
144/* No-op implementation of "begin_diagnostic" for JSON output. */
145
146static void
147json_begin_diagnostic (diagnostic_context *, diagnostic_info *)
148{
149}
150
151/* Implementation of "end_diagnostic" for JSON output.
152 Generate a JSON object for DIAGNOSTIC, and store for output
153 within current diagnostic group. */
154
155static void
156json_end_diagnostic (diagnostic_context *context, diagnostic_info *diagnostic,
157 diagnostic_t orig_diag_kind)
158{
159 json::object *diag_obj = new json::object ();
160
161 /* Get "kind" of diagnostic. */
162 {
163 static const char *const diagnostic_kind_text[] = {
164#define DEFINE_DIAGNOSTIC_KIND(K, T, C) (T),
165#include "diagnostic.def"
166#undef DEFINE_DIAGNOSTIC_KIND
167 "must-not-happen"
168 };
169 /* Lose the trailing ": ". */
170 const char *kind_text = diagnostic_kind_text[diagnostic->kind];
171 size_t len = strlen (kind_text);
172 gcc_assert (len > 2);
173 gcc_assert (kind_text[len - 2] == ':');
174 gcc_assert (kind_text[len - 1] == ' ');
175 char *rstrip = xstrdup (kind_text);
176 rstrip[len - 2] = '\0';
177 diag_obj->set ("kind", new json::string (rstrip));
178 free (rstrip);
179 }
180
181 // FIXME: encoding of the message (json::string requires UTF-8)
182 diag_obj->set ("message",
183 new json::string (pp_formatted_text (context->printer)));
184 pp_clear_output_area (context->printer);
185
186 char *option_text;
187 option_text = context->option_name (context, diagnostic->option_index,
188 orig_diag_kind, diagnostic->kind);
189 if (option_text)
190 {
191 diag_obj->set ("option", new json::string (option_text));
192 free (option_text);
193 }
194
b4c7ca2e
DM
195 if (context->get_option_url)
196 {
197 char *option_url = context->get_option_url (context,
198 diagnostic->option_index);
199 if (option_url)
200 {
201 diag_obj->set ("option_url", new json::string (option_url));
202 free (option_url);
203 }
204 }
205
478dd60d
DM
206 /* If we've already emitted a diagnostic within this auto_diagnostic_group,
207 then add diag_obj to its "children" array. */
208 if (cur_group)
209 {
210 gcc_assert (cur_children_array);
211 cur_children_array->append (diag_obj);
212 }
213 else
214 {
215 /* Otherwise, make diag_obj be the top-level object within the group;
004bb936 216 add a "children" array and record the column origin. */
478dd60d
DM
217 toplevel_array->append (diag_obj);
218 cur_group = diag_obj;
219 cur_children_array = new json::array ();
220 diag_obj->set ("children", cur_children_array);
004bb936
LH
221 diag_obj->set ("column-origin",
222 new json::integer_number (context->column_origin));
478dd60d
DM
223 }
224
225 const rich_location *richloc = diagnostic->richloc;
226
227 json::array *loc_array = new json::array ();
228 diag_obj->set ("locations", loc_array);
229
230 for (unsigned int i = 0; i < richloc->get_num_locations (); i++)
231 {
232 const location_range *loc_range = richloc->get_range (i);
004bb936 233 json::object *loc_obj = json_from_location_range (context, loc_range, i);
478dd60d
DM
234 if (loc_obj)
235 loc_array->append (loc_obj);
236 }
237
238 if (richloc->get_num_fixit_hints ())
239 {
240 json::array *fixit_array = new json::array ();
241 diag_obj->set ("fixits", fixit_array);
242 for (unsigned int i = 0; i < richloc->get_num_fixit_hints (); i++)
243 {
244 const fixit_hint *hint = richloc->get_fixit_hint (i);
004bb936 245 json::object *fixit_obj = json_from_fixit_hint (context, hint);
478dd60d
DM
246 fixit_array->append (fixit_obj);
247 }
248 }
249
250 /* TODO: tree-ish things:
251 TODO: functions
252 TODO: inlining information
253 TODO: macro expansion information. */
6d4a35ca
DM
254
255 if (diagnostic->metadata)
256 {
257 json::object *metadata_obj = json_from_metadata (diagnostic->metadata);
258 diag_obj->set ("metadata", metadata_obj);
259 }
4bc1899b
DM
260
261 const diagnostic_path *path = richloc->get_path ();
262 if (path && context->make_json_for_path)
263 {
264 json::value *path_value = context->make_json_for_path (context, path);
265 diag_obj->set ("path", path_value);
266 }
bd5e882c
DM
267
268 diag_obj->set ("escape-source",
269 new json::literal (richloc->escape_on_output_p ()));
478dd60d
DM
270}
271
272/* No-op implementation of "begin_group_cb" for JSON output. */
273
274static void
275json_begin_group (diagnostic_context *)
276{
277}
278
279/* Implementation of "end_group_cb" for JSON output. */
280
281static void
282json_end_group (diagnostic_context *)
283{
284 cur_group = NULL;
285 cur_children_array = NULL;
286}
287
5ab73173 288/* Flush the top-level array to OUTF. */
478dd60d
DM
289
290static void
5ab73173 291json_flush_to_file (FILE *outf)
478dd60d 292{
5ab73173
DM
293 toplevel_array->dump (outf);
294 fprintf (outf, "\n");
478dd60d
DM
295 delete toplevel_array;
296 toplevel_array = NULL;
297}
298
5ab73173 299/* Callback for final cleanup for JSON output to stderr. */
478dd60d 300
5ab73173
DM
301static void
302json_stderr_final_cb (diagnostic_context *)
303{
304 json_flush_to_file (stderr);
305}
306
307static char *json_output_base_file_name;
308
309/* Callback for final cleanup for JSON output to a file. */
310
311static void
312json_file_final_cb (diagnostic_context *)
478dd60d 313{
5ab73173
DM
314 char *filename = concat (json_output_base_file_name, ".gcc.json", NULL);
315 FILE *outf = fopen (filename, "w");
316 if (!outf)
478dd60d 317 {
5ab73173
DM
318 const char *errstr = xstrerror (errno);
319 fnotice (stderr, "error: unable to open '%s' for writing: %s\n",
320 filename, errstr);
321 free (filename);
322 return;
478dd60d 323 }
5ab73173
DM
324 json_flush_to_file (outf);
325 fclose (outf);
326 free (filename);
327}
328
329/* Populate CONTEXT in preparation for JSON output (either to stderr, or
330 to a file). */
331
332static void
333diagnostic_output_format_init_json (diagnostic_context *context)
334{
335 /* Set up top-level JSON array. */
336 if (toplevel_array == NULL)
337 toplevel_array = new json::array ();
338
339 /* Override callbacks. */
340 context->begin_diagnostic = json_begin_diagnostic;
341 context->end_diagnostic = json_end_diagnostic;
342 context->begin_group_cb = json_begin_group;
343 context->end_group_cb = json_end_group;
344 context->print_path = NULL; /* handled in json_end_diagnostic. */
345
346 /* The metadata is handled in JSON format, rather than as text. */
347 context->show_cwe = false;
0b14f590 348 context->show_rules = false;
5ab73173
DM
349
350 /* The option is handled in JSON format, rather than as text. */
351 context->show_option_requested = false;
352
353 /* Don't colorize the text. */
354 pp_show_color (context->printer) = false;
355}
356
357/* Populate CONTEXT in preparation for JSON output to stderr. */
358
359void
360diagnostic_output_format_init_json_stderr (diagnostic_context *context)
361{
362 diagnostic_output_format_init_json (context);
363 context->final_cb = json_stderr_final_cb;
364}
365
366/* Populate CONTEXT in preparation for JSON output to a file named
367 BASE_FILE_NAME.gcc.json. */
368
369void
370diagnostic_output_format_init_json_file (diagnostic_context *context,
371 const char *base_file_name)
372{
373 diagnostic_output_format_init_json (context);
374 context->final_cb = json_file_final_cb;
375 json_output_base_file_name = xstrdup (base_file_name);
478dd60d 376}
30d3ba51
DM
377
378#if CHECKING_P
379
380namespace selftest {
381
382/* We shouldn't call json_from_expanded_location on UNKNOWN_LOCATION,
383 but verify that we handle this gracefully. */
384
385static void
386test_unknown_location ()
387{
004bb936
LH
388 test_diagnostic_context dc;
389 delete json_from_expanded_location (&dc, UNKNOWN_LOCATION);
30d3ba51
DM
390}
391
392/* Verify that we gracefully handle attempts to serialize bad
393 compound locations. */
394
395static void
396test_bad_endpoints ()
397{
398 location_t bad_endpoints
399 = make_location (BUILTINS_LOCATION,
400 UNKNOWN_LOCATION, UNKNOWN_LOCATION);
401
402 location_range loc_range;
403 loc_range.m_loc = bad_endpoints;
404 loc_range.m_range_display_kind = SHOW_RANGE_WITH_CARET;
405 loc_range.m_label = NULL;
406
004bb936
LH
407 test_diagnostic_context dc;
408 json::object *obj = json_from_location_range (&dc, &loc_range, 0);
30d3ba51
DM
409 /* We should have a "caret" value, but no "start" or "finish" values. */
410 ASSERT_TRUE (obj != NULL);
411 ASSERT_TRUE (obj->get ("caret") != NULL);
412 ASSERT_TRUE (obj->get ("start") == NULL);
413 ASSERT_TRUE (obj->get ("finish") == NULL);
414 delete obj;
415}
416
417/* Run all of the selftests within this file. */
418
419void
420diagnostic_format_json_cc_tests ()
421{
422 test_unknown_location ();
423 test_bad_endpoints ();
424}
425
426} // namespace selftest
427
428#endif /* #if CHECKING_P */