LCOV - code coverage report
Current view: top level - gcc - diagnostic-show-locus.c (source / functions) Hit Total Coverage
Test: gcc.info Lines: 1960 1977 99.1 %
Date: 2020-04-04 11:58:09 Functions: 93 101 92.1 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 0 0 -

           Branch data     Line data    Source code
       1                 :            : /* Diagnostic subroutines for printing source-code
       2                 :            :    Copyright (C) 1999-2020 Free Software Foundation, Inc.
       3                 :            :    Contributed by Gabriel Dos Reis <gdr@codesourcery.com>
       4                 :            : 
       5                 :            : This file is part of GCC.
       6                 :            : 
       7                 :            : GCC is free software; you can redistribute it and/or modify it under
       8                 :            : the terms of the GNU General Public License as published by the Free
       9                 :            : Software Foundation; either version 3, or (at your option) any later
      10                 :            : version.
      11                 :            : 
      12                 :            : GCC is distributed in the hope that it will be useful, but WITHOUT ANY
      13                 :            : WARRANTY; without even the implied warranty of MERCHANTABILITY or
      14                 :            : FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
      15                 :            : for more details.
      16                 :            : 
      17                 :            : You should have received a copy of the GNU General Public License
      18                 :            : along with GCC; see the file COPYING3.  If not see
      19                 :            : <http://www.gnu.org/licenses/>.  */
      20                 :            : 
      21                 :            : #include "config.h"
      22                 :            : #include "system.h"
      23                 :            : #include "coretypes.h"
      24                 :            : #include "version.h"
      25                 :            : #include "demangle.h"
      26                 :            : #include "intl.h"
      27                 :            : #include "backtrace.h"
      28                 :            : #include "diagnostic.h"
      29                 :            : #include "diagnostic-color.h"
      30                 :            : #include "gcc-rich-location.h"
      31                 :            : #include "selftest.h"
      32                 :            : #include "selftest-diagnostic.h"
      33                 :            : #include "cpplib.h"
      34                 :            : 
      35                 :            : #ifdef HAVE_TERMIOS_H
      36                 :            : # include <termios.h>
      37                 :            : #endif
      38                 :            : 
      39                 :            : #ifdef GWINSZ_IN_SYS_IOCTL
      40                 :            : # include <sys/ioctl.h>
      41                 :            : #endif
      42                 :            : 
      43                 :            : /* Disable warnings about quoting issues in the pp_xxx calls below
      44                 :            :    that (intentionally) don't follow GCC diagnostic conventions.  */
      45                 :            : #if __GNUC__ >= 10
      46                 :            : #  pragma GCC diagnostic push
      47                 :            : #  pragma GCC diagnostic ignored "-Wformat-diag"
      48                 :            : #endif
      49                 :            : 
      50                 :            : /* Classes for rendering source code and diagnostics, within an
      51                 :            :    anonymous namespace.
      52                 :            :    The work is done by "class layout", which embeds and uses
      53                 :            :    "class colorizer" and "class layout_range" to get things done.  */
      54                 :            : 
      55                 :            : namespace {
      56                 :            : 
      57                 :            : /* The state at a given point of the source code, assuming that we're
      58                 :            :    in a range: which range are we in, and whether we should draw a caret at
      59                 :            :    this point.  */
      60                 :            : 
      61                 :            : struct point_state
      62                 :            : {
      63                 :            :   int range_idx;
      64                 :            :   bool draw_caret_p;
      65                 :            : };
      66                 :            : 
      67                 :            : /* A class to inject colorization codes when printing the diagnostic locus.
      68                 :            : 
      69                 :            :    It has one kind of colorization for each of:
      70                 :            :      - normal text
      71                 :            :      - range 0 (the "primary location")
      72                 :            :      - range 1
      73                 :            :      - range 2
      74                 :            : 
      75                 :            :    The class caches the lookup of the color codes for the above.
      76                 :            : 
      77                 :            :    The class also has responsibility for tracking which of the above is
      78                 :            :    active, filtering out unnecessary changes.  This allows
      79                 :            :    layout::print_source_line and layout::print_annotation_line
      80                 :            :    to simply request a colorization code for *every* character they print,
      81                 :            :    via this class, and have the filtering be done for them here.  */
      82                 :            : 
      83                 :            : class colorizer
      84                 :            : {
      85                 :            :  public:
      86                 :            :   colorizer (diagnostic_context *context,
      87                 :            :              diagnostic_t diagnostic_kind);
      88                 :            :   ~colorizer ();
      89                 :            : 
      90                 :     126669 :   void set_range (int range_idx)
      91                 :            :   {
      92                 :            :     /* Normally we emphasize the primary location, then alternate between
      93                 :            :        two colors for the secondary locations.
      94                 :            :        But if we're printing a run of events in a diagnostic path, that
      95                 :            :        makes no sense, so print all of them with the same colorization.  */
      96                 :     126669 :     if (m_diagnostic_kind == DK_DIAGNOSTIC_PATH)
      97                 :       7002 :       set_state (0);
      98                 :            :     else
      99                 :     119667 :       set_state (range_idx);
     100                 :     126669 :   }
     101                 :   16880500 :   void set_normal_text () { set_state (STATE_NORMAL_TEXT); }
     102                 :      14334 :   void set_fixit_insert () { set_state (STATE_FIXIT_INSERT); }
     103                 :       6299 :   void set_fixit_delete () { set_state (STATE_FIXIT_DELETE); }
     104                 :            : 
     105                 :            :  private:
     106                 :            :   void set_state (int state);
     107                 :            :   void begin_state (int state);
     108                 :            :   void finish_state (int state);
     109                 :            :   const char *get_color_by_name (const char *);
     110                 :            : 
     111                 :            :  private:
     112                 :            :   static const int STATE_NORMAL_TEXT = -1;
     113                 :            :   static const int STATE_FIXIT_INSERT  = -2;
     114                 :            :   static const int STATE_FIXIT_DELETE  = -3;
     115                 :            : 
     116                 :            :   diagnostic_context *m_context;
     117                 :            :   diagnostic_t m_diagnostic_kind;
     118                 :            :   int m_current_state;
     119                 :            :   const char *m_range1;
     120                 :            :   const char *m_range2;
     121                 :            :   const char *m_fixit_insert;
     122                 :            :   const char *m_fixit_delete;
     123                 :            :   const char *m_stop_color;
     124                 :            : };
     125                 :            : 
     126                 :            : /* In order to handle multibyte sources properly, all of this logic needs to be
     127                 :            :    aware of the distinction between the number of bytes and the number of
     128                 :            :    display columns occupied by a character, which are not the same for non-ASCII
     129                 :            :    characters.  For example, the Unicode pi symbol, U+03C0, is encoded in UTF-8
     130                 :            :    as "\xcf\x80", and thus occupies 2 bytes of space while only occupying 1
     131                 :            :    display column when it is output.  A typical emoji, such as U+1F602 (in
     132                 :            :    UTF-8, "\xf0\x9f\x98\x82"), requires 4 bytes and has a display width of 2.
     133                 :            : 
     134                 :            :    The below example line, which is also used for selftests below, shows how the
     135                 :            :    display column and byte column are related:
     136                 :            : 
     137                 :            :      0000000001111111111222222   display
     138                 :            :      1234567890123456789012345   columns
     139                 :            :      SS_foo = P_bar.SS_fieldP;
     140                 :            :      0000000111111111222222223   byte
     141                 :            :      1356789012456789134567891   columns
     142                 :            : 
     143                 :            :    Here SS represents the two display columns for the U+1F602 emoji, and P
     144                 :            :    represents the one display column for the U+03C0 pi symbol.  As an example, a
     145                 :            :    diagnostic pointing to the final P on this line is at byte column 29 and
     146                 :            :    display column 24.  This reflects the fact that the three extended characters
     147                 :            :    before the final P occupy cumulatively 5 more bytes than they do display
     148                 :            :    columns (a difference of 2 for each of the two SSs, and one for the other P).
     149                 :            : 
     150                 :            :    One or the other of the two column units is more useful depending on the
     151                 :            :    context.  For instance, in order to output the caret at the correct location,
     152                 :            :    we need to count display columns; in order to colorize a source line, we need
     153                 :            :    to count the bytes.  All locations are provided to us as byte counts, which
     154                 :            :    we augment with the display column on demand so that it can be used when
     155                 :            :    needed.  This is not the most efficient way to do things since it requires
     156                 :            :    looping over the whole line each time, but it should be fine for the purpose
     157                 :            :    of outputting diagnostics.
     158                 :            : 
     159                 :            :    In order to keep straight which units (byte or display) are in use at a
     160                 :            :    given time, the following enum lets us specify that explicitly.  */
     161                 :            : 
     162                 :            : enum column_unit {
     163                 :            :   /* Measured in raw bytes.  */
     164                 :            :   CU_BYTES = 0,
     165                 :            : 
     166                 :            :   /* Measured in display units.  */
     167                 :            :   CU_DISPLAY_COLS,
     168                 :            : 
     169                 :            :   /* For arrays indexed by column_unit.  */
     170                 :            :   CU_NUM_UNITS
     171                 :            : };
     172                 :            : 
     173                 :            : /* Utility class to augment an exploc with the corresponding display column.  */
     174                 :            : 
     175                 :            : class exploc_with_display_col : public expanded_location
     176                 :            : {
     177                 :            :  public:
     178                 :      97709 :   exploc_with_display_col (const expanded_location &exploc)
     179                 :      97709 :     : expanded_location (exploc),
     180                 :     195418 :       m_display_col (location_compute_display_column (exploc)) {}
     181                 :            : 
     182                 :            :   int m_display_col;
     183                 :            : };
     184                 :            : 
     185                 :            : 
     186                 :            : /* A point within a layout_range; similar to an exploc_with_display_col,
     187                 :            :    but after filtering on file.  */
     188                 :            : 
     189                 :            : class layout_point
     190                 :            : {
     191                 :            :  public:
     192                 :     309483 :   layout_point (const expanded_location &exploc)
     193                 :     309483 :     : m_line (exploc.line)
     194                 :            :   {
     195                 :     309483 :     m_columns[CU_BYTES] = exploc.column;
     196                 :     412644 :     m_columns[CU_DISPLAY_COLS] = location_compute_display_column (exploc);
     197                 :            :   }
     198                 :            : 
     199                 :            :   linenum_type m_line;
     200                 :            :   int m_columns[CU_NUM_UNITS];
     201                 :            : };
     202                 :            : 
     203                 :            : /* A class for use by "class layout" below: a filtered location_range.  */
     204                 :            : 
     205                 :            : class layout_range
     206                 :            : {
     207                 :            :  public:
     208                 :            :   layout_range (const expanded_location *start_exploc,
     209                 :            :                 const expanded_location *finish_exploc,
     210                 :            :                 enum range_display_kind range_display_kind,
     211                 :            :                 const expanded_location *caret_exploc,
     212                 :            :                 unsigned original_idx,
     213                 :            :                 const range_label *label);
     214                 :            : 
     215                 :            :   bool contains_point (linenum_type row, int column,
     216                 :            :                        enum column_unit col_unit) const;
     217                 :            :   bool intersects_line_p (linenum_type row) const;
     218                 :            : 
     219                 :            :   layout_point m_start;
     220                 :            :   layout_point m_finish;
     221                 :            :   enum range_display_kind m_range_display_kind;
     222                 :            :   layout_point m_caret;
     223                 :            :   unsigned m_original_idx;
     224                 :            :   const range_label *m_label;
     225                 :            : };
     226                 :            : 
     227                 :            : /* A struct for use by layout::print_source_line for telling
     228                 :            :    layout::print_annotation_line the extents of the source line that
     229                 :            :    it printed, so that underlines can be clipped appropriately.  */
     230                 :            : 
     231                 :            : struct line_bounds
     232                 :            : {
     233                 :            :   int m_first_non_ws;
     234                 :            :   int m_last_non_ws;
     235                 :            : 
     236                 :            :   void convert_to_display_cols (char_span line)
     237                 :            :   {
     238                 :            :     m_first_non_ws = cpp_byte_column_to_display_column (line.get_buffer (),
     239                 :            :                                                         line.length (),
     240                 :            :                                                         m_first_non_ws);
     241                 :            : 
     242                 :            :     m_last_non_ws = cpp_byte_column_to_display_column (line.get_buffer (),
     243                 :            :                                                        line.length (),
     244                 :            :                                                        m_last_non_ws);
     245                 :            :   }
     246                 :            : };
     247                 :            : 
     248                 :            : /* A range of contiguous source lines within a layout (e.g. "lines 5-10"
     249                 :            :    or "line 23").  During the layout ctor, layout::calculate_line_spans
     250                 :            :    splits the pertinent source lines into a list of disjoint line_span
     251                 :            :    instances (e.g. lines 5-10, lines 15-20, line 23).  */
     252                 :            : 
     253                 :            : class line_span
     254                 :            : {
     255                 :            : public:
     256                 :     219575 :   line_span (linenum_type first_line, linenum_type last_line)
     257                 :     199375 :     : m_first_line (first_line), m_last_line (last_line)
     258                 :            :   {
     259                 :          0 :     gcc_assert (first_line <= last_line);
     260                 :            :   }
     261                 :      95567 :   linenum_type get_first_line () const { return m_first_line; }
     262                 :      95567 :   linenum_type get_last_line () const { return m_last_line; }
     263                 :            : 
     264                 :       2679 :   bool contains_line_p (linenum_type line) const
     265                 :            :   {
     266                 :       2486 :     return line >= m_first_line && line <= m_last_line;
     267                 :            :   }
     268                 :            : 
     269                 :     467455 :   static int comparator (const void *p1, const void *p2)
     270                 :            :   {
     271                 :     467455 :     const line_span *ls1 = (const line_span *)p1;
     272                 :     467455 :     const line_span *ls2 = (const line_span *)p2;
     273                 :     467445 :     int first_line_cmp = compare (ls1->m_first_line, ls2->m_first_line);
     274                 :       5361 :     if (first_line_cmp)
     275                 :      11624 :       return first_line_cmp;
     276                 :     455825 :     return compare (ls1->m_last_line, ls2->m_last_line);
     277                 :            :   }
     278                 :            : 
     279                 :            :   linenum_type m_first_line;
     280                 :            :   linenum_type m_last_line;
     281                 :            : };
     282                 :            : 
     283                 :            : #if CHECKING_P
     284                 :            : 
     285                 :            : /* Selftests for line_span.  */
     286                 :            : 
     287                 :            : static void
     288                 :          2 : test_line_span ()
     289                 :            : {
     290                 :          2 :   line_span line_one (1, 1);
     291                 :          2 :   ASSERT_EQ (1, line_one.get_first_line ());
     292                 :          2 :   ASSERT_EQ (1, line_one.get_last_line ());
     293                 :          2 :   ASSERT_FALSE (line_one.contains_line_p (0));
     294                 :          2 :   ASSERT_TRUE (line_one.contains_line_p (1));
     295                 :          2 :   ASSERT_FALSE (line_one.contains_line_p (2));
     296                 :            : 
     297                 :          2 :   line_span lines_1_to_3 (1, 3);
     298                 :          2 :   ASSERT_EQ (1, lines_1_to_3.get_first_line ());
     299                 :          2 :   ASSERT_EQ (3, lines_1_to_3.get_last_line ());
     300                 :          2 :   ASSERT_TRUE (lines_1_to_3.contains_line_p (1));
     301                 :          2 :   ASSERT_TRUE (lines_1_to_3.contains_line_p (3));
     302                 :            : 
     303                 :          2 :   ASSERT_EQ (0, line_span::comparator (&line_one, &line_one));
     304                 :          2 :   ASSERT_GT (line_span::comparator (&lines_1_to_3, &line_one), 0);
     305                 :          2 :   ASSERT_LT (line_span::comparator (&line_one, &lines_1_to_3), 0);
     306                 :            : 
     307                 :            :   /* A linenum > 2^31.  */
     308                 :          2 :   const linenum_type LARGEST_LINE = 0xffffffff;
     309                 :          2 :   line_span largest_line (LARGEST_LINE, LARGEST_LINE);
     310                 :          2 :   ASSERT_EQ (LARGEST_LINE, largest_line.get_first_line ());
     311                 :          2 :   ASSERT_EQ (LARGEST_LINE, largest_line.get_last_line ());
     312                 :            : 
     313                 :          2 :   ASSERT_GT (line_span::comparator (&largest_line, &line_one), 0);
     314                 :          2 :   ASSERT_LT (line_span::comparator (&line_one, &largest_line), 0);
     315                 :          2 : }
     316                 :            : 
     317                 :            : #endif /* #if CHECKING_P */
     318                 :            : 
     319                 :            : /* A class to control the overall layout when printing a diagnostic.
     320                 :            : 
     321                 :            :    The layout is determined within the constructor.
     322                 :            :    It is then printed by repeatedly calling the "print_source_line",
     323                 :            :    "print_annotation_line" and "print_any_fixits" methods.
     324                 :            : 
     325                 :            :    We assume we have disjoint ranges.  */
     326                 :            : 
     327                 :            : class layout
     328                 :            : {
     329                 :            :  public:
     330                 :            :   layout (diagnostic_context *context,
     331                 :            :           rich_location *richloc,
     332                 :            :           diagnostic_t diagnostic_kind);
     333                 :            : 
     334                 :            :   bool maybe_add_location_range (const location_range *loc_range,
     335                 :            :                                  unsigned original_idx,
     336                 :            :                                  bool restrict_to_current_line_spans);
     337                 :            : 
     338                 :     387172 :   int get_num_line_spans () const { return m_line_spans.length (); }
     339                 :     348728 :   const line_span *get_line_span (int idx) const { return &m_line_spans[idx]; }
     340                 :            : 
     341                 :        736 :   int get_linenum_width () const { return m_linenum_width; }
     342                 :        736 :   int get_x_offset_display () const { return m_x_offset_display; }
     343                 :            : 
     344                 :            :   void print_gap_in_line_numbering ();
     345                 :            :   bool print_heading_for_line_span_index_p (int line_span_idx) const;
     346                 :            : 
     347                 :            :   expanded_location get_expanded_location (const line_span *) const;
     348                 :            : 
     349                 :            :   void print_line (linenum_type row);
     350                 :            : 
     351                 :            :  private:
     352                 :            :   bool will_show_line_p (linenum_type row) const;
     353                 :            :   void print_leading_fixits (linenum_type row);
     354                 :            :   void print_source_line (linenum_type row, const char *line, int line_bytes,
     355                 :            :                           line_bounds *lbounds_out);
     356                 :            :   bool should_print_annotation_line_p (linenum_type row) const;
     357                 :            :   void start_annotation_line (char margin_char = ' ') const;
     358                 :            :   void print_annotation_line (linenum_type row, const line_bounds lbounds);
     359                 :            :   void print_any_labels (linenum_type row);
     360                 :            :   void print_trailing_fixits (linenum_type row);
     361                 :            : 
     362                 :            :   bool annotation_line_showed_range_p (linenum_type line, int start_column,
     363                 :            :                                        int finish_column) const;
     364                 :            :   void show_ruler (int max_column) const;
     365                 :            : 
     366                 :            :   bool validate_fixit_hint_p (const fixit_hint *hint);
     367                 :            : 
     368                 :            :   void calculate_line_spans ();
     369                 :            :   void calculate_linenum_width ();
     370                 :            :   void calculate_x_offset_display ();
     371                 :            : 
     372                 :            :   void print_newline ();
     373                 :            : 
     374                 :            :   bool
     375                 :            :   get_state_at_point (/* Inputs.  */
     376                 :            :                       linenum_type row, int column,
     377                 :            :                       int first_non_ws, int last_non_ws,
     378                 :            :                       enum column_unit col_unit,
     379                 :            :                       /* Outputs.  */
     380                 :            :                       point_state *out_state);
     381                 :            : 
     382                 :            :   int
     383                 :            :   get_x_bound_for_row (linenum_type row, int caret_column,
     384                 :            :                        int last_non_ws);
     385                 :            : 
     386                 :            :   void
     387                 :            :   move_to_column (int *column, int dest_column, bool add_left_margin);
     388                 :            : 
     389                 :            :  private:
     390                 :            :   diagnostic_context *m_context;
     391                 :            :   pretty_printer *m_pp;
     392                 :            :   location_t m_primary_loc;
     393                 :            :   exploc_with_display_col m_exploc;
     394                 :            :   colorizer m_colorizer;
     395                 :            :   bool m_colorize_source_p;
     396                 :            :   bool m_show_labels_p;
     397                 :            :   bool m_show_line_numbers_p;
     398                 :            :   bool m_diagnostic_path_p;
     399                 :            :   auto_vec <layout_range> m_layout_ranges;
     400                 :            :   auto_vec <const fixit_hint *> m_fixit_hints;
     401                 :            :   auto_vec <line_span> m_line_spans;
     402                 :            :   int m_linenum_width;
     403                 :            :   int m_x_offset_display;
     404                 :            : };
     405                 :            : 
     406                 :            : /* Implementation of "class colorizer".  */
     407                 :            : 
     408                 :            : /* The constructor for "colorizer".  Lookup and store color codes for the
     409                 :            :    different kinds of things we might need to print.  */
     410                 :            : 
     411                 :      97709 : colorizer::colorizer (diagnostic_context *context,
     412                 :      97709 :                       diagnostic_t diagnostic_kind) :
     413                 :            :   m_context (context),
     414                 :            :   m_diagnostic_kind (diagnostic_kind),
     415                 :      97709 :   m_current_state (STATE_NORMAL_TEXT)
     416                 :            : {
     417                 :      97709 :   m_range1 = get_color_by_name ("range1");
     418                 :      97709 :   m_range2 = get_color_by_name ("range2");
     419                 :      97709 :   m_fixit_insert = get_color_by_name ("fixit-insert");
     420                 :      97709 :   m_fixit_delete = get_color_by_name ("fixit-delete");
     421                 :      97709 :   m_stop_color = colorize_stop (pp_show_color (context->printer));
     422                 :      97709 : }
     423                 :            : 
     424                 :            : /* The destructor for "colorize".  If colorization is on, print a code to
     425                 :            :    turn it off.  */
     426                 :            : 
     427                 :          0 : colorizer::~colorizer ()
     428                 :            : {
     429                 :          0 :   finish_state (m_current_state);
     430                 :          0 : }
     431                 :            : 
     432                 :            : /* Update state, printing color codes if necessary if there's a state
     433                 :            :    change.  */
     434                 :            : 
     435                 :            : void
     436                 :   17027800 : colorizer::set_state (int new_state)
     437                 :            : {
     438                 :   17027800 :   if (m_current_state != new_state)
     439                 :            :     {
     440                 :     260048 :       finish_state (m_current_state);
     441                 :     130024 :       m_current_state = new_state;
     442                 :     130024 :       begin_state (new_state);
     443                 :            :     }
     444                 :   17027800 : }
     445                 :            : 
     446                 :            : /* Turn on any colorization for STATE.  */
     447                 :            : 
     448                 :            : void
     449                 :     130024 : colorizer::begin_state (int state)
     450                 :            : {
     451                 :     130024 :   switch (state)
     452                 :            :     {
     453                 :            :     case STATE_NORMAL_TEXT:
     454                 :            :       break;
     455                 :            : 
     456                 :      14334 :     case STATE_FIXIT_INSERT:
     457                 :      14334 :       pp_string (m_context->printer, m_fixit_insert);
     458                 :      14334 :       break;
     459                 :            : 
     460                 :       6299 :     case STATE_FIXIT_DELETE:
     461                 :       6299 :       pp_string (m_context->printer, m_fixit_delete);
     462                 :       6299 :       break;
     463                 :            : 
     464                 :      39953 :     case 0:
     465                 :            :       /* Make range 0 be the same color as the "kind" text
     466                 :            :          (error vs warning vs note).  */
     467                 :      39953 :       pp_string
     468                 :      39953 :         (m_context->printer,
     469                 :      39953 :          colorize_start (pp_show_color (m_context->printer),
     470                 :            :                          diagnostic_get_color_for_kind (m_diagnostic_kind)));
     471                 :      39953 :       break;
     472                 :            : 
     473                 :       2561 :     case 1:
     474                 :       2561 :       pp_string (m_context->printer, m_range1);
     475                 :       2561 :       break;
     476                 :            : 
     477                 :       1325 :     case 2:
     478                 :       1325 :       pp_string (m_context->printer, m_range2);
     479                 :       1325 :       break;
     480                 :            : 
     481                 :        571 :     default:
     482                 :            :       /* For ranges beyond 2, alternate between color 1 and color 2.  */
     483                 :        571 :       {
     484                 :        571 :         gcc_assert (state > 2);
     485                 :        571 :         pp_string (m_context->printer,
     486                 :        571 :                    state % 2 ? m_range1 : m_range2);
     487                 :            :       }
     488                 :        571 :       break;
     489                 :            :     }
     490                 :     130024 : }
     491                 :            : 
     492                 :            : /* Turn off any colorization for STATE.  */
     493                 :            : 
     494                 :            : void
     495                 :     130024 : colorizer::finish_state (int state)
     496                 :            : {
     497                 :     130024 :   if (state != STATE_NORMAL_TEXT)
     498                 :      65043 :     pp_string (m_context->printer, m_stop_color);
     499                 :          0 : }
     500                 :            : 
     501                 :            : /* Get the color code for NAME (or the empty string if
     502                 :            :    colorization is disabled).  */
     503                 :            : 
     504                 :            : const char *
     505                 :     390836 : colorizer::get_color_by_name (const char *name)
     506                 :            : {
     507                 :     390836 :   return colorize_start (pp_show_color (m_context->printer), name);
     508                 :            : }
     509                 :            : 
     510                 :            : /* Implementation of class layout_range.  */
     511                 :            : 
     512                 :            : /* The constructor for class layout_range.
     513                 :            :    Initialize various layout_point fields from expanded_location
     514                 :            :    equivalents; we've already filtered on file.  */
     515                 :            : 
     516                 :     103161 : layout_range::layout_range (const expanded_location *start_exploc,
     517                 :            :                             const expanded_location *finish_exploc,
     518                 :            :                             enum range_display_kind range_display_kind,
     519                 :            :                             const expanded_location *caret_exploc,
     520                 :            :                             unsigned original_idx,
     521                 :     103161 :                             const range_label *label)
     522                 :            : : m_start (*start_exploc),
     523                 :            :   m_finish (*finish_exploc),
     524                 :            :   m_range_display_kind (range_display_kind),
     525                 :            :   m_caret (*caret_exploc),
     526                 :            :   m_original_idx (original_idx),
     527                 :     103161 :   m_label (label)
     528                 :            : {
     529                 :     103161 : }
     530                 :            : 
     531                 :            : /* Is (column, row) within the given range?
     532                 :            :    We've already filtered on the file.
     533                 :            : 
     534                 :            :    Ranges are closed (both limits are within the range).
     535                 :            : 
     536                 :            :    Example A: a single-line range:
     537                 :            :      start:  (col=22, line=2)
     538                 :            :      finish: (col=38, line=2)
     539                 :            : 
     540                 :            :   |00000011111111112222222222333333333344444444444
     541                 :            :   |34567890123456789012345678901234567890123456789
     542                 :            : --+-----------------------------------------------
     543                 :            : 01|bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb
     544                 :            : 02|bbbbbbbbbbbbbbbbbbbSwwwwwwwwwwwwwwwFaaaaaaaaaaa
     545                 :            : 03|aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
     546                 :            : 
     547                 :            :    Example B: a multiline range with
     548                 :            :      start:  (col=14, line=3)
     549                 :            :      finish: (col=08, line=5)
     550                 :            : 
     551                 :            :   |00000011111111112222222222333333333344444444444
     552                 :            :   |34567890123456789012345678901234567890123456789
     553                 :            : --+-----------------------------------------------
     554                 :            : 01|bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb
     555                 :            : 02|bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb
     556                 :            : 03|bbbbbbbbbbbSwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwww
     557                 :            : 04|wwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwww
     558                 :            : 05|wwwwwFaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
     559                 :            : 06|aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
     560                 :            : --+-----------------------------------------------
     561                 :            : 
     562                 :            :    Legend:
     563                 :            :    - 'b' indicates a point *before* the range
     564                 :            :    - 'S' indicates the start of the range
     565                 :            :    - 'w' indicates a point within the range
     566                 :            :    - 'F' indicates the finish of the range (which is
     567                 :            :          within it).
     568                 :            :    - 'a' indicates a subsequent point *after* the range.
     569                 :            : 
     570                 :            :    COL_UNIT controls whether we check the byte column or
     571                 :            :    the display column; one or the other is more convenient
     572                 :            :    depending on the context.  */
     573                 :            : 
     574                 :            : bool
     575                 :   16824000 : layout_range::contains_point (linenum_type row, int column,
     576                 :            :                               enum column_unit col_unit) const
     577                 :            : {
     578                 :   16824000 :   gcc_assert (m_start.m_line <= m_finish.m_line);
     579                 :            :   /* ...but the equivalent isn't true for the columns;
     580                 :            :      consider example B in the comment above.  */
     581                 :            : 
     582                 :   16824000 :   if (row < m_start.m_line)
     583                 :            :     /* Points before the first line of the range are
     584                 :            :        outside it (corresponding to line 01 in example A
     585                 :            :        and lines 01 and 02 in example B above).  */
     586                 :            :     return false;
     587                 :            : 
     588                 :   16801300 :   if (row == m_start.m_line)
     589                 :            :     /* On same line as start of range (corresponding
     590                 :            :        to line 02 in example A and line 03 in example B).  */
     591                 :            :     {
     592                 :   16744500 :       if (column < m_start.m_columns[col_unit])
     593                 :            :         /* Points on the starting line of the range, but
     594                 :            :            before the column in which it begins.  */
     595                 :            :         return false;
     596                 :            : 
     597                 :     334330 :       if (row < m_finish.m_line)
     598                 :            :         /* This is a multiline range; the point
     599                 :            :            is within it (corresponds to line 03 in example B
     600                 :            :            from column 14 onwards) */
     601                 :            :         return true;
     602                 :            :       else
     603                 :            :         {
     604                 :            :           /* This is a single-line range.  */
     605                 :     327858 :           gcc_assert (row == m_finish.m_line);
     606                 :     327858 :           return column <= m_finish.m_columns[col_unit];
     607                 :            :         }
     608                 :            :     }
     609                 :            : 
     610                 :            :   /* The point is in a line beyond that containing the
     611                 :            :      start of the range: lines 03 onwards in example A,
     612                 :            :      and lines 04 onwards in example B.  */
     613                 :      56802 :   gcc_assert (row > m_start.m_line);
     614                 :            : 
     615                 :      56802 :   if (row > m_finish.m_line)
     616                 :            :     /* The point is beyond the final line of the range
     617                 :            :        (lines 03 onwards in example A, and lines 06 onwards
     618                 :            :        in example B).  */
     619                 :            :     return false;
     620                 :            : 
     621                 :       5772 :   if (row < m_finish.m_line)
     622                 :            :     {
     623                 :            :       /* The point is in a line that's fully within a multiline
     624                 :            :          range (e.g. line 04 in example B).  */
     625                 :       2497 :       gcc_assert (m_start.m_line < m_finish.m_line);
     626                 :            :       return true;
     627                 :            :     }
     628                 :            : 
     629                 :       3275 :   gcc_assert (row ==  m_finish.m_line);
     630                 :            : 
     631                 :       3275 :   return column <= m_finish.m_columns[col_unit];
     632                 :            : }
     633                 :            : 
     634                 :            : /* Does this layout_range contain any part of line ROW?  */
     635                 :            : 
     636                 :            : bool
     637                 :      90968 : layout_range::intersects_line_p (linenum_type row) const
     638                 :            : {
     639                 :          0 :   gcc_assert (m_start.m_line <= m_finish.m_line);
     640                 :      90968 :   if (row < m_start.m_line)
     641                 :            :     return false;
     642                 :      90626 :   if (row > m_finish.m_line)
     643                 :          0 :     return false;
     644                 :            :   return true;
     645                 :            : }
     646                 :            : 
     647                 :            : #if CHECKING_P
     648                 :            : 
     649                 :            : /* Create some expanded locations for testing layout_range.  The filename
     650                 :            :    member of the explocs is set to the empty string.  This member will only be
     651                 :            :    inspected by the calls to location_compute_display_column() made from the
     652                 :            :    layout_point constructors.  That function will check for an empty filename
     653                 :            :    argument and not attempt to open it, rather treating the non-existent data
     654                 :            :    as if the display width were the same as the byte count.  Tests exercising a
     655                 :            :    real difference between byte count and display width are performed later,
     656                 :            :    e.g. in test_diagnostic_show_locus_one_liner_utf8().  */
     657                 :            : 
     658                 :            : static layout_range
     659                 :          6 : make_range (int start_line, int start_col, int end_line, int end_col)
     660                 :            : {
     661                 :          6 :   const expanded_location start_exploc
     662                 :          6 :     = {"", start_line, start_col, NULL, false};
     663                 :          6 :   const expanded_location finish_exploc
     664                 :          6 :     = {"", end_line, end_col, NULL, false};
     665                 :          6 :   return layout_range (&start_exploc, &finish_exploc, SHOW_RANGE_WITHOUT_CARET,
     666                 :          0 :                        &start_exploc, 0, NULL);
     667                 :            : }
     668                 :            : 
     669                 :            : /* Selftests for layout_range::contains_point and
     670                 :            :    layout_range::intersects_line_p.  */
     671                 :            : 
     672                 :            : /* Selftest for layout_range, where the layout_range
     673                 :            :    is a range with start==end i.e. a single point.  */
     674                 :            : 
     675                 :            : static void
     676                 :          2 : test_layout_range_for_single_point ()
     677                 :            : {
     678                 :          2 :   layout_range point = make_range (7, 10, 7, 10);
     679                 :            : 
     680                 :            :   /* Tests for layout_range::contains_point.  */
     681                 :            : 
     682                 :          6 :   for (int i = 0; i != CU_NUM_UNITS; ++i)
     683                 :            :     {
     684                 :          4 :       const enum column_unit col_unit = (enum column_unit) i;
     685                 :            : 
     686                 :            :       /* Before the line.  */
     687                 :          4 :       ASSERT_FALSE (point.contains_point (6, 1, col_unit));
     688                 :            : 
     689                 :            :       /* On the line, but before start.  */
     690                 :          4 :       ASSERT_FALSE (point.contains_point (7, 9, col_unit));
     691                 :            : 
     692                 :            :       /* At the point.  */
     693                 :          4 :       ASSERT_TRUE (point.contains_point (7, 10, col_unit));
     694                 :            : 
     695                 :            :       /* On the line, after the point.  */
     696                 :          4 :       ASSERT_FALSE (point.contains_point (7, 11, col_unit));
     697                 :            : 
     698                 :            :       /* After the line.  */
     699                 :          4 :       ASSERT_FALSE (point.contains_point (8, 1, col_unit));
     700                 :            :     }
     701                 :            : 
     702                 :            :   /* Tests for layout_range::intersects_line_p.  */
     703                 :          4 :   ASSERT_FALSE (point.intersects_line_p (6));
     704                 :          4 :   ASSERT_TRUE (point.intersects_line_p (7));
     705                 :          4 :   ASSERT_FALSE (point.intersects_line_p (8));
     706                 :          2 : }
     707                 :            : 
     708                 :            : /* Selftest for layout_range, where the layout_range
     709                 :            :    is the single-line range shown as "Example A" above.  */
     710                 :            : 
     711                 :            : static void
     712                 :          2 : test_layout_range_for_single_line ()
     713                 :            : {
     714                 :          2 :   layout_range example_a = make_range (2, 22, 2, 38);
     715                 :            : 
     716                 :            :   /* Tests for layout_range::contains_point.  */
     717                 :            : 
     718                 :          6 :   for (int i = 0; i != CU_NUM_UNITS; ++i)
     719                 :            :     {
     720                 :          4 :       const enum column_unit col_unit = (enum column_unit) i;
     721                 :            : 
     722                 :            :       /* Before the line.  */
     723                 :          4 :       ASSERT_FALSE (example_a.contains_point (1, 1, col_unit));
     724                 :            : 
     725                 :            :       /* On the line, but before start.  */
     726                 :          4 :       ASSERT_FALSE (example_a.contains_point (2, 21, col_unit));
     727                 :            : 
     728                 :            :       /* On the line, at the start.  */
     729                 :          4 :       ASSERT_TRUE (example_a.contains_point (2, 22, col_unit));
     730                 :            : 
     731                 :            :       /* On the line, within the range.  */
     732                 :          4 :       ASSERT_TRUE (example_a.contains_point (2, 23, col_unit));
     733                 :            : 
     734                 :            :       /* On the line, at the end.  */
     735                 :          4 :       ASSERT_TRUE (example_a.contains_point (2, 38, col_unit));
     736                 :            : 
     737                 :            :       /* On the line, after the end.  */
     738                 :          4 :       ASSERT_FALSE (example_a.contains_point (2, 39, col_unit));
     739                 :            : 
     740                 :            :       /* After the line.  */
     741                 :          4 :       ASSERT_FALSE (example_a.contains_point (2, 39, col_unit));
     742                 :            :     }
     743                 :            : 
     744                 :            :   /* Tests for layout_range::intersects_line_p.  */
     745                 :          4 :   ASSERT_FALSE (example_a.intersects_line_p (1));
     746                 :          4 :   ASSERT_TRUE (example_a.intersects_line_p (2));
     747                 :          4 :   ASSERT_FALSE (example_a.intersects_line_p (3));
     748                 :          2 : }
     749                 :            : 
     750                 :            : /* Selftest for layout_range, where the layout_range
     751                 :            :    is the multi-line range shown as "Example B" above.  */
     752                 :            : 
     753                 :            : static void
     754                 :          2 : test_layout_range_for_multiple_lines ()
     755                 :            : {
     756                 :          2 :   layout_range example_b = make_range (3, 14, 5, 8);
     757                 :            : 
     758                 :            :   /* Tests for layout_range::contains_point.  */
     759                 :            : 
     760                 :          6 :   for (int i = 0; i != CU_NUM_UNITS; ++i)
     761                 :            :     {
     762                 :          4 :       const enum column_unit col_unit = (enum column_unit) i;
     763                 :            : 
     764                 :            :       /* Before first line.  */
     765                 :          4 :       ASSERT_FALSE (example_b.contains_point (1, 1, col_unit));
     766                 :            : 
     767                 :            :       /* On the first line, but before start.  */
     768                 :          4 :       ASSERT_FALSE (example_b.contains_point (3, 13, col_unit));
     769                 :            : 
     770                 :            :       /* At the start.  */
     771                 :          4 :       ASSERT_TRUE (example_b.contains_point (3, 14, col_unit));
     772                 :            : 
     773                 :            :       /* On the first line, within the range.  */
     774                 :          4 :       ASSERT_TRUE (example_b.contains_point (3, 15, col_unit));
     775                 :            : 
     776                 :            :       /* On an interior line.
     777                 :            :          The column number should not matter; try various boundary
     778                 :            :          values.  */
     779                 :          4 :       ASSERT_TRUE (example_b.contains_point (4, 1, col_unit));
     780                 :          4 :       ASSERT_TRUE (example_b.contains_point (4, 7, col_unit));
     781                 :          4 :       ASSERT_TRUE (example_b.contains_point (4, 8, col_unit));
     782                 :          4 :       ASSERT_TRUE (example_b.contains_point (4, 9, col_unit));
     783                 :          4 :       ASSERT_TRUE (example_b.contains_point (4, 13, col_unit));
     784                 :          4 :       ASSERT_TRUE (example_b.contains_point (4, 14, col_unit));
     785                 :          4 :       ASSERT_TRUE (example_b.contains_point (4, 15, col_unit));
     786                 :            : 
     787                 :            :       /* On the final line, before the end.  */
     788                 :          4 :       ASSERT_TRUE (example_b.contains_point (5, 7, col_unit));
     789                 :            : 
     790                 :            :       /* On the final line, at the end.  */
     791                 :          4 :       ASSERT_TRUE (example_b.contains_point (5, 8, col_unit));
     792                 :            : 
     793                 :            :       /* On the final line, after the end.  */
     794                 :          4 :       ASSERT_FALSE (example_b.contains_point (5, 9, col_unit));
     795                 :            : 
     796                 :            :       /* After the line.  */
     797                 :          4 :       ASSERT_FALSE (example_b.contains_point (6, 1, col_unit));
     798                 :            :     }
     799                 :            : 
     800                 :            :   /* Tests for layout_range::intersects_line_p.  */
     801                 :          4 :   ASSERT_FALSE (example_b.intersects_line_p (2));
     802                 :          4 :   ASSERT_TRUE (example_b.intersects_line_p (3));
     803                 :          4 :   ASSERT_TRUE (example_b.intersects_line_p (4));
     804                 :          4 :   ASSERT_TRUE (example_b.intersects_line_p (5));
     805                 :          4 :   ASSERT_FALSE (example_b.intersects_line_p (6));
     806                 :          2 : }
     807                 :            : 
     808                 :            : #endif /* #if CHECKING_P */
     809                 :            : 
     810                 :            : /* Given a source line LINE of length LINE_BYTES bytes, determine the length
     811                 :            :    (still in bytes, not display cols) without any trailing whitespace.  */
     812                 :            : 
     813                 :            : static int
     814                 :     182470 : get_line_bytes_without_trailing_whitespace (const char *line, int line_bytes)
     815                 :            : {
     816                 :     182470 :   int result = line_bytes;
     817                 :  520431000 :   while (result > 0)
     818                 :            :     {
     819                 :  520291000 :       char ch = line[result - 1];
     820                 :  520291000 :       if (ch == ' ' || ch == '\t' || ch == '\r')
     821                 :  520248000 :         result--;
     822                 :            :       else
     823                 :            :         break;
     824                 :            :     }
     825                 :     182470 :   gcc_assert (result >= 0);
     826                 :     182470 :   gcc_assert (result <= line_bytes);
     827                 :     182470 :   gcc_assert (result == 0 ||
     828                 :            :               (line[result - 1] != ' '
     829                 :            :                && line[result -1] != '\t'
     830                 :            :                && line[result -1] != '\r'));
     831                 :     182470 :   return result;
     832                 :            : }
     833                 :            : 
     834                 :            : #if CHECKING_P
     835                 :            : 
     836                 :            : /* A helper function for testing get_line_bytes_without_trailing_whitespace.  */
     837                 :            : 
     838                 :            : static void
     839                 :         16 : assert_eq (const char *line, int expected_bytes)
     840                 :            : {
     841                 :         16 :   int actual_value
     842                 :         16 :     = get_line_bytes_without_trailing_whitespace (line, strlen (line));
     843                 :         16 :   ASSERT_EQ (actual_value, expected_bytes);
     844                 :         16 : }
     845                 :            : 
     846                 :            : /* Verify that get_line_bytes_without_trailing_whitespace is sane for
     847                 :            :    various inputs.  It is not required to handle newlines.  */
     848                 :            : 
     849                 :            : static void
     850                 :          2 : test_get_line_bytes_without_trailing_whitespace ()
     851                 :            : {
     852                 :          2 :   assert_eq ("", 0);
     853                 :          2 :   assert_eq (" ", 0);
     854                 :          2 :   assert_eq ("\t", 0);
     855                 :          2 :   assert_eq ("\r", 0);
     856                 :          2 :   assert_eq ("hello world", 11);
     857                 :          2 :   assert_eq ("hello world     ", 11);
     858                 :          2 :   assert_eq ("hello world     \t\t  ", 11);
     859                 :          2 :   assert_eq ("hello world\r", 11);
     860                 :          2 : }
     861                 :            : 
     862                 :            : #endif /* #if CHECKING_P */
     863                 :            : 
     864                 :            : /* Helper function for layout's ctor, for sanitizing locations relative
     865                 :            :    to the primary location within a diagnostic.
     866                 :            : 
     867                 :            :    Compare LOC_A and LOC_B to see if it makes sense to print underlines
     868                 :            :    connecting their expanded locations.  Doing so is only guaranteed to
     869                 :            :    make sense if the locations share the same macro expansion "history"
     870                 :            :    i.e. they can be traced through the same macro expansions, eventually
     871                 :            :    reaching an ordinary map.
     872                 :            : 
     873                 :            :    This may be too strong a condition, but it effectively sanitizes
     874                 :            :    PR c++/70105, which has an example of printing an expression where the
     875                 :            :    final location of the expression is in a different macro, which
     876                 :            :    erroneously was leading to hundreds of lines of irrelevant source
     877                 :            :    being printed.  */
     878                 :            : 
     879                 :            : static bool
     880                 :     206472 : compatible_locations_p (location_t loc_a, location_t loc_b)
     881                 :            : {
     882                 :     206669 :   if (IS_ADHOC_LOC (loc_a))
     883                 :        128 :     loc_a = get_location_from_adhoc_loc (line_table, loc_a);
     884                 :     206669 :   if (IS_ADHOC_LOC (loc_b))
     885                 :       7478 :     loc_b = get_location_from_adhoc_loc (line_table, loc_b);
     886                 :            : 
     887                 :            :   /* If either location is one of the special locations outside of a
     888                 :            :      linemap, they are only compatible if they are equal.  */
     889                 :     206669 :   if (loc_a < RESERVED_LOCATION_COUNT
     890                 :     206669 :       || loc_b < RESERVED_LOCATION_COUNT)
     891                 :         13 :     return loc_a == loc_b;
     892                 :            : 
     893                 :     206656 :   const line_map *map_a = linemap_lookup (line_table, loc_a);
     894                 :     206656 :   linemap_assert (map_a);
     895                 :            : 
     896                 :     206656 :   const line_map *map_b = linemap_lookup (line_table, loc_b);
     897                 :     206656 :   linemap_assert (map_b);
     898                 :            : 
     899                 :            :   /* Are they within the same map?  */
     900                 :     206656 :   if (map_a == map_b)
     901                 :            :     {
     902                 :            :       /* Are both within the same macro expansion?  */
     903                 :     206592 :       if (linemap_macro_expansion_map_p (map_a))
     904                 :            :         {
     905                 :            :           /* Expand each location towards the spelling location, and
     906                 :            :              recurse.  */
     907                 :        197 :           const line_map_macro *macro_map = linemap_check_macro (map_a);
     908                 :        197 :           location_t loc_a_toward_spelling
     909                 :        197 :             = linemap_macro_map_loc_unwind_toward_spelling (line_table,
     910                 :            :                                                             macro_map,
     911                 :            :                                                             loc_a);
     912                 :        197 :           location_t loc_b_toward_spelling
     913                 :        197 :             = linemap_macro_map_loc_unwind_toward_spelling (line_table,
     914                 :            :                                                             macro_map,
     915                 :            :                                                             loc_b);
     916                 :        197 :           return compatible_locations_p (loc_a_toward_spelling,
     917                 :        197 :                                          loc_b_toward_spelling);
     918                 :            :         }
     919                 :            : 
     920                 :            :       /* Otherwise they are within the same ordinary map.  */
     921                 :            :       return true;
     922                 :            :     }
     923                 :            :   else
     924                 :            :     {
     925                 :            :       /* Within different maps.  */
     926                 :            : 
     927                 :            :       /* If either is within a macro expansion, they are incompatible.  */
     928                 :         64 :       if (linemap_macro_expansion_map_p (map_a)
     929                 :         64 :           || linemap_macro_expansion_map_p (map_b))
     930                 :         38 :         return false;
     931                 :            : 
     932                 :            :       /* Within two different ordinary maps; they are compatible iff they
     933                 :            :          are in the same file.  */
     934                 :         26 :       const line_map_ordinary *ord_map_a = linemap_check_ordinary (map_a);
     935                 :         26 :       const line_map_ordinary *ord_map_b = linemap_check_ordinary (map_b);
     936                 :         26 :       return ord_map_a->to_file == ord_map_b->to_file;
     937                 :            :     }
     938                 :            : }
     939                 :            : 
     940                 :            : /* Comparator for sorting fix-it hints.  */
     941                 :            : 
     942                 :            : static int
     943                 :      34832 : fixit_cmp (const void *p_a, const void *p_b)
     944                 :            : {
     945                 :      34832 :   const fixit_hint * hint_a = *static_cast<const fixit_hint * const *> (p_a);
     946                 :      34832 :   const fixit_hint * hint_b = *static_cast<const fixit_hint * const *> (p_b);
     947                 :      34832 :   return hint_a->get_start_loc () - hint_b->get_start_loc ();
     948                 :            : }
     949                 :            : 
     950                 :            : /* Implementation of class layout.  */
     951                 :            : 
     952                 :            : /* Constructor for class layout.
     953                 :            : 
     954                 :            :    Filter the ranges from the rich_location to those that we can
     955                 :            :    sanely print, populating m_layout_ranges and m_fixit_hints.
     956                 :            :    Determine the range of lines that we will print, splitting them
     957                 :            :    up into an ordered list of disjoint spans of contiguous line numbers.
     958                 :            :    Determine m_x_offset_display, to ensure that the primary caret
     959                 :            :    will fit within the max_width provided by the diagnostic_context.  */
     960                 :            : 
     961                 :      97709 : layout::layout (diagnostic_context * context,
     962                 :            :                 rich_location *richloc,
     963                 :      97709 :                 diagnostic_t diagnostic_kind)
     964                 :            : : m_context (context),
     965                 :      97709 :   m_pp (context->printer),
     966                 :     195418 :   m_primary_loc (richloc->get_range (0)->m_loc),
     967                 :      97709 :   m_exploc (richloc->get_expanded_location (0)),
     968                 :            :   m_colorizer (context, diagnostic_kind),
     969                 :      97709 :   m_colorize_source_p (context->colorize_source_p),
     970                 :      97709 :   m_show_labels_p (context->show_labels_p),
     971                 :      97709 :   m_show_line_numbers_p (context->show_line_numbers_p),
     972                 :      97709 :   m_diagnostic_path_p (diagnostic_kind == DK_DIAGNOSTIC_PATH),
     973                 :      97709 :   m_layout_ranges (richloc->get_num_locations ()),
     974                 :      97709 :   m_fixit_hints (richloc->get_num_fixit_hints ()),
     975                 :      97709 :   m_line_spans (1 + richloc->get_num_locations ()),
     976                 :            :   m_linenum_width (0),
     977                 :     311131 :   m_x_offset_display (0)
     978                 :            : {
     979                 :     199401 :   for (unsigned int idx = 0; idx < richloc->get_num_locations (); idx++)
     980                 :            :     {
     981                 :            :       /* This diagnostic printer can only cope with "sufficiently sane" ranges.
     982                 :            :          Ignore any ranges that are awkward to handle.  */
     983                 :     101692 :       const location_range *loc_range = richloc->get_range (idx);
     984                 :     101692 :       maybe_add_location_range (loc_range, idx, false);
     985                 :            :     }
     986                 :            : 
     987                 :            :   /* Populate m_fixit_hints, filtering to only those that are in the
     988                 :            :      same file.  */
     989                 :     117909 :   for (unsigned int i = 0; i < richloc->get_num_fixit_hints (); i++)
     990                 :            :     {
     991                 :      20200 :       const fixit_hint *hint = richloc->get_fixit_hint (i);
     992                 :      20200 :       if (validate_fixit_hint_p (hint))
     993                 :      20200 :         m_fixit_hints.safe_push (hint);
     994                 :            :     }
     995                 :            : 
     996                 :            :   /* Sort m_fixit_hints.  */
     997                 :      97709 :   m_fixit_hints.qsort (fixit_cmp);
     998                 :            : 
     999                 :            :   /* Populate the indicated members.  */
    1000                 :      97709 :   calculate_line_spans ();
    1001                 :      97709 :   calculate_linenum_width ();
    1002                 :      97709 :   calculate_x_offset_display ();
    1003                 :            : 
    1004                 :      97709 :   if (context->show_ruler_p)
    1005                 :        164 :     show_ruler (m_x_offset_display + m_context->caret_max_width);
    1006                 :      97709 : }
    1007                 :            : 
    1008                 :            : 
    1009                 :            : /* Attempt to add LOC_RANGE to m_layout_ranges, filtering them to
    1010                 :            :    those that we can sanely print.
    1011                 :            : 
    1012                 :            :    ORIGINAL_IDX is the index of LOC_RANGE within its rich_location,
    1013                 :            :    (for use as extrinsic state by label ranges FIXME).
    1014                 :            : 
    1015                 :            :    If RESTRICT_TO_CURRENT_LINE_SPANS is true, then LOC_RANGE is also
    1016                 :            :    filtered against this layout instance's current line spans: it
    1017                 :            :    will only be added if the location is fully within the lines
    1018                 :            :    already specified by other locations.
    1019                 :            : 
    1020                 :            :    Return true iff LOC_RANGE was added.  */
    1021                 :            : 
    1022                 :            : bool
    1023                 :     103166 : layout::maybe_add_location_range (const location_range *loc_range,
    1024                 :            :                                   unsigned original_idx,
    1025                 :            :                                   bool restrict_to_current_line_spans)
    1026                 :            : {
    1027                 :     103166 :   gcc_assert (loc_range);
    1028                 :            : 
    1029                 :            :   /* Split the "range" into caret and range information.  */
    1030                 :     103166 :   source_range src_range = get_range_from_loc (line_table, loc_range->m_loc);
    1031                 :            : 
    1032                 :            :   /* Expand the various locations.  */
    1033                 :     103166 :   expanded_location start
    1034                 :            :     = linemap_client_expand_location_to_spelling_point
    1035                 :     103166 :     (src_range.m_start, LOCATION_ASPECT_START);
    1036                 :     103166 :   expanded_location finish
    1037                 :            :     = linemap_client_expand_location_to_spelling_point
    1038                 :     103166 :     (src_range.m_finish, LOCATION_ASPECT_FINISH);
    1039                 :     103166 :   expanded_location caret
    1040                 :            :     = linemap_client_expand_location_to_spelling_point
    1041                 :     103166 :     (loc_range->m_loc, LOCATION_ASPECT_CARET);
    1042                 :            : 
    1043                 :            :   /* If any part of the range isn't in the same file as the primary
    1044                 :            :      location of this diagnostic, ignore the range.  */
    1045                 :     103166 :   if (start.file != m_exploc.file)
    1046                 :            :     return false;
    1047                 :     103157 :   if (finish.file != m_exploc.file)
    1048                 :            :     return false;
    1049                 :     103155 :   if (loc_range->m_range_display_kind == SHOW_RANGE_WITH_CARET)
    1050                 :      97753 :     if (caret.file != m_exploc.file)
    1051                 :            :       return false;
    1052                 :            : 
    1053                 :            :   /* Sanitize the caret location for non-primary ranges.  */
    1054                 :     103155 :   if (m_layout_ranges.length () > 0)
    1055                 :       5449 :     if (loc_range->m_range_display_kind == SHOW_RANGE_WITH_CARET)
    1056                 :        203 :       if (!compatible_locations_p (loc_range->m_loc, m_primary_loc))
    1057                 :            :         /* Discard any non-primary ranges that can't be printed
    1058                 :            :            sanely relative to the primary location.  */
    1059                 :            :         return false;
    1060                 :            : 
    1061                 :            :   /* Everything is now known to be in the correct source file,
    1062                 :            :      but it may require further sanitization.  */
    1063                 :     103155 :   layout_range ri (&start, &finish, loc_range->m_range_display_kind, &caret,
    1064                 :     103155 :                    original_idx, loc_range->m_label);
    1065                 :            : 
    1066                 :            :   /* If we have a range that finishes before it starts (perhaps
    1067                 :            :      from something built via macro expansion), printing the
    1068                 :            :      range is likely to be nonsensical.  Also, attempting to do so
    1069                 :            :      breaks assumptions within the printing code  (PR c/68473).
    1070                 :            :      Similarly, don't attempt to print ranges if one or both ends
    1071                 :            :      of the range aren't sane to print relative to the
    1072                 :            :      primary location (PR c++/70105).  */
    1073                 :     103155 :   if (start.line > finish.line
    1074                 :     103147 :       || !compatible_locations_p (src_range.m_start, m_primary_loc)
    1075                 :     206277 :       || !compatible_locations_p (src_range.m_finish, m_primary_loc))
    1076                 :            :     {
    1077                 :            :       /* Is this the primary location?  */
    1078                 :         46 :       if (m_layout_ranges.length () == 0)
    1079                 :            :         {
    1080                 :            :           /* We want to print the caret for the primary location, but
    1081                 :            :              we must sanitize away m_start and m_finish.  */
    1082                 :         25 :           ri.m_start = ri.m_caret;
    1083                 :         25 :           ri.m_finish = ri.m_caret;
    1084                 :            :         }
    1085                 :            :       else
    1086                 :            :         /* This is a non-primary range; ignore it.  */
    1087                 :            :         return false;
    1088                 :            :     }
    1089                 :            : 
    1090                 :            :   /* Potentially filter to just the lines already specified by other
    1091                 :            :      locations.  This is for use by gcc_rich_location::add_location_if_nearby.
    1092                 :            :      The layout ctor doesn't use it, and can't because m_line_spans
    1093                 :            :      hasn't been set up at that point.  */
    1094                 :     103134 :   if (restrict_to_current_line_spans)
    1095                 :            :     {
    1096                 :       1295 :       if (!will_show_line_p (start.line))
    1097                 :            :         return false;
    1098                 :       1123 :       if (!will_show_line_p (finish.line))
    1099                 :            :         return false;
    1100                 :       1123 :       if (loc_range->m_range_display_kind == SHOW_RANGE_WITH_CARET)
    1101                 :          0 :         if (!will_show_line_p (caret.line))
    1102                 :            :           return false;
    1103                 :            :     }
    1104                 :            : 
    1105                 :            :   /* Passed all the tests; add the range to m_layout_ranges so that
    1106                 :            :      it will be printed.  */
    1107                 :     102962 :   m_layout_ranges.safe_push (ri);
    1108                 :     102962 :   return true;
    1109                 :            : }
    1110                 :            : 
    1111                 :            : /* Return true iff ROW is within one of the line spans for this layout.  */
    1112                 :            : 
    1113                 :            : bool
    1114                 :       2418 : layout::will_show_line_p (linenum_type row) const
    1115                 :            : {
    1116                 :       5180 :   for (int line_span_idx = 0; line_span_idx < get_num_line_spans ();
    1117                 :            :        line_span_idx++)
    1118                 :            :     {
    1119                 :       2418 :       const line_span *line_span = get_line_span (line_span_idx);
    1120                 :       2418 :       if (line_span->contains_line_p (row))
    1121                 :            :         return true;
    1122                 :            :     }
    1123                 :            :   return false;
    1124                 :            : }
    1125                 :            : 
    1126                 :            : /* Print a line showing a gap in the line numbers, for showing the boundary
    1127                 :            :    between two line spans.  */
    1128                 :            : 
    1129                 :            : void
    1130                 :         63 : layout::print_gap_in_line_numbering ()
    1131                 :            : {
    1132                 :         63 :   gcc_assert (m_show_line_numbers_p);
    1133                 :            : 
    1134                 :         63 :   pp_emit_prefix (m_pp);
    1135                 :            : 
    1136                 :        441 :   for (int i = 0; i < m_linenum_width + 1; i++)
    1137                 :        378 :     pp_character (m_pp, '.');
    1138                 :            : 
    1139                 :         63 :   pp_newline (m_pp);
    1140                 :         63 : }
    1141                 :            : 
    1142                 :            : /* Return true iff we should print a heading when starting the
    1143                 :            :    line span with the given index.  */
    1144                 :            : 
    1145                 :            : bool
    1146                 :      76448 : layout::print_heading_for_line_span_index_p (int line_span_idx) const
    1147                 :            : {
    1148                 :            :   /* We print a heading for every change of line span, hence for every
    1149                 :            :      line span after the initial one.  */
    1150                 :      76448 :   if (line_span_idx > 0)
    1151                 :            :     return true;
    1152                 :            : 
    1153                 :            :   /* We also do it for the initial span if the primary location of the
    1154                 :            :      diagnostic is in a different span.  */
    1155                 :      76385 :   if (m_exploc.line > (int)get_line_span (0)->m_last_line)
    1156                 :         59 :     return true;
    1157                 :            : 
    1158                 :            :   return false;
    1159                 :            : }
    1160                 :            : 
    1161                 :            : /* Get an expanded_location for the first location of interest within
    1162                 :            :    the given line_span.
    1163                 :            :    Used when printing a heading to indicate a new line span.  */
    1164                 :            : 
    1165                 :            : expanded_location
    1166                 :        122 : layout::get_expanded_location (const line_span *line_span) const
    1167                 :            : {
    1168                 :            :   /* Whenever possible, use the caret location.  */
    1169                 :        122 :   if (line_span->contains_line_p (m_exploc.line))
    1170                 :         59 :     return m_exploc;
    1171                 :            : 
    1172                 :            :   /* Otherwise, use the start of the first range that's present
    1173                 :            :      within the line_span.  */
    1174                 :        258 :   for (unsigned int i = 0; i < m_layout_ranges.length (); i++)
    1175                 :            :     {
    1176                 :         73 :       const layout_range *lr = &m_layout_ranges[i];
    1177                 :        132 :       if (line_span->contains_line_p (lr->m_start.m_line))
    1178                 :            :         {
    1179                 :          7 :           expanded_location exploc = m_exploc;
    1180                 :          7 :           exploc.line = lr->m_start.m_line;
    1181                 :          7 :           exploc.column = lr->m_start.m_columns[CU_BYTES];
    1182                 :          7 :           return exploc;
    1183                 :            :         }
    1184                 :            :     }
    1185                 :            : 
    1186                 :            :   /* Otherwise, use the location of the first fixit-hint present within
    1187                 :            :      the line_span.  */
    1188                 :        112 :   for (unsigned int i = 0; i < m_fixit_hints.length (); i++)
    1189                 :            :     {
    1190                 :         56 :       const fixit_hint *hint = m_fixit_hints[i];
    1191                 :         56 :       location_t loc = hint->get_start_loc ();
    1192                 :         56 :       expanded_location exploc = expand_location (loc);
    1193                 :         56 :       if (line_span->contains_line_p (exploc.line))
    1194                 :         56 :         return exploc;
    1195                 :            :     }
    1196                 :            : 
    1197                 :            :   /* It should not be possible to have a line span that didn't
    1198                 :            :      contain any of the layout_range or fixit_hint instances.  */
    1199                 :          0 :   gcc_unreachable ();
    1200                 :            :   return m_exploc;
    1201                 :            : }
    1202                 :            : 
    1203                 :            : /* Determine if HINT is meaningful to print within this layout.  */
    1204                 :            : 
    1205                 :            : bool
    1206                 :      20200 : layout::validate_fixit_hint_p (const fixit_hint *hint)
    1207                 :            : {
    1208                 :      20200 :   if (LOCATION_FILE (hint->get_start_loc ()) != m_exploc.file)
    1209                 :            :     return false;
    1210                 :      20200 :   if (LOCATION_FILE (hint->get_next_loc ()) != m_exploc.file)
    1211                 :          0 :     return false;
    1212                 :            : 
    1213                 :            :   return true;
    1214                 :            : }
    1215                 :            : 
    1216                 :            : /* Determine the range of lines affected by HINT.
    1217                 :            :    This assumes that HINT has already been filtered by
    1218                 :            :    validate_fixit_hint_p, and so affects the correct source file.  */
    1219                 :            : 
    1220                 :            : static line_span
    1221                 :      20200 : get_line_span_for_fixit_hint (const fixit_hint *hint)
    1222                 :            : {
    1223                 :      20200 :   gcc_assert (hint);
    1224                 :            : 
    1225                 :      20200 :   int start_line = LOCATION_LINE (hint->get_start_loc ());
    1226                 :            : 
    1227                 :            :   /* For line-insertion fix-it hints, add the previous line to the
    1228                 :            :      span, to give the user more context on the proposed change.  */
    1229                 :      20200 :   if (hint->ends_with_newline_p ())
    1230                 :        291 :     if (start_line > 1)
    1231                 :        224 :       start_line--;
    1232                 :            : 
    1233                 :      60600 :   return line_span (start_line,
    1234                 :      20200 :                     LOCATION_LINE (hint->get_next_loc ()));
    1235                 :            : }
    1236                 :            : 
    1237                 :            : /* We want to print the pertinent source code at a diagnostic.  The
    1238                 :            :    rich_location can contain multiple locations.  This will have been
    1239                 :            :    filtered into m_exploc (the caret for the primary location) and
    1240                 :            :    m_layout_ranges, for those ranges within the same source file.
    1241                 :            : 
    1242                 :            :    We will print a subset of the lines within the source file in question,
    1243                 :            :    as a collection of "spans" of lines.
    1244                 :            : 
    1245                 :            :    This function populates m_line_spans with an ordered, disjoint list of
    1246                 :            :    the line spans of interest.
    1247                 :            : 
    1248                 :            :    Printing a gap between line spans takes one line, so, when printing
    1249                 :            :    line numbers, we allow a gap of up to one line between spans when
    1250                 :            :    merging, since it makes more sense to print the source line rather than a
    1251                 :            :    "gap-in-line-numbering" line.  When not printing line numbers, it's
    1252                 :            :    better to be more explicit about what's going on, so keeping them as
    1253                 :            :    separate spans is preferred.
    1254                 :            : 
    1255                 :            :    For example, if the primary range is on lines 8-10, with secondary ranges
    1256                 :            :    covering lines 5-6 and lines 13-15:
    1257                 :            : 
    1258                 :            :      004
    1259                 :            :      005                   |RANGE 1
    1260                 :            :      006                   |RANGE 1
    1261                 :            :      007
    1262                 :            :      008  |PRIMARY RANGE
    1263                 :            :      009  |PRIMARY CARET
    1264                 :            :      010  |PRIMARY RANGE
    1265                 :            :      011
    1266                 :            :      012
    1267                 :            :      013                                |RANGE 2
    1268                 :            :      014                                |RANGE 2
    1269                 :            :      015                                |RANGE 2
    1270                 :            :      016
    1271                 :            : 
    1272                 :            :    With line numbering on, we want two spans: lines 5-10 and lines 13-15.
    1273                 :            : 
    1274                 :            :    With line numbering off (with span headers), we want three spans: lines 5-6,
    1275                 :            :    lines 8-10, and lines 13-15.  */
    1276                 :            : 
    1277                 :            : void
    1278                 :      97709 : layout::calculate_line_spans ()
    1279                 :            : {
    1280                 :            :   /* This should only be called once, by the ctor.  */
    1281                 :      97709 :   gcc_assert (m_line_spans.length () == 0);
    1282                 :            : 
    1283                 :            :   /* Populate tmp_spans with individual spans, for each of
    1284                 :            :      m_exploc, and for m_layout_ranges.  */
    1285                 :     195418 :   auto_vec<line_span> tmp_spans (1 + m_layout_ranges.length ());
    1286                 :      97709 :   tmp_spans.safe_push (line_span (m_exploc.line, m_exploc.line));
    1287                 :     398738 :   for (unsigned int i = 0; i < m_layout_ranges.length (); i++)
    1288                 :            :     {
    1289                 :     101660 :       const layout_range *lr = &m_layout_ranges[i];
    1290                 :     101660 :       gcc_assert (lr->m_start.m_line <= lr->m_finish.m_line);
    1291                 :     101660 :       tmp_spans.safe_push (line_span (lr->m_start.m_line,
    1292                 :            :                                       lr->m_finish.m_line));
    1293                 :            :     }
    1294                 :            : 
    1295                 :            :   /* Also add spans for any fix-it hints, in case they cover other lines.  */
    1296                 :     156113 :   for (unsigned int i = 0; i < m_fixit_hints.length (); i++)
    1297                 :            :     {
    1298                 :      20200 :       const fixit_hint *hint = m_fixit_hints[i];
    1299                 :      20200 :       gcc_assert (hint);
    1300                 :      20200 :       tmp_spans.safe_push (get_line_span_for_fixit_hint (hint));
    1301                 :            :     }
    1302                 :            : 
    1303                 :            :   /* Sort them.  */
    1304                 :      97709 :   tmp_spans.qsort(line_span::comparator);
    1305                 :            : 
    1306                 :            :   /* Now iterate through tmp_spans, copying into m_line_spans, and
    1307                 :            :      combining where possible.  */
    1308                 :      97709 :   gcc_assert (tmp_spans.length () > 0);
    1309                 :      97709 :   m_line_spans.safe_push (tmp_spans[0]);
    1310                 :     439138 :   for (unsigned int i = 1; i < tmp_spans.length (); i++)
    1311                 :            :     {
    1312                 :     243720 :       line_span *current = &m_line_spans[m_line_spans.length () - 1];
    1313                 :     121860 :       const line_span *next = &tmp_spans[i];
    1314                 :     121860 :       gcc_assert (next->m_first_line >= current->m_first_line);
    1315                 :     121860 :       const int merger_distance = m_show_line_numbers_p ? 1 : 0;
    1316                 :     121860 :       if ((linenum_arith_t)next->m_first_line
    1317                 :     121860 :           <= (linenum_arith_t)current->m_last_line + 1 + merger_distance)
    1318                 :            :         {
    1319                 :            :           /* We can merge them. */
    1320                 :     121699 :           if (next->m_last_line > current->m_last_line)
    1321                 :        431 :             current->m_last_line = next->m_last_line;
    1322                 :            :         }
    1323                 :            :       else
    1324                 :            :         {
    1325                 :            :           /* No merger possible.  */
    1326                 :        161 :           m_line_spans.safe_push (*next);
    1327                 :            :         }
    1328                 :            :     }
    1329                 :            : 
    1330                 :            :   /* Verify the result, in m_line_spans.  */
    1331                 :      97709 :   gcc_assert (m_line_spans.length () > 0);
    1332                 :      97870 :   for (unsigned int i = 1; i < m_line_spans.length (); i++)
    1333                 :            :     {
    1334                 :        161 :       const line_span *prev = &m_line_spans[i - 1];
    1335                 :        161 :       const line_span *next = &m_line_spans[i];
    1336                 :            :       /* The individual spans must be sane.  */
    1337                 :        161 :       gcc_assert (prev->m_first_line <= prev->m_last_line);
    1338                 :        161 :       gcc_assert (next->m_first_line <= next->m_last_line);
    1339                 :            :       /* The spans must be ordered.  */
    1340                 :        161 :       gcc_assert (prev->m_first_line < next->m_first_line);
    1341                 :            :       /* There must be a gap of at least one line between separate spans.  */
    1342                 :        161 :       gcc_assert ((prev->m_last_line + 1) < next->m_first_line);
    1343                 :            :     }
    1344                 :      97709 : }
    1345                 :            : 
    1346                 :            : /* Determine how many display columns need to be reserved for line numbers,
    1347                 :            :    based on the largest line number that will be needed, and populate
    1348                 :            :    m_linenum_width.  */
    1349                 :            : 
    1350                 :            : void
    1351                 :      97709 : layout::calculate_linenum_width ()
    1352                 :            : {
    1353                 :      97709 :   gcc_assert (m_line_spans.length () > 0);
    1354                 :      97709 :   const line_span *last_span = &m_line_spans[m_line_spans.length () - 1];
    1355                 :      97709 :   int highest_line = last_span->m_last_line;
    1356                 :      97709 :   if (highest_line < 0)
    1357                 :            :     highest_line = 0;
    1358                 :      97709 :   m_linenum_width = num_digits (highest_line);
    1359                 :            :   /* If we're showing jumps in the line-numbering, allow at least 3 chars.  */
    1360                 :      97709 :   if (m_line_spans.length () > 1)
    1361                 :        147 :     m_linenum_width = MAX (m_linenum_width, 3);
    1362                 :            :   /* If there's a minimum margin width, apply it (subtracting 1 for the space
    1363                 :            :      after the line number.  */
    1364                 :      97709 :   m_linenum_width = MAX (m_linenum_width, m_context->min_margin_width - 1);
    1365                 :      97709 : }
    1366                 :            : 
    1367                 :            : /* Calculate m_x_offset_display, which improves readability in case the source
    1368                 :            :    line of interest is longer than the user's display.  All lines output will be
    1369                 :            :    shifted to the left (so that their beginning is no longer displayed) by
    1370                 :            :    m_x_offset_display display columns, so that the caret is in a reasonable
    1371                 :            :    location.  */
    1372                 :            : 
    1373                 :            : void
    1374                 :      97709 : layout::calculate_x_offset_display ()
    1375                 :            : {
    1376                 :      97709 :   m_x_offset_display = 0;
    1377                 :            : 
    1378                 :      97709 :   const int max_width = m_context->caret_max_width;
    1379                 :      97709 :   if (!max_width)
    1380                 :            :     {
    1381                 :            :       /* Nothing to do, the width is not capped.  */
    1382                 :      97118 :       return;
    1383                 :            :     }
    1384                 :            : 
    1385                 :      97677 :   const char_span line = location_get_source_line (m_exploc.file,
    1386                 :      97677 :                                                    m_exploc.line);
    1387                 :      97677 :   if (!line)
    1388                 :            :     {
    1389                 :            :       /* Nothing to do, we couldn't find the source line.  */
    1390                 :            :       return;
    1391                 :            :     }
    1392                 :      91929 :   int caret_display_column = m_exploc.m_display_col;
    1393                 :      91929 :   const int line_bytes
    1394                 :      91929 :     = get_line_bytes_without_trailing_whitespace (line.get_buffer (),
    1395                 :      91929 :                                                   line.length ());
    1396                 :      91929 :   int eol_display_column
    1397                 :      91929 :     = cpp_display_width (line.get_buffer (), line_bytes);
    1398                 :      91929 :   if (caret_display_column > eol_display_column
    1399                 :      91929 :       || !caret_display_column)
    1400                 :            :     {
    1401                 :            :       /* This does not make sense, so don't try to do anything in this case.  */
    1402                 :            :       return;
    1403                 :            :     }
    1404                 :            : 
    1405                 :            :   /* Adjust caret and eol positions to include the left margin.  If we are
    1406                 :            :      outputting line numbers, then the left margin is equal to m_linenum_width
    1407                 :            :      plus three for the " | " which follows it.  Otherwise the left margin width
    1408                 :            :      is equal to 1, because layout::print_source_line() will prefix each line
    1409                 :            :      with a space.  */
    1410                 :      19167 :   const int source_display_cols = eol_display_column;
    1411                 :      19167 :   int left_margin_size = 1;
    1412                 :      19167 :   if (m_show_line_numbers_p)
    1413                 :      13585 :       left_margin_size = m_linenum_width + 3;
    1414                 :      19167 :   caret_display_column += left_margin_size;
    1415                 :      19167 :   eol_display_column += left_margin_size;
    1416                 :            : 
    1417                 :      19167 :   if (eol_display_column <= max_width)
    1418                 :            :     {
    1419                 :            :       /* Nothing to do, everything fits in the display.  */
    1420                 :            :       return;
    1421                 :            :     }
    1422                 :            : 
    1423                 :            :   /* The line is too long for the display.  Calculate an offset such that the
    1424                 :            :      caret is not too close to the right edge of the screen.  It will be
    1425                 :            :      CARET_LINE_MARGIN display columns from the right edge, unless it is closer
    1426                 :            :      than that to the end of the source line anyway.  */
    1427                 :        655 :   int right_margin_size = CARET_LINE_MARGIN;
    1428                 :        655 :   right_margin_size = MIN (eol_display_column - caret_display_column,
    1429                 :            :                            right_margin_size);
    1430                 :        655 :   if (right_margin_size + left_margin_size >= max_width)
    1431                 :            :     {
    1432                 :            :       /* The max_width is very small, so anything we try to do will not be very
    1433                 :            :          effective; just punt in this case and output with no offset.  */
    1434                 :            :       return;
    1435                 :            :     }
    1436                 :        591 :   const int max_caret_display_column = max_width - right_margin_size;
    1437                 :        591 :   if (caret_display_column > max_caret_display_column)
    1438                 :            :     {
    1439                 :        548 :       m_x_offset_display = caret_display_column - max_caret_display_column;
    1440                 :            :       /* Make sure we don't offset the line into oblivion.  */
    1441                 :        548 :       static const int min_cols_visible = 2;
    1442                 :        548 :       if (source_display_cols - m_x_offset_display < min_cols_visible)
    1443                 :          0 :         m_x_offset_display = 0;
    1444                 :            :     }
    1445                 :            : }
    1446                 :            : 
    1447                 :            : /* Print line ROW of source code, potentially colorized at any ranges, and
    1448                 :            :    populate *LBOUNDS_OUT.
    1449                 :            :    LINE is the source line (not necessarily 0-terminated) and LINE_BYTES
    1450                 :            :    is its length in bytes.
    1451                 :            :    This function deals only with byte offsets, not display columns, so
    1452                 :            :    m_x_offset_display must be converted from display to byte units.  In
    1453                 :            :    particular, LINE_BYTES and LBOUNDS_OUT are in bytes.  */
    1454                 :            : 
    1455                 :            : void
    1456                 :      90525 : layout::print_source_line (linenum_type row, const char *line, int line_bytes,
    1457                 :            :                            line_bounds *lbounds_out)
    1458                 :            : {
    1459                 :      90525 :   m_colorizer.set_normal_text ();
    1460                 :            : 
    1461                 :      90525 :   pp_emit_prefix (m_pp);
    1462                 :      90525 :   if (m_show_line_numbers_p)
    1463                 :            :     {
    1464                 :      15781 :       int width = num_digits (row);
    1465                 :      50107 :       for (int i = 0; i < m_linenum_width - width; i++)
    1466                 :      34326 :         pp_space (m_pp);
    1467                 :      15781 :       pp_printf (m_pp, "%i | ", row);
    1468                 :            :     }
    1469                 :            :   else
    1470                 :      74744 :     pp_space (m_pp);
    1471                 :            : 
    1472                 :            :   /* We will stop printing the source line at any trailing whitespace, and start
    1473                 :            :      printing it as per m_x_offset_display.  */
    1474                 :      90525 :   line_bytes = get_line_bytes_without_trailing_whitespace (line,
    1475                 :            :                                                            line_bytes);
    1476                 :      90525 :   int x_offset_bytes = 0;
    1477                 :      90525 :   if (m_x_offset_display)
    1478                 :            :     {
    1479                 :         68 :       x_offset_bytes = cpp_display_column_to_byte_column (line, line_bytes,
    1480                 :            :                                                           m_x_offset_display);
    1481                 :            :       /* In case the leading portion of the line that will be skipped over ends
    1482                 :            :          with a character with wcwidth > 1, then it is possible we skipped too
    1483                 :            :          much, so account for that by padding with spaces.  */
    1484                 :         68 :       const int overage
    1485                 :         68 :         = cpp_byte_column_to_display_column (line, line_bytes, x_offset_bytes)
    1486                 :         68 :         - m_x_offset_display;
    1487                 :        100 :       for (int column = 0; column < overage; ++column)
    1488                 :         32 :         pp_space (m_pp);
    1489                 :         68 :       line += x_offset_bytes;
    1490                 :            :     }
    1491                 :            : 
    1492                 :            :   /* Print the line.  */
    1493                 :      90525 :   int first_non_ws = INT_MAX;
    1494                 :      90525 :   int last_non_ws = 0;
    1495                 :     841072 :   for (int col_byte = 1 + x_offset_bytes; col_byte <= line_bytes; col_byte++)
    1496                 :            :     {
    1497                 :            :       /* Assuming colorization is enabled for the caret and underline
    1498                 :            :          characters, we may also colorize the associated characters
    1499                 :            :          within the source line.
    1500                 :            : 
    1501                 :            :          For frontends that generate range information, we color the
    1502                 :            :          associated characters in the source line the same as the
    1503                 :            :          carets and underlines in the annotation line, to make it easier
    1504                 :            :          for the reader to see the pertinent code.
    1505                 :            : 
    1506                 :            :          For frontends that only generate carets, we don't colorize the
    1507                 :            :          characters above them, since this would look strange (e.g.
    1508                 :            :          colorizing just the first character in a token).  */
    1509                 :     750547 :       if (m_colorize_source_p)
    1510                 :            :         {
    1511                 :     250875 :           bool in_range_p;
    1512                 :     250875 :           point_state state;
    1513                 :     250875 :           in_range_p = get_state_at_point (row, col_byte,
    1514                 :            :                                            0, INT_MAX,
    1515                 :            :                                            CU_BYTES,
    1516                 :            :                                            &state);
    1517                 :     250875 :           if (in_range_p)
    1518                 :      39060 :             m_colorizer.set_range (state.range_idx);
    1519                 :            :           else
    1520                 :     250875 :             m_colorizer.set_normal_text ();
    1521                 :            :         }
    1522                 :     750547 :       char c = *line;
    1523                 :     750547 :       if (c == '\0' || c == '\t' || c == '\r')
    1524                 :            :         c = ' ';
    1525                 :     750336 :       if (c != ' ')
    1526                 :            :         {
    1527                 :     621592 :           last_non_ws = col_byte;
    1528                 :     621592 :           if (first_non_ws == INT_MAX)
    1529                 :      20437 :             first_non_ws = col_byte;
    1530                 :            :         }
    1531                 :     750547 :       pp_character (m_pp, c);
    1532                 :     750547 :       line++;
    1533                 :            :     }
    1534                 :     181050 :   print_newline ();
    1535                 :            : 
    1536                 :      90525 :   lbounds_out->m_first_non_ws = first_non_ws;
    1537                 :      90525 :   lbounds_out->m_last_non_ws = last_non_ws;
    1538                 :      90525 : }
    1539                 :            : 
    1540                 :            : /* Determine if we should print an annotation line for ROW.
    1541                 :            :    i.e. if any of m_layout_ranges contains ROW.  */
    1542                 :            : 
    1543                 :            : bool
    1544                 :      90525 : layout::should_print_annotation_line_p (linenum_type row) const
    1545                 :            : {
    1546                 :      90525 :   layout_range *range;
    1547                 :      90525 :   int i;
    1548                 :      91542 :   FOR_EACH_VEC_ELT (m_layout_ranges, i, range)
    1549                 :            :     {
    1550                 :      91247 :       if (range->m_range_display_kind == SHOW_LINES_WITHOUT_RANGE)
    1551                 :            :         return false;
    1552                 :      91963 :       if (range->intersects_line_p (row))
    1553                 :            :         return true;
    1554                 :            :     }
    1555                 :            :   return false;
    1556                 :            : }
    1557                 :            : 
    1558                 :            : /* Begin an annotation line.  If m_show_line_numbers_p, print the left
    1559                 :            :    margin, which is empty for annotation lines.  Otherwise, do nothing.  */
    1560                 :            : 
    1561                 :            : void
    1562                 :     112142 : layout::start_annotation_line (char margin_char) const
    1563                 :            : {
    1564                 :     112142 :   pp_emit_prefix (m_pp);
    1565                 :     112142 :   if (m_show_line_numbers_p)
    1566                 :            :     {
    1567                 :            :       /* Print the margin.  If MARGIN_CHAR != ' ', then print up to 3
    1568                 :            :          of it, right-aligned, padded with spaces.  */
    1569                 :            :       int i;
    1570                 :      49051 :       for (i = 0; i < m_linenum_width - 3; i++)
    1571                 :      32598 :         pp_space (m_pp);
    1572                 :      65786 :       for (; i < m_linenum_width; i++)
    1573                 :      49333 :         pp_character (m_pp, margin_char);
    1574                 :      16453 :       pp_string (m_pp, " |");
    1575                 :            :     }
    1576                 :     112142 : }
    1577                 :            : 
    1578                 :            : /* Print a line consisting of the caret/underlines for the given
    1579                 :            :    source line.  This function works with display columns, rather than byte
    1580                 :            :    counts; in particular, LBOUNDS should be in display column units.  */
    1581                 :            : 
    1582                 :            : void
    1583                 :      89929 : layout::print_annotation_line (linenum_type row, const line_bounds lbounds)
    1584                 :            : {
    1585                 :      89929 :   int x_bound = get_x_bound_for_row (row, m_exploc.m_display_col,
    1586                 :            :                                      lbounds.m_last_non_ws);
    1587                 :            : 
    1588                 :      89929 :   start_annotation_line ();
    1589                 :      89929 :   pp_space (m_pp);
    1590                 :            : 
    1591                 :   16523500 :   for (int column = 1 + m_x_offset_display; column < x_bound; column++)
    1592                 :            :     {
    1593                 :   16433600 :       bool in_range_p;
    1594                 :   16433600 :       point_state state;
    1595                 :   32867100 :       in_range_p = get_state_at_point (row, column,
    1596                 :   16433600 :                                        lbounds.m_first_non_ws,
    1597                 :            :                                        lbounds.m_last_non_ws,
    1598                 :            :                                        CU_DISPLAY_COLS,
    1599                 :            :                                        &state);
    1600                 :   16433600 :       if (in_range_p)
    1601                 :            :         {
    1602                 :            :           /* Within a range.  Draw either the caret or an underline.  */
    1603                 :      83133 :           m_colorizer.set_range (state.range_idx);
    1604                 :      83133 :           if (state.draw_caret_p)
    1605                 :            :             {
    1606                 :            :               /* Draw the caret.  */
    1607                 :      34344 :               char caret_char;
    1608                 :      34344 :               if (state.range_idx < rich_location::STATICALLY_ALLOCATED_RANGES)
    1609                 :      34277 :                 caret_char = m_context->caret_chars[state.range_idx];
    1610                 :            :               else
    1611                 :            :                 caret_char = '^';
    1612                 :      34344 :               pp_character (m_pp, caret_char);
    1613                 :            :             }
    1614                 :            :           else
    1615                 :      48789 :             pp_character (m_pp, '~');
    1616                 :            :         }
    1617                 :            :       else
    1618                 :            :         {
    1619                 :            :           /* Not in a range.  */
    1620                 :   16350400 :           m_colorizer.set_normal_text ();
    1621                 :   16350400 :           pp_character (m_pp, ' ');
    1622                 :            :         }
    1623                 :            :     }
    1624                 :     179858 :   print_newline ();
    1625                 :      89929 : }
    1626                 :            : 
    1627                 :            : /* Implementation detail of layout::print_any_labels.
    1628                 :            : 
    1629                 :            :    A label within the given row of source.  */
    1630                 :            : 
    1631                 :            : class line_label
    1632                 :            : {
    1633                 :            : public:
    1634                 :       2129 :   line_label (int state_idx, int column, label_text text)
    1635                 :       2129 :   : m_state_idx (state_idx), m_column (column),
    1636                 :       2129 :     m_text (text), m_label_line (0), m_has_vbar (true)
    1637                 :            :   {
    1638                 :       2129 :     const int bytes = strlen (text.m_buffer);
    1639                 :       2129 :     m_display_width = cpp_display_width (text.m_buffer, bytes);
    1640                 :       2129 :   }
    1641                 :            : 
    1642                 :            :   /* Sorting is primarily by column, then by state index.  */
    1643                 :       7733 :   static int comparator (const void *p1, const void *p2)
    1644                 :            :   {
    1645                 :       7733 :     const line_label *ll1 = (const line_label *)p1;
    1646                 :       7733 :     const line_label *ll2 = (const line_label *)p2;
    1647                 :       7733 :     int column_cmp = compare (ll1->m_column, ll2->m_column);
    1648                 :       3091 :     if (column_cmp)
    1649                 :       6289 :       return column_cmp;
    1650                 :            :     /* Order by reverse state index, so that labels are printed
    1651                 :            :        in order of insertion into the rich_location when the
    1652                 :            :        sorted list is walked backwards.  */
    1653                 :       2888 :     return -compare (ll1->m_state_idx, ll2->m_state_idx);
    1654                 :            :   }
    1655                 :            : 
    1656                 :            :   int m_state_idx;
    1657                 :            :   int m_column;
    1658                 :            :   label_text m_text;
    1659                 :            :   size_t m_display_width;
    1660                 :            :   int m_label_line;
    1661                 :            :   bool m_has_vbar;
    1662                 :            : };
    1663                 :            : 
    1664                 :            : /* Print any labels in this row.  */
    1665                 :            : void
    1666                 :      90472 : layout::print_any_labels (linenum_type row)
    1667                 :            : {
    1668                 :      90472 :   int i;
    1669                 :      91540 :   auto_vec<line_label> labels;
    1670                 :            : 
    1671                 :            :   /* Gather the labels that are to be printed into "labels".  */
    1672                 :      90472 :   {
    1673                 :      90472 :     layout_range *range;
    1674                 :     183827 :     FOR_EACH_VEC_ELT (m_layout_ranges, i, range)
    1675                 :            :       {
    1676                 :            :         /* Most ranges don't have labels, so reject this first.  */
    1677                 :      93355 :         if (range->m_label == NULL)
    1678                 :      91226 :           continue;
    1679                 :            : 
    1680                 :            :         /* The range's caret must be on this line.  */
    1681                 :       3547 :         if (range->m_caret.m_line != row)
    1682                 :       1384 :           continue;
    1683                 :            : 
    1684                 :            :         /* Reject labels that aren't fully visible due to clipping
    1685                 :            :            by m_x_offset_display.  */
    1686                 :       2163 :         const int disp_col = range->m_caret.m_columns[CU_DISPLAY_COLS];
    1687                 :       2163 :         if (disp_col <= m_x_offset_display)
    1688                 :          2 :           continue;
    1689                 :            : 
    1690                 :       2161 :         label_text text;
    1691                 :       2161 :         text = range->m_label->get_text (range->m_original_idx);
    1692                 :            : 
    1693                 :            :         /* Allow for labels that return NULL from their get_text
    1694                 :            :            implementation (so e.g. such labels can control their own
    1695                 :            :            visibility).  */
    1696                 :       2161 :         if (text.m_buffer == NULL)
    1697                 :         32 :           continue;
    1698                 :            : 
    1699                 :       2129 :         labels.safe_push (line_label (i, disp_col, text));
    1700                 :            :       }
    1701                 :            :   }
    1702                 :            : 
    1703                 :            :   /* Bail out if there are no labels on this row.  */
    1704                 :      90472 :   if (labels.length () == 0)
    1705                 :      89404 :     return;
    1706                 :            : 
    1707                 :            :   /* Sort them.  */
    1708                 :       1068 :   labels.qsort(line_label::comparator);
    1709                 :            : 
    1710                 :            :   /* Figure out how many "label lines" we need, and which
    1711                 :            :      one each label is printed in.
    1712                 :            : 
    1713                 :            :      For example, if the labels aren't too densely packed,
    1714                 :            :      we can fit them on the same line, giving two "label lines":
    1715                 :            : 
    1716                 :            :        foo + bar
    1717                 :            :        ~~~   ~~~
    1718                 :            :        |     |        : label line 0
    1719                 :            :        l0    l1       : label line 1
    1720                 :            : 
    1721                 :            :      If they would touch each other or overlap, then we need
    1722                 :            :      additional "label lines":
    1723                 :            : 
    1724                 :            :        foo + bar
    1725                 :            :        ~~~   ~~~
    1726                 :            :        |     |             : label line 0
    1727                 :            :        |     label 1       : label line 1
    1728                 :            :        label 0             : label line 2
    1729                 :            : 
    1730                 :            :      Place the final label on label line 1, and work backwards, adding
    1731                 :            :      label lines as needed.
    1732                 :            : 
    1733                 :            :      If multiple labels are at the same place, put them on separate
    1734                 :            :      label lines:
    1735                 :            : 
    1736                 :            :        foo + bar
    1737                 :            :            ^               : label line 0
    1738                 :            :            |               : label line 1
    1739                 :            :            label 0         : label line 2
    1740                 :            :            label 1         : label line 3.  */
    1741                 :            : 
    1742                 :       1068 :   int max_label_line = 1;
    1743                 :       1068 :   {
    1744                 :       1068 :     int next_column = INT_MAX;
    1745                 :       1068 :     line_label *label;
    1746                 :       5333 :     FOR_EACH_VEC_ELT_REVERSE (labels, i, label)
    1747                 :            :       {
    1748                 :            :         /* Would this label "touch" or overlap the next label?  */
    1749                 :       2129 :         if (label->m_column + label->m_display_width >= (size_t)next_column)
    1750                 :            :           {
    1751                 :        654 :             max_label_line++;
    1752                 :            : 
    1753                 :            :             /* If we've already seen labels with the same column, suppress the
    1754                 :            :                vertical bar for subsequent ones in this backwards iteration;
    1755                 :            :                hence only the one with the highest label_line has m_has_vbar set.  */
    1756                 :        654 :             if (label->m_column == next_column)
    1757                 :        273 :               label->m_has_vbar = false;
    1758                 :            :           }
    1759                 :            : 
    1760                 :       2129 :         label->m_label_line = max_label_line;
    1761                 :       2129 :         next_column = label->m_column;
    1762                 :            :       }
    1763                 :            :   }
    1764                 :            : 
    1765                 :            :   /* Print the "label lines".  For each label within the line, print
    1766                 :            :      either a vertical bar ('|') for the labels that are lower down, or the
    1767                 :            :      labels themselves once we've reached their line.  */
    1768                 :            :   {
    1769                 :       3858 :     for (int label_line = 0; label_line <= max_label_line; label_line++)
    1770                 :            :       {
    1771                 :       2790 :         start_annotation_line ();
    1772                 :       2790 :         pp_space (m_pp);
    1773                 :       2790 :         int column = 1 + m_x_offset_display;
    1774                 :       2790 :         line_label *label;
    1775                 :       8845 :         FOR_EACH_VEC_ELT (labels, i, label)
    1776                 :            :           {
    1777                 :       6709 :             if (label_line > label->m_label_line)
    1778                 :            :               /* We've printed all the labels for this label line.  */
    1779                 :            :               break;
    1780                 :            : 
    1781                 :       6055 :             if (label_line == label->m_label_line)
    1782                 :            :               {
    1783                 :       2129 :                 gcc_assert (column <= label->m_column);
    1784                 :       2129 :                 move_to_column (&column, label->m_column, true);
    1785                 :            :                 /* Colorize the text, unless it's for events in a
    1786                 :            :                    diagnostic_path.  */
    1787                 :       2129 :                 if (!m_diagnostic_path_p)
    1788                 :       1800 :                   m_colorizer.set_range (label->m_state_idx);
    1789                 :       2129 :                 pp_string (m_pp, label->m_text.m_buffer);
    1790                 :       2129 :                 m_colorizer.set_normal_text ();
    1791                 :       2129 :                 column += label->m_display_width;
    1792                 :            :               }
    1793                 :       3926 :             else if (label->m_has_vbar)
    1794                 :            :               {
    1795                 :       2676 :                 gcc_assert (column <= label->m_column);
    1796                 :       2676 :                 move_to_column (&column, label->m_column, true);
    1797                 :       2676 :                 m_colorizer.set_range (label->m_state_idx);
    1798                 :       2676 :                 pp_character (m_pp, '|');
    1799                 :       2676 :                 m_colorizer.set_normal_text ();
    1800                 :       2676 :                 column++;
    1801                 :            :               }
    1802                 :            :           }
    1803                 :       2790 :         print_newline ();
    1804                 :            :       }
    1805                 :            :     }
    1806                 :            : 
    1807                 :            :   /* Clean up.  */
    1808                 :            :   {
    1809                 :            :     line_label *label;
    1810                 :       4265 :     FOR_EACH_VEC_ELT (labels, i, label)
    1811                 :       2129 :       label->m_text.maybe_free ();
    1812                 :            :   }
    1813                 :            : }
    1814                 :            : 
    1815                 :            : /* If there are any fixit hints inserting new lines before source line ROW,
    1816                 :            :    print them.
    1817                 :            : 
    1818                 :            :    They are printed on lines of their own, before the source line
    1819                 :            :    itself, with a leading '+'.  */
    1820                 :            : 
    1821                 :            : void
    1822                 :      90525 : layout::print_leading_fixits (linenum_type row)
    1823                 :            : {
    1824                 :     146150 :   for (unsigned int i = 0; i < m_fixit_hints.length (); i++)
    1825                 :            :     {
    1826                 :      19513 :       const fixit_hint *hint = m_fixit_hints[i];
    1827                 :            : 
    1828                 :      19513 :       if (!hint->ends_with_newline_p ())
    1829                 :            :         /* Not a newline fixit; print it in print_trailing_fixits.  */
    1830                 :      18914 :         continue;
    1831                 :            : 
    1832                 :        599 :       gcc_assert (hint->insertion_p ());
    1833                 :            : 
    1834                 :        599 :       if (hint->affects_line_p (m_exploc.file, row))
    1835                 :            :         {
    1836                 :            :           /* Printing the '+' with normal colorization
    1837                 :            :              and the inserted line with "insert" colorization
    1838                 :            :              helps them stand out from each other, and from
    1839                 :            :              the surrounding text.  */
    1840                 :        291 :           m_colorizer.set_normal_text ();
    1841                 :        291 :           start_annotation_line ('+');
    1842                 :        291 :           pp_character (m_pp, '+');
    1843                 :        291 :           m_colorizer.set_fixit_insert ();
    1844                 :            :           /* Print all but the trailing newline of the fix-it hint.
    1845                 :            :              We have to print the newline separately to avoid
    1846                 :            :              getting additional pp prefixes printed.  */
    1847                 :       4993 :           for (size_t i = 0; i < hint->get_length () - 1; i++)
    1848                 :       4702 :             pp_character (m_pp, hint->get_string ()[i]);
    1849                 :        291 :           m_colorizer.set_normal_text ();
    1850                 :        291 :           pp_newline (m_pp);
    1851                 :            :         }
    1852                 :            :     }
    1853                 :      90525 : }
    1854                 :            : 
    1855                 :            : /* Subroutine of layout::print_trailing_fixits.
    1856                 :            : 
    1857                 :            :    Determine if the annotation line printed for LINE contained
    1858                 :            :    the exact range from START_COLUMN to FINISH_COLUMN (in display units).  */
    1859                 :            : 
    1860                 :            : bool
    1861                 :       8260 : layout::annotation_line_showed_range_p (linenum_type line, int start_column,
    1862                 :            :                                         int finish_column) const
    1863                 :            : {
    1864                 :       8260 :   layout_range *range;
    1865                 :       8260 :   int i;
    1866                 :      15670 :   FOR_EACH_VEC_ELT (m_layout_ranges, i, range)
    1867                 :      10739 :     if (range->m_start.m_line == line
    1868                 :       8597 :         && range->m_start.m_columns[CU_DISPLAY_COLS] == start_column
    1869                 :       7727 :         && range->m_finish.m_line == line
    1870                 :       7727 :         && range->m_finish.m_columns[CU_DISPLAY_COLS] == finish_column)
    1871                 :            :       return true;
    1872                 :            :   return false;
    1873                 :            : }
    1874                 :            : 
    1875                 :            : /* Classes for printing trailing fix-it hints i.e. those that
    1876                 :            :    don't add new lines.
    1877                 :            : 
    1878                 :            :    For insertion, these can look like:
    1879                 :            : 
    1880                 :            :      new_text
    1881                 :            : 
    1882                 :            :    For replacement, these can look like:
    1883                 :            : 
    1884                 :            :      ------------- : underline showing affected range
    1885                 :            :      new_text
    1886                 :            : 
    1887                 :            :    For deletion, these can look like:
    1888                 :            : 
    1889                 :            :      ------------- : underline showing affected range
    1890                 :            : 
    1891                 :            :    This can become confusing if they overlap, and so we need
    1892                 :            :    to do some preprocessing to decide what to print.
    1893                 :            :    We use the list of fixit_hint instances affecting the line
    1894                 :            :    to build a list of "correction" instances, and print the
    1895                 :            :    latter.
    1896                 :            : 
    1897                 :            :    For example, consider a set of fix-its for converting
    1898                 :            :    a C-style cast to a C++ const_cast.
    1899                 :            : 
    1900                 :            :    Given:
    1901                 :            : 
    1902                 :            :    ..000000000111111111122222222223333333333.
    1903                 :            :    ..123456789012345678901234567890123456789.
    1904                 :            :      foo *f = (foo *)ptr->field;
    1905                 :            :                           ^~~~~
    1906                 :            : 
    1907                 :            :    and the fix-it hints:
    1908                 :            :      - replace col 10 (the open paren) with "const_cast<"
    1909                 :            :      - replace col 16 (the close paren) with "> ("
    1910                 :            :      - insert ")" before col 27
    1911                 :            : 
    1912                 :            :    then we would get odd-looking output:
    1913                 :            : 
    1914                 :            :      foo *f = (foo *)ptr->field;
    1915                 :            :                           ^~~~~
    1916                 :            :               -
    1917                 :            :               const_cast<
    1918                 :            :                     -
    1919                 :            :                     > (        )
    1920                 :            : 
    1921                 :            :    It would be better to detect when fixit hints are going to
    1922                 :            :    overlap (those that require new lines), and to consolidate
    1923                 :            :    the printing of such fixits, giving something like:
    1924                 :            : 
    1925                 :            :      foo *f = (foo *)ptr->field;
    1926                 :            :                           ^~~~~
    1927                 :            :               -----------------
    1928                 :            :               const_cast<foo *> (ptr->field)
    1929                 :            : 
    1930                 :            :    This works by detecting when the printing would overlap, and
    1931                 :            :    effectively injecting no-op replace hints into the gaps between
    1932                 :            :    such fix-its, so that the printing joins up.
    1933                 :            : 
    1934                 :            :    In the above example, the overlap of:
    1935                 :            :      - replace col 10 (the open paren) with "const_cast<"
    1936                 :            :    and:
    1937                 :            :      - replace col 16 (the close paren) with "> ("
    1938                 :            :    is fixed by injecting a no-op:
    1939                 :            :      - replace cols 11-15 with themselves ("foo *")
    1940                 :            :    and consolidating these, making:
    1941                 :            :      - replace cols 10-16 with "const_cast<" + "foo *" + "> ("
    1942                 :            :    i.e.:
    1943                 :            :      - replace cols 10-16 with "const_cast<foo *> ("
    1944                 :            : 
    1945                 :            :    This overlaps with the final fix-it hint:
    1946                 :            :      - insert ")" before col 27
    1947                 :            :    and so we repeat the consolidation process, by injecting
    1948                 :            :    a no-op:
    1949                 :            :      - replace cols 17-26 with themselves ("ptr->field")
    1950                 :            :    giving:
    1951                 :            :      - replace cols 10-26 with "const_cast<foo *> (" + "ptr->field" + ")"
    1952                 :            :    i.e.:
    1953                 :            :      - replace cols 10-26 with "const_cast<foo *> (ptr->field)"
    1954                 :            : 
    1955                 :            :    and is thus printed as desired.  */
    1956                 :            : 
    1957                 :            : /* A range of (byte or display) columns within a line.  */
    1958                 :            : 
    1959                 :            : class column_range
    1960                 :            : {
    1961                 :            : public:
    1962                 :      57077 :   column_range (int start_, int finish_) : start (start_), finish (finish_)
    1963                 :            :   {
    1964                 :            :     /* We must have either a range, or an insertion.  */
    1965                 :      20616 :     gcc_assert (start <= finish || finish == start - 1);
    1966                 :            :   }
    1967                 :            : 
    1968                 :       1128 :   bool operator== (const column_range &other) const
    1969                 :            :   {
    1970                 :       1128 :     return start == other.start && finish == other.finish;
    1971                 :            :   }
    1972                 :            : 
    1973                 :            :   int start;
    1974                 :            :   int finish;
    1975                 :            : };
    1976                 :            : 
    1977                 :            : /* Get the range of bytes or display columns that HINT would affect.  */
    1978                 :            : static column_range
    1979                 :      36884 : get_affected_range (const fixit_hint *hint, enum column_unit col_unit)
    1980                 :            : {
    1981                 :      36884 :   expanded_location exploc_start = expand_location (hint->get_start_loc ());
    1982                 :      36884 :   expanded_location exploc_finish = expand_location (hint->get_next_loc ());
    1983                 :      36884 :   --exploc_finish.column;
    1984                 :            : 
    1985                 :      36884 :   int start_column;
    1986                 :      36884 :   int finish_column;
    1987                 :      36884 :   if (col_unit == CU_DISPLAY_COLS)
    1988                 :            :     {
    1989                 :      18412 :       start_column = location_compute_display_column (exploc_start);
    1990                 :      18412 :       if (hint->insertion_p ())
    1991                 :       9939 :         finish_column = start_column - 1;
    1992                 :            :       else
    1993                 :       8473 :         finish_column = location_compute_display_column (exploc_finish);
    1994                 :            :     }
    1995                 :            :   else
    1996                 :            :     {
    1997                 :      18472 :       start_column = exploc_start.column;
    1998                 :      18472 :       finish_column = exploc_finish.column;
    1999                 :            :     }
    2000                 :      36884 :   return column_range (start_column, finish_column);
    2001                 :            : }
    2002                 :            : 
    2003                 :            : /* Get the range of display columns that would be printed for HINT.  */
    2004                 :            : 
    2005                 :            : static column_range
    2006                 :      18472 : get_printed_columns (const fixit_hint *hint)
    2007                 :            : {
    2008                 :      18472 :   expanded_location exploc = expand_location (hint->get_start_loc ());
    2009                 :      18472 :   int start_column = location_compute_display_column (exploc);
    2010                 :      36944 :   int hint_width = cpp_display_width (hint->get_string (),
    2011                 :      18472 :                                       hint->get_length ());
    2012                 :      18472 :   int final_hint_column = start_column + hint_width - 1;
    2013                 :      18472 :   if (hint->insertion_p ())
    2014                 :            :     {
    2015                 :      10553 :       return column_range (start_column, final_hint_column);
    2016                 :            :     }
    2017                 :            :   else
    2018                 :            :     {
    2019                 :       8473 :       exploc = expand_location (hint->get_next_loc ());
    2020                 :       8473 :       --exploc.column;
    2021                 :       8473 :       int finish_column = location_compute_display_column (exploc);
    2022                 :       8473 :       return column_range (start_column,
    2023                 :       8473 :                            MAX (finish_column, final_hint_column));
    2024                 :            :     }
    2025                 :            : }
    2026                 :            : 
    2027                 :            : /* A correction on a particular line.
    2028                 :            :    This describes a plan for how to print one or more fixit_hint
    2029                 :            :    instances that affected the line, potentially consolidating hints
    2030                 :            :    into corrections to make the result easier for the user to read.  */
    2031                 :            : 
    2032                 :            : class correction
    2033                 :            : {
    2034                 :            : public:
    2035                 :      17651 :   correction (column_range affected_bytes,
    2036                 :            :               column_range affected_columns,
    2037                 :            :               column_range printed_columns,
    2038                 :            :               const char *new_text, size_t new_text_len)
    2039                 :      17651 :   : m_affected_bytes (affected_bytes),
    2040                 :            :     m_affected_columns (affected_columns),
    2041                 :            :     m_printed_columns (printed_columns),
    2042                 :      17651 :     m_text (xstrdup (new_text)),
    2043                 :            :     m_byte_length (new_text_len),
    2044                 :      17651 :     m_alloc_sz (new_text_len + 1)
    2045                 :            :   {
    2046                 :      35302 :     compute_display_cols ();
    2047                 :      17651 :   }
    2048                 :            : 
    2049                 :      17651 :   ~correction () { free (m_text); }
    2050                 :            : 
    2051                 :      17595 :   bool insertion_p () const
    2052                 :            :   {
    2053                 :      17595 :     return m_affected_bytes.start == m_affected_bytes.finish + 1;
    2054                 :            :   }
    2055                 :            : 
    2056                 :            :   void ensure_capacity (size_t len);
    2057                 :            :   void ensure_terminated ();
    2058                 :            : 
    2059                 :      18244 :   void compute_display_cols ()
    2060                 :            :   {
    2061                 :      17651 :     m_display_cols = cpp_display_width (m_text, m_byte_length);
    2062                 :            :   }
    2063                 :            : 
    2064                 :            :   void overwrite (int dst_offset, const char_span &src_span)
    2065                 :            :   {
    2066                 :            :     gcc_assert (dst_offset >= 0);
    2067                 :            :     gcc_assert (dst_offset + src_span.length () < m_alloc_sz);
    2068                 :            :     memcpy (m_text + dst_offset, src_span.get_buffer (),
    2069                 :            :             src_span.length ());
    2070                 :            :   }
    2071                 :            : 
    2072                 :            :   /* If insert, then start: the column before which the text
    2073                 :            :      is to be inserted, and finish is offset by the length of
    2074                 :            :      the replacement.
    2075                 :            :      If replace, then the range of columns affected.  */
    2076                 :            :   column_range m_affected_bytes;
    2077                 :            :   column_range m_affected_columns;
    2078                 :            : 
    2079                 :            :   /* If insert, then start: the column before which the text
    2080                 :            :      is to be inserted, and finish is offset by the length of
    2081                 :            :      the replacement.
    2082                 :            :      If replace, then the range of columns affected.  */
    2083                 :            :   column_range m_printed_columns;
    2084                 :            : 
    2085                 :            :   /* The text to be inserted/used as replacement.  */
    2086                 :            :   char *m_text;
    2087                 :            :   size_t m_byte_length; /* Not including null-terminator.  */
    2088                 :            :   int m_display_cols;
    2089                 :            :   size_t m_alloc_sz;
    2090                 :            : };
    2091                 :            : 
    2092                 :            : /* Ensure that m_text can hold a string of length LEN
    2093                 :            :    (plus 1 for 0-termination).  */
    2094                 :            : 
    2095                 :            : void
    2096                 :        593 : correction::ensure_capacity (size_t len)
    2097                 :            : {
    2098                 :            :   /* Allow 1 extra byte for 0-termination.  */
    2099                 :          0 :   if (m_alloc_sz < (len + 1))
    2100                 :            :     {
    2101                 :        306 :       size_t new_alloc_sz = (len + 1) * 2;
    2102                 :        306 :       m_text = (char *)xrealloc (m_text, new_alloc_sz);
    2103                 :        306 :       m_alloc_sz = new_alloc_sz;
    2104                 :            :     }
    2105                 :          0 : }
    2106                 :            : 
    2107                 :            : /* Ensure that m_text is 0-terminated.  */
    2108                 :            : 
    2109                 :            : void
    2110                 :        593 : correction::ensure_terminated ()
    2111                 :            : {
    2112                 :            :   /* 0-terminate the buffer.  */
    2113                 :        593 :   gcc_assert (m_byte_length < m_alloc_sz);
    2114                 :        593 :   m_text[m_byte_length] = '\0';
    2115                 :        593 : }
    2116                 :            : 
    2117                 :            : /* A list of corrections affecting a particular line.
    2118                 :            :    This is used by layout::print_trailing_fixits for planning
    2119                 :            :    how to print the fix-it hints affecting the line.  */
    2120                 :            : 
    2121                 :            : class line_corrections
    2122                 :            : {
    2123                 :            : public:
    2124                 :      90581 :   line_corrections (const char *filename, linenum_type row)
    2125                 :         56 :   : m_filename (filename), m_row (row)
    2126                 :            :   {}
    2127                 :            :   ~line_corrections ();
    2128                 :            : 
    2129                 :            :   void add_hint (const fixit_hint *hint);
    2130                 :            : 
    2131                 :            :   const char *m_filename;
    2132                 :            :   linenum_type m_row;
    2133                 :            :   auto_vec <correction *> m_corrections;
    2134                 :            : };
    2135                 :            : 
    2136                 :            : /* struct line_corrections.  */
    2137                 :            : 
    2138                 :     271743 : line_corrections::~line_corrections ()
    2139                 :            : {
    2140                 :      90581 :   unsigned i;
    2141                 :      90581 :   correction *c;
    2142                 :     124245 :   FOR_EACH_VEC_ELT (m_corrections, i, c)
    2143                 :      17651 :     delete c;
    2144                 :      90581 : }
    2145                 :            : 
    2146                 :            : /* A struct wrapping a particular source line, allowing
    2147                 :            :    run-time bounds-checking of accesses in a checked build.  */
    2148                 :            : 
    2149                 :            : class source_line
    2150                 :            : {
    2151                 :            : public:
    2152                 :            :   source_line (const char *filename, int line);
    2153                 :            : 
    2154                 :        593 :   char_span as_span () { return char_span (chars, width); }
    2155                 :            : 
    2156                 :            :   const char *chars;
    2157                 :            :   int width;
    2158                 :            : };
    2159                 :            : 
    2160                 :            : /* source_line's ctor.  */
    2161                 :            : 
    2162                 :        593 : source_line::source_line (const char *filename, int line)
    2163                 :            : {
    2164                 :          0 :   char_span span = location_get_source_line (filename, line);
    2165                 :        593 :   chars = span.get_buffer ();
    2166                 :        593 :   width = span.length ();
    2167                 :          0 : }
    2168                 :            : 
    2169                 :            : /* Add HINT to the corrections for this line.
    2170                 :            :    Attempt to consolidate nearby hints so that they will not
    2171                 :            :    overlap with printed.  */
    2172                 :            : 
    2173                 :            : void
    2174                 :      18244 : line_corrections::add_hint (const fixit_hint *hint)
    2175                 :            : {
    2176                 :      18244 :   column_range affected_bytes = get_affected_range (hint, CU_BYTES);
    2177                 :      18244 :   column_range affected_columns = get_affected_range (hint, CU_DISPLAY_COLS);
    2178                 :      18244 :   column_range printed_columns = get_printed_columns (hint);
    2179                 :            : 
    2180                 :            :   /* Potentially consolidate.  */
    2181                 :      18244 :   if (!m_corrections.is_empty ())
    2182                 :            :     {
    2183                 :       2231 :       correction *last_correction
    2184                 :       2231 :         = m_corrections[m_corrections.length () - 1];
    2185                 :            : 
    2186                 :            :       /* The following consolidation code assumes that the fix-it hints
    2187                 :            :          have been sorted by start (done within layout's ctor).  */
    2188                 :       2231 :       gcc_assert (affected_bytes.start
    2189                 :            :                   >= last_correction->m_affected_bytes.start);
    2190                 :       2231 :       gcc_assert (printed_columns.start
    2191                 :            :                   >= last_correction->m_printed_columns.start);
    2192                 :            : 
    2193                 :       2231 :       if (printed_columns.start <= last_correction->m_printed_columns.finish)
    2194                 :            :         {
    2195                 :            :           /* We have two hints for which the printed forms of the hints
    2196                 :            :              would touch or overlap, so we need to consolidate them to avoid
    2197                 :            :              confusing the user.
    2198                 :            :              Attempt to inject a "replace" correction from immediately
    2199                 :            :              after the end of the last hint to immediately before the start
    2200                 :            :              of the next hint.  */
    2201                 :        593 :           column_range between (last_correction->m_affected_bytes.finish + 1,
    2202                 :        593 :                                 affected_bytes.start - 1);
    2203                 :            : 
    2204                 :            :           /* Try to read the source.  */
    2205                 :        593 :           source_line line (m_filename, m_row);
    2206                 :        593 :           if (line.chars && between.finish < line.width)
    2207                 :            :             {
    2208                 :            :               /* Consolidate into the last correction:
    2209                 :            :                  add a no-op "replace" of the "between" text, and
    2210                 :            :                  add the text from the new hint.  */
    2211                 :        593 :               int old_byte_len = last_correction->m_byte_length;
    2212                 :        593 :               gcc_assert (old_byte_len >= 0);
    2213                 :        593 :               int between_byte_len = between.finish + 1 - between.start;
    2214                 :        593 :               gcc_assert (between_byte_len >= 0);
    2215                 :        593 :               int new_byte_len
    2216                 :        593 :                 = old_byte_len + between_byte_len + hint->get_length ();
    2217                 :        593 :               gcc_assert (new_byte_len >= 0);
    2218                 :        593 :               last_correction->ensure_capacity (new_byte_len);
    2219                 :        593 :               last_correction->overwrite
    2220                 :        593 :                 (old_byte_len,
    2221                 :        593 :                  line.as_span ().subspan (between.start - 1,
    2222                 :        593 :                                           between.finish + 1 - between.start));
    2223                 :        593 :               last_correction->overwrite (old_byte_len + between_byte_len,
    2224                 :        593 :                                           char_span (hint->get_string (),
    2225                 :            :                                                      hint->get_length ()));
    2226                 :        593 :               last_correction->m_byte_length = new_byte_len;
    2227                 :        593 :               last_correction->ensure_terminated ();
    2228                 :        593 :               last_correction->m_affected_bytes.finish
    2229                 :        593 :                 = affected_bytes.finish;
    2230                 :        593 :               last_correction->m_affected_columns.finish
    2231                 :        593 :                 = affected_columns.finish;
    2232                 :        593 :               int prev_display_cols = last_correction->m_display_cols;
    2233                 :        593 :               last_correction->compute_display_cols ();
    2234                 :        593 :               last_correction->m_printed_columns.finish
    2235                 :        593 :                 += last_correction->m_display_cols - prev_display_cols;
    2236                 :        593 :               return;
    2237                 :            :             }
    2238                 :            :         }
    2239                 :            :     }
    2240                 :            : 
    2241                 :            :   /* If no consolidation happened, add a new correction instance.  */
    2242                 :      17651 :   m_corrections.safe_push (new correction (affected_bytes,
    2243                 :            :                                            affected_columns,
    2244                 :            :                                            printed_columns,
    2245                 :      17651 :                                            hint->get_string (),
    2246                 :      17651 :                                            hint->get_length ()));
    2247                 :            : }
    2248                 :            : 
    2249                 :            : /* If there are any fixit hints on source line ROW, print them.
    2250                 :            :    They are printed in order, attempting to combine them onto lines, but
    2251                 :            :    starting new lines if necessary.
    2252                 :            :    Fix-it hints that insert new lines are handled separately,
    2253                 :            :    in layout::print_leading_fixits.  */
    2254                 :            : 
    2255                 :            : void
    2256                 :      90525 : layout::print_trailing_fixits (linenum_type row)
    2257                 :            : {
    2258                 :            :   /* Build a list of correction instances for the line,
    2259                 :            :      potentially consolidating hints (for the sake of readability).  */
    2260                 :     181050 :   line_corrections corrections (m_exploc.file, row);
    2261                 :     146150 :   for (unsigned int i = 0; i < m_fixit_hints.length (); i++)
    2262                 :            :     {
    2263                 :      19513 :       const fixit_hint *hint = m_fixit_hints[i];
    2264                 :            : 
    2265                 :            :       /* Newline fixits are handled by layout::print_leading_fixits.  */
    2266                 :      19513 :       if (hint->ends_with_newline_p ())
    2267                 :        599 :         continue;
    2268                 :            : 
    2269                 :      18914 :       if (hint->affects_line_p (m_exploc.file, row))
    2270                 :      18076 :         corrections.add_hint (hint);
    2271                 :            :     }
    2272                 :            : 
    2273                 :            :   /* Now print the corrections.  */
    2274                 :      90525 :   unsigned i;
    2275                 :      90525 :   correction *c;
    2276                 :      90525 :   int column = m_x_offset_display;
    2277                 :            : 
    2278                 :      90525 :   if (!corrections.m_corrections.is_empty ())
    2279                 :      15957 :     start_annotation_line ();
    2280                 :            : 
    2281                 :     108120 :   FOR_EACH_VEC_ELT (corrections.m_corrections, i, c)
    2282                 :            :     {
    2283                 :            :       /* For now we assume each fixit hint can only touch one line.  */
    2284                 :      17595 :       if (c->insertion_p ())
    2285                 :            :         {
    2286                 :            :           /* This assumes the insertion just affects one line.  */
    2287                 :       9335 :           int start_column = c->m_printed_columns.start;
    2288                 :       9335 :           move_to_column (&column, start_column, true);
    2289                 :       9335 :           m_colorizer.set_fixit_insert ();
    2290                 :       9335 :           pp_string (m_pp, c->m_text);
    2291                 :       9335 :           m_colorizer.set_normal_text ();
    2292                 :       9335 :           column += c->m_display_cols;
    2293                 :            :         }
    2294                 :            :       else
    2295                 :            :         {
    2296                 :            :           /* If the range of the replacement wasn't printed in the
    2297                 :            :              annotation line, then print an extra underline to
    2298                 :            :              indicate exactly what is being replaced.
    2299                 :            :              Always show it for removals.  */
    2300                 :       8260 :           int start_column = c->m_affected_columns.start;
    2301                 :       8260 :           int finish_column = c->m_affected_columns.finish;
    2302                 :       8260 :           if (!annotation_line_showed_range_p (row, start_column,
    2303                 :            :                                                finish_column)
    2304                 :       8260 :               || c->m_byte_length == 0)
    2305                 :            :             {
    2306                 :       6299 :               move_to_column (&column, start_column, true);
    2307                 :       6299 :               m_colorizer.set_fixit_delete ();
    2308                 :    4161830 :               for (; column <= finish_column; column++)
    2309                 :    4155530 :                 pp_character (m_pp, '-');
    2310                 :       6299 :               m_colorizer.set_normal_text ();
    2311                 :            :             }
    2312                 :            :           /* Print the replacement text.  REPLACE also covers
    2313                 :            :              removals, so only do this extra work (potentially starting
    2314                 :            :              a new line) if we have actual replacement text.  */
    2315                 :       8260 :           if (c->m_byte_length > 0)
    2316                 :            :             {
    2317                 :       4708 :               move_to_column (&column, start_column, true);
    2318                 :       4708 :               m_colorizer.set_fixit_insert ();
    2319                 :       4708 :               pp_string (m_pp, c->m_text);
    2320                 :       4708 :               m_colorizer.set_normal_text ();
    2321                 :       4708 :               column += c->m_display_cols;
    2322                 :            :             }
    2323                 :            :         }
    2324                 :            :     }
    2325                 :            : 
    2326                 :            :   /* Add a trailing newline, if necessary.  */
    2327                 :      90525 :   move_to_column (&column, 0, false);
    2328                 :      90525 : }
    2329                 :            : 
    2330                 :            : /* Disable any colorization and emit a newline.  */
    2331                 :            : 
    2332                 :            : void
    2333                 :     202012 : layout::print_newline ()
    2334                 :            : {
    2335                 :     183244 :   m_colorizer.set_normal_text ();
    2336                 :     202012 :   pp_newline (m_pp);
    2337                 :          0 : }
    2338                 :            : 
    2339                 :            : /* Return true if (ROW/COLUMN) is within a range of the layout.
    2340                 :            :    If it returns true, OUT_STATE is written to, with the
    2341                 :            :    range index, and whether we should draw the caret at
    2342                 :            :    (ROW/COLUMN) (as opposed to an underline).  COL_UNIT controls
    2343                 :            :    whether all inputs and outputs are in bytes or display column units.  */
    2344                 :            : 
    2345                 :            : bool
    2346                 :   16684400 : layout::get_state_at_point (/* Inputs.  */
    2347                 :            :                             linenum_type row, int column,
    2348                 :            :                             int first_non_ws, int last_non_ws,
    2349                 :            :                             enum column_unit col_unit,
    2350                 :            :                             /* Outputs.  */
    2351                 :            :                             point_state *out_state)
    2352                 :            : {
    2353                 :   16684400 :   layout_range *range;
    2354                 :   16684400 :   int i;
    2355                 :   33383700 :   FOR_EACH_VEC_ELT (m_layout_ranges, i, range)
    2356                 :            :     {
    2357                 :   16827900 :       if (range->m_range_display_kind == SHOW_LINES_WITHOUT_RANGE)
    2358                 :            :         /* Bail out early, so that such ranges don't affect underlining or
    2359                 :            :            source colorization.  */
    2360                 :       3973 :         continue;
    2361                 :            : 
    2362                 :   16823900 :       if (range->contains_point (row, column, col_unit))
    2363                 :            :         {
    2364                 :     128671 :           out_state->range_idx = i;
    2365                 :            : 
    2366                 :            :           /* Are we at the range's caret?  is it visible? */
    2367                 :     128671 :           out_state->draw_caret_p = false;
    2368                 :     128671 :           if (range->m_range_display_kind == SHOW_RANGE_WITH_CARET
    2369                 :     113225 :               && row == range->m_caret.m_line
    2370                 :     110858 :               && column == range->m_caret.m_columns[col_unit])
    2371                 :      37874 :             out_state->draw_caret_p = true;
    2372                 :            : 
    2373                 :            :           /* Within a multiline range, don't display any underline
    2374                 :            :              in any leading or trailing whitespace on a line.
    2375                 :            :              We do display carets, however.  */
    2376                 :     128671 :           if (!out_state->draw_caret_p)
    2377                 :      90797 :             if (column < first_non_ws || column > last_non_ws)
    2378                 :            :               return false;
    2379                 :            : 
    2380                 :            :           /* We are within a range.  */
    2381                 :     122193 :           return true;
    2382                 :            :         }
    2383                 :            :     }
    2384                 :            : 
    2385                 :            :   return false;
    2386                 :            : }
    2387                 :            : 
    2388                 :            : /* Helper function for use by layout::print_line when printing the
    2389                 :            :    annotation line under the source line.
    2390                 :            :    Get the display column beyond the rightmost one that could contain a caret
    2391                 :            :    or range marker, given that we stop rendering at trailing whitespace.
    2392                 :            :    ROW is the source line within the given file.
    2393                 :            :    CARET_COLUMN is the display column of range 0's caret.
    2394                 :            :    LAST_NON_WS_COLUMN is the last display column containing a non-whitespace
    2395                 :            :    character of source (as determined when printing the source line).  */
    2396                 :            : 
    2397                 :            : int
    2398                 :      89929 : layout::get_x_bound_for_row (linenum_type row, int caret_column,
    2399                 :            :                              int last_non_ws_column)
    2400                 :            : {
    2401                 :      89929 :   int result = caret_column + 1;
    2402                 :            : 
    2403                 :      89929 :   layout_range *range;
    2404                 :      89929 :   int i;
    2405                 :     182700 :   FOR_EACH_VEC_ELT (m_layout_ranges, i, range)
    2406                 :            :     {
    2407                 :      92771 :       if (row >= range->m_start.m_line)
    2408                 :            :         {
    2409                 :      92091 :           if (range->m_finish.m_line == row)
    2410                 :            :             {
    2411                 :            :               /* On the final line within a range; ensure that
    2412                 :            :                  we render up to the end of the range.  */
    2413                 :      91387 :               const int disp_col = range->m_finish.m_columns[CU_DISPLAY_COLS];
    2414                 :      91387 :               if (result <= disp_col)
    2415                 :       5408 :                 result = disp_col + 1;
    2416                 :            :             }
    2417                 :        704 :           else if (row < range->m_finish.m_line)
    2418                 :            :             {
    2419                 :            :               /* Within a multiline range; ensure that we render up to the
    2420                 :            :                  last non-whitespace column.  */
    2421                 :         94 :               if (result <= last_non_ws_column)
    2422                 :         56 :                 result = last_non_ws_column + 1;
    2423                 :            :             }
    2424                 :            :         }
    2425                 :            :     }
    2426                 :            : 
    2427                 :      89929 :   return result;
    2428                 :            : }
    2429                 :            : 
    2430                 :            : /* Given *COLUMN as an x-coordinate, print spaces to position
    2431                 :            :    successive output at DEST_COLUMN, printing a newline if necessary,
    2432                 :            :    and updating *COLUMN.  If ADD_LEFT_MARGIN, then print the (empty)
    2433                 :            :    left margin after any newline.  */
    2434                 :            : 
    2435                 :            : void
    2436                 :     115672 : layout::move_to_column (int *column, int dest_column, bool add_left_margin)
    2437                 :            : {
    2438                 :            :   /* Start a new line if we need to.  */
    2439                 :     115672 :   if (*column > dest_column)
    2440                 :            :     {
    2441                 :      18768 :       print_newline ();
    2442                 :      18768 :       if (add_left_margin)
    2443                 :       2747 :         start_annotation_line ();
    2444                 :      18768 :       *column = m_x_offset_display;
    2445                 :            :     }
    2446                 :            : 
    2447                 :    7570120 :   while (*column < dest_column)
    2448                 :            :     {
    2449                 :    7454450 :       pp_space (m_pp);
    2450                 :    7454450 :       (*column)++;
    2451                 :            :     }
    2452                 :     115672 : }
    2453                 :            : 
    2454                 :            : /* For debugging layout issues, render a ruler giving column numbers
    2455                 :            :    (after the 1-column indent).  */
    2456                 :            : 
    2457                 :            : void
    2458                 :        164 : layout::show_ruler (int max_column) const
    2459                 :            : {
    2460                 :            :   /* Hundreds.  */
    2461                 :        164 :   if (max_column > 99)
    2462                 :            :     {
    2463                 :        100 :       start_annotation_line ();
    2464                 :        100 :       pp_space (m_pp);
    2465                 :       4896 :       for (int column = 1 + m_x_offset_display; column <= max_column; column++)
    2466                 :       4796 :         if (column % 10 == 0)
    2467                 :        444 :           pp_character (m_pp, '0' + (column / 100) % 10);
    2468                 :            :         else
    2469                 :       4352 :           pp_space (m_pp);
    2470                 :        100 :       pp_newline (m_pp);
    2471                 :            :     }
    2472                 :            : 
    2473                 :            :   /* Tens.  */
    2474                 :        164 :   start_annotation_line ();
    2475                 :        164 :   pp_space (m_pp);
    2476                 :       8160 :   for (int column = 1 + m_x_offset_display; column <= max_column; column++)
    2477                 :       7996 :     if (column % 10 == 0)
    2478                 :        764 :       pp_character (m_pp, '0' + (column / 10) % 10);
    2479                 :            :     else
    2480                 :       7232 :       pp_space (m_pp);
    2481                 :        164 :   pp_newline (m_pp);
    2482                 :            : 
    2483                 :            :   /* Units.  */
    2484                 :        164 :   start_annotation_line ();
    2485                 :        164 :   pp_space (m_pp);
    2486                 :       8160 :   for (int column = 1 + m_x_offset_display; column <= max_column; column++)
    2487                 :       7996 :     pp_character (m_pp, '0' + (column % 10));
    2488                 :        164 :   pp_newline (m_pp);
    2489                 :        164 : }
    2490                 :            : 
    2491                 :            : /* Print leading fix-its (for new lines inserted before the source line)
    2492                 :            :    then the source line, followed by an annotation line
    2493                 :            :    consisting of any caret/underlines, then any fixits.
    2494                 :            :    If the source line can't be read, print nothing.  */
    2495                 :            : void
    2496                 :      96269 : layout::print_line (linenum_type row)
    2497                 :            : {
    2498                 :      96269 :   char_span line = location_get_source_line (m_exploc.file, row);
    2499                 :      96269 :   if (!line)
    2500                 :       5744 :     return;
    2501                 :            : 
    2502                 :      90525 :   line_bounds lbounds;
    2503                 :      90525 :   print_leading_fixits (row);
    2504                 :      90525 :   print_source_line (row, line.get_buffer (), line.length (), &lbounds);
    2505                 :      90525 :   if (should_print_annotation_line_p (row))
    2506                 :            :     {
    2507                 :      89929 :       if (lbounds.m_first_non_ws != INT_MAX)
    2508                 :      19849 :         lbounds.convert_to_display_cols (line);
    2509                 :      89929 :       print_annotation_line (row, lbounds);
    2510                 :            :     }
    2511                 :      90525 :   if (m_show_labels_p)
    2512                 :      90472 :     print_any_labels (row);
    2513                 :      90525 :   print_trailing_fixits (row);
    2514                 :            : }
    2515                 :            : 
    2516                 :            : } /* End of anonymous namespace.  */
    2517                 :            : 
    2518                 :            : /* If LOC is within the spans of lines that will already be printed for
    2519                 :            :    this gcc_rich_location, then add it as a secondary location and return true.
    2520                 :            : 
    2521                 :            :    Otherwise return false.  */
    2522                 :            : 
    2523                 :            : bool
    2524                 :       1474 : gcc_rich_location::add_location_if_nearby (location_t loc,
    2525                 :            :                                            bool restrict_to_current_line_spans,
    2526                 :            :                                            const range_label *label)
    2527                 :            : {
    2528                 :            :   /* Use the layout location-handling logic to sanitize LOC,
    2529                 :            :      filtering it to the current line spans within a temporary
    2530                 :            :      layout instance.  */
    2531                 :       2948 :   layout layout (global_dc, this, DK_ERROR);
    2532                 :       1474 :   location_range loc_range;
    2533                 :       1474 :   loc_range.m_loc = loc;
    2534                 :       1474 :   loc_range.m_range_display_kind = SHOW_RANGE_WITHOUT_CARET;
    2535                 :       1474 :   if (!layout.maybe_add_location_range (&loc_range, 0,
    2536                 :            :                                         restrict_to_current_line_spans))
    2537                 :            :     return false;
    2538                 :            : 
    2539                 :       1302 :   add_range (loc, SHOW_RANGE_WITHOUT_CARET, label);
    2540                 :       1302 :   return true;
    2541                 :            : }
    2542                 :            : 
    2543                 :            : /* Print the physical source code corresponding to the location of
    2544                 :            :    this diagnostic, with additional annotations.  */
    2545                 :            : 
    2546                 :            : void
    2547                 :     294882 : diagnostic_show_locus (diagnostic_context * context,
    2548                 :            :                        rich_location *richloc,
    2549                 :            :                        diagnostic_t diagnostic_kind)
    2550                 :            : {
    2551                 :     294882 :   location_t loc = richloc->get_loc ();
    2552                 :            :   /* Do nothing if source-printing has been disabled.  */
    2553                 :     294882 :   if (!context->show_caret)
    2554                 :     199447 :     return;
    2555                 :            : 
    2556                 :            :   /* Don't attempt to print source for UNKNOWN_LOCATION and for builtins.  */
    2557                 :      95972 :   if (loc <= BUILTINS_LOCATION)
    2558                 :            :     return;
    2559                 :            : 
    2560                 :            :   /* Don't print the same source location twice in a row, unless we have
    2561                 :            :      fix-it hints.  */
    2562                 :      95828 :   if (loc == context->last_location
    2563                 :      95828 :       && richloc->get_num_fixit_hints () == 0)
    2564                 :            :     return;
    2565                 :            : 
    2566                 :      95435 :   context->last_location = loc;
    2567                 :            : 
    2568                 :      95435 :   layout layout (context, richloc, diagnostic_kind);
    2569                 :     381992 :   for (int line_span_idx = 0; line_span_idx < layout.get_num_line_spans ();
    2570                 :            :        line_span_idx++)
    2571                 :            :     {
    2572                 :      95561 :       const line_span *line_span = layout.get_line_span (line_span_idx);
    2573                 :      95561 :       if (context->show_line_numbers_p)
    2574                 :            :         {
    2575                 :            :           /* With line numbers, we should show whenever the line-numbering
    2576                 :            :              "jumps".  */
    2577                 :      19113 :           if (line_span_idx > 0)
    2578                 :         63 :             layout.print_gap_in_line_numbering ();
    2579                 :            :         }
    2580                 :            :       else
    2581                 :            :         {
    2582                 :            :           /* Without line numbers, we print headings for some line spans.  */
    2583                 :      76448 :           if (layout.print_heading_for_line_span_index_p (line_span_idx))
    2584                 :            :             {
    2585                 :        122 :               expanded_location exploc
    2586                 :        122 :                 = layout.get_expanded_location (line_span);
    2587                 :        122 :               context->start_span (context, exploc);
    2588                 :            :             }
    2589                 :            :         }
    2590                 :            :       /* Iterate over the lines within this span (using linenum_arith_t to
    2591                 :            :          avoid overflow with 0xffffffff causing an infinite loop).  */
    2592                 :      95561 :       linenum_arith_t last_line = line_span->get_last_line ();
    2593                 :     191766 :       for (linenum_arith_t row = line_span->get_first_line ();
    2594                 :     191766 :            row <= last_line; row++)
    2595                 :      96205 :         layout.print_line (row);
    2596                 :            :     }
    2597                 :            : }
    2598                 :            : 
    2599                 :            : #if CHECKING_P
    2600                 :            : 
    2601                 :            : namespace selftest {
    2602                 :            : 
    2603                 :            : /* Selftests for diagnostic_show_locus.  */
    2604                 :            : 
    2605                 :            : /* For precise tests of the layout, make clear where the source line will
    2606                 :            :    start.  test_left_margin sets the total byte count from the left side of the
    2607                 :            :    screen to the start of source lines, after the line number and the separator,
    2608                 :            :    which consists of the three characters " | ".  */
    2609                 :            : static const int test_linenum_sep = 3;
    2610                 :            : static const int test_left_margin = 7;
    2611                 :            : 
    2612                 :            : /* Helper function for test_layout_x_offset_display_utf8().  */
    2613                 :            : static void
    2614                 :        736 : test_offset_impl (int caret_byte_col, int max_width,
    2615                 :            :                   int expected_x_offset_display,
    2616                 :            :                   int left_margin = test_left_margin)
    2617                 :            : {
    2618                 :       1472 :   test_diagnostic_context dc;
    2619                 :        736 :   dc.caret_max_width = max_width;
    2620                 :            :   /* diagnostic_context::min_margin_width sets the minimum space reserved for
    2621                 :            :      the line number plus one space after.  */
    2622                 :        736 :   dc.min_margin_width = left_margin - test_linenum_sep + 1;
    2623                 :        736 :   dc.show_line_numbers_p = true;
    2624                 :        736 :   rich_location richloc (line_table,
    2625                 :            :                          linemap_position_for_column (line_table,
    2626                 :       1472 :                                                       caret_byte_col));
    2627                 :        736 :   layout test_layout (&dc, &richloc, DK_ERROR);
    2628                 :        736 :   ASSERT_EQ (left_margin - test_linenum_sep,
    2629                 :            :              test_layout.get_linenum_width ());
    2630                 :        736 :   ASSERT_EQ (expected_x_offset_display,
    2631                 :            :              test_layout.get_x_offset_display ());
    2632                 :        736 : }
    2633                 :            : 
    2634                 :            : /* Test that layout::calculate_x_offset_display() works.  */
    2635                 :            : static void
    2636                 :         48 : test_layout_x_offset_display_utf8 (const line_table_case &case_)
    2637                 :            : {
    2638                 :            : 
    2639                 :         48 :   const char *content
    2640                 :            :     = "This line is very long, so that we can use it to test the logic for "
    2641                 :            :       "clipping long lines.  Also this: \xf0\x9f\x98\x82\xf0\x9f\x98\x82 is a "
    2642                 :            :       "pair of emojis that occupies 8 bytes and 4 display columns, starting at "
    2643                 :            :       "column #102.\n";
    2644                 :            : 
    2645                 :            :   /* Number of bytes in the line, subtracting one to remove the newline.  */
    2646                 :         48 :   const int line_bytes = strlen (content) - 1;
    2647                 :            : 
    2648                 :            :   /* Number of display columns occupied by the line; each of the 2 emojis
    2649                 :            :      takes up 2 fewer display columns than it does bytes.  */
    2650                 :         48 :   const int line_display_cols = line_bytes - 2*2;
    2651                 :            : 
    2652                 :            :   /* The column of the first emoji.  Byte or display is the same as there are
    2653                 :            :      no multibyte characters earlier on the line.  */
    2654                 :         48 :   const int emoji_col = 102;
    2655                 :            : 
    2656                 :         80 :   temp_source_file tmp (SELFTEST_LOCATION, ".c", content);
    2657                 :         80 :   line_table_test ltt (case_);
    2658                 :            : 
    2659                 :         48 :   linemap_add (line_table, LC_ENTER, false, tmp.get_filename (), 1);
    2660                 :            : 
    2661                 :         48 :   location_t line_end = linemap_position_for_column (line_table, line_bytes);
    2662                 :            : 
    2663                 :            :   /* Don't attempt to run the tests if column data might be unavailable.  */
    2664                 :         48 :   if (line_end > LINE_MAP_MAX_LOCATION_WITH_COLS)
    2665                 :         16 :     return;
    2666                 :            : 
    2667                 :         32 :   ASSERT_STREQ (tmp.get_filename (), LOCATION_FILE (line_end));
    2668                 :         32 :   ASSERT_EQ (1, LOCATION_LINE (line_end));
    2669                 :         32 :   ASSERT_EQ (line_bytes, LOCATION_COLUMN (line_end));
    2670                 :            : 
    2671                 :         32 :   char_span lspan = location_get_source_line (tmp.get_filename (), 1);
    2672                 :         32 :   ASSERT_EQ (line_display_cols,
    2673                 :            :              cpp_display_width (lspan.get_buffer (), lspan.length ()));
    2674                 :         32 :   ASSERT_EQ (line_display_cols,
    2675                 :            :              location_compute_display_column (expand_location (line_end)));
    2676                 :         32 :   ASSERT_EQ (0, memcmp (lspan.get_buffer () + (emoji_col - 1),
    2677                 :            :                         "\xf0\x9f\x98\x82\xf0\x9f\x98\x82", 8));
    2678                 :            : 
    2679                 :            :   /* (caret_byte, max_width, expected_x_offset_display, [left_margin])  */
    2680                 :            : 
    2681                 :            :   /* No constraint on the width -> no offset.  */
    2682                 :         32 :   test_offset_impl (emoji_col, 0, 0);
    2683                 :            : 
    2684                 :            :   /* Caret is before the beginning -> no offset.  */
    2685                 :         32 :   test_offset_impl (0, 100, 0);
    2686                 :            : 
    2687                 :            :   /* Caret is past the end of the line -> no offset.  */
    2688                 :         32 :   test_offset_impl (line_bytes+1, 100, 0);
    2689                 :            : 
    2690                 :            :   /* Line fits in the display -> no offset.  */
    2691                 :         32 :   test_offset_impl (line_bytes, line_display_cols + test_left_margin, 0);
    2692                 :         32 :   test_offset_impl (emoji_col, line_display_cols + test_left_margin, 0);
    2693                 :            : 
    2694                 :            :   /* Line is too long for the display but caret location is OK
    2695                 :            :      anyway -> no offset.  */
    2696                 :         32 :   static const int small_width = 24;
    2697                 :         32 :   test_offset_impl (1, small_width, 0);
    2698                 :            : 
    2699                 :            :   /* Width constraint is very small -> no offset.  */
    2700                 :         32 :   test_offset_impl (emoji_col, CARET_LINE_MARGIN, 0);
    2701                 :            : 
    2702                 :            :   /* Line would be offset, but due to large line numbers, offsetting
    2703                 :            :      would remove the whole line -> no offset.  */
    2704                 :         32 :   static const int huge_left_margin = 100;
    2705                 :         32 :   test_offset_impl (emoji_col, huge_left_margin, 0, huge_left_margin);
    2706                 :            : 
    2707                 :            :   /* Line is the same length as the display, but the line number makes it too
    2708                 :            :      long, so offset is required.  Caret is at the end so padding on the right
    2709                 :            :      is not in effect.  */
    2710                 :        128 :   for (int excess = 1; excess <= 3; ++excess)
    2711                 :         96 :     test_offset_impl (line_bytes, line_display_cols + test_left_margin - excess,
    2712                 :            :                       excess);
    2713                 :            : 
    2714                 :            :   /* Line is much too long for the display, caret is near the end ->
    2715                 :            :      offset should be such that the line fits in the display and caret
    2716                 :            :      remains the same distance from the end that it was.  */
    2717                 :        352 :   for (int caret_offset = 0, max_offset = MIN (CARET_LINE_MARGIN, 10);
    2718                 :        384 :        caret_offset <= max_offset; ++caret_offset)
    2719                 :        352 :     test_offset_impl (line_bytes - caret_offset, small_width,
    2720                 :            :                       line_display_cols + test_left_margin - small_width);
    2721                 :            : 
    2722                 :            :   /* As previous case but caret is closer to the middle; now we want it to end
    2723                 :            :      up CARET_LINE_MARGIN bytes from the end.  */
    2724                 :         32 :   ASSERT_GT (line_display_cols - emoji_col, CARET_LINE_MARGIN);
    2725                 :         32 :   test_offset_impl (emoji_col, small_width,
    2726                 :            :                     emoji_col + test_left_margin
    2727                 :            :                     - (small_width - CARET_LINE_MARGIN));
    2728                 :            : 
    2729                 :            :   /* Test that the source line is offset as expected when printed.  */
    2730                 :         32 :   {
    2731                 :         64 :     test_diagnostic_context dc;
    2732                 :         32 :     dc.caret_max_width = small_width - 6;
    2733                 :         32 :     dc.min_margin_width = test_left_margin - test_linenum_sep + 1;
    2734                 :         32 :     dc.show_line_numbers_p = true;
    2735                 :         32 :     dc.show_ruler_p = true;
    2736                 :         32 :     rich_location richloc (line_table,
    2737                 :            :                            linemap_position_for_column (line_table,
    2738                 :         64 :                                                         emoji_col));
    2739                 :         32 :     layout test_layout (&dc, &richloc, DK_ERROR);
    2740                 :         32 :     test_layout.print_line (1);
    2741                 :         32 :     ASSERT_STREQ ("     |         1         \n"
    2742                 :            :                   "     |         1         \n"
    2743                 :            :                   "     | 234567890123456789\n"
    2744                 :            :                   "   1 | \xf0\x9f\x98\x82\xf0\x9f\x98\x82 is a pair of emojis "
    2745                 :            :                   "that occupies 8 bytes and 4 display columns, starting at "
    2746                 :            :                   "column #102.\n"
    2747                 :            :                   "     | ^\n\n",
    2748                 :            :                   pp_formatted_text (dc.printer));
    2749                 :            :   }
    2750                 :            : 
    2751                 :            :   /* Similar to the previous example, but now the offset called for would split
    2752                 :            :      the first emoji in the middle of the UTF-8 sequence.  Check that we replace
    2753                 :            :      it with a padding space in this case.  */
    2754                 :         32 :   {
    2755                 :         64 :     test_diagnostic_context dc;
    2756                 :         32 :     dc.caret_max_width = small_width - 5;
    2757                 :         32 :     dc.min_margin_width = test_left_margin - test_linenum_sep + 1;
    2758                 :         32 :     dc.show_line_numbers_p = true;
    2759                 :         32 :     dc.show_ruler_p = true;
    2760                 :         32 :     rich_location richloc (line_table,
    2761                 :            :                            linemap_position_for_column (line_table,
    2762                 :         64 :                                                         emoji_col + 2));
    2763                 :         32 :     layout test_layout (&dc, &richloc, DK_ERROR);
    2764                 :         32 :     test_layout.print_line (1);
    2765                 :         32 :     ASSERT_STREQ ("     |        1         1 \n"
    2766                 :            :                   "     |        1         2 \n"
    2767                 :            :                   "     | 3456789012345678901\n"
    2768                 :            :                   "   1 |  \xf0\x9f\x98\x82 is a pair of emojis "
    2769                 :            :                   "that occupies 8 bytes and 4 display columns, starting at "
    2770                 :            :                   "column #102.\n"
    2771                 :            :                   "     |  ^\n\n",
    2772                 :            :                   pp_formatted_text (dc.printer));
    2773                 :            :   }
    2774                 :            : 
    2775                 :            : }
    2776                 :            : 
    2777                 :            : /* Verify that diagnostic_show_locus works sanely on UNKNOWN_LOCATION.  */
    2778                 :            : 
    2779                 :            : static void
    2780                 :          2 : test_diagnostic_show_locus_unknown_location ()
    2781                 :            : {
    2782                 :          4 :   test_diagnostic_context dc;
    2783                 :          2 :   rich_location richloc (line_table, UNKNOWN_LOCATION);
    2784                 :          2 :   diagnostic_show_locus (&dc, &richloc, DK_ERROR);
    2785                 :          2 :   ASSERT_STREQ ("", pp_formatted_text (dc.printer));
    2786                 :          2 : }
    2787                 :            : 
    2788                 :            : /* Verify that diagnostic_show_locus works sanely for various
    2789                 :            :    single-line cases.
    2790                 :            : 
    2791                 :            :    All of these work on the following 1-line source file:
    2792                 :            :      .0000000001111111
    2793                 :            :      .1234567890123456
    2794                 :            :      "foo = bar.field;\n"
    2795                 :            :    which is set up by test_diagnostic_show_locus_one_liner and calls
    2796                 :            :    them.  */
    2797                 :            : 
    2798                 :            : /* Just a caret.  */
    2799                 :            : 
    2800                 :            : static void
    2801                 :         32 : test_one_liner_simple_caret ()
    2802                 :            : {
    2803                 :         64 :   test_diagnostic_context dc;
    2804                 :         32 :   location_t caret = linemap_position_for_column (line_table, 10);
    2805                 :         32 :   rich_location richloc (line_table, caret);
    2806                 :         32 :   diagnostic_show_locus (&dc, &richloc, DK_ERROR);
    2807                 :         32 :   ASSERT_STREQ (" foo = bar.field;\n"
    2808                 :            :                 "          ^\n",
    2809                 :            :                 pp_formatted_text (dc.printer));
    2810                 :         32 : }
    2811                 :            : 
    2812                 :            : /* Caret and range.  */
    2813                 :            : 
    2814                 :            : static void
    2815                 :         32 : test_one_liner_caret_and_range ()
    2816                 :            : {
    2817                 :         64 :   test_diagnostic_context dc;
    2818                 :         32 :   location_t caret = linemap_position_for_column (line_table, 10);
    2819                 :         32 :   location_t start = linemap_position_for_column (line_table, 7);
    2820                 :         32 :   location_t finish = linemap_position_for_column (line_table, 15);
    2821                 :         32 :   location_t loc = make_location (caret, start, finish);
    2822                 :         32 :   rich_location richloc (line_table, loc);
    2823                 :         32 :   diagnostic_show_locus (&dc, &richloc, DK_ERROR);
    2824                 :         32 :   ASSERT_STREQ (" foo = bar.field;\n"
    2825                 :            :                 "       ~~~^~~~~~\n",
    2826                 :            :                 pp_formatted_text (dc.printer));
    2827                 :         32 : }
    2828                 :            : 
    2829                 :            : /* Multiple ranges and carets.  */
    2830                 :            : 
    2831                 :            : static void
    2832                 :         32 : test_one_liner_multiple_carets_and_ranges ()
    2833                 :            : {
    2834                 :         64 :   test_diagnostic_context dc;
    2835                 :         32 :   location_t foo
    2836                 :         32 :     = make_location (linemap_position_for_column (line_table, 2),
    2837                 :            :                      linemap_position_for_column (line_table, 1),
    2838                 :            :                      linemap_position_for_column (line_table, 3));
    2839                 :         32 :   dc.caret_chars[0] = 'A';
    2840                 :            : 
    2841                 :         32 :   location_t bar
    2842                 :         32 :     = make_location (linemap_position_for_column (line_table, 8),
    2843                 :            :                      linemap_position_for_column (line_table, 7),
    2844                 :            :                      linemap_position_for_column (line_table, 9));
    2845                 :         32 :   dc.caret_chars[1] = 'B';
    2846                 :            : 
    2847                 :         32 :   location_t field
    2848                 :         32 :     = make_location (linemap_position_for_column (line_table, 13),
    2849                 :            :                      linemap_position_for_column (line_table, 11),
    2850                 :            :                      linemap_position_for_column (line_table, 15));
    2851                 :         32 :   dc.caret_chars[2] = 'C';
    2852                 :            : 
    2853                 :         32 :   rich_location richloc (line_table, foo);
    2854                 :         32 :   richloc.add_range (bar, SHOW_RANGE_WITH_CARET);
    2855                 :         32 :   richloc.add_range (field, SHOW_RANGE_WITH_CARET);
    2856                 :         32 :   diagnostic_show_locus (&dc, &richloc, DK_ERROR);
    2857                 :         32 :   ASSERT_STREQ (" foo = bar.field;\n"
    2858                 :            :                 " ~A~   ~B~ ~~C~~\n",
    2859                 :            :                 pp_formatted_text (dc.printer));
    2860                 :         32 : }
    2861                 :            : 
    2862                 :            : /* Insertion fix-it hint: adding an "&" to the front of "bar.field". */
    2863                 :            : 
    2864                 :            : static void
    2865                 :         32 : test_one_liner_fixit_insert_before ()
    2866                 :            : {
    2867                 :         64 :   test_diagnostic_context dc;
    2868                 :         32 :   location_t caret = linemap_position_for_column (line_table, 7);
    2869                 :         32 :   rich_location richloc (line_table, caret);
    2870                 :         32 :   richloc.add_fixit_insert_before ("&");
    2871                 :         32 :   diagnostic_show_locus (&dc, &richloc, DK_ERROR);
    2872                 :         32 :   ASSERT_STREQ (" foo = bar.field;\n"
    2873                 :            :                 "       ^\n"
    2874                 :            :                 "       &\n",
    2875                 :            :                 pp_formatted_text (dc.printer));
    2876                 :         32 : }
    2877                 :            : 
    2878                 :            : /* Insertion fix-it hint: adding a "[0]" after "foo". */
    2879                 :            : 
    2880                 :            : static void
    2881                 :         32 : test_one_liner_fixit_insert_after ()
    2882                 :            : {
    2883                 :         64 :   test_diagnostic_context dc;
    2884                 :         32 :   location_t start = linemap_position_for_column (line_table, 1);
    2885                 :         32 :   location_t finish = linemap_position_for_column (line_table, 3);
    2886                 :         32 :   location_t foo = make_location (start, start, finish);
    2887                 :         32 :   rich_location richloc (line_table, foo);
    2888                 :         32 :   richloc.add_fixit_insert_after ("[0]");
    2889                 :         32 :   diagnostic_show_locus (&dc, &richloc, DK_ERROR);
    2890                 :         32 :   ASSERT_STREQ (" foo = bar.field;\n"
    2891                 :            :                 " ^~~\n"
    2892                 :            :                 "    [0]\n",
    2893                 :            :                 pp_formatted_text (dc.printer));
    2894                 :         32 : }
    2895                 :            : 
    2896                 :            : /* Removal fix-it hint: removal of the ".field".
    2897                 :            :    Also verify the interaction of pp_set_prefix with rulers and
    2898                 :            :    fix-it hints.  */
    2899                 :            : 
    2900                 :            : static void
    2901                 :         32 : test_one_liner_fixit_remove ()
    2902                 :            : {
    2903                 :         32 :   location_t start = linemap_position_for_column (line_table, 10);
    2904                 :         32 :   location_t finish = linemap_position_for_column (line_table, 15);
    2905                 :         32 :   location_t dot = make_location (start, start, finish);
    2906                 :         64 :   rich_location richloc (line_table, dot);
    2907                 :         32 :   richloc.add_fixit_remove ();
    2908                 :            : 
    2909                 :            :   /* Normal.  */
    2910                 :         32 :   {
    2911                 :         32 :     test_diagnostic_context dc;
    2912                 :         32 :     diagnostic_show_locus (&dc, &richloc, DK_ERROR);
    2913                 :         32 :     ASSERT_STREQ (" foo = bar.field;\n"
    2914                 :            :                   "          ^~~~~~\n"
    2915                 :            :                   "          ------\n",
    2916                 :            :                   pp_formatted_text (dc.printer));
    2917                 :            :   }
    2918                 :            : 
    2919                 :            :   /* Test of adding a prefix.  */
    2920                 :         32 :   {
    2921                 :         32 :     test_diagnostic_context dc;
    2922                 :         32 :     pp_prefixing_rule (dc.printer) = DIAGNOSTICS_SHOW_PREFIX_EVERY_LINE;
    2923                 :         32 :     pp_set_prefix (dc.printer, xstrdup ("TEST PREFIX:"));
    2924                 :         32 :     diagnostic_show_locus (&dc, &richloc, DK_ERROR);
    2925                 :         32 :     ASSERT_STREQ ("TEST PREFIX: foo = bar.field;\n"
    2926                 :            :                   "TEST PREFIX:          ^~~~~~\n"
    2927                 :            :                   "TEST PREFIX:          ------\n",
    2928                 :            :                   pp_formatted_text (dc.printer));
    2929                 :            :   }
    2930                 :            : 
    2931                 :            :   /* Normal, with ruler.  */
    2932                 :         32 :   {
    2933                 :         32 :     test_diagnostic_context dc;
    2934                 :         32 :     dc.show_ruler_p = true;
    2935                 :         32 :     dc.caret_max_width = 104;
    2936                 :         32 :     diagnostic_show_locus (&dc, &richloc, DK_ERROR);
    2937                 :         32 :     ASSERT_STREQ ("          0         0         0         0         0         0         0         0         0         1    \n"
    2938                 :            :                   "          1         2         3         4         5         6         7         8         9         0    \n"
    2939                 :            :                   " 12345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234\n"
    2940                 :            :                   " foo = bar.field;\n"
    2941                 :            :                   "          ^~~~~~\n"
    2942                 :            :                   "          ------\n",
    2943                 :            :                   pp_formatted_text (dc.printer));
    2944                 :            :   }
    2945                 :            : 
    2946                 :            :   /* Test of adding a prefix, with ruler.  */
    2947                 :         32 :   {
    2948                 :         32 :     test_diagnostic_context dc;
    2949                 :         32 :     dc.show_ruler_p = true;
    2950                 :         32 :     dc.caret_max_width = 50;
    2951                 :         32 :     pp_prefixing_rule (dc.printer) = DIAGNOSTICS_SHOW_PREFIX_EVERY_LINE;
    2952                 :         32 :     pp_set_prefix (dc.printer, xstrdup ("TEST PREFIX:"));
    2953                 :         32 :     diagnostic_show_locus (&dc, &richloc, DK_ERROR);
    2954                 :         32 :     ASSERT_STREQ ("TEST PREFIX:          1         2         3         4         5\n"
    2955                 :            :                   "TEST PREFIX: 12345678901234567890123456789012345678901234567890\n"
    2956                 :            :                   "TEST PREFIX: foo = bar.field;\n"
    2957                 :            :                   "TEST PREFIX:          ^~~~~~\n"
    2958                 :            :                   "TEST PREFIX:          ------\n",
    2959                 :            :                   pp_formatted_text (dc.printer));
    2960                 :            :   }
    2961                 :            : 
    2962                 :            :   /* Test of adding a prefix, with ruler and line numbers.  */
    2963                 :         32 :   {
    2964                 :         32 :     test_diagnostic_context dc;
    2965                 :         32 :     dc.show_ruler_p = true;
    2966                 :         32 :     dc.caret_max_width = 50;
    2967                 :         32 :     dc.show_line_numbers_p = true;
    2968                 :         32 :     pp_prefixing_rule (dc.printer) = DIAGNOSTICS_SHOW_PREFIX_EVERY_LINE;
    2969                 :         32 :     pp_set_prefix (dc.printer, xstrdup ("TEST PREFIX:"));
    2970                 :         32 :     diagnostic_show_locus (&dc, &richloc, DK_ERROR);
    2971                 :         32 :     ASSERT_STREQ ("TEST PREFIX:      |          1         2         3         4         5\n"
    2972                 :            :                   "TEST PREFIX:      | 12345678901234567890123456789012345678901234567890\n"
    2973                 :            :                   "TEST PREFIX:    1 | foo = bar.field;\n"
    2974                 :            :                   "TEST PREFIX:      |          ^~~~~~\n"
    2975                 :            :                   "TEST PREFIX:      |          ------\n",
    2976                 :            :                   pp_formatted_text (dc.printer));
    2977                 :            :   }
    2978                 :         32 : }
    2979                 :            : 
    2980                 :            : /* Replace fix-it hint: replacing "field" with "m_field". */
    2981                 :            : 
    2982                 :            : static void
    2983                 :         32 : test_one_liner_fixit_replace ()
    2984                 :            : {
    2985                 :         64 :   test_diagnostic_context dc;
    2986                 :         32 :   location_t start = linemap_position_for_column (line_table, 11);
    2987                 :         32 :   location_t finish = linemap_position_for_column (line_table, 15);
    2988                 :         32 :   location_t field = make_location (start, start, finish);
    2989                 :         32 :   rich_location richloc (line_table, field);
    2990                 :         32 :   richloc.add_fixit_replace ("m_field");
    2991                 :         32 :   diagnostic_show_locus (&dc, &richloc, DK_ERROR);
    2992                 :         32 :   ASSERT_STREQ (" foo = bar.field;\n"
    2993                 :            :                 "           ^~~~~\n"
    2994                 :            :                 "           m_field\n",
    2995                 :            :                 pp_formatted_text (dc.printer));
    2996                 :         32 : }
    2997                 :            : 
    2998                 :            : /* Replace fix-it hint: replacing "field" with "m_field",
    2999                 :            :    but where the caret was elsewhere.  */
    3000                 :            : 
    3001                 :            : static void
    3002                 :         32 : test_one_liner_fixit_replace_non_equal_range ()
    3003                 :            : {
    3004                 :         64 :   test_diagnostic_context dc;
    3005                 :         32 :   location_t equals = linemap_position_for_column (line_table, 5);
    3006                 :         32 :   location_t start = linemap_position_for_column (line_table, 11);
    3007                 :         32 :   location_t finish = linemap_position_for_column (line_table, 15);
    3008                 :         32 :   rich_location richloc (line_table, equals);
    3009                 :         32 :   source_range range;
    3010                 :         32 :   range.m_start = start;
    3011                 :         32 :   range.m_finish = finish;
    3012                 :         32 :   richloc.add_fixit_replace (range, "m_field");
    3013                 :         32 :   diagnostic_show_locus (&dc, &richloc, DK_ERROR);
    3014                 :            :   /* The replacement range is not indicated in the annotation line, so
    3015                 :            :      it should be indicated via an additional underline.  */
    3016                 :         32 :   ASSERT_STREQ (" foo = bar.field;\n"
    3017                 :            :                 "     ^\n"
    3018                 :            :                 "           -----\n"
    3019                 :            :                 "           m_field\n",
    3020                 :            :                 pp_formatted_text (dc.printer));
    3021                 :         32 : }
    3022                 :            : 
    3023                 :            : /* Replace fix-it hint: replacing "field" with "m_field",
    3024                 :            :    where the caret was elsewhere, but where a secondary range
    3025                 :            :    exactly covers "field".  */
    3026                 :            : 
    3027                 :            : static void
    3028                 :         32 : test_one_liner_fixit_replace_equal_secondary_range ()
    3029                 :            : {
    3030                 :         64 :   test_diagnostic_context dc;
    3031                 :         32 :   location_t equals = linemap_position_for_column (line_table, 5);
    3032                 :         32 :   location_t start = linemap_position_for_column (line_table, 11);
    3033                 :         32 :   location_t finish = linemap_position_for_column (line_table, 15);
    3034                 :         32 :   rich_location richloc (line_table, equals);
    3035                 :         32 :   location_t field = make_location (start, start, finish);
    3036                 :         32 :   richloc.add_range (field);
    3037                 :         32 :   richloc.add_fixit_replace (field, "m_field");
    3038                 :         32 :   diagnostic_show_locus (&dc, &richloc, DK_ERROR);
    3039                 :            :   /* The replacement range is indicated in the annotation line,
    3040                 :            :      so it shouldn't be indicated via an additional underline.  */
    3041                 :         32 :   ASSERT_STREQ (" foo = bar.field;\n"
    3042                 :            :                 "     ^     ~~~~~\n"
    3043                 :            :                 "           m_field\n",
    3044                 :            :                 pp_formatted_text (dc.printer));
    3045                 :         32 : }
    3046                 :            : 
    3047                 :            : /* Verify that we can use ad-hoc locations when adding fixits to a
    3048                 :            :    rich_location.  */
    3049                 :            : 
    3050                 :            : static void
    3051                 :         32 : test_one_liner_fixit_validation_adhoc_locations ()
    3052                 :            : {
    3053                 :            :   /* Generate a range that's too long to be packed, so must
    3054                 :            :      be stored as an ad-hoc location (given the defaults
    3055                 :            :      of 5 bits or 0 bits of packed range); 41 columns > 2**5.  */
    3056                 :         32 :   const location_t c7 = linemap_position_for_column (line_table, 7);
    3057                 :         32 :   const location_t c47 = linemap_position_for_column (line_table, 47);
    3058                 :         32 :   const location_t loc = make_location (c7, c7, c47);
    3059                 :            : 
    3060                 :         32 :   if (c47 > LINE_MAP_MAX_LOCATION_WITH_COLS)
    3061                 :            :     return;
    3062                 :            : 
    3063                 :         32 :   ASSERT_TRUE (IS_ADHOC_LOC (loc));
    3064                 :            : 
    3065                 :            :   /* Insert.  */
    3066                 :         32 :   {
    3067                 :         64 :     rich_location richloc (line_table, loc);
    3068                 :         32 :     richloc.add_fixit_insert_before (loc, "test");
    3069                 :            :     /* It should not have been discarded by the validator.  */
    3070                 :         32 :     ASSERT_EQ (1, richloc.get_num_fixit_hints ());
    3071                 :            : 
    3072                 :         32 :     test_diagnostic_context dc;
    3073                 :         32 :     diagnostic_show_locus (&dc, &richloc, DK_ERROR);
    3074                 :         32 :     ASSERT_STREQ (" foo = bar.field;\n"
    3075                 :            :                   "       ^~~~~~~~~~                               \n"
    3076                 :            :                   "       test\n",
    3077                 :            :                   pp_formatted_text (dc.printer));
    3078                 :            :   }
    3079                 :            : 
    3080                 :            :   /* Remove.  */
    3081                 :         32 :   {
    3082                 :         64 :     rich_location richloc (line_table, loc);
    3083                 :         32 :     source_range range = source_range::from_locations (loc, c47);
    3084                 :         32 :     richloc.add_fixit_remove (range);
    3085                 :            :     /* It should not have been discarded by the validator.  */
    3086                 :         32 :     ASSERT_EQ (1, richloc.get_num_fixit_hints ());
    3087                 :            : 
    3088                 :         32 :     test_diagnostic_context dc;
    3089                 :         32 :     diagnostic_show_locus (&dc, &richloc, DK_ERROR);
    3090                 :         32 :     ASSERT_STREQ (" foo = bar.field;\n"
    3091                 :            :                   "       ^~~~~~~~~~                               \n"
    3092                 :            :                   "       -----------------------------------------\n",
    3093                 :            :                   pp_formatted_text (dc.printer));
    3094                 :            :   }
    3095                 :            : 
    3096                 :            :   /* Replace.  */
    3097                 :         32 :   {
    3098                 :         64 :     rich_location richloc (line_table, loc);
    3099                 :         32 :     source_range range = source_range::from_locations (loc, c47);
    3100                 :         32 :     richloc.add_fixit_replace (range, "test");
    3101                 :            :     /* It should not have been discarded by the validator.  */
    3102                 :         32 :     ASSERT_EQ (1, richloc.get_num_fixit_hints ());
    3103                 :            : 
    3104                 :         32 :     test_diagnostic_context dc;
    3105                 :         32 :     diagnostic_show_locus (&dc, &richloc, DK_ERROR);
    3106                 :         32 :     ASSERT_STREQ (" foo = bar.field;\n"
    3107                 :            :                   "       ^~~~~~~~~~                               \n"
    3108                 :            :                   "       test\n",
    3109                 :            :                   pp_formatted_text (dc.printer));
    3110                 :            :   }
    3111                 :            : }
    3112                 :            : 
    3113                 :            : /* Test of consolidating insertions at the same location.  */
    3114                 :            : 
    3115                 :            : static void
    3116                 :         32 : test_one_liner_many_fixits_1 ()
    3117                 :            : {
    3118                 :         64 :   test_diagnostic_context dc;
    3119                 :         32 :   location_t equals = linemap_position_for_column (line_table, 5);
    3120                 :         64 :   rich_location richloc (line_table, equals);
    3121                 :        640 :   for (int i = 0; i < 19; i++)
    3122                 :        608 :     richloc.add_fixit_insert_before ("a");
    3123                 :         32 :   ASSERT_EQ (1, richloc.get_num_fixit_hints ());
    3124                 :         32 :   diagnostic_show_locus (&dc, &richloc, DK_ERROR);
    3125                 :         32 :   ASSERT_STREQ (" foo = bar.field;\n"
    3126                 :            :                 "     ^\n"
    3127                 :            :                 "     aaaaaaaaaaaaaaaaaaa\n",
    3128                 :            :                 pp_formatted_text (dc.printer));
    3129                 :         32 : }
    3130                 :            : 
    3131                 :            : /* Ensure that we can add an arbitrary number of fix-it hints to a
    3132                 :            :    rich_location, even if they are not consolidated.  */
    3133                 :            : 
    3134                 :            : static void
    3135                 :         32 : test_one_liner_many_fixits_2 ()
    3136                 :            : {
    3137                 :         64 :   test_diagnostic_context dc;
    3138                 :         32 :   location_t equals = linemap_position_for_column (line_table, 5);
    3139                 :         64 :   rich_location richloc (line_table, equals);
    3140                 :        640 :   for (int i = 0; i < 19; i++)
    3141                 :            :     {
    3142                 :        608 :       location_t loc = linemap_position_for_column (line_table, i * 2);
    3143                 :        608 :       richloc.add_fixit_insert_before (loc, "a");
    3144                 :            :     }
    3145                 :         32 :   ASSERT_EQ (19, richloc.get_num_fixit_hints ());
    3146                 :         32 :   diagnostic_show_locus (&dc, &richloc, DK_ERROR);
    3147                 :         32 :   ASSERT_STREQ (" foo = bar.field;\n"
    3148                 :            :                 "     ^\n"
    3149                 :            :                 "a a a a a a a a a a a a a a a a a a a\n",
    3150                 :            :                 pp_formatted_text (dc.printer));
    3151                 :         32 : }
    3152                 :            : 
    3153                 :            : /* Test of labeling the ranges within a rich_location.  */
    3154                 :            : 
    3155                 :            : static void
    3156                 :         32 : test_one_liner_labels ()
    3157                 :            : {
    3158                 :         32 :   location_t foo
    3159                 :         32 :     = make_location (linemap_position_for_column (line_table, 1),
    3160                 :            :                      linemap_position_for_column (line_table, 1),
    3161                 :            :                      linemap_position_for_column (line_table, 3));
    3162                 :         32 :   location_t bar
    3163                 :         32 :     = make_location (linemap_position_for_column (line_table, 7),
    3164                 :            :                      linemap_position_for_column (line_table, 7),
    3165                 :            :                      linemap_position_for_column (line_table, 9));
    3166                 :         32 :   location_t field
    3167                 :         32 :     = make_location (linemap_position_for_column (line_table, 11),
    3168                 :            :                      linemap_position_for_column (line_table, 11),
    3169                 :            :                      linemap_position_for_column (line_table, 15));
    3170                 :            : 
    3171                 :            :   /* Example where all the labels fit on one line.  */
    3172                 :         32 :   {
    3173                 :         32 :     text_range_label label0 ("0");
    3174                 :         64 :     text_range_label label1 ("1");
    3175                 :         64 :     text_range_label label2 ("2");
    3176                 :         64 :     gcc_rich_location richloc (foo, &label0);
    3177                 :         32 :     richloc.add_range (bar, SHOW_RANGE_WITHOUT_CARET, &label1);
    3178                 :         32 :     richloc.add_range (field, SHOW_RANGE_WITHOUT_CARET, &label2);
    3179                 :            : 
    3180                 :         32 :     {
    3181                 :         32 :       test_diagnostic_context dc;
    3182                 :         32 :       diagnostic_show_locus (&dc, &richloc, DK_ERROR);
    3183                 :         32 :       ASSERT_STREQ (" foo = bar.field;\n"
    3184                 :            :                     " ^~~   ~~~ ~~~~~\n"
    3185                 :            :                     " |     |   |\n"
    3186                 :            :                     " 0     1   2\n",
    3187                 :            :                     pp_formatted_text (dc.printer));
    3188                 :            :     }
    3189                 :            : 
    3190                 :            :     /* Verify that we can disable label-printing.  */
    3191                 :         32 :     {
    3192                 :         32 :       test_diagnostic_context dc;
    3193                 :         32 :       dc.show_labels_p = false;
    3194                 :         32 :       diagnostic_show_locus (&dc, &richloc, DK_ERROR);
    3195                 :         32 :       ASSERT_STREQ (" foo = bar.field;\n"
    3196                 :            :                     " ^~~   ~~~ ~~~~~\n",
    3197                 :            :                     pp_formatted_text (dc.printer));
    3198                 :            :     }
    3199                 :            :   }
    3200                 :            : 
    3201                 :            :   /* Example where the labels need extra lines.  */
    3202                 :         32 :   {
    3203                 :         32 :     text_range_label label0 ("label 0");
    3204                 :         64 :     text_range_label label1 ("label 1");
    3205                 :         64 :     text_range_label label2 ("label 2");
    3206                 :         64 :     gcc_rich_location richloc (foo, &label0);
    3207                 :         32 :     richloc.add_range (bar, SHOW_RANGE_WITHOUT_CARET, &label1);
    3208                 :         32 :     richloc.add_range (field, SHOW_RANGE_WITHOUT_CARET, &label2);
    3209                 :            : 
    3210                 :         64 :     test_diagnostic_context dc;
    3211                 :         32 :     diagnostic_show_locus (&dc, &richloc, DK_ERROR);
    3212                 :         32 :     ASSERT_STREQ (" foo = bar.field;\n"
    3213                 :            :                   " ^~~   ~~~ ~~~~~\n"
    3214                 :            :                   " |     |   |\n"
    3215                 :            :                   " |     |   label 2\n"
    3216                 :            :                   " |     label 1\n"
    3217                 :            :                   " label 0\n",
    3218                 :            :                   pp_formatted_text (dc.printer));
    3219                 :            :   }
    3220                 :            : 
    3221                 :            :   /* Example of boundary conditions: label 0 and 1 have just enough clearance,
    3222                 :            :      but label 1 just touches label 2.  */
    3223                 :         32 :   {
    3224                 :         32 :     text_range_label label0 ("aaaaa");
    3225                 :         64 :     text_range_label label1 ("bbbb");
    3226                 :         64 :     text_range_label label2 ("c");
    3227                 :         64 :     gcc_rich_location richloc (foo, &label0);
    3228                 :         32 :     richloc.add_range (bar, SHOW_RANGE_WITHOUT_CARET, &label1);
    3229                 :         32 :     richloc.add_range (field, SHOW_RANGE_WITHOUT_CARET, &label2);
    3230                 :            : 
    3231                 :         64 :     test_diagnostic_context dc;
    3232                 :         32 :     diagnostic_show_locus (&dc, &richloc, DK_ERROR);
    3233                 :         32 :     ASSERT_STREQ (" foo = bar.field;\n"
    3234                 :            :                   " ^~~   ~~~ ~~~~~\n"
    3235                 :            :                   " |     |   |\n"
    3236                 :            :                   " |     |   c\n"
    3237                 :            :                   " aaaaa bbbb\n",
    3238                 :            :                   pp_formatted_text (dc.printer));
    3239                 :            :   }
    3240                 :            : 
    3241                 :            :   /* Example of out-of-order ranges (thus requiring a sort).  */
    3242                 :         32 :   {
    3243                 :         32 :     text_range_label label0 ("0");
    3244                 :         64 :     text_range_label label1 ("1");
    3245                 :         64 :     text_range_label label2 ("2");
    3246                 :         64 :     gcc_rich_location richloc (field, &label0);
    3247                 :         32 :     richloc.add_range (bar, SHOW_RANGE_WITHOUT_CARET, &label1);
    3248                 :         32 :     richloc.add_range (foo, SHOW_RANGE_WITHOUT_CARET, &label2);
    3249                 :            : 
    3250                 :         64 :     test_diagnostic_context dc;
    3251                 :         32 :     diagnostic_show_locus (&dc, &richloc, DK_ERROR);
    3252                 :         32 :     ASSERT_STREQ (" foo = bar.field;\n"
    3253                 :            :                   " ~~~   ~~~ ^~~~~\n"
    3254                 :            :                   " |     |   |\n"
    3255                 :            :                   " 2     1   0\n",
    3256                 :            :                   pp_formatted_text (dc.printer));
    3257                 :            :   }
    3258                 :            : 
    3259                 :            :   /* Ensure we don't ICE if multiple ranges with labels are on
    3260                 :            :      the same point.  */
    3261                 :         32 :   {
    3262                 :         32 :     text_range_label label0 ("label 0");
    3263                 :         64 :     text_range_label label1 ("label 1");
    3264                 :         64 :     text_range_label label2 ("label 2");
    3265                 :         64 :     gcc_rich_location richloc (bar, &label0);
    3266                 :         32 :     richloc.add_range (bar, SHOW_RANGE_WITHOUT_CARET, &label1);
    3267                 :         32 :     richloc.add_range (bar, SHOW_RANGE_WITHOUT_CARET, &label2);
    3268                 :            : 
    3269                 :         64 :     test_diagnostic_context dc;
    3270                 :         32 :     diagnostic_show_locus (&dc, &richloc, DK_ERROR);
    3271                 :         32 :     ASSERT_STREQ (" foo = bar.field;\n"
    3272                 :            :                   "       ^~~\n"
    3273                 :            :                   "       |\n"
    3274                 :            :                   "       label 0\n"
    3275                 :            :                   "       label 1\n"
    3276                 :            :                   "       label 2\n",
    3277                 :            :                   pp_formatted_text (dc.printer));
    3278                 :            :   }
    3279                 :            : 
    3280                 :            :   /* Example of out-of-order ranges (thus requiring a sort), where
    3281                 :            :      they overlap, and there are multiple ranges on the same point.  */
    3282                 :         32 :   {
    3283                 :         32 :     text_range_label label_0a ("label 0a");
    3284                 :         64 :     text_range_label label_1a ("label 1a");
    3285                 :         64 :     text_range_label label_2a ("label 2a");
    3286                 :         64 :     text_range_label label_0b ("label 0b");
    3287                 :         64 :     text_range_label label_1b ("label 1b");
    3288                 :         64 :     text_range_label label_2b ("label 2b");
    3289                 :         64 :     text_range_label label_0c ("label 0c");
    3290                 :         64 :     text_range_label label_1c ("label 1c");
    3291                 :         64 :     text_range_label label_2c ("label 2c");
    3292                 :         64 :     gcc_rich_location richloc (field, &label_0a);
    3293                 :         32 :     richloc.add_range (bar, SHOW_RANGE_WITHOUT_CARET, &label_1a);
    3294                 :         32 :     richloc.add_range (foo, SHOW_RANGE_WITHOUT_CARET, &label_2a);
    3295                 :            : 
    3296                 :         32 :     richloc.add_range (field, SHOW_RANGE_WITHOUT_CARET, &label_0b);
    3297                 :         32 :     richloc.add_range (bar, SHOW_RANGE_WITHOUT_CARET, &label_1b);
    3298                 :         32 :     richloc.add_range (foo, SHOW_RANGE_WITHOUT_CARET, &label_2b);
    3299                 :            : 
    3300                 :         32 :     richloc.add_range (field, SHOW_RANGE_WITHOUT_CARET, &label_0c);
    3301                 :         32 :     richloc.add_range (bar, SHOW_RANGE_WITHOUT_CARET, &label_1c);
    3302                 :         32 :     richloc.add_range (foo, SHOW_RANGE_WITHOUT_CARET, &label_2c);
    3303                 :            : 
    3304                 :         64 :     test_diagnostic_context dc;
    3305                 :         32 :     diagnostic_show_locus (&dc, &richloc, DK_ERROR);
    3306                 :         32 :     ASSERT_STREQ (" foo = bar.field;\n"
    3307                 :            :                   " ~~~   ~~~ ^~~~~\n"
    3308                 :            :                   " |     |   |\n"
    3309                 :            :                   " |     |   label 0a\n"
    3310                 :            :                   " |     |   label 0b\n"
    3311                 :            :                   " |     |   label 0c\n"
    3312                 :            :                   " |     label 1a\n"
    3313                 :            :                   " |     label 1b\n"
    3314                 :            :                   " |     label 1c\n"
    3315                 :            :                   " label 2a\n"
    3316                 :            :                   " label 2b\n"
    3317                 :            :                   " label 2c\n",
    3318                 :            :                   pp_formatted_text (dc.printer));
    3319                 :            :   }
    3320                 :            : 
    3321                 :            :   /* Verify that a NULL result from range_label::get_text is
    3322                 :            :      handled gracefully.  */
    3323                 :         32 :   {
    3324                 :         32 :     text_range_label label (NULL);
    3325                 :         32 :     gcc_rich_location richloc (bar, &label);
    3326                 :            : 
    3327                 :         64 :     test_diagnostic_context dc;
    3328                 :         32 :     diagnostic_show_locus (&dc, &richloc, DK_ERROR);
    3329                 :         32 :     ASSERT_STREQ (" foo = bar.field;\n"
    3330                 :            :                   "       ^~~\n",
    3331                 :            :                   pp_formatted_text (dc.printer));
    3332                 :            :    }
    3333                 :            : 
    3334                 :            :   /* TODO: example of formatted printing (needs to be in
    3335                 :            :      gcc-rich-location.c due to Makefile.in issues).  */
    3336                 :         32 : }
    3337                 :            : 
    3338                 :            : /* Run the various one-liner tests.  */
    3339                 :            : 
    3340                 :            : static void
    3341                 :         48 : test_diagnostic_show_locus_one_liner (const line_table_case &case_)
    3342                 :            : {
    3343                 :            :   /* Create a tempfile and write some text to it.
    3344                 :            :      ....................0000000001111111.
    3345                 :            :      ....................1234567890123456.  */
    3346                 :         48 :   const char *content = "foo = bar.field;\n";
    3347                 :         80 :   temp_source_file tmp (SELFTEST_LOCATION, ".c", content);
    3348                 :         80 :   line_table_test ltt (case_);
    3349                 :            : 
    3350                 :         48 :   linemap_add (line_table, LC_ENTER, false, tmp.get_filename (), 1);
    3351                 :            : 
    3352                 :         48 :   location_t line_end = linemap_position_for_column (line_table, 16);
    3353                 :            : 
    3354                 :            :   /* Don't attempt to run the tests if column data might be unavailable.  */
    3355                 :         48 :   if (line_end > LINE_MAP_MAX_LOCATION_WITH_COLS)
    3356                 :         16 :     return;
    3357                 :            : 
    3358                 :         32 :   ASSERT_STREQ (tmp.get_filename (), LOCATION_FILE (line_end));
    3359                 :         32 :   ASSERT_EQ (1, LOCATION_LINE (line_end));
    3360                 :         32 :   ASSERT_EQ (16, LOCATION_COLUMN (line_end));
    3361                 :            : 
    3362                 :         32 :   test_one_liner_simple_caret ();
    3363                 :         32 :   test_one_liner_caret_and_range ();
    3364                 :         32 :   test_one_liner_multiple_carets_and_ranges ();
    3365                 :         32 :   test_one_liner_fixit_insert_before ();
    3366                 :         32 :   test_one_liner_fixit_insert_after ();
    3367                 :         32 :   test_one_liner_fixit_remove ();
    3368                 :         32 :   test_one_liner_fixit_replace ();
    3369                 :         32 :   test_one_liner_fixit_replace_non_equal_range ();
    3370                 :         32 :   test_one_liner_fixit_replace_equal_secondary_range ();
    3371                 :         32 :   test_one_liner_fixit_validation_adhoc_locations ();
    3372                 :         32 :   test_one_liner_many_fixits_1 ();
    3373                 :         32 :   test_one_liner_many_fixits_2 ();
    3374                 :         32 :   test_one_liner_labels ();
    3375                 :            : }
    3376                 :            : 
    3377                 :            : /* Version of all one-liner tests exercising multibyte awareness.  For
    3378                 :            :    simplicity we stick to using two multibyte characters in the test, U+1F602
    3379                 :            :    == "\xf0\x9f\x98\x82", which uses 4 bytes and 2 display columns, and U+03C0
    3380                 :            :    == "\xcf\x80", which uses 2 bytes and 1 display column.  Note: all of the
    3381                 :            :    below asserts would be easier to read if we used UTF-8 directly in the
    3382                 :            :    string constants, but it seems better not to demand the host compiler
    3383                 :            :    support this, when it isn't otherwise necessary.  Instead, whenever an
    3384                 :            :    extended character appears in a string, we put a line break after it so that
    3385                 :            :    all succeeding characters can appear visually at the correct display column.
    3386                 :            : 
    3387                 :            :    All of these work on the following 1-line source file:
    3388                 :            : 
    3389                 :            :      .0000000001111111111222222   display
    3390                 :            :      .1234567890123456789012345   columns
    3391                 :            :      "SS_foo = P_bar.SS_fieldP;\n"
    3392                 :            :      .0000000111111111222222223   byte
    3393                 :            :      .1356789012456789134567891   columns
    3394                 :            : 
    3395                 :            :    which is set up by test_diagnostic_show_locus_one_liner and calls
    3396                 :            :    them.  Here SS represents the two display columns for the U+1F602 emoji and
    3397                 :            :    P represents the one display column for the U+03C0 pi symbol.  */
    3398                 :            : 
    3399                 :            : /* Just a caret.  */
    3400                 :            : 
    3401                 :            : static void
    3402                 :         32 : test_one_liner_simple_caret_utf8 ()
    3403                 :            : {
    3404                 :         64 :   test_diagnostic_context dc;
    3405                 :         32 :   location_t caret = linemap_position_for_column (line_table, 18);
    3406                 :         32 :   rich_location richloc (line_table, caret);
    3407                 :         32 :   diagnostic_show_locus (&dc, &richloc, DK_ERROR);
    3408                 :         32 :   ASSERT_STREQ (" \xf0\x9f\x98\x82"
    3409                 :            :                    "_foo = \xcf\x80"
    3410                 :            :                            "_bar.\xf0\x9f\x98\x82"
    3411                 :            :                                   "_field\xcf\x80"
    3412                 :            :                                          ";\n"
    3413                 :            :                 "               ^\n",
    3414                 :            :                 pp_formatted_text (dc.printer));
    3415                 :         32 : }
    3416                 :            : 
    3417                 :            : /* Caret and range.  */
    3418                 :            : static void
    3419                 :         32 : test_one_liner_caret_and_range_utf8 ()
    3420                 :            : {
    3421                 :         64 :   test_diagnostic_context dc;
    3422                 :         32 :   location_t caret = linemap_position_for_column (line_table, 18);
    3423                 :         32 :   location_t start = linemap_position_for_column (line_table, 12);
    3424                 :         32 :   location_t finish = linemap_position_for_column (line_table, 30);
    3425                 :         32 :   location_t loc = make_location (caret, start, finish);
    3426                 :         32 :   rich_location richloc (line_table, loc);
    3427                 :         32 :   diagnostic_show_locus (&dc, &richloc, DK_ERROR);
    3428                 :         32 :   ASSERT_STREQ (" \xf0\x9f\x98\x82"
    3429                 :            :                    "_foo = \xcf\x80"
    3430                 :            :                            "_bar.\xf0\x9f\x98\x82"
    3431                 :            :                                   "_field\xcf\x80"
    3432                 :            :                                          ";\n"
    3433                 :            :                 "          ~~~~~^~~~~~~~~~\n",
    3434                 :            :                 pp_formatted_text (dc.printer));
    3435                 :         32 : }
    3436                 :            : 
    3437                 :            : /* Multiple ranges and carets.  */
    3438                 :            : 
    3439                 :            : static void
    3440                 :         32 : test_one_liner_multiple_carets_and_ranges_utf8 ()
    3441                 :            : {
    3442                 :         64 :   test_diagnostic_context dc;
    3443                 :         32 :   location_t foo
    3444                 :         32 :     = make_location (linemap_position_for_column (line_table, 7),
    3445                 :            :                      linemap_position_for_column (line_table, 1),
    3446                 :            :                      linemap_position_for_column (line_table, 8));
    3447                 :         32 :   dc.caret_chars[0] = 'A';
    3448                 :            : 
    3449                 :         32 :   location_t bar
    3450                 :         32 :     = make_location (linemap_position_for_column (line_table, 16),
    3451                 :            :                      linemap_position_for_column (line_table, 12),
    3452                 :            :                      linemap_position_for_column (line_table, 17));
    3453                 :         32 :   dc.caret_chars[1] = 'B';
    3454                 :            : 
    3455                 :         32 :   location_t field
    3456                 :         32 :     = make_location (linemap_position_for_column (line_table, 26),
    3457                 :            :                      linemap_position_for_column (line_table, 19),
    3458                 :            :                      linemap_position_for_column (line_table, 30));
    3459                 :         32 :   dc.caret_chars[2] = 'C';
    3460                 :         32 :   rich_location richloc (line_table, foo);
    3461                 :         32 :   richloc.add_range (bar, SHOW_RANGE_WITH_CARET);
    3462                 :         32 :   richloc.add_range (field, SHOW_RANGE_WITH_CARET);
    3463                 :         32 :   diagnostic_show_locus (&dc, &richloc, DK_ERROR);
    3464                 :         32 :   ASSERT_STREQ (" \xf0\x9f\x98\x82"
    3465                 :            :                    "_foo = \xcf\x80"
    3466                 :            :                            "_bar.\xf0\x9f\x98\x82"
    3467                 :            :                                   "_field\xcf\x80"
    3468                 :            :                                          ";\n"
    3469                 :            :                 " ~~~~A~   ~~~B~ ~~~~~C~~~\n",
    3470                 :            :                 pp_formatted_text (dc.printer));
    3471                 :         32 : }
    3472                 :            : 
    3473                 :            : /* Insertion fix-it hint: adding an "&" to the front of "P_bar.field". */
    3474                 :            : 
    3475                 :            : static void
    3476                 :         32 : test_one_liner_fixit_insert_before_utf8 ()
    3477                 :            : {
    3478                 :         64 :   test_diagnostic_context dc;
    3479                 :         32 :   location_t caret = linemap_position_for_column (line_table, 12);
    3480                 :         32 :   rich_location richloc (line_table, caret);
    3481                 :         32 :   richloc.add_fixit_insert_before ("&");
    3482                 :         32 :   diagnostic_show_locus (&dc, &richloc, DK_ERROR);
    3483                 :         32 :   ASSERT_STREQ (" \xf0\x9f\x98\x82"
    3484                 :            :                    "_foo = \xcf\x80"
    3485                 :            :                            "_bar.\xf0\x9f\x98\x82"
    3486                 :            :                                   "_field\xcf\x80"
    3487                 :            :                                          ";\n"
    3488                 :            :                 "          ^\n"
    3489                 :            :                 "          &\n",
    3490                 :            :                 pp_formatted_text (dc.printer));
    3491                 :         32 : }
    3492                 :            : 
    3493                 :            : /* Insertion fix-it hint: adding a "[0]" after "SS_foo". */
    3494                 :            : 
    3495                 :            : static void
    3496                 :         32 : test_one_liner_fixit_insert_after_utf8 ()
    3497                 :            : {
    3498                 :         64 :   test_diagnostic_context dc;
    3499                 :         32 :   location_t start = linemap_position_for_column (line_table, 1);
    3500                 :         32 :   location_t finish = linemap_position_for_column (line_table, 8);
    3501                 :         32 :   location_t foo = make_location (start, start, finish);
    3502                 :         32 :   rich_location richloc (line_table, foo);
    3503                 :         32 :   richloc.add_fixit_insert_after ("[0]");
    3504                 :         32 :   diagnostic_show_locus (&dc, &richloc, DK_ERROR);
    3505                 :         32 :   ASSERT_STREQ (" \xf0\x9f\x98\x82"
    3506                 :            :                    "_foo = \xcf\x80"
    3507                 :            :                            "_bar.\xf0\x9f\x98\x82"
    3508                 :            :                                   "_field\xcf\x80"
    3509                 :            :                                          ";\n"
    3510                 :            :                 " ^~~~~~\n"
    3511                 :            :                 "       [0]\n",
    3512                 :            :                 pp_formatted_text (dc.printer));
    3513                 :         32 : }
    3514                 :            : 
    3515                 :            : /* Removal fix-it hint: removal of the ".SS_fieldP". */
    3516                 :            : 
    3517                 :            : static void
    3518                 :         32 : test_one_liner_fixit_remove_utf8 ()
    3519                 :            : {
    3520                 :         64 :   test_diagnostic_context dc;
    3521                 :         32 :   location_t start = linemap_position_for_column (line_table, 18);
    3522                 :         32 :   location_t finish = linemap_position_for_column (line_table, 30);
    3523                 :         32 :   location_t dot = make_location (start, start, finish);
    3524                 :         32 :   rich_location richloc (line_table, dot);
    3525                 :         32 :   richloc.add_fixit_remove ();
    3526                 :         32 :   diagnostic_show_locus (&dc, &richloc, DK_ERROR);
    3527                 :         32 :   ASSERT_STREQ (" \xf0\x9f\x98\x82"
    3528                 :            :                    "_foo = \xcf\x80"
    3529                 :            :                            "_bar.\xf0\x9f\x98\x82"
    3530                 :            :                                   "_field\xcf\x80"
    3531                 :            :                                          ";\n"
    3532                 :            :                 "               ^~~~~~~~~~\n"
    3533                 :            :                 "               ----------\n",
    3534                 :            :                 pp_formatted_text (dc.printer));
    3535                 :         32 : }
    3536                 :            : 
    3537                 :            : /* Replace fix-it hint: replacing "SS_fieldP" with "m_SSfieldP". */
    3538                 :            : 
    3539                 :            : static void
    3540                 :         32 : test_one_liner_fixit_replace_utf8 ()
    3541                 :            : {
    3542                 :         64 :   test_diagnostic_context dc;
    3543                 :         32 :   location_t start = linemap_position_for_column (line_table, 19);
    3544                 :         32 :   location_t finish = linemap_position_for_column (line_table, 30);
    3545                 :         32 :   location_t field = make_location (start, start, finish);
    3546                 :         32 :   rich_location richloc (line_table, field);
    3547                 :         32 :   richloc.add_fixit_replace ("m_\xf0\x9f\x98\x82_field\xcf\x80");
    3548                 :         32 :   diagnostic_show_locus (&dc, &richloc, DK_ERROR);
    3549                 :         32 :   ASSERT_STREQ (" \xf0\x9f\x98\x82"
    3550                 :            :                    "_foo = \xcf\x80"
    3551                 :            :                            "_bar.\xf0\x9f\x98\x82"
    3552                 :            :                                   "_field\xcf\x80"
    3553                 :            :                                          ";\n"
    3554                 :            :                 "                ^~~~~~~~~\n"
    3555                 :            :                 "                m_\xf0\x9f\x98\x82"
    3556                 :            :                                     "_field\xcf\x80\n",
    3557                 :            :                 pp_formatted_text (dc.printer));
    3558                 :         32 : }
    3559                 :            : 
    3560                 :            : /* Replace fix-it hint: replacing "SS_fieldP" with "m_SSfieldP",
    3561                 :            :    but where the caret was elsewhere.  */
    3562                 :            : 
    3563                 :            : static void
    3564                 :         32 : test_one_liner_fixit_replace_non_equal_range_utf8 ()
    3565                 :            : {
    3566                 :         64 :   test_diagnostic_context dc;
    3567                 :         32 :   location_t equals = linemap_position_for_column (line_table, 10);
    3568                 :         32 :   location_t start = linemap_position_for_column (line_table, 19);
    3569                 :         32 :   location_t finish = linemap_position_for_column (line_table, 30);
    3570                 :         32 :   rich_location richloc (line_table, equals);
    3571                 :         32 :   source_range range;
    3572                 :         32 :   range.m_start = start;
    3573                 :         32 :   range.m_finish = finish;
    3574                 :         32 :   richloc.add_fixit_replace (range, "m_\xf0\x9f\x98\x82_field\xcf\x80");
    3575                 :         32 :   diagnostic_show_locus (&dc, &richloc, DK_ERROR);
    3576                 :            :   /* The replacement range is not indicated in the annotation line, so
    3577                 :            :      it should be indicated via an additional underline.  */
    3578                 :         32 :   ASSERT_STREQ (" \xf0\x9f\x98\x82"
    3579                 :            :                    "_foo = \xcf\x80"
    3580                 :            :                            "_bar.\xf0\x9f\x98\x82"
    3581                 :            :                                   "_field\xcf\x80"
    3582                 :            :                                          ";\n"
    3583                 :            :                 "        ^\n"
    3584                 :            :                 "                ---------\n"
    3585                 :            :                 "                m_\xf0\x9f\x98\x82"
    3586                 :            :                                     "_field\xcf\x80\n",
    3587                 :            :                 pp_formatted_text (dc.printer));
    3588                 :         32 : }
    3589                 :            : 
    3590                 :            : /* Replace fix-it hint: replacing "SS_fieldP" with "m_SSfieldP",
    3591                 :            :    where the caret was elsewhere, but where a secondary range
    3592                 :            :    exactly covers "field".  */
    3593                 :            : 
    3594                 :            : static void
    3595                 :         32 : test_one_liner_fixit_replace_equal_secondary_range_utf8 ()
    3596                 :            : {
    3597                 :         64 :   test_diagnostic_context dc;
    3598                 :         32 :   location_t equals = linemap_position_for_column (line_table, 10);
    3599                 :         32 :   location_t start = linemap_position_for_column (line_table, 19);
    3600                 :         32 :   location_t finish = linemap_position_for_column (line_table, 30);
    3601                 :         32 :   rich_location richloc (line_table, equals);
    3602                 :         32 :   location_t field = make_location (start, start, finish);
    3603                 :         32 :   richloc.add_range (field);
    3604                 :         32 :   richloc.add_fixit_replace (field, "m_\xf0\x9f\x98\x82_field\xcf\x80");
    3605                 :         32 :   diagnostic_show_locus (&dc, &richloc, DK_ERROR);
    3606                 :            :   /* The replacement range is indicated in the annotation line,
    3607                 :            :      so it shouldn't be indicated via an additional underline.  */
    3608                 :         32 :   ASSERT_STREQ (" \xf0\x9f\x98\x82"
    3609                 :            :                    "_foo = \xcf\x80"
    3610                 :            :                            "_bar.\xf0\x9f\x98\x82"
    3611                 :            :                                   "_field\xcf\x80"
    3612                 :            :                                          ";\n"
    3613                 :            :                 "        ^       ~~~~~~~~~\n"
    3614                 :            :                 "                m_\xf0\x9f\x98\x82"
    3615                 :            :                                     "_field\xcf\x80\n",
    3616                 :            :                 pp_formatted_text (dc.printer));
    3617                 :         32 : }
    3618                 :            : 
    3619                 :            : /* Verify that we can use ad-hoc locations when adding fixits to a
    3620                 :            :    rich_location.  */
    3621                 :            : 
    3622                 :            : static void
    3623                 :         32 : test_one_liner_fixit_validation_adhoc_locations_utf8 ()
    3624                 :            : {
    3625                 :            :   /* Generate a range that's too long to be packed, so must
    3626                 :            :      be stored as an ad-hoc location (given the defaults
    3627                 :            :      of 5 bits or 0 bits of packed range); 41 columns > 2**5.  */
    3628                 :         32 :   const location_t c12 = linemap_position_for_column (line_table, 12);
    3629                 :         32 :   const location_t c52 = linemap_position_for_column (line_table, 52);
    3630                 :         32 :   const location_t loc = make_location (c12, c12, c52);
    3631                 :            : 
    3632                 :         32 :   if (c52 > LINE_MAP_MAX_LOCATION_WITH_COLS)
    3633                 :            :     return;
    3634                 :            : 
    3635                 :         32 :   ASSERT_TRUE (IS_ADHOC_LOC (loc));
    3636                 :            : 
    3637                 :            :   /* Insert.  */
    3638                 :         32 :   {
    3639                 :         64 :     rich_location richloc (line_table, loc);
    3640                 :         32 :     richloc.add_fixit_insert_before (loc, "test");
    3641                 :            :     /* It should not have been discarded by the validator.  */
    3642                 :         32 :     ASSERT_EQ (1, richloc.get_num_fixit_hints ());
    3643                 :            : 
    3644                 :         32 :     test_diagnostic_context dc;
    3645                 :         32 :     diagnostic_show_locus (&dc, &richloc, DK_ERROR);
    3646                 :         32 :     ASSERT_STREQ (" \xf0\x9f\x98\x82"
    3647                 :            :                      "_foo = \xcf\x80"
    3648                 :            :                              "_bar.\xf0\x9f\x98\x82"
    3649                 :            :                                     "_field\xcf\x80"
    3650                 :            :                                            ";\n"
    3651                 :            :                   "          ^~~~~~~~~~~~~~~~                     \n"
    3652                 :            :                   "          test\n",
    3653                 :            :                 pp_formatted_text (dc.printer));
    3654                 :            :   }
    3655                 :            : 
    3656                 :            :   /* Remove.  */
    3657                 :         32 :   {
    3658                 :         64 :     rich_location richloc (line_table, loc);
    3659                 :         32 :     source_range range = source_range::from_locations (loc, c52);
    3660                 :         32 :     richloc.add_fixit_remove (range);
    3661                 :            :     /* It should not have been discarded by the validator.  */
    3662                 :         32 :     ASSERT_EQ (1, richloc.get_num_fixit_hints ());
    3663                 :            : 
    3664                 :         32 :     test_diagnostic_context dc;
    3665                 :         32 :     diagnostic_show_locus (&dc, &richloc, DK_ERROR);
    3666                 :         32 :     ASSERT_STREQ (" \xf0\x9f\x98\x82"
    3667                 :            :                      "_foo = \xcf\x80"
    3668                 :            :                              "_bar.\xf0\x9f\x98\x82"
    3669                 :            :                                     "_field\xcf\x80"
    3670                 :            :                                            ";\n"
    3671                 :            :                   "          ^~~~~~~~~~~~~~~~                     \n"
    3672                 :            :                   "          -------------------------------------\n",
    3673                 :            :                 pp_formatted_text (dc.printer));
    3674                 :            :   }
    3675                 :            : 
    3676                 :            :   /* Replace.  */
    3677                 :         32 :   {
    3678                 :         64 :     rich_location richloc (line_table, loc);
    3679                 :         32 :     source_range range = source_range::from_locations (loc, c52);
    3680                 :         32 :     richloc.add_fixit_replace (range, "test");
    3681                 :            :     /* It should not have been discarded by the validator.  */
    3682                 :         32 :     ASSERT_EQ (1, richloc.get_num_fixit_hints ());
    3683                 :            : 
    3684                 :         32 :     test_diagnostic_context dc;
    3685                 :         32 :     diagnostic_show_locus (&dc, &richloc, DK_ERROR);
    3686                 :         32 :     ASSERT_STREQ (" \xf0\x9f\x98\x82"
    3687                 :            :                      "_foo = \xcf\x80"
    3688                 :            :                              "_bar.\xf0\x9f\x98\x82"
    3689                 :            :                                     "_field\xcf\x80"
    3690                 :            :                                            ";\n"
    3691                 :            :                   "          ^~~~~~~~~~~~~~~~                     \n"
    3692                 :            :                   "          test\n",
    3693                 :            :                 pp_formatted_text (dc.printer));
    3694                 :            :   }
    3695                 :            : }
    3696                 :            : 
    3697                 :            : /* Test of consolidating insertions at the same location.  */
    3698                 :            : 
    3699                 :            : static void
    3700                 :         32 : test_one_liner_many_fixits_1_utf8 ()
    3701                 :            : {
    3702                 :         64 :   test_diagnostic_context dc;
    3703                 :         32 :   location_t equals = linemap_position_for_column (line_table, 10);
    3704                 :         64 :   rich_location richloc (line_table, equals);
    3705                 :        640 :   for (int i = 0; i < 19; i++)
    3706                 :        928 :     richloc.add_fixit_insert_before (i & 1 ? "@" : "\xcf\x80");
    3707                 :         32 :   ASSERT_EQ (1, richloc.get_num_fixit_hints ());
    3708                 :         32 :   diagnostic_show_locus (&dc, &richloc, DK_ERROR);
    3709                 :         32 :   ASSERT_STREQ (" \xf0\x9f\x98\x82"
    3710                 :            :                    "_foo = \xcf\x80"
    3711                 :            :                            "_bar.\xf0\x9f\x98\x82"
    3712                 :            :                                   "_field\xcf\x80"
    3713                 :            :                                          ";\n"
    3714                 :            :                 "        ^\n"
    3715                 :            :                 "        \xcf\x80@\xcf\x80@\xcf\x80@\xcf\x80@\xcf\x80@"
    3716                 :            :                 "\xcf\x80@\xcf\x80@\xcf\x80@\xcf\x80@\xcf\x80\n",
    3717                 :            :                 pp_formatted_text (dc.printer));
    3718                 :         32 : }
    3719                 :            : 
    3720                 :            : /* Ensure that we can add an arbitrary number of fix-it hints to a
    3721                 :            :    rich_location, even if they are not consolidated.  */
    3722                 :            : 
    3723                 :            : static void
    3724                 :         32 : test_one_liner_many_fixits_2_utf8 ()
    3725                 :            : {
    3726                 :         64 :   test_diagnostic_context dc;
    3727                 :         32 :   location_t equals = linemap_position_for_column (line_table, 10);
    3728                 :         32 :   rich_location richloc (line_table, equals);
    3729                 :         32 :   const int nlocs = 19;
    3730                 :         32 :   int locs[nlocs] = {1, 5, 7, 9, 11, 14, 16, 18, 23, 25, 27, 29, 32,
    3731                 :            :                      34, 36, 38, 40, 42, 44};
    3732                 :        640 :   for (int i = 0; i != nlocs; ++i)
    3733                 :            :     {
    3734                 :        608 :       location_t loc = linemap_position_for_column (line_table, locs[i]);
    3735                 :        928 :       richloc.add_fixit_insert_before (loc, i & 1 ? "@" : "\xcf\x80");
    3736                 :            :     }
    3737                 :            : 
    3738                 :         32 :   ASSERT_EQ (nlocs, richloc.get_num_fixit_hints ());
    3739                 :         32 :   diagnostic_show_locus (&dc, &richloc, DK_ERROR);
    3740                 :         32 :   ASSERT_STREQ (" \xf0\x9f\x98\x82"
    3741                 :            :                    "_foo = \xcf\x80"
    3742                 :            :                            "_bar.\xf0\x9f\x98\x82"
    3743                 :            :                                   "_field\xcf\x80"
    3744                 :            :                                          ";\n"
    3745                 :            :                 "        ^\n"
    3746                 :            :                 " \xcf\x80 @ \xcf\x80 @ \xcf\x80 @ \xcf\x80 @  \xcf\x80 @"
    3747                 :            :                 " \xcf\x80 @ \xcf\x80 @ \xcf\x80 @ \xcf\x80 @ \xcf\x80\n",
    3748                 :            :                 pp_formatted_text (dc.printer));
    3749                 :         32 : }
    3750                 :            : 
    3751                 :            : /* Test of labeling the ranges within a rich_location.  */
    3752                 :            : 
    3753                 :            : static void
    3754                 :         32 : test_one_liner_labels_utf8 ()
    3755                 :            : {
    3756                 :         32 :   location_t foo
    3757                 :         32 :     = make_location (linemap_position_for_column (line_table, 1),
    3758                 :            :                      linemap_position_for_column (line_table, 1),
    3759                 :            :                      linemap_position_for_column (line_table, 8));
    3760                 :         32 :   location_t bar
    3761                 :         32 :     = make_location (linemap_position_for_column (line_table, 12),
    3762                 :            :                      linemap_position_for_column (line_table, 12),
    3763                 :            :                      linemap_position_for_column (line_table, 17));
    3764                 :         32 :   location_t field
    3765                 :         32 :     = make_location (linemap_position_for_column (line_table, 19),
    3766                 :            :                      linemap_position_for_column (line_table, 19),
    3767                 :            :                      linemap_position_for_column (line_table, 30));
    3768                 :            : 
    3769                 :            :   /* Example where all the labels fit on one line.  */
    3770                 :         32 :   {
    3771                 :            :     /* These three labels contain multibyte characters such that their byte
    3772                 :            :        lengths are respectively (12, 10, 18), but their display widths are only
    3773                 :            :        (6, 5, 9).  All three fit on the line when considering the display
    3774                 :            :        widths, but not when considering the byte widths, so verify that we do
    3775                 :            :        indeed put them all on one line.  */
    3776                 :         32 :     text_range_label label0
    3777                 :         64 :       ("\xcf\x80\xcf\x80\xcf\x80\xcf\x80\xcf\x80\xcf\x80");
    3778                 :         32 :     text_range_label label1
    3779                 :         64 :       ("\xf0\x9f\x98\x82\xf0\x9f\x98\x82\xcf\x80");
    3780                 :         32 :     text_range_label label2
    3781                 :            :       ("\xf0\x9f\x98\x82\xcf\x80\xf0\x9f\x98\x82\xf0\x9f\x98\x82\xcf\x80"
    3782                 :         64 :        "\xcf\x80");
    3783                 :         64 :     gcc_rich_location richloc (foo, &label0);
    3784                 :         32 :     richloc.add_range (bar, SHOW_RANGE_WITHOUT_CARET, &label1);
    3785                 :         32 :     richloc.add_range (field, SHOW_RANGE_WITHOUT_CARET, &label2);
    3786                 :            : 
    3787                 :         32 :     {
    3788                 :         32 :       test_diagnostic_context dc;
    3789                 :         32 :       diagnostic_show_locus (&dc, &richloc, DK_ERROR);
    3790                 :         32 :       ASSERT_STREQ (" \xf0\x9f\x98\x82"
    3791                 :            :                        "_foo = \xcf\x80"
    3792                 :            :                                "_bar.\xf0\x9f\x98\x82"
    3793                 :            :                                       "_field\xcf\x80"
    3794                 :            :                                              ";\n"
    3795                 :            :                     " ^~~~~~   ~~~~~ ~~~~~~~~~\n"
    3796                 :            :                     " |        |     |\n"
    3797                 :            :                     " \xcf\x80\xcf\x80\xcf\x80\xcf\x80\xcf\x80\xcf\x80"
    3798                 :            :                            "   \xf0\x9f\x98\x82\xf0\x9f\x98\x82\xcf\x80"
    3799                 :            :                                    " \xf0\x9f\x98\x82\xcf\x80\xf0\x9f\x98\x82"
    3800                 :            :                                          "\xf0\x9f\x98\x82\xcf\x80\xcf\x80\n",
    3801                 :            :                     pp_formatted_text (dc.printer));
    3802                 :            :     }
    3803                 :            : 
    3804                 :            :   }
    3805                 :            : 
    3806                 :            :   /* Example where the labels need extra lines.  */
    3807                 :         32 :   {
    3808                 :         32 :     text_range_label label0 ("label 0\xf0\x9f\x98\x82");
    3809                 :         64 :     text_range_label label1 ("label 1\xcf\x80");
    3810                 :         64 :     text_range_label label2 ("label 2\xcf\x80");
    3811                 :         64 :     gcc_rich_location richloc (foo, &label0);
    3812                 :         32 :     richloc.add_range (bar, SHOW_RANGE_WITHOUT_CARET, &label1);
    3813                 :         32 :     richloc.add_range (field, SHOW_RANGE_WITHOUT_CARET, &label2);
    3814                 :            : 
    3815                 :         64 :     test_diagnostic_context dc;
    3816                 :         32 :     diagnostic_show_locus (&dc, &richloc, DK_ERROR);
    3817                 :            : 
    3818                 :         32 :     ASSERT_STREQ (" \xf0\x9f\x98\x82"
    3819                 :            :                      "_foo = \xcf\x80"
    3820                 :            :                              "_bar.\xf0\x9f\x98\x82"
    3821                 :            :                                     "_field\xcf\x80"
    3822                 :            :                                            ";\n"
    3823                 :            :                   " ^~~~~~   ~~~~~ ~~~~~~~~~\n"
    3824                 :            :                   " |        |     |\n"
    3825                 :            :                   " |        |     label 2\xcf\x80\n"
    3826                 :            :                   " |        label 1\xcf\x80\n"
    3827                 :            :                   " label 0\xf0\x9f\x98\x82\n",
    3828                 :            :                   pp_formatted_text (dc.printer));
    3829                 :            :   }
    3830                 :            : 
    3831                 :            :   /* Example of boundary conditions: label 0 and 1 have just enough clearance,
    3832                 :            :      but label 1 just touches label 2.  */
    3833                 :         32 :   {
    3834                 :         32 :     text_range_label label0 ("aaaaa\xf0\x9f\x98\x82\xcf\x80");
    3835                 :         64 :     text_range_label label1 ("bb\xf0\x9f\x98\x82\xf0\x9f\x98\x82");
    3836                 :         64 :     text_range_label label2 ("c");
    3837                 :         32 :     gcc_rich_location richloc (foo, &label0);
    3838                 :         32 :     richloc.add_range (bar, SHOW_RANGE_WITHOUT_CARET, &label1);
    3839                 :         32 :     richloc.add_range (field, SHOW_RANGE_WITHOUT_CARET, &label2);
    3840                 :            : 
    3841                 :         64 :     test_diagnostic_context dc;
    3842                 :         32 :     diagnostic_show_locus (&dc, &richloc, DK_ERROR);
    3843                 :         32 :     ASSERT_STREQ (" \xf0\x9f\x98\x82"
    3844                 :            :                      "_foo = \xcf\x80"
    3845                 :            :                              "_bar.\xf0\x9f\x98\x82"
    3846                 :            :                                     "_field\xcf\x80"
    3847                 :            :                                            ";\n"
    3848                 :            :                   " ^~~~~~   ~~~~~ ~~~~~~~~~\n"
    3849                 :            :                   " |        |     |\n"
    3850                 :            :                   " |        |     c\n"
    3851                 :            :                   " aaaaa\xf0\x9f\x98\x82\xcf\x80"
    3852                 :            :                            " bb\xf0\x9f\x98\x82\xf0\x9f\x98\x82\n",
    3853                 :            :                   pp_formatted_text (dc.printer));
    3854                 :            :   }
    3855                 :         32 : }
    3856                 :            : 
    3857                 :            : /* Run the various one-liner tests.  */
    3858                 :            : 
    3859                 :            : static void
    3860                 :         48 : test_diagnostic_show_locus_one_liner_utf8 (const line_table_case &case_)
    3861                 :            : {
    3862                 :            :   /* Create a tempfile and write some text to it.  */
    3863                 :         48 :   const char *content
    3864                 :            :     /* Display columns.
    3865                 :            :        0000000000000000000000011111111111111111111111111111112222222222222
    3866                 :            :        1111111122222222345678900000000123456666666677777777890123444444445  */
    3867                 :            :     = "\xf0\x9f\x98\x82_foo = \xcf\x80_bar.\xf0\x9f\x98\x82_field\xcf\x80;\n";
    3868                 :            :     /* 0000000000000000000001111111111111111111222222222222222222222233333
    3869                 :            :        1111222233334444567890122223333456789999000011112222345678999900001
    3870                 :            :        Byte columns.  */
    3871                 :         80 :   temp_source_file tmp (SELFTEST_LOCATION, ".c", content);
    3872                 :         80 :   line_table_test ltt (case_);
    3873                 :            : 
    3874                 :         48 :   linemap_add (line_table, LC_ENTER, false, tmp.get_filename (), 1);
    3875                 :            : 
    3876                 :         48 :   location_t line_end = linemap_position_for_column (line_table, 31);
    3877                 :            : 
    3878                 :            :   /* Don't attempt to run the tests if column data might be unavailable.  */
    3879                 :         48 :   if (line_end > LINE_MAP_MAX_LOCATION_WITH_COLS)
    3880                 :         16 :     return;
    3881                 :            : 
    3882                 :         32 :   ASSERT_STREQ (tmp.get_filename (), LOCATION_FILE (line_end));
    3883                 :         32 :   ASSERT_EQ (1, LOCATION_LINE (line_end));
    3884                 :         32 :   ASSERT_EQ (31, LOCATION_COLUMN (line_end));
    3885                 :            : 
    3886                 :         32 :   char_span lspan = location_get_source_line (tmp.get_filename (), 1);
    3887                 :         32 :   ASSERT_EQ (25, cpp_display_width (lspan.get_buffer (), lspan.length ()));
    3888                 :         32 :   ASSERT_EQ (25, location_compute_display_column (expand_location (line_end)));
    3889                 :            : 
    3890                 :         32 :   test_one_liner_simple_caret_utf8 ();
    3891                 :         32 :   test_one_liner_caret_and_range_utf8 ();
    3892                 :         32 :   test_one_liner_multiple_carets_and_ranges_utf8 ();
    3893                 :         32 :   test_one_liner_fixit_insert_before_utf8 ();
    3894                 :         32 :   test_one_liner_fixit_insert_after_utf8 ();
    3895                 :         32 :   test_one_liner_fixit_remove_utf8 ();
    3896                 :         32 :   test_one_liner_fixit_replace_utf8 ();
    3897                 :         32 :   test_one_liner_fixit_replace_non_equal_range_utf8 ();
    3898                 :         32 :   test_one_liner_fixit_replace_equal_secondary_range_utf8 ();
    3899                 :         32 :   test_one_liner_fixit_validation_adhoc_locations_utf8 ();
    3900                 :         32 :   test_one_liner_many_fixits_1_utf8 ();
    3901                 :         32 :   test_one_liner_many_fixits_2_utf8 ();
    3902                 :         32 :   test_one_liner_labels_utf8 ();
    3903                 :            : }
    3904                 :            : 
    3905                 :            : /* Verify that gcc_rich_location::add_location_if_nearby works.  */
    3906                 :            : 
    3907                 :            : static void
    3908                 :         48 : test_add_location_if_nearby (const line_table_case &case_)
    3909                 :            : {
    3910                 :            :   /* Create a tempfile and write some text to it.
    3911                 :            :      ...000000000111111111122222222223333333333.
    3912                 :            :      ...123456789012345678901234567890123456789.  */
    3913                 :         48 :   const char *content
    3914                 :            :     = ("struct same_line { double x; double y; ;\n" /* line 1.  */
    3915                 :            :        "struct different_line\n"                    /* line 2.  */
    3916                 :            :        "{\n"                                        /* line 3.  */
    3917                 :            :        "  double x;\n"                              /* line 4.  */
    3918                 :            :        "  double y;\n"                              /* line 5.  */
    3919                 :            :        ";\n");                                      /* line 6.  */
    3920                 :         76 :   temp_source_file tmp (SELFTEST_LOCATION, ".c", content);
    3921                 :         76 :   line_table_test ltt (case_);
    3922                 :            : 
    3923                 :         48 :   const line_map_ordinary *ord_map
    3924                 :         48 :     = linemap_check_ordinary (linemap_add (line_table, LC_ENTER, false,
    3925                 :            :                                            tmp.get_filename (), 0));
    3926                 :            : 
    3927                 :         48 :   linemap_line_start (line_table, 1, 100);
    3928                 :            : 
    3929                 :         48 :   const location_t final_line_end
    3930                 :         48 :     = linemap_position_for_line_and_column (line_table, ord_map, 6, 7);
    3931                 :            : 
    3932                 :            :   /* Don't attempt to run the tests if column data might be unavailable.  */
    3933                 :         48 :   if (final_line_end > LINE_MAP_MAX_LOCATION_WITH_COLS)
    3934                 :         20 :     return;
    3935                 :            : 
    3936                 :            :   /* Test of add_location_if_nearby on the same line as the
    3937                 :            :      primary location.  */
    3938                 :         28 :   {
    3939                 :         28 :     const location_t missing_close_brace_1_39
    3940                 :         28 :       = linemap_position_for_line_and_column (line_table, ord_map, 1, 39);
    3941                 :         28 :     const location_t matching_open_brace_1_18
    3942                 :         28 :       = linemap_position_for_line_and_column (line_table, ord_map, 1, 18);
    3943                 :         28 :     gcc_rich_location richloc (missing_close_brace_1_39);
    3944                 :         28 :     bool added = richloc.add_location_if_nearby (matching_open_brace_1_18);
    3945                 :         28 :     ASSERT_TRUE (added);
    3946                 :         28 :     ASSERT_EQ (2, richloc.get_num_locations ());
    3947                 :         56 :     test_diagnostic_context dc;
    3948                 :         28 :     diagnostic_show_locus (&dc, &richloc, DK_ERROR);
    3949                 :         28 :     ASSERT_STREQ (" struct same_line { double x; double y; ;\n"
    3950                 :            :                   "                  ~                    ^\n",
    3951                 :            :                   pp_formatted_text (dc.printer));
    3952                 :            :   }
    3953                 :            : 
    3954                 :            :   /* Test of add_location_if_nearby on a different line to the
    3955                 :            :      primary location.  */
    3956                 :         28 :   {
    3957                 :         28 :     const location_t missing_close_brace_6_1
    3958                 :         28 :       = linemap_position_for_line_and_column (line_table, ord_map, 6, 1);
    3959                 :         28 :     const location_t matching_open_brace_3_1
    3960                 :         28 :       = linemap_position_for_line_and_column (line_table, ord_map, 3, 1);
    3961                 :         56 :     gcc_rich_location richloc (missing_close_brace_6_1);
    3962                 :         28 :     bool added = richloc.add_location_if_nearby (matching_open_brace_3_1);
    3963                 :         28 :     ASSERT_FALSE (added);
    3964                 :         28 :     ASSERT_EQ (1, richloc.get_num_locations ());
    3965                 :            :   }
    3966                 :            : }
    3967                 :            : 
    3968                 :            : /* Verify that we print fixits even if they only affect lines
    3969                 :            :    outside those covered by the ranges in the rich_location.  */
    3970                 :            : 
    3971                 :            : static void
    3972                 :         48 : test_diagnostic_show_locus_fixit_lines (const line_table_case &case_)
    3973                 :            : {
    3974                 :            :   /* Create a tempfile and write some text to it.
    3975                 :            :      ...000000000111111111122222222223333333333.
    3976                 :            :      ...123456789012345678901234567890123456789.  */
    3977                 :         48 :   const char *content
    3978                 :            :     = ("struct point { double x; double y; };\n" /* line 1.  */
    3979                 :            :        "struct point origin = {x: 0.0,\n"        /* line 2.  */
    3980                 :            :        "                       y\n"              /* line 3.  */
    3981                 :            :        "\n"                                      /* line 4.  */
    3982                 :            :        "\n"                                      /* line 5.  */
    3983                 :            :        "                        : 0.0};\n");     /* line 6.  */
    3984                 :         76 :   temp_source_file tmp (SELFTEST_LOCATION, ".c", content);
    3985                 :         76 :   line_table_test ltt (case_);
    3986                 :            : 
    3987                 :         48 :   const line_map_ordinary *ord_map
    3988                 :         48 :     = linemap_check_ordinary (linemap_add (line_table, LC_ENTER, false,
    3989                 :            :                                            tmp.get_filename (), 0));
    3990                 :            : 
    3991                 :         48 :   linemap_line_start (line_table, 1, 100);
    3992                 :            : 
    3993                 :         48 :   const location_t final_line_end
    3994                 :         48 :     = linemap_position_for_line_and_column (line_table, ord_map, 6, 36);
    3995                 :            : 
    3996                 :            :   /* Don't attempt to run the tests if column data might be unavailable.  */
    3997                 :         48 :   if (final_line_end > LINE_MAP_MAX_LOCATION_WITH_COLS)
    3998                 :         20 :     return;
    3999                 :            : 
    4000                 :            :   /* A pair of tests for modernizing the initializers to C99-style.  */
    4001                 :            : 
    4002                 :            :   /* The one-liner case (line 2).  */
    4003                 :         28 :   {
    4004                 :         56 :     test_diagnostic_context dc;
    4005                 :         28 :     const location_t x
    4006                 :         28 :       = linemap_position_for_line_and_column (line_table, ord_map, 2, 24);
    4007                 :         28 :     const location_t colon
    4008                 :         28 :       = linemap_position_for_line_and_column (line_table, ord_map, 2, 25);
    4009                 :         28 :     rich_location richloc (line_table, colon);
    4010                 :         28 :     richloc.add_fixit_insert_before (x, ".");
    4011                 :         28 :     richloc.add_fixit_replace (colon, "=");
    4012                 :         28 :     diagnostic_show_locus (&dc, &richloc, DK_ERROR);
    4013                 :         28 :     ASSERT_STREQ (" struct point origin = {x: 0.0,\n"
    4014                 :            :                   "                         ^\n"
    4015                 :            :                   "                        .=\n",
    4016                 :            :                   pp_formatted_text (dc.printer));
    4017                 :            :   }
    4018                 :            : 
    4019                 :            :   /* The multiline case.  The caret for the rich_location is on line 6;
    4020                 :            :      verify that insertion fixit on line 3 is still printed (and that
    4021                 :            :      span starts are printed due to the gap between the span at line 3
    4022                 :            :      and that at line 6).  */
    4023                 :         28 :   {
    4024                 :         56 :     test_diagnostic_context dc;
    4025                 :         28 :     const location_t y
    4026                 :         28 :       = linemap_position_for_line_and_column (line_table, ord_map, 3, 24);
    4027                 :         28 :     const location_t colon
    4028                 :         28 :       = linemap_position_for_line_and_column (line_table, ord_map, 6, 25);
    4029                 :         28 :     rich_location richloc (line_table, colon);
    4030                 :         28 :     richloc.add_fixit_insert_before (y, ".");
    4031                 :         28 :     richloc.add_fixit_replace (colon, "=");
    4032                 :         28 :     diagnostic_show_locus (&dc, &richloc, DK_ERROR);
    4033                 :         28 :     ASSERT_STREQ ("FILENAME:3:24:\n"
    4034                 :            :                   "                        y\n"
    4035                 :            :                   "                        .\n"
    4036                 :            :                   "FILENAME:6:25:\n"
    4037                 :            :                   "                         : 0.0};\n"
    4038                 :            :                   "                         ^\n"
    4039                 :            :                   "                         =\n",
    4040                 :            :                   pp_formatted_text (dc.printer));
    4041                 :            :   }
    4042                 :            : 
    4043                 :            :   /* As above, but verify the behavior of multiple line spans
    4044                 :            :      with line-numbering enabled.  */
    4045                 :         28 :   {
    4046                 :         28 :     const location_t y
    4047                 :         28 :       = linemap_position_for_line_and_column (line_table, ord_map, 3, 24);
    4048                 :         28 :     const location_t colon
    4049                 :         28 :       = linemap_position_for_line_and_column (line_table, ord_map, 6, 25);
    4050                 :         56 :     rich_location richloc (line_table, colon);
    4051                 :         28 :     richloc.add_fixit_insert_before (y, ".");
    4052                 :         28 :     richloc.add_fixit_replace (colon, "=");
    4053                 :         28 :     test_diagnostic_context dc;
    4054                 :         28 :     dc.show_line_numbers_p = true;
    4055                 :         28 :     diagnostic_show_locus (&dc, &richloc, DK_ERROR);
    4056                 :         28 :     ASSERT_STREQ ("    3 |                        y\n"
    4057                 :            :                   "      |                        .\n"
    4058                 :            :                   "......\n"
    4059                 :            :                   "    6 |                         : 0.0};\n"
    4060                 :            :                   "      |                         ^\n"
    4061                 :            :                   "      |                         =\n",
    4062                 :            :                   pp_formatted_text (dc.printer));
    4063                 :            :   }
    4064                 :            : }
    4065                 :            : 
    4066                 :            : 
    4067                 :            : /* Verify that fix-it hints are appropriately consolidated.
    4068                 :            : 
    4069                 :            :    If any fix-it hints in a rich_location involve locations beyond
    4070                 :            :    LINE_MAP_MAX_LOCATION_WITH_COLS, then we can't reliably apply
    4071                 :            :    the fix-it as a whole, so there should be none.
    4072                 :            : 
    4073                 :            :    Otherwise, verify that consecutive "replace" and "remove" fix-its
    4074                 :            :    are merged, and that other fix-its remain separate.   */
    4075                 :            : 
    4076                 :            : static void
    4077                 :         48 : test_fixit_consolidation (const line_table_case &case_)
    4078                 :            : {
    4079                 :         96 :   line_table_test ltt (case_);
    4080                 :            : 
    4081                 :         48 :   linemap_add (line_table, LC_ENTER, false, "test.c", 1);
    4082                 :            : 
    4083                 :         48 :   const location_t c10 = linemap_position_for_column (line_table, 10);
    4084                 :         48 :   const location_t c15 = linemap_position_for_column (line_table, 15);
    4085                 :         48 :   const location_t c16 = linemap_position_for_column (line_table, 16);
    4086                 :         48 :   const location_t c17 = linemap_position_for_column (line_table, 17);
    4087                 :         48 :   const location_t c20 = linemap_position_for_column (line_table, 20);
    4088                 :         48 :   const location_t c21 = linemap_position_for_column (line_table, 21);
    4089                 :         48 :   const location_t caret = c10;
    4090                 :            : 
    4091                 :            :   /* Insert + insert. */
    4092                 :         48 :   {
    4093                 :         96 :     rich_location richloc (line_table, caret);
    4094                 :         48 :     richloc.add_fixit_insert_before (c10, "foo");
    4095                 :         48 :     richloc.add_fixit_insert_before (c15, "bar");
    4096                 :            : 
    4097                 :         48 :     if (c15 > LINE_MAP_MAX_LOCATION_WITH_COLS)
    4098                 :            :       /* Bogus column info for 2nd fixit, so no fixits.  */
    4099                 :         16 :       ASSERT_EQ (0, richloc.get_num_fixit_hints ());
    4100                 :            :     else
    4101                 :            :       /* They should not have been merged.  */
    4102                 :         32 :       ASSERT_EQ (2, richloc.get_num_fixit_hints ());
    4103                 :            :   }
    4104                 :            : 
    4105                 :            :   /* Insert + replace. */
    4106                 :         48 :   {
    4107                 :         96 :     rich_location richloc (line_table, caret);
    4108                 :         48 :     richloc.add_fixit_insert_before (c10, "foo");
    4109                 :         48 :     richloc.add_fixit_replace (source_range::from_locations (c15, c17),
    4110                 :            :                                "bar");
    4111                 :            : 
    4112                 :         48 :     if (c17 > LINE_MAP_MAX_LOCATION_WITH_COLS)
    4113                 :            :       /* Bogus column info for 2nd fixit, so no fixits.  */
    4114                 :         16 :       ASSERT_EQ (0, richloc.get_num_fixit_hints ());
    4115                 :            :     else
    4116                 :            :       /* They should not have been merged.  */
    4117                 :         32 :       ASSERT_EQ (2, richloc.get_num_fixit_hints ());
    4118                 :            :   }
    4119                 :            : 
    4120                 :            :   /* Replace + non-consecutive insert. */
    4121                 :         48 :   {
    4122                 :         96 :     rich_location richloc (line_table, caret);
    4123                 :         48 :     richloc.add_fixit_replace (source_range::from_locations (c10, c15),
    4124                 :            :                                "bar");
    4125                 :         48 :     richloc.add_fixit_insert_before (c17, "foo");
    4126                 :            : 
    4127                 :         48 :     if (c17 > LINE_MAP_MAX_LOCATION_WITH_COLS)
    4128                 :            :       /* Bogus column info for 2nd fixit, so no fixits.  */
    4129                 :         16 :       ASSERT_EQ (0, richloc.get_num_fixit_hints ());
    4130                 :            :     else
    4131                 :            :       /* They should not have been merged.  */
    4132                 :         32 :       ASSERT_EQ (2, richloc.get_num_fixit_hints ());
    4133                 :            :   }
    4134                 :            : 
    4135                 :            :   /* Replace + non-consecutive replace. */
    4136                 :         48 :   {
    4137                 :         96 :     rich_location richloc (line_table, caret);
    4138                 :         48 :     richloc.add_fixit_replace (source_range::from_locations (c10, c15),
    4139                 :            :                                "foo");
    4140                 :         48 :     richloc.add_fixit_replace (source_range::from_locations (c17, c20),
    4141                 :            :                                "bar");
    4142                 :            : 
    4143                 :         48 :     if (c20 > LINE_MAP_MAX_LOCATION_WITH_COLS)
    4144                 :            :       /* Bogus column info for 2nd fixit, so no fixits.  */
    4145                 :         16 :       ASSERT_EQ (0, richloc.get_num_fixit_hints ());
    4146                 :            :     else
    4147                 :            :       /* They should not have been merged.  */
    4148                 :         32 :       ASSERT_EQ (2, richloc.get_num_fixit_hints ());
    4149                 :            :   }
    4150                 :            : 
    4151                 :            :   /* Replace + consecutive replace. */
    4152                 :         48 :   {
    4153                 :         96 :     rich_location richloc (line_table, caret);
    4154                 :         48 :     richloc.add_fixit_replace (source_range::from_locations (c10, c15),
    4155                 :            :                                "foo");
    4156                 :         48 :     richloc.add_fixit_replace (source_range::from_locations (c16, c20),
    4157                 :            :                                "bar");
    4158                 :            : 
    4159                 :         48 :     if (c20 > LINE_MAP_MAX_LOCATION_WITH_COLS)
    4160                 :            :       /* Bogus column info for 2nd fixit, so no fixits.  */
    4161                 :         16 :       ASSERT_EQ (0, richloc.get_num_fixit_hints ());
    4162                 :            :     else
    4163                 :            :       {
    4164                 :            :         /* They should have been merged into a single "replace".  */
    4165                 :         32 :         ASSERT_EQ (1, richloc.get_num_fixit_hints ());
    4166                 :         32 :         const fixit_hint *hint = richloc.get_fixit_hint (0);
    4167                 :         32 :         ASSERT_STREQ ("foobar", hint->get_string ());
    4168                 :         32 :         ASSERT_EQ (c10, hint->get_start_loc ());
    4169                 :         32 :         ASSERT_EQ (c21, hint->get_next_loc ());
    4170                 :            :       }
    4171                 :            :   }
    4172                 :            : 
    4173                 :            :   /* Replace + consecutive removal. */
    4174                 :         48 :   {
    4175                 :         96 :     rich_location richloc (line_table, caret);
    4176                 :         48 :     richloc.add_fixit_replace (source_range::from_locations (c10, c15),
    4177                 :            :                                "foo");
    4178                 :         48 :     richloc.add_fixit_remove (source_range::from_locations (c16, c20));
    4179                 :            : 
    4180                 :         48 :     if (c20 > LINE_MAP_MAX_LOCATION_WITH_COLS)
    4181                 :            :       /* Bogus column info for 2nd fixit, so no fixits.  */
    4182                 :         16 :       ASSERT_EQ (0, richloc.get_num_fixit_hints ());
    4183                 :            :     else
    4184                 :            :       {
    4185                 :            :         /* They should have been merged into a single replace, with the
    4186                 :            :            range extended to cover that of the removal.  */
    4187                 :         32 :         ASSERT_EQ (1, richloc.get_num_fixit_hints ());
    4188                 :         32 :         const fixit_hint *hint = richloc.get_fixit_hint (0);
    4189                 :         32 :         ASSERT_STREQ ("foo", hint->get_string ());
    4190                 :         32 :         ASSERT_EQ (c10, hint->get_start_loc ());
    4191                 :         32 :         ASSERT_EQ (c21, hint->get_next_loc ());
    4192                 :            :       }
    4193                 :            :   }
    4194                 :            : 
    4195                 :            :   /* Consecutive removals. */
    4196                 :         48 :   {
    4197                 :         96 :     rich_location richloc (line_table, caret);
    4198                 :         48 :     richloc.add_fixit_remove (source_range::from_locations (c10, c15));
    4199                 :         48 :     richloc.add_fixit_remove (source_range::from_locations (c16, c20));
    4200                 :            : 
    4201                 :         48 :     if (c20 > LINE_MAP_MAX_LOCATION_WITH_COLS)
    4202                 :            :       /* Bogus column info for 2nd fixit, so no fixits.  */
    4203                 :         16 :       ASSERT_EQ (0, richloc.get_num_fixit_hints ());
    4204                 :            :     else
    4205                 :            :       {
    4206                 :            :         /* They should have been merged into a single "replace-with-empty".  */
    4207                 :         32 :         ASSERT_EQ (1, richloc.get_num_fixit_hints ());
    4208                 :         32 :         const fixit_hint *hint = richloc.get_fixit_hint (0);
    4209                 :         32 :         ASSERT_STREQ ("", hint->get_string ());
    4210                 :         32 :         ASSERT_EQ (c10, hint->get_start_loc ());
    4211                 :         32 :         ASSERT_EQ (c21, hint->get_next_loc ());
    4212                 :            :       }
    4213                 :            :   }
    4214                 :         48 : }
    4215                 :            : 
    4216                 :            : /* Verify that the line_corrections machinery correctly prints
    4217                 :            :    overlapping fixit-hints.  */
    4218                 :            : 
    4219                 :            : static void
    4220                 :         48 : test_overlapped_fixit_printing (const line_table_case &case_)
    4221                 :            : {
    4222                 :            :   /* Create a tempfile and write some text to it.
    4223                 :            :      ...000000000111111111122222222223333333333.
    4224                 :            :      ...123456789012345678901234567890123456789.  */
    4225                 :         48 :   const char *content
    4226                 :            :     = ("  foo *f = (foo *)ptr->field;\n");
    4227                 :         76 :   temp_source_file tmp (SELFTEST_LOCATION, ".C", content);
    4228                 :         76 :   line_table_test ltt (case_);
    4229                 :            : 
    4230                 :         48 :   const line_map_ordinary *ord_map
    4231                 :         48 :     = linemap_check_ordinary (linemap_add (line_table, LC_ENTER, false,
    4232                 :            :                                            tmp.get_filename (), 0));
    4233                 :            : 
    4234                 :         48 :   linemap_line_start (line_table, 1, 100);
    4235                 :            : 
    4236                 :         48 :   const location_t final_line_end
    4237                 :         48 :     = linemap_position_for_line_and_column (line_table, ord_map, 6, 36);
    4238                 :            : 
    4239                 :            :   /* Don't attempt to run the tests if column data might be unavailable.  */
    4240                 :         48 :   if (final_line_end > LINE_MAP_MAX_LOCATION_WITH_COLS)
    4241                 :         20 :     return;
    4242                 :            : 
    4243                 :            :   /* A test for converting a C-style cast to a C++-style cast.  */
    4244                 :         28 :   const location_t open_paren
    4245                 :         28 :     = linemap_position_for_line_and_column (line_table, ord_map, 1, 12);
    4246                 :         28 :   const location_t close_paren
    4247                 :         28 :     = linemap_position_for_line_and_column (line_table, ord_map, 1, 18);
    4248                 :         28 :   const location_t expr_start
    4249                 :         28 :     = linemap_position_for_line_and_column (line_table, ord_map, 1, 19);
    4250                 :         28 :   const location_t expr_finish
    4251                 :         28 :     = linemap_position_for_line_and_column (line_table, ord_map, 1, 28);
    4252                 :         28 :   const location_t expr = make_location (expr_start, expr_start, expr_finish);
    4253                 :            : 
    4254                 :            :   /* Various examples of fix-it hints that aren't themselves consolidated,
    4255                 :            :      but for which the *printing* may need consolidation.  */
    4256                 :            : 
    4257                 :            :   /* Example where 3 fix-it hints are printed as one.  */
    4258                 :         28 :   {
    4259                 :         56 :     test_diagnostic_context dc;
    4260                 :         56 :     rich_location richloc (line_table, expr);
    4261                 :         28 :     richloc.add_fixit_replace (open_paren, "const_cast<");
    4262                 :         28 :     richloc.add_fixit_replace (close_paren, "> (");
    4263                 :         28 :     richloc.add_fixit_insert_after (")");
    4264                 :            : 
    4265                 :         28 :     diagnostic_show_locus (&dc, &richloc, DK_ERROR);
    4266                 :         28 :     ASSERT_STREQ ("   foo *f = (foo *)ptr->field;\n"
    4267                 :            :                   "                   ^~~~~~~~~~\n"
    4268                 :            :                   "            -----------------\n"
    4269                 :            :                   "            const_cast<foo *> (ptr->field)\n",
    4270                 :            :                   pp_formatted_text (dc.printer));
    4271                 :            : 
    4272                 :            :     /* Unit-test the line_corrections machinery.  */
    4273                 :         28 :     ASSERT_EQ (3, richloc.get_num_fixit_hints ());
    4274                 :         28 :     const fixit_hint *hint_0 = richloc.get_fixit_hint (0);
    4275                 :         56 :     ASSERT_EQ (column_range (12, 12), get_affected_range (hint_0, CU_BYTES));
    4276                 :         56 :     ASSERT_EQ (column_range (12, 12),
    4277                 :            :                            get_affected_range (hint_0, CU_DISPLAY_COLS));
    4278                 :         56 :     ASSERT_EQ (column_range (12, 22), get_printed_columns (hint_0));
    4279                 :         28 :     const fixit_hint *hint_1 = richloc.get_fixit_hint (1);
    4280                 :         56 :     ASSERT_EQ (column_range (18, 18), get_affected_range (hint_1, CU_BYTES));
    4281                 :         56 :     ASSERT_EQ (column_range (18, 18),
    4282                 :            :                            get_affected_range (hint_1, CU_DISPLAY_COLS));
    4283                 :         56 :     ASSERT_EQ (column_range (18, 20), get_printed_columns (hint_1));
    4284                 :         28 :     const fixit_hint *hint_2 = richloc.get_fixit_hint (2);
    4285                 :         56 :     ASSERT_EQ (column_range (29, 28), get_affected_range (hint_2, CU_BYTES));
    4286                 :         56 :     ASSERT_EQ (column_range (29, 28),
    4287                 :            :                            get_affected_range (hint_2, CU_DISPLAY_COLS));
    4288                 :         56 :     ASSERT_EQ (column_range (29, 29), get_printed_columns (hint_2));
    4289                 :            : 
    4290                 :            :     /* Add each hint in turn to a line_corrections instance,
    4291                 :            :        and verify that they are consolidated into one correction instance
    4292                 :            :        as expected.  */
    4293                 :         28 :     line_corrections lc (tmp.get_filename (), 1);
    4294                 :            : 
    4295                 :            :     /* The first replace hint by itself.  */
    4296                 :         28 :     lc.add_hint (hint_0);
    4297                 :         28 :     ASSERT_EQ (1, lc.m_corrections.length ());
    4298                 :         56 :     ASSERT_EQ (column_range (12, 12), lc.m_corrections[0]->m_affected_bytes);
    4299                 :         56 :     ASSERT_EQ (column_range (12, 12), lc.m_corrections[0]->m_affected_columns);
    4300                 :         56 :     ASSERT_EQ (column_range (12, 22), lc.m_corrections[0]->m_printed_columns);
    4301                 :         28 :     ASSERT_STREQ ("const_cast<", lc.m_corrections[0]->m_text);
    4302                 :            : 
    4303                 :            :     /* After the second replacement hint, they are printed together
    4304                 :            :        as a replacement (along with the text between them).  */
    4305                 :         28 :     lc.add_hint (hint_1);
    4306                 :         28 :     ASSERT_EQ (1, lc.m_corrections.length ());
    4307                 :         28 :     ASSERT_STREQ ("const_cast<foo *> (", lc.m_corrections[0]->m_text);
    4308                 :         56 :     ASSERT_EQ (column_range (12, 18), lc.m_corrections[0]->m_affected_bytes);
    4309                 :         56 :     ASSERT_EQ (column_range (12, 18), lc.m_corrections[0]->m_affected_columns);
    4310                 :         56 :     ASSERT_EQ (column_range (12, 30), lc.m_corrections[0]->m_printed_columns);
    4311                 :            : 
    4312                 :            :     /* After the final insertion hint, they are all printed together
    4313                 :            :        as a replacement (along with the text between them).  */
    4314                 :         28 :     lc.add_hint (hint_2);
    4315                 :         28 :     ASSERT_STREQ ("const_cast<foo *> (ptr->field)",
    4316                 :            :                   lc.m_corrections[0]->m_text);
    4317                 :         28 :     ASSERT_EQ (1, lc.m_corrections.length ());
    4318                 :         56 :     ASSERT_EQ (column_range (12, 28), lc.m_corrections[0]->m_affected_bytes);
    4319                 :         56 :     ASSERT_EQ (column_range (12, 28), lc.m_corrections[0]->m_affected_columns);
    4320                 :         56 :     ASSERT_EQ (column_range (12, 41), lc.m_corrections[0]->m_printed_columns);
    4321                 :            :   }
    4322                 :            : 
    4323                 :            :   /* Example where two are consolidated during printing.  */
    4324                 :         28 :   {
    4325                 :         56 :     test_diagnostic_context dc;
    4326                 :         28 :     rich_location richloc (line_table, expr);
    4327                 :         28 :     richloc.add_fixit_replace (open_paren, "CAST (");
    4328                 :         28 :     richloc.add_fixit_replace (close_paren, ") (");
    4329                 :         28 :     richloc.add_fixit_insert_after (")");
    4330                 :            : 
    4331                 :         28 :     diagnostic_show_locus (&dc, &richloc, DK_ERROR);
    4332                 :         28 :     ASSERT_STREQ ("   foo *f = (foo *)ptr->field;\n"
    4333                 :            :                   "                   ^~~~~~~~~~\n"
    4334                 :            :                   "            -\n"
    4335                 :            :                   "            CAST (-\n"
    4336                 :            :                   "                  ) (        )\n",
    4337                 :            :                   pp_formatted_text (dc.printer));
    4338                 :            :   }
    4339                 :            : 
    4340                 :            :   /* Example where none are consolidated during printing.  */
    4341                 :         28 :   {
    4342                 :         56 :     test_diagnostic_context dc;
    4343                 :         28 :     rich_location richloc (line_table, expr);
    4344                 :         28 :     richloc.add_fixit_replace (open_paren, "CST (");
    4345                 :         28 :     richloc.add_fixit_replace (close_paren, ") (");
    4346                 :         28 :     richloc.add_fixit_insert_after (")");
    4347                 :            : 
    4348                 :         28 :     diagnostic_show_locus (&dc, &richloc, DK_ERROR);
    4349                 :         28 :     ASSERT_STREQ ("   foo *f = (foo *)ptr->field;\n"
    4350                 :            :                   "                   ^~~~~~~~~~\n"
    4351                 :            :                   "            -\n"
    4352                 :            :                   "            CST ( -\n"
    4353                 :            :                   "                  ) (        )\n",
    4354                 :            :                   pp_formatted_text (dc.printer));
    4355                 :            :   }
    4356                 :            : 
    4357                 :            :   /* Example of deletion fix-it hints.  */
    4358                 :         28 :   {
    4359                 :         56 :     test_diagnostic_context dc;
    4360                 :         28 :     rich_location richloc (line_table, expr);
    4361                 :         28 :     richloc.add_fixit_insert_before (open_paren, "(bar *)");
    4362                 :         28 :     source_range victim = {open_paren, close_paren};
    4363                 :         28 :     richloc.add_fixit_remove (victim);
    4364                 :            : 
    4365                 :            :     /* This case is actually handled by fixit-consolidation,
    4366                 :            :        rather than by line_corrections.  */
    4367                 :         28 :     ASSERT_EQ (1, richloc.get_num_fixit_hints ());
    4368                 :            : 
    4369                 :         28 :     diagnostic_show_locus (&dc, &richloc, DK_ERROR);
    4370                 :         28 :     ASSERT_STREQ ("   foo *f = (foo *)ptr->field;\n"
    4371                 :            :                   "                   ^~~~~~~~~~\n"
    4372                 :            :                   "            -------\n"
    4373                 :            :                   "            (bar *)\n",
    4374                 :            :                   pp_formatted_text (dc.printer));
    4375                 :            :   }
    4376                 :            : 
    4377                 :            :   /* Example of deletion fix-it hints that would overlap.  */
    4378                 :         28 :   {
    4379                 :         56 :     test_diagnostic_context dc;
    4380                 :         28 :     rich_location richloc (line_table, expr);
    4381                 :         28 :     richloc.add_fixit_insert_before (open_paren, "(longer *)");
    4382                 :         28 :     source_range victim = {expr_start, expr_finish};
    4383                 :         28 :     richloc.add_fixit_remove (victim);
    4384                 :            : 
    4385                 :            :     /* These fixits are not consolidated.  */
    4386                 :         28 :     ASSERT_EQ (2, richloc.get_num_fixit_hints ());
    4387                 :            : 
    4388                 :            :     /* But the corrections are.  */
    4389                 :         28 :     diagnostic_show_locus (&dc, &richloc, DK_ERROR);
    4390                 :         28 :     ASSERT_STREQ ("   foo *f = (foo *)ptr->field;\n"
    4391                 :            :                   "                   ^~~~~~~~~~\n"
    4392                 :            :                   "            -----------------\n"
    4393                 :            :                   "            (longer *)(foo *)\n",
    4394                 :            :                   pp_formatted_text (dc.printer));
    4395                 :            :   }
    4396                 :            : 
    4397                 :            :   /* Example of insertion fix-it hints that would overlap.  */
    4398                 :         28 :   {
    4399                 :         56 :     test_diagnostic_context dc;
    4400                 :         28 :     rich_location richloc (line_table, expr);
    4401                 :         28 :     richloc.add_fixit_insert_before (open_paren, "LONGER THAN THE CAST");
    4402                 :         28 :     richloc.add_fixit_insert_after (close_paren, "TEST");
    4403                 :            : 
    4404                 :            :     /* The first insertion is long enough that if printed naively,
    4405                 :            :        it would overlap with the second.
    4406                 :            :        Verify that they are printed as a single replacement.  */
    4407                 :         28 :     diagnostic_show_locus (&dc, &richloc, DK_ERROR);
    4408                 :         28 :     ASSERT_STREQ ("   foo *f = (foo *)ptr->field;\n"
    4409                 :            :                   "                   ^~~~~~~~~~\n"
    4410                 :            :                   "            -------\n"
    4411                 :            :                   "            LONGER THAN THE CAST(foo *)TEST\n",
    4412                 :            :                   pp_formatted_text (dc.printer));
    4413                 :            :   }
    4414                 :            : }
    4415                 :            : 
    4416                 :            : /* Multibyte-aware version of preceding tests.  See comments above
    4417                 :            :    test_one_liner_simple_caret_utf8() too, we use the same two multibyte
    4418                 :            :    characters here.  */
    4419                 :            : 
    4420                 :            : static void
    4421                 :         48 : test_overlapped_fixit_printing_utf8 (const line_table_case &case_)
    4422                 :            : {
    4423                 :            :   /* Create a tempfile and write some text to it.  */
    4424                 :            : 
    4425                 :         48 :   const char *content
    4426                 :            :     /* Display columns.
    4427                 :            :        00000000000000000000000111111111111111111111111222222222222222223
    4428                 :            :        12344444444555555556789012344444444555555556789012345678999999990  */
    4429                 :            :     = "  f\xf0\x9f\x98\x82 *f = (f\xf0\x9f\x98\x82 *)ptr->field\xcf\x80;\n";
    4430                 :            :     /* 00000000000000000000011111111111111111111112222222222333333333333
    4431                 :            :        12344445555666677778901234566667777888899990123456789012333344445
    4432                 :            :        Byte columns.  */
    4433                 :            : 
    4434                 :         76 :   temp_source_file tmp (SELFTEST_LOCATION, ".C", content);
    4435                 :         76 :   line_table_test ltt (case_);
    4436                 :            : 
    4437                 :         48 :   const line_map_ordinary *ord_map
    4438                 :         48 :     = linemap_check_ordinary (linemap_add (line_table, LC_ENTER, false,
    4439                 :            :                                            tmp.get_filename (), 0));
    4440                 :            : 
    4441                 :         48 :   linemap_line_start (line_table, 1, 100);
    4442                 :            : 
    4443                 :         48 :   const location_t final_line_end
    4444                 :         48 :     = linemap_position_for_line_and_column (line_table, ord_map, 6, 50);
    4445                 :            : 
    4446                 :            :   /* Don't attempt to run the tests if column data might be unavailable.  */
    4447                 :         48 :   if (final_line_end > LINE_MAP_MAX_LOCATION_WITH_COLS)
    4448                 :         20 :     return;
    4449                 :            : 
    4450                 :            :   /* A test for converting a C-style cast to a C++-style cast.  */
    4451                 :         28 :   const location_t open_paren
    4452                 :         28 :     = linemap_position_for_line_and_column (line_table, ord_map, 1, 14);
    4453                 :         28 :   const location_t close_paren
    4454                 :         28 :     = linemap_position_for_line_and_column (line_table, ord_map, 1, 22);
    4455                 :         28 :   const location_t expr_start
    4456                 :         28 :     = linemap_position_for_line_and_column (line_table, ord_map, 1, 23);
    4457                 :         28 :   const location_t expr_finish
    4458                 :         28 :     = linemap_position_for_line_and_column (line_table, ord_map, 1, 34);
    4459                 :         28 :   const location_t expr = make_location (expr_start, expr_start, expr_finish);
    4460                 :            : 
    4461                 :            :   /* Various examples of fix-it hints that aren't themselves consolidated,
    4462                 :            :      but for which the *printing* may need consolidation.  */
    4463                 :            : 
    4464                 :            :   /* Example where 3 fix-it hints are printed as one.  */
    4465                 :         28 :   {
    4466                 :         56 :     test_diagnostic_context dc;
    4467                 :         56 :     rich_location richloc (line_table, expr);
    4468                 :         28 :     richloc.add_fixit_replace (open_paren, "const_cast<");
    4469                 :         28 :     richloc.add_fixit_replace (close_paren, "> (");
    4470                 :         28 :     richloc.add_fixit_insert_after (")");
    4471                 :            : 
    4472                 :         28 :     diagnostic_show_locus (&dc, &richloc, DK_ERROR);
    4473                 :         28 :     ASSERT_STREQ ("   f\xf0\x9f\x98\x82"
    4474                 :            :                         " *f = (f\xf0\x9f\x98\x82"
    4475                 :            :                                   " *)ptr->field\xcf\x80"
    4476                 :            :                                                 ";\n"
    4477                 :            :                   "                   ^~~~~~~~~~~\n"
    4478                 :            :                   "            ------------------\n"
    4479                 :            :                   "            const_cast<f\xf0\x9f\x98\x82"
    4480                 :            :                                             " *> (ptr->field\xcf\x80"
    4481                 :            :                                                             ")\n",
    4482                 :            :                   pp_formatted_text (dc.printer));
    4483                 :            : 
    4484                 :            :     /* Unit-test the line_corrections machinery.  */
    4485                 :         28 :     ASSERT_EQ (3, richloc.get_num_fixit_hints ());
    4486                 :         28 :     const fixit_hint *hint_0 = richloc.get_fixit_hint (0);
    4487                 :         56 :     ASSERT_EQ (column_range (14, 14), get_affected_range (hint_0, CU_BYTES));
    4488                 :         56 :     ASSERT_EQ (column_range (12, 12),
    4489                 :            :                            get_affected_range (hint_0, CU_DISPLAY_COLS));
    4490                 :         56 :     ASSERT_EQ (column_range (12, 22), get_printed_columns (hint_0));
    4491                 :         28 :     const fixit_hint *hint_1 = richloc.get_fixit_hint (1);
    4492                 :         56 :     ASSERT_EQ (column_range (22, 22), get_affected_range (hint_1, CU_BYTES));
    4493                 :         56 :     ASSERT_EQ (column_range (18, 18),
    4494                 :            :                            get_affected_range (hint_1, CU_DISPLAY_COLS));
    4495                 :         56 :     ASSERT_EQ (column_range (18, 20), get_printed_columns (hint_1));
    4496                 :         28 :     const fixit_hint *hint_2 = richloc.get_fixit_hint (2);
    4497                 :         56 :     ASSERT_EQ (column_range (35, 34), get_affected_range (hint_2, CU_BYTES));
    4498                 :         56 :     ASSERT_EQ (column_range (30, 29),
    4499                 :            :                            get_affected_range (hint_2, CU_DISPLAY_COLS));
    4500                 :         56 :     ASSERT_EQ (column_range (30, 30), get_printed_columns (hint_2));
    4501                 :            : 
    4502                 :            :     /* Add each hint in turn to a line_corrections instance,
    4503                 :            :        and verify that they are consolidated into one correction instance
    4504                 :            :        as expected.  */
    4505                 :         28 :     line_corrections lc (tmp.get_filename (), 1);
    4506                 :            : 
    4507                 :            :     /* The first replace hint by itself.  */
    4508                 :         28 :     lc.add_hint (hint_0);
    4509                 :         28 :     ASSERT_EQ (1, lc.m_corrections.length ());
    4510                 :         56 :     ASSERT_EQ (column_range (14, 14), lc.m_corrections[0]->m_affected_bytes);
    4511                 :         56 :     ASSERT_EQ (column_range (12, 12), lc.m_corrections[0]->m_affected_columns);
    4512                 :         56 :     ASSERT_EQ (column_range (12, 22), lc.m_corrections[0]->m_printed_columns);
    4513                 :         28 :     ASSERT_STREQ ("const_cast<", lc.m_corrections[0]->m_text);
    4514                 :            : 
    4515                 :            :     /* After the second replacement hint, they are printed together
    4516                 :            :        as a replacement (along with the text between them).  */
    4517                 :         28 :     lc.add_hint (hint_1);
    4518                 :         28 :     ASSERT_EQ (1, lc.m_corrections.length ());
    4519                 :         28 :     ASSERT_STREQ ("const_cast<f\xf0\x9f\x98\x82 *> (",
    4520                 :            :                   lc.m_corrections[0]->m_text);
    4521                 :         56 :     ASSERT_EQ (column_range (14, 22), lc.m_corrections[0]->m_affected_bytes);
    4522                 :         56 :     ASSERT_EQ (column_range (12, 18), lc.m_corrections[0]->m_affected_columns);
    4523                 :         56 :     ASSERT_EQ (column_range (12, 30), lc.m_corrections[0]->m_printed_columns);
    4524                 :            : 
    4525                 :            :     /* After the final insertion hint, they are all printed together
    4526                 :            :        as a replacement (along with the text between them).  */
    4527                 :         28 :     lc.add_hint (hint_2);
    4528                 :         28 :     ASSERT_STREQ ("const_cast<f\xf0\x9f\x98\x82 *> (ptr->field\xcf\x80)",
    4529                 :            :                   lc.m_corrections[0]->m_text);
    4530                 :         28 :     ASSERT_EQ (1, lc.m_corrections.length ());
    4531                 :         56 :     ASSERT_EQ (column_range (14, 34), lc.m_corrections[0]->m_affected_bytes);
    4532                 :         56 :     ASSERT_EQ (column_range (12, 29), lc.m_corrections[0]->m_affected_columns);
    4533                 :         56 :     ASSERT_EQ (column_range (12, 42), lc.m_corrections[0]->m_printed_columns);
    4534                 :            :   }
    4535                 :            : 
    4536                 :            :   /* Example where two are consolidated during printing.  */
    4537                 :         28 :   {
    4538                 :         56 :     test_diagnostic_context dc;
    4539                 :         28 :     rich_location richloc (line_table, expr);
    4540                 :         28 :     richloc.add_fixit_replace (open_paren, "CAST (");
    4541                 :         28 :     richloc.add_fixit_replace (close_paren, ") (");
    4542                 :         28 :     richloc.add_fixit_insert_after (")");
    4543                 :            : 
    4544                 :         28 :     diagnostic_show_locus (&dc, &richloc, DK_ERROR);
    4545                 :         28 :     ASSERT_STREQ ("   f\xf0\x9f\x98\x82"
    4546                 :            :                         " *f = (f\xf0\x9f\x98\x82"
    4547                 :            :                                   " *)ptr->field\xcf\x80"
    4548                 :            :                                                 ";\n"
    4549                 :            :                   "                   ^~~~~~~~~~~\n"
    4550                 :            :                   "            -\n"
    4551                 :            :                   "            CAST (-\n"
    4552                 :            :                   "                  ) (         )\n",
    4553                 :            :                   pp_formatted_text (dc.printer));
    4554                 :            :   }
    4555                 :            : 
    4556                 :            :   /* Example where none are consolidated during printing.  */
    4557                 :         28 :   {
    4558                 :         56 :     test_diagnostic_context dc;
    4559                 :         28 :     rich_location richloc (line_table, expr);
    4560                 :         28 :     richloc.add_fixit_replace (open_paren, "CST (");
    4561                 :         28 :     richloc.add_fixit_replace (close_paren, ") (");
    4562                 :         28 :     richloc.add_fixit_insert_after (")");
    4563                 :            : 
    4564                 :         28 :     diagnostic_show_locus (&dc, &richloc, DK_ERROR);
    4565                 :         28 :     ASSERT_STREQ ("   f\xf0\x9f\x98\x82"
    4566                 :            :                         " *f = (f\xf0\x9f\x98\x82"
    4567                 :            :                                   " *)ptr->field\xcf\x80"
    4568                 :            :                                                 ";\n"
    4569                 :            :                   "                   ^~~~~~~~~~~\n"
    4570                 :            :                   "            -\n"
    4571                 :            :                   "            CST ( -\n"
    4572                 :            :                   "                  ) (         )\n",
    4573                 :            :                   pp_formatted_text (dc.printer));
    4574                 :            :   }
    4575                 :            : 
    4576                 :            :   /* Example of deletion fix-it hints.  */
    4577                 :         28 :   {
    4578                 :         56 :     test_diagnostic_context dc;
    4579                 :         28 :     rich_location richloc (line_table, expr);
    4580                 :         28 :     richloc.add_fixit_insert_before (open_paren, "(bar\xf0\x9f\x98\x82 *)");
    4581                 :         28 :     source_range victim = {open_paren, close_paren};
    4582                 :         28 :     richloc.add_fixit_remove (victim);
    4583                 :            : 
    4584                 :            :     /* This case is actually handled by fixit-consolidation,
    4585                 :            :        rather than by line_corrections.  */
    4586                 :         28 :     ASSERT_EQ (1, richloc.get_num_fixit_hints ());
    4587                 :            : 
    4588                 :         28 :     diagnostic_show_locus (&dc, &richloc, DK_ERROR);
    4589                 :         28 :     ASSERT_STREQ ("   f\xf0\x9f\x98\x82"
    4590                 :            :                         " *f = (f\xf0\x9f\x98\x82"
    4591                 :            :                                   " *)ptr->field\xcf\x80"
    4592                 :            :                                                 ";\n"
    4593                 :            :                   "                   ^~~~~~~~~~~\n"
    4594                 :            :                   "            -------\n"
    4595                 :            :                   "            (bar\xf0\x9f\x98\x82"
    4596                 :            :                                     " *)\n",
    4597                 :            :                   pp_formatted_text (dc.printer));
    4598                 :            :   }
    4599                 :            : 
    4600                 :            :   /* Example of deletion fix-it hints that would overlap.  */
    4601                 :         28 :   {
    4602                 :         56 :     test_diagnostic_context dc;
    4603                 :         28 :     rich_location richloc (line_table, expr);
    4604                 :         28 :     richloc.add_fixit_insert_before (open_paren, "(long\xf0\x9f\x98\x82 *)");
    4605                 :         28 :     source_range victim = {expr_start, expr_finish};
    4606                 :         28 :     richloc.add_fixit_remove (victim);
    4607                 :            : 
    4608                 :            :     /* These fixits are not consolidated.  */
    4609                 :         28 :     ASSERT_EQ (2, richloc.get_num_fixit_hints ());
    4610                 :            : 
    4611                 :            :     /* But the corrections are.  */
    4612                 :         28 :     diagnostic_show_locus (&dc, &richloc, DK_ERROR);
    4613                 :         28 :     ASSERT_STREQ ("   f\xf0\x9f\x98\x82"
    4614                 :            :                         " *f = (f\xf0\x9f\x98\x82"
    4615                 :            :                                   " *)ptr->field\xcf\x80"
    4616                 :            :                                                 ";\n"
    4617                 :            :                   "                   ^~~~~~~~~~~\n"
    4618                 :            :                   "            ------------------\n"
    4619                 :            :                   "            (long\xf0\x9f\x98\x82"
    4620                 :            :                                      " *)(f\xf0\x9f\x98\x82"
    4621                 :            :                                             " *)\n",
    4622                 :            :                   pp_formatted_text (dc.printer));
    4623                 :            :   }
    4624                 :            : 
    4625                 :            :   /* Example of insertion fix-it hints that would overlap.  */
    4626                 :         28 :   {
    4627                 :         56 :     test_diagnostic_context dc;
    4628                 :         28 :     rich_location richloc (line_table, expr);
    4629                 :         28 :     richloc.add_fixit_insert_before
    4630                 :         28 :       (open_paren, "L\xf0\x9f\x98\x82NGER THAN THE CAST");
    4631                 :         28 :     richloc.add_fixit_insert_after (close_paren, "TEST");
    4632                 :            : 
    4633                 :            :     /* The first insertion is long enough that if printed naively,
    4634                 :            :        it would overlap with the second.
    4635                 :            :        Verify that they are printed as a single replacement.  */
    4636                 :         28 :     diagnostic_show_locus (&dc, &richloc, DK_ERROR);
    4637                 :         28 :     ASSERT_STREQ ("   f\xf0\x9f\x98\x82"
    4638                 :            :                         " *f = (f\xf0\x9f\x98\x82"
    4639                 :            :                                   " *)ptr->field\xcf\x80"
    4640                 :            :                                                 ";\n"
    4641                 :            :                   "                   ^~~~~~~~~~~\n"
    4642                 :            :                   "            -------\n"
    4643                 :            :                   "            L\xf0\x9f\x98\x82"
    4644                 :            :                                  "NGER THAN THE CAST(f\xf0\x9f\x98\x82"
    4645                 :            :                                                        " *)TEST\n",
    4646                 :            :                   pp_formatted_text (dc.printer));
    4647                 :            :   }
    4648                 :            : }
    4649                 :            : 
    4650                 :            : /* Verify that the line_corrections machinery correctly prints
    4651                 :            :    overlapping fixit-hints that have been added in the wrong
    4652                 :            :    order.
    4653                 :            :    Adapted from PR c/81405 seen on gcc.dg/init-excess-1.c*/
    4654                 :            : 
    4655                 :            : static void
    4656                 :         48 : test_overlapped_fixit_printing_2 (const line_table_case &case_)
    4657                 :            : {
    4658                 :            :   /* Create a tempfile and write some text to it.
    4659                 :            :      ...000000000111111111122222222223333333333.
    4660                 :            :      ...123456789012345678901234567890123456789.  */
    4661                 :         48 :   const char *content
    4662                 :            :     = ("int a5[][0][0] = { 1, 2 };\n");
    4663                 :         78 :   temp_source_file tmp (SELFTEST_LOCATION, ".c", content);
    4664                 :         78 :   line_table_test ltt (case_);
    4665                 :            : 
    4666                 :         48 :   const line_map_ordinary *ord_map
    4667                 :         48 :     = linemap_check_ordinary (linemap_add (line_table, LC_ENTER, false,
    4668                 :            :                                            tmp.get_filename (), 0));
    4669                 :            : 
    4670                 :         48 :   linemap_line_start (line_table, 1, 100);
    4671                 :            : 
    4672                 :         48 :   const location_t final_line_end
    4673                 :         48 :     = linemap_position_for_line_and_column (line_table, ord_map, 1, 100);
    4674                 :            : 
    4675                 :            :   /* Don't attempt to run the tests if column data might be unavailable.  */
    4676                 :         48 :   if (final_line_end > LINE_MAP_MAX_LOCATION_WITH_COLS)
    4677                 :         18 :     return;
    4678                 :            : 
    4679                 :         30 :   const location_t col_1
    4680                 :         30 :     = linemap_position_for_line_and_column (line_table, ord_map, 1, 1);
    4681                 :         30 :   const location_t col_20
    4682                 :         30 :     = linemap_position_for_line_and_column (line_table, ord_map, 1, 20);
    4683                 :         30 :   const location_t col_21
    4684                 :         30 :     = linemap_position_for_line_and_column (line_table, ord_map, 1, 21);
    4685                 :         30 :   const location_t col_23
    4686                 :         30 :     = linemap_position_for_line_and_column (line_table, ord_map, 1, 23);
    4687                 :         30 :   const location_t col_25
    4688                 :         30 :     = linemap_position_for_line_and_column (line_table, ord_map, 1, 25);
    4689                 :            : 
    4690                 :            :   /* Two insertions, in the wrong order.  */
    4691                 :         30 :   {
    4692                 :         60 :     rich_location richloc (line_table, col_20);
    4693                 :         30 :     richloc.add_fixit_insert_before (col_23, "{");
    4694                 :         30 :     richloc.add_fixit_insert_before (col_21, "}");
    4695                 :            : 
    4696                 :            :     /* These fixits should be accepted; they can't be consolidated.  */
    4697                 :         30 :     ASSERT_EQ (2, richloc.get_num_fixit_hints ());
    4698                 :         30 :     const fixit_hint *hint_0 = richloc.get_fixit_hint (0);
    4699                 :         60 :     ASSERT_EQ (column_range (23, 22), get_affected_range (hint_0, CU_BYTES));
    4700                 :         60 :     ASSERT_EQ (column_range (23, 23), get_printed_columns (hint_0));
    4701                 :         30 :     const fixit_hint *hint_1 = richloc.get_fixit_hint (1);
    4702                 :         60 :     ASSERT_EQ (column_range (21, 20), get_affected_range (hint_1, CU_BYTES));
    4703                 :         60 :     ASSERT_EQ (column_range (21, 21), get_printed_columns (hint_1));
    4704                 :            : 
    4705                 :            :     /* Verify that they're printed correctly.  */
    4706                 :         30 :     test_diagnostic_context dc;
    4707                 :         30 :     diagnostic_show_locus (&dc, &richloc, DK_ERROR);
    4708                 :         30 :     ASSERT_STREQ (" int a5[][0][0] = { 1, 2 };\n"
    4709                 :            :                   "                    ^\n"
    4710                 :            :                   "                     } {\n",
    4711                 :            :                   pp_formatted_text (dc.printer));
    4712                 :            :   }
    4713                 :            : 
    4714                 :            :   /* Various overlapping insertions, some occurring "out of order"
    4715                 :            :      (reproducing the fix-it hints from PR c/81405).  */
    4716                 :         30 :   {
    4717                 :         60 :     test_diagnostic_context dc;
    4718                 :         30 :     rich_location richloc (line_table, col_20);
    4719                 :            : 
    4720                 :         30 :     richloc.add_fixit_insert_before (col_20, "{{");
    4721                 :         30 :     richloc.add_fixit_insert_before (col_21, "}}");
    4722                 :         30 :     richloc.add_fixit_insert_before (col_23, "{");
    4723                 :         30 :     richloc.add_fixit_insert_before (col_21, "}");
    4724                 :         30 :     richloc.add_fixit_insert_before (col_23, "{{");
    4725                 :         30 :     richloc.add_fixit_insert_before (col_25, "}");
    4726                 :         30 :     richloc.add_fixit_insert_before (col_21, "}");
    4727                 :         30 :     richloc.add_fixit_insert_before (col_1, "{");
    4728                 :         30 :     richloc.add_fixit_insert_before (col_25, "}");
    4729                 :         30 :     diagnostic_show_locus (&dc, &richloc, DK_ERROR);
    4730                 :         30 :     ASSERT_STREQ (" int a5[][0][0] = { 1, 2 };\n"
    4731                 :            :                   "                    ^\n"
    4732                 :            :                   " {                  -----\n"
    4733                 :            :                   "                    {{1}}}}, {{{2 }}\n",
    4734                 :            :                   pp_formatted_text (dc.printer));
    4735                 :            :   }
    4736                 :            : }
    4737                 :            : 
    4738                 :            : /* Insertion fix-it hint: adding a "break;" on a line by itself.  */
    4739                 :            : 
    4740                 :            : static void
    4741                 :         48 : test_fixit_insert_containing_newline (const line_table_case &case_)
    4742                 :            : {
    4743                 :            :   /* Create a tempfile and write some text to it.
    4744                 :            :      .........................0000000001111111.
    4745                 :            :      .........................1234567890123456.  */
    4746                 :         48 :   const char *old_content = ("    case 'a':\n" /* line 1. */
    4747                 :            :                              "      x = a;\n"  /* line 2. */
    4748                 :            :                              "    case 'b':\n" /* line 3. */
    4749                 :            :                              "      x = b;\n");/* line 4. */
    4750                 :            : 
    4751                 :         80 :   temp_source_file tmp (SELFTEST_LOCATION, ".c", old_content);
    4752                 :         80 :   line_table_test ltt (case_);
    4753                 :         48 :   linemap_add (line_table, LC_ENTER, false, tmp.get_filename (), 3);
    4754                 :            : 
    4755                 :         48 :   location_t case_start = linemap_position_for_column (line_table, 5);
    4756                 :         48 :   location_t case_finish = linemap_position_for_column (line_table, 13);
    4757                 :         48 :   location_t case_loc = make_location (case_start, case_start, case_finish);
    4758                 :         48 :   location_t line_start = linemap_position_for_column (line_table, 1);
    4759                 :            : 
    4760                 :         48 :   if (case_finish > LINE_MAP_MAX_LOCATION_WITH_COLS)
    4761                 :         16 :     return;
    4762                 :            : 
    4763                 :            :   /* Add a "break;" on a line by itself before line 3 i.e. before
    4764                 :            :      column 1 of line 3. */
    4765                 :         32 :   {
    4766                 :         64 :     rich_location richloc (line_table, case_loc);
    4767                 :         32 :     richloc.add_fixit_insert_before (line_start, "      break;\n");
    4768                 :            : 
    4769                 :            :     /* Without line numbers.  */
    4770                 :         32 :     {
    4771                 :         32 :       test_diagnostic_context dc;
    4772                 :         32 :       diagnostic_show_locus (&dc, &richloc, DK_ERROR);
    4773                 :         32 :       ASSERT_STREQ ("       x = a;\n"
    4774                 :            :                     "+      break;\n"
    4775                 :            :                     "     case 'b':\n"
    4776                 :            :                     "     ^~~~~~~~~\n",
    4777                 :            :                     pp_formatted_text (dc.printer));
    4778                 :            :     }
    4779                 :            : 
    4780                 :            :     /* With line numbers.  */
    4781                 :         32 :     {
    4782                 :         32 :       test_diagnostic_context dc;
    4783                 :         32 :       dc.show_line_numbers_p = true;
    4784                 :         32 :       diagnostic_show_locus (&dc, &richloc, DK_ERROR);
    4785                 :         32 :       ASSERT_STREQ ("    2 |       x = a;\n"
    4786                 :            :                     "  +++ |+      break;\n"
    4787                 :            :                     "    3 |     case 'b':\n"
    4788                 :            :                     "      |     ^~~~~~~~~\n",
    4789                 :            :                     pp_formatted_text (dc.printer));
    4790                 :            :     }
    4791                 :            :   }
    4792                 :            : 
    4793                 :            :   /* Verify that attempts to add text with a newline fail when the
    4794                 :            :      insertion point is *not* at the start of a line.  */
    4795                 :         32 :   {
    4796                 :         64 :     rich_location richloc (line_table, case_loc);
    4797                 :         32 :     richloc.add_fixit_insert_before (case_start, "break;\n");
    4798                 :         32 :     ASSERT_TRUE (richloc.seen_impossible_fixit_p ());
    4799                 :         32 :     test_diagnostic_context dc;
    4800                 :         32 :     diagnostic_show_locus (&dc, &richloc, DK_ERROR);
    4801                 :         32 :     ASSERT_STREQ ("     case 'b':\n"
    4802                 :            :                   "     ^~~~~~~~~\n",
    4803                 :            :                   pp_formatted_text (dc.printer));
    4804                 :            :   }
    4805                 :            : }
    4806                 :            : 
    4807                 :            : /* Insertion fix-it hint: adding a "#include <stdio.h>\n" to the top
    4808                 :            :    of the file, where the fix-it is printed in a different line-span
    4809                 :            :    to the primary range of the diagnostic.  */
    4810                 :            : 
    4811                 :            : static void
    4812                 :         48 : test_fixit_insert_containing_newline_2 (const line_table_case &case_)
    4813                 :            : {
    4814                 :            :   /* Create a tempfile and write some text to it.
    4815                 :            :      .........................0000000001111111.
    4816                 :            :      .........................1234567890123456.  */
    4817                 :         48 :   const char *old_content = ("test (int ch)\n"  /* line 1. */
    4818                 :            :                              "{\n"              /* line 2. */
    4819                 :            :                              " putchar (ch);\n" /* line 3. */
    4820                 :            :                              "}\n");            /* line 4. */
    4821                 :            : 
    4822                 :         76 :   temp_source_file tmp (SELFTEST_LOCATION, ".c", old_content);
    4823                 :         76 :   line_table_test ltt (case_);
    4824                 :            : 
    4825                 :         48 :   const line_map_ordinary *ord_map = linemap_check_ordinary
    4826                 :         48 :     (linemap_add (line_table, LC_ENTER, false, tmp.get_filename (), 0));
    4827                 :         48 :   linemap_line_start (line_table, 1, 100);
    4828                 :            : 
    4829                 :            :   /* The primary range is the "putchar" token.  */
    4830                 :         48 :   location_t putchar_start
    4831                 :         48 :     = linemap_position_for_line_and_column (line_table, ord_map, 3, 2);
    4832                 :         48 :   location_t putchar_finish
    4833                 :         48 :     = linemap_position_for_line_and_column (line_table, ord_map, 3, 8);
    4834                 :         48 :   location_t putchar_loc
    4835                 :         48 :     = make_location (putchar_start, putchar_start, putchar_finish);
    4836                 :         76 :   rich_location richloc (line_table, putchar_loc);
    4837                 :            : 
    4838                 :            :   /* Add a "#include <stdio.h>" on a line by itself at the top of the file.  */
    4839                 :         48 :   location_t file_start
    4840                 :         48 :      = linemap_position_for_line_and_column (line_table, ord_map,  1, 1);
    4841                 :         48 :   richloc.add_fixit_insert_before (file_start, "#include <stdio.h>\n");
    4842                 :            : 
    4843                 :         48 :   if (putchar_finish > LINE_MAP_MAX_LOCATION_WITH_COLS)
    4844                 :         20 :     return;
    4845                 :            : 
    4846                 :         28 :   {
    4847                 :         28 :     test_diagnostic_context dc;
    4848                 :         28 :     diagnostic_show_locus (&dc, &richloc, DK_ERROR);
    4849                 :         28 :     ASSERT_STREQ ("FILENAME:1:1:\n"
    4850                 :            :                   "+#include <stdio.h>\n"
    4851                 :            :                   " test (int ch)\n"
    4852                 :            :                   "FILENAME:3:2:\n"
    4853                 :            :                   "  putchar (ch);\n"
    4854                 :            :                   "  ^~~~~~~\n",
    4855                 :            :                   pp_formatted_text (dc.printer));
    4856                 :            :   }
    4857                 :            : 
    4858                 :            :   /* With line-numbering, the line spans are close enough to be
    4859                 :            :      consolidated, since it makes little sense to skip line 2.  */
    4860                 :         28 :   {
    4861                 :         28 :     test_diagnostic_context dc;
    4862                 :         28 :     dc.show_line_numbers_p = true;
    4863                 :         28 :     diagnostic_show_locus (&dc, &richloc, DK_ERROR);
    4864                 :         28 :     ASSERT_STREQ ("  +++ |+#include <stdio.h>\n"
    4865                 :            :                   "    1 | test (int ch)\n"
    4866                 :            :                   "    2 | {\n"
    4867                 :            :                   "    3 |  putchar (ch);\n"
    4868                 :            :                   "      |  ^~~~~~~\n",
    4869                 :            :                   pp_formatted_text (dc.printer));
    4870                 :            :   }
    4871                 :            : }
    4872                 :            : 
    4873                 :            : /* Replacement fix-it hint containing a newline.
    4874                 :            :    This will fail, as newlines are only supported when inserting at the
    4875                 :            :    beginning of a line.  */
    4876                 :            : 
    4877                 :            : static void
    4878                 :         48 : test_fixit_replace_containing_newline (const line_table_case &case_)
    4879                 :            : {
    4880                 :            :   /* Create a tempfile and write some text to it.
    4881                 :            :     .........................0000000001111.
    4882                 :            :     .........................1234567890123.  */
    4883                 :         48 :   const char *old_content = "foo = bar ();\n";
    4884                 :            : 
    4885                 :         80 :   temp_source_file tmp (SELFTEST_LOCATION, ".c", old_content);
    4886                 :         80 :   line_table_test ltt (case_);
    4887                 :         48 :   linemap_add (line_table, LC_ENTER, false, tmp.get_filename (), 1);
    4888                 :            : 
    4889                 :            :   /* Replace the " = " with "\n  = ", as if we were reformatting an
    4890                 :            :      overly long line.  */
    4891                 :         48 :   location_t start = linemap_position_for_column (line_table, 4);
    4892                 :         48 :   location_t finish = linemap_position_for_column (line_table, 6);
    4893                 :         48 :   location_t loc = linemap_position_for_column (line_table, 13);
    4894                 :         80 :   rich_location richloc (line_table, loc);
    4895                 :         48 :   source_range range = source_range::from_locations (start, finish);
    4896                 :         48 :   richloc.add_fixit_replace (range, "\n =");
    4897                 :            : 
    4898                 :            :   /* Arbitrary newlines are not yet supported within fix-it hints, so
    4899                 :            :      the fix-it should not be displayed.  */
    4900                 :         48 :   ASSERT_TRUE (richloc.seen_impossible_fixit_p ());
    4901                 :            : 
    4902                 :         48 :   if (finish > LINE_MAP_MAX_LOCATION_WITH_COLS)
    4903                 :         16 :     return;
    4904                 :            : 
    4905                 :         32 :   test_diagnostic_context dc;
    4906                 :         32 :   diagnostic_show_locus (&dc, &richloc, DK_ERROR);
    4907                 :         32 :   ASSERT_STREQ (" foo = bar ();\n"
    4908                 :            :                 "             ^\n",
    4909                 :            :                 pp_formatted_text (dc.printer));
    4910                 :            : }
    4911                 :            : 
    4912                 :            : /* Fix-it hint, attempting to delete a newline.
    4913                 :            :    This will fail, as we currently only support fix-it hints that
    4914                 :            :    affect one line at a time.  */
    4915                 :            : 
    4916                 :            : static void
    4917                 :         48 : test_fixit_deletion_affecting_newline (const line_table_case &case_)
    4918                 :            : {
    4919                 :            :   /* Create a tempfile and write some text to it.
    4920                 :            :     ..........................0000000001111.
    4921                 :            :     ..........................1234567890123.  */
    4922                 :         48 :   const char *old_content = ("foo = bar (\n"
    4923                 :            :                              "      );\n");
    4924                 :            : 
    4925                 :         76 :   temp_source_file tmp (SELFTEST_LOCATION, ".c", old_content);
    4926                 :         76 :   line_table_test ltt (case_);
    4927                 :         48 :   const line_map_ordinary *ord_map = linemap_check_ordinary
    4928                 :         48 :     (linemap_add (line_table, LC_ENTER, false, tmp.get_filename (), 0));
    4929                 :         48 :   linemap_line_start (line_table, 1, 100);
    4930                 :            : 
    4931                 :            :   /* Attempt to delete the " (\n...)".  */
    4932                 :         48 :   location_t start
    4933                 :         48 :     = linemap_position_for_line_and_column (line_table, ord_map, 1, 10);
    4934                 :         48 :   location_t caret
    4935                 :         48 :     = linemap_position_for_line_and_column (line_table, ord_map, 1, 11);
    4936                 :         48 :   location_t finish
    4937                 :         48 :     = linemap_position_for_line_and_column (line_table, ord_map, 2, 7);
    4938                 :         48 :   location_t loc = make_location (caret, start, finish);
    4939                 :         76 :   rich_location richloc (line_table, loc);
    4940                 :         48 :   richloc. add_fixit_remove ();
    4941                 :            : 
    4942                 :            :   /* Fix-it hints that affect more than one line are not yet supported, so
    4943                 :            :      the fix-it should not be displayed.  */
    4944                 :         48 :   ASSERT_TRUE (richloc.seen_impossible_fixit_p ());
    4945                 :            : 
    4946                 :         48 :   if (finish > LINE_MAP_MAX_LOCATION_WITH_COLS)
    4947                 :         20 :     return;
    4948                 :            : 
    4949                 :         28 :   test_diagnostic_context dc;
    4950                 :         28 :   diagnostic_show_locus (&dc, &richloc, DK_ERROR);
    4951                 :         28 :   ASSERT_STREQ (" foo = bar (\n"
    4952                 :            :                 "          ~^\n"
    4953                 :            :                 "       );\n"
    4954                 :            :                 "       ~    \n",
    4955                 :            :                 pp_formatted_text (dc.printer));
    4956                 :            : }
    4957                 :            : 
    4958                 :            : /* Verify that line numbers are correctly printed for the case of
    4959                 :            :    a multiline range in which the width of the line numbers changes
    4960                 :            :    (e.g. from "9" to "10").  */
    4961                 :            : 
    4962                 :            : static void
    4963                 :          2 : test_line_numbers_multiline_range ()
    4964                 :            : {
    4965                 :            :   /* Create a tempfile and write some text to it.  */
    4966                 :          4 :   pretty_printer pp;
    4967                 :         42 :   for (int i = 0; i < 20; i++)
    4968                 :            :     /* .........0000000001111111.
    4969                 :            :    .............1234567890123456.  */
    4970                 :         40 :     pp_printf (&pp, "this is line %i\n", i + 1);
    4971                 :          4 :   temp_source_file tmp (SELFTEST_LOCATION, ".txt", pp_formatted_text (&pp));
    4972                 :          4 :   line_table_test ltt;
    4973                 :            : 
    4974                 :          2 :   const line_map_ordinary *ord_map = linemap_check_ordinary
    4975                 :          2 :     (linemap_add (line_table, LC_ENTER, false, tmp.get_filename (), 0));
    4976                 :          2 :   linemap_line_start (line_table, 1, 100);
    4977                 :            : 
    4978                 :            :   /* Create a multi-line location, starting at the "line" of line 9, with
    4979                 :            :      a caret on the "is" of line 10, finishing on the "this" line 11.  */
    4980                 :            : 
    4981                 :          2 :   location_t start
    4982                 :          2 :     = linemap_position_for_line_and_column (line_table, ord_map, 9, 9);
    4983                 :          2 :   location_t caret
    4984                 :          2 :     = linemap_position_for_line_and_column (line_table, ord_map, 10, 6);
    4985                 :          2 :   location_t finish
    4986                 :          2 :     = linemap_position_for_line_and_column (line_table, ord_map, 11, 4);
    4987                 :          2 :   location_t loc = make_location (caret, start, finish);
    4988                 :            : 
    4989                 :          2 :   test_diagnostic_context dc;
    4990                 :          2 :   dc.show_line_numbers_p = true;
    4991                 :          2 :   dc.min_margin_width = 0;
    4992                 :          4 :   gcc_rich_location richloc (loc);
    4993                 :          2 :   diagnostic_show_locus (&dc, &richloc, DK_ERROR);
    4994                 :          2 :   ASSERT_STREQ (" 9 | this is line 9\n"
    4995                 :            :                 "   |         ~~~~~~\n"
    4996                 :            :                 "10 | this is line 10\n"
    4997                 :            :                 "   | ~~~~~^~~~~~~~~~\n"
    4998                 :            :                 "11 | this is line 11\n"
    4999                 :            :                 "   | ~~~~  \n",
    5000                 :            :                 pp_formatted_text (dc.printer));
    5001                 :          2 : }
    5002                 :            : 
    5003                 :            : /* Run all of the selftests within this file.  */
    5004                 :            : 
    5005                 :            : void
    5006                 :          2 : diagnostic_show_locus_c_tests ()
    5007                 :            : {
    5008                 :          2 :   test_line_span ();
    5009                 :            : 
    5010                 :          2 :   test_layout_range_for_single_point ();
    5011                 :          2 :   test_layout_range_for_single_line ();
    5012                 :          2 :   test_layout_range_for_multiple_lines ();
    5013                 :            : 
    5014                 :          2 :   for_each_line_table_case (test_layout_x_offset_display_utf8);
    5015                 :            : 
    5016                 :          2 :   test_get_line_bytes_without_trailing_whitespace ();
    5017                 :            : 
    5018                 :          2 :   test_diagnostic_show_locus_unknown_location ();
    5019                 :            : 
    5020                 :          2 :   for_each_line_table_case (test_diagnostic_show_locus_one_liner);
    5021                 :          2 :   for_each_line_table_case (test_diagnostic_show_locus_one_liner_utf8);
    5022                 :          2 :   for_each_line_table_case (test_add_location_if_nearby);
    5023                 :          2 :   for_each_line_table_case (test_diagnostic_show_locus_fixit_lines);
    5024                 :          2 :   for_each_line_table_case (test_fixit_consolidation);
    5025                 :          2 :   for_each_line_table_case (test_overlapped_fixit_printing);
    5026                 :          2 :   for_each_line_table_case (test_overlapped_fixit_printing_utf8);
    5027                 :          2 :   for_each_line_table_case (test_overlapped_fixit_printing_2);
    5028                 :          2 :   for_each_line_table_case (test_fixit_insert_containing_newline);
    5029                 :          2 :   for_each_line_table_case (test_fixit_insert_containing_newline_2);
    5030                 :          2 :   for_each_line_table_case (test_fixit_replace_containing_newline);
    5031                 :          2 :   for_each_line_table_case (test_fixit_deletion_affecting_newline);
    5032                 :            : 
    5033                 :          2 :   test_line_numbers_multiline_range ();
    5034                 :          2 : }
    5035                 :            : 
    5036                 :            : } // namespace selftest
    5037                 :            : 
    5038                 :            : #endif /* #if CHECKING_P */
    5039                 :            : 
    5040                 :            : #if __GNUC__ >= 10
    5041                 :            : #  pragma GCC diagnostic pop
    5042                 :            : #endif

Generated by: LCOV version 1.0

LCOV profile is generated on x86_64 machine using following configure options: configure --disable-bootstrap --enable-coverage=opt --enable-languages=c,c++,fortran,go,jit,lto --enable-host-shared. GCC test suite is run with the built compiler.