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>
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 3, or (at your option) any later
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
23 #include "coretypes.h"
27 #include "backtrace.h"
28 #include "diagnostic.h"
29 #include "diagnostic-color.h"
36 #ifdef GWINSZ_IN_SYS_IOCTL
37 # include <sys/ioctl.h>
40 /* Classes for rendering source code and diagnostics, within an
42 The work is done by "class layout", which embeds and uses
43 "class colorizer" and "class layout_range" to get things done. */
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
57 /* A class to inject colorization codes when printing the diagnostic locus.
59 It has one kind of colorization for each of:
61 - range 0 (the "primary location")
65 The class caches the lookup of the color codes for the above.
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. */
76 colorizer (diagnostic_context
*context
,
77 diagnostic_t diagnostic_kind
);
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
); }
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 *);
92 static const int STATE_NORMAL_TEXT
= -1;
93 static const int STATE_FIXIT_INSERT
= -2;
94 static const int STATE_FIXIT_DELETE
= -3;
96 diagnostic_context
*m_context
;
97 diagnostic_t m_diagnostic_kind
;
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
;
107 /* A point within a layout_range; similar to an expanded_location,
108 but after filtering on file. */
113 layout_point (const expanded_location
&exploc
)
114 : m_line (exploc
.line
),
115 m_column (exploc
.column
) {}
121 /* A class for use by "class layout" below: a filtered location_range. */
126 layout_range (const expanded_location
*start_exploc
,
127 const expanded_location
*finish_exploc
,
129 const expanded_location
*caret_exploc
);
131 bool contains_point (int row
, int column
) const;
132 bool intersects_line_p (int row
) const;
134 layout_point m_start
;
135 layout_point m_finish
;
137 layout_point m_caret
;
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. */
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). */
157 line_span (linenum_type first_line
, linenum_type last_line
)
158 : m_first_line (first_line
), m_last_line (last_line
)
160 gcc_assert (first_line
<= last_line
);
162 linenum_type
get_first_line () const { return m_first_line
; }
163 linenum_type
get_last_line () const { return m_last_line
; }
165 bool contains_line_p (linenum_type line
) const
167 return line
>= m_first_line
&& line
<= m_last_line
;
170 static int comparator (const void *p1
, const void *p2
)
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
;
176 return first_line_diff
;
177 return (int)ls1
->m_last_line
- (int)ls2
->m_last_line
;
180 linenum_type m_first_line
;
181 linenum_type m_last_line
;
184 /* A class to control the overall layout when printing a diagnostic.
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.
190 We assume we have disjoint ranges. */
195 layout (diagnostic_context
*context
,
196 rich_location
*richloc
,
197 diagnostic_t diagnostic_kind
);
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
]; }
202 bool print_heading_for_line_span_index_p (int line_span_idx
) const;
204 expanded_location
get_expanded_location (const line_span
*) const;
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
);
213 void show_ruler (int max_column
) const;
216 bool validate_fixit_hint_p (const fixit_hint
*hint
);
218 void calculate_line_spans ();
220 void print_newline ();
223 get_state_at_point (/* Inputs. */
225 int first_non_ws
, int last_non_ws
,
227 point_state
*out_state
);
230 get_x_bound_for_row (int row
, int caret_column
,
234 move_to_column (int *column
, int dest_column
);
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
;
249 /* Implementation of "class colorizer". */
251 /* The constructor for "colorizer". Lookup and store color codes for the
252 different kinds of things we might need to print. */
254 colorizer::colorizer (diagnostic_context
*context
,
255 diagnostic_t diagnostic_kind
) :
257 m_diagnostic_kind (diagnostic_kind
),
258 m_current_state (STATE_NORMAL_TEXT
)
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
));
267 /* The destructor for "colorize". If colorization is on, print a code to
270 colorizer::~colorizer ()
272 finish_state (m_current_state
);
275 /* Update state, printing color codes if necessary if there's a state
279 colorizer::set_state (int new_state
)
281 if (m_current_state
!= new_state
)
283 finish_state (m_current_state
);
284 m_current_state
= new_state
;
285 begin_state (new_state
);
289 /* Turn on any colorization for STATE. */
292 colorizer::begin_state (int state
)
296 case STATE_NORMAL_TEXT
:
299 case STATE_FIXIT_INSERT
:
300 pp_string (m_context
->printer
, m_fixit_insert
);
303 case STATE_FIXIT_DELETE
:
304 pp_string (m_context
->printer
, m_fixit_delete
);
308 /* Make range 0 be the same color as the "kind" text
309 (error vs warning vs note). */
312 colorize_start (pp_show_color (m_context
->printer
),
313 diagnostic_get_color_for_kind (m_diagnostic_kind
)));
317 pp_string (m_context
->printer
, m_range1
);
321 pp_string (m_context
->printer
, m_range2
);
325 /* For ranges beyond 2, alternate between color 1 and color 2. */
327 gcc_assert (state
> 2);
328 pp_string (m_context
->printer
,
329 state
% 2 ? m_range1
: m_range2
);
335 /* Turn off any colorization for STATE. */
338 colorizer::finish_state (int state
)
340 if (state
!= STATE_NORMAL_TEXT
)
341 pp_string (m_context
->printer
, m_stop_color
);
344 /* Get the color code for NAME (or the empty string if
345 colorization is disabled). */
348 colorizer::get_color_by_name (const char *name
)
350 return colorize_start (pp_show_color (m_context
->printer
), name
);
353 /* Implementation of class layout_range. */
355 /* The constructor for class layout_range.
356 Initialize various layout_point fields from expanded_location
357 equivalents; we've already filtered on file. */
359 layout_range::layout_range (const expanded_location
*start_exploc
,
360 const expanded_location
*finish_exploc
,
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
)
370 /* Is (column, row) within the given range?
371 We've already filtered on the file.
373 Ranges are closed (both limits are within the range).
375 Example A: a single-line range:
376 start: (col=22, line=2)
377 finish: (col=38, line=2)
379 |00000011111111112222222222333333333344444444444
380 |34567890123456789012345678901234567890123456789
381 --+-----------------------------------------------
382 01|bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb
383 02|bbbbbbbbbbbbbbbbbbbSwwwwwwwwwwwwwwwFaaaaaaaaaaa
384 03|aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
386 Example B: a multiline range with
387 start: (col=14, line=3)
388 finish: (col=08, line=5)
390 |00000011111111112222222222333333333344444444444
391 |34567890123456789012345678901234567890123456789
392 --+-----------------------------------------------
393 01|bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb
394 02|bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb
395 03|bbbbbbbbbbbSwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwww
396 04|wwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwww
397 05|wwwwwFaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
398 06|aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
399 --+-----------------------------------------------
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
407 - 'a' indicates a subsequent point *after* the range. */
410 layout_range::contains_point (int row
, int column
) const
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. */
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). */
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). */
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. */
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) */
438 /* This is a single-line range. */
439 gcc_assert (row
== m_finish
.m_line
);
440 return column
<= m_finish
.m_column
;
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
);
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
455 if (row
< m_finish
.m_line
)
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
);
463 gcc_assert (row
== m_finish
.m_line
);
465 return column
<= m_finish
.m_column
;
468 /* Does this layout_range contain any part of line ROW? */
471 layout_range::intersects_line_p (int row
) const
473 gcc_assert (m_start
.m_line
<= m_finish
.m_line
);
474 if (row
< m_start
.m_line
)
476 if (row
> m_finish
.m_line
)
483 /* A helper function for testing layout_range. */
486 make_range (int start_line
, int start_col
, int end_line
, int end_col
)
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,
496 /* Selftests for layout_range::contains_point and
497 layout_range::intersects_line_p. */
499 /* Selftest for layout_range, where the layout_range
500 is a range with start==end i.e. a single point. */
503 test_layout_range_for_single_point ()
505 layout_range point
= make_range (7, 10, 7, 10);
507 /* Tests for layout_range::contains_point. */
509 /* Before the line. */
510 ASSERT_FALSE (point
.contains_point (6, 1));
512 /* On the line, but before start. */
513 ASSERT_FALSE (point
.contains_point (7, 9));
516 ASSERT_TRUE (point
.contains_point (7, 10));
518 /* On the line, after the point. */
519 ASSERT_FALSE (point
.contains_point (7, 11));
521 /* After the line. */
522 ASSERT_FALSE (point
.contains_point (8, 1));
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));
530 /* Selftest for layout_range, where the layout_range
531 is the single-line range shown as "Example A" above. */
534 test_layout_range_for_single_line ()
536 layout_range example_a
= make_range (2, 22, 2, 38);
538 /* Tests for layout_range::contains_point. */
540 /* Before the line. */
541 ASSERT_FALSE (example_a
.contains_point (1, 1));
543 /* On the line, but before start. */
544 ASSERT_FALSE (example_a
.contains_point (2, 21));
546 /* On the line, at the start. */
547 ASSERT_TRUE (example_a
.contains_point (2, 22));
549 /* On the line, within the range. */
550 ASSERT_TRUE (example_a
.contains_point (2, 23));
552 /* On the line, at the end. */
553 ASSERT_TRUE (example_a
.contains_point (2, 38));
555 /* On the line, after the end. */
556 ASSERT_FALSE (example_a
.contains_point (2, 39));
558 /* After the line. */
559 ASSERT_FALSE (example_a
.contains_point (2, 39));
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));
567 /* Selftest for layout_range, where the layout_range
568 is the multi-line range shown as "Example B" above. */
571 test_layout_range_for_multiple_lines ()
573 layout_range example_b
= make_range (3, 14, 5, 8);
575 /* Tests for layout_range::contains_point. */
577 /* Before first line. */
578 ASSERT_FALSE (example_b
.contains_point (1, 1));
580 /* On the first line, but before start. */
581 ASSERT_FALSE (example_b
.contains_point (3, 13));
584 ASSERT_TRUE (example_b
.contains_point (3, 14));
586 /* On the first line, within the range. */
587 ASSERT_TRUE (example_b
.contains_point (3, 15));
589 /* On an interior line.
590 The column number should not matter; try various boundary
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));
600 /* On the final line, before the end. */
601 ASSERT_TRUE (example_b
.contains_point (5, 7));
603 /* On the final line, at the end. */
604 ASSERT_TRUE (example_b
.contains_point (5, 8));
606 /* On the final line, after the end. */
607 ASSERT_FALSE (example_b
.contains_point (5, 9));
609 /* After the line. */
610 ASSERT_FALSE (example_b
.contains_point (6, 1));
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));
620 #endif /* #if CHECKING_P */
622 /* Given a source line LINE of length LINE_WIDTH, determine the width
623 without any trailing whitespace. */
626 get_line_width_without_trailing_whitespace (const char *line
, int line_width
)
628 int result
= line_width
;
631 char ch
= line
[result
- 1];
632 if (ch
== ' ' || ch
== '\t')
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'));
647 /* A helper function for testing get_line_width_without_trailing_whitespace. */
650 assert_eq (const char *line
, int expected_width
)
653 = get_line_width_without_trailing_whitespace (line
, strlen (line
));
654 ASSERT_EQ (actual_value
, expected_width
);
657 /* Verify that get_line_width_without_trailing_whitespace is sane for
658 various inputs. It is not required to handle newlines. */
661 test_get_line_width_without_trailing_whitespace ()
666 assert_eq ("hello world", 11);
667 assert_eq ("hello world ", 11);
668 assert_eq ("hello world \t\t ", 11);
671 #endif /* #if CHECKING_P */
673 /* Helper function for layout's ctor, for sanitizing locations relative
674 to the primary location within a diagnostic.
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.
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
689 compatible_locations_p (location_t loc_a
, location_t loc_b
)
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
);
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
;
702 const line_map
*map_a
= linemap_lookup (line_table
, loc_a
);
703 linemap_assert (map_a
);
705 const line_map
*map_b
= linemap_lookup (line_table
, loc_b
);
706 linemap_assert (map_b
);
708 /* Are they within the same map? */
711 /* Are both within the same macro expansion? */
712 if (linemap_macro_expansion_map_p (map_a
))
714 /* Expand each location towards the spelling location, and
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
,
721 source_location loc_b_toward_spelling
722 = linemap_macro_map_loc_unwind_toward_spelling (line_table
,
725 return compatible_locations_p (loc_a_toward_spelling
,
726 loc_b_toward_spelling
);
729 /* Otherwise they are within the same ordinary map. */
734 /* Within different maps. */
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
))
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
;
749 /* Implementation of class layout. */
751 /* Constructor for class layout.
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. */
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 ()),
774 source_location primary_loc
= richloc
->get_range (0)->m_loc
;
776 for (unsigned int idx
= 0; idx
< richloc
->get_num_locations (); idx
++)
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
);
782 /* Split the "range" into caret and range information. */
783 source_range src_range
= get_range_from_loc (line_table
, loc_range
->m_loc
);
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
);
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
)
797 if (finish
.file
!= m_exploc
.file
)
799 if (loc_range
->m_show_caret_p
)
800 if (caret
.file
!= m_exploc
.file
)
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. */
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
);
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
))
826 /* Is this the primary location? */
827 if (m_layout_ranges
.length () == 0)
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
;
835 /* This is a non-primary range; ignore it. */
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
);
844 /* Populate m_fixit_hints, filtering to only those that are in the
846 for (unsigned int i
= 0; i
< richloc
->get_num_fixit_hints (); i
++)
848 const fixit_hint
*hint
= richloc
->get_fixit_hint (i
);
849 if (validate_fixit_hint_p (hint
))
850 m_fixit_hints
.safe_push (hint
);
853 /* Populate m_line_spans. */
854 calculate_line_spans ();
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
;
861 const char *line
= location_get_source_line (m_exploc
.file
, m_exploc
.line
,
863 if (line
&& m_exploc
.column
<= line_width
)
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);
874 if (context
->show_ruler_p
)
875 show_ruler (m_x_offset
+ max_width
);
878 /* Return true iff we should print a heading when starting the
879 line span with the given index. */
882 layout::print_heading_for_line_span_index_p (int line_span_idx
) const
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)
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
)
897 /* Get an expanded_location for the first location of interest within
899 Used when printing a heading to indicate a new line span. */
902 layout::get_expanded_location (const line_span
*line_span
) const
904 /* Whenever possible, use the caret location. */
905 if (line_span
->contains_line_p (m_exploc
.line
))
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
++)
912 const layout_range
*lr
= &m_layout_ranges
[i
];
913 if (line_span
->contains_line_p (lr
->m_start
.m_line
))
915 expanded_location exploc
= m_exploc
;
916 exploc
.line
= lr
->m_start
.m_line
;
917 exploc
.column
= lr
->m_start
.m_column
;
922 /* Otherwise, use the location of the first fixit-hint present within
924 for (unsigned int i
= 0; i
< m_fixit_hints
.length (); i
++)
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
))
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. */
939 /* Determine if HINT is meaningful to print within this layout. */
942 layout::validate_fixit_hint_p (const fixit_hint
*hint
)
944 if (LOCATION_FILE (hint
->get_start_loc ()) != m_exploc
.file
)
946 if (LOCATION_FILE (hint
->get_next_loc ()) != m_exploc
.file
)
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. */
957 get_line_span_for_fixit_hint (const fixit_hint
*hint
)
960 return line_span (LOCATION_LINE (hint
->get_start_loc ()),
961 LOCATION_LINE (hint
->get_next_loc ()));
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.
969 We will print a subset of the lines within the source file in question,
970 as a collection of "spans" of lines.
972 This function populates m_line_spans with an ordered, disjoint list of
973 the line spans of interest.
975 For example, if the primary caret location is on line 7, with ranges
976 covering lines 5-6 and lines 9-12:
989 then we want two spans: lines 5-7 and lines 9-12. */
992 layout::calculate_line_spans ()
994 /* This should only be called once, by the ctor. */
995 gcc_assert (m_line_spans
.length () == 0);
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
++)
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
));
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
++)
1012 const fixit_hint
*hint
= m_fixit_hints
[i
];
1014 tmp_spans
.safe_push (get_line_span_for_fixit_hint (hint
));
1018 tmp_spans
.qsort(line_span::comparator
);
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
++)
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)
1031 /* We can merge them. */
1032 if (next
->m_last_line
> current
->m_last_line
)
1033 current
->m_last_line
= next
->m_last_line
;
1037 /* No merger possible. */
1038 m_line_spans
.safe_push (*next
);
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
++)
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
);
1058 /* Attempt to print line ROW of source code, potentially colorized at any
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
1065 layout::print_source_line (int row
, line_bounds
*lbounds_out
)
1068 const char *line
= location_get_source_line (m_exploc
.file
, row
,
1073 m_colorizer
.set_normal_text ();
1075 /* We will stop printing the source line at any trailing
1077 line_width
= get_line_width_without_trailing_whitespace (line
,
1082 int first_non_ws
= INT_MAX
;
1083 int last_non_ws
= 0;
1085 for (column
= 1 + m_x_offset
; column
<= line_width
; column
++)
1087 /* Assuming colorization is enabled for the caret and underline
1088 characters, we may also colorize the associated characters
1089 within the source line.
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.
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
)
1103 in_range_p
= get_state_at_point (row
, column
,
1107 m_colorizer
.set_range (state
.range_idx
);
1109 m_colorizer
.set_normal_text ();
1111 char c
= *line
== '\t' ? ' ' : *line
;
1116 last_non_ws
= column
;
1117 if (first_non_ws
== INT_MAX
)
1118 first_non_ws
= column
;
1120 pp_character (m_pp
, c
);
1125 lbounds_out
->m_first_non_ws
= first_non_ws
;
1126 lbounds_out
->m_last_non_ws
= last_non_ws
;
1130 /* Determine if we should print an annotation line for ROW.
1131 i.e. if any of m_layout_ranges contains ROW. */
1134 layout::should_print_annotation_line_p (int row
) const
1136 layout_range
*range
;
1138 FOR_EACH_VEC_ELT (m_layout_ranges
, i
, range
)
1139 if (range
->intersects_line_p (row
))
1144 /* Print a line consisting of the caret/underlines for the given
1148 layout::print_annotation_line (int row
, const line_bounds lbounds
)
1150 int x_bound
= get_x_bound_for_row (row
, m_exploc
.column
,
1151 lbounds
.m_last_non_ws
);
1154 for (int column
= 1 + m_x_offset
; column
< x_bound
; column
++)
1158 in_range_p
= get_state_at_point (row
, column
,
1159 lbounds
.m_first_non_ws
,
1160 lbounds
.m_last_non_ws
,
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
)
1168 /* Draw the caret. */
1170 if (state
.range_idx
< rich_location::STATICALLY_ALLOCATED_RANGES
)
1171 caret_char
= m_context
->caret_chars
[state
.range_idx
];
1174 pp_character (m_pp
, caret_char
);
1177 pp_character (m_pp
, '~');
1181 /* Not in a range. */
1182 m_colorizer
.set_normal_text ();
1183 pp_character (m_pp
, ' ');
1189 /* Subroutine of layout::print_any_fixits.
1191 Determine if the annotation line printed for LINE contained
1192 the exact range from START_COLUMN to FINISH_COLUMN. */
1195 layout::annotation_line_showed_range_p (int line
, int start_column
,
1196 int finish_column
) const
1198 layout_range
*range
;
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
)
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. */
1214 layout::print_any_fixits (int row
)
1217 for (unsigned int i
= 0; i
< m_fixit_hints
.length (); i
++)
1219 const fixit_hint
*hint
= m_fixit_hints
[i
];
1220 if (hint
->affects_line_p (m_exploc
.file
, row
))
1222 /* For now we assume each fixit hint can only touch one line. */
1223 if (hint
->insertion_p ())
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 ();
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;
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
,
1245 || hint
->get_length () == 0)
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 ();
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)
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 ();
1268 /* Add a trailing newline, if necessary. */
1269 move_to_column (&column
, 0);
1272 /* Disable any colorization and emit a newline. */
1275 layout::print_newline ()
1277 m_colorizer
.set_normal_text ();
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). */
1287 layout::get_state_at_point (/* Inputs. */
1288 int row
, int column
,
1289 int first_non_ws
, int last_non_ws
,
1291 point_state
*out_state
)
1293 layout_range
*range
;
1295 FOR_EACH_VEC_ELT (m_layout_ranges
, i
, range
)
1297 if (range
->contains_point (row
, column
))
1299 out_state
->range_idx
= i
;
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;
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
)
1315 /* We are within a range. */
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). */
1333 layout::get_x_bound_for_row (int row
, int caret_column
,
1334 int last_non_ws_column
)
1336 int result
= caret_column
+ 1;
1338 layout_range
*range
;
1340 FOR_EACH_VEC_ELT (m_layout_ranges
, i
, range
)
1342 if (row
>= range
->m_start
.m_line
)
1344 if (range
->m_finish
.m_line
== row
)
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;
1351 else if (row
< range
->m_finish
.m_line
)
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;
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. */
1369 layout::move_to_column (int *column
, int dest_column
)
1371 /* Start a new line if we need to. */
1372 if (*column
> dest_column
)
1378 while (*column
< dest_column
)
1385 /* For debugging layout issues, render a ruler giving column numbers
1386 (after the 1-column indent). */
1389 layout::show_ruler (int max_column
) const
1392 if (max_column
> 99)
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);
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);
1414 for (int column
= 1 + m_x_offset
; column
<= max_column
; column
++)
1415 pp_character (m_pp
, '0' + (column
% 10));
1419 } /* End of anonymous namespace. */
1421 /* Print the physical source code corresponding to the location of
1422 this diagnostic, with additional annotations. */
1425 diagnostic_show_locus (diagnostic_context
* context
,
1426 rich_location
*richloc
,
1427 diagnostic_t diagnostic_kind
)
1429 pp_newline (context
->printer
);
1431 location_t loc
= richloc
->get_loc ();
1432 /* Do nothing if source-printing has been disabled. */
1433 if (!context
->show_caret
)
1436 /* Don't attempt to print source for UNKNOWN_LOCATION and for builtins. */
1437 if (loc
<= BUILTINS_LOCATION
)
1440 /* Don't print the same source location twice in a row, unless we have
1442 if (loc
== context
->last_location
1443 && richloc
->get_num_fixit_hints () == 0)
1446 context
->last_location
= loc
;
1448 const char *saved_prefix
= pp_get_prefix (context
->printer
);
1449 pp_set_prefix (context
->printer
, NULL
);
1451 layout
layout (context
, richloc
, diagnostic_kind
);
1452 for (int line_span_idx
= 0; line_span_idx
< layout
.get_num_line_spans ();
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
))
1458 expanded_location exploc
= layout
.get_expanded_location (line_span
);
1459 context
->start_span (context
, exploc
);
1461 int last_line
= line_span
->get_last_line ();
1462 for (int row
= line_span
->get_first_line (); row
<= last_line
; row
++)
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
))
1470 if (layout
.should_print_annotation_line_p (row
))
1471 layout
.print_annotation_line (row
, lbounds
);
1472 layout
.print_any_fixits (row
);
1477 pp_set_prefix (context
->printer
, saved_prefix
);
1482 namespace selftest
{
1484 /* Selftests for diagnostic_show_locus. */
1486 /* Convenience subclass of diagnostic_context for testing
1487 diagnostic_show_locus. */
1489 class test_diagnostic_context
: public diagnostic_context
1492 test_diagnostic_context ()
1494 diagnostic_initialize (this, 0);
1497 start_span
= start_span_cb
;
1499 ~test_diagnostic_context ()
1501 diagnostic_finish (this);
1504 /* Implementation of diagnostic_start_span_fn, hiding the
1505 real filename (to avoid printing the names of tempfiles). */
1507 start_span_cb (diagnostic_context
*context
, expanded_location exploc
)
1509 exploc
.file
= "FILENAME";
1510 default_diagnostic_start_span_fn (context
, exploc
);
1514 /* Verify that diagnostic_show_locus works sanely on UNKNOWN_LOCATION. */
1517 test_diagnostic_show_locus_unknown_location ()
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
));
1525 /* Verify that diagnostic_show_locus works sanely for various
1528 All of these work on the following 1-line source file:
1531 "foo = bar.field;\n"
1532 which is set up by test_diagnostic_show_locus_one_liner and calls
1538 test_one_liner_simple_caret ()
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
);
1545 " foo = bar.field;\n"
1547 pp_formatted_text (dc
.printer
));
1550 /* Caret and range. */
1553 test_one_liner_caret_and_range ()
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
);
1563 " foo = bar.field;\n"
1565 pp_formatted_text (dc
.printer
));
1568 /* Multiple ranges and carets. */
1571 test_one_liner_multiple_carets_and_ranges ()
1573 test_diagnostic_context dc
;
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';
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';
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';
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
);
1597 " foo = bar.field;\n"
1599 pp_formatted_text (dc
.printer
));
1602 /* Insertion fix-it hint: adding an "&" to the front of "bar.field". */
1605 test_one_liner_fixit_insert_before ()
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
);
1613 " foo = bar.field;\n"
1616 pp_formatted_text (dc
.printer
));
1619 /* Insertion fix-it hint: adding a "[0]" after "foo". */
1622 test_one_liner_fixit_insert_after ()
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
);
1632 " foo = bar.field;\n"
1635 pp_formatted_text (dc
.printer
));
1638 /* Removal fix-it hint: removal of the ".field". */
1641 test_one_liner_fixit_remove ()
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
);
1651 " foo = bar.field;\n"
1654 pp_formatted_text (dc
.printer
));
1657 /* Replace fix-it hint: replacing "field" with "m_field". */
1660 test_one_liner_fixit_replace ()
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
);
1670 " foo = bar.field;\n"
1673 pp_formatted_text (dc
.printer
));
1676 /* Replace fix-it hint: replacing "field" with "m_field",
1677 but where the caret was elsewhere. */
1680 test_one_liner_fixit_replace_non_equal_range ()
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
);
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. */
1695 " foo = bar.field;\n"
1699 pp_formatted_text (dc
.printer
));
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". */
1707 test_one_liner_fixit_replace_equal_secondary_range ()
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. */
1721 " foo = bar.field;\n"
1724 pp_formatted_text (dc
.printer
));
1727 /* Verify that we can use ad-hoc locations when adding fixits to a
1731 test_one_liner_fixit_validation_adhoc_locations ()
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
);
1740 if (c47
> LINE_MAP_MAX_LOCATION_WITH_COLS
)
1743 ASSERT_TRUE (IS_ADHOC_LOC (loc
));
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 ());
1752 test_diagnostic_context dc
;
1753 diagnostic_show_locus (&dc
, &richloc
, DK_ERROR
);
1755 " foo = bar.field;\n"
1758 pp_formatted_text (dc
.printer
));
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 ());
1769 test_diagnostic_context dc
;
1770 diagnostic_show_locus (&dc
, &richloc
, DK_ERROR
);
1772 " foo = bar.field;\n"
1774 " -----------------------------------------\n",
1775 pp_formatted_text (dc
.printer
));
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 ());
1786 test_diagnostic_context dc
;
1787 diagnostic_show_locus (&dc
, &richloc
, DK_ERROR
);
1789 " foo = bar.field;\n"
1792 pp_formatted_text (dc
.printer
));
1796 /* Test of consolidating insertions at the same location. */
1799 test_one_liner_many_fixits_1 ()
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
);
1809 " foo = bar.field;\n"
1811 " aaaaaaaaaaaaaaaaaaa\n",
1812 pp_formatted_text (dc
.printer
));
1815 /* Ensure that we can add an arbitrary number of fix-it hints to a
1816 rich_location, even if they are not consolidated. */
1819 test_one_liner_many_fixits_2 ()
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
++)
1826 location_t loc
= linemap_position_for_column (line_table
, i
* 2);
1827 richloc
.add_fixit_insert_before (loc
, "a");
1829 ASSERT_EQ (19, richloc
.get_num_fixit_hints ());
1830 diagnostic_show_locus (&dc
, &richloc
, DK_ERROR
);
1832 " foo = bar.field;\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
));
1838 /* Run the various one-liner tests. */
1841 test_diagnostic_show_locus_one_liner (const line_table_case
&case_
)
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_
);
1850 linemap_add (line_table
, LC_ENTER
, false, tmp
.get_filename (), 1);
1852 location_t line_end
= linemap_position_for_column (line_table
, 16);
1854 /* Don't attempt to run the tests if column data might be unavailable. */
1855 if (line_end
> LINE_MAP_MAX_LOCATION_WITH_COLS
)
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
));
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 ();
1876 /* Verify that we print fixits even if they only affect lines
1877 outside those covered by the ranges in the rich_location. */
1880 test_diagnostic_show_locus_fixit_lines (const line_table_case
&case_
)
1882 /* Create a tempfile and write some text to it.
1883 ...000000000111111111122222222223333333333.
1884 ...123456789012345678901234567890123456789. */
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. */
1891 " : 0.0};\n"); /* line 6. */
1892 temp_source_file
tmp (SELFTEST_LOCATION
, ".c", content
);
1893 line_table_test
ltt (case_
);
1895 const line_map_ordinary
*ord_map
1896 = linemap_check_ordinary (linemap_add (line_table
, LC_ENTER
, false,
1897 tmp
.get_filename (), 0));
1899 linemap_line_start (line_table
, 1, 100);
1901 const location_t final_line_end
1902 = linemap_position_for_line_and_column (line_table
, ord_map
, 6, 36);
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
)
1908 /* A pair of tests for modernizing the initializers to C99-style. */
1910 /* The one-liner case (line 2). */
1912 test_diagnostic_context dc
;
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
);
1922 " struct point origin = {x: 0.0,\n"
1925 pp_formatted_text (dc
.printer
));
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). */
1933 test_diagnostic_context dc
;
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
);
1950 pp_formatted_text (dc
.printer
));
1955 /* Verify that fix-it hints are appropriately consolidated.
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.
1961 Otherwise, verify that consecutive "replace" and "remove" fix-its
1962 are merged, and that other fix-its remain separate. */
1965 test_fixit_consolidation (const line_table_case
&case_
)
1967 line_table_test
ltt (case_
);
1969 linemap_add (line_table
, LC_ENTER
, false, "test.c", 1);
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
;
1979 /* Insert + insert. */
1981 rich_location
richloc (line_table
, caret
);
1982 richloc
.add_fixit_insert_before (c10
, "foo");
1983 richloc
.add_fixit_insert_before (c15
, "bar");
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 ());
1989 /* They should not have been merged. */
1990 ASSERT_EQ (2, richloc
.get_num_fixit_hints ());
1993 /* Insert + replace. */
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
),
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 ());
2004 /* They should not have been merged. */
2005 ASSERT_EQ (2, richloc
.get_num_fixit_hints ());
2008 /* Replace + non-consecutive insert. */
2010 rich_location
richloc (line_table
, caret
);
2011 richloc
.add_fixit_replace (source_range::from_locations (c10
, c15
),
2013 richloc
.add_fixit_insert_before (c17
, "foo");
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 ());
2019 /* They should not have been merged. */
2020 ASSERT_EQ (2, richloc
.get_num_fixit_hints ());
2023 /* Replace + non-consecutive replace. */
2025 rich_location
richloc (line_table
, caret
);
2026 richloc
.add_fixit_replace (source_range::from_locations (c10
, c15
),
2028 richloc
.add_fixit_replace (source_range::from_locations (c17
, c20
),
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 ());
2035 /* They should not have been merged. */
2036 ASSERT_EQ (2, richloc
.get_num_fixit_hints ());
2039 /* Replace + consecutive replace. */
2041 rich_location
richloc (line_table
, caret
);
2042 richloc
.add_fixit_replace (source_range::from_locations (c10
, c15
),
2044 richloc
.add_fixit_replace (source_range::from_locations (c16
, c20
),
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 ());
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 ());
2061 /* Replace + consecutive removal. */
2063 rich_location
richloc (line_table
, caret
);
2064 richloc
.add_fixit_replace (source_range::from_locations (c10
, c15
),
2066 richloc
.add_fixit_remove (source_range::from_locations (c16
, c20
));
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 ());
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 ());
2083 /* Consecutive removals. */
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
));
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 ());
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 ());
2104 /* Insertion fix-it hint: adding a "break;" on a line by itself.
2105 This will fail, as newlines aren't yet supported. */
2108 test_fixit_insert_containing_newline (const line_table_case
&case_
)
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. */
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);
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");
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 ());
2135 if (case_finish
> LINE_MAP_MAX_LOCATION_WITH_COLS
)
2138 test_diagnostic_context dc
;
2139 diagnostic_show_locus (&dc
, &richloc
, DK_ERROR
);
2143 pp_formatted_text (dc
.printer
));
2146 /* Replacement fix-it hint containing a newline.
2147 This will fail, as newlines aren't yet supported. */
2150 test_fixit_replace_containing_newline (const line_table_case
&case_
)
2152 /* Create a tempfile and write some text to it.
2153 .........................0000000001111.
2154 .........................1234567890123. */
2155 const char *old_content
= "foo = bar ();\n";
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);
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 =");
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 ());
2174 if (finish
> LINE_MAP_MAX_LOCATION_WITH_COLS
)
2177 test_diagnostic_context dc
;
2178 diagnostic_show_locus (&dc
, &richloc
, DK_ERROR
);
2182 pp_formatted_text (dc
.printer
));
2185 /* Run all of the selftests within this file. */
2188 diagnostic_show_locus_c_tests ()
2190 test_layout_range_for_single_point ();
2191 test_layout_range_for_single_line ();
2192 test_layout_range_for_multiple_lines ();
2194 test_get_line_width_without_trailing_whitespace ();
2196 test_diagnostic_show_locus_unknown_location ();
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
);
2205 } // namespace selftest
2207 #endif /* #if CHECKING_P */