typeck.c (cp_truthvalue_conversion): Add tsubst_flags_t parameter and use it in calls...
[official-gcc.git] / gcc / diagnostic-format-json.cc
blob6782ec9dffb039d12636b423dc448b897657ba71
1 /* JSON output for diagnostics
2 Copyright (C) 2018-2019 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 "json.h"
27 #include "selftest.h"
29 /* The top-level JSON array of pending diagnostics. */
31 static json::array *toplevel_array;
33 /* The JSON object for the current diagnostic group. */
35 static json::object *cur_group;
37 /* The JSON array for the "children" array within the current diagnostic
38 group. */
40 static json::array *cur_children_array;
42 /* Generate a JSON object for LOC. */
44 static json::object *
45 json_from_expanded_location (location_t loc)
47 expanded_location exploc = expand_location (loc);
48 json::object *result = new json::object ();
49 if (exploc.file)
50 result->set ("file", new json::string (exploc.file));
51 result->set ("line", new json::integer_number (exploc.line));
52 result->set ("column", new json::integer_number (exploc.column));
53 return result;
56 /* Generate a JSON object for LOC_RANGE. */
58 static json::object *
59 json_from_location_range (const location_range *loc_range, unsigned range_idx)
61 location_t caret_loc = get_pure_location (loc_range->m_loc);
63 if (caret_loc == UNKNOWN_LOCATION)
64 return NULL;
66 location_t start_loc = get_start (loc_range->m_loc);
67 location_t finish_loc = get_finish (loc_range->m_loc);
69 json::object *result = new json::object ();
70 result->set ("caret", json_from_expanded_location (caret_loc));
71 if (start_loc != caret_loc
72 && start_loc != UNKNOWN_LOCATION)
73 result->set ("start", json_from_expanded_location (start_loc));
74 if (finish_loc != caret_loc
75 && finish_loc != UNKNOWN_LOCATION)
76 result->set ("finish", json_from_expanded_location (finish_loc));
78 if (loc_range->m_label)
80 label_text text;
81 text = loc_range->m_label->get_text (range_idx);
82 if (text.m_buffer)
83 result->set ("label", new json::string (text.m_buffer));
84 text.maybe_free ();
87 return result;
90 /* Generate a JSON object for HINT. */
92 static json::object *
93 json_from_fixit_hint (const fixit_hint *hint)
95 json::object *fixit_obj = new json::object ();
97 location_t start_loc = hint->get_start_loc ();
98 fixit_obj->set ("start", json_from_expanded_location (start_loc));
99 location_t next_loc = hint->get_next_loc ();
100 fixit_obj->set ("next", json_from_expanded_location (next_loc));
101 fixit_obj->set ("string", new json::string (hint->get_string ()));
103 return fixit_obj;
106 /* No-op implementation of "begin_diagnostic" for JSON output. */
108 static void
109 json_begin_diagnostic (diagnostic_context *, diagnostic_info *)
113 /* Implementation of "end_diagnostic" for JSON output.
114 Generate a JSON object for DIAGNOSTIC, and store for output
115 within current diagnostic group. */
117 static void
118 json_end_diagnostic (diagnostic_context *context, diagnostic_info *diagnostic,
119 diagnostic_t orig_diag_kind)
121 json::object *diag_obj = new json::object ();
123 /* Get "kind" of diagnostic. */
125 static const char *const diagnostic_kind_text[] = {
126 #define DEFINE_DIAGNOSTIC_KIND(K, T, C) (T),
127 #include "diagnostic.def"
128 #undef DEFINE_DIAGNOSTIC_KIND
129 "must-not-happen"
131 /* Lose the trailing ": ". */
132 const char *kind_text = diagnostic_kind_text[diagnostic->kind];
133 size_t len = strlen (kind_text);
134 gcc_assert (len > 2);
135 gcc_assert (kind_text[len - 2] == ':');
136 gcc_assert (kind_text[len - 1] == ' ');
137 char *rstrip = xstrdup (kind_text);
138 rstrip[len - 2] = '\0';
139 diag_obj->set ("kind", new json::string (rstrip));
140 free (rstrip);
143 // FIXME: encoding of the message (json::string requires UTF-8)
144 diag_obj->set ("message",
145 new json::string (pp_formatted_text (context->printer)));
146 pp_clear_output_area (context->printer);
148 char *option_text;
149 option_text = context->option_name (context, diagnostic->option_index,
150 orig_diag_kind, diagnostic->kind);
151 if (option_text)
153 diag_obj->set ("option", new json::string (option_text));
154 free (option_text);
157 if (context->get_option_url)
159 char *option_url = context->get_option_url (context,
160 diagnostic->option_index);
161 if (option_url)
163 diag_obj->set ("option_url", new json::string (option_url));
164 free (option_url);
168 /* If we've already emitted a diagnostic within this auto_diagnostic_group,
169 then add diag_obj to its "children" array. */
170 if (cur_group)
172 gcc_assert (cur_children_array);
173 cur_children_array->append (diag_obj);
175 else
177 /* Otherwise, make diag_obj be the top-level object within the group;
178 add a "children" array. */
179 toplevel_array->append (diag_obj);
180 cur_group = diag_obj;
181 cur_children_array = new json::array ();
182 diag_obj->set ("children", cur_children_array);
185 const rich_location *richloc = diagnostic->richloc;
187 json::array *loc_array = new json::array ();
188 diag_obj->set ("locations", loc_array);
190 for (unsigned int i = 0; i < richloc->get_num_locations (); i++)
192 const location_range *loc_range = richloc->get_range (i);
193 json::object *loc_obj = json_from_location_range (loc_range, i);
194 if (loc_obj)
195 loc_array->append (loc_obj);
198 if (richloc->get_num_fixit_hints ())
200 json::array *fixit_array = new json::array ();
201 diag_obj->set ("fixits", fixit_array);
202 for (unsigned int i = 0; i < richloc->get_num_fixit_hints (); i++)
204 const fixit_hint *hint = richloc->get_fixit_hint (i);
205 json::object *fixit_obj = json_from_fixit_hint (hint);
206 fixit_array->append (fixit_obj);
210 /* TODO: tree-ish things:
211 TODO: functions
212 TODO: inlining information
213 TODO: macro expansion information. */
216 /* No-op implementation of "begin_group_cb" for JSON output. */
218 static void
219 json_begin_group (diagnostic_context *)
223 /* Implementation of "end_group_cb" for JSON output. */
225 static void
226 json_end_group (diagnostic_context *)
228 cur_group = NULL;
229 cur_children_array = NULL;
232 /* Callback for final cleanup for JSON output. */
234 static void
235 json_final_cb (diagnostic_context *)
237 /* Flush the top-level array. */
238 toplevel_array->dump (stderr);
239 fprintf (stderr, "\n");
240 delete toplevel_array;
241 toplevel_array = NULL;
244 /* Set the output format for CONTEXT to FORMAT. */
246 void
247 diagnostic_output_format_init (diagnostic_context *context,
248 enum diagnostics_output_format format)
250 switch (format)
252 default:
253 gcc_unreachable ();
254 case DIAGNOSTICS_OUTPUT_FORMAT_TEXT:
255 /* The default; do nothing. */
256 break;
258 case DIAGNOSTICS_OUTPUT_FORMAT_JSON:
260 /* Set up top-level JSON array. */
261 if (toplevel_array == NULL)
262 toplevel_array = new json::array ();
264 /* Override callbacks. */
265 context->begin_diagnostic = json_begin_diagnostic;
266 context->end_diagnostic = json_end_diagnostic;
267 context->begin_group_cb = json_begin_group;
268 context->end_group_cb = json_end_group;
269 context->final_cb = json_final_cb;
271 /* The option is handled in JSON format, rather than as text. */
272 context->show_option_requested = false;
274 /* Don't colorize the text. */
275 pp_show_color (context->printer) = false;
277 break;
281 #if CHECKING_P
283 namespace selftest {
285 /* We shouldn't call json_from_expanded_location on UNKNOWN_LOCATION,
286 but verify that we handle this gracefully. */
288 static void
289 test_unknown_location ()
291 delete json_from_expanded_location (UNKNOWN_LOCATION);
294 /* Verify that we gracefully handle attempts to serialize bad
295 compound locations. */
297 static void
298 test_bad_endpoints ()
300 location_t bad_endpoints
301 = make_location (BUILTINS_LOCATION,
302 UNKNOWN_LOCATION, UNKNOWN_LOCATION);
304 location_range loc_range;
305 loc_range.m_loc = bad_endpoints;
306 loc_range.m_range_display_kind = SHOW_RANGE_WITH_CARET;
307 loc_range.m_label = NULL;
309 json::object *obj = json_from_location_range (&loc_range, 0);
310 /* We should have a "caret" value, but no "start" or "finish" values. */
311 ASSERT_TRUE (obj != NULL);
312 ASSERT_TRUE (obj->get ("caret") != NULL);
313 ASSERT_TRUE (obj->get ("start") == NULL);
314 ASSERT_TRUE (obj->get ("finish") == NULL);
315 delete obj;
318 /* Run all of the selftests within this file. */
320 void
321 diagnostic_format_json_cc_tests ()
323 test_unknown_location ();
324 test_bad_endpoints ();
327 } // namespace selftest
329 #endif /* #if CHECKING_P */