]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/diagnostic-show-locus.c
Eliminate fixit_hint class hierarchy
[thirdparty/gcc.git] / gcc / diagnostic-show-locus.c
1 /* Diagnostic subroutines for printing source-code
2 Copyright (C) 1999-2017 Free Software Foundation, Inc.
3 Contributed by Gabriel Dos Reis <gdr@codesourcery.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 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "version.h"
25 #include "demangle.h"
26 #include "intl.h"
27 #include "backtrace.h"
28 #include "diagnostic.h"
29 #include "diagnostic-color.h"
30 #include "selftest.h"
31
32 #ifdef HAVE_TERMIOS_H
33 # include <termios.h>
34 #endif
35
36 #ifdef GWINSZ_IN_SYS_IOCTL
37 # include <sys/ioctl.h>
38 #endif
39
40 /* Classes for rendering source code and diagnostics, within an
41 anonymous namespace.
42 The work is done by "class layout", which embeds and uses
43 "class colorizer" and "class layout_range" to get things done. */
44
45 namespace {
46
47 /* The state at a given point of the source code, assuming that we're
48 in a range: which range are we in, and whether we should draw a caret at
49 this point. */
50
51 struct point_state
52 {
53 int range_idx;
54 bool draw_caret_p;
55 };
56
57 /* A class to inject colorization codes when printing the diagnostic locus.
58
59 It has one kind of colorization for each of:
60 - normal text
61 - range 0 (the "primary location")
62 - range 1
63 - range 2
64
65 The class caches the lookup of the color codes for the above.
66
67 The class also has responsibility for tracking which of the above is
68 active, filtering out unnecessary changes. This allows
69 layout::print_source_line and layout::print_annotation_line
70 to simply request a colorization code for *every* character they print,
71 via this class, and have the filtering be done for them here. */
72
73 class colorizer
74 {
75 public:
76 colorizer (diagnostic_context *context,
77 diagnostic_t diagnostic_kind);
78 ~colorizer ();
79
80 void set_range (int range_idx) { set_state (range_idx); }
81 void set_normal_text () { set_state (STATE_NORMAL_TEXT); }
82 void set_fixit_insert () { set_state (STATE_FIXIT_INSERT); }
83 void set_fixit_delete () { set_state (STATE_FIXIT_DELETE); }
84
85 private:
86 void set_state (int state);
87 void begin_state (int state);
88 void finish_state (int state);
89 const char *get_color_by_name (const char *);
90
91 private:
92 static const int STATE_NORMAL_TEXT = -1;
93 static const int STATE_FIXIT_INSERT = -2;
94 static const int STATE_FIXIT_DELETE = -3;
95
96 diagnostic_context *m_context;
97 diagnostic_t m_diagnostic_kind;
98 int m_current_state;
99 const char *m_caret;
100 const char *m_range1;
101 const char *m_range2;
102 const char *m_fixit_insert;
103 const char *m_fixit_delete;
104 const char *m_stop_color;
105 };
106
107 /* A point within a layout_range; similar to an expanded_location,
108 but after filtering on file. */
109
110 class layout_point
111 {
112 public:
113 layout_point (const expanded_location &exploc)
114 : m_line (exploc.line),
115 m_column (exploc.column) {}
116
117 int m_line;
118 int m_column;
119 };
120
121 /* A class for use by "class layout" below: a filtered location_range. */
122
123 class layout_range
124 {
125 public:
126 layout_range (const expanded_location *start_exploc,
127 const expanded_location *finish_exploc,
128 bool show_caret_p,
129 const expanded_location *caret_exploc);
130
131 bool contains_point (int row, int column) const;
132 bool intersects_line_p (int row) const;
133
134 layout_point m_start;
135 layout_point m_finish;
136 bool m_show_caret_p;
137 layout_point m_caret;
138 };
139
140 /* A struct for use by layout::print_source_line for telling
141 layout::print_annotation_line the extents of the source line that
142 it printed, so that underlines can be clipped appropriately. */
143
144 struct line_bounds
145 {
146 int m_first_non_ws;
147 int m_last_non_ws;
148 };
149
150 /* A range of contiguous source lines within a layout (e.g. "lines 5-10"
151 or "line 23"). During the layout ctor, layout::calculate_line_spans
152 splits the pertinent source lines into a list of disjoint line_span
153 instances (e.g. lines 5-10, lines 15-20, line 23). */
154
155 struct line_span
156 {
157 line_span (linenum_type first_line, linenum_type last_line)
158 : m_first_line (first_line), m_last_line (last_line)
159 {
160 gcc_assert (first_line <= last_line);
161 }
162 linenum_type get_first_line () const { return m_first_line; }
163 linenum_type get_last_line () const { return m_last_line; }
164
165 bool contains_line_p (linenum_type line) const
166 {
167 return line >= m_first_line && line <= m_last_line;
168 }
169
170 static int comparator (const void *p1, const void *p2)
171 {
172 const line_span *ls1 = (const line_span *)p1;
173 const line_span *ls2 = (const line_span *)p2;
174 int first_line_diff = (int)ls1->m_first_line - (int)ls2->m_first_line;
175 if (first_line_diff)
176 return first_line_diff;
177 return (int)ls1->m_last_line - (int)ls2->m_last_line;
178 }
179
180 linenum_type m_first_line;
181 linenum_type m_last_line;
182 };
183
184 /* A class to control the overall layout when printing a diagnostic.
185
186 The layout is determined within the constructor.
187 It is then printed by repeatedly calling the "print_source_line",
188 "print_annotation_line" and "print_any_fixits" methods.
189
190 We assume we have disjoint ranges. */
191
192 class layout
193 {
194 public:
195 layout (diagnostic_context *context,
196 rich_location *richloc,
197 diagnostic_t diagnostic_kind);
198
199 int get_num_line_spans () const { return m_line_spans.length (); }
200 const line_span *get_line_span (int idx) const { return &m_line_spans[idx]; }
201
202 bool print_heading_for_line_span_index_p (int line_span_idx) const;
203
204 expanded_location get_expanded_location (const line_span *) const;
205
206 bool print_source_line (int row, line_bounds *lbounds_out);
207 bool should_print_annotation_line_p (int row) const;
208 void print_annotation_line (int row, const line_bounds lbounds);
209 bool annotation_line_showed_range_p (int line, int start_column,
210 int finish_column) const;
211 void print_any_fixits (int row);
212
213 void show_ruler (int max_column) const;
214
215 private:
216 bool validate_fixit_hint_p (const fixit_hint *hint);
217
218 void calculate_line_spans ();
219
220 void print_newline ();
221
222 bool
223 get_state_at_point (/* Inputs. */
224 int row, int column,
225 int first_non_ws, int last_non_ws,
226 /* Outputs. */
227 point_state *out_state);
228
229 int
230 get_x_bound_for_row (int row, int caret_column,
231 int last_non_ws);
232
233 void
234 move_to_column (int *column, int dest_column);
235
236 private:
237 diagnostic_context *m_context;
238 pretty_printer *m_pp;
239 diagnostic_t m_diagnostic_kind;
240 expanded_location m_exploc;
241 colorizer m_colorizer;
242 bool m_colorize_source_p;
243 auto_vec <layout_range> m_layout_ranges;
244 auto_vec <const fixit_hint *> m_fixit_hints;
245 auto_vec <line_span> m_line_spans;
246 int m_x_offset;
247 };
248
249 /* Implementation of "class colorizer". */
250
251 /* The constructor for "colorizer". Lookup and store color codes for the
252 different kinds of things we might need to print. */
253
254 colorizer::colorizer (diagnostic_context *context,
255 diagnostic_t diagnostic_kind) :
256 m_context (context),
257 m_diagnostic_kind (diagnostic_kind),
258 m_current_state (STATE_NORMAL_TEXT)
259 {
260 m_range1 = get_color_by_name ("range1");
261 m_range2 = get_color_by_name ("range2");
262 m_fixit_insert = get_color_by_name ("fixit-insert");
263 m_fixit_delete = get_color_by_name ("fixit-delete");
264 m_stop_color = colorize_stop (pp_show_color (context->printer));
265 }
266
267 /* The destructor for "colorize". If colorization is on, print a code to
268 turn it off. */
269
270 colorizer::~colorizer ()
271 {
272 finish_state (m_current_state);
273 }
274
275 /* Update state, printing color codes if necessary if there's a state
276 change. */
277
278 void
279 colorizer::set_state (int new_state)
280 {
281 if (m_current_state != new_state)
282 {
283 finish_state (m_current_state);
284 m_current_state = new_state;
285 begin_state (new_state);
286 }
287 }
288
289 /* Turn on any colorization for STATE. */
290
291 void
292 colorizer::begin_state (int state)
293 {
294 switch (state)
295 {
296 case STATE_NORMAL_TEXT:
297 break;
298
299 case STATE_FIXIT_INSERT:
300 pp_string (m_context->printer, m_fixit_insert);
301 break;
302
303 case STATE_FIXIT_DELETE:
304 pp_string (m_context->printer, m_fixit_delete);
305 break;
306
307 case 0:
308 /* Make range 0 be the same color as the "kind" text
309 (error vs warning vs note). */
310 pp_string
311 (m_context->printer,
312 colorize_start (pp_show_color (m_context->printer),
313 diagnostic_get_color_for_kind (m_diagnostic_kind)));
314 break;
315
316 case 1:
317 pp_string (m_context->printer, m_range1);
318 break;
319
320 case 2:
321 pp_string (m_context->printer, m_range2);
322 break;
323
324 default:
325 /* For ranges beyond 2, alternate between color 1 and color 2. */
326 {
327 gcc_assert (state > 2);
328 pp_string (m_context->printer,
329 state % 2 ? m_range1 : m_range2);
330 }
331 break;
332 }
333 }
334
335 /* Turn off any colorization for STATE. */
336
337 void
338 colorizer::finish_state (int state)
339 {
340 if (state != STATE_NORMAL_TEXT)
341 pp_string (m_context->printer, m_stop_color);
342 }
343
344 /* Get the color code for NAME (or the empty string if
345 colorization is disabled). */
346
347 const char *
348 colorizer::get_color_by_name (const char *name)
349 {
350 return colorize_start (pp_show_color (m_context->printer), name);
351 }
352
353 /* Implementation of class layout_range. */
354
355 /* The constructor for class layout_range.
356 Initialize various layout_point fields from expanded_location
357 equivalents; we've already filtered on file. */
358
359 layout_range::layout_range (const expanded_location *start_exploc,
360 const expanded_location *finish_exploc,
361 bool show_caret_p,
362 const expanded_location *caret_exploc)
363 : m_start (*start_exploc),
364 m_finish (*finish_exploc),
365 m_show_caret_p (show_caret_p),
366 m_caret (*caret_exploc)
367 {
368 }
369
370 /* Is (column, row) within the given range?
371 We've already filtered on the file.
372
373 Ranges are closed (both limits are within the range).
374
375 Example A: a single-line range:
376 start: (col=22, line=2)
377 finish: (col=38, line=2)
378
379 |00000011111111112222222222333333333344444444444
380 |34567890123456789012345678901234567890123456789
381 --+-----------------------------------------------
382 01|bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb
383 02|bbbbbbbbbbbbbbbbbbbSwwwwwwwwwwwwwwwFaaaaaaaaaaa
384 03|aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
385
386 Example B: a multiline range with
387 start: (col=14, line=3)
388 finish: (col=08, line=5)
389
390 |00000011111111112222222222333333333344444444444
391 |34567890123456789012345678901234567890123456789
392 --+-----------------------------------------------
393 01|bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb
394 02|bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb
395 03|bbbbbbbbbbbSwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwww
396 04|wwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwww
397 05|wwwwwFaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
398 06|aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
399 --+-----------------------------------------------
400
401 Legend:
402 - 'b' indicates a point *before* the range
403 - 'S' indicates the start of the range
404 - 'w' indicates a point within the range
405 - 'F' indicates the finish of the range (which is
406 within it).
407 - 'a' indicates a subsequent point *after* the range. */
408
409 bool
410 layout_range::contains_point (int row, int column) const
411 {
412 gcc_assert (m_start.m_line <= m_finish.m_line);
413 /* ...but the equivalent isn't true for the columns;
414 consider example B in the comment above. */
415
416 if (row < m_start.m_line)
417 /* Points before the first line of the range are
418 outside it (corresponding to line 01 in example A
419 and lines 01 and 02 in example B above). */
420 return false;
421
422 if (row == m_start.m_line)
423 /* On same line as start of range (corresponding
424 to line 02 in example A and line 03 in example B). */
425 {
426 if (column < m_start.m_column)
427 /* Points on the starting line of the range, but
428 before the column in which it begins. */
429 return false;
430
431 if (row < m_finish.m_line)
432 /* This is a multiline range; the point
433 is within it (corresponds to line 03 in example B
434 from column 14 onwards) */
435 return true;
436 else
437 {
438 /* This is a single-line range. */
439 gcc_assert (row == m_finish.m_line);
440 return column <= m_finish.m_column;
441 }
442 }
443
444 /* The point is in a line beyond that containing the
445 start of the range: lines 03 onwards in example A,
446 and lines 04 onwards in example B. */
447 gcc_assert (row > m_start.m_line);
448
449 if (row > m_finish.m_line)
450 /* The point is beyond the final line of the range
451 (lines 03 onwards in example A, and lines 06 onwards
452 in example B). */
453 return false;
454
455 if (row < m_finish.m_line)
456 {
457 /* The point is in a line that's fully within a multiline
458 range (e.g. line 04 in example B). */
459 gcc_assert (m_start.m_line < m_finish.m_line);
460 return true;
461 }
462
463 gcc_assert (row == m_finish.m_line);
464
465 return column <= m_finish.m_column;
466 }
467
468 /* Does this layout_range contain any part of line ROW? */
469
470 bool
471 layout_range::intersects_line_p (int row) const
472 {
473 gcc_assert (m_start.m_line <= m_finish.m_line);
474 if (row < m_start.m_line)
475 return false;
476 if (row > m_finish.m_line)
477 return false;
478 return true;
479 }
480
481 #if CHECKING_P
482
483 /* A helper function for testing layout_range. */
484
485 static layout_range
486 make_range (int start_line, int start_col, int end_line, int end_col)
487 {
488 const expanded_location start_exploc
489 = {"test.c", start_line, start_col, NULL, false};
490 const expanded_location finish_exploc
491 = {"test.c", end_line, end_col, NULL, false};
492 return layout_range (&start_exploc, &finish_exploc, false,
493 &start_exploc);
494 }
495
496 /* Selftests for layout_range::contains_point and
497 layout_range::intersects_line_p. */
498
499 /* Selftest for layout_range, where the layout_range
500 is a range with start==end i.e. a single point. */
501
502 static void
503 test_layout_range_for_single_point ()
504 {
505 layout_range point = make_range (7, 10, 7, 10);
506
507 /* Tests for layout_range::contains_point. */
508
509 /* Before the line. */
510 ASSERT_FALSE (point.contains_point (6, 1));
511
512 /* On the line, but before start. */
513 ASSERT_FALSE (point.contains_point (7, 9));
514
515 /* At the point. */
516 ASSERT_TRUE (point.contains_point (7, 10));
517
518 /* On the line, after the point. */
519 ASSERT_FALSE (point.contains_point (7, 11));
520
521 /* After the line. */
522 ASSERT_FALSE (point.contains_point (8, 1));
523
524 /* Tests for layout_range::intersects_line_p. */
525 ASSERT_FALSE (point.intersects_line_p (6));
526 ASSERT_TRUE (point.intersects_line_p (7));
527 ASSERT_FALSE (point.intersects_line_p (8));
528 }
529
530 /* Selftest for layout_range, where the layout_range
531 is the single-line range shown as "Example A" above. */
532
533 static void
534 test_layout_range_for_single_line ()
535 {
536 layout_range example_a = make_range (2, 22, 2, 38);
537
538 /* Tests for layout_range::contains_point. */
539
540 /* Before the line. */
541 ASSERT_FALSE (example_a.contains_point (1, 1));
542
543 /* On the line, but before start. */
544 ASSERT_FALSE (example_a.contains_point (2, 21));
545
546 /* On the line, at the start. */
547 ASSERT_TRUE (example_a.contains_point (2, 22));
548
549 /* On the line, within the range. */
550 ASSERT_TRUE (example_a.contains_point (2, 23));
551
552 /* On the line, at the end. */
553 ASSERT_TRUE (example_a.contains_point (2, 38));
554
555 /* On the line, after the end. */
556 ASSERT_FALSE (example_a.contains_point (2, 39));
557
558 /* After the line. */
559 ASSERT_FALSE (example_a.contains_point (2, 39));
560
561 /* Tests for layout_range::intersects_line_p. */
562 ASSERT_FALSE (example_a.intersects_line_p (1));
563 ASSERT_TRUE (example_a.intersects_line_p (2));
564 ASSERT_FALSE (example_a.intersects_line_p (3));
565 }
566
567 /* Selftest for layout_range, where the layout_range
568 is the multi-line range shown as "Example B" above. */
569
570 static void
571 test_layout_range_for_multiple_lines ()
572 {
573 layout_range example_b = make_range (3, 14, 5, 8);
574
575 /* Tests for layout_range::contains_point. */
576
577 /* Before first line. */
578 ASSERT_FALSE (example_b.contains_point (1, 1));
579
580 /* On the first line, but before start. */
581 ASSERT_FALSE (example_b.contains_point (3, 13));
582
583 /* At the start. */
584 ASSERT_TRUE (example_b.contains_point (3, 14));
585
586 /* On the first line, within the range. */
587 ASSERT_TRUE (example_b.contains_point (3, 15));
588
589 /* On an interior line.
590 The column number should not matter; try various boundary
591 values. */
592 ASSERT_TRUE (example_b.contains_point (4, 1));
593 ASSERT_TRUE (example_b.contains_point (4, 7));
594 ASSERT_TRUE (example_b.contains_point (4, 8));
595 ASSERT_TRUE (example_b.contains_point (4, 9));
596 ASSERT_TRUE (example_b.contains_point (4, 13));
597 ASSERT_TRUE (example_b.contains_point (4, 14));
598 ASSERT_TRUE (example_b.contains_point (4, 15));
599
600 /* On the final line, before the end. */
601 ASSERT_TRUE (example_b.contains_point (5, 7));
602
603 /* On the final line, at the end. */
604 ASSERT_TRUE (example_b.contains_point (5, 8));
605
606 /* On the final line, after the end. */
607 ASSERT_FALSE (example_b.contains_point (5, 9));
608
609 /* After the line. */
610 ASSERT_FALSE (example_b.contains_point (6, 1));
611
612 /* Tests for layout_range::intersects_line_p. */
613 ASSERT_FALSE (example_b.intersects_line_p (2));
614 ASSERT_TRUE (example_b.intersects_line_p (3));
615 ASSERT_TRUE (example_b.intersects_line_p (4));
616 ASSERT_TRUE (example_b.intersects_line_p (5));
617 ASSERT_FALSE (example_b.intersects_line_p (6));
618 }
619
620 #endif /* #if CHECKING_P */
621
622 /* Given a source line LINE of length LINE_WIDTH, determine the width
623 without any trailing whitespace. */
624
625 static int
626 get_line_width_without_trailing_whitespace (const char *line, int line_width)
627 {
628 int result = line_width;
629 while (result > 0)
630 {
631 char ch = line[result - 1];
632 if (ch == ' ' || ch == '\t')
633 result--;
634 else
635 break;
636 }
637 gcc_assert (result >= 0);
638 gcc_assert (result <= line_width);
639 gcc_assert (result == 0 ||
640 (line[result - 1] != ' '
641 && line[result -1] != '\t'));
642 return result;
643 }
644
645 #if CHECKING_P
646
647 /* A helper function for testing get_line_width_without_trailing_whitespace. */
648
649 static void
650 assert_eq (const char *line, int expected_width)
651 {
652 int actual_value
653 = get_line_width_without_trailing_whitespace (line, strlen (line));
654 ASSERT_EQ (actual_value, expected_width);
655 }
656
657 /* Verify that get_line_width_without_trailing_whitespace is sane for
658 various inputs. It is not required to handle newlines. */
659
660 static void
661 test_get_line_width_without_trailing_whitespace ()
662 {
663 assert_eq ("", 0);
664 assert_eq (" ", 0);
665 assert_eq ("\t", 0);
666 assert_eq ("hello world", 11);
667 assert_eq ("hello world ", 11);
668 assert_eq ("hello world \t\t ", 11);
669 }
670
671 #endif /* #if CHECKING_P */
672
673 /* Helper function for layout's ctor, for sanitizing locations relative
674 to the primary location within a diagnostic.
675
676 Compare LOC_A and LOC_B to see if it makes sense to print underlines
677 connecting their expanded locations. Doing so is only guaranteed to
678 make sense if the locations share the same macro expansion "history"
679 i.e. they can be traced through the same macro expansions, eventually
680 reaching an ordinary map.
681
682 This may be too strong a condition, but it effectively sanitizes
683 PR c++/70105, which has an example of printing an expression where the
684 final location of the expression is in a different macro, which
685 erroneously was leading to hundreds of lines of irrelevant source
686 being printed. */
687
688 static bool
689 compatible_locations_p (location_t loc_a, location_t loc_b)
690 {
691 if (IS_ADHOC_LOC (loc_a))
692 loc_a = get_location_from_adhoc_loc (line_table, loc_a);
693 if (IS_ADHOC_LOC (loc_b))
694 loc_b = get_location_from_adhoc_loc (line_table, loc_b);
695
696 /* If either location is one of the special locations outside of a
697 linemap, they are only compatible if they are equal. */
698 if (loc_a < RESERVED_LOCATION_COUNT
699 || loc_b < RESERVED_LOCATION_COUNT)
700 return loc_a == loc_b;
701
702 const line_map *map_a = linemap_lookup (line_table, loc_a);
703 linemap_assert (map_a);
704
705 const line_map *map_b = linemap_lookup (line_table, loc_b);
706 linemap_assert (map_b);
707
708 /* Are they within the same map? */
709 if (map_a == map_b)
710 {
711 /* Are both within the same macro expansion? */
712 if (linemap_macro_expansion_map_p (map_a))
713 {
714 /* Expand each location towards the spelling location, and
715 recurse. */
716 const line_map_macro *macro_map = linemap_check_macro (map_a);
717 source_location loc_a_toward_spelling
718 = linemap_macro_map_loc_unwind_toward_spelling (line_table,
719 macro_map,
720 loc_a);
721 source_location loc_b_toward_spelling
722 = linemap_macro_map_loc_unwind_toward_spelling (line_table,
723 macro_map,
724 loc_b);
725 return compatible_locations_p (loc_a_toward_spelling,
726 loc_b_toward_spelling);
727 }
728
729 /* Otherwise they are within the same ordinary map. */
730 return true;
731 }
732 else
733 {
734 /* Within different maps. */
735
736 /* If either is within a macro expansion, they are incompatible. */
737 if (linemap_macro_expansion_map_p (map_a)
738 || linemap_macro_expansion_map_p (map_b))
739 return false;
740
741 /* Within two different ordinary maps; they are compatible iff they
742 are in the same file. */
743 const line_map_ordinary *ord_map_a = linemap_check_ordinary (map_a);
744 const line_map_ordinary *ord_map_b = linemap_check_ordinary (map_b);
745 return ord_map_a->to_file == ord_map_b->to_file;
746 }
747 }
748
749 /* Implementation of class layout. */
750
751 /* Constructor for class layout.
752
753 Filter the ranges from the rich_location to those that we can
754 sanely print, populating m_layout_ranges and m_fixit_hints.
755 Determine the range of lines that we will print, splitting them
756 up into an ordered list of disjoint spans of contiguous line numbers.
757 Determine m_x_offset, to ensure that the primary caret
758 will fit within the max_width provided by the diagnostic_context. */
759
760 layout::layout (diagnostic_context * context,
761 rich_location *richloc,
762 diagnostic_t diagnostic_kind)
763 : m_context (context),
764 m_pp (context->printer),
765 m_diagnostic_kind (diagnostic_kind),
766 m_exploc (richloc->get_expanded_location (0)),
767 m_colorizer (context, diagnostic_kind),
768 m_colorize_source_p (context->colorize_source_p),
769 m_layout_ranges (richloc->get_num_locations ()),
770 m_fixit_hints (richloc->get_num_fixit_hints ()),
771 m_line_spans (1 + richloc->get_num_locations ()),
772 m_x_offset (0)
773 {
774 source_location primary_loc = richloc->get_range (0)->m_loc;
775
776 for (unsigned int idx = 0; idx < richloc->get_num_locations (); idx++)
777 {
778 /* This diagnostic printer can only cope with "sufficiently sane" ranges.
779 Ignore any ranges that are awkward to handle. */
780 const location_range *loc_range = richloc->get_range (idx);
781
782 /* Split the "range" into caret and range information. */
783 source_range src_range = get_range_from_loc (line_table, loc_range->m_loc);
784
785 /* Expand the various locations. */
786 expanded_location start
787 = linemap_client_expand_location_to_spelling_point (src_range.m_start);
788 expanded_location finish
789 = linemap_client_expand_location_to_spelling_point (src_range.m_finish);
790 expanded_location caret
791 = linemap_client_expand_location_to_spelling_point (loc_range->m_loc);
792
793 /* If any part of the range isn't in the same file as the primary
794 location of this diagnostic, ignore the range. */
795 if (start.file != m_exploc.file)
796 continue;
797 if (finish.file != m_exploc.file)
798 continue;
799 if (loc_range->m_show_caret_p)
800 if (caret.file != m_exploc.file)
801 continue;
802
803 /* Sanitize the caret location for non-primary ranges. */
804 if (m_layout_ranges.length () > 0)
805 if (loc_range->m_show_caret_p)
806 if (!compatible_locations_p (loc_range->m_loc, primary_loc))
807 /* Discard any non-primary ranges that can't be printed
808 sanely relative to the primary location. */
809 continue;
810
811 /* Everything is now known to be in the correct source file,
812 but it may require further sanitization. */
813 layout_range ri (&start, &finish, loc_range->m_show_caret_p, &caret);
814
815 /* If we have a range that finishes before it starts (perhaps
816 from something built via macro expansion), printing the
817 range is likely to be nonsensical. Also, attempting to do so
818 breaks assumptions within the printing code (PR c/68473).
819 Similarly, don't attempt to print ranges if one or both ends
820 of the range aren't sane to print relative to the
821 primary location (PR c++/70105). */
822 if (start.line > finish.line
823 || !compatible_locations_p (src_range.m_start, primary_loc)
824 || !compatible_locations_p (src_range.m_finish, primary_loc))
825 {
826 /* Is this the primary location? */
827 if (m_layout_ranges.length () == 0)
828 {
829 /* We want to print the caret for the primary location, but
830 we must sanitize away m_start and m_finish. */
831 ri.m_start = ri.m_caret;
832 ri.m_finish = ri.m_caret;
833 }
834 else
835 /* This is a non-primary range; ignore it. */
836 continue;
837 }
838
839 /* Passed all the tests; add the range to m_layout_ranges so that
840 it will be printed. */
841 m_layout_ranges.safe_push (ri);
842 }
843
844 /* Populate m_fixit_hints, filtering to only those that are in the
845 same file. */
846 for (unsigned int i = 0; i < richloc->get_num_fixit_hints (); i++)
847 {
848 const fixit_hint *hint = richloc->get_fixit_hint (i);
849 if (validate_fixit_hint_p (hint))
850 m_fixit_hints.safe_push (hint);
851 }
852
853 /* Populate m_line_spans. */
854 calculate_line_spans ();
855
856 /* Adjust m_x_offset.
857 Center the primary caret to fit in max_width; all columns
858 will be adjusted accordingly. */
859 int max_width = m_context->caret_max_width;
860 int line_width;
861 const char *line = location_get_source_line (m_exploc.file, m_exploc.line,
862 &line_width);
863 if (line && m_exploc.column <= line_width)
864 {
865 int right_margin = CARET_LINE_MARGIN;
866 int column = m_exploc.column;
867 right_margin = MIN (line_width - column, right_margin);
868 right_margin = max_width - right_margin;
869 if (line_width >= max_width && column > right_margin)
870 m_x_offset = column - right_margin;
871 gcc_assert (m_x_offset >= 0);
872 }
873
874 if (context->show_ruler_p)
875 show_ruler (m_x_offset + max_width);
876 }
877
878 /* Return true iff we should print a heading when starting the
879 line span with the given index. */
880
881 bool
882 layout::print_heading_for_line_span_index_p (int line_span_idx) const
883 {
884 /* We print a heading for every change of line span, hence for every
885 line span after the initial one. */
886 if (line_span_idx > 0)
887 return true;
888
889 /* We also do it for the initial span if the primary location of the
890 diagnostic is in a different span. */
891 if (m_exploc.line > (int)get_line_span (0)->m_last_line)
892 return true;
893
894 return false;
895 }
896
897 /* Get an expanded_location for the first location of interest within
898 the given line_span.
899 Used when printing a heading to indicate a new line span. */
900
901 expanded_location
902 layout::get_expanded_location (const line_span *line_span) const
903 {
904 /* Whenever possible, use the caret location. */
905 if (line_span->contains_line_p (m_exploc.line))
906 return m_exploc;
907
908 /* Otherwise, use the start of the first range that's present
909 within the line_span. */
910 for (unsigned int i = 0; i < m_layout_ranges.length (); i++)
911 {
912 const layout_range *lr = &m_layout_ranges[i];
913 if (line_span->contains_line_p (lr->m_start.m_line))
914 {
915 expanded_location exploc = m_exploc;
916 exploc.line = lr->m_start.m_line;
917 exploc.column = lr->m_start.m_column;
918 return exploc;
919 }
920 }
921
922 /* Otherwise, use the location of the first fixit-hint present within
923 the line_span. */
924 for (unsigned int i = 0; i < m_fixit_hints.length (); i++)
925 {
926 const fixit_hint *hint = m_fixit_hints[i];
927 location_t loc = hint->get_start_loc ();
928 expanded_location exploc = expand_location (loc);
929 if (line_span->contains_line_p (exploc.line))
930 return exploc;
931 }
932
933 /* It should not be possible to have a line span that didn't
934 contain any of the layout_range or fixit_hint instances. */
935 gcc_unreachable ();
936 return m_exploc;
937 }
938
939 /* Determine if HINT is meaningful to print within this layout. */
940
941 bool
942 layout::validate_fixit_hint_p (const fixit_hint *hint)
943 {
944 if (LOCATION_FILE (hint->get_start_loc ()) != m_exploc.file)
945 return false;
946 if (LOCATION_FILE (hint->get_next_loc ()) != m_exploc.file)
947 return false;
948
949 return true;
950 }
951
952 /* Determine the range of lines affected by HINT.
953 This assumes that HINT has already been filtered by
954 validate_fixit_hint_p, and so affects the correct source file. */
955
956 static line_span
957 get_line_span_for_fixit_hint (const fixit_hint *hint)
958 {
959 gcc_assert (hint);
960 return line_span (LOCATION_LINE (hint->get_start_loc ()),
961 LOCATION_LINE (hint->get_next_loc ()));
962 }
963
964 /* We want to print the pertinent source code at a diagnostic. The
965 rich_location can contain multiple locations. This will have been
966 filtered into m_exploc (the caret for the primary location) and
967 m_layout_ranges, for those ranges within the same source file.
968
969 We will print a subset of the lines within the source file in question,
970 as a collection of "spans" of lines.
971
972 This function populates m_line_spans with an ordered, disjoint list of
973 the line spans of interest.
974
975 For example, if the primary caret location is on line 7, with ranges
976 covering lines 5-6 and lines 9-12:
977
978 004
979 005 |RANGE 0
980 006 |RANGE 0
981 007 |PRIMARY CARET
982 008
983 009 |RANGE 1
984 010 |RANGE 1
985 011 |RANGE 1
986 012 |RANGE 1
987 013
988
989 then we want two spans: lines 5-7 and lines 9-12. */
990
991 void
992 layout::calculate_line_spans ()
993 {
994 /* This should only be called once, by the ctor. */
995 gcc_assert (m_line_spans.length () == 0);
996
997 /* Populate tmp_spans with individual spans, for each of
998 m_exploc, and for m_layout_ranges. */
999 auto_vec<line_span> tmp_spans (1 + m_layout_ranges.length ());
1000 tmp_spans.safe_push (line_span (m_exploc.line, m_exploc.line));
1001 for (unsigned int i = 0; i < m_layout_ranges.length (); i++)
1002 {
1003 const layout_range *lr = &m_layout_ranges[i];
1004 gcc_assert (lr->m_start.m_line <= lr->m_finish.m_line);
1005 tmp_spans.safe_push (line_span (lr->m_start.m_line,
1006 lr->m_finish.m_line));
1007 }
1008
1009 /* Also add spans for any fix-it hints, in case they cover other lines. */
1010 for (unsigned int i = 0; i < m_fixit_hints.length (); i++)
1011 {
1012 const fixit_hint *hint = m_fixit_hints[i];
1013 gcc_assert (hint);
1014 tmp_spans.safe_push (get_line_span_for_fixit_hint (hint));
1015 }
1016
1017 /* Sort them. */
1018 tmp_spans.qsort(line_span::comparator);
1019
1020 /* Now iterate through tmp_spans, copying into m_line_spans, and
1021 combining where possible. */
1022 gcc_assert (tmp_spans.length () > 0);
1023 m_line_spans.safe_push (tmp_spans[0]);
1024 for (unsigned int i = 1; i < tmp_spans.length (); i++)
1025 {
1026 line_span *current = &m_line_spans[m_line_spans.length () - 1];
1027 const line_span *next = &tmp_spans[i];
1028 gcc_assert (next->m_first_line >= current->m_first_line);
1029 if (next->m_first_line <= current->m_last_line + 1)
1030 {
1031 /* We can merge them. */
1032 if (next->m_last_line > current->m_last_line)
1033 current->m_last_line = next->m_last_line;
1034 }
1035 else
1036 {
1037 /* No merger possible. */
1038 m_line_spans.safe_push (*next);
1039 }
1040 }
1041
1042 /* Verify the result, in m_line_spans. */
1043 gcc_assert (m_line_spans.length () > 0);
1044 for (unsigned int i = 1; i < m_line_spans.length (); i++)
1045 {
1046 const line_span *prev = &m_line_spans[i - 1];
1047 const line_span *next = &m_line_spans[i];
1048 /* The individual spans must be sane. */
1049 gcc_assert (prev->m_first_line <= prev->m_last_line);
1050 gcc_assert (next->m_first_line <= next->m_last_line);
1051 /* The spans must be ordered. */
1052 gcc_assert (prev->m_first_line < next->m_first_line);
1053 /* There must be a gap of at least one line between separate spans. */
1054 gcc_assert ((prev->m_last_line + 1) < next->m_first_line);
1055 }
1056 }
1057
1058 /* Attempt to print line ROW of source code, potentially colorized at any
1059 ranges.
1060 Return true if the line was printed, populating *LBOUNDS_OUT.
1061 Return false if the source line could not be read, leaving *LBOUNDS_OUT
1062 untouched. */
1063
1064 bool
1065 layout::print_source_line (int row, line_bounds *lbounds_out)
1066 {
1067 int line_width;
1068 const char *line = location_get_source_line (m_exploc.file, row,
1069 &line_width);
1070 if (!line)
1071 return false;
1072
1073 m_colorizer.set_normal_text ();
1074
1075 /* We will stop printing the source line at any trailing
1076 whitespace. */
1077 line_width = get_line_width_without_trailing_whitespace (line,
1078 line_width);
1079 line += m_x_offset;
1080
1081 pp_space (m_pp);
1082 int first_non_ws = INT_MAX;
1083 int last_non_ws = 0;
1084 int column;
1085 for (column = 1 + m_x_offset; column <= line_width; column++)
1086 {
1087 /* Assuming colorization is enabled for the caret and underline
1088 characters, we may also colorize the associated characters
1089 within the source line.
1090
1091 For frontends that generate range information, we color the
1092 associated characters in the source line the same as the
1093 carets and underlines in the annotation line, to make it easier
1094 for the reader to see the pertinent code.
1095
1096 For frontends that only generate carets, we don't colorize the
1097 characters above them, since this would look strange (e.g.
1098 colorizing just the first character in a token). */
1099 if (m_colorize_source_p)
1100 {
1101 bool in_range_p;
1102 point_state state;
1103 in_range_p = get_state_at_point (row, column,
1104 0, INT_MAX,
1105 &state);
1106 if (in_range_p)
1107 m_colorizer.set_range (state.range_idx);
1108 else
1109 m_colorizer.set_normal_text ();
1110 }
1111 char c = *line == '\t' ? ' ' : *line;
1112 if (c == '\0')
1113 c = ' ';
1114 if (c != ' ')
1115 {
1116 last_non_ws = column;
1117 if (first_non_ws == INT_MAX)
1118 first_non_ws = column;
1119 }
1120 pp_character (m_pp, c);
1121 line++;
1122 }
1123 print_newline ();
1124
1125 lbounds_out->m_first_non_ws = first_non_ws;
1126 lbounds_out->m_last_non_ws = last_non_ws;
1127 return true;
1128 }
1129
1130 /* Determine if we should print an annotation line for ROW.
1131 i.e. if any of m_layout_ranges contains ROW. */
1132
1133 bool
1134 layout::should_print_annotation_line_p (int row) const
1135 {
1136 layout_range *range;
1137 int i;
1138 FOR_EACH_VEC_ELT (m_layout_ranges, i, range)
1139 if (range->intersects_line_p (row))
1140 return true;
1141 return false;
1142 }
1143
1144 /* Print a line consisting of the caret/underlines for the given
1145 source line. */
1146
1147 void
1148 layout::print_annotation_line (int row, const line_bounds lbounds)
1149 {
1150 int x_bound = get_x_bound_for_row (row, m_exploc.column,
1151 lbounds.m_last_non_ws);
1152
1153 pp_space (m_pp);
1154 for (int column = 1 + m_x_offset; column < x_bound; column++)
1155 {
1156 bool in_range_p;
1157 point_state state;
1158 in_range_p = get_state_at_point (row, column,
1159 lbounds.m_first_non_ws,
1160 lbounds.m_last_non_ws,
1161 &state);
1162 if (in_range_p)
1163 {
1164 /* Within a range. Draw either the caret or an underline. */
1165 m_colorizer.set_range (state.range_idx);
1166 if (state.draw_caret_p)
1167 {
1168 /* Draw the caret. */
1169 char caret_char;
1170 if (state.range_idx < rich_location::STATICALLY_ALLOCATED_RANGES)
1171 caret_char = m_context->caret_chars[state.range_idx];
1172 else
1173 caret_char = '^';
1174 pp_character (m_pp, caret_char);
1175 }
1176 else
1177 pp_character (m_pp, '~');
1178 }
1179 else
1180 {
1181 /* Not in a range. */
1182 m_colorizer.set_normal_text ();
1183 pp_character (m_pp, ' ');
1184 }
1185 }
1186 print_newline ();
1187 }
1188
1189 /* Subroutine of layout::print_any_fixits.
1190
1191 Determine if the annotation line printed for LINE contained
1192 the exact range from START_COLUMN to FINISH_COLUMN. */
1193
1194 bool
1195 layout::annotation_line_showed_range_p (int line, int start_column,
1196 int finish_column) const
1197 {
1198 layout_range *range;
1199 int i;
1200 FOR_EACH_VEC_ELT (m_layout_ranges, i, range)
1201 if (range->m_start.m_line == line
1202 && range->m_start.m_column == start_column
1203 && range->m_finish.m_line == line
1204 && range->m_finish.m_column == finish_column)
1205 return true;
1206 return false;
1207 }
1208
1209 /* If there are any fixit hints on source line ROW, print them.
1210 They are printed in order, attempting to combine them onto lines, but
1211 starting new lines if necessary. */
1212
1213 void
1214 layout::print_any_fixits (int row)
1215 {
1216 int column = 0;
1217 for (unsigned int i = 0; i < m_fixit_hints.length (); i++)
1218 {
1219 const fixit_hint *hint = m_fixit_hints[i];
1220 if (hint->affects_line_p (m_exploc.file, row))
1221 {
1222 /* For now we assume each fixit hint can only touch one line. */
1223 if (hint->insertion_p ())
1224 {
1225 /* This assumes the insertion just affects one line. */
1226 int start_column = LOCATION_COLUMN (hint->get_start_loc ());
1227 move_to_column (&column, start_column);
1228 m_colorizer.set_fixit_insert ();
1229 pp_string (m_pp, hint->get_string ());
1230 m_colorizer.set_normal_text ();
1231 column += hint->get_length ();
1232 }
1233 else
1234 {
1235 int line = LOCATION_LINE (hint->get_start_loc ());
1236 int start_column = LOCATION_COLUMN (hint->get_start_loc ());
1237 int finish_column = LOCATION_COLUMN (hint->get_next_loc ()) - 1;
1238
1239 /* If the range of the replacement wasn't printed in the
1240 annotation line, then print an extra underline to
1241 indicate exactly what is being replaced.
1242 Always show it for removals. */
1243 if (!annotation_line_showed_range_p (line, start_column,
1244 finish_column)
1245 || hint->get_length () == 0)
1246 {
1247 move_to_column (&column, start_column);
1248 m_colorizer.set_fixit_delete ();
1249 for (; column <= finish_column; column++)
1250 pp_character (m_pp, '-');
1251 m_colorizer.set_normal_text ();
1252 }
1253 /* Print the replacement text. REPLACE also covers
1254 removals, so only do this extra work (potentially starting
1255 a new line) if we have actual replacement text. */
1256 if (hint->get_length () > 0)
1257 {
1258 move_to_column (&column, start_column);
1259 m_colorizer.set_fixit_insert ();
1260 pp_string (m_pp, hint->get_string ());
1261 m_colorizer.set_normal_text ();
1262 column += hint->get_length ();
1263 }
1264 }
1265 }
1266 }
1267
1268 /* Add a trailing newline, if necessary. */
1269 move_to_column (&column, 0);
1270 }
1271
1272 /* Disable any colorization and emit a newline. */
1273
1274 void
1275 layout::print_newline ()
1276 {
1277 m_colorizer.set_normal_text ();
1278 pp_newline (m_pp);
1279 }
1280
1281 /* Return true if (ROW/COLUMN) is within a range of the layout.
1282 If it returns true, OUT_STATE is written to, with the
1283 range index, and whether we should draw the caret at
1284 (ROW/COLUMN) (as opposed to an underline). */
1285
1286 bool
1287 layout::get_state_at_point (/* Inputs. */
1288 int row, int column,
1289 int first_non_ws, int last_non_ws,
1290 /* Outputs. */
1291 point_state *out_state)
1292 {
1293 layout_range *range;
1294 int i;
1295 FOR_EACH_VEC_ELT (m_layout_ranges, i, range)
1296 {
1297 if (range->contains_point (row, column))
1298 {
1299 out_state->range_idx = i;
1300
1301 /* Are we at the range's caret? is it visible? */
1302 out_state->draw_caret_p = false;
1303 if (range->m_show_caret_p
1304 && row == range->m_caret.m_line
1305 && column == range->m_caret.m_column)
1306 out_state->draw_caret_p = true;
1307
1308 /* Within a multiline range, don't display any underline
1309 in any leading or trailing whitespace on a line.
1310 We do display carets, however. */
1311 if (!out_state->draw_caret_p)
1312 if (column < first_non_ws || column > last_non_ws)
1313 return false;
1314
1315 /* We are within a range. */
1316 return true;
1317 }
1318 }
1319
1320 return false;
1321 }
1322
1323 /* Helper function for use by layout::print_line when printing the
1324 annotation line under the source line.
1325 Get the column beyond the rightmost one that could contain a caret or
1326 range marker, given that we stop rendering at trailing whitespace.
1327 ROW is the source line within the given file.
1328 CARET_COLUMN is the column of range 0's caret.
1329 LAST_NON_WS_COLUMN is the last column containing a non-whitespace
1330 character of source (as determined when printing the source line). */
1331
1332 int
1333 layout::get_x_bound_for_row (int row, int caret_column,
1334 int last_non_ws_column)
1335 {
1336 int result = caret_column + 1;
1337
1338 layout_range *range;
1339 int i;
1340 FOR_EACH_VEC_ELT (m_layout_ranges, i, range)
1341 {
1342 if (row >= range->m_start.m_line)
1343 {
1344 if (range->m_finish.m_line == row)
1345 {
1346 /* On the final line within a range; ensure that
1347 we render up to the end of the range. */
1348 if (result <= range->m_finish.m_column)
1349 result = range->m_finish.m_column + 1;
1350 }
1351 else if (row < range->m_finish.m_line)
1352 {
1353 /* Within a multiline range; ensure that we render up to the
1354 last non-whitespace column. */
1355 if (result <= last_non_ws_column)
1356 result = last_non_ws_column + 1;
1357 }
1358 }
1359 }
1360
1361 return result;
1362 }
1363
1364 /* Given *COLUMN as an x-coordinate, print spaces to position
1365 successive output at DEST_COLUMN, printing a newline if necessary,
1366 and updating *COLUMN. */
1367
1368 void
1369 layout::move_to_column (int *column, int dest_column)
1370 {
1371 /* Start a new line if we need to. */
1372 if (*column > dest_column)
1373 {
1374 print_newline ();
1375 *column = 0;
1376 }
1377
1378 while (*column < dest_column)
1379 {
1380 pp_space (m_pp);
1381 (*column)++;
1382 }
1383 }
1384
1385 /* For debugging layout issues, render a ruler giving column numbers
1386 (after the 1-column indent). */
1387
1388 void
1389 layout::show_ruler (int max_column) const
1390 {
1391 /* Hundreds. */
1392 if (max_column > 99)
1393 {
1394 pp_space (m_pp);
1395 for (int column = 1 + m_x_offset; column <= max_column; column++)
1396 if (0 == column % 10)
1397 pp_character (m_pp, '0' + (column / 100) % 10);
1398 else
1399 pp_space (m_pp);
1400 pp_newline (m_pp);
1401 }
1402
1403 /* Tens. */
1404 pp_space (m_pp);
1405 for (int column = 1 + m_x_offset; column <= max_column; column++)
1406 if (0 == column % 10)
1407 pp_character (m_pp, '0' + (column / 10) % 10);
1408 else
1409 pp_space (m_pp);
1410 pp_newline (m_pp);
1411
1412 /* Units. */
1413 pp_space (m_pp);
1414 for (int column = 1 + m_x_offset; column <= max_column; column++)
1415 pp_character (m_pp, '0' + (column % 10));
1416 pp_newline (m_pp);
1417 }
1418
1419 } /* End of anonymous namespace. */
1420
1421 /* Print the physical source code corresponding to the location of
1422 this diagnostic, with additional annotations. */
1423
1424 void
1425 diagnostic_show_locus (diagnostic_context * context,
1426 rich_location *richloc,
1427 diagnostic_t diagnostic_kind)
1428 {
1429 pp_newline (context->printer);
1430
1431 location_t loc = richloc->get_loc ();
1432 /* Do nothing if source-printing has been disabled. */
1433 if (!context->show_caret)
1434 return;
1435
1436 /* Don't attempt to print source for UNKNOWN_LOCATION and for builtins. */
1437 if (loc <= BUILTINS_LOCATION)
1438 return;
1439
1440 /* Don't print the same source location twice in a row, unless we have
1441 fix-it hints. */
1442 if (loc == context->last_location
1443 && richloc->get_num_fixit_hints () == 0)
1444 return;
1445
1446 context->last_location = loc;
1447
1448 const char *saved_prefix = pp_get_prefix (context->printer);
1449 pp_set_prefix (context->printer, NULL);
1450
1451 layout layout (context, richloc, diagnostic_kind);
1452 for (int line_span_idx = 0; line_span_idx < layout.get_num_line_spans ();
1453 line_span_idx++)
1454 {
1455 const line_span *line_span = layout.get_line_span (line_span_idx);
1456 if (layout.print_heading_for_line_span_index_p (line_span_idx))
1457 {
1458 expanded_location exploc = layout.get_expanded_location (line_span);
1459 context->start_span (context, exploc);
1460 }
1461 int last_line = line_span->get_last_line ();
1462 for (int row = line_span->get_first_line (); row <= last_line; row++)
1463 {
1464 /* Print the source line, followed by an annotation line
1465 consisting of any caret/underlines, then any fixits.
1466 If the source line can't be read, print nothing. */
1467 line_bounds lbounds;
1468 if (layout.print_source_line (row, &lbounds))
1469 {
1470 if (layout.should_print_annotation_line_p (row))
1471 layout.print_annotation_line (row, lbounds);
1472 layout.print_any_fixits (row);
1473 }
1474 }
1475 }
1476
1477 pp_set_prefix (context->printer, saved_prefix);
1478 }
1479
1480 #if CHECKING_P
1481
1482 namespace selftest {
1483
1484 /* Selftests for diagnostic_show_locus. */
1485
1486 /* Convenience subclass of diagnostic_context for testing
1487 diagnostic_show_locus. */
1488
1489 class test_diagnostic_context : public diagnostic_context
1490 {
1491 public:
1492 test_diagnostic_context ()
1493 {
1494 diagnostic_initialize (this, 0);
1495 show_caret = true;
1496 show_column = true;
1497 start_span = start_span_cb;
1498 }
1499 ~test_diagnostic_context ()
1500 {
1501 diagnostic_finish (this);
1502 }
1503
1504 /* Implementation of diagnostic_start_span_fn, hiding the
1505 real filename (to avoid printing the names of tempfiles). */
1506 static void
1507 start_span_cb (diagnostic_context *context, expanded_location exploc)
1508 {
1509 exploc.file = "FILENAME";
1510 default_diagnostic_start_span_fn (context, exploc);
1511 }
1512 };
1513
1514 /* Verify that diagnostic_show_locus works sanely on UNKNOWN_LOCATION. */
1515
1516 static void
1517 test_diagnostic_show_locus_unknown_location ()
1518 {
1519 test_diagnostic_context dc;
1520 rich_location richloc (line_table, UNKNOWN_LOCATION);
1521 diagnostic_show_locus (&dc, &richloc, DK_ERROR);
1522 ASSERT_STREQ ("\n", pp_formatted_text (dc.printer));
1523 }
1524
1525 /* Verify that diagnostic_show_locus works sanely for various
1526 single-line cases.
1527
1528 All of these work on the following 1-line source file:
1529 .0000000001111111
1530 .1234567890123456
1531 "foo = bar.field;\n"
1532 which is set up by test_diagnostic_show_locus_one_liner and calls
1533 them. */
1534
1535 /* Just a caret. */
1536
1537 static void
1538 test_one_liner_simple_caret ()
1539 {
1540 test_diagnostic_context dc;
1541 location_t caret = linemap_position_for_column (line_table, 10);
1542 rich_location richloc (line_table, caret);
1543 diagnostic_show_locus (&dc, &richloc, DK_ERROR);
1544 ASSERT_STREQ ("\n"
1545 " foo = bar.field;\n"
1546 " ^\n",
1547 pp_formatted_text (dc.printer));
1548 }
1549
1550 /* Caret and range. */
1551
1552 static void
1553 test_one_liner_caret_and_range ()
1554 {
1555 test_diagnostic_context dc;
1556 location_t caret = linemap_position_for_column (line_table, 10);
1557 location_t start = linemap_position_for_column (line_table, 7);
1558 location_t finish = linemap_position_for_column (line_table, 15);
1559 location_t loc = make_location (caret, start, finish);
1560 rich_location richloc (line_table, loc);
1561 diagnostic_show_locus (&dc, &richloc, DK_ERROR);
1562 ASSERT_STREQ ("\n"
1563 " foo = bar.field;\n"
1564 " ~~~^~~~~~\n",
1565 pp_formatted_text (dc.printer));
1566 }
1567
1568 /* Multiple ranges and carets. */
1569
1570 static void
1571 test_one_liner_multiple_carets_and_ranges ()
1572 {
1573 test_diagnostic_context dc;
1574 location_t foo
1575 = make_location (linemap_position_for_column (line_table, 2),
1576 linemap_position_for_column (line_table, 1),
1577 linemap_position_for_column (line_table, 3));
1578 dc.caret_chars[0] = 'A';
1579
1580 location_t bar
1581 = make_location (linemap_position_for_column (line_table, 8),
1582 linemap_position_for_column (line_table, 7),
1583 linemap_position_for_column (line_table, 9));
1584 dc.caret_chars[1] = 'B';
1585
1586 location_t field
1587 = make_location (linemap_position_for_column (line_table, 13),
1588 linemap_position_for_column (line_table, 11),
1589 linemap_position_for_column (line_table, 15));
1590 dc.caret_chars[2] = 'C';
1591
1592 rich_location richloc (line_table, foo);
1593 richloc.add_range (bar, true);
1594 richloc.add_range (field, true);
1595 diagnostic_show_locus (&dc, &richloc, DK_ERROR);
1596 ASSERT_STREQ ("\n"
1597 " foo = bar.field;\n"
1598 " ~A~ ~B~ ~~C~~\n",
1599 pp_formatted_text (dc.printer));
1600 }
1601
1602 /* Insertion fix-it hint: adding an "&" to the front of "bar.field". */
1603
1604 static void
1605 test_one_liner_fixit_insert_before ()
1606 {
1607 test_diagnostic_context dc;
1608 location_t caret = linemap_position_for_column (line_table, 7);
1609 rich_location richloc (line_table, caret);
1610 richloc.add_fixit_insert_before ("&");
1611 diagnostic_show_locus (&dc, &richloc, DK_ERROR);
1612 ASSERT_STREQ ("\n"
1613 " foo = bar.field;\n"
1614 " ^\n"
1615 " &\n",
1616 pp_formatted_text (dc.printer));
1617 }
1618
1619 /* Insertion fix-it hint: adding a "[0]" after "foo". */
1620
1621 static void
1622 test_one_liner_fixit_insert_after ()
1623 {
1624 test_diagnostic_context dc;
1625 location_t start = linemap_position_for_column (line_table, 1);
1626 location_t finish = linemap_position_for_column (line_table, 3);
1627 location_t foo = make_location (start, start, finish);
1628 rich_location richloc (line_table, foo);
1629 richloc.add_fixit_insert_after ("[0]");
1630 diagnostic_show_locus (&dc, &richloc, DK_ERROR);
1631 ASSERT_STREQ ("\n"
1632 " foo = bar.field;\n"
1633 " ^~~\n"
1634 " [0]\n",
1635 pp_formatted_text (dc.printer));
1636 }
1637
1638 /* Removal fix-it hint: removal of the ".field". */
1639
1640 static void
1641 test_one_liner_fixit_remove ()
1642 {
1643 test_diagnostic_context dc;
1644 location_t start = linemap_position_for_column (line_table, 10);
1645 location_t finish = linemap_position_for_column (line_table, 15);
1646 location_t dot = make_location (start, start, finish);
1647 rich_location richloc (line_table, dot);
1648 richloc.add_fixit_remove ();
1649 diagnostic_show_locus (&dc, &richloc, DK_ERROR);
1650 ASSERT_STREQ ("\n"
1651 " foo = bar.field;\n"
1652 " ^~~~~~\n"
1653 " ------\n",
1654 pp_formatted_text (dc.printer));
1655 }
1656
1657 /* Replace fix-it hint: replacing "field" with "m_field". */
1658
1659 static void
1660 test_one_liner_fixit_replace ()
1661 {
1662 test_diagnostic_context dc;
1663 location_t start = linemap_position_for_column (line_table, 11);
1664 location_t finish = linemap_position_for_column (line_table, 15);
1665 location_t field = make_location (start, start, finish);
1666 rich_location richloc (line_table, field);
1667 richloc.add_fixit_replace ("m_field");
1668 diagnostic_show_locus (&dc, &richloc, DK_ERROR);
1669 ASSERT_STREQ ("\n"
1670 " foo = bar.field;\n"
1671 " ^~~~~\n"
1672 " m_field\n",
1673 pp_formatted_text (dc.printer));
1674 }
1675
1676 /* Replace fix-it hint: replacing "field" with "m_field",
1677 but where the caret was elsewhere. */
1678
1679 static void
1680 test_one_liner_fixit_replace_non_equal_range ()
1681 {
1682 test_diagnostic_context dc;
1683 location_t equals = linemap_position_for_column (line_table, 5);
1684 location_t start = linemap_position_for_column (line_table, 11);
1685 location_t finish = linemap_position_for_column (line_table, 15);
1686 rich_location richloc (line_table, equals);
1687 source_range range;
1688 range.m_start = start;
1689 range.m_finish = finish;
1690 richloc.add_fixit_replace (range, "m_field");
1691 diagnostic_show_locus (&dc, &richloc, DK_ERROR);
1692 /* The replacement range is not indicated in the annotation line, so
1693 it should be indicated via an additional underline. */
1694 ASSERT_STREQ ("\n"
1695 " foo = bar.field;\n"
1696 " ^\n"
1697 " -----\n"
1698 " m_field\n",
1699 pp_formatted_text (dc.printer));
1700 }
1701
1702 /* Replace fix-it hint: replacing "field" with "m_field",
1703 where the caret was elsewhere, but where a secondary range
1704 exactly covers "field". */
1705
1706 static void
1707 test_one_liner_fixit_replace_equal_secondary_range ()
1708 {
1709 test_diagnostic_context dc;
1710 location_t equals = linemap_position_for_column (line_table, 5);
1711 location_t start = linemap_position_for_column (line_table, 11);
1712 location_t finish = linemap_position_for_column (line_table, 15);
1713 rich_location richloc (line_table, equals);
1714 location_t field = make_location (start, start, finish);
1715 richloc.add_range (field, false);
1716 richloc.add_fixit_replace (field, "m_field");
1717 diagnostic_show_locus (&dc, &richloc, DK_ERROR);
1718 /* The replacement range is indicated in the annotation line,
1719 so it shouldn't be indicated via an additional underline. */
1720 ASSERT_STREQ ("\n"
1721 " foo = bar.field;\n"
1722 " ^ ~~~~~\n"
1723 " m_field\n",
1724 pp_formatted_text (dc.printer));
1725 }
1726
1727 /* Verify that we can use ad-hoc locations when adding fixits to a
1728 rich_location. */
1729
1730 static void
1731 test_one_liner_fixit_validation_adhoc_locations ()
1732 {
1733 /* Generate a range that's too long to be packed, so must
1734 be stored as an ad-hoc location (given the defaults
1735 of 5 bits or 0 bits of packed range); 41 columns > 2**5. */
1736 const location_t c7 = linemap_position_for_column (line_table, 7);
1737 const location_t c47 = linemap_position_for_column (line_table, 47);
1738 const location_t loc = make_location (c7, c7, c47);
1739
1740 if (c47 > LINE_MAP_MAX_LOCATION_WITH_COLS)
1741 return;
1742
1743 ASSERT_TRUE (IS_ADHOC_LOC (loc));
1744
1745 /* Insert. */
1746 {
1747 rich_location richloc (line_table, loc);
1748 richloc.add_fixit_insert_before (loc, "test");
1749 /* It should not have been discarded by the validator. */
1750 ASSERT_EQ (1, richloc.get_num_fixit_hints ());
1751
1752 test_diagnostic_context dc;
1753 diagnostic_show_locus (&dc, &richloc, DK_ERROR);
1754 ASSERT_STREQ ("\n"
1755 " foo = bar.field;\n"
1756 " ^~~~~~~~~~ \n"
1757 " test\n",
1758 pp_formatted_text (dc.printer));
1759 }
1760
1761 /* Remove. */
1762 {
1763 rich_location richloc (line_table, loc);
1764 source_range range = source_range::from_locations (loc, c47);
1765 richloc.add_fixit_remove (range);
1766 /* It should not have been discarded by the validator. */
1767 ASSERT_EQ (1, richloc.get_num_fixit_hints ());
1768
1769 test_diagnostic_context dc;
1770 diagnostic_show_locus (&dc, &richloc, DK_ERROR);
1771 ASSERT_STREQ ("\n"
1772 " foo = bar.field;\n"
1773 " ^~~~~~~~~~ \n"
1774 " -----------------------------------------\n",
1775 pp_formatted_text (dc.printer));
1776 }
1777
1778 /* Replace. */
1779 {
1780 rich_location richloc (line_table, loc);
1781 source_range range = source_range::from_locations (loc, c47);
1782 richloc.add_fixit_replace (range, "test");
1783 /* It should not have been discarded by the validator. */
1784 ASSERT_EQ (1, richloc.get_num_fixit_hints ());
1785
1786 test_diagnostic_context dc;
1787 diagnostic_show_locus (&dc, &richloc, DK_ERROR);
1788 ASSERT_STREQ ("\n"
1789 " foo = bar.field;\n"
1790 " ^~~~~~~~~~ \n"
1791 " test\n",
1792 pp_formatted_text (dc.printer));
1793 }
1794 }
1795
1796 /* Test of consolidating insertions at the same location. */
1797
1798 static void
1799 test_one_liner_many_fixits_1 ()
1800 {
1801 test_diagnostic_context dc;
1802 location_t equals = linemap_position_for_column (line_table, 5);
1803 rich_location richloc (line_table, equals);
1804 for (int i = 0; i < 19; i++)
1805 richloc.add_fixit_insert_before ("a");
1806 ASSERT_EQ (1, richloc.get_num_fixit_hints ());
1807 diagnostic_show_locus (&dc, &richloc, DK_ERROR);
1808 ASSERT_STREQ ("\n"
1809 " foo = bar.field;\n"
1810 " ^\n"
1811 " aaaaaaaaaaaaaaaaaaa\n",
1812 pp_formatted_text (dc.printer));
1813 }
1814
1815 /* Ensure that we can add an arbitrary number of fix-it hints to a
1816 rich_location, even if they are not consolidated. */
1817
1818 static void
1819 test_one_liner_many_fixits_2 ()
1820 {
1821 test_diagnostic_context dc;
1822 location_t equals = linemap_position_for_column (line_table, 5);
1823 rich_location richloc (line_table, equals);
1824 for (int i = 0; i < 19; i++)
1825 {
1826 location_t loc = linemap_position_for_column (line_table, i * 2);
1827 richloc.add_fixit_insert_before (loc, "a");
1828 }
1829 ASSERT_EQ (19, richloc.get_num_fixit_hints ());
1830 diagnostic_show_locus (&dc, &richloc, DK_ERROR);
1831 ASSERT_STREQ ("\n"
1832 " foo = bar.field;\n"
1833 " ^\n"
1834 "a a a a a a a a a a a a a a a a a a a\n",
1835 pp_formatted_text (dc.printer));
1836 }
1837
1838 /* Run the various one-liner tests. */
1839
1840 static void
1841 test_diagnostic_show_locus_one_liner (const line_table_case &case_)
1842 {
1843 /* Create a tempfile and write some text to it.
1844 ....................0000000001111111.
1845 ....................1234567890123456. */
1846 const char *content = "foo = bar.field;\n";
1847 temp_source_file tmp (SELFTEST_LOCATION, ".c", content);
1848 line_table_test ltt (case_);
1849
1850 linemap_add (line_table, LC_ENTER, false, tmp.get_filename (), 1);
1851
1852 location_t line_end = linemap_position_for_column (line_table, 16);
1853
1854 /* Don't attempt to run the tests if column data might be unavailable. */
1855 if (line_end > LINE_MAP_MAX_LOCATION_WITH_COLS)
1856 return;
1857
1858 ASSERT_STREQ (tmp.get_filename (), LOCATION_FILE (line_end));
1859 ASSERT_EQ (1, LOCATION_LINE (line_end));
1860 ASSERT_EQ (16, LOCATION_COLUMN (line_end));
1861
1862 test_one_liner_simple_caret ();
1863 test_one_liner_caret_and_range ();
1864 test_one_liner_multiple_carets_and_ranges ();
1865 test_one_liner_fixit_insert_before ();
1866 test_one_liner_fixit_insert_after ();
1867 test_one_liner_fixit_remove ();
1868 test_one_liner_fixit_replace ();
1869 test_one_liner_fixit_replace_non_equal_range ();
1870 test_one_liner_fixit_replace_equal_secondary_range ();
1871 test_one_liner_fixit_validation_adhoc_locations ();
1872 test_one_liner_many_fixits_1 ();
1873 test_one_liner_many_fixits_2 ();
1874 }
1875
1876 /* Verify that we print fixits even if they only affect lines
1877 outside those covered by the ranges in the rich_location. */
1878
1879 static void
1880 test_diagnostic_show_locus_fixit_lines (const line_table_case &case_)
1881 {
1882 /* Create a tempfile and write some text to it.
1883 ...000000000111111111122222222223333333333.
1884 ...123456789012345678901234567890123456789. */
1885 const char *content
1886 = ("struct point { double x; double y; };\n" /* line 1. */
1887 "struct point origin = {x: 0.0,\n" /* line 2. */
1888 " y\n" /* line 3. */
1889 "\n" /* line 4. */
1890 "\n" /* line 5. */
1891 " : 0.0};\n"); /* line 6. */
1892 temp_source_file tmp (SELFTEST_LOCATION, ".c", content);
1893 line_table_test ltt (case_);
1894
1895 const line_map_ordinary *ord_map
1896 = linemap_check_ordinary (linemap_add (line_table, LC_ENTER, false,
1897 tmp.get_filename (), 0));
1898
1899 linemap_line_start (line_table, 1, 100);
1900
1901 const location_t final_line_end
1902 = linemap_position_for_line_and_column (line_table, ord_map, 6, 36);
1903
1904 /* Don't attempt to run the tests if column data might be unavailable. */
1905 if (final_line_end > LINE_MAP_MAX_LOCATION_WITH_COLS)
1906 return;
1907
1908 /* A pair of tests for modernizing the initializers to C99-style. */
1909
1910 /* The one-liner case (line 2). */
1911 {
1912 test_diagnostic_context dc;
1913 const location_t x
1914 = linemap_position_for_line_and_column (line_table, ord_map, 2, 24);
1915 const location_t colon
1916 = linemap_position_for_line_and_column (line_table, ord_map, 2, 25);
1917 rich_location richloc (line_table, colon);
1918 richloc.add_fixit_insert_before (x, ".");
1919 richloc.add_fixit_replace (colon, "=");
1920 diagnostic_show_locus (&dc, &richloc, DK_ERROR);
1921 ASSERT_STREQ ("\n"
1922 " struct point origin = {x: 0.0,\n"
1923 " ^\n"
1924 " .=\n",
1925 pp_formatted_text (dc.printer));
1926 }
1927
1928 /* The multiline case. The caret for the rich_location is on line 6;
1929 verify that insertion fixit on line 3 is still printed (and that
1930 span starts are printed due to the gap between the span at line 3
1931 and that at line 6). */
1932 {
1933 test_diagnostic_context dc;
1934 const location_t y
1935 = linemap_position_for_line_and_column (line_table, ord_map, 3, 24);
1936 const location_t colon
1937 = linemap_position_for_line_and_column (line_table, ord_map, 6, 25);
1938 rich_location richloc (line_table, colon);
1939 richloc.add_fixit_insert_before (y, ".");
1940 richloc.add_fixit_replace (colon, "=");
1941 diagnostic_show_locus (&dc, &richloc, DK_ERROR);
1942 ASSERT_STREQ ("\n"
1943 "FILENAME:3:24:\n"
1944 " y\n"
1945 " .\n"
1946 "FILENAME:6:25:\n"
1947 " : 0.0};\n"
1948 " ^\n"
1949 " =\n",
1950 pp_formatted_text (dc.printer));
1951 }
1952 }
1953
1954
1955 /* Verify that fix-it hints are appropriately consolidated.
1956
1957 If any fix-it hints in a rich_location involve locations beyond
1958 LINE_MAP_MAX_LOCATION_WITH_COLS, then we can't reliably apply
1959 the fix-it as a whole, so there should be none.
1960
1961 Otherwise, verify that consecutive "replace" and "remove" fix-its
1962 are merged, and that other fix-its remain separate. */
1963
1964 static void
1965 test_fixit_consolidation (const line_table_case &case_)
1966 {
1967 line_table_test ltt (case_);
1968
1969 linemap_add (line_table, LC_ENTER, false, "test.c", 1);
1970
1971 const location_t c10 = linemap_position_for_column (line_table, 10);
1972 const location_t c15 = linemap_position_for_column (line_table, 15);
1973 const location_t c16 = linemap_position_for_column (line_table, 16);
1974 const location_t c17 = linemap_position_for_column (line_table, 17);
1975 const location_t c20 = linemap_position_for_column (line_table, 20);
1976 const location_t c21 = linemap_position_for_column (line_table, 21);
1977 const location_t caret = c10;
1978
1979 /* Insert + insert. */
1980 {
1981 rich_location richloc (line_table, caret);
1982 richloc.add_fixit_insert_before (c10, "foo");
1983 richloc.add_fixit_insert_before (c15, "bar");
1984
1985 if (c15 > LINE_MAP_MAX_LOCATION_WITH_COLS)
1986 /* Bogus column info for 2nd fixit, so no fixits. */
1987 ASSERT_EQ (0, richloc.get_num_fixit_hints ());
1988 else
1989 /* They should not have been merged. */
1990 ASSERT_EQ (2, richloc.get_num_fixit_hints ());
1991 }
1992
1993 /* Insert + replace. */
1994 {
1995 rich_location richloc (line_table, caret);
1996 richloc.add_fixit_insert_before (c10, "foo");
1997 richloc.add_fixit_replace (source_range::from_locations (c15, c17),
1998 "bar");
1999
2000 if (c17 > LINE_MAP_MAX_LOCATION_WITH_COLS)
2001 /* Bogus column info for 2nd fixit, so no fixits. */
2002 ASSERT_EQ (0, richloc.get_num_fixit_hints ());
2003 else
2004 /* They should not have been merged. */
2005 ASSERT_EQ (2, richloc.get_num_fixit_hints ());
2006 }
2007
2008 /* Replace + non-consecutive insert. */
2009 {
2010 rich_location richloc (line_table, caret);
2011 richloc.add_fixit_replace (source_range::from_locations (c10, c15),
2012 "bar");
2013 richloc.add_fixit_insert_before (c17, "foo");
2014
2015 if (c17 > LINE_MAP_MAX_LOCATION_WITH_COLS)
2016 /* Bogus column info for 2nd fixit, so no fixits. */
2017 ASSERT_EQ (0, richloc.get_num_fixit_hints ());
2018 else
2019 /* They should not have been merged. */
2020 ASSERT_EQ (2, richloc.get_num_fixit_hints ());
2021 }
2022
2023 /* Replace + non-consecutive replace. */
2024 {
2025 rich_location richloc (line_table, caret);
2026 richloc.add_fixit_replace (source_range::from_locations (c10, c15),
2027 "foo");
2028 richloc.add_fixit_replace (source_range::from_locations (c17, c20),
2029 "bar");
2030
2031 if (c20 > LINE_MAP_MAX_LOCATION_WITH_COLS)
2032 /* Bogus column info for 2nd fixit, so no fixits. */
2033 ASSERT_EQ (0, richloc.get_num_fixit_hints ());
2034 else
2035 /* They should not have been merged. */
2036 ASSERT_EQ (2, richloc.get_num_fixit_hints ());
2037 }
2038
2039 /* Replace + consecutive replace. */
2040 {
2041 rich_location richloc (line_table, caret);
2042 richloc.add_fixit_replace (source_range::from_locations (c10, c15),
2043 "foo");
2044 richloc.add_fixit_replace (source_range::from_locations (c16, c20),
2045 "bar");
2046
2047 if (c20 > LINE_MAP_MAX_LOCATION_WITH_COLS)
2048 /* Bogus column info for 2nd fixit, so no fixits. */
2049 ASSERT_EQ (0, richloc.get_num_fixit_hints ());
2050 else
2051 {
2052 /* They should have been merged into a single "replace". */
2053 ASSERT_EQ (1, richloc.get_num_fixit_hints ());
2054 const fixit_hint *hint = richloc.get_fixit_hint (0);
2055 ASSERT_STREQ ("foobar", hint->get_string ());
2056 ASSERT_EQ (c10, hint->get_start_loc ());
2057 ASSERT_EQ (c21, hint->get_next_loc ());
2058 }
2059 }
2060
2061 /* Replace + consecutive removal. */
2062 {
2063 rich_location richloc (line_table, caret);
2064 richloc.add_fixit_replace (source_range::from_locations (c10, c15),
2065 "foo");
2066 richloc.add_fixit_remove (source_range::from_locations (c16, c20));
2067
2068 if (c20 > LINE_MAP_MAX_LOCATION_WITH_COLS)
2069 /* Bogus column info for 2nd fixit, so no fixits. */
2070 ASSERT_EQ (0, richloc.get_num_fixit_hints ());
2071 else
2072 {
2073 /* They should have been merged into a single replace, with the
2074 range extended to cover that of the removal. */
2075 ASSERT_EQ (1, richloc.get_num_fixit_hints ());
2076 const fixit_hint *hint = richloc.get_fixit_hint (0);
2077 ASSERT_STREQ ("foo", hint->get_string ());
2078 ASSERT_EQ (c10, hint->get_start_loc ());
2079 ASSERT_EQ (c21, hint->get_next_loc ());
2080 }
2081 }
2082
2083 /* Consecutive removals. */
2084 {
2085 rich_location richloc (line_table, caret);
2086 richloc.add_fixit_remove (source_range::from_locations (c10, c15));
2087 richloc.add_fixit_remove (source_range::from_locations (c16, c20));
2088
2089 if (c20 > LINE_MAP_MAX_LOCATION_WITH_COLS)
2090 /* Bogus column info for 2nd fixit, so no fixits. */
2091 ASSERT_EQ (0, richloc.get_num_fixit_hints ());
2092 else
2093 {
2094 /* They should have been merged into a single "replace-with-empty". */
2095 ASSERT_EQ (1, richloc.get_num_fixit_hints ());
2096 const fixit_hint *hint = richloc.get_fixit_hint (0);
2097 ASSERT_STREQ ("", hint->get_string ());
2098 ASSERT_EQ (c10, hint->get_start_loc ());
2099 ASSERT_EQ (c21, hint->get_next_loc ());
2100 }
2101 }
2102 }
2103
2104 /* Insertion fix-it hint: adding a "break;" on a line by itself.
2105 This will fail, as newlines aren't yet supported. */
2106
2107 static void
2108 test_fixit_insert_containing_newline (const line_table_case &case_)
2109 {
2110 /* Create a tempfile and write some text to it.
2111 .........................0000000001111111.
2112 .........................1234567890123456. */
2113 const char *old_content = (" case 'a':\n" /* line 1. */
2114 " x = a;\n" /* line 2. */
2115 " case 'b':\n" /* line 3. */
2116 " x = b;\n");/* line 4. */
2117
2118 temp_source_file tmp (SELFTEST_LOCATION, ".c", old_content);
2119 line_table_test ltt (case_);
2120 linemap_add (line_table, LC_ENTER, false, tmp.get_filename (), 3);
2121
2122 /* Add a "break;" on a line by itself before line 3 i.e. before
2123 column 1 of line 3. */
2124 location_t case_start = linemap_position_for_column (line_table, 5);
2125 location_t case_finish = linemap_position_for_column (line_table, 13);
2126 location_t case_loc = make_location (case_start, case_start, case_finish);
2127 rich_location richloc (line_table, case_loc);
2128 location_t line_start = linemap_position_for_column (line_table, 1);
2129 richloc.add_fixit_insert_before (line_start, " break;\n");
2130
2131 /* Newlines are not yet supported within fix-it hints, so
2132 the fix-it should not be displayed. */
2133 ASSERT_TRUE (richloc.seen_impossible_fixit_p ());
2134
2135 if (case_finish > LINE_MAP_MAX_LOCATION_WITH_COLS)
2136 return;
2137
2138 test_diagnostic_context dc;
2139 diagnostic_show_locus (&dc, &richloc, DK_ERROR);
2140 ASSERT_STREQ ("\n"
2141 " case 'b':\n"
2142 " ^~~~~~~~~\n",
2143 pp_formatted_text (dc.printer));
2144 }
2145
2146 /* Replacement fix-it hint containing a newline.
2147 This will fail, as newlines aren't yet supported. */
2148
2149 static void
2150 test_fixit_replace_containing_newline (const line_table_case &case_)
2151 {
2152 /* Create a tempfile and write some text to it.
2153 .........................0000000001111.
2154 .........................1234567890123. */
2155 const char *old_content = "foo = bar ();\n";
2156
2157 temp_source_file tmp (SELFTEST_LOCATION, ".c", old_content);
2158 line_table_test ltt (case_);
2159 linemap_add (line_table, LC_ENTER, false, tmp.get_filename (), 1);
2160
2161 /* Replace the " = " with "\n = ", as if we were reformatting an
2162 overly long line. */
2163 location_t start = linemap_position_for_column (line_table, 4);
2164 location_t finish = linemap_position_for_column (line_table, 6);
2165 location_t loc = linemap_position_for_column (line_table, 13);
2166 rich_location richloc (line_table, loc);
2167 source_range range = source_range::from_locations (start, finish);
2168 richloc.add_fixit_replace (range, "\n =");
2169
2170 /* Newlines are not yet supported within fix-it hints, so
2171 the fix-it should not be displayed. */
2172 ASSERT_TRUE (richloc.seen_impossible_fixit_p ());
2173
2174 if (finish > LINE_MAP_MAX_LOCATION_WITH_COLS)
2175 return;
2176
2177 test_diagnostic_context dc;
2178 diagnostic_show_locus (&dc, &richloc, DK_ERROR);
2179 ASSERT_STREQ ("\n"
2180 " foo = bar ();\n"
2181 " ^\n",
2182 pp_formatted_text (dc.printer));
2183 }
2184
2185 /* Run all of the selftests within this file. */
2186
2187 void
2188 diagnostic_show_locus_c_tests ()
2189 {
2190 test_layout_range_for_single_point ();
2191 test_layout_range_for_single_line ();
2192 test_layout_range_for_multiple_lines ();
2193
2194 test_get_line_width_without_trailing_whitespace ();
2195
2196 test_diagnostic_show_locus_unknown_location ();
2197
2198 for_each_line_table_case (test_diagnostic_show_locus_one_liner);
2199 for_each_line_table_case (test_diagnostic_show_locus_fixit_lines);
2200 for_each_line_table_case (test_fixit_consolidation);
2201 for_each_line_table_case (test_fixit_insert_containing_newline);
2202 for_each_line_table_case (test_fixit_replace_containing_newline);
2203 }
2204
2205 } // namespace selftest
2206
2207 #endif /* #if CHECKING_P */