CWG 616, 1213 - value category of subobject references.
[official-gcc.git] / gcc / gcc-rich-location.c
blob0a0adf932c1dceafc3872e12420ffd60256df090
1 /* Implementation of gcc_rich_location class
2 Copyright (C) 2014-2018 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 3, or (at your option) any later
9 version.
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 for more details.
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
20 #include "config.h"
21 #include "system.h"
22 #include "coretypes.h"
23 #include "tm.h"
24 #include "rtl.h"
25 #include "hash-set.h"
26 #include "vec.h"
27 #include "input.h"
28 #include "alias.h"
29 #include "symtab.h"
30 #include "inchash.h"
31 #include "tree-core.h"
32 #include "tree.h"
33 #include "diagnostic-core.h"
34 #include "gcc-rich-location.h"
35 #include "print-tree.h"
36 #include "pretty-print.h"
37 #include "intl.h"
38 #include "cpplib.h"
39 #include "diagnostic.h"
41 /* Add a range to the rich_location, covering expression EXPR. */
43 void
44 gcc_rich_location::add_expr (tree expr)
46 gcc_assert (expr);
48 if (CAN_HAVE_RANGE_P (expr))
49 add_range (EXPR_LOCATION (expr), false);
52 /* If T is an expression, add a range for it to the rich_location. */
54 void
55 gcc_rich_location::maybe_add_expr (tree t)
57 if (EXPR_P (t))
58 add_expr (t);
61 /* Add a fixit hint suggesting replacing the range at MISSPELLED_TOKEN_LOC
62 with the identifier HINT_ID. */
64 void
65 gcc_rich_location::add_fixit_misspelled_id (location_t misspelled_token_loc,
66 tree hint_id)
68 gcc_assert (TREE_CODE (hint_id) == IDENTIFIER_NODE);
70 add_fixit_replace (misspelled_token_loc, IDENTIFIER_POINTER (hint_id));
73 /* Return true if there is nothing on LOC's line before LOC. */
75 static bool
76 blank_line_before_p (location_t loc)
78 expanded_location exploc = expand_location (loc);
79 char_span line = location_get_source_line (exploc.file, exploc.line);
80 if (!line)
81 return false;
82 if (line.length () < (size_t)exploc.column)
83 return false;
84 /* Columns are 1-based. */
85 for (int column = 1; column < exploc.column; ++column)
86 if (!ISSPACE (line[column - 1]))
87 return false;
88 return true;
91 /* Subroutine of gcc_rich_location::add_fixit_insert_formatted.
92 Return true if we should add the content on its own line,
93 false otherwise.
94 If true is returned then *OUT_START_OF_LINE is written to. */
96 static bool
97 use_new_line (location_t insertion_point, location_t indent,
98 location_t *out_start_of_line)
100 if (indent == UNKNOWN_LOCATION)
101 return false;
102 const line_map *indent_map = linemap_lookup (line_table, indent);
103 if (linemap_macro_expansion_map_p (indent_map))
104 return false;
106 if (!blank_line_before_p (insertion_point))
107 return false;
109 /* Locate the start of the line containing INSERTION_POINT. */
110 const line_map *insertion_point_map
111 = linemap_lookup (line_table, insertion_point);
112 if (linemap_macro_expansion_map_p (insertion_point_map))
113 return false;
114 const line_map_ordinary *ordmap
115 = linemap_check_ordinary (insertion_point_map);
116 expanded_location exploc_insertion_point = expand_location (insertion_point);
117 location_t start_of_line
118 = linemap_position_for_line_and_column (line_table, ordmap,
119 exploc_insertion_point.line, 1);
120 *out_start_of_line = start_of_line;
121 return true;
124 /* Add a fix-it hint suggesting the insertion of CONTENT before
125 INSERTION_POINT.
127 Attempt to handle formatting: if INSERTION_POINT is the first thing on
128 its line, and INDENT is sufficiently sane, then add CONTENT on its own
129 line, using the indentation of INDENT.
130 Otherwise, add CONTENT directly before INSERTION_POINT.
132 For example, adding "CONTENT;" with the closing brace as the insertion
133 point and "INDENT;" as the indentation point:
135 if ()
137 INDENT;
140 would lead to:
142 if ()
144 INDENT;
145 CONTENT;
148 but adding it to:
150 if () {INDENT;}
152 would lead to:
154 if () {INDENT;CONTENT;}
157 void
158 gcc_rich_location::add_fixit_insert_formatted (const char *content,
159 location_t insertion_point,
160 location_t indent)
162 location_t start_of_line;
163 if (use_new_line (insertion_point, indent, &start_of_line))
165 /* Add CONTENT on its own line, using the indentation of INDENT. */
167 /* Generate an insertion string, indenting by the amount INDENT
168 was indented. */
169 int indent_column = LOCATION_COLUMN (get_start (indent));
170 pretty_printer tmp_pp;
171 pretty_printer *pp = &tmp_pp;
172 /* Columns are 1-based. */
173 for (int column = 1; column < indent_column; ++column)
174 pp_space (pp);
175 pp_string (pp, content);
176 pp_newline (pp);
178 add_fixit_insert_before (start_of_line, pp_formatted_text (pp));
180 else
181 add_fixit_insert_before (insertion_point, content);