19
19
20
20
#include < fmt/core.h>
21
21
22
- #include < assert.h>
23
- #include < errno.h>
24
- #include < string.h>
25
22
#include < algorithm>
23
+ #include < cassert>
24
+ #include < cerrno>
25
+ #include < cstring>
26
26
#include < filesystem>
27
27
#include < stdexcept>
28
28
#include < string>
@@ -84,24 +84,24 @@ std::vector<uint_least32_t> get_line_offsets(std::string_view sv) {
84
84
} // namespace
85
85
86
86
source source_manager::add_source (
87
- const std::string& file_name, std::vector<char > text) {
87
+ std::string_view file_name, std::vector<char > text) {
88
88
assert (text.back () == ' \0 ' );
89
89
std::string_view sv (text.data (), text.size ());
90
- sources_.push_back (
91
- source_info{ file_name, std::move (text), get_line_offsets (sv)});
90
+ sources_.push_back (source_info{
91
+ std::string ( file_name) , std::move (text), get_line_offsets (sv)});
92
92
return {/* .start = */
93
93
source_location (/* source_id= */ sources_.size (), /* * offset= */ 0 ),
94
94
/* .text = */ sv};
95
95
}
96
96
97
- std::string source_manager::get_file_path (const std::string& file_name) const {
97
+ std::string source_manager::get_file_path (std::string_view file_name) const {
98
98
if (file_source_map_.find (file_name) != file_source_map_.end ()) {
99
- return file_name;
99
+ return std::string ( file_name) ;
100
100
}
101
101
return std::filesystem::absolute (file_name).string ();
102
102
}
103
103
104
- std::optional<source> source_manager::get_file (const std::string& file_name) {
104
+ std::optional<source> source_manager::get_file (std::string_view file_name) {
105
105
if (auto source = file_source_map_.find (file_name);
106
106
source != file_source_map_.end ()) {
107
107
return source->second ;
@@ -156,11 +156,11 @@ std::optional<source> source_manager::get_file(const std::string& file_name) {
156
156
}
157
157
158
158
source source_manager::add_virtual_file (
159
- const std::string& file_name, const std::string& src) {
159
+ std::string_view file_name, std::string_view src) {
160
160
if (file_source_map_.find (file_name) != file_source_map_.end ()) {
161
- throw std::runtime_error (std::string (" file already added: " ) + file_name);
161
+ throw std::runtime_error (fmt::format (" file already added: {} " , file_name) );
162
162
}
163
- const char * start = src.c_str ();
163
+ const char * start = src.data ();
164
164
auto source =
165
165
add_source (file_name, std::vector<char >(start, start + src.size () + 1 ));
166
166
file_source_map_.emplace (file_name, source);
@@ -202,21 +202,21 @@ resolved_location::resolved_location(
202
202
}
203
203
204
204
source_manager::path_or_error source_manager::find_include_file (
205
- const std::string& filename,
206
- const std::string& parent_path,
205
+ std::string_view filename,
206
+ std::string_view parent_path,
207
207
const std::vector<std::string>& search_paths) {
208
208
if (auto itr = found_includes_.find (filename); itr != found_includes_.end ()) {
209
209
return source_manager::path_or_error{std::in_place_index<0 >, itr->second };
210
210
}
211
211
212
212
auto found = [&](std::string path) {
213
- found_includes_[filename] = path;
213
+ found_includes_[std::string ( filename) ] = path;
214
214
return source_manager::path_or_error{
215
215
std::in_place_index<0 >, std::move (path)};
216
216
};
217
217
218
218
if (file_source_map_.find (filename) != file_source_map_.end ()) {
219
- return found (filename);
219
+ return found (std::string ( filename) );
220
220
}
221
221
222
222
// Absolute path? Just try that.
@@ -236,7 +236,7 @@ source_manager::path_or_error source_manager::find_include_file(
236
236
// new search path with current dir global
237
237
std::vector<std::string> sp = search_paths;
238
238
auto itr = found_includes_.find (parent_path);
239
- const std::string& resolved_parent_path =
239
+ std::string_view resolved_parent_path =
240
240
itr != found_includes_.end () ? itr->second : parent_path;
241
241
auto dir = std::filesystem::path (resolved_parent_path).parent_path ().string ();
242
242
dir = dir.empty () ? " ." : dir;
@@ -271,7 +271,7 @@ source_manager::path_or_error source_manager::find_include_file(
271
271
}
272
272
273
273
std::optional<std::string> source_manager::found_include_file (
274
- const std::string& filename) const {
274
+ std::string_view filename) const {
275
275
if (auto itr = found_includes_.find (filename); itr != found_includes_.end ()) {
276
276
return itr->second ;
277
277
}
0 commit comments