Debian Codegraph @

gcc/testsuite/gcc.dg/plugin/diagnostic_plugin_test_show_locus.cc

raw
1 /* { dg-options "-O" } */
2
3 /* This plugin exercises the diagnostics-printing code.
4
5 The goal is to unit-test the range-printing code without needing any
6 correct range data within the compiler's IR. We can't use any real
7 diagnostics for this, so we have to fake it, hence this plugin.
8
9 There are two test files used with this code:
10
11 diagnostic-test-show-locus-ascii-bw.c
12 ..........................-ascii-color.c
13
14 to exercise uncolored vs colored output by supplying plugin arguments
15 to hack in the desired behavior:
16
17 -fplugin-arg-diagnostic_plugin_test_show_locus-color
18
19 The test files contain functions, but the body of each
20 function is disabled using the preprocessor. The plugin detects
21 the functions by name, and inject diagnostics within them, using
22 hard-coded locations relative to the top of each function.
23
24 The plugin uses a function "get_loc" below to map from line/column
25 numbers to location_t, and this relies on input_location being in
26 the same ordinary line_map as the locations in question. The plugin
27 runs after parsing, so input_location will be at the end of the file.
28
29 This need for all of the test code to be in a single ordinary line map
30 means that each test file needs to have a very long line near the top
31 (potentially to cover the extra byte-count of colorized data),
32 to ensure that further very long lines don't start a new linemap.
33 This also means that we can't use macros in the test files. */
34
35 #include "gcc-plugin.h"
36 #include "config.h"
37 #include "system.h"
38 #include "coretypes.h"
39 #include "tm.h"
40 #include "tree.h"
41 #include "stringpool.h"
42 #include "toplev.h"
43 #include "basic-block.h"
44 #include "hash-table.h"
45 #include "vec.h"
46 #include "ggc.h"
47 #include "basic-block.h"
48 #include "tree-ssa-alias.h"
49 #include "internal-fn.h"
50 #include "gimple.h"
51 #include "gimple-iterator.h"
52 #include "gimple-fold.h"
53 #include "tree-eh.h"
54 #include "gimple-expr.h"
55 #include "is-a.h"
56 #include "tree.h"
57 #include "tree-pass.h"
58 #include "intl.h"
59 #include "plugin-version.h"
60 #include "diagnostic.h"
61 #include "context.h"
62 #include "print-tree.h"
63 #include "gcc-rich-location.h"
64 #include "text-range-label.h"
65 #include "diagnostic-format-text.h"
66
67 int plugin_is_GPL_compatible;
68
69 const pass_data pass_data_test_show_locus =
70 {
71 GIMPLE_PASS, /* type */
72 "test_show_locus", /* name */
73 OPTGROUP_NONE, /* optinfo_flags */
74 TV_NONE, /* tv_id */
75 PROP_ssa, /* properties_required */
76 0, /* properties_provided */
77 0, /* properties_destroyed */
78 0, /* todo_flags_start */
79 0, /* todo_flags_finish */
80 };
81
82 class pass_test_show_locus : public gimple_opt_pass
83 {
84 public:
85 pass_test_show_locus(gcc::context *ctxt)
86 : gimple_opt_pass(pass_data_test_show_locus, ctxt)
87 {}
88
89 /* opt_pass methods: */
90 bool gate (function *) { return true; }
91 virtual unsigned int execute (function *);
92
93 }; // class pass_test_show_locus
94
95 /* Given LINE_NUM and COL_NUM, generate a location_t in the
96 current file, relative to input_location. This relies on the
97 location being expressible in the same ordinary line_map as
98 input_location (which is typically at the end of the source file
99 when this is called). Hence the test files we compile with this
100 plugin must have an initial very long line (to avoid long lines
101 starting a new line map), and must not use macros.
102
103 COL_NUM uses the Emacs convention of 0-based column numbers. */
104
105 static location_t
106 get_loc (unsigned int line_num, unsigned int col_num)
107 {
108 /* Use input_location to get the relevant line_map */
109 const struct line_map_ordinary *line_map
110 = (const line_map_ordinary *)(linemap_lookup (line_table,
111 input_location));
112
113 /* Convert from 0-based column numbers to 1-based column numbers. */
114 location_t loc
115 = linemap_position_for_line_and_column (line_table,
116 line_map,
117 line_num, col_num + 1);
118
119 return loc;
120 }
121
122 /* Was "color" passed in as a plugin argument? */
123 static bool force_show_locus_color = false;
124
125 /* We want to verify the colorized output of diagnostic_show_locus,
126 but turning on colorization for everything confuses "dg-warning" etc.
127 Hence we special-case it within this plugin by using this modified
128 version of default_diagnostic_text_finalizer, which, if "color" is
129 passed in as a plugin argument turns on colorization, but just
130 for diagnostic_show_locus. */
131
132 static void
133 custom_diagnostic_text_finalizer (diagnostic_text_output_format &text_output,
134 const diagnostic_info *diagnostic,
135 diagnostic_t)
136 {
137 pretty_printer *const pp = text_output.get_printer ();
138 bool old_show_color = pp_show_color (pp);
139 if (force_show_locus_color)
140 pp_show_color (pp) = true;
141 char *saved_prefix = pp_take_prefix (pp);
142 pp_set_prefix (pp, NULL);
143 pp_newline (pp);
144 diagnostic_show_locus (&text_output.get_context (),
145 text_output.get_source_printing_options (),
146 diagnostic->richloc, diagnostic->kind, pp);
147 pp_show_color (pp) = old_show_color;
148 pp_set_prefix (pp, saved_prefix);
149 pp_flush (pp);
150 }
151
152 /* Add a location to RICHLOC with caret==start at START, ranging to FINISH. */
153
154 static void
155 add_range (rich_location *richloc, location_t start, location_t finish,
156 enum range_display_kind range_display_kind
157 = SHOW_RANGE_WITHOUT_CARET,
158 const range_label *label = NULL)
159 {
160 richloc->add_range (make_location (start, start, finish), range_display_kind,
161 label);
162 }
163
164 /* Exercise the diagnostic machinery to emit various warnings,
165 for use by diagnostic-test-show-locus-*.c.
166
167 We inject each warning relative to the start of a function,
168 which avoids lots of hardcoded absolute locations. */
169
170 static void
171 test_show_locus (function *fun)
172 {
173 tree fndecl = fun->decl;
174 tree identifier = DECL_NAME (fndecl);
175 const char *fnname = IDENTIFIER_POINTER (identifier);
176 location_t fnstart = fun->function_start_locus;
177 int fnstart_line = LOCATION_LINE (fnstart);
178
179 diagnostic_text_finalizer (global_dc) = custom_diagnostic_text_finalizer;
180
181 /* Hardcode the "terminal width", to verify the behavior of
182 very wide lines. */
183 global_dc->m_source_printing.max_width = 71;
184
185 if (0 == strcmp (fnname, "test_simple"))
186 {
187 const int line = fnstart_line + 2;
188 rich_location richloc (line_table, get_loc (line, 15));
189 add_range (&richloc, get_loc (line, 10), get_loc (line, 14));
190 add_range (&richloc, get_loc (line, 16), get_loc (line, 16));
191 warning_at (&richloc, 0, "test");
192 }
193
194 if (0 == strcmp (fnname, "test_simple_2"))
195 {
196 const int line = fnstart_line + 2;
197 rich_location richloc (line_table, get_loc (line, 24));
198 add_range (&richloc, get_loc (line, 6), get_loc (line, 22));
199 add_range (&richloc, get_loc (line, 26), get_loc (line, 43));
200 warning_at (&richloc, 0, "test");
201 }
202
203 if (0 == strcmp (fnname, "test_multiline"))
204 {
205 const int line = fnstart_line + 2;
206 text_range_label label ("label");
207 rich_location richloc (line_table, get_loc (line + 1, 7), &label);
208 add_range (&richloc, get_loc (line, 7), get_loc (line, 23));
209 add_range (&richloc, get_loc (line + 1, 9), get_loc (line + 1, 26));
210 warning_at (&richloc, 0, "test");
211 }
212
213 if (0 == strcmp (fnname, "test_many_lines"))
214 {
215 const int line = fnstart_line + 2;
216 text_range_label label0 ("label 0");
217 text_range_label label1 ("label 1");
218 text_range_label label2 ("label 2");
219 rich_location richloc (line_table, get_loc (line + 5, 7), &label0);
220 add_range (&richloc, get_loc (line, 7), get_loc (line + 4, 65),
221 SHOW_RANGE_WITHOUT_CARET, &label1);
222 add_range (&richloc, get_loc (line + 5, 9), get_loc (line + 10, 61),
223 SHOW_RANGE_WITHOUT_CARET, &label2);
224 warning_at (&richloc, 0, "test");
225 }
226
227 /* Example of a rich_location where the range is larger than
228 one character. */
229 if (0 == strcmp (fnname, "test_richloc_from_proper_range"))
230 {
231 const int line = fnstart_line + 2;
232 location_t start = get_loc (line, 12);
233 location_t finish = get_loc (line, 16);
234 rich_location richloc (line_table, make_location (start, start, finish));
235 warning_at (&richloc, 0, "test");
236 }
237
238 /* Example of a single-range location where the range starts
239 before the caret. */
240 if (0 == strcmp (fnname, "test_caret_within_proper_range"))
241 {
242 const int line = fnstart_line + 2;
243 warning_at (make_location (get_loc (line, 16), get_loc (line, 12),
244 get_loc (line, 20)),
245 0, "test");
246 }
247
248 /* Example of a very wide line, where the information of interest
249 is beyond the width of the terminal (hardcoded above), with
250 a secondary location that exactly fits on the left-margin. */
251 if (0 == strcmp (fnname, "test_very_wide_line"))
252 {
253 const int line = fnstart_line + 2;
254 global_dc->m_source_printing.show_ruler_p = true;
255 text_range_label label0 ("label 0");
256 text_range_label label1 ("label 1");
257 rich_location richloc (line_table,
258 make_location (get_loc (line, 94),
259 get_loc (line, 90),
260 get_loc (line, 98)),
261 &label0);
262 richloc.add_range (get_loc (line, 35), SHOW_RANGE_WITHOUT_CARET,
263 &label1);
264 richloc.add_fixit_replace ("bar * foo");
265 warning_at (&richloc, 0, "test");
266 global_dc->m_source_printing.show_ruler_p = false;
267 }
268
269 /* Likewise, but with a secondary location that's immediately before
270 the left margin; the location and label should be gracefully dropped. */
271 if (0 == strcmp (fnname, "test_very_wide_line_2"))
272 {
273 const int line = fnstart_line + 2;
274 global_dc->m_source_printing.show_ruler_p = true;
275 text_range_label label0 ("label 0");
276 text_range_label label1 ("label 1");
277 rich_location richloc (line_table,
278 make_location (get_loc (line, 94),
279 get_loc (line, 90),
280 get_loc (line, 98)),
281 &label0);
282 richloc.add_fixit_replace ("bar * foo");
283 richloc.add_range (get_loc (line, 34), SHOW_RANGE_WITHOUT_CARET,
284 &label1);
285 warning_at (&richloc, 0, "test");
286 global_dc->m_source_printing.show_ruler_p = false;
287 }
288
289 /* Example of multiple carets. */
290 if (0 == strcmp (fnname, "test_multiple_carets"))
291 {
292 const int line = fnstart_line + 2;
293 location_t caret_a = get_loc (line, 7);
294 location_t caret_b = get_loc (line, 11);
295 rich_location richloc (line_table, caret_a);
296 add_range (&richloc, caret_b, caret_b, SHOW_RANGE_WITH_CARET);
297 global_dc->m_source_printing.caret_chars[0] = 'A';
298 global_dc->m_source_printing.caret_chars[1] = 'B';
299 warning_at (&richloc, 0, "test");
300 global_dc->m_source_printing.caret_chars[0] = '^';
301 global_dc->m_source_printing.caret_chars[1] = '^';
302 }
303
304 /* Tests of rendering fixit hints. */
305 if (0 == strcmp (fnname, "test_fixit_insert"))
306 {
307 const int line = fnstart_line + 2;
308 location_t start = get_loc (line, 19);
309 location_t finish = get_loc (line, 22);
310 rich_location richloc (line_table, make_location (start, start, finish));
311 richloc.add_fixit_insert_before ("{");
312 richloc.add_fixit_insert_after ("}");
313 warning_at (&richloc, 0, "example of insertion hints");
314 }
315
316 if (0 == strcmp (fnname, "test_fixit_insert_newline"))
317 {
318 const int line = fnstart_line + 6;
319 location_t line_start = get_loc (line, 0);
320 location_t case_start = get_loc (line, 4);
321 location_t case_finish = get_loc (line, 11);
322 location_t case_loc = make_location (case_start, case_start, case_finish);
323 rich_location richloc (line_table, case_loc);
324 richloc.add_fixit_insert_before (line_start, " break;\n");
325 warning_at (&richloc, 0, "example of newline insertion hint");
326 }
327
328 if (0 == strcmp (fnname, "test_fixit_remove"))
329 {
330 const int line = fnstart_line + 2;
331 location_t start = get_loc (line, 8);
332 location_t finish = get_loc (line, 8);
333 rich_location richloc (line_table, make_location (start, start, finish));
334 source_range src_range;
335 src_range.m_start = start;
336 src_range.m_finish = finish;
337 richloc.add_fixit_remove (src_range);
338 warning_at (&richloc, 0, "example of a removal hint");
339 }
340
341 if (0 == strcmp (fnname, "test_fixit_replace"))
342 {
343 const int line = fnstart_line + 2;
344 location_t start = get_loc (line, 2);
345 location_t finish = get_loc (line, 19);
346 rich_location richloc (line_table, make_location (start, start, finish));
347 source_range src_range;
348 src_range.m_start = start;
349 src_range.m_finish = finish;
350 richloc.add_fixit_replace (src_range, "gtk_widget_show_all");
351 warning_at (&richloc, 0, "example of a replacement hint");
352 }
353
354 if (0 == strcmp (fnname, "test_mutually_exclusive_suggestions"))
355 {
356 const int line = fnstart_line + 2;
357 location_t start = get_loc (line, 2);
358 location_t finish = get_loc (line, 9);
359 source_range src_range;
360 src_range.m_start = start;
361 src_range.m_finish = finish;
362
363 {
364 rich_location richloc (line_table, make_location (start, start, finish));
365 richloc.add_fixit_replace (src_range, "replacement_1");
366 richloc.fixits_cannot_be_auto_applied ();
367 warning_at (&richloc, 0, "warning 1");
368 }
369
370 {
371 rich_location richloc (line_table, make_location (start, start, finish));
372 richloc.add_fixit_replace (src_range, "replacement_2");
373 richloc.fixits_cannot_be_auto_applied ();
374 warning_at (&richloc, 0, "warning 2");
375 }
376 }
377
378 /* Tests of gcc_rich_location::add_fixit_insert_formatted. */
379
380 if (0 == strcmp (fnname, "test_add_fixit_insert_formatted_single_line"))
381 {
382 const int line = fnstart_line + 1;
383 location_t insertion_point = get_loc (line, 3);
384 location_t indent = get_loc (line, 2);
385 gcc_rich_location richloc (insertion_point);
386 richloc.add_fixit_insert_formatted ("INSERTED-CONTENT",
387 insertion_point, indent);
388 inform (&richloc, "single-line insertion");
389 }
390
391 if (0 == strcmp (fnname, "test_add_fixit_insert_formatted_multiline"))
392 {
393 location_t insertion_point = fun->function_end_locus;
394 location_t indent = get_loc (fnstart_line + 1, 2);
395 gcc_rich_location richloc (insertion_point);
396 richloc.add_fixit_insert_formatted ("INSERTED-CONTENT",
397 insertion_point, indent);
398 inform (&richloc, "multiline insertion");
399 }
400
401 /* Example of two carets where both carets appear to have an off-by-one
402 error appearing one column early.
403 Seen with gfortran.dg/associate_5.f03.
404 In an earlier version of the printer, the printing of caret 0 aka
405 "1" was suppressed due to it appearing within the leading whitespace
406 before the text in its line. Ensure that we at least faithfully
407 print both carets, at the given (erroneous) locations. */
408 if (0 == strcmp (fnname, "test_caret_on_leading_whitespace"))
409 {
410 const int line = fnstart_line + 3;
411 location_t caret_a = get_loc (line, 5);
412 location_t caret_b = get_loc (line - 1, 19);
413 rich_location richloc (line_table, caret_a);
414 richloc.add_range (caret_b, SHOW_RANGE_WITH_CARET);
415 global_dc->m_source_printing.caret_chars[0] = '1';
416 global_dc->m_source_printing.caret_chars[1] = '2';
417 warning_at (&richloc, 0, "test");
418 global_dc->m_source_printing.caret_chars[0] = '^';
419 global_dc->m_source_printing.caret_chars[1] = '^';
420 }
421
422 /* Example of using the "%q+D" format code, which as well as printing
423 a quoted decl, overrides the given location to use the location of
424 the decl. */
425 if (0 == strcmp (fnname, "test_percent_q_plus_d"))
426 {
427 const int line = fnstart_line + 3;
428 tree local = (*fun->local_decls)[0];
429 warning_at (input_location, 0,
430 "example of plus in format code for %q+D", local);
431 }
432
433 /* Example of many locations and many fixits.
434 Underline (separately) every word in a comment, and convert them
435 to upper case. Give all of the ranges labels (sharing one label). */
436 if (0 == strcmp (fnname, "test_many_nested_locations"))
437 {
438 const char *file = LOCATION_FILE (fnstart);
439 const int start_line = fnstart_line + 2;
440 const int finish_line = start_line + 7;
441 location_t loc = get_loc (start_line - 1, 2);
442 text_range_label label ("label");
443 rich_location richloc (line_table, loc);
444 for (int line = start_line; line <= finish_line; line++)
445 {
446 file_cache &fc = global_dc->get_file_cache ();
447 char_span content = fc.get_source_line (file, line);
448 gcc_assert (content);
449 /* Split line up into words. */
450 for (int idx = 0; idx < content.length (); idx++)
451 {
452 if (ISALPHA (content[idx]))
453 {
454 int start_idx = idx;
455 while (idx < content.length () && ISALPHA (content[idx]))
456 idx++;
457 if (idx == content.length () || !ISALPHA (content[idx]))
458 {
459 location_t start_of_word = get_loc (line, start_idx);
460 location_t end_of_word = get_loc (line, idx - 1);
461 location_t word
462 = make_location (start_of_word, start_of_word,
463 end_of_word);
464 richloc.add_range (word, SHOW_RANGE_WITH_CARET, &label);
465
466 /* Add a fixit, converting to upper case. */
467 char_span word_span = content.subspan (start_idx, idx - start_idx);
468 char *copy = word_span.xstrdup ();
469 for (char *ch = copy; *ch; ch++)
470 *ch = TOUPPER (*ch);
471 richloc.add_fixit_replace (word, copy);
472 free (copy);
473 }
474 }
475 }
476 }
477 /* Verify that we added enough locations to fully exercise
478 rich_location. We want to exceed both the
479 statically-allocated buffer in class rich_location,
480 and then trigger a reallocation of the dynamic buffer. */
481 gcc_assert (richloc.get_num_locations () > 3 + (2 * 16));
482 warning_at (&richloc, 0, "test of %i locations",
483 richloc.get_num_locations ());
484 }
485 }
486
487 unsigned int
488 pass_test_show_locus::execute (function *fun)
489 {
490 test_show_locus (fun);
491 return 0;
492 }
493
494 static gimple_opt_pass *
495 make_pass_test_show_locus (gcc::context *ctxt)
496 {
497 return new pass_test_show_locus (ctxt);
498 }
499
500 int
501 plugin_init (struct plugin_name_args *plugin_info,
502 struct plugin_gcc_version *version)
503 {
504 struct register_pass_info pass_info;
505 const char *plugin_name = plugin_info->base_name;
506 int argc = plugin_info->argc;
507 struct plugin_argument *argv = plugin_info->argv;
508
509 if (!plugin_default_version_check (version, &gcc_version))
510 return 1;
511
512 for (int i = 0; i < argc; i++)
513 {
514 if (0 == strcmp (argv[i].key, "color"))
515 force_show_locus_color = true;
516 }
517
518 pass_info.pass = make_pass_test_show_locus (g);
519 pass_info.reference_pass_name = "ssa";
520 pass_info.ref_pass_instance_number = 1;
521 pass_info.pos_op = PASS_POS_INSERT_AFTER;
522 register_callback (plugin_name, PLUGIN_PASS_MANAGER_SETUP, NULL,
523 &pass_info);
524
525 return 0;
526 }