Prepare the code for migrating return types from `const std::string&` to

`absl::string_view`.

PiperOrigin-RevId: 651520812
pull/17420/head
Protobuf Team Bot 5 months ago committed by Copybara-Service
parent 34136b3794
commit b8cb34d744
  1. 1
      hpb_generator/BUILD
  2. 3
      hpb_generator/gen_utils.cc
  3. 3
      hpb_generator/gen_utils.h
  4. 5
      hpb_generator/names.cc
  5. 76
      src/google/protobuf/compiler/command_line_interface.cc
  6. 2
      src/google/protobuf/compiler/cpp/enum.cc
  7. 3
      src/google/protobuf/compiler/cpp/extension.cc
  8. 4
      src/google/protobuf/compiler/cpp/file.cc
  9. 6
      src/google/protobuf/compiler/cpp/helpers.cc
  10. 4
      src/google/protobuf/compiler/cpp/helpers.h
  11. 21
      src/google/protobuf/compiler/cpp/unittest.inc
  12. 2
      src/google/protobuf/compiler/csharp/csharp_enum.cc
  13. 4
      src/google/protobuf/compiler/csharp/csharp_helpers.cc
  14. 2
      src/google/protobuf/compiler/csharp/csharp_helpers.h
  15. 5
      src/google/protobuf/compiler/csharp/csharp_message.cc
  16. 4
      src/google/protobuf/compiler/csharp/names.cc
  17. 7
      src/google/protobuf/compiler/java/doc_comment.cc
  18. 3
      src/google/protobuf/compiler/java/doc_comment.h
  19. 2
      src/google/protobuf/compiler/java/helpers.h
  20. 2
      src/google/protobuf/compiler/java/lite/message.cc
  21. 4
      src/google/protobuf/compiler/java/name_resolver.cc
  22. 6
      src/google/protobuf/compiler/java/shared_code_generator.cc
  23. 2
      src/google/protobuf/compiler/objectivec/enum.cc
  24. 3
      src/google/protobuf/compiler/objectivec/field.cc
  25. 2
      src/google/protobuf/compiler/objectivec/file.cc
  26. 4
      src/google/protobuf/compiler/objectivec/helpers.cc
  27. 18
      src/google/protobuf/compiler/objectivec/names.cc
  28. 15
      src/google/protobuf/compiler/php/names.cc
  29. 6
      src/google/protobuf/compiler/php/names.h
  30. 17
      src/google/protobuf/compiler/php/php_generator.cc
  31. 2
      src/google/protobuf/compiler/python/generator.cc
  32. 2
      src/google/protobuf/compiler/python/helpers.cc
  33. 12
      src/google/protobuf/compiler/python/pyi_generator.cc

@ -103,5 +103,6 @@ cc_library(
":output", ":output",
"//src/google/protobuf", "//src/google/protobuf",
"//upb_generator:keywords", "//upb_generator:keywords",
"@com_google_absl//absl/strings:string_view",
], ],
) )

@ -12,6 +12,7 @@
#include <vector> #include <vector>
#include "absl/strings/ascii.h" #include "absl/strings/ascii.h"
#include "absl/strings/string_view.h"
namespace google::protobuf::hpb_generator { namespace google::protobuf::hpb_generator {
@ -102,7 +103,7 @@ std::vector<const protobuf::FieldDescriptor*> FieldNumberOrder(
return fields; return fields;
} }
std::string ToCamelCase(const std::string& input, bool lower_first) { std::string ToCamelCase(const absl::string_view input, bool lower_first) {
bool capitalize_next = !lower_first; bool capitalize_next = !lower_first;
std::string result; std::string result;
result.reserve(input.size()); result.reserve(input.size());

@ -12,6 +12,7 @@
#include <vector> #include <vector>
#include "google/protobuf/descriptor.pb.h" #include "google/protobuf/descriptor.pb.h"
#include "absl/strings/string_view.h"
#include "google/protobuf/compiler/code_generator.h" #include "google/protobuf/compiler/code_generator.h"
#include "google/protobuf/descriptor.h" #include "google/protobuf/descriptor.h"
@ -38,7 +39,7 @@ std::vector<const protobuf::FieldDescriptor*> SortedExtensions(
std::vector<const protobuf::FieldDescriptor*> FieldNumberOrder( std::vector<const protobuf::FieldDescriptor*> FieldNumberOrder(
const protobuf::Descriptor* message); const protobuf::Descriptor* message);
std::string ToCamelCase(const std::string& input, bool lower_first); std::string ToCamelCase(absl::string_view input, bool lower_first);
} // namespace protobuf } // namespace protobuf
} // namespace google::hpb_generator } // namespace google::hpb_generator

@ -9,6 +9,7 @@
#include <string> #include <string>
#include "absl/strings/string_view.h"
#include "upb_generator/keywords.h" #include "upb_generator/keywords.h"
namespace google::protobuf::hpb_generator { namespace google::protobuf::hpb_generator {
@ -21,11 +22,11 @@ std::string NamespaceFromPackageName(absl::string_view package_name) {
"::protos"); "::protos");
} }
std::string DotsToColons(const std::string& name) { std::string DotsToColons(const absl::string_view name) {
return absl::StrReplaceAll(name, {{".", "::"}}); return absl::StrReplaceAll(name, {{".", "::"}});
} }
std::string Namespace(const std::string& package) { std::string Namespace(const absl::string_view package) {
if (package.empty()) return ""; if (package.empty()) return "";
return "::" + DotsToColons(package); return "::" + DotsToColons(package);
} }

@ -11,32 +11,23 @@
#include "google/protobuf/compiler/command_line_interface.h" #include "google/protobuf/compiler/command_line_interface.h"
#include <cstdint> #include <errno.h>
#include <cstdlib> #include <limits.h>
#include <cstring>
#include "absl/algorithm/container.h"
#include "absl/base/attributes.h"
#include "absl/base/log_severity.h"
#include "absl/container/btree_map.h"
#include "absl/container/btree_set.h"
#include "absl/container/flat_hash_map.h"
#include "absl/log/globals.h"
#include "absl/status/status.h"
#include "absl/status/statusor.h"
#include "absl/strings/str_cat.h"
#include "absl/types/span.h"
#include "google/protobuf/compiler/versions.h"
#include "google/protobuf/descriptor_database.h"
#include "google/protobuf/descriptor_visitor.h"
#include "google/protobuf/feature_resolver.h"
#include "google/protobuf/io/zero_copy_stream_impl_lite.h"
#include "google/protobuf/stubs/platform_macros.h"
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <sys/types.h> #include <sys/types.h>
#include <algorithm>
#include <cstdint>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <iostream>
#include <memory>
#include <ostream>
#include <string>
#include <utility>
#include <vector>
#ifdef major #ifdef major
#undef major #undef major
#endif #endif
@ -45,20 +36,10 @@
#endif #endif
#include <fcntl.h> #include <fcntl.h>
#include <sys/stat.h> #include <sys/stat.h>
#ifndef _MSC_VER #ifndef _MSC_VER
#include <unistd.h> #include <unistd.h>
#endif #endif
#include <errno.h>
#include <fstream>
#include <iostream>
#include <limits.h> // For PATH_MAX
#include <memory>
#include <string>
#include <utility>
#include <vector>
#if defined(__APPLE__) #if defined(__APPLE__)
#include <mach-o/dyld.h> #include <mach-o/dyld.h>
@ -66,27 +47,44 @@
#include <sys/sysctl.h> #include <sys/sysctl.h>
#endif #endif
#include "absl/algorithm/container.h"
#include "absl/base/attributes.h"
#include "absl/base/log_severity.h"
#include "absl/container/btree_map.h"
#include "absl/container/btree_set.h"
#include "absl/container/flat_hash_map.h"
#include "absl/container/flat_hash_set.h"
#include "absl/log/absl_check.h" #include "absl/log/absl_check.h"
#include "absl/log/absl_log.h" #include "absl/log/absl_log.h"
#include "absl/container/flat_hash_set.h" #include "absl/log/globals.h"
#include "absl/status/status.h"
#include "absl/status/statusor.h"
#include "absl/strings/ascii.h"
#include "absl/strings/match.h" #include "absl/strings/match.h"
#include "absl/strings/str_cat.h"
#include "absl/strings/str_format.h" #include "absl/strings/str_format.h"
#include "absl/strings/str_replace.h" #include "absl/strings/str_replace.h"
#include "absl/strings/str_split.h" #include "absl/strings/str_split.h"
#include "absl/strings/string_view.h" #include "absl/strings/string_view.h"
#include "absl/strings/substitute.h" #include "absl/strings/substitute.h"
#include "absl/types/span.h"
#include "google/protobuf/compiler/code_generator.h" #include "google/protobuf/compiler/code_generator.h"
#include "google/protobuf/compiler/importer.h" #include "google/protobuf/compiler/importer.h"
#include "google/protobuf/compiler/plugin.pb.h" #include "google/protobuf/compiler/plugin.pb.h"
#include "google/protobuf/compiler/retention.h" #include "google/protobuf/compiler/retention.h"
#include "google/protobuf/compiler/subprocess.h" #include "google/protobuf/compiler/subprocess.h"
#include "google/protobuf/compiler/versions.h"
#include "google/protobuf/compiler/zip_writer.h" #include "google/protobuf/compiler/zip_writer.h"
#include "google/protobuf/descriptor.h" #include "google/protobuf/descriptor.h"
#include "google/protobuf/descriptor.pb.h" #include "google/protobuf/descriptor.pb.h"
#include "google/protobuf/descriptor_database.h"
#include "google/protobuf/descriptor_visitor.h"
#include "google/protobuf/dynamic_message.h" #include "google/protobuf/dynamic_message.h"
#include "google/protobuf/feature_resolver.h"
#include "google/protobuf/io/coded_stream.h" #include "google/protobuf/io/coded_stream.h"
#include "google/protobuf/io/printer.h" #include "google/protobuf/io/printer.h"
#include "google/protobuf/io/zero_copy_stream_impl.h" #include "google/protobuf/io/zero_copy_stream_impl.h"
#include "google/protobuf/io/zero_copy_stream_impl_lite.h"
#include "google/protobuf/text_format.h" #include "google/protobuf/text_format.h"
@ -94,9 +92,7 @@
#include "google/protobuf/io/io_win32.h" #include "google/protobuf/io/io_win32.h"
#endif #endif
#if defined(_WIN32) || defined(__CYGWIN__) #include "google/protobuf/stubs/platform_macros.h"
#include "absl/strings/ascii.h"
#endif
// Must be included last. // Must be included last.
#include "google/protobuf/port_def.inc" #include "google/protobuf/port_def.inc"
@ -3168,10 +3164,10 @@ void GatherOccupiedFieldRanges(
// Utility function for PrintFreeFieldNumbers. // Utility function for PrintFreeFieldNumbers.
// Actually prints the formatted free field numbers for given message name and // Actually prints the formatted free field numbers for given message name and
// occupied ranges. // occupied ranges.
void FormatFreeFieldNumbers(const std::string& name, void FormatFreeFieldNumbers(absl::string_view name,
const absl::btree_set<FieldRange>& ranges) { const absl::btree_set<FieldRange>& ranges) {
std::string output; std::string output;
absl::StrAppendFormat(&output, "%-35s free:", name.c_str()); absl::StrAppendFormat(&output, "%-35s free:", name);
int next_free_number = 1; int next_free_number = 1;
for (const auto& range : ranges) { for (const auto& range : ranges) {
// This happens when groups re-use parent field numbers, in which // This happens when groups re-use parent field numbers, in which

@ -41,7 +41,7 @@ absl::flat_hash_map<absl::string_view, std::string> EnumVars(
const EnumValueDescriptor* min, const EnumValueDescriptor* max) { const EnumValueDescriptor* min, const EnumValueDescriptor* max) {
auto classname = ClassName(enum_, false); auto classname = ClassName(enum_, false);
return { return {
{"Enum", enum_->name()}, {"Enum", std::string(enum_->name())},
{"Enum_", ResolveKeyword(enum_->name())}, {"Enum_", ResolveKeyword(enum_->name())},
{"Msg_Enum", classname}, {"Msg_Enum", classname},
{"::Msg_Enum", QualifiedClassName(enum_, options)}, {"::Msg_Enum", QualifiedClassName(enum_, options)},

@ -63,8 +63,7 @@ ExtensionGenerator::ExtensionGenerator(const FieldDescriptor* descriptor,
variables_["extendee"] = variables_["extendee"] =
QualifiedClassName(descriptor_->containing_type(), options_); QualifiedClassName(descriptor_->containing_type(), options_);
variables_["type_traits"] = type_traits_; variables_["type_traits"] = type_traits_;
std::string name = descriptor_->name(); variables_["name"] = ResolveKeyword(descriptor_->name());
variables_["name"] = ResolveKeyword(name);
variables_["constant_name"] = FieldConstantName(descriptor_); variables_["constant_name"] = FieldConstantName(descriptor_);
variables_["field_type"] = variables_["field_type"] =
absl::StrCat(static_cast<int>(descriptor_->type())); absl::StrCat(static_cast<int>(descriptor_->type()));

@ -59,7 +59,7 @@ using ::google::protobuf::internal::cpp::IsLazilyInitializedFile;
absl::flat_hash_map<absl::string_view, std::string> FileVars( absl::flat_hash_map<absl::string_view, std::string> FileVars(
const FileDescriptor* file, const Options& options) { const FileDescriptor* file, const Options& options) {
return { return {
{"filename", file->name()}, {"filename", std::string(file->name())},
{"package_ns", Namespace(file, options)}, {"package_ns", Namespace(file, options)},
{"tablename", UniqueName("TableStruct", file, options)}, {"tablename", UniqueName("TableStruct", file, options)},
{"desc_table", DescriptorTableName(file, options)}, {"desc_table", DescriptorTableName(file, options)},
@ -1833,3 +1833,5 @@ std::vector<const Descriptor*> FileGenerator::MessagesInTopologicalOrder()
} // namespace compiler } // namespace compiler
} // namespace protobuf } // namespace protobuf
} // namespace google } // namespace google
#include "google/protobuf/port_undef.inc"

@ -529,7 +529,7 @@ std::string SuperClassName(const Descriptor* descriptor,
} }
std::string FieldName(const FieldDescriptor* field) { std::string FieldName(const FieldDescriptor* field) {
std::string result = field->name(); std::string result = std::string(field->name());
absl::AsciiStrToLower(&result); absl::AsciiStrToLower(&result);
if (Keywords().count(result) > 0) { if (Keywords().count(result) > 0) {
result.append("_"); result.append("_");
@ -563,7 +563,7 @@ std::string QualifiedOneofCaseConstantName(const FieldDescriptor* field) {
} }
std::string EnumValueName(const EnumValueDescriptor* enum_value) { std::string EnumValueName(const EnumValueDescriptor* enum_value) {
std::string result = enum_value->name(); std::string result = std::string(enum_value->name());
if (Keywords().count(result) > 0) { if (Keywords().count(result) > 0) {
result.append("_"); result.append("_");
} }
@ -902,7 +902,7 @@ std::string SafeFunctionName(const Descriptor* descriptor,
const FieldDescriptor* field, const FieldDescriptor* field,
absl::string_view prefix) { absl::string_view prefix) {
// Do not use FieldName() since it will escape keywords. // Do not use FieldName() since it will escape keywords.
std::string name = field->name(); std::string name = std::string(field->name());
absl::AsciiStrToLower(&name); absl::AsciiStrToLower(&name);
std::string function_name = absl::StrCat(prefix, name); std::string function_name = absl::StrCat(prefix, name);
if (descriptor->FindFieldByName(function_name)) { if (descriptor->FindFieldByName(function_name)) {

@ -583,7 +583,7 @@ inline std::string IncludeGuard(const FileDescriptor* file,
extension = ".proto.static_reflection.h"; extension = ".proto.static_reflection.h";
} }
std::string filename_identifier = std::string filename_identifier =
FilenameIdentifier(file->name() + extension); FilenameIdentifier(absl::StrCat(file->name(), extension));
if (IsWellKnownMessage(file)) { if (IsWellKnownMessage(file)) {
// For well-known messages we need third_party/protobuf and net/proto2 to // For well-known messages we need third_party/protobuf and net/proto2 to
@ -1022,7 +1022,7 @@ class PROTOC_EXPORT Formatter {
template <typename T> template <typename T>
std::string FieldComment(const T* field, const Options& options) { std::string FieldComment(const T* field, const Options& options) {
if (options.strip_nonfunctional_codegen) { if (options.strip_nonfunctional_codegen) {
return field->name(); return std::string(field->name());
} }
// Print the field's (or oneof's) proto-syntax definition as a comment. // Print the field's (or oneof's) proto-syntax definition as a comment.
// We don't want to print group bodies so we cut off after the first // We don't want to print group bodies so we cut off after the first

@ -40,8 +40,8 @@
#include "google/protobuf/compiler/cpp/helpers.h" #include "google/protobuf/compiler/cpp/helpers.h"
#include "google/protobuf/unittest_no_generic_services.pb.h" #include "google/protobuf/unittest_no_generic_services.pb.h"
#include "google/protobuf/descriptor.pb.h" #include "google/protobuf/descriptor.pb.h"
#include "google/protobuf/testing/googletest.h"
#include <gtest/gtest.h> #include <gtest/gtest.h>
#include <gmock/gmock.h>
#include "absl/base/casts.h" #include "absl/base/casts.h"
#include "absl/strings/substitute.h" #include "absl/strings/substitute.h"
#include "google/protobuf/arena.h" #include "google/protobuf/arena.h"
@ -66,6 +66,8 @@ namespace cpp {
// Can't use an anonymous namespace here due to brokenness of Tru64 compiler. // Can't use an anonymous namespace here due to brokenness of Tru64 compiler.
namespace cpp_unittest { namespace cpp_unittest {
using ::testing::UnorderedElementsAre;
void DoNothing() {} void DoNothing() {}
@ -2080,19 +2082,18 @@ TEST(HELPERS_TEST_NAME, TestSCC) {
UNITTEST::TestMutualRecursionA a; UNITTEST::TestMutualRecursionA a;
MessageSCCAnalyzer scc_analyzer((Options())); MessageSCCAnalyzer scc_analyzer((Options()));
const SCC* scc = scc_analyzer.GetSCC(a.GetDescriptor()); const SCC* scc = scc_analyzer.GetSCC(a.GetDescriptor());
std::vector<std::string> names; std::vector<absl::string_view> names;
names.reserve(scc->descriptors.size()); names.reserve(scc->descriptors.size());
for (int i = 0; i < scc->descriptors.size(); i++) { for (int i = 0; i < scc->descriptors.size(); i++) {
names.push_back(scc->descriptors[i]->full_name()); names.push_back(scc->descriptors[i]->full_name());
} }
std::string package = a.GetDescriptor()->file()->package(); const absl::string_view package = a.GetDescriptor()->file()->package();
ASSERT_EQ(names.size(), 4); EXPECT_THAT(names,
std::sort(names.begin(), names.end()); UnorderedElementsAre(
EXPECT_EQ(names[0], absl::StrCat(package, ".TestMutualRecursionA")); absl::StrCat(package, ".TestMutualRecursionA"),
EXPECT_EQ(names[1], absl::StrCat(package, ".TestMutualRecursionA.SubGroup")); absl::StrCat(package, ".TestMutualRecursionA.SubGroup"),
EXPECT_EQ(names[2], absl::StrCat(package, ".TestMutualRecursionA.SubMessage"),
absl::StrCat(package, ".TestMutualRecursionA.SubMessage")); absl::StrCat(package, ".TestMutualRecursionB")));
EXPECT_EQ(names[3], absl::StrCat(package, ".TestMutualRecursionB"));
MessageAnalysis result = scc_analyzer.GetSCCAnalysis(scc); MessageAnalysis result = scc_analyzer.GetSCCAnalysis(scc);
EXPECT_EQ(result.is_recursive, true); EXPECT_EQ(result.is_recursive, true);

@ -50,7 +50,7 @@ void EnumGenerator::Generate(io::Printer* printer) {
if (descriptor_->value(i)->options().deprecated()) { if (descriptor_->value(i)->options().deprecated()) {
printer->Print("[global::System.ObsoleteAttribute]\n"); printer->Print("[global::System.ObsoleteAttribute]\n");
} }
std::string original_name = descriptor_->value(i)->name(); const absl::string_view original_name = descriptor_->value(i)->name();
std::string name = std::string name =
GetEnumValueName(descriptor_->name(), descriptor_->value(i)->name()); GetEnumValueName(descriptor_->name(), descriptor_->value(i)->name());
// Make sure we don't get any duplicate names due to prefix removal. // Make sure we don't get any duplicate names due to prefix removal.

@ -208,9 +208,9 @@ std::string GetFullExtensionName(const FieldDescriptor* descriptor) {
// editions are like groups, but have a real name, so we use that. // editions are like groups, but have a real name, so we use that.
std::string GetFieldName(const FieldDescriptor* descriptor) { std::string GetFieldName(const FieldDescriptor* descriptor) {
if (internal::cpp::IsGroupLike(*descriptor)) { if (internal::cpp::IsGroupLike(*descriptor)) {
return descriptor->message_type()->name(); return std::string(descriptor->message_type()->name());
} else { } else {
return descriptor->name(); return std::string(descriptor->name());
} }
} }

@ -98,7 +98,7 @@ inline bool IsDescriptorOptionMessage(const Descriptor* descriptor) {
if (!IsDescriptorProto(descriptor->file())) { if (!IsDescriptorProto(descriptor->file())) {
return false; return false;
} }
const std::string name = descriptor->name(); const absl::string_view name = descriptor->name();
return name == "FileOptions" || return name == "FileOptions" ||
name == "MessageOptions" || name == "MessageOptions" ||
name == "FieldOptions" || name == "FieldOptions" ||

@ -9,6 +9,7 @@
#include <algorithm> #include <algorithm>
#include <sstream> #include <sstream>
#include <string>
#include "google/protobuf/compiler/code_generator.h" #include "google/protobuf/compiler/code_generator.h"
#include "absl/container/flat_hash_map.h" #include "absl/container/flat_hash_map.h"
@ -65,7 +66,9 @@ MessageGenerator::MessageGenerator(const Descriptor* descriptor,
MessageGenerator::~MessageGenerator() {} MessageGenerator::~MessageGenerator() {}
std::string MessageGenerator::class_name() { return descriptor_->name(); } std::string MessageGenerator::class_name() {
return std::string(descriptor_->name());
}
std::string MessageGenerator::full_class_name() { std::string MessageGenerator::full_class_name() {
return GetClassName(descriptor_); return GetClassName(descriptor_);

@ -38,9 +38,9 @@ absl::string_view StripDotProto(absl::string_view proto_file) {
// Returns the Pascal-cased last part of the proto file. For example, // Returns the Pascal-cased last part of the proto file. For example,
// input of "google/protobuf/foo_bar.proto" would result in "FooBar". // input of "google/protobuf/foo_bar.proto" would result in "FooBar".
std::string GetFileNameBase(const FileDescriptor* descriptor) { std::string GetFileNameBase(const FileDescriptor* descriptor) {
std::string proto_file = descriptor->name(); const absl::string_view proto_file = descriptor->name();
int lastslash = proto_file.find_last_of('/'); int lastslash = proto_file.find_last_of('/');
std::string base = proto_file.substr(lastslash + 1); const absl::string_view base = proto_file.substr(lastslash + 1);
return UnderscoresToPascalCase(StripDotProto(base)); return UnderscoresToPascalCase(StripDotProto(base));
} }

@ -19,6 +19,7 @@
#include <vector> #include <vector>
#include "absl/strings/str_split.h" #include "absl/strings/str_split.h"
#include "absl/strings/string_view.h"
#include "google/protobuf/compiler/java/options.h" #include "google/protobuf/compiler/java/options.h"
#include "google/protobuf/descriptor.h" #include "google/protobuf/descriptor.h"
#include "google/protobuf/descriptor.pb.h" #include "google/protobuf/descriptor.pb.h"
@ -29,7 +30,7 @@ namespace protobuf {
namespace compiler { namespace compiler {
namespace java { namespace java {
std::string EscapeJavadoc(const std::string& input) { std::string EscapeJavadoc(absl::string_view input) {
std::string result; std::string result;
result.reserve(input.size() * 2); result.reserve(input.size() * 2);
@ -87,7 +88,7 @@ std::string EscapeJavadoc(const std::string& input) {
return result; return result;
} }
static std::string EscapeKdoc(const std::string& input) { static std::string EscapeKdoc(absl::string_view input) {
std::string result; std::string result;
result.reserve(input.size() * 2); result.reserve(input.size() * 2);
@ -142,7 +143,7 @@ static void WriteDocCommentBodyForLocation(io::Printer* printer,
comments = EscapeJavadoc(comments); comments = EscapeJavadoc(comments);
} }
std::vector<std::string> lines = absl::StrSplit(comments, "\n"); std::vector<std::string> lines = absl::StrSplit(comments, '\n');
while (!lines.empty() && lines.back().empty()) { while (!lines.empty() && lines.back().empty()) {
lines.pop_back(); lines.pop_back();
} }

@ -12,6 +12,7 @@
#ifndef GOOGLE_PROTOBUF_COMPILER_JAVA_DOC_COMMENT_H__ #ifndef GOOGLE_PROTOBUF_COMPILER_JAVA_DOC_COMMENT_H__
#define GOOGLE_PROTOBUF_COMPILER_JAVA_DOC_COMMENT_H__ #define GOOGLE_PROTOBUF_COMPILER_JAVA_DOC_COMMENT_H__
#include "absl/strings/string_view.h"
#include "google/protobuf/compiler/java/options.h" #include "google/protobuf/compiler/java/options.h"
#include "google/protobuf/descriptor.h" #include "google/protobuf/descriptor.h"
@ -71,7 +72,7 @@ void WriteMethodDocComment(io::Printer* printer, const MethodDescriptor* method,
// Exposed for testing only. // Exposed for testing only.
// Also called by proto1-Java code generator. // Also called by proto1-Java code generator.
PROTOC_EXPORT std::string EscapeJavadoc(const std::string& input); PROTOC_EXPORT std::string EscapeJavadoc(absl::string_view input);
} // namespace java } // namespace java
} // namespace compiler } // namespace compiler

@ -108,7 +108,7 @@ std::string ExtraMessageOrBuilderInterfaces(const Descriptor* descriptor);
// Get the unqualified Java class name for mutable messages. i.e. without // Get the unqualified Java class name for mutable messages. i.e. without
// package or outer classnames. // package or outer classnames.
inline std::string ShortMutableJavaClassName(const Descriptor* descriptor) { inline std::string ShortMutableJavaClassName(const Descriptor* descriptor) {
return descriptor->name(); return std::string(descriptor->name());
} }
// Whether the given descriptor is for one of the core descriptor protos. We // Whether the given descriptor is for one of the core descriptor protos. We

@ -95,7 +95,7 @@ void ImmutableMessageLiteGenerator::GenerateInterface(io::Printer* printer) {
{"deprecation", {"deprecation",
descriptor_->options().deprecated() ? "@java.lang.Deprecated " : ""}, descriptor_->options().deprecated() ? "@java.lang.Deprecated " : ""},
{"extra_interfaces", ExtraMessageOrBuilderInterfaces(descriptor_)}, {"extra_interfaces", ExtraMessageOrBuilderInterfaces(descriptor_)},
{"classname", descriptor_->name()}, {"classname", std::string(descriptor_->name())},
}; };
if (!context_->options().opensource_runtime) { if (!context_->options().opensource_runtime) {

@ -55,7 +55,7 @@ std::string ClassNameWithoutPackage(const Descriptor* descriptor,
} }
std::string ClassNameWithoutPackageKotlin(const Descriptor* descriptor) { std::string ClassNameWithoutPackageKotlin(const Descriptor* descriptor) {
std::string result = descriptor->name(); std::string result = std::string(descriptor->name());
const Descriptor* temp = descriptor->containing_type(); const Descriptor* temp = descriptor->containing_type();
while (temp) { while (temp) {
@ -71,7 +71,7 @@ std::string ClassNameWithoutPackage(const EnumDescriptor* descriptor,
// Doesn't append "Mutable" for enum type's name. // Doesn't append "Mutable" for enum type's name.
const Descriptor* message_descriptor = descriptor->containing_type(); const Descriptor* message_descriptor = descriptor->containing_type();
if (message_descriptor == nullptr) { if (message_descriptor == nullptr) {
return descriptor->name(); return std::string(descriptor->name());
} else { } else {
return absl::StrCat(ClassNameWithoutPackage(message_descriptor, immutable), return absl::StrCat(ClassNameWithoutPackage(message_descriptor, immutable),
".", descriptor->name()); ".", descriptor->name());

@ -10,6 +10,7 @@
#include "google/protobuf/compiler/java/shared_code_generator.h" #include "google/protobuf/compiler/java/shared_code_generator.h"
#include <memory> #include <memory>
#include <utility>
#include "absl/strings/escaping.h" #include "absl/strings/escaping.h"
#include "absl/strings/str_cat.h" #include "absl/strings/str_cat.h"
@ -17,6 +18,7 @@
#include "google/protobuf/compiler/java/helpers.h" #include "google/protobuf/compiler/java/helpers.h"
#include "google/protobuf/compiler/java/name_resolver.h" #include "google/protobuf/compiler/java/name_resolver.h"
#include "google/protobuf/compiler/java/names.h" #include "google/protobuf/compiler/java/names.h"
#include "google/protobuf/compiler/java/options.h"
#include "google/protobuf/compiler/retention.h" #include "google/protobuf/compiler/retention.h"
#include "google/protobuf/compiler/versions.h" #include "google/protobuf/compiler/versions.h"
#include "google/protobuf/descriptor.h" #include "google/protobuf/descriptor.h"
@ -166,7 +168,7 @@ void SharedCodeGenerator::GenerateDescriptors(io::Printer* printer) {
// Find out all dependencies. // Find out all dependencies.
std::vector<std::pair<std::string, std::string> > dependencies; std::vector<std::pair<std::string, std::string> > dependencies;
for (int i = 0; i < file_->dependency_count(); i++) { for (int i = 0; i < file_->dependency_count(); i++) {
std::string filename = file_->dependency(i)->name(); const absl::string_view filename = file_->dependency(i)->name();
std::string package = FileJavaPackage(file_->dependency(i), true, options_); std::string package = FileJavaPackage(file_->dependency(i), true, options_);
std::string classname = std::string classname =
name_resolver_->GetDescriptorClassName(file_->dependency(i)); name_resolver_->GetDescriptorClassName(file_->dependency(i));
@ -176,7 +178,7 @@ void SharedCodeGenerator::GenerateDescriptors(io::Printer* printer) {
} else { } else {
full_name = absl::StrCat(package, ".", classname); full_name = absl::StrCat(package, ".", classname);
} }
dependencies.push_back(std::make_pair(filename, full_name)); dependencies.push_back(std::make_pair(std::string(filename), full_name));
} }
// ----------------------------------------------------------------- // -----------------------------------------------------------------

@ -170,7 +170,7 @@ void EnumGenerator::GenerateSource(io::Printer* printer) const {
text_blob += short_name + '\0'; text_blob += short_name + '\0';
if (UnCamelCaseEnumShortName(short_name) != v->name()) { if (UnCamelCaseEnumShortName(short_name) != v->name()) {
text_format_decode_data.AddString(enum_value_description_key, short_name, text_format_decode_data.AddString(enum_value_description_key, short_name,
v->name()); std::string(v->name()));
} }
} }

@ -137,8 +137,7 @@ bool HasNonZeroDefaultValue(const FieldDescriptor* field) {
case FieldDescriptor::CPPTYPE_BOOL: case FieldDescriptor::CPPTYPE_BOOL:
return field->default_value_bool(); return field->default_value_bool();
case FieldDescriptor::CPPTYPE_STRING: { case FieldDescriptor::CPPTYPE_STRING: {
const std::string& default_string = field->default_value_string(); return !field->default_value_string().empty();
return !default_string.empty();
} }
case FieldDescriptor::CPPTYPE_ENUM: case FieldDescriptor::CPPTYPE_ENUM:
// The default value for an enum field is the first enum value, so there // The default value for an enum field is the first enum value, so there

@ -530,7 +530,7 @@ void FileGenerator::GenerateFile(io::Printer* p, GeneratedFileType file_type,
// #import the headers for anything that a plain dependency of this // #import the headers for anything that a plain dependency of this
// proto file (that means they were just an include, not a "public" // proto file (that means they were just an include, not a "public"
// include). // include).
absl::flat_hash_set<std::string> public_import_names; absl::flat_hash_set<absl::string_view> public_import_names;
for (int i = 0; i < file_->public_dependency_count(); i++) { for (int i = 0; i < file_->public_dependency_count(); i++) {
public_import_names.insert(file_->public_dependency(i)->name()); public_import_names.insert(file_->public_dependency(i)->name());
} }

@ -405,7 +405,7 @@ bool HasWKTWithObjCCategory(const FileDescriptor* file) {
// We don't check the name prefix or proto package because some files // We don't check the name prefix or proto package because some files
// (descriptor.proto), aren't shipped generated by the library, so this // (descriptor.proto), aren't shipped generated by the library, so this
// seems to be the safest way to only catch the ones shipped. // seems to be the safest way to only catch the ones shipped.
const std::string name = file->name(); const absl::string_view name = file->name();
if (name == "google/protobuf/any.proto" || if (name == "google/protobuf/any.proto" ||
name == "google/protobuf/duration.proto" || name == "google/protobuf/duration.proto" ||
name == "google/protobuf/timestamp.proto") { name == "google/protobuf/timestamp.proto") {
@ -419,7 +419,7 @@ bool IsWKTWithObjCCategory(const Descriptor* descriptor) {
if (!HasWKTWithObjCCategory(descriptor->file())) { if (!HasWKTWithObjCCategory(descriptor->file())) {
return false; return false;
} }
const std::string full_name = descriptor->full_name(); const absl::string_view full_name = descriptor->full_name();
if (full_name == "google.protobuf.Any" || if (full_name == "google.protobuf.Any" ||
full_name == "google.protobuf.Duration" || full_name == "google.protobuf.Duration" ||
full_name == "google.protobuf.Timestamp") { full_name == "google.protobuf.Timestamp") {

@ -163,11 +163,12 @@ absl::string_view PrefixModeStorage::prefix_from_proto_package_mappings(
} }
} }
const std::string package = file->package(); const absl::string_view package = file->package();
// For files without packages, the can be registered as "no_package:PATH", // For files without packages, the can be registered as "no_package:PATH",
// allowing the expected prefixes file. // allowing the expected prefixes file.
const std::string lookup_key = const std::string lookup_key =
package.empty() ? absl::StrCat(kNoPackagePrefix, file->name()) : package; package.empty() ? absl::StrCat(kNoPackagePrefix, file->name())
: std::string(package);
auto prefix_lookup = package_to_prefix_map_.find(lookup_key); auto prefix_lookup = package_to_prefix_map_.find(lookup_key);
@ -574,9 +575,9 @@ std::string SanitizeNameForObjC(absl::string_view prefix,
std::string NameFromFieldDescriptor(const FieldDescriptor* field) { std::string NameFromFieldDescriptor(const FieldDescriptor* field) {
if (internal::cpp::IsGroupLike(*field)) { if (internal::cpp::IsGroupLike(*field)) {
return field->message_type()->name(); return std::string(field->message_type()->name());
} else { } else {
return field->name(); return std::string(field->name());
} }
} }
@ -1179,7 +1180,7 @@ bool IsProtobufLibraryBundledProtoFile(const FileDescriptor* file) {
// We don't check the name prefix or proto package because some files // We don't check the name prefix or proto package because some files
// (descriptor.proto), aren't shipped generated by the library, so this // (descriptor.proto), aren't shipped generated by the library, so this
// seems to be the safest way to only catch the ones shipped. // seems to be the safest way to only catch the ones shipped.
const std::string name = file->name(); const absl::string_view name = file->name();
if (name == "google/protobuf/any.proto" || if (name == "google/protobuf/any.proto" ||
name == "google/protobuf/api.proto" || name == "google/protobuf/api.proto" ||
name == "google/protobuf/duration.proto" || name == "google/protobuf/duration.proto" ||
@ -1241,12 +1242,13 @@ bool ValidateObjCClassPrefix(
bool has_prefix = file->options().has_objc_class_prefix(); bool has_prefix = file->options().has_objc_class_prefix();
bool have_expected_prefix_file = !expected_prefixes_path.empty(); bool have_expected_prefix_file = !expected_prefixes_path.empty();
const std::string prefix = file->options().objc_class_prefix(); const absl::string_view prefix = file->options().objc_class_prefix();
const std::string package = file->package(); const absl::string_view package = file->package();
// For files without packages, the can be registered as "no_package:PATH", // For files without packages, the can be registered as "no_package:PATH",
// allowing the expected prefixes file. // allowing the expected prefixes file.
const std::string lookup_key = const std::string lookup_key =
package.empty() ? absl::StrCat(kNoPackagePrefix, file->name()) : package; package.empty() ? absl::StrCat(kNoPackagePrefix, file->name())
: std::string(package);
// NOTE: src/google/protobuf/compiler/plugin.cc makes use of cerr for some // NOTE: src/google/protobuf/compiler/plugin.cc makes use of cerr for some
// error cases, so it seems to be ok to use as a back door for warnings. // error cases, so it seems to be ok to use as a back door for warnings.

@ -49,7 +49,7 @@ bool IsReservedName(absl::string_view name) {
return false; return false;
} }
std::string ReservedNamePrefix(const std::string& classname, std::string ReservedNamePrefix(const absl::string_view classname,
const FileDescriptor* file) { const FileDescriptor* file) {
if (IsReservedName(classname)) { if (IsReservedName(classname)) {
if (file->package() == "google.protobuf") { if (file->package() == "google.protobuf") {
@ -65,7 +65,7 @@ std::string ReservedNamePrefix(const std::string& classname,
namespace { namespace {
template <typename DescriptorType> template <typename DescriptorType>
std::string ClassNamePrefixImpl(const std::string& classname, std::string ClassNamePrefixImpl(const absl::string_view classname,
const DescriptorType* desc) { const DescriptorType* desc) {
const std::string& prefix = (desc->file()->options()).php_class_prefix(); const std::string& prefix = (desc->file()->options()).php_class_prefix();
if (!prefix.empty()) { if (!prefix.empty()) {
@ -77,19 +77,20 @@ std::string ClassNamePrefixImpl(const std::string& classname,
template <typename DescriptorType> template <typename DescriptorType>
std::string GeneratedClassNameImpl(const DescriptorType* desc) { std::string GeneratedClassNameImpl(const DescriptorType* desc) {
std::string classname = ClassNamePrefixImpl(desc->name(), desc) + desc->name(); std::string classname =
absl::StrCat(ClassNamePrefixImpl(desc->name(), desc), desc->name());
const Descriptor* containing = desc->containing_type(); const Descriptor* containing = desc->containing_type();
while (containing != nullptr) { while (containing != nullptr) {
classname = ClassNamePrefixImpl(containing->name(), desc) + containing->name() classname = absl::StrCat(ClassNamePrefixImpl(containing->name(), desc),
+ '\\' + classname; containing->name(), "\\", classname);
containing = containing->containing_type(); containing = containing->containing_type();
} }
return classname; return classname;
} }
std::string GeneratedClassNameImpl(const ServiceDescriptor* desc) { std::string GeneratedClassNameImpl(const ServiceDescriptor* desc) {
std::string classname = desc->name(); const absl::string_view classname = desc->name();
return ClassNamePrefixImpl(classname, desc) + classname; return absl::StrCat(ClassNamePrefixImpl(classname, desc), classname);
} }
} // namespace } // namespace

@ -24,13 +24,13 @@ namespace php {
PROTOC_EXPORT bool IsReservedName(absl::string_view name); PROTOC_EXPORT bool IsReservedName(absl::string_view name);
// A prefix to stick in front of reserved names to avoid clashes. // A prefix to stick in front of reserved names to avoid clashes.
PROTOC_EXPORT std::string ReservedNamePrefix(const std::string& classname, PROTOC_EXPORT std::string ReservedNamePrefix(absl::string_view classname,
const FileDescriptor* file); const FileDescriptor* file);
// A prefix to stick in front of all class names. // A prefix to stick in front of all class names.
PROTOC_EXPORT std::string ClassNamePrefix(const std::string& classname, PROTOC_EXPORT std::string ClassNamePrefix(absl::string_view classname,
const Descriptor* desc); const Descriptor* desc);
PROTOC_EXPORT std::string ClassNamePrefix(const std::string& classname, PROTOC_EXPORT std::string ClassNamePrefix(absl::string_view classname,
const EnumDescriptor* desc); const EnumDescriptor* desc);
// To skip reserved keywords in php, some generated classname are prefixed. // To skip reserved keywords in php, some generated classname are prefixed.

@ -752,9 +752,10 @@ void GenerateEnumToPool(const EnumDescriptor* en, io::Printer* printer) {
for (int i = 0; i < en->value_count(); i++) { for (int i = 0; i < en->value_count(); i++) {
const EnumValueDescriptor* value = en->value(i); const EnumValueDescriptor* value = en->value(i);
printer->Print("->value(\"^name^\", ^number^)\n", "name", printer->Print(
ConstantNamePrefix(value->name()) + value->name(), "number", "->value(\"^name^\", ^number^)\n", "name",
IntToString(value->number())); absl::StrCat(ConstantNamePrefix(value->name()), value->name()),
"number", IntToString(value->number()));
} }
printer->Print("->finalizeToPool();\n\n"); printer->Print("->finalizeToPool();\n\n");
Outdent(printer); Outdent(printer);
@ -1172,16 +1173,18 @@ bool GenerateEnumFile(const FileDescriptor* file, const EnumDescriptor* en,
hasReserved = true; hasReserved = true;
} }
printer.Print("const ^name^ = ^number^;\n", "name", prefix + value->name(), printer.Print("const ^name^ = ^number^;\n", "name",
"number", IntToString(value->number())); absl::StrCat(prefix, value->name()), "number",
IntToString(value->number()));
} }
printer.Print("\nprivate static $valueToName = [\n"); printer.Print("\nprivate static $valueToName = [\n");
Indent(&printer); Indent(&printer);
for (int i = 0; i < en->value_count(); i++) { for (int i = 0; i < en->value_count(); i++) {
const EnumValueDescriptor* value = en->value(i); const EnumValueDescriptor* value = en->value(i);
printer.Print("self::^constant^ => '^name^',\n", "constant", printer.Print(
ConstantNamePrefix(value->name()) + value->name(), "name", "self::^constant^ => '^name^',\n", "constant",
absl::StrCat(ConstantNamePrefix(value->name()), value->name()), "name",
value->name()); value->name());
} }
Outdent(&printer); Outdent(&printer);

@ -1433,7 +1433,7 @@ void Generator::FixOptionsForEnum(const EnumDescriptor& enum_descriptor,
PrintDescriptorOptionsFixingCode( PrintDescriptorOptionsFixingCode(
value_descriptor, proto.value(i), value_descriptor, proto.value(i),
absl::StrFormat("%s.values_by_name[\"%s\"]", descriptor_name.c_str(), absl::StrFormat("%s.values_by_name[\"%s\"]", descriptor_name.c_str(),
value_descriptor.name().c_str())); value_descriptor.name()));
} }
} }

@ -94,7 +94,7 @@ std::string GeneratedCodeToBase64(const GeneratedCodeInfo& annotations) {
template <typename DescriptorT> template <typename DescriptorT>
std::string NamePrefixedWithNestedTypes(const DescriptorT& descriptor, std::string NamePrefixedWithNestedTypes(const DescriptorT& descriptor,
absl::string_view separator) { absl::string_view separator) {
std::string name = descriptor.name(); std::string name = std::string(descriptor.name());
const Descriptor* parent = descriptor.containing_type(); const Descriptor* parent = descriptor.containing_type();
if (parent != nullptr) { if (parent != nullptr) {
std::string prefix = NamePrefixedWithNestedTypes(*parent, separator); std::string prefix = NamePrefixedWithNestedTypes(*parent, separator);

@ -39,7 +39,7 @@ std::string PyiGenerator::ModuleLevelName(const DescriptorT& descriptor) const {
std::string name = NamePrefixedWithNestedTypes(descriptor, "."); std::string name = NamePrefixedWithNestedTypes(descriptor, ".");
if (descriptor.file() != file_) { if (descriptor.file() != file_) {
std::string module_alias; std::string module_alias;
std::string filename = descriptor.file()->name(); const absl::string_view filename = descriptor.file()->name();
if (import_map_.find(filename) == import_map_.end()) { if (import_map_.find(filename) == import_map_.end()) {
std::string module_name = ModuleName(descriptor.file()->name()); std::string module_name = ModuleName(descriptor.file()->name());
std::vector<absl::string_view> tokens = absl::StrSplit(module_name, '.'); std::vector<absl::string_view> tokens = absl::StrSplit(module_name, '.');
@ -71,7 +71,7 @@ struct ImportModules {
}; };
// Checks whether a descriptor name matches a well-known type. // Checks whether a descriptor name matches a well-known type.
bool IsWellKnownType(const std::string& name) { bool IsWellKnownType(const absl::string_view name) {
// LINT.IfChange(wktbases) // LINT.IfChange(wktbases)
return (name == "google.protobuf.Any" || return (name == "google.protobuf.Any" ||
name == "google.protobuf.Duration" || name == "google.protobuf.Duration" ||
@ -132,7 +132,7 @@ void CheckImportModules(const Descriptor* descriptor,
void PyiGenerator::PrintImportForDescriptor( void PyiGenerator::PrintImportForDescriptor(
const FileDescriptor& desc, absl::flat_hash_set<std::string>* seen_aliases, const FileDescriptor& desc, absl::flat_hash_set<std::string>* seen_aliases,
bool* has_importlib) const { bool* has_importlib) const {
const std::string& filename = desc.name(); const absl::string_view filename = desc.name();
std::string module_name_owned = StrippedModuleName(filename); std::string module_name_owned = StrippedModuleName(filename);
absl::string_view module_name(module_name_owned); absl::string_view module_name(module_name_owned);
size_t last_dot_pos = module_name.rfind('.'); size_t last_dot_pos = module_name.rfind('.');
@ -288,7 +288,7 @@ printer_->Annotate(label.c_str(), descriptor);
} }
void PyiGenerator::PrintEnum(const EnumDescriptor& enum_descriptor) const { void PyiGenerator::PrintEnum(const EnumDescriptor& enum_descriptor) const {
std::string enum_name = enum_descriptor.name(); const absl::string_view enum_name = enum_descriptor.name();
printer_->Print( printer_->Print(
"class $enum_name$(int, metaclass=_enum_type_wrapper.EnumTypeWrapper):\n" "class $enum_name$(int, metaclass=_enum_type_wrapper.EnumTypeWrapper):\n"
" __slots__ = ()\n", " __slots__ = ()\n",
@ -385,7 +385,7 @@ void PyiGenerator::PrintMessage(
if (!is_nested) { if (!is_nested) {
printer_->Print("\n"); printer_->Print("\n");
} }
std::string class_name = message_descriptor.name(); const absl::string_view class_name = message_descriptor.name();
std::string extra_base; std::string extra_base;
// A well-known type needs to inherit from its corresponding base class in // A well-known type needs to inherit from its corresponding base class in
// net/proto2/python/internal/well_known_types. // net/proto2/python/internal/well_known_types.
@ -484,7 +484,7 @@ void PyiGenerator::PrintMessage(
has_key_words = true; has_key_words = true;
continue; continue;
} }
std::string field_name = field_des->name(); std::string field_name = std::string(field_des->name());
if (is_first && field_name == "self") { if (is_first && field_name == "self") {
// See b/144146793 for an example of real code that generates a (self, // See b/144146793 for an example of real code that generates a (self,
// self) method signature. Since repeating a parameter name is illegal in // self) method signature. Since repeating a parameter name is illegal in

Loading…
Cancel
Save