]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/diagnostic-format-json.cc
Daily bump.
[thirdparty/gcc.git] / gcc / diagnostic-format-json.cc
1 /* JSON output for diagnostics
2 Copyright (C) 2018-2019 Free Software Foundation, Inc.
3 Contributed by David Malcolm <dmalcolm@redhat.com>.
4
5 This file is part of GCC.
6
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.
11
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.
16
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/>. */
20
21
22 #include "config.h"
23 #include "system.h"
24 #include "coretypes.h"
25 #include "diagnostic.h"
26 #include "json.h"
27 #include "selftest.h"
28
29 /* The top-level JSON array of pending diagnostics. */
30
31 static json::array *toplevel_array;
32
33 /* The JSON object for the current diagnostic group. */
34
35 static json::object *cur_group;
36
37 /* The JSON array for the "children" array within the current diagnostic
38 group. */
39
40 static json::array *cur_children_array;
41
42 /* Generate a JSON object for LOC. */
43
44 static json::object *
45 json_from_expanded_location (location_t loc)
46 {
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::number (exploc.line));
52 result->set ("column", new json::number (exploc.column));
53 return result;
54 }
55
56 /* Generate a JSON object for LOC_RANGE. */
57
58 static json::object *
59 json_from_location_range (const location_range *loc_range, unsigned range_idx)
60 {
61 location_t caret_loc = get_pure_location (loc_range->m_loc);
62
63 if (caret_loc == UNKNOWN_LOCATION)
64 return NULL;
65
66 location_t start_loc = get_start (loc_range->m_loc);
67 location_t finish_loc = get_finish (loc_range->m_loc);
68
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));
77
78 if (loc_range->m_label)
79 {
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 ();
85 }
86
87 return result;
88 }
89
90 /* Generate a JSON object for HINT. */
91
92 static json::object *
93 json_from_fixit_hint (const fixit_hint *hint)
94 {
95 json::object *fixit_obj = new json::object ();
96
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 ()));
102
103 return fixit_obj;
104 }
105
106 /* No-op implementation of "begin_diagnostic" for JSON output. */
107
108 static void
109 json_begin_diagnostic (diagnostic_context *, diagnostic_info *)
110 {
111 }
112
113 /* Implementation of "end_diagnostic" for JSON output.
114 Generate a JSON object for DIAGNOSTIC, and store for output
115 within current diagnostic group. */
116
117 static void
118 json_end_diagnostic (diagnostic_context *context, diagnostic_info *diagnostic,
119 diagnostic_t orig_diag_kind)
120 {
121 json::object *diag_obj = new json::object ();
122
123 /* Get "kind" of diagnostic. */
124 {
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"
130 };
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);
141 }
142
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);
147
148 char *option_text;
149 option_text = context->option_name (context, diagnostic->option_index,
150 orig_diag_kind, diagnostic->kind);
151 if (option_text)
152 {
153 diag_obj->set ("option", new json::string (option_text));
154 free (option_text);
155 }
156
157 /* If we've already emitted a diagnostic within this auto_diagnostic_group,
158 then add diag_obj to its "children" array. */
159 if (cur_group)
160 {
161 gcc_assert (cur_children_array);
162 cur_children_array->append (diag_obj);
163 }
164 else
165 {
166 /* Otherwise, make diag_obj be the top-level object within the group;
167 add a "children" array. */
168 toplevel_array->append (diag_obj);
169 cur_group = diag_obj;
170 cur_children_array = new json::array ();
171 diag_obj->set ("children", cur_children_array);
172 }
173
174 const rich_location *richloc = diagnostic->richloc;
175
176 json::array *loc_array = new json::array ();
177 diag_obj->set ("locations", loc_array);
178
179 for (unsigned int i = 0; i < richloc->get_num_locations (); i++)
180 {
181 const location_range *loc_range = richloc->get_range (i);
182 json::object *loc_obj = json_from_location_range (loc_range, i);
183 if (loc_obj)
184 loc_array->append (loc_obj);
185 }
186
187 if (richloc->get_num_fixit_hints ())
188 {
189 json::array *fixit_array = new json::array ();
190 diag_obj->set ("fixits", fixit_array);
191 for (unsigned int i = 0; i < richloc->get_num_fixit_hints (); i++)
192 {
193 const fixit_hint *hint = richloc->get_fixit_hint (i);
194 json::object *fixit_obj = json_from_fixit_hint (hint);
195 fixit_array->append (fixit_obj);
196 }
197 }
198
199 /* TODO: tree-ish things:
200 TODO: functions
201 TODO: inlining information
202 TODO: macro expansion information. */
203 }
204
205 /* No-op implementation of "begin_group_cb" for JSON output. */
206
207 static void
208 json_begin_group (diagnostic_context *)
209 {
210 }
211
212 /* Implementation of "end_group_cb" for JSON output. */
213
214 static void
215 json_end_group (diagnostic_context *)
216 {
217 cur_group = NULL;
218 cur_children_array = NULL;
219 }
220
221 /* Callback for final cleanup for JSON output. */
222
223 static void
224 json_final_cb (diagnostic_context *)
225 {
226 /* Flush the top-level array. */
227 toplevel_array->dump (stderr);
228 fprintf (stderr, "\n");
229 delete toplevel_array;
230 toplevel_array = NULL;
231 }
232
233 /* Set the output format for CONTEXT to FORMAT. */
234
235 void
236 diagnostic_output_format_init (diagnostic_context *context,
237 enum diagnostics_output_format format)
238 {
239 switch (format)
240 {
241 default:
242 gcc_unreachable ();
243 case DIAGNOSTICS_OUTPUT_FORMAT_TEXT:
244 /* The default; do nothing. */
245 break;
246
247 case DIAGNOSTICS_OUTPUT_FORMAT_JSON:
248 {
249 /* Set up top-level JSON array. */
250 if (toplevel_array == NULL)
251 toplevel_array = new json::array ();
252
253 /* Override callbacks. */
254 context->begin_diagnostic = json_begin_diagnostic;
255 context->end_diagnostic = json_end_diagnostic;
256 context->begin_group_cb = json_begin_group;
257 context->end_group_cb = json_end_group;
258 context->final_cb = json_final_cb;
259
260 /* The option is handled in JSON format, rather than as text. */
261 context->show_option_requested = false;
262
263 /* Don't colorize the text. */
264 pp_show_color (context->printer) = false;
265 }
266 break;
267 }
268 }
269
270 #if CHECKING_P
271
272 namespace selftest {
273
274 /* We shouldn't call json_from_expanded_location on UNKNOWN_LOCATION,
275 but verify that we handle this gracefully. */
276
277 static void
278 test_unknown_location ()
279 {
280 delete json_from_expanded_location (UNKNOWN_LOCATION);
281 }
282
283 /* Verify that we gracefully handle attempts to serialize bad
284 compound locations. */
285
286 static void
287 test_bad_endpoints ()
288 {
289 location_t bad_endpoints
290 = make_location (BUILTINS_LOCATION,
291 UNKNOWN_LOCATION, UNKNOWN_LOCATION);
292
293 location_range loc_range;
294 loc_range.m_loc = bad_endpoints;
295 loc_range.m_range_display_kind = SHOW_RANGE_WITH_CARET;
296 loc_range.m_label = NULL;
297
298 json::object *obj = json_from_location_range (&loc_range, 0);
299 /* We should have a "caret" value, but no "start" or "finish" values. */
300 ASSERT_TRUE (obj != NULL);
301 ASSERT_TRUE (obj->get ("caret") != NULL);
302 ASSERT_TRUE (obj->get ("start") == NULL);
303 ASSERT_TRUE (obj->get ("finish") == NULL);
304 delete obj;
305 }
306
307 /* Run all of the selftests within this file. */
308
309 void
310 diagnostic_format_json_cc_tests ()
311 {
312 test_unknown_location ();
313 test_bad_endpoints ();
314 }
315
316 } // namespace selftest
317
318 #endif /* #if CHECKING_P */