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 ("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
->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 (column_fields
[i
].name
, new json::integer_number (col
));
115 if (column_fields
[i
].unit
== orig_unit
)
118 gcc_assert (the_column
!= INT_MIN
);
119 result
->set ("column", new json::integer_number (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 ("label", new json::string (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", new json::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 ("cwe",
182 new json::integer_number (metadata
->get_cwe ()));
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. */
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
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
));
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
);
223 option_text
= m_context
.m_option_name (&m_context
, diagnostic
->option_index
,
224 orig_diag_kind
, diagnostic
->kind
);
227 diag_obj
->set ("option", new json::string (option_text
));
231 if (m_context
.m_get_option_url
)
233 char *option_url
= m_context
.m_get_option_url (&m_context
,
234 diagnostic
->option_index
);
237 diag_obj
->set ("option_url", new json::string (option_url
));
242 /* If we've already emitted a diagnostic within this auto_diagnostic_group,
243 then add diag_obj to its "children" array. */
246 gcc_assert (m_cur_children_array
);
247 m_cur_children_array
->append (diag_obj
);
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
.m_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
);
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:
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
.m_make_json_for_path
)
301 json::value
*path_value
302 = m_context
.m_make_json_for_path (&m_context
, path
);
303 diag_obj
->set ("path", path_value
);
306 diag_obj
->set ("escape-source",
307 new json::literal (richloc
->escape_on_output_p ()));
310 class json_stderr_output_format
: public json_output_format
313 json_stderr_output_format (diagnostic_context
&context
)
314 : json_output_format (context
)
317 ~json_stderr_output_format ()
319 flush_to_file (stderr
);
323 class json_file_output_format
: public json_output_format
326 json_file_output_format (diagnostic_context
&context
,
327 const char *base_file_name
)
328 : json_output_format (context
),
329 m_base_file_name (xstrdup (base_file_name
))
333 ~json_file_output_format ()
335 char *filename
= concat (m_base_file_name
, ".gcc.json", NULL
);
336 free (m_base_file_name
);
337 m_base_file_name
= nullptr;
338 FILE *outf
= fopen (filename
, "w");
341 const char *errstr
= xstrerror (errno
);
342 fnotice (stderr
, "error: unable to open '%s' for writing: %s\n",
347 flush_to_file (outf
);
353 char *m_base_file_name
;
356 /* Populate CONTEXT in preparation for JSON output (either to stderr, or
360 diagnostic_output_format_init_json (diagnostic_context
*context
)
362 /* Override callbacks. */
363 context
->m_print_path
= nullptr; /* handled in json_end_diagnostic. */
365 /* The metadata is handled in JSON format, rather than as text. */
366 context
->set_show_cwe (false);
367 context
->set_show_rules (false);
369 /* The option is handled in JSON format, rather than as text. */
370 context
->set_show_option_requested (false);
372 /* Don't colorize the text. */
373 pp_show_color (context
->printer
) = false;
376 /* Populate CONTEXT in preparation for JSON output to stderr. */
379 diagnostic_output_format_init_json_stderr (diagnostic_context
*context
)
381 diagnostic_output_format_init_json (context
);
382 context
->set_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. */
389 diagnostic_output_format_init_json_file (diagnostic_context
*context
,
390 const char *base_file_name
)
392 diagnostic_output_format_init_json (context
);
393 context
->set_output_format (new json_file_output_format (*context
,
401 /* We shouldn't call json_from_expanded_location on UNKNOWN_LOCATION,
402 but verify that we handle this gracefully. */
405 test_unknown_location ()
407 test_diagnostic_context dc
;
408 delete json_from_expanded_location (&dc
, UNKNOWN_LOCATION
);
411 /* Verify that we gracefully handle attempts to serialize bad
412 compound locations. */
415 test_bad_endpoints ()
417 location_t bad_endpoints
418 = make_location (BUILTINS_LOCATION
,
419 UNKNOWN_LOCATION
, UNKNOWN_LOCATION
);
421 location_range loc_range
;
422 loc_range
.m_loc
= bad_endpoints
;
423 loc_range
.m_range_display_kind
= SHOW_RANGE_WITH_CARET
;
424 loc_range
.m_label
= NULL
;
426 test_diagnostic_context dc
;
427 json::object
*obj
= json_from_location_range (&dc
, &loc_range
, 0);
428 /* We should have a "caret" value, but no "start" or "finish" values. */
429 ASSERT_TRUE (obj
!= NULL
);
430 ASSERT_TRUE (obj
->get ("caret") != NULL
);
431 ASSERT_TRUE (obj
->get ("start") == NULL
);
432 ASSERT_TRUE (obj
->get ("finish") == NULL
);
436 /* Run all of the selftests within this file. */
439 diagnostic_format_json_cc_tests ()
441 test_unknown_location ();
442 test_bad_endpoints ();
445 } // namespace selftest
447 #endif /* #if CHECKING_P */