S/390: Add -fsplit-stack support
[official-gcc.git] / gcc / diagnostic-show-locus.c
blob3acdb32089c976f704f5d31f9241f6500c1fb1f2
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 location_range *loc_range);
122 bool contains_point (int row, int column) const;
124 layout_point m_start;
125 layout_point m_finish;
126 bool m_show_caret_p;
127 layout_point m_caret;
130 /* A struct for use by layout::print_source_line for telling
131 layout::print_annotation_line the extents of the source line that
132 it printed, so that underlines can be clipped appropriately. */
134 struct line_bounds
136 int m_first_non_ws;
137 int m_last_non_ws;
140 /* A range of contiguous source lines within a layout (e.g. "lines 5-10"
141 or "line 23"). During the layout ctor, layout::calculate_line_spans
142 splits the pertinent source lines into a list of disjoint line_span
143 instances (e.g. lines 5-10, lines 15-20, line 23). */
145 struct line_span
147 line_span (linenum_type first_line, linenum_type last_line)
148 : m_first_line (first_line), m_last_line (last_line)
150 gcc_assert (first_line <= last_line);
152 linenum_type get_first_line () const { return m_first_line; }
153 linenum_type get_last_line () const { return m_last_line; }
155 bool contains_line_p (linenum_type line) const
157 return line >= m_first_line && line <= m_last_line;
160 static int comparator (const void *p1, const void *p2)
162 const line_span *ls1 = (const line_span *)p1;
163 const line_span *ls2 = (const line_span *)p2;
164 int first_line_diff = (int)ls1->m_first_line - (int)ls2->m_first_line;
165 if (first_line_diff)
166 return first_line_diff;
167 return (int)ls1->m_last_line - (int)ls2->m_last_line;
170 linenum_type m_first_line;
171 linenum_type m_last_line;
174 /* A class to control the overall layout when printing a diagnostic.
176 The layout is determined within the constructor.
177 It is then printed by repeatedly calling the "print_source_line",
178 "print_annotation_line" and "print_any_fixits" methods.
180 We assume we have disjoint ranges. */
182 class layout
184 public:
185 layout (diagnostic_context *context,
186 const diagnostic_info *diagnostic);
188 int get_num_line_spans () const { return m_line_spans.length (); }
189 const line_span *get_line_span (int idx) const { return &m_line_spans[idx]; }
191 bool print_heading_for_line_span_index_p (int line_span_idx) const;
193 expanded_location get_expanded_location (const line_span *) const;
195 bool print_source_line (int row, line_bounds *lbounds_out);
196 void print_annotation_line (int row, const line_bounds lbounds);
197 void print_any_fixits (int row, const rich_location *richloc);
199 private:
200 void calculate_line_spans ();
202 void print_newline ();
204 bool
205 get_state_at_point (/* Inputs. */
206 int row, int column,
207 int first_non_ws, int last_non_ws,
208 /* Outputs. */
209 point_state *out_state);
212 get_x_bound_for_row (int row, int caret_column,
213 int last_non_ws);
215 void
216 move_to_column (int *column, int dest_column);
218 private:
219 diagnostic_context *m_context;
220 pretty_printer *m_pp;
221 diagnostic_t m_diagnostic_kind;
222 expanded_location m_exploc;
223 colorizer m_colorizer;
224 bool m_colorize_source_p;
225 auto_vec <layout_range> m_layout_ranges;
226 auto_vec <line_span> m_line_spans;
227 int m_x_offset;
230 /* Implementation of "class colorizer". */
232 /* The constructor for "colorizer". Lookup and store color codes for the
233 different kinds of things we might need to print. */
235 colorizer::colorizer (diagnostic_context *context,
236 const diagnostic_info *diagnostic) :
237 m_context (context),
238 m_diagnostic (diagnostic),
239 m_current_state (STATE_NORMAL_TEXT)
241 m_caret_ce = colorize_stop (pp_show_color (context->printer));
242 m_range1_cs = colorize_start (pp_show_color (context->printer), "range1");
243 m_range2_cs = colorize_start (pp_show_color (context->printer), "range2");
244 m_range_ce = colorize_stop (pp_show_color (context->printer));
247 /* The destructor for "colorize". If colorization is on, print a code to
248 turn it off. */
250 colorizer::~colorizer ()
252 finish_state (m_current_state);
255 /* Update state, printing color codes if necessary if there's a state
256 change. */
258 void
259 colorizer::set_state (int new_state)
261 if (m_current_state != new_state)
263 finish_state (m_current_state);
264 m_current_state = new_state;
265 begin_state (new_state);
269 /* Turn on any colorization for STATE. */
271 void
272 colorizer::begin_state (int state)
274 switch (state)
276 case STATE_NORMAL_TEXT:
277 break;
279 case 0:
280 /* Make range 0 be the same color as the "kind" text
281 (error vs warning vs note). */
282 pp_string
283 (m_context->printer,
284 colorize_start (pp_show_color (m_context->printer),
285 diagnostic_get_color_for_kind (m_diagnostic->kind)));
286 break;
288 case 1:
289 pp_string (m_context->printer, m_range1_cs);
290 break;
292 case 2:
293 pp_string (m_context->printer, m_range2_cs);
294 break;
296 default:
297 /* We don't expect more than 3 ranges per diagnostic. */
298 gcc_unreachable ();
299 break;
303 /* Turn off any colorization for STATE. */
305 void
306 colorizer::finish_state (int state)
308 switch (state)
310 case STATE_NORMAL_TEXT:
311 break;
313 case 0:
314 pp_string (m_context->printer, m_caret_ce);
315 break;
317 default:
318 /* Within a range. */
319 gcc_assert (state > 0);
320 pp_string (m_context->printer, m_range_ce);
321 break;
325 /* Implementation of class layout_range. */
327 /* The constructor for class layout_range.
328 Initialize various layout_point fields from expanded_location
329 equivalents; we've already filtered on file. */
331 layout_range::layout_range (const location_range *loc_range)
332 : m_start (loc_range->m_start),
333 m_finish (loc_range->m_finish),
334 m_show_caret_p (loc_range->m_show_caret_p),
335 m_caret (loc_range->m_caret)
339 /* Is (column, row) within the given range?
340 We've already filtered on the file.
342 Ranges are closed (both limits are within the range).
344 Example A: a single-line range:
345 start: (col=22, line=2)
346 finish: (col=38, line=2)
348 |00000011111111112222222222333333333344444444444
349 |34567890123456789012345678901234567890123456789
350 --+-----------------------------------------------
351 01|bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb
352 02|bbbbbbbbbbbbbbbbbbbSwwwwwwwwwwwwwwwFaaaaaaaaaaa
353 03|aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
355 Example B: a multiline range with
356 start: (col=14, line=3)
357 finish: (col=08, line=5)
359 |00000011111111112222222222333333333344444444444
360 |34567890123456789012345678901234567890123456789
361 --+-----------------------------------------------
362 01|bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb
363 02|bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb
364 03|bbbbbbbbbbbSwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwww
365 04|wwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwww
366 05|wwwwwFaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
367 06|aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
368 --+-----------------------------------------------
370 Legend:
371 - 'b' indicates a point *before* the range
372 - 'S' indicates the start of the range
373 - 'w' indicates a point within the range
374 - 'F' indicates the finish of the range (which is
375 within it).
376 - 'a' indicates a subsequent point *after* the range. */
378 bool
379 layout_range::contains_point (int row, int column) const
381 gcc_assert (m_start.m_line <= m_finish.m_line);
382 /* ...but the equivalent isn't true for the columns;
383 consider example B in the comment above. */
385 if (row < m_start.m_line)
386 /* Points before the first line of the range are
387 outside it (corresponding to line 01 in example A
388 and lines 01 and 02 in example B above). */
389 return false;
391 if (row == m_start.m_line)
392 /* On same line as start of range (corresponding
393 to line 02 in example A and line 03 in example B). */
395 if (column < m_start.m_column)
396 /* Points on the starting line of the range, but
397 before the column in which it begins. */
398 return false;
400 if (row < m_finish.m_line)
401 /* This is a multiline range; the point
402 is within it (corresponds to line 03 in example B
403 from column 14 onwards) */
404 return true;
405 else
407 /* This is a single-line range. */
408 gcc_assert (row == m_finish.m_line);
409 return column <= m_finish.m_column;
413 /* The point is in a line beyond that containing the
414 start of the range: lines 03 onwards in example A,
415 and lines 04 onwards in example B. */
416 gcc_assert (row > m_start.m_line);
418 if (row > m_finish.m_line)
419 /* The point is beyond the final line of the range
420 (lines 03 onwards in example A, and lines 06 onwards
421 in example B). */
422 return false;
424 if (row < m_finish.m_line)
426 /* The point is in a line that's fully within a multiline
427 range (e.g. line 04 in example B). */
428 gcc_assert (m_start.m_line < m_finish.m_line);
429 return true;
432 gcc_assert (row == m_finish.m_line);
434 return column <= m_finish.m_column;
437 /* Given a source line LINE of length LINE_WIDTH, determine the width
438 without any trailing whitespace. */
440 static int
441 get_line_width_without_trailing_whitespace (const char *line, int line_width)
443 int result = line_width;
444 while (result > 0)
446 char ch = line[result - 1];
447 if (ch == ' ' || ch == '\t')
448 result--;
449 else
450 break;
452 gcc_assert (result >= 0);
453 gcc_assert (result <= line_width);
454 gcc_assert (result == 0 ||
455 (line[result - 1] != ' '
456 && line[result -1] != '\t'));
457 return result;
460 /* Implementation of class layout. */
462 /* Constructor for class layout.
464 Filter the ranges from the rich_location to those that we can
465 sanely print, populating m_layout_ranges.
466 Determine the range of lines that we will print, splitting them
467 up into an ordered list of disjoint spans of contiguous line numbers.
468 Determine m_x_offset, to ensure that the primary caret
469 will fit within the max_width provided by the diagnostic_context. */
471 layout::layout (diagnostic_context * context,
472 const diagnostic_info *diagnostic)
473 : m_context (context),
474 m_pp (context->printer),
475 m_diagnostic_kind (diagnostic->kind),
476 m_exploc (diagnostic->richloc->lazily_expand_location ()),
477 m_colorizer (context, diagnostic),
478 m_colorize_source_p (context->colorize_source_p),
479 m_layout_ranges (rich_location::MAX_RANGES),
480 m_line_spans (1 + rich_location::MAX_RANGES),
481 m_x_offset (0)
483 rich_location *richloc = diagnostic->richloc;
484 for (unsigned int idx = 0; idx < richloc->get_num_locations (); idx++)
486 /* This diagnostic printer can only cope with "sufficiently sane" ranges.
487 Ignore any ranges that are awkward to handle. */
488 const location_range *loc_range = richloc->get_range (idx);
490 /* If any part of the range isn't in the same file as the primary
491 location of this diagnostic, ignore the range. */
492 if (loc_range->m_start.file != m_exploc.file)
493 continue;
494 if (loc_range->m_finish.file != m_exploc.file)
495 continue;
496 if (loc_range->m_show_caret_p)
497 if (loc_range->m_caret.file != m_exploc.file)
498 continue;
500 /* Everything is now known to be in the correct source file,
501 but it may require further sanitization. */
502 layout_range ri (loc_range);
504 /* If we have a range that finishes before it starts (perhaps
505 from something built via macro expansion), printing the
506 range is likely to be nonsensical. Also, attempting to do so
507 breaks assumptions within the printing code (PR c/68473). */
508 if (loc_range->m_start.line > loc_range->m_finish.line)
510 /* Is this the primary location? */
511 if (m_layout_ranges.length () == 0)
513 /* We want to print the caret for the primary location, but
514 we must sanitize away m_start and m_finish. */
515 ri.m_start = ri.m_caret;
516 ri.m_finish = ri.m_caret;
518 else
519 /* This is a non-primary range; ignore it. */
520 continue;
523 /* Passed all the tests; add the range to m_layout_ranges so that
524 it will be printed. */
525 m_layout_ranges.safe_push (ri);
528 /* Populate m_line_spans. */
529 calculate_line_spans ();
531 /* Adjust m_x_offset.
532 Center the primary caret to fit in max_width; all columns
533 will be adjusted accordingly. */
534 int max_width = m_context->caret_max_width;
535 int line_width;
536 const char *line = location_get_source_line (m_exploc.file, m_exploc.line,
537 &line_width);
538 if (line && m_exploc.column <= line_width)
540 int right_margin = CARET_LINE_MARGIN;
541 int column = m_exploc.column;
542 right_margin = MIN (line_width - column, right_margin);
543 right_margin = max_width - right_margin;
544 if (line_width >= max_width && column > right_margin)
545 m_x_offset = column - right_margin;
546 gcc_assert (m_x_offset >= 0);
550 /* Return true iff we should print a heading when starting the
551 line span with the given index. */
553 bool
554 layout::print_heading_for_line_span_index_p (int line_span_idx) const
556 /* We print a heading for every change of line span, hence for every
557 line span after the initial one. */
558 if (line_span_idx > 0)
559 return true;
561 /* We also do it for the initial span if the primary location of the
562 diagnostic is in a different span. */
563 if (m_exploc.line > (int)get_line_span (0)->m_last_line)
564 return true;
566 return false;
569 /* Get an expanded_location for the first location of interest within
570 the given line_span.
571 Used when printing a heading to indicate a new line span. */
573 expanded_location
574 layout::get_expanded_location (const line_span *line_span) const
576 /* Whenever possible, use the caret location. */
577 if (line_span->contains_line_p (m_exploc.line))
578 return m_exploc;
580 /* Otherwise, use the start of the first range that's present
581 within the line_span. */
582 for (unsigned int i = 0; i < m_layout_ranges.length (); i++)
584 const layout_range *lr = &m_layout_ranges[i];
585 if (line_span->contains_line_p (lr->m_start.m_line))
587 expanded_location exploc = m_exploc;
588 exploc.line = lr->m_start.m_line;
589 exploc.column = lr->m_start.m_column;
590 return exploc;
594 /* It should not be possible to have a line span that didn't
595 contain any of the layout_range instances. */
596 gcc_unreachable ();
597 return m_exploc;
600 /* We want to print the pertinent source code at a diagnostic. The
601 rich_location can contain multiple locations. This will have been
602 filtered into m_exploc (the caret for the primary location) and
603 m_layout_ranges, for those ranges within the same source file.
605 We will print a subset of the lines within the source file in question,
606 as a collection of "spans" of lines.
608 This function populates m_line_spans with an ordered, disjoint list of
609 the line spans of interest.
611 For example, if the primary caret location is on line 7, with ranges
612 covering lines 5-6 and lines 9-12:
615 005 |RANGE 0
616 006 |RANGE 0
617 007 |PRIMARY CARET
619 009 |RANGE 1
620 010 |RANGE 1
621 011 |RANGE 1
622 012 |RANGE 1
625 then we want two spans: lines 5-7 and lines 9-12. */
627 void
628 layout::calculate_line_spans ()
630 /* This should only be called once, by the ctor. */
631 gcc_assert (m_line_spans.length () == 0);
633 /* Populate tmp_spans with individual spans, for each of
634 m_exploc, and for m_layout_ranges. */
635 auto_vec<line_span> tmp_spans (1 + rich_location::MAX_RANGES);
636 tmp_spans.safe_push (line_span (m_exploc.line, m_exploc.line));
637 for (unsigned int i = 0; i < m_layout_ranges.length (); i++)
639 const layout_range *lr = &m_layout_ranges[i];
640 gcc_assert (lr->m_start.m_line <= lr->m_finish.m_line);
641 tmp_spans.safe_push (line_span (lr->m_start.m_line,
642 lr->m_finish.m_line));
645 /* Sort them. */
646 tmp_spans.qsort(line_span::comparator);
648 /* Now iterate through tmp_spans, copying into m_line_spans, and
649 combining where possible. */
650 gcc_assert (tmp_spans.length () > 0);
651 m_line_spans.safe_push (tmp_spans[0]);
652 for (unsigned int i = 1; i < tmp_spans.length (); i++)
654 line_span *current = &m_line_spans[m_line_spans.length () - 1];
655 const line_span *next = &tmp_spans[i];
656 gcc_assert (next->m_first_line >= current->m_first_line);
657 if (next->m_first_line <= current->m_last_line + 1)
659 /* We can merge them. */
660 if (next->m_last_line > current->m_last_line)
661 current->m_last_line = next->m_last_line;
663 else
665 /* No merger possible. */
666 m_line_spans.safe_push (*next);
670 /* Verify the result, in m_line_spans. */
671 gcc_assert (m_line_spans.length () > 0);
672 for (unsigned int i = 1; i < m_line_spans.length (); i++)
674 const line_span *prev = &m_line_spans[i - 1];
675 const line_span *next = &m_line_spans[i];
676 /* The individual spans must be sane. */
677 gcc_assert (prev->m_first_line <= prev->m_last_line);
678 gcc_assert (next->m_first_line <= next->m_last_line);
679 /* The spans must be ordered. */
680 gcc_assert (prev->m_first_line < next->m_first_line);
681 /* There must be a gap of at least one line between separate spans. */
682 gcc_assert ((prev->m_last_line + 1) < next->m_first_line);
686 /* Attempt to print line ROW of source code, potentially colorized at any
687 ranges.
688 Return true if the line was printed, populating *LBOUNDS_OUT.
689 Return false if the source line could not be read, leaving *LBOUNDS_OUT
690 untouched. */
692 bool
693 layout::print_source_line (int row, line_bounds *lbounds_out)
695 int line_width;
696 const char *line = location_get_source_line (m_exploc.file, row,
697 &line_width);
698 if (!line)
699 return false;
701 m_colorizer.set_normal_text ();
703 /* We will stop printing the source line at any trailing
704 whitespace. */
705 line_width = get_line_width_without_trailing_whitespace (line,
706 line_width);
707 line += m_x_offset;
709 pp_space (m_pp);
710 int first_non_ws = INT_MAX;
711 int last_non_ws = 0;
712 int column;
713 for (column = 1 + m_x_offset; column <= line_width; column++)
715 /* Assuming colorization is enabled for the caret and underline
716 characters, we may also colorize the associated characters
717 within the source line.
719 For frontends that generate range information, we color the
720 associated characters in the source line the same as the
721 carets and underlines in the annotation line, to make it easier
722 for the reader to see the pertinent code.
724 For frontends that only generate carets, we don't colorize the
725 characters above them, since this would look strange (e.g.
726 colorizing just the first character in a token). */
727 if (m_colorize_source_p)
729 bool in_range_p;
730 point_state state;
731 in_range_p = get_state_at_point (row, column,
732 0, INT_MAX,
733 &state);
734 if (in_range_p)
735 m_colorizer.set_range (state.range_idx);
736 else
737 m_colorizer.set_normal_text ();
739 char c = *line == '\t' ? ' ' : *line;
740 if (c == '\0')
741 c = ' ';
742 if (c != ' ')
744 last_non_ws = column;
745 if (first_non_ws == INT_MAX)
746 first_non_ws = column;
748 pp_character (m_pp, c);
749 line++;
751 print_newline ();
753 lbounds_out->m_first_non_ws = first_non_ws;
754 lbounds_out->m_last_non_ws = last_non_ws;
755 return true;
758 /* Print a line consisting of the caret/underlines for the given
759 source line. */
761 void
762 layout::print_annotation_line (int row, const line_bounds lbounds)
764 int x_bound = get_x_bound_for_row (row, m_exploc.column,
765 lbounds.m_last_non_ws);
767 pp_space (m_pp);
768 for (int column = 1 + m_x_offset; column < x_bound; column++)
770 bool in_range_p;
771 point_state state;
772 in_range_p = get_state_at_point (row, column,
773 lbounds.m_first_non_ws,
774 lbounds.m_last_non_ws,
775 &state);
776 if (in_range_p)
778 /* Within a range. Draw either the caret or an underline. */
779 m_colorizer.set_range (state.range_idx);
780 if (state.draw_caret_p)
781 /* Draw the caret. */
782 pp_character (m_pp, m_context->caret_chars[state.range_idx]);
783 else
784 pp_character (m_pp, '~');
786 else
788 /* Not in a range. */
789 m_colorizer.set_normal_text ();
790 pp_character (m_pp, ' ');
793 print_newline ();
796 /* If there are any fixit hints on source line ROW within RICHLOC, print them.
797 They are printed in order, attempting to combine them onto lines, but
798 starting new lines if necessary. */
800 void
801 layout::print_any_fixits (int row, const rich_location *richloc)
803 int column = 0;
804 for (unsigned int i = 0; i < richloc->get_num_fixit_hints (); i++)
806 fixit_hint *hint = richloc->get_fixit_hint (i);
807 if (hint->affects_line_p (m_exploc.file, row))
809 /* For now we assume each fixit hint can only touch one line. */
810 switch (hint->get_kind ())
812 case fixit_hint::INSERT:
814 fixit_insert *insert = static_cast <fixit_insert *> (hint);
815 /* This assumes the insertion just affects one line. */
816 int start_column
817 = LOCATION_COLUMN (insert->get_location ());
818 move_to_column (&column, start_column);
819 m_colorizer.set_fixit_hint ();
820 pp_string (m_pp, insert->get_string ());
821 m_colorizer.set_normal_text ();
822 column += insert->get_length ();
824 break;
826 case fixit_hint::REMOVE:
828 fixit_remove *remove = static_cast <fixit_remove *> (hint);
829 /* This assumes the removal just affects one line. */
830 source_range src_range = remove->get_range ();
831 int start_column = LOCATION_COLUMN (src_range.m_start);
832 int finish_column = LOCATION_COLUMN (src_range.m_finish);
833 move_to_column (&column, start_column);
834 for (int column = start_column; column <= finish_column; column++)
836 m_colorizer.set_fixit_hint ();
837 pp_character (m_pp, '-');
838 m_colorizer.set_normal_text ();
841 break;
843 case fixit_hint::REPLACE:
845 fixit_replace *replace = static_cast <fixit_replace *> (hint);
846 int start_column
847 = LOCATION_COLUMN (replace->get_range ().m_start);
848 move_to_column (&column, start_column);
849 m_colorizer.set_fixit_hint ();
850 pp_string (m_pp, replace->get_string ());
851 m_colorizer.set_normal_text ();
852 column += replace->get_length ();
854 break;
856 default:
857 gcc_unreachable ();
862 /* Add a trailing newline, if necessary. */
863 move_to_column (&column, 0);
866 /* Disable any colorization and emit a newline. */
868 void
869 layout::print_newline ()
871 m_colorizer.set_normal_text ();
872 pp_newline (m_pp);
875 /* Return true if (ROW/COLUMN) is within a range of the layout.
876 If it returns true, OUT_STATE is written to, with the
877 range index, and whether we should draw the caret at
878 (ROW/COLUMN) (as opposed to an underline). */
880 bool
881 layout::get_state_at_point (/* Inputs. */
882 int row, int column,
883 int first_non_ws, int last_non_ws,
884 /* Outputs. */
885 point_state *out_state)
887 layout_range *range;
888 int i;
889 FOR_EACH_VEC_ELT (m_layout_ranges, i, range)
891 if (range->contains_point (row, column))
893 out_state->range_idx = i;
895 /* Are we at the range's caret? is it visible? */
896 out_state->draw_caret_p = false;
897 if (range->m_show_caret_p
898 && row == range->m_caret.m_line
899 && column == range->m_caret.m_column)
900 out_state->draw_caret_p = true;
902 /* Within a multiline range, don't display any underline
903 in any leading or trailing whitespace on a line.
904 We do display carets, however. */
905 if (!out_state->draw_caret_p)
906 if (column < first_non_ws || column > last_non_ws)
907 return false;
909 /* We are within a range. */
910 return true;
914 return false;
917 /* Helper function for use by layout::print_line when printing the
918 annotation line under the source line.
919 Get the column beyond the rightmost one that could contain a caret or
920 range marker, given that we stop rendering at trailing whitespace.
921 ROW is the source line within the given file.
922 CARET_COLUMN is the column of range 0's caret.
923 LAST_NON_WS_COLUMN is the last column containing a non-whitespace
924 character of source (as determined when printing the source line). */
927 layout::get_x_bound_for_row (int row, int caret_column,
928 int last_non_ws_column)
930 int result = caret_column + 1;
932 layout_range *range;
933 int i;
934 FOR_EACH_VEC_ELT (m_layout_ranges, i, range)
936 if (row >= range->m_start.m_line)
938 if (range->m_finish.m_line == row)
940 /* On the final line within a range; ensure that
941 we render up to the end of the range. */
942 if (result <= range->m_finish.m_column)
943 result = range->m_finish.m_column + 1;
945 else if (row < range->m_finish.m_line)
947 /* Within a multiline range; ensure that we render up to the
948 last non-whitespace column. */
949 if (result <= last_non_ws_column)
950 result = last_non_ws_column + 1;
955 return result;
958 /* Given *COLUMN as an x-coordinate, print spaces to position
959 successive output at DEST_COLUMN, printing a newline if necessary,
960 and updating *COLUMN. */
962 void
963 layout::move_to_column (int *column, int dest_column)
965 /* Start a new line if we need to. */
966 if (*column > dest_column)
968 print_newline ();
969 *column = 0;
972 while (*column < dest_column)
974 pp_space (m_pp);
975 (*column)++;
979 } /* End of anonymous namespace. */
981 /* Print the physical source code corresponding to the location of
982 this diagnostic, with additional annotations. */
984 void
985 diagnostic_show_locus (diagnostic_context * context,
986 const diagnostic_info *diagnostic)
988 pp_newline (context->printer);
990 if (!context->show_caret
991 || diagnostic_location (diagnostic, 0) <= BUILTINS_LOCATION
992 || diagnostic_location (diagnostic, 0) == context->last_location)
993 return;
995 context->last_location = diagnostic_location (diagnostic, 0);
997 const char *saved_prefix = pp_get_prefix (context->printer);
998 pp_set_prefix (context->printer, NULL);
1000 layout layout (context, diagnostic);
1001 for (int line_span_idx = 0; line_span_idx < layout.get_num_line_spans ();
1002 line_span_idx++)
1004 const line_span *line_span = layout.get_line_span (line_span_idx);
1005 if (layout.print_heading_for_line_span_index_p (line_span_idx))
1007 expanded_location exploc = layout.get_expanded_location (line_span);
1008 context->start_span (context, exploc);
1010 int last_line = line_span->get_last_line ();
1011 for (int row = line_span->get_first_line (); row <= last_line; row++)
1013 /* Print the source line, followed by an annotation line
1014 consisting of any caret/underlines, then any fixits.
1015 If the source line can't be read, print nothing. */
1016 line_bounds lbounds;
1017 if (layout.print_source_line (row, &lbounds))
1019 layout.print_annotation_line (row, lbounds);
1020 layout.print_any_fixits (row, diagnostic->richloc);
1025 pp_set_prefix (context->printer, saved_prefix);