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
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/>. */
24 #include "coretypes.h"
25 #include "diagnostic.h"
26 #include "selftest-diagnostic.h"
27 #include "diagnostic-metadata.h"
31 /* Subclass of diagnostic_output_format for JSON output. */
33 class json_output_format
: public diagnostic_output_format
36 void on_begin_group () final override
40 void on_end_group () final override
42 m_cur_group
= nullptr;
43 m_cur_children_array
= nullptr;
46 on_begin_diagnostic (diagnostic_info
*) final override
51 on_end_diagnostic (diagnostic_info
*diagnostic
,
52 diagnostic_t orig_diag_kind
) final override
;
53 void on_diagram (const diagnostic_diagram
&) final override
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. */
69 flush_to_file (FILE *outf
)
71 m_toplevel_array
->dump (outf
);
73 delete m_toplevel_array
;
74 m_toplevel_array
= nullptr;
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
86 json::array
*m_cur_children_array
;
89 /* Generate a JSON object for LOC. */
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 ();
97 result
->set_string ("file", exploc
.file
);
98 result
->set_integer ("line", exploc
.line
);
100 const enum diagnostics_column_unit orig_unit
= context
->m_column_unit
;
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
->m_column_unit
= column_fields
[i
].unit
;
113 const int col
= context
->converted_column (exploc
);
114 result
->set_integer (column_fields
[i
].name
, col
);
115 if (column_fields
[i
].unit
== orig_unit
)
118 gcc_assert (the_column
!= INT_MIN
);
119 result
->set_integer ("column", the_column
);
120 context
->m_column_unit
= orig_unit
;
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
)
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
));
151 result
->set_string ("label", text
.get ());
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 ("string", hint
->get_string ());
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_integer ("cwe", metadata
->get_cwe ());
186 /* Implementation of "on_end_diagnostic" vfunc for JSON output.
187 Generate a JSON object for DIAGNOSTIC, and store for output
188 within current diagnostic group. */
191 json_output_format::on_end_diagnostic (diagnostic_info
*diagnostic
,
192 diagnostic_t orig_diag_kind
)
194 json::object
*diag_obj
= new json::object ();
196 /* Get "kind" of diagnostic. */
198 static const char *const diagnostic_kind_text
[] = {
199 #define DEFINE_DIAGNOSTIC_KIND(K, T, C) (T),
200 #include "diagnostic.def"
201 #undef DEFINE_DIAGNOSTIC_KIND
204 /* Lose the trailing ": ". */
205 const char *kind_text
= diagnostic_kind_text
[diagnostic
->kind
];
206 size_t len
= strlen (kind_text
);
207 gcc_assert (len
> 2);
208 gcc_assert (kind_text
[len
- 2] == ':');
209 gcc_assert (kind_text
[len
- 1] == ' ');
210 char *rstrip
= xstrdup (kind_text
);
211 rstrip
[len
- 2] = '\0';
212 diag_obj
->set_string ("kind", rstrip
);
216 // FIXME: encoding of the message (json::string requires UTF-8)
217 diag_obj
->set_string ("message", pp_formatted_text (m_context
.printer
));
218 pp_clear_output_area (m_context
.printer
);
220 if (char *option_text
= m_context
.make_option_name (diagnostic
->option_index
,
224 diag_obj
->set_string ("option", option_text
);
228 if (char *option_url
= m_context
.make_option_url (diagnostic
->option_index
))
230 diag_obj
->set_string ("option_url", option_url
);
234 /* If we've already emitted a diagnostic within this auto_diagnostic_group,
235 then add diag_obj to its "children" array. */
238 gcc_assert (m_cur_children_array
);
239 m_cur_children_array
->append (diag_obj
);
243 /* Otherwise, make diag_obj be the top-level object within the group;
244 add a "children" array and record the column origin. */
245 m_toplevel_array
->append (diag_obj
);
246 m_cur_group
= diag_obj
;
247 m_cur_children_array
= new json::array ();
248 diag_obj
->set ("children", m_cur_children_array
);
249 diag_obj
->set_integer ("column-origin", m_context
.m_column_origin
);
252 const rich_location
*richloc
= diagnostic
->richloc
;
254 json::array
*loc_array
= new json::array ();
255 diag_obj
->set ("locations", loc_array
);
257 for (unsigned int i
= 0; i
< richloc
->get_num_locations (); i
++)
259 const location_range
*loc_range
= richloc
->get_range (i
);
260 json::object
*loc_obj
261 = json_from_location_range (&m_context
, loc_range
, i
);
263 loc_array
->append (loc_obj
);
266 if (richloc
->get_num_fixit_hints ())
268 json::array
*fixit_array
= new json::array ();
269 diag_obj
->set ("fixits", fixit_array
);
270 for (unsigned int i
= 0; i
< richloc
->get_num_fixit_hints (); i
++)
272 const fixit_hint
*hint
= richloc
->get_fixit_hint (i
);
273 json::object
*fixit_obj
= json_from_fixit_hint (&m_context
, hint
);
274 fixit_array
->append (fixit_obj
);
278 /* TODO: tree-ish things:
280 TODO: inlining information
281 TODO: macro expansion information. */
283 if (diagnostic
->metadata
)
285 json::object
*metadata_obj
= json_from_metadata (diagnostic
->metadata
);
286 diag_obj
->set ("metadata", metadata_obj
);
289 const diagnostic_path
*path
= richloc
->get_path ();
290 if (path
&& m_context
.m_make_json_for_path
)
292 json::value
*path_value
293 = m_context
.m_make_json_for_path (&m_context
, path
);
294 diag_obj
->set ("path", path_value
);
297 diag_obj
->set ("escape-source",
298 new json::literal (richloc
->escape_on_output_p ()));
301 class json_stderr_output_format
: public json_output_format
304 json_stderr_output_format (diagnostic_context
&context
)
305 : json_output_format (context
)
308 ~json_stderr_output_format ()
310 flush_to_file (stderr
);
314 class json_file_output_format
: public json_output_format
317 json_file_output_format (diagnostic_context
&context
,
318 const char *base_file_name
)
319 : json_output_format (context
),
320 m_base_file_name (xstrdup (base_file_name
))
324 ~json_file_output_format ()
326 char *filename
= concat (m_base_file_name
, ".gcc.json", NULL
);
327 free (m_base_file_name
);
328 m_base_file_name
= nullptr;
329 FILE *outf
= fopen (filename
, "w");
332 const char *errstr
= xstrerror (errno
);
333 fnotice (stderr
, "error: unable to open '%s' for writing: %s\n",
338 flush_to_file (outf
);
344 char *m_base_file_name
;
347 /* Populate CONTEXT in preparation for JSON output (either to stderr, or
351 diagnostic_output_format_init_json (diagnostic_context
*context
)
353 /* Override callbacks. */
354 context
->m_print_path
= nullptr; /* handled in json_end_diagnostic. */
356 /* The metadata is handled in JSON format, rather than as text. */
357 context
->set_show_cwe (false);
358 context
->set_show_rules (false);
360 /* The option is handled in JSON format, rather than as text. */
361 context
->set_show_option_requested (false);
363 /* Don't colorize the text. */
364 pp_show_color (context
->printer
) = false;
367 /* Populate CONTEXT in preparation for JSON output to stderr. */
370 diagnostic_output_format_init_json_stderr (diagnostic_context
*context
)
372 diagnostic_output_format_init_json (context
);
373 context
->set_output_format (new json_stderr_output_format (*context
));
376 /* Populate CONTEXT in preparation for JSON output to a file named
377 BASE_FILE_NAME.gcc.json. */
380 diagnostic_output_format_init_json_file (diagnostic_context
*context
,
381 const char *base_file_name
)
383 diagnostic_output_format_init_json (context
);
384 context
->set_output_format (new json_file_output_format (*context
,
392 /* We shouldn't call json_from_expanded_location on UNKNOWN_LOCATION,
393 but verify that we handle this gracefully. */
396 test_unknown_location ()
398 test_diagnostic_context dc
;
399 delete json_from_expanded_location (&dc
, UNKNOWN_LOCATION
);
402 /* Verify that we gracefully handle attempts to serialize bad
403 compound locations. */
406 test_bad_endpoints ()
408 location_t bad_endpoints
409 = make_location (BUILTINS_LOCATION
,
410 UNKNOWN_LOCATION
, UNKNOWN_LOCATION
);
412 location_range loc_range
;
413 loc_range
.m_loc
= bad_endpoints
;
414 loc_range
.m_range_display_kind
= SHOW_RANGE_WITH_CARET
;
415 loc_range
.m_label
= NULL
;
417 test_diagnostic_context dc
;
418 json::object
*obj
= json_from_location_range (&dc
, &loc_range
, 0);
419 /* We should have a "caret" value, but no "start" or "finish" values. */
420 ASSERT_TRUE (obj
!= NULL
);
421 ASSERT_TRUE (obj
->get ("caret") != NULL
);
422 ASSERT_TRUE (obj
->get ("start") == NULL
);
423 ASSERT_TRUE (obj
->get ("finish") == NULL
);
427 /* Run all of the selftests within this file. */
430 diagnostic_format_json_cc_tests ()
432 test_unknown_location ();
433 test_bad_endpoints ();
436 } // namespace selftest
438 #endif /* #if CHECKING_P */