PR other/69554: avoid excessive source printing for widely-separated locations
[gcc.git] / gcc / diagnostic-show-locus.c
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>
4
5 This file is part of GCC.
6
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 3, or (at your option) any later
10 version.
11
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
20
21 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "version.h"
25 #include "demangle.h"
26 #include "intl.h"
27 #include "backtrace.h"
28 #include "diagnostic.h"
29 #include "diagnostic-color.h"
30
31 #ifdef HAVE_TERMIOS_H
32 # include <termios.h>
33 #endif
34
35 #ifdef GWINSZ_IN_SYS_IOCTL
36 # include <sys/ioctl.h>
37 #endif
38
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. */
43
44 namespace {
45
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. */
49
50 struct point_state
51 {
52 int range_idx;
53 bool draw_caret_p;
54 };
55
56 /* A class to inject colorization codes when printing the diagnostic locus.
57
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
63
64 The class caches the lookup of the color codes for the above.
65
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. */
71
72 class colorizer
73 {
74 public:
75 colorizer (diagnostic_context *context,
76 const diagnostic_info *diagnostic);
77 ~colorizer ();
78
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); }
82
83 private:
84 void set_state (int state);
85 void begin_state (int state);
86 void finish_state (int state);
87
88 private:
89 static const int STATE_NORMAL_TEXT = -1;
90
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;
99 };
100
101 /* A point within a layout_range; similar to an expanded_location,
102 but after filtering on file. */
103
104 class layout_point
105 {
106 public:
107 layout_point (const expanded_location &exploc)
108 : m_line (exploc.line),
109 m_column (exploc.column) {}
110
111 int m_line;
112 int m_column;
113 };
114
115 /* A class for use by "class layout" below: a filtered location_range. */
116
117 class layout_range
118 {
119 public:
120 layout_range (const location_range *loc_range);
121
122 bool contains_point (int row, int column) const;
123
124 layout_point m_start;
125 layout_point m_finish;
126 bool m_show_caret_p;
127 layout_point m_caret;
128 };
129
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. */
133
134 struct line_bounds
135 {
136 int m_first_non_ws;
137 int m_last_non_ws;
138 };
139
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). */
144
145 struct line_span
146 {
147 line_span (linenum_type first_line, linenum_type last_line)
148 : m_first_line (first_line), m_last_line (last_line)
149 {
150 gcc_assert (first_line <= last_line);
151 }
152 linenum_type get_first_line () const { return m_first_line; }
153 linenum_type get_last_line () const { return m_last_line; }
154
155 bool contains_line_p (linenum_type line) const
156 {
157 return line >= m_first_line && line <= m_last_line;
158 }
159
160 static int comparator (const void *p1, const void *p2)
161 {
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;
168 }
169
170 linenum_type m_first_line;
171 linenum_type m_last_line;
172 };
173
174 /* A class to control the overall layout when printing a diagnostic.
175
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.
179
180 We assume we have disjoint ranges. */
181
182 class layout
183 {
184 public:
185 layout (diagnostic_context *context,
186 const diagnostic_info *diagnostic);
187
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]; }
190
191 bool print_heading_for_line_span_index_p (int line_span_idx) const;
192
193 expanded_location get_expanded_location (const line_span *) const;
194
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);
198
199 private:
200 void calculate_line_spans ();
201
202 void print_newline ();
203
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);
210
211 int
212 get_x_bound_for_row (int row, int caret_column,
213 int last_non_ws);
214
215 void
216 move_to_column (int *column, int dest_column);
217
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;
228 };
229
230 /* Implementation of "class colorizer". */
231
232 /* The constructor for "colorizer". Lookup and store color codes for the
233 different kinds of things we might need to print. */
234
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)
240 {
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));
245 }
246
247 /* The destructor for "colorize". If colorization is on, print a code to
248 turn it off. */
249
250 colorizer::~colorizer ()
251 {
252 finish_state (m_current_state);
253 }
254
255 /* Update state, printing color codes if necessary if there's a state
256 change. */
257
258 void
259 colorizer::set_state (int new_state)
260 {
261 if (m_current_state != new_state)
262 {
263 finish_state (m_current_state);
264 m_current_state = new_state;
265 begin_state (new_state);
266 }
267 }
268
269 /* Turn on any colorization for STATE. */
270
271 void
272 colorizer::begin_state (int state)
273 {
274 switch (state)
275 {
276 case STATE_NORMAL_TEXT:
277 break;
278
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;
287
288 case 1:
289 pp_string (m_context->printer, m_range1_cs);
290 break;
291
292 case 2:
293 pp_string (m_context->printer, m_range2_cs);
294 break;
295
296 default:
297 /* We don't expect more than 3 ranges per diagnostic. */
298 gcc_unreachable ();
299 break;
300 }
301 }
302
303 /* Turn off any colorization for STATE. */
304
305 void
306 colorizer::finish_state (int state)
307 {
308 switch (state)
309 {
310 case STATE_NORMAL_TEXT:
311 break;
312
313 case 0:
314 pp_string (m_context->printer, m_caret_ce);
315 break;
316
317 default:
318 /* Within a range. */
319 gcc_assert (state > 0);
320 pp_string (m_context->printer, m_range_ce);
321 break;
322 }
323 }
324
325 /* Implementation of class layout_range. */
326
327 /* The constructor for class layout_range.
328 Initialize various layout_point fields from expanded_location
329 equivalents; we've already filtered on file. */
330
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)
336 {
337 }
338
339 /* Is (column, row) within the given range?
340 We've already filtered on the file.
341
342 Ranges are closed (both limits are within the range).
343
344 Example A: a single-line range:
345 start: (col=22, line=2)
346 finish: (col=38, line=2)
347
348 |00000011111111112222222222333333333344444444444
349 |34567890123456789012345678901234567890123456789
350 --+-----------------------------------------------
351 01|bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb
352 02|bbbbbbbbbbbbbbbbbbbSwwwwwwwwwwwwwwwFaaaaaaaaaaa
353 03|aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
354
355 Example B: a multiline range with
356 start: (col=14, line=3)
357 finish: (col=08, line=5)
358
359 |00000011111111112222222222333333333344444444444
360 |34567890123456789012345678901234567890123456789
361 --+-----------------------------------------------
362 01|bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb
363 02|bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb
364 03|bbbbbbbbbbbSwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwww
365 04|wwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwww
366 05|wwwwwFaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
367 06|aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
368 --+-----------------------------------------------
369
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. */
377
378 bool
379 layout_range::contains_point (int row, int column) const
380 {
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. */
384
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;
390
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). */
394 {
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;
399
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
406 {
407 /* This is a single-line range. */
408 gcc_assert (row == m_finish.m_line);
409 return column <= m_finish.m_column;
410 }
411 }
412
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);
417
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;
423
424 if (row < m_finish.m_line)
425 {
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;
430 }
431
432 gcc_assert (row == m_finish.m_line);
433
434 return column <= m_finish.m_column;
435 }
436
437 /* Given a source line LINE of length LINE_WIDTH, determine the width
438 without any trailing whitespace. */
439
440 static int
441 get_line_width_without_trailing_whitespace (const char *line, int line_width)
442 {
443 int result = line_width;
444 while (result > 0)
445 {
446 char ch = line[result - 1];
447 if (ch == ' ' || ch == '\t')
448 result--;
449 else
450 break;
451 }
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;
458 }
459
460 /* Implementation of class layout. */
461
462 /* Constructor for class layout.
463
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. */
470
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)
482 {
483 rich_location *richloc = diagnostic->richloc;
484 for (unsigned int idx = 0; idx < richloc->get_num_locations (); idx++)
485 {
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);
489
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;
499
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);
503
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)
509 {
510 /* Is this the primary location? */
511 if (m_layout_ranges.length () == 0)
512 {
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;
517 }
518 else
519 /* This is a non-primary range; ignore it. */
520 continue;
521 }
522
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);
526 }
527
528 /* Populate m_line_spans. */
529 calculate_line_spans ();
530
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)
539 {
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);
547 }
548 }
549
550 /* Return true iff we should print a heading when starting the
551 line span with the given index. */
552
553 bool
554 layout::print_heading_for_line_span_index_p (int line_span_idx) const
555 {
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;
560
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;
565
566 return false;
567 }
568
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. */
572
573 expanded_location
574 layout::get_expanded_location (const line_span *line_span) const
575 {
576 /* Whenever possible, use the caret location. */
577 if (line_span->contains_line_p (m_exploc.line))
578 return m_exploc;
579
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++)
583 {
584 const layout_range *lr = &m_layout_ranges[i];
585 if (line_span->contains_line_p (lr->m_start.m_line))
586 {
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;
591 }
592 }
593
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;
598 }
599
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.
604
605 We will print a subset of the lines within the source file in question,
606 as a collection of "spans" of lines.
607
608 This function populates m_line_spans with an ordered, disjoint list of
609 the line spans of interest.
610
611 For example, if the primary caret location is on line 7, with ranges
612 covering lines 5-6 and lines 9-12:
613
614 004
615 005 |RANGE 0
616 006 |RANGE 0
617 007 |PRIMARY CARET
618 008
619 009 |RANGE 1
620 010 |RANGE 1
621 011 |RANGE 1
622 012 |RANGE 1
623 013
624
625 then we want two spans: lines 5-7 and lines 9-12. */
626
627 void
628 layout::calculate_line_spans ()
629 {
630 /* This should only be called once, by the ctor. */
631 gcc_assert (m_line_spans.length () == 0);
632
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++)
638 {
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));
643 }
644
645 /* Sort them. */
646 tmp_spans.qsort(line_span::comparator);
647
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++)
653 {
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)
658 {
659 /* We can merge them. */
660 if (next->m_last_line > current->m_last_line)
661 current->m_last_line = next->m_last_line;
662 }
663 else
664 {
665 /* No merger possible. */
666 m_line_spans.safe_push (*next);
667 }
668 }
669
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++)
673 {
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);
683 }
684 }
685
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. */
691
692 bool
693 layout::print_source_line (int row, line_bounds *lbounds_out)
694 {
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;
700
701 m_colorizer.set_normal_text ();
702
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;
708
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++)
714 {
715 /* Assuming colorization is enabled for the caret and underline
716 characters, we may also colorize the associated characters
717 within the source line.
718
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.
723
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)
728 {
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 ();
738 }
739 char c = *line == '\t' ? ' ' : *line;
740 if (c == '\0')
741 c = ' ';
742 if (c != ' ')
743 {
744 last_non_ws = column;
745 if (first_non_ws == INT_MAX)
746 first_non_ws = column;
747 }
748 pp_character (m_pp, c);
749 line++;
750 }
751 print_newline ();
752
753 lbounds_out->m_first_non_ws = first_non_ws;
754 lbounds_out->m_last_non_ws = last_non_ws;
755 return true;
756 }
757
758 /* Print a line consisting of the caret/underlines for the given
759 source line. */
760
761 void
762 layout::print_annotation_line (int row, const line_bounds lbounds)
763 {
764 int x_bound = get_x_bound_for_row (row, m_exploc.column,
765 lbounds.m_last_non_ws);
766
767 pp_space (m_pp);
768 for (int column = 1 + m_x_offset; column < x_bound; column++)
769 {
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)
777 {
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, '~');
785 }
786 else
787 {
788 /* Not in a range. */
789 m_colorizer.set_normal_text ();
790 pp_character (m_pp, ' ');
791 }
792 }
793 print_newline ();
794 }
795
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. */
799
800 void
801 layout::print_any_fixits (int row, const rich_location *richloc)
802 {
803 int column = 0;
804 for (unsigned int i = 0; i < richloc->get_num_fixit_hints (); i++)
805 {
806 fixit_hint *hint = richloc->get_fixit_hint (i);
807 if (hint->affects_line_p (m_exploc.file, row))
808 {
809 /* For now we assume each fixit hint can only touch one line. */
810 switch (hint->get_kind ())
811 {
812 case fixit_hint::INSERT:
813 {
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 ();
823 }
824 break;
825
826 case fixit_hint::REMOVE:
827 {
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++)
835 {
836 m_colorizer.set_fixit_hint ();
837 pp_character (m_pp, '-');
838 m_colorizer.set_normal_text ();
839 }
840 }
841 break;
842
843 case fixit_hint::REPLACE:
844 {
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 ();
853 }
854 break;
855
856 default:
857 gcc_unreachable ();
858 }
859 }
860 }
861
862 /* Add a trailing newline, if necessary. */
863 move_to_column (&column, 0);
864 }
865
866 /* Disable any colorization and emit a newline. */
867
868 void
869 layout::print_newline ()
870 {
871 m_colorizer.set_normal_text ();
872 pp_newline (m_pp);
873 }
874
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). */
879
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)
886 {
887 layout_range *range;
888 int i;
889 FOR_EACH_VEC_ELT (m_layout_ranges, i, range)
890 {
891 if (range->contains_point (row, column))
892 {
893 out_state->range_idx = i;
894
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;
901
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;
908
909 /* We are within a range. */
910 return true;
911 }
912 }
913
914 return false;
915 }
916
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). */
925
926 int
927 layout::get_x_bound_for_row (int row, int caret_column,
928 int last_non_ws_column)
929 {
930 int result = caret_column + 1;
931
932 layout_range *range;
933 int i;
934 FOR_EACH_VEC_ELT (m_layout_ranges, i, range)
935 {
936 if (row >= range->m_start.m_line)
937 {
938 if (range->m_finish.m_line == row)
939 {
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;
944 }
945 else if (row < range->m_finish.m_line)
946 {
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;
951 }
952 }
953 }
954
955 return result;
956 }
957
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. */
961
962 void
963 layout::move_to_column (int *column, int dest_column)
964 {
965 /* Start a new line if we need to. */
966 if (*column > dest_column)
967 {
968 print_newline ();
969 *column = 0;
970 }
971
972 while (*column < dest_column)
973 {
974 pp_space (m_pp);
975 (*column)++;
976 }
977 }
978
979 } /* End of anonymous namespace. */
980
981 /* Print the physical source code corresponding to the location of
982 this diagnostic, with additional annotations. */
983
984 void
985 diagnostic_show_locus (diagnostic_context * context,
986 const diagnostic_info *diagnostic)
987 {
988 pp_newline (context->printer);
989
990 if (!context->show_caret
991 || diagnostic_location (diagnostic, 0) <= BUILTINS_LOCATION
992 || diagnostic_location (diagnostic, 0) == context->last_location)
993 return;
994
995 context->last_location = diagnostic_location (diagnostic, 0);
996
997 const char *saved_prefix = pp_get_prefix (context->printer);
998 pp_set_prefix (context->printer, NULL);
999
1000 layout layout (context, diagnostic);
1001 for (int line_span_idx = 0; line_span_idx < layout.get_num_line_spans ();
1002 line_span_idx++)
1003 {
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))
1006 {
1007 expanded_location exploc = layout.get_expanded_location (line_span);
1008 context->start_span (context, exploc);
1009 }
1010 int last_line = line_span->get_last_line ();
1011 for (int row = line_span->get_first_line (); row <= last_line; row++)
1012 {
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))
1018 {
1019 layout.print_annotation_line (row, lbounds);
1020 layout.print_any_fixits (row, diagnostic->richloc);
1021 }
1022 }
1023 }
1024
1025 pp_set_prefix (context->printer, saved_prefix);
1026 }