summaryrefslogtreecommitdiffstats
path: root/contrib/libs/protoc/src/google/protobuf/compiler/importer.cc
diff options
context:
space:
mode:
authornechda <[email protected]>2024-08-29 23:50:27 +0300
committernechda <[email protected]>2024-08-30 00:05:25 +0300
commite10d6638f07a82edae3ea8197b9f5c0affcc07ea (patch)
tree571c38cec05813766a1ad290c9d51ce7ace52919 /contrib/libs/protoc/src/google/protobuf/compiler/importer.cc
parente79b38f2bbbf78d295d1901d2a79f898022d5224 (diff)
Update cpp-protobuf to 22.5
Привет!\ Этот PR переключат cpp & python библиотеки protobuf на версию 22.5 Если у вас возникли проблемы после влития этого PR: 1. Если начали падать канон тесты, то проведите их переканонизацию 2. Прочитайте <https://wiki.yandex-team.ru/users/nechda/obnovlenie-cpp-protobuf-22.5/> страничку с основными изменениями 3. Если страничка в вики не помогла, то пишите в [DEVTOOLSSUPPORT](https://st.yandex-team.ru/DEVTOOLSSUPPORT) 7fecade616c20a841b9e9af7b7998bdfc8d2807d
Diffstat (limited to 'contrib/libs/protoc/src/google/protobuf/compiler/importer.cc')
-rw-r--r--contrib/libs/protoc/src/google/protobuf/compiler/importer.cc133
1 files changed, 66 insertions, 67 deletions
diff --git a/contrib/libs/protoc/src/google/protobuf/compiler/importer.cc b/contrib/libs/protoc/src/google/protobuf/compiler/importer.cc
index 80bb1270ef6..91f1caddb31 100644
--- a/contrib/libs/protoc/src/google/protobuf/compiler/importer.cc
+++ b/contrib/libs/protoc/src/google/protobuf/compiler/importer.cc
@@ -32,6 +32,8 @@
// Based on original Protocol Buffers design by
// Sanjay Ghemawat, Jeff Dean, and others.
+#include "google/protobuf/compiler/importer.h"
+
#ifdef _MSC_VER
#include <direct.h>
#else
@@ -44,13 +46,18 @@
#include <algorithm>
#include <memory>
-
-#include <google/protobuf/compiler/importer.h>
-#include <google/protobuf/compiler/parser.h>
-#include <google/protobuf/io/tokenizer.h>
-#include <google/protobuf/io/zero_copy_stream_impl.h>
-#include <google/protobuf/stubs/strutil.h>
-#include <google/protobuf/io/io_win32.h>
+#include <vector>
+
+#include "y_absl/strings/match.h"
+#include "y_absl/strings/str_cat.h"
+#include "y_absl/strings/str_join.h"
+#include "y_absl/strings/str_replace.h"
+#include "y_absl/strings/str_split.h"
+#include "y_absl/strings/string_view.h"
+#include "google/protobuf/compiler/parser.h"
+#include "google/protobuf/io/io_win32.h"
+#include "google/protobuf/io/tokenizer.h"
+#include "google/protobuf/io/zero_copy_stream_impl.h"
#ifdef _WIN32
#include <ctype.h>
@@ -70,7 +77,7 @@ using google::protobuf::io::win32::open;
// Returns true if the text looks like a Windows-style absolute path, starting
// with a drive letter. Example: "C:\foo". TODO(kenton): Share this with
// copy in command_line_interface.cc?
-static bool IsWindowsAbsolutePath(const TProtoStringType& text) {
+static bool IsWindowsAbsolutePath(y_absl::string_view text) {
#if defined(_WIN32) || defined(__CYGWIN__)
return text.size() >= 3 && text[1] == ':' && isalpha(text[0]) &&
(text[2] == '/' || text[2] == '\\') && text.find_last_of(':') == 1;
@@ -98,9 +105,10 @@ class SourceTreeDescriptorDatabase::SingleFileErrorCollector
bool had_errors() { return had_errors_; }
// implements ErrorCollector ---------------------------------------
- void AddError(int line, int column, const TProtoStringType& message) override {
+ void RecordError(int line, int column, y_absl::string_view message) override {
if (multi_file_error_collector_ != nullptr) {
- multi_file_error_collector_->AddError(filename_, line, column, message);
+ multi_file_error_collector_->RecordError(filename_, line, column,
+ message);
}
had_errors_ = true;
}
@@ -140,8 +148,8 @@ bool SourceTreeDescriptorDatabase::FindFileByName(const TProtoStringType& filena
return true;
}
if (error_collector_ != nullptr) {
- error_collector_->AddError(filename, -1, 0,
- source_tree_->GetLastErrorMessage());
+ error_collector_->RecordError(filename, -1, 0,
+ source_tree_->GetLastErrorMessage());
}
return false;
}
@@ -183,10 +191,10 @@ SourceTreeDescriptorDatabase::ValidationErrorCollector::
SourceTreeDescriptorDatabase::ValidationErrorCollector::
~ValidationErrorCollector() {}
-void SourceTreeDescriptorDatabase::ValidationErrorCollector::AddError(
- const TProtoStringType& filename, const TProtoStringType& element_name,
+void SourceTreeDescriptorDatabase::ValidationErrorCollector::RecordError(
+ y_absl::string_view filename, y_absl::string_view element_name,
const Message* descriptor, ErrorLocation location,
- const TProtoStringType& message) {
+ y_absl::string_view message) {
if (owner_->error_collector_ == nullptr) return;
int line, column;
@@ -196,13 +204,13 @@ void SourceTreeDescriptorDatabase::ValidationErrorCollector::AddError(
} else {
owner_->source_locations_.Find(descriptor, location, &line, &column);
}
- owner_->error_collector_->AddError(filename, line, column, message);
+ owner_->error_collector_->RecordError(filename, line, column, message);
}
-void SourceTreeDescriptorDatabase::ValidationErrorCollector::AddWarning(
- const TProtoStringType& filename, const TProtoStringType& element_name,
+void SourceTreeDescriptorDatabase::ValidationErrorCollector::RecordWarning(
+ y_absl::string_view filename, y_absl::string_view element_name,
const Message* descriptor, ErrorLocation location,
- const TProtoStringType& message) {
+ y_absl::string_view message) {
if (owner_->error_collector_ == nullptr) return;
int line, column;
@@ -212,7 +220,7 @@ void SourceTreeDescriptorDatabase::ValidationErrorCollector::AddWarning(
} else {
owner_->source_locations_.Find(descriptor, location, &line, &column);
}
- owner_->error_collector_->AddWarning(filename, line, column, message);
+ owner_->error_collector_->RecordWarning(filename, line, column, message);
}
// ===================================================================
@@ -251,10 +259,6 @@ DiskSourceTree::DiskSourceTree() {}
DiskSourceTree::~DiskSourceTree() {}
-static inline char LastChar(const TProtoStringType& str) {
- return str[str.size() - 1];
-}
-
// Given a path, returns an equivalent path with these changes:
// - On Windows, any backslashes are replaced with forward slashes.
// - Any instances of the directory "." are removed.
@@ -274,45 +278,39 @@ static inline char LastChar(const TProtoStringType& str) {
// then if foo/bar is a symbolic link, foo/bar/baz.proto will canonicalize
// to a path which does not appear to be under foo, and thus the compiler
// will complain that baz.proto is not inside the --proto_path.
-static TProtoStringType CanonicalizePath(TProtoStringType path) {
+static TProtoStringType CanonicalizePath(y_absl::string_view path) {
#ifdef _WIN32
// The Win32 API accepts forward slashes as a path delimiter even though
// backslashes are standard. Let's avoid confusion and use only forward
// slashes.
- if (HasPrefixString(path, "\\\\")) {
+ TProtoStringType path_str;
+ if (y_absl::StartsWith(path, "\\\\")) {
// Avoid converting two leading backslashes.
- path = "\\\\" + StringReplace(path.substr(2), "\\", "/", true);
+ path_str = y_absl::StrCat("\\\\",
+ y_absl::StrReplaceAll(path.substr(2), {{"\\", "/"}}));
} else {
- path = StringReplace(path, "\\", "/", true);
+ path_str = y_absl::StrReplaceAll(path, {{"\\", "/"}});
}
+ path = path_str;
#endif
- std::vector<TProtoStringType> canonical_parts;
- std::vector<TProtoStringType> parts = Split(
- path, "/", true); // Note: Removes empty parts.
- for (const TProtoStringType& part : parts) {
+ std::vector<y_absl::string_view> canonical_parts;
+ if (!path.empty() && path.front() == '/') canonical_parts.push_back("");
+ for (y_absl::string_view part : y_absl::StrSplit(path, '/', y_absl::SkipEmpty())) {
if (part == ".") {
// Ignore.
} else {
canonical_parts.push_back(part);
}
}
- TProtoStringType result = Join(canonical_parts, "/");
- if (!path.empty() && path[0] == '/') {
- // Restore leading slash.
- result = '/' + result;
- }
- if (!path.empty() && LastChar(path) == '/' && !result.empty() &&
- LastChar(result) != '/') {
- // Restore trailing slash.
- result += '/';
- }
- return result;
+ if (!path.empty() && path.back() == '/') canonical_parts.push_back("");
+
+ return y_absl::StrJoin(canonical_parts, "/");
}
-static inline bool ContainsParentReference(const TProtoStringType& path) {
- return path == ".." || HasPrefixString(path, "../") ||
- HasSuffixString(path, "/..") || path.find("/../") != TProtoStringType::npos;
+static inline bool ContainsParentReference(y_absl::string_view path) {
+ return path == ".." || y_absl::StartsWith(path, "../") ||
+ y_absl::EndsWith(path, "/..") || y_absl::StrContains(path, "/../");
}
// Maps a file from an old location to a new one. Typically, old_prefix is
@@ -332,28 +330,28 @@ static inline bool ContainsParentReference(const TProtoStringType& path) {
// assert(!ApplyMapping("foo/bar", "baz", "qux", &result));
// assert(!ApplyMapping("foo/bar", "baz", "qux", &result));
// assert(!ApplyMapping("foobar", "foo", "baz", &result));
-static bool ApplyMapping(const TProtoStringType& filename,
- const TProtoStringType& old_prefix,
- const TProtoStringType& new_prefix, TProtoStringType* result) {
+static bool ApplyMapping(y_absl::string_view filename,
+ y_absl::string_view old_prefix,
+ y_absl::string_view new_prefix, TProtoStringType* result) {
if (old_prefix.empty()) {
// old_prefix matches any relative path.
if (ContainsParentReference(filename)) {
// We do not allow the file name to use "..".
return false;
}
- if (HasPrefixString(filename, "/") || IsWindowsAbsolutePath(filename)) {
+ if (y_absl::StartsWith(filename, "/") || IsWindowsAbsolutePath(filename)) {
// This is an absolute path, so it isn't matched by the empty string.
return false;
}
- result->assign(new_prefix);
+ result->assign(TProtoStringType(new_prefix));
if (!result->empty()) result->push_back('/');
- result->append(filename);
+ result->append(TProtoStringType(filename));
return true;
- } else if (HasPrefixString(filename, old_prefix)) {
+ } else if (y_absl::StartsWith(filename, old_prefix)) {
// old_prefix is a prefix of the filename. Is it the whole filename?
if (filename.size() == old_prefix.size()) {
// Yep, it's an exact match.
- *result = new_prefix;
+ *result = TProtoStringType(new_prefix);
return true;
} else {
// Not an exact match. Is the next character a '/'? Otherwise,
@@ -370,14 +368,14 @@ static bool ApplyMapping(const TProtoStringType& filename,
if (after_prefix_start != -1) {
// Yep. So the prefixes are directories and the filename is a file
// inside them.
- TProtoStringType after_prefix = filename.substr(after_prefix_start);
+ y_absl::string_view after_prefix = filename.substr(after_prefix_start);
if (ContainsParentReference(after_prefix)) {
// We do not allow the file name to use "..".
return false;
}
- result->assign(new_prefix);
+ result->assign(TProtoStringType(new_prefix));
if (!result->empty()) result->push_back('/');
- result->append(after_prefix);
+ result->append(TProtoStringType(after_prefix));
return true;
}
}
@@ -386,13 +384,14 @@ static bool ApplyMapping(const TProtoStringType& filename,
return false;
}
-void DiskSourceTree::MapPath(const TProtoStringType& virtual_path,
- const TProtoStringType& disk_path) {
- mappings_.push_back(Mapping(virtual_path, CanonicalizePath(disk_path)));
+void DiskSourceTree::MapPath(y_absl::string_view virtual_path,
+ y_absl::string_view disk_path) {
+ mappings_.push_back(
+ Mapping(TProtoStringType(virtual_path), CanonicalizePath(disk_path)));
}
DiskSourceTree::DiskFileToVirtualFileResult
-DiskSourceTree::DiskFileToVirtualFile(const TProtoStringType& disk_file,
+DiskSourceTree::DiskFileToVirtualFile(y_absl::string_view disk_file,
TProtoStringType* virtual_file,
TProtoStringType* shadowing_disk_file) {
int mapping_index = -1;
@@ -436,7 +435,7 @@ DiskSourceTree::DiskFileToVirtualFile(const TProtoStringType& disk_file,
return SUCCESS;
}
-bool DiskSourceTree::VirtualFileToDiskFile(const TProtoStringType& virtual_file,
+bool DiskSourceTree::VirtualFileToDiskFile(y_absl::string_view virtual_file,
TProtoStringType* disk_file) {
std::unique_ptr<io::ZeroCopyInputStream> stream(
OpenVirtualFile(virtual_file, disk_file));
@@ -452,7 +451,7 @@ TProtoStringType DiskSourceTree::GetLastErrorMessage() {
}
io::ZeroCopyInputStream* DiskSourceTree::OpenVirtualFile(
- const TProtoStringType& virtual_file, TProtoStringType* disk_file) {
+ y_absl::string_view virtual_file, TProtoStringType* disk_file) {
if (virtual_file != CanonicalizePath(virtual_file) ||
ContainsParentReference(virtual_file)) {
// We do not allow importing of paths containing things like ".." or
@@ -479,7 +478,7 @@ io::ZeroCopyInputStream* DiskSourceTree::OpenVirtualFile(
if (errno == EACCES) {
// The file exists but is not readable.
last_error_message_ =
- "Read access is denied for file: " + temp_disk_file;
+ y_absl::StrCat("Read access is denied for file: ", temp_disk_file);
return nullptr;
}
}
@@ -489,11 +488,11 @@ io::ZeroCopyInputStream* DiskSourceTree::OpenVirtualFile(
}
io::ZeroCopyInputStream* DiskSourceTree::OpenDiskFile(
- const TProtoStringType& filename) {
+ y_absl::string_view filename) {
struct stat sb;
int ret = 0;
do {
- ret = stat(filename.c_str(), &sb);
+ ret = stat(TProtoStringType(filename).c_str(), &sb);
} while (ret != 0 && errno == EINTR);
#if defined(_WIN32)
if (ret == 0 && sb.st_mode & S_IFDIR) {
@@ -508,7 +507,7 @@ io::ZeroCopyInputStream* DiskSourceTree::OpenDiskFile(
#endif
int file_descriptor;
do {
- file_descriptor = open(filename.c_str(), O_RDONLY);
+ file_descriptor = open(TProtoStringType(filename).c_str(), O_RDONLY);
} while (file_descriptor < 0 && errno == EINTR);
if (file_descriptor >= 0) {
io::FileInputStream* result = new io::FileInputStream(file_descriptor);