1 // Copyright (C) 2020-2023 Free Software Foundation, Inc.
3 // This file is part of GCC.
5 // GCC is free software; you can redistribute it and/or modify it under
6 // the terms of the GNU General Public License as published by the Free
7 // Software Foundation; either version 3, or (at your option) any later
10 // GCC is distributed in the hope that it will be useful, but WITHOUT ANY
11 // WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 // You should have received a copy of the GNU General Public License
16 // along with GCC; see the file COPYING3. If not see
17 // <http://www.gnu.org/licenses/>.
19 // rust-linemap.cc -- GCC implementation of Linemap.
21 #include "rust-linemap.h"
23 // This class implements the Linemap interface defined by the
26 class Gcc_linemap
: public Linemap
29 Gcc_linemap () : Linemap (), in_file_ (false) {}
31 void start_file (const char *file_name
, unsigned int line_begin
);
33 void start_line (unsigned int line_number
, unsigned int line_size
);
35 Location
get_location (unsigned int column
);
39 std::string
to_string (Location
);
41 std::string
location_file (Location
);
43 int location_line (Location
);
45 int location_column (Location
);
48 Location
get_predeclared_location ();
50 Location
get_unknown_location ();
52 bool is_predeclared (Location
);
54 bool is_unknown (Location
);
57 // Whether we are currently reading a file.
61 Linemap
*Linemap::instance_
= NULL
;
63 // Start getting locations from a new file.
66 Gcc_linemap::start_file (const char *file_name
, unsigned line_begin
)
69 linemap_add (line_table
, LC_LEAVE
, 0, NULL
, 0);
70 linemap_add (line_table
, LC_ENTER
, 0, file_name
, line_begin
);
71 this->in_file_
= true;
74 // Stringify a location
77 Gcc_linemap::to_string (Location location
)
79 const line_map_ordinary
*lmo
;
80 location_t resolved_location
;
82 // Screen out unknown and predeclared locations; produce output
83 // only for simple file:line locations.
85 = linemap_resolve_location (line_table
, location
.gcc_location (),
86 LRK_SPELLING_LOCATION
, &lmo
);
87 if (lmo
== NULL
|| resolved_location
< RESERVED_LOCATION_COUNT
)
89 const char *path
= LINEMAP_FILE (lmo
);
93 // Strip the source file down to the base file, to reduce clutter.
95 ss
<< lbasename (path
) << ":" << SOURCE_LINE (lmo
, location
.gcc_location ())
96 << ":" << SOURCE_COLUMN (lmo
, location
.gcc_location ());
100 // Return the file name for a given location.
103 Gcc_linemap::location_file (Location loc
)
105 return LOCATION_FILE (loc
.gcc_location ());
108 // Return the line number for a given location.
111 Gcc_linemap::location_line (Location loc
)
113 return LOCATION_LINE (loc
.gcc_location ());
116 // Return the column number for a given location.
118 Gcc_linemap::location_column (Location loc
)
120 return LOCATION_COLUMN (loc
.gcc_location ());
123 // Stop getting locations.
128 linemap_add (line_table
, LC_LEAVE
, 0, NULL
, 0);
129 this->in_file_
= false;
135 Gcc_linemap::start_line (unsigned lineno
, unsigned linesize
)
137 linemap_line_start (line_table
, lineno
, linesize
);
143 Gcc_linemap::get_location (unsigned column
)
145 return Location (linemap_position_for_column (line_table
, column
));
148 // Get the unknown location.
151 Gcc_linemap::get_unknown_location ()
153 return Location (UNKNOWN_LOCATION
);
156 // Get the predeclared location.
159 Gcc_linemap::get_predeclared_location ()
161 return Location (BUILTINS_LOCATION
);
164 // Return whether a location is the predeclared location.
167 Gcc_linemap::is_predeclared (Location loc
)
169 return loc
.gcc_location () == BUILTINS_LOCATION
;
172 // Return whether a location is the unknown location.
175 Gcc_linemap::is_unknown (Location loc
)
177 return loc
.gcc_location () == UNKNOWN_LOCATION
;
180 // Return the Linemap to use for the gcc backend.
185 return new Gcc_linemap
;
188 RichLocation::RichLocation (Location root
)
189 : gcc_rich_loc (line_table
, root
.gcc_location ())
191 /*rich_location (line_maps *set, location_t loc,
192 const range_label *label = NULL);*/
195 RichLocation::~RichLocation () {}
198 RichLocation::add_range (Location loc
)
200 gcc_rich_loc
.add_range (loc
.gcc_location ());
204 RichLocation::add_fixit_insert_before (const std::string
&new_parent
)
206 gcc_rich_loc
.add_fixit_insert_before (new_parent
.c_str ());
210 RichLocation::add_fixit_insert_before (Location where
,
211 const std::string
&new_parent
)
213 gcc_rich_loc
.add_fixit_insert_before (where
.gcc_location (),
214 new_parent
.c_str ());
218 RichLocation::add_fixit_insert_after (const std::string
&new_parent
)
220 gcc_rich_loc
.add_fixit_insert_after (new_parent
.c_str ());
224 RichLocation::add_fixit_insert_after (Location where
,
225 const std::string
&new_parent
)
227 gcc_rich_loc
.add_fixit_insert_after (where
.gcc_location (),
228 new_parent
.c_str ());