Skip to content

Commit c7abd10

Browse files
praihanfacebook-github-bot
authored andcommitted
Make source_location use string_view instead of const string&
Summary: `const std::string&` requires the caller to copy if all they have is `std::string_view`. Let's modernize the code here. Heterogenous lookups are not supported by `std::unordered_map` until C++20 so I've had to switch to `std::map`. This shouldn't be a problem in practice. Change is no-op. Reviewed By: iahs Differential Revision: D69197835 fbshipit-source-id: 83b0849a5bada97f9c41b13845a5567aeb2f2694
1 parent ef103b5 commit c7abd10

File tree

2 files changed

+30
-30
lines changed

2 files changed

+30
-30
lines changed

thrift/compiler/source_location.cc

+18-18
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,10 @@
1919

2020
#include <fmt/core.h>
2121

22-
#include <assert.h>
23-
#include <errno.h>
24-
#include <string.h>
2522
#include <algorithm>
23+
#include <cassert>
24+
#include <cerrno>
25+
#include <cstring>
2626
#include <filesystem>
2727
#include <stdexcept>
2828
#include <string>
@@ -84,24 +84,24 @@ std::vector<uint_least32_t> get_line_offsets(std::string_view sv) {
8484
} // namespace
8585

8686
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) {
8888
assert(text.back() == '\0');
8989
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)});
9292
return {/* .start = */
9393
source_location(/* source_id= */ sources_.size(), /** offset= */ 0),
9494
/* .text = */ sv};
9595
}
9696

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 {
9898
if (file_source_map_.find(file_name) != file_source_map_.end()) {
99-
return file_name;
99+
return std::string(file_name);
100100
}
101101
return std::filesystem::absolute(file_name).string();
102102
}
103103

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) {
105105
if (auto source = file_source_map_.find(file_name);
106106
source != file_source_map_.end()) {
107107
return source->second;
@@ -156,11 +156,11 @@ std::optional<source> source_manager::get_file(const std::string& file_name) {
156156
}
157157

158158
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) {
160160
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));
162162
}
163-
const char* start = src.c_str();
163+
const char* start = src.data();
164164
auto source =
165165
add_source(file_name, std::vector<char>(start, start + src.size() + 1));
166166
file_source_map_.emplace(file_name, source);
@@ -202,21 +202,21 @@ resolved_location::resolved_location(
202202
}
203203

204204
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,
207207
const std::vector<std::string>& search_paths) {
208208
if (auto itr = found_includes_.find(filename); itr != found_includes_.end()) {
209209
return source_manager::path_or_error{std::in_place_index<0>, itr->second};
210210
}
211211

212212
auto found = [&](std::string path) {
213-
found_includes_[filename] = path;
213+
found_includes_[std::string(filename)] = path;
214214
return source_manager::path_or_error{
215215
std::in_place_index<0>, std::move(path)};
216216
};
217217

218218
if (file_source_map_.find(filename) != file_source_map_.end()) {
219-
return found(filename);
219+
return found(std::string(filename));
220220
}
221221

222222
// Absolute path? Just try that.
@@ -236,7 +236,7 @@ source_manager::path_or_error source_manager::find_include_file(
236236
// new search path with current dir global
237237
std::vector<std::string> sp = search_paths;
238238
auto itr = found_includes_.find(parent_path);
239-
const std::string& resolved_parent_path =
239+
std::string_view resolved_parent_path =
240240
itr != found_includes_.end() ? itr->second : parent_path;
241241
auto dir = std::filesystem::path(resolved_parent_path).parent_path().string();
242242
dir = dir.empty() ? "." : dir;
@@ -271,7 +271,7 @@ source_manager::path_or_error source_manager::find_include_file(
271271
}
272272

273273
std::optional<std::string> source_manager::found_include_file(
274-
const std::string& filename) const {
274+
std::string_view filename) const {
275275
if (auto itr = found_includes_.find(filename); itr != found_includes_.end()) {
276276
return itr->second;
277277
}

thrift/compiler/source_location.h

+12-12
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,13 @@
1616

1717
#pragma once
1818

19-
#include <stdint.h>
20-
2119
#include <cstdint>
2220
#include <deque>
21+
#include <functional>
22+
#include <map>
2323
#include <optional>
2424
#include <string>
25-
#include <unordered_map>
25+
#include <string_view>
2626
#include <variant>
2727
#include <vector>
2828

@@ -115,10 +115,10 @@ class source_manager {
115115
// sources_ grows.
116116
std::deque<source_info> sources_;
117117

118-
std::unordered_map<std::string, source> file_source_map_;
118+
std::map<std::string, source, std::less<>> file_source_map_;
119119

120120
// Maps from filepaths present in the AST to filepaths on disk.
121-
std::unordered_map<std::string, std::string> found_includes_;
121+
std::map<std::string, std::string, std::less<>> found_includes_;
122122

123123
const source_info* get_source(uint_least32_t source_id) const {
124124
return source_id > 0 && source_id <= sources_.size()
@@ -128,20 +128,20 @@ class source_manager {
128128

129129
friend class resolved_location;
130130

131-
source add_source(const std::string& file_name, std::vector<char> text);
131+
source add_source(std::string_view file_name, std::vector<char> text);
132132

133133
public:
134134
// Loads a file and returns a source object representing its content.
135135
// The file can be a real file or a virtual one previously registered with
136136
// add_virtual_file.
137137
// Returns an empty optional if opening or reading the file fails.
138138
// Makes use of the result of previous calls to find_include_file.
139-
std::optional<source> get_file(const std::string& file_name);
139+
std::optional<source> get_file(std::string_view file_name);
140140

141-
std::string get_file_path(const std::string& file_name) const;
141+
std::string get_file_path(std::string_view file_name) const;
142142

143143
// Adds a virtual file with the specified name and content.
144-
source add_virtual_file(const std::string& file_name, const std::string& src);
144+
source add_virtual_file(std::string_view file_name, std::string_view src);
145145

146146
// Returns the start location of a source containing the specified location.
147147
// It is a member function in case we add clang-like compression of locations.
@@ -171,12 +171,12 @@ class source_manager {
171171
// The resolved path is made available to get_file.
172172
using path_or_error = std::variant<std::string, std::string>;
173173
path_or_error find_include_file(
174-
const std::string& filename,
175-
const std::string& program_path,
174+
std::string_view filename,
175+
std::string_view program_path,
176176
const std::vector<std::string>& search_paths);
177177
// Queries for a file previously found by find_include_file.
178178
std::optional<std::string> found_include_file(
179-
const std::string& filename) const;
179+
std::string_view filename) const;
180180
};
181181

182182
} // namespace apache::thrift::compiler

0 commit comments

Comments
 (0)