Remove assert in get_def_bb_for_const
[official-gcc.git] / gcc / diagnostic-show-locus.c
blobeeccee517160112435192fd722e9fff83eece3f3
1 /* Diagnostic subroutines for printing source-code
2 Copyright (C) 1999-2016 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
10 version.
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
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"
31 #ifdef HAVE_TERMIOS_H
32 # include <termios.h>
33 #endif
35 #ifdef GWINSZ_IN_SYS_IOCTL
36 # include <sys/ioctl.h>
37 #endif
39 /* Classes for rendering source code and diagnostics, within an
40 anonymous namespace.
41 The work is done by "class layout", which embeds and uses
42 "class colorizer" and "class layout_range" to get things done. */
44 namespace {
46 /* The state at a given point of the source code, assuming that we're
47 in a range: which range are we in, and whether we should draw a caret at
48 this point. */
50 struct point_state
52 int range_idx;
53 bool draw_caret_p;
56 /* A class to inject colorization codes when printing the diagnostic locus.
58 It has one kind of colorization for each of:
59 - normal text
60 - range 0 (the "primary location")
61 - range 1
62 - range 2
64 The class caches the lookup of the color codes for the above.
66 The class also has responsibility for tracking which of the above is
67 active, filtering out unnecessary changes. This allows
68 layout::print_source_line and layout::print_annotation_line
69 to simply request a colorization code for *every* character they print,
70 via this class, and have the filtering be done for them here. */
72 class colorizer
74 public:
75 colorizer (diagnostic_context *context,
76 const diagnostic_info *diagnostic);
77 ~colorizer ();
79 void set_range (int range_idx) { set_state (range_idx); }
80 void set_normal_text () { set_state (STATE_NORMAL_TEXT); }
81 void set_fixit_hint () { set_state (0); }
83 private:
84 void set_state (int state);
85 void begin_state (int state);
86 void finish_state (int state);
88 private:
89 static const int STATE_NORMAL_TEXT = -1;
91 diagnostic_context *m_context;
92 const diagnostic_info *m_diagnostic;
93 int m_current_state;
94 const char *m_caret_cs;
95 const char *m_caret_ce;
96 const char *m_range1_cs;
97 const char *m_range2_cs;
98 const char *m_range_ce;
101 /* A point within a layout_range; similar to an expanded_location,
102 but after filtering on file. */
104 class layout_point
106 public:
107 layout_point (const expanded_location &exploc)
108 : m_line (exploc.line),
109 m_column (exploc.column) {}
111 int m_line;
112 int m_column;
115 /* A class for use by "class layout" below: a filtered location_range. */
117 class layout_range
119 public:
120 layout_range (const expanded_location *start_exploc,
121 const expanded_location *finish_exploc,
122 bool show_caret_p,
123 const expanded_location *caret_exploc);
125 bool contains_point (int row, int column) const;
127 layout_point m_start;
128 layout_point m_finish;
129 bool m_show_caret_p;
130 layout_point m_caret;
133 /* A struct for use by layout::print_source_line for telling
134 layout::print_annotation_line the extents of the source line that
135 it printed, so that underlines can be clipped appropriately. */
137 struct line_bounds
139 int m_first_non_ws;
140 int m_last_non_ws;
143 /* A range of contiguous source lines within a layout (e.g. "lines 5-10"
144 or "line 23"). During the layout ctor, layout::calculate_line_spans
145 splits the pertinent source lines into a list of disjoint line_span
146 instances (e.g. lines 5-10, lines 15-20, line 23). */
148 struct line_span
150 line_span (linenum_type first_line, linenum_type last_line)
151 : m_first_line (first_line), m_last_line (last_line)
153 gcc_assert (first_line <= last_line);
155 linenum_type get_first_line () const { return m_first_line; }
156 linenum_type get_last_line () const { return m_last_line; }
158 bool contains_line_p (linenum_type line) const
160 return line >= m_first_line && line <= m_last_line;
163 static int comparator (const void *p1, const void *p2)
165 const line_span *ls1 = (const line_span *)p1;
166 const line_span *ls2 = (const line_span *)p2;
167 int first_line_diff = (int)ls1->m_first_line - (int)ls2->m_first_line;
168 if (first_line_diff)
169 return first_line_diff;
170 return (int)ls1->m_last_line - (int)ls2->m_last_line;
173 linenum_type m_first_line;
174 linenum_type m_last_line;
177 /* A class to control the overall layout when printing a diagnostic.
179 The layout is determined within the constructor.
180 It is then printed by repeatedly calling the "print_source_line",
181 "print_annotation_line" and "print_any_fixits" methods.
183 We assume we have disjoint ranges. */
185 class layout
187 public:
188 layout (diagnostic_context *context,
189 const diagnostic_info *diagnostic);
191 int get_num_line_spans () const { return m_line_spans.length (); }
192 const line_span *get_line_span (int idx) const { return &m_line_spans[idx]; }
194 bool print_heading_for_line_span_index_p (int line_span_idx) const;
196 expanded_location get_expanded_location (const line_span *) const;
198 bool print_source_line (int row, line_bounds *lbounds_out);
199 void print_annotation_line (int row, const line_bounds lbounds);
200 void print_any_fixits (int row, const rich_location *richloc);
202 void show_ruler (int max_column) const;
204 private:
205 void calculate_line_spans ();
207 void print_newline ();
209 bool
210 get_state_at_point (/* Inputs. */
211 int row, int column,
212 int first_non_ws, int last_non_ws,
213 /* Outputs. */
214 point_state *out_state);
217 get_x_bound_for_row (int row, int caret_column,
218 int last_non_ws);
220 void
221 move_to_column (int *column, int dest_column);
223 private:
224 diagnostic_context *m_context;
225 pretty_printer *m_pp;
226 diagnostic_t m_diagnostic_kind;
227 expanded_location m_exploc;
228 colorizer m_colorizer;
229 bool m_colorize_source_p;
230 auto_vec <layout_range> m_layout_ranges;
231 auto_vec <line_span> m_line_spans;
232 int m_x_offset;
235 /* Implementation of "class colorizer". */
237 /* The constructor for "colorizer". Lookup and store color codes for the
238 different kinds of things we might need to print. */
240 colorizer::colorizer (diagnostic_context *context,
241 const diagnostic_info *diagnostic) :
242 m_context (context),
243 m_diagnostic (diagnostic),
244 m_current_state (STATE_NORMAL_TEXT)
246 m_caret_ce = colorize_stop (pp_show_color (context->printer));
247 m_range1_cs = colorize_start (pp_show_color (context->printer), "range1");
248 m_range2_cs = colorize_start (pp_show_color (context->printer), "range2");
249 m_range_ce = colorize_stop (pp_show_color (context->printer));
252 /* The destructor for "colorize". If colorization is on, print a code to
253 turn it off. */
255 colorizer::~colorizer ()
257 finish_state (m_current_state);
260 /* Update state, printing color codes if necessary if there's a state
261 change. */
263 void
264 colorizer::set_state (int new_state)
266 if (m_current_state != new_state)
268 finish_state (m_current_state);
269 m_current_state = new_state;
270 begin_state (new_state);
274 /* Turn on any colorization for STATE. */
276 void
277 colorizer::begin_state (int state)
279 switch (state)
281 case STATE_NORMAL_TEXT:
282 break;
284 case 0:
285 /* Make range 0 be the same color as the "kind" text
286 (error vs warning vs note). */
287 pp_string
288 (m_context->printer,
289 colorize_start (pp_show_color (m_context->printer),
290 diagnostic_get_color_for_kind (m_diagnostic->kind)));
291 break;
293 case 1:
294 pp_string (m_context->printer, m_range1_cs);
295 break;
297 case 2:
298 pp_string (m_context->printer, m_range2_cs);
299 break;
301 default:
302 /* We don't expect more than 3 ranges per diagnostic. */
303 gcc_unreachable ();
304 break;
308 /* Turn off any colorization for STATE. */
310 void
311 colorizer::finish_state (int state)
313 switch (state)
315 case STATE_NORMAL_TEXT:
316 break;
318 case 0:
319 pp_string (m_context->printer, m_caret_ce);
320 break;
322 default:
323 /* Within a range. */
324 gcc_assert (state > 0);
325 pp_string (m_context->printer, m_range_ce);
326 break;
330 /* Implementation of class layout_range. */
332 /* The constructor for class layout_range.
333 Initialize various layout_point fields from expanded_location
334 equivalents; we've already filtered on file. */
336 layout_range::layout_range (const expanded_location *start_exploc,
337 const expanded_location *finish_exploc,
338 bool show_caret_p,
339 const expanded_location *caret_exploc)
340 : m_start (*start_exploc),
341 m_finish (*finish_exploc),
342 m_show_caret_p (show_caret_p),
343 m_caret (*caret_exploc)
347 /* Is (column, row) within the given range?
348 We've already filtered on the file.
350 Ranges are closed (both limits are within the range).
352 Example A: a single-line range:
353 start: (col=22, line=2)
354 finish: (col=38, line=2)
356 |00000011111111112222222222333333333344444444444
357 |34567890123456789012345678901234567890123456789
358 --+-----------------------------------------------
359 01|bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb
360 02|bbbbbbbbbbbbbbbbbbbSwwwwwwwwwwwwwwwFaaaaaaaaaaa
361 03|aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
363 Example B: a multiline range with
364 start: (col=14, line=3)
365 finish: (col=08, line=5)
367 |00000011111111112222222222333333333344444444444
368 |34567890123456789012345678901234567890123456789
369 --+-----------------------------------------------
370 01|bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb
371 02|bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb
372 03|bbbbbbbbbbbSwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwww
373 04|wwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwww
374 05|wwwwwFaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
375 06|aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
376 --+-----------------------------------------------
378 Legend:
379 - 'b' indicates a point *before* the range
380 - 'S' indicates the start of the range
381 - 'w' indicates a point within the range
382 - 'F' indicates the finish of the range (which is
383 within it).
384 - 'a' indicates a subsequent point *after* the range. */
386 bool
387 layout_range::contains_point (int row, int column) const
389 gcc_assert (m_start.m_line <= m_finish.m_line);
390 /* ...but the equivalent isn't true for the columns;
391 consider example B in the comment above. */
393 if (row < m_start.m_line)
394 /* Points before the first line of the range are
395 outside it (corresponding to line 01 in example A
396 and lines 01 and 02 in example B above). */
397 return false;
399 if (row == m_start.m_line)
400 /* On same line as start of range (corresponding
401 to line 02 in example A and line 03 in example B). */
403 if (column < m_start.m_column)
404 /* Points on the starting line of the range, but
405 before the column in which it begins. */
406 return false;
408 if (row < m_finish.m_line)
409 /* This is a multiline range; the point
410 is within it (corresponds to line 03 in example B
411 from column 14 onwards) */
412 return true;
413 else
415 /* This is a single-line range. */
416 gcc_assert (row == m_finish.m_line);
417 return column <= m_finish.m_column;
421 /* The point is in a line beyond that containing the
422 start of the range: lines 03 onwards in example A,
423 and lines 04 onwards in example B. */
424 gcc_assert (row > m_start.m_line);
426 if (row > m_finish.m_line)
427 /* The point is beyond the final line of the range
428 (lines 03 onwards in example A, and lines 06 onwards
429 in example B). */
430 return false;
432 if (row < m_finish.m_line)
434 /* The point is in a line that's fully within a multiline
435 range (e.g. line 04 in example B). */
436 gcc_assert (m_start.m_line < m_finish.m_line);
437 return true;
440 gcc_assert (row == m_finish.m_line);
442 return column <= m_finish.m_column;
445 /* Given a source line LINE of length LINE_WIDTH, determine the width
446 without any trailing whitespace. */
448 static int
449 get_line_width_without_trailing_whitespace (const char *line, int line_width)
451 int result = line_width;
452 while (result > 0)
454 char ch = line[result - 1];
455 if (ch == ' ' || ch == '\t')
456 result--;
457 else
458 break;
460 gcc_assert (result >= 0);
461 gcc_assert (result <= line_width);
462 gcc_assert (result == 0 ||
463 (line[result - 1] != ' '
464 && line[result -1] != '\t'));
465 return result;
468 /* Helper function for layout's ctor, for sanitizing locations relative
469 to the primary location within a diagnostic.
471 Compare LOC_A and LOC_B to see if it makes sense to print underlines
472 connecting their expanded locations. Doing so is only guaranteed to
473 make sense if the locations share the same macro expansion "history"
474 i.e. they can be traced through the same macro expansions, eventually
475 reaching an ordinary map.
477 This may be too strong a condition, but it effectively sanitizes
478 PR c++/70105, which has an example of printing an expression where the
479 final location of the expression is in a different macro, which
480 erroneously was leading to hundreds of lines of irrelevant source
481 being printed. */
483 static bool
484 compatible_locations_p (location_t loc_a, location_t loc_b)
486 if (IS_ADHOC_LOC (loc_a))
487 loc_a = get_location_from_adhoc_loc (line_table, loc_a);
488 if (IS_ADHOC_LOC (loc_b))
489 loc_b = get_location_from_adhoc_loc (line_table, loc_b);
491 /* If either location is one of the special locations outside of a
492 linemap, they are only compatible if they are equal. */
493 if (loc_a < RESERVED_LOCATION_COUNT
494 || loc_b < RESERVED_LOCATION_COUNT)
495 return loc_a == loc_b;
497 const line_map *map_a = linemap_lookup (line_table, loc_a);
498 linemap_assert (map_a);
500 const line_map *map_b = linemap_lookup (line_table, loc_b);
501 linemap_assert (map_b);
503 /* Are they within the same map? */
504 if (map_a == map_b)
506 /* Are both within the same macro expansion? */
507 if (linemap_macro_expansion_map_p (map_a))
509 /* Expand each location towards the spelling location, and
510 recurse. */
511 const line_map_macro *macro_map = linemap_check_macro (map_a);
512 source_location loc_a_toward_spelling
513 = linemap_macro_map_loc_unwind_toward_spelling (line_table,
514 macro_map,
515 loc_a);
516 source_location loc_b_toward_spelling
517 = linemap_macro_map_loc_unwind_toward_spelling (line_table,
518 macro_map,
519 loc_b);
520 return compatible_locations_p (loc_a_toward_spelling,
521 loc_b_toward_spelling);
524 /* Otherwise they are within the same ordinary map. */
525 return true;
527 else
529 /* Within different maps. */
531 /* If either is within a macro expansion, they are incompatible. */
532 if (linemap_macro_expansion_map_p (map_a)
533 || linemap_macro_expansion_map_p (map_b))
534 return false;
536 /* Within two different ordinary maps; they are compatible iff they
537 are in the same file. */
538 const line_map_ordinary *ord_map_a = linemap_check_ordinary (map_a);
539 const line_map_ordinary *ord_map_b = linemap_check_ordinary (map_b);
540 return ord_map_a->to_file == ord_map_b->to_file;
544 /* Implementation of class layout. */
546 /* Constructor for class layout.
548 Filter the ranges from the rich_location to those that we can
549 sanely print, populating m_layout_ranges.
550 Determine the range of lines that we will print, splitting them
551 up into an ordered list of disjoint spans of contiguous line numbers.
552 Determine m_x_offset, to ensure that the primary caret
553 will fit within the max_width provided by the diagnostic_context. */
555 layout::layout (diagnostic_context * context,
556 const diagnostic_info *diagnostic)
557 : m_context (context),
558 m_pp (context->printer),
559 m_diagnostic_kind (diagnostic->kind),
560 m_exploc (diagnostic->richloc->get_expanded_location (0)),
561 m_colorizer (context, diagnostic),
562 m_colorize_source_p (context->colorize_source_p),
563 m_layout_ranges (rich_location::MAX_RANGES),
564 m_line_spans (1 + rich_location::MAX_RANGES),
565 m_x_offset (0)
567 rich_location *richloc = diagnostic->richloc;
568 source_location primary_loc = richloc->get_range (0)->m_loc;
570 for (unsigned int idx = 0; idx < richloc->get_num_locations (); idx++)
572 /* This diagnostic printer can only cope with "sufficiently sane" ranges.
573 Ignore any ranges that are awkward to handle. */
574 const location_range *loc_range = richloc->get_range (idx);
576 /* Split the "range" into caret and range information. */
577 source_range src_range = get_range_from_loc (line_table, loc_range->m_loc);
579 /* Expand the various locations. */
580 expanded_location start
581 = linemap_client_expand_location_to_spelling_point (src_range.m_start);
582 expanded_location finish
583 = linemap_client_expand_location_to_spelling_point (src_range.m_finish);
584 expanded_location caret
585 = linemap_client_expand_location_to_spelling_point (loc_range->m_loc);
587 /* If any part of the range isn't in the same file as the primary
588 location of this diagnostic, ignore the range. */
589 if (start.file != m_exploc.file)
590 continue;
591 if (finish.file != m_exploc.file)
592 continue;
593 if (loc_range->m_show_caret_p)
594 if (caret.file != m_exploc.file)
595 continue;
597 /* Sanitize the caret location for non-primary ranges. */
598 if (m_layout_ranges.length () > 0)
599 if (loc_range->m_show_caret_p)
600 if (!compatible_locations_p (loc_range->m_loc, primary_loc))
601 /* Discard any non-primary ranges that can't be printed
602 sanely relative to the primary location. */
603 continue;
605 /* Everything is now known to be in the correct source file,
606 but it may require further sanitization. */
607 layout_range ri (&start, &finish, loc_range->m_show_caret_p, &caret);
609 /* If we have a range that finishes before it starts (perhaps
610 from something built via macro expansion), printing the
611 range is likely to be nonsensical. Also, attempting to do so
612 breaks assumptions within the printing code (PR c/68473).
613 Similarly, don't attempt to print ranges if one or both ends
614 of the range aren't sane to print relative to the
615 primary location (PR c++/70105). */
616 if (start.line > finish.line
617 || !compatible_locations_p (src_range.m_start, primary_loc)
618 || !compatible_locations_p (src_range.m_finish, primary_loc))
620 /* Is this the primary location? */
621 if (m_layout_ranges.length () == 0)
623 /* We want to print the caret for the primary location, but
624 we must sanitize away m_start and m_finish. */
625 ri.m_start = ri.m_caret;
626 ri.m_finish = ri.m_caret;
628 else
629 /* This is a non-primary range; ignore it. */
630 continue;
633 /* Passed all the tests; add the range to m_layout_ranges so that
634 it will be printed. */
635 m_layout_ranges.safe_push (ri);
638 /* Populate m_line_spans. */
639 calculate_line_spans ();
641 /* Adjust m_x_offset.
642 Center the primary caret to fit in max_width; all columns
643 will be adjusted accordingly. */
644 int max_width = m_context->caret_max_width;
645 int line_width;
646 const char *line = location_get_source_line (m_exploc.file, m_exploc.line,
647 &line_width);
648 if (line && m_exploc.column <= line_width)
650 int right_margin = CARET_LINE_MARGIN;
651 int column = m_exploc.column;
652 right_margin = MIN (line_width - column, right_margin);
653 right_margin = max_width - right_margin;
654 if (line_width >= max_width && column > right_margin)
655 m_x_offset = column - right_margin;
656 gcc_assert (m_x_offset >= 0);
659 if (context->show_ruler_p)
660 show_ruler (m_x_offset + max_width);
663 /* Return true iff we should print a heading when starting the
664 line span with the given index. */
666 bool
667 layout::print_heading_for_line_span_index_p (int line_span_idx) const
669 /* We print a heading for every change of line span, hence for every
670 line span after the initial one. */
671 if (line_span_idx > 0)
672 return true;
674 /* We also do it for the initial span if the primary location of the
675 diagnostic is in a different span. */
676 if (m_exploc.line > (int)get_line_span (0)->m_last_line)
677 return true;
679 return false;
682 /* Get an expanded_location for the first location of interest within
683 the given line_span.
684 Used when printing a heading to indicate a new line span. */
686 expanded_location
687 layout::get_expanded_location (const line_span *line_span) const
689 /* Whenever possible, use the caret location. */
690 if (line_span->contains_line_p (m_exploc.line))
691 return m_exploc;
693 /* Otherwise, use the start of the first range that's present
694 within the line_span. */
695 for (unsigned int i = 0; i < m_layout_ranges.length (); i++)
697 const layout_range *lr = &m_layout_ranges[i];
698 if (line_span->contains_line_p (lr->m_start.m_line))
700 expanded_location exploc = m_exploc;
701 exploc.line = lr->m_start.m_line;
702 exploc.column = lr->m_start.m_column;
703 return exploc;
707 /* It should not be possible to have a line span that didn't
708 contain any of the layout_range instances. */
709 gcc_unreachable ();
710 return m_exploc;
713 /* We want to print the pertinent source code at a diagnostic. The
714 rich_location can contain multiple locations. This will have been
715 filtered into m_exploc (the caret for the primary location) and
716 m_layout_ranges, for those ranges within the same source file.
718 We will print a subset of the lines within the source file in question,
719 as a collection of "spans" of lines.
721 This function populates m_line_spans with an ordered, disjoint list of
722 the line spans of interest.
724 For example, if the primary caret location is on line 7, with ranges
725 covering lines 5-6 and lines 9-12:
728 005 |RANGE 0
729 006 |RANGE 0
730 007 |PRIMARY CARET
732 009 |RANGE 1
733 010 |RANGE 1
734 011 |RANGE 1
735 012 |RANGE 1
738 then we want two spans: lines 5-7 and lines 9-12. */
740 void
741 layout::calculate_line_spans ()
743 /* This should only be called once, by the ctor. */
744 gcc_assert (m_line_spans.length () == 0);
746 /* Populate tmp_spans with individual spans, for each of
747 m_exploc, and for m_layout_ranges. */
748 auto_vec<line_span> tmp_spans (1 + rich_location::MAX_RANGES);
749 tmp_spans.safe_push (line_span (m_exploc.line, m_exploc.line));
750 for (unsigned int i = 0; i < m_layout_ranges.length (); i++)
752 const layout_range *lr = &m_layout_ranges[i];
753 gcc_assert (lr->m_start.m_line <= lr->m_finish.m_line);
754 tmp_spans.safe_push (line_span (lr->m_start.m_line,
755 lr->m_finish.m_line));
758 /* Sort them. */
759 tmp_spans.qsort(line_span::comparator);
761 /* Now iterate through tmp_spans, copying into m_line_spans, and
762 combining where possible. */
763 gcc_assert (tmp_spans.length () > 0);
764 m_line_spans.safe_push (tmp_spans[0]);
765 for (unsigned int i = 1; i < tmp_spans.length (); i++)
767 line_span *current = &m_line_spans[m_line_spans.length () - 1];
768 const line_span *next = &tmp_spans[i];
769 gcc_assert (next->m_first_line >= current->m_first_line);
770 if (next->m_first_line <= current->m_last_line + 1)
772 /* We can merge them. */
773 if (next->m_last_line > current->m_last_line)
774 current->m_last_line = next->m_last_line;
776 else
778 /* No merger possible. */
779 m_line_spans.safe_push (*next);
783 /* Verify the result, in m_line_spans. */
784 gcc_assert (m_line_spans.length () > 0);
785 for (unsigned int i = 1; i < m_line_spans.length (); i++)
787 const line_span *prev = &m_line_spans[i - 1];
788 const line_span *next = &m_line_spans[i];
789 /* The individual spans must be sane. */
790 gcc_assert (prev->m_first_line <= prev->m_last_line);
791 gcc_assert (next->m_first_line <= next->m_last_line);
792 /* The spans must be ordered. */
793 gcc_assert (prev->m_first_line < next->m_first_line);
794 /* There must be a gap of at least one line between separate spans. */
795 gcc_assert ((prev->m_last_line + 1) < next->m_first_line);
799 /* Attempt to print line ROW of source code, potentially colorized at any
800 ranges.
801 Return true if the line was printed, populating *LBOUNDS_OUT.
802 Return false if the source line could not be read, leaving *LBOUNDS_OUT
803 untouched. */
805 bool
806 layout::print_source_line (int row, line_bounds *lbounds_out)
808 int line_width;
809 const char *line = location_get_source_line (m_exploc.file, row,
810 &line_width);
811 if (!line)
812 return false;
814 m_colorizer.set_normal_text ();
816 /* We will stop printing the source line at any trailing
817 whitespace. */
818 line_width = get_line_width_without_trailing_whitespace (line,
819 line_width);
820 line += m_x_offset;
822 pp_space (m_pp);
823 int first_non_ws = INT_MAX;
824 int last_non_ws = 0;
825 int column;
826 for (column = 1 + m_x_offset; column <= line_width; column++)
828 /* Assuming colorization is enabled for the caret and underline
829 characters, we may also colorize the associated characters
830 within the source line.
832 For frontends that generate range information, we color the
833 associated characters in the source line the same as the
834 carets and underlines in the annotation line, to make it easier
835 for the reader to see the pertinent code.
837 For frontends that only generate carets, we don't colorize the
838 characters above them, since this would look strange (e.g.
839 colorizing just the first character in a token). */
840 if (m_colorize_source_p)
842 bool in_range_p;
843 point_state state;
844 in_range_p = get_state_at_point (row, column,
845 0, INT_MAX,
846 &state);
847 if (in_range_p)
848 m_colorizer.set_range (state.range_idx);
849 else
850 m_colorizer.set_normal_text ();
852 char c = *line == '\t' ? ' ' : *line;
853 if (c == '\0')
854 c = ' ';
855 if (c != ' ')
857 last_non_ws = column;
858 if (first_non_ws == INT_MAX)
859 first_non_ws = column;
861 pp_character (m_pp, c);
862 line++;
864 print_newline ();
866 lbounds_out->m_first_non_ws = first_non_ws;
867 lbounds_out->m_last_non_ws = last_non_ws;
868 return true;
871 /* Print a line consisting of the caret/underlines for the given
872 source line. */
874 void
875 layout::print_annotation_line (int row, const line_bounds lbounds)
877 int x_bound = get_x_bound_for_row (row, m_exploc.column,
878 lbounds.m_last_non_ws);
880 pp_space (m_pp);
881 for (int column = 1 + m_x_offset; column < x_bound; column++)
883 bool in_range_p;
884 point_state state;
885 in_range_p = get_state_at_point (row, column,
886 lbounds.m_first_non_ws,
887 lbounds.m_last_non_ws,
888 &state);
889 if (in_range_p)
891 /* Within a range. Draw either the caret or an underline. */
892 m_colorizer.set_range (state.range_idx);
893 if (state.draw_caret_p)
894 /* Draw the caret. */
895 pp_character (m_pp, m_context->caret_chars[state.range_idx]);
896 else
897 pp_character (m_pp, '~');
899 else
901 /* Not in a range. */
902 m_colorizer.set_normal_text ();
903 pp_character (m_pp, ' ');
906 print_newline ();
909 /* If there are any fixit hints on source line ROW within RICHLOC, print them.
910 They are printed in order, attempting to combine them onto lines, but
911 starting new lines if necessary. */
913 void
914 layout::print_any_fixits (int row, const rich_location *richloc)
916 int column = 0;
917 for (unsigned int i = 0; i < richloc->get_num_fixit_hints (); i++)
919 fixit_hint *hint = richloc->get_fixit_hint (i);
920 if (hint->affects_line_p (m_exploc.file, row))
922 /* For now we assume each fixit hint can only touch one line. */
923 switch (hint->get_kind ())
925 case fixit_hint::INSERT:
927 fixit_insert *insert = static_cast <fixit_insert *> (hint);
928 /* This assumes the insertion just affects one line. */
929 int start_column
930 = LOCATION_COLUMN (insert->get_location ());
931 move_to_column (&column, start_column);
932 m_colorizer.set_fixit_hint ();
933 pp_string (m_pp, insert->get_string ());
934 m_colorizer.set_normal_text ();
935 column += insert->get_length ();
937 break;
939 case fixit_hint::REMOVE:
941 fixit_remove *remove = static_cast <fixit_remove *> (hint);
942 /* This assumes the removal just affects one line. */
943 source_range src_range = remove->get_range ();
944 int start_column = LOCATION_COLUMN (src_range.m_start);
945 int finish_column = LOCATION_COLUMN (src_range.m_finish);
946 move_to_column (&column, start_column);
947 for (int column = start_column; column <= finish_column; column++)
949 m_colorizer.set_fixit_hint ();
950 pp_character (m_pp, '-');
951 m_colorizer.set_normal_text ();
954 break;
956 case fixit_hint::REPLACE:
958 fixit_replace *replace = static_cast <fixit_replace *> (hint);
959 int start_column
960 = LOCATION_COLUMN (replace->get_range ().m_start);
961 move_to_column (&column, start_column);
962 m_colorizer.set_fixit_hint ();
963 pp_string (m_pp, replace->get_string ());
964 m_colorizer.set_normal_text ();
965 column += replace->get_length ();
967 break;
969 default:
970 gcc_unreachable ();
975 /* Add a trailing newline, if necessary. */
976 move_to_column (&column, 0);
979 /* Disable any colorization and emit a newline. */
981 void
982 layout::print_newline ()
984 m_colorizer.set_normal_text ();
985 pp_newline (m_pp);
988 /* Return true if (ROW/COLUMN) is within a range of the layout.
989 If it returns true, OUT_STATE is written to, with the
990 range index, and whether we should draw the caret at
991 (ROW/COLUMN) (as opposed to an underline). */
993 bool
994 layout::get_state_at_point (/* Inputs. */
995 int row, int column,
996 int first_non_ws, int last_non_ws,
997 /* Outputs. */
998 point_state *out_state)
1000 layout_range *range;
1001 int i;
1002 FOR_EACH_VEC_ELT (m_layout_ranges, i, range)
1004 if (range->contains_point (row, column))
1006 out_state->range_idx = i;
1008 /* Are we at the range's caret? is it visible? */
1009 out_state->draw_caret_p = false;
1010 if (range->m_show_caret_p
1011 && row == range->m_caret.m_line
1012 && column == range->m_caret.m_column)
1013 out_state->draw_caret_p = true;
1015 /* Within a multiline range, don't display any underline
1016 in any leading or trailing whitespace on a line.
1017 We do display carets, however. */
1018 if (!out_state->draw_caret_p)
1019 if (column < first_non_ws || column > last_non_ws)
1020 return false;
1022 /* We are within a range. */
1023 return true;
1027 return false;
1030 /* Helper function for use by layout::print_line when printing the
1031 annotation line under the source line.
1032 Get the column beyond the rightmost one that could contain a caret or
1033 range marker, given that we stop rendering at trailing whitespace.
1034 ROW is the source line within the given file.
1035 CARET_COLUMN is the column of range 0's caret.
1036 LAST_NON_WS_COLUMN is the last column containing a non-whitespace
1037 character of source (as determined when printing the source line). */
1040 layout::get_x_bound_for_row (int row, int caret_column,
1041 int last_non_ws_column)
1043 int result = caret_column + 1;
1045 layout_range *range;
1046 int i;
1047 FOR_EACH_VEC_ELT (m_layout_ranges, i, range)
1049 if (row >= range->m_start.m_line)
1051 if (range->m_finish.m_line == row)
1053 /* On the final line within a range; ensure that
1054 we render up to the end of the range. */
1055 if (result <= range->m_finish.m_column)
1056 result = range->m_finish.m_column + 1;
1058 else if (row < range->m_finish.m_line)
1060 /* Within a multiline range; ensure that we render up to the
1061 last non-whitespace column. */
1062 if (result <= last_non_ws_column)
1063 result = last_non_ws_column + 1;
1068 return result;
1071 /* Given *COLUMN as an x-coordinate, print spaces to position
1072 successive output at DEST_COLUMN, printing a newline if necessary,
1073 and updating *COLUMN. */
1075 void
1076 layout::move_to_column (int *column, int dest_column)
1078 /* Start a new line if we need to. */
1079 if (*column > dest_column)
1081 print_newline ();
1082 *column = 0;
1085 while (*column < dest_column)
1087 pp_space (m_pp);
1088 (*column)++;
1092 /* For debugging layout issues, render a ruler giving column numbers
1093 (after the 1-column indent). */
1095 void
1096 layout::show_ruler (int max_column) const
1098 /* Hundreds. */
1099 if (max_column > 99)
1101 pp_space (m_pp);
1102 for (int column = 1 + m_x_offset; column <= max_column; column++)
1103 if (0 == column % 10)
1104 pp_character (m_pp, '0' + (column / 100) % 10);
1105 else
1106 pp_space (m_pp);
1107 pp_newline (m_pp);
1110 /* Tens. */
1111 pp_space (m_pp);
1112 for (int column = 1 + m_x_offset; column <= max_column; column++)
1113 if (0 == column % 10)
1114 pp_character (m_pp, '0' + (column / 10) % 10);
1115 else
1116 pp_space (m_pp);
1117 pp_newline (m_pp);
1119 /* Units. */
1120 pp_space (m_pp);
1121 for (int column = 1 + m_x_offset; column <= max_column; column++)
1122 pp_character (m_pp, '0' + (column % 10));
1123 pp_newline (m_pp);
1126 } /* End of anonymous namespace. */
1128 /* Print the physical source code corresponding to the location of
1129 this diagnostic, with additional annotations. */
1131 void
1132 diagnostic_show_locus (diagnostic_context * context,
1133 const diagnostic_info *diagnostic)
1135 pp_newline (context->printer);
1137 if (!context->show_caret
1138 || diagnostic_location (diagnostic, 0) <= BUILTINS_LOCATION
1139 || diagnostic_location (diagnostic, 0) == context->last_location)
1140 return;
1142 context->last_location = diagnostic_location (diagnostic, 0);
1144 const char *saved_prefix = pp_get_prefix (context->printer);
1145 pp_set_prefix (context->printer, NULL);
1147 layout layout (context, diagnostic);
1148 for (int line_span_idx = 0; line_span_idx < layout.get_num_line_spans ();
1149 line_span_idx++)
1151 const line_span *line_span = layout.get_line_span (line_span_idx);
1152 if (layout.print_heading_for_line_span_index_p (line_span_idx))
1154 expanded_location exploc = layout.get_expanded_location (line_span);
1155 context->start_span (context, exploc);
1157 int last_line = line_span->get_last_line ();
1158 for (int row = line_span->get_first_line (); row <= last_line; row++)
1160 /* Print the source line, followed by an annotation line
1161 consisting of any caret/underlines, then any fixits.
1162 If the source line can't be read, print nothing. */
1163 line_bounds lbounds;
1164 if (layout.print_source_line (row, &lbounds))
1166 layout.print_annotation_line (row, lbounds);
1167 layout.print_any_fixits (row, diagnostic->richloc);
1172 pp_set_prefix (context->printer, saved_prefix);