From b8cb34d744c1eedaf3d1c23407cc827453e7a439 Mon Sep 17 00:00:00 2001 From: Protobuf Team Bot Date: Thu, 11 Jul 2024 13:40:04 -0700 Subject: [PATCH] Prepare the code for migrating return types from `const std::string&` to `absl::string_view`. PiperOrigin-RevId: 651520812 --- hpb_generator/BUILD | 1 + hpb_generator/gen_utils.cc | 3 +- hpb_generator/gen_utils.h | 3 +- hpb_generator/names.cc | 5 +- .../compiler/command_line_interface.cc | 76 +++++++++---------- src/google/protobuf/compiler/cpp/enum.cc | 2 +- src/google/protobuf/compiler/cpp/extension.cc | 3 +- src/google/protobuf/compiler/cpp/file.cc | 4 +- src/google/protobuf/compiler/cpp/helpers.cc | 6 +- src/google/protobuf/compiler/cpp/helpers.h | 4 +- src/google/protobuf/compiler/cpp/unittest.inc | 21 ++--- .../protobuf/compiler/csharp/csharp_enum.cc | 2 +- .../compiler/csharp/csharp_helpers.cc | 4 +- .../protobuf/compiler/csharp/csharp_helpers.h | 2 +- .../compiler/csharp/csharp_message.cc | 5 +- src/google/protobuf/compiler/csharp/names.cc | 8 +- .../protobuf/compiler/java/doc_comment.cc | 7 +- .../protobuf/compiler/java/doc_comment.h | 3 +- src/google/protobuf/compiler/java/helpers.h | 2 +- .../protobuf/compiler/java/lite/message.cc | 2 +- .../protobuf/compiler/java/name_resolver.cc | 4 +- .../compiler/java/shared_code_generator.cc | 6 +- .../protobuf/compiler/objectivec/enum.cc | 2 +- .../protobuf/compiler/objectivec/field.cc | 3 +- .../protobuf/compiler/objectivec/file.cc | 2 +- .../protobuf/compiler/objectivec/helpers.cc | 4 +- .../protobuf/compiler/objectivec/names.cc | 18 +++-- src/google/protobuf/compiler/php/names.cc | 15 ++-- src/google/protobuf/compiler/php/names.h | 6 +- .../protobuf/compiler/php/php_generator.cc | 19 +++-- .../protobuf/compiler/python/generator.cc | 2 +- .../protobuf/compiler/python/helpers.cc | 2 +- .../protobuf/compiler/python/pyi_generator.cc | 12 +-- 33 files changed, 136 insertions(+), 122 deletions(-) diff --git a/hpb_generator/BUILD b/hpb_generator/BUILD index 63cfe8b7a5..7ca5c590b9 100644 --- a/hpb_generator/BUILD +++ b/hpb_generator/BUILD @@ -103,5 +103,6 @@ cc_library( ":output", "//src/google/protobuf", "//upb_generator:keywords", + "@com_google_absl//absl/strings:string_view", ], ) diff --git a/hpb_generator/gen_utils.cc b/hpb_generator/gen_utils.cc index 3827425467..f89afc99e2 100644 --- a/hpb_generator/gen_utils.cc +++ b/hpb_generator/gen_utils.cc @@ -12,6 +12,7 @@ #include #include "absl/strings/ascii.h" +#include "absl/strings/string_view.h" namespace google::protobuf::hpb_generator { @@ -102,7 +103,7 @@ std::vector FieldNumberOrder( 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; std::string result; result.reserve(input.size()); diff --git a/hpb_generator/gen_utils.h b/hpb_generator/gen_utils.h index 3cf8a850a9..a83500c719 100644 --- a/hpb_generator/gen_utils.h +++ b/hpb_generator/gen_utils.h @@ -12,6 +12,7 @@ #include #include "google/protobuf/descriptor.pb.h" +#include "absl/strings/string_view.h" #include "google/protobuf/compiler/code_generator.h" #include "google/protobuf/descriptor.h" @@ -38,7 +39,7 @@ std::vector SortedExtensions( std::vector FieldNumberOrder( 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 google::hpb_generator diff --git a/hpb_generator/names.cc b/hpb_generator/names.cc index b3145f34b9..5ea3da9d6a 100644 --- a/hpb_generator/names.cc +++ b/hpb_generator/names.cc @@ -9,6 +9,7 @@ #include +#include "absl/strings/string_view.h" #include "upb_generator/keywords.h" namespace google::protobuf::hpb_generator { @@ -21,11 +22,11 @@ std::string NamespaceFromPackageName(absl::string_view package_name) { "::protos"); } -std::string DotsToColons(const std::string& name) { +std::string DotsToColons(const absl::string_view name) { return absl::StrReplaceAll(name, {{".", "::"}}); } -std::string Namespace(const std::string& package) { +std::string Namespace(const absl::string_view package) { if (package.empty()) return ""; return "::" + DotsToColons(package); } diff --git a/src/google/protobuf/compiler/command_line_interface.cc b/src/google/protobuf/compiler/command_line_interface.cc index 1ee01f50ff..cd95c8b413 100644 --- a/src/google/protobuf/compiler/command_line_interface.cc +++ b/src/google/protobuf/compiler/command_line_interface.cc @@ -11,32 +11,23 @@ #include "google/protobuf/compiler/command_line_interface.h" -#include -#include -#include - -#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 +#include #include #include #include + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include #ifdef major #undef major #endif @@ -45,20 +36,10 @@ #endif #include #include + #ifndef _MSC_VER #include #endif -#include - -#include -#include - -#include // For PATH_MAX - -#include -#include -#include -#include #if defined(__APPLE__) #include @@ -66,27 +47,44 @@ #include #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_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/str_cat.h" #include "absl/strings/str_format.h" #include "absl/strings/str_replace.h" #include "absl/strings/str_split.h" #include "absl/strings/string_view.h" #include "absl/strings/substitute.h" +#include "absl/types/span.h" #include "google/protobuf/compiler/code_generator.h" #include "google/protobuf/compiler/importer.h" #include "google/protobuf/compiler/plugin.pb.h" #include "google/protobuf/compiler/retention.h" #include "google/protobuf/compiler/subprocess.h" +#include "google/protobuf/compiler/versions.h" #include "google/protobuf/compiler/zip_writer.h" #include "google/protobuf/descriptor.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/feature_resolver.h" #include "google/protobuf/io/coded_stream.h" #include "google/protobuf/io/printer.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" @@ -94,9 +92,7 @@ #include "google/protobuf/io/io_win32.h" #endif -#if defined(_WIN32) || defined(__CYGWIN__) -#include "absl/strings/ascii.h" -#endif +#include "google/protobuf/stubs/platform_macros.h" // Must be included last. #include "google/protobuf/port_def.inc" @@ -3168,10 +3164,10 @@ void GatherOccupiedFieldRanges( // Utility function for PrintFreeFieldNumbers. // Actually prints the formatted free field numbers for given message name and // occupied ranges. -void FormatFreeFieldNumbers(const std::string& name, +void FormatFreeFieldNumbers(absl::string_view name, const absl::btree_set& ranges) { std::string output; - absl::StrAppendFormat(&output, "%-35s free:", name.c_str()); + absl::StrAppendFormat(&output, "%-35s free:", name); int next_free_number = 1; for (const auto& range : ranges) { // This happens when groups re-use parent field numbers, in which diff --git a/src/google/protobuf/compiler/cpp/enum.cc b/src/google/protobuf/compiler/cpp/enum.cc index d61a622bb2..c40a5dff12 100644 --- a/src/google/protobuf/compiler/cpp/enum.cc +++ b/src/google/protobuf/compiler/cpp/enum.cc @@ -41,7 +41,7 @@ absl::flat_hash_map EnumVars( const EnumValueDescriptor* min, const EnumValueDescriptor* max) { auto classname = ClassName(enum_, false); return { - {"Enum", enum_->name()}, + {"Enum", std::string(enum_->name())}, {"Enum_", ResolveKeyword(enum_->name())}, {"Msg_Enum", classname}, {"::Msg_Enum", QualifiedClassName(enum_, options)}, diff --git a/src/google/protobuf/compiler/cpp/extension.cc b/src/google/protobuf/compiler/cpp/extension.cc index 1d95d61a55..4dbfe5abe5 100644 --- a/src/google/protobuf/compiler/cpp/extension.cc +++ b/src/google/protobuf/compiler/cpp/extension.cc @@ -63,8 +63,7 @@ ExtensionGenerator::ExtensionGenerator(const FieldDescriptor* descriptor, variables_["extendee"] = QualifiedClassName(descriptor_->containing_type(), options_); variables_["type_traits"] = type_traits_; - std::string name = descriptor_->name(); - variables_["name"] = ResolveKeyword(name); + variables_["name"] = ResolveKeyword(descriptor_->name()); variables_["constant_name"] = FieldConstantName(descriptor_); variables_["field_type"] = absl::StrCat(static_cast(descriptor_->type())); diff --git a/src/google/protobuf/compiler/cpp/file.cc b/src/google/protobuf/compiler/cpp/file.cc index aeca55051e..246e09fa09 100644 --- a/src/google/protobuf/compiler/cpp/file.cc +++ b/src/google/protobuf/compiler/cpp/file.cc @@ -59,7 +59,7 @@ using ::google::protobuf::internal::cpp::IsLazilyInitializedFile; absl::flat_hash_map FileVars( const FileDescriptor* file, const Options& options) { return { - {"filename", file->name()}, + {"filename", std::string(file->name())}, {"package_ns", Namespace(file, options)}, {"tablename", UniqueName("TableStruct", file, options)}, {"desc_table", DescriptorTableName(file, options)}, @@ -1833,3 +1833,5 @@ std::vector FileGenerator::MessagesInTopologicalOrder() } // namespace compiler } // namespace protobuf } // namespace google + +#include "google/protobuf/port_undef.inc" diff --git a/src/google/protobuf/compiler/cpp/helpers.cc b/src/google/protobuf/compiler/cpp/helpers.cc index f221614e67..c6a6c56f45 100644 --- a/src/google/protobuf/compiler/cpp/helpers.cc +++ b/src/google/protobuf/compiler/cpp/helpers.cc @@ -529,7 +529,7 @@ std::string SuperClassName(const Descriptor* descriptor, } std::string FieldName(const FieldDescriptor* field) { - std::string result = field->name(); + std::string result = std::string(field->name()); absl::AsciiStrToLower(&result); if (Keywords().count(result) > 0) { result.append("_"); @@ -563,7 +563,7 @@ std::string QualifiedOneofCaseConstantName(const FieldDescriptor* field) { } 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) { result.append("_"); } @@ -902,7 +902,7 @@ std::string SafeFunctionName(const Descriptor* descriptor, const FieldDescriptor* field, absl::string_view prefix) { // Do not use FieldName() since it will escape keywords. - std::string name = field->name(); + std::string name = std::string(field->name()); absl::AsciiStrToLower(&name); std::string function_name = absl::StrCat(prefix, name); if (descriptor->FindFieldByName(function_name)) { diff --git a/src/google/protobuf/compiler/cpp/helpers.h b/src/google/protobuf/compiler/cpp/helpers.h index 1aadfbe3eb..95c2b6b953 100644 --- a/src/google/protobuf/compiler/cpp/helpers.h +++ b/src/google/protobuf/compiler/cpp/helpers.h @@ -583,7 +583,7 @@ inline std::string IncludeGuard(const FileDescriptor* file, extension = ".proto.static_reflection.h"; } std::string filename_identifier = - FilenameIdentifier(file->name() + extension); + FilenameIdentifier(absl::StrCat(file->name(), extension)); if (IsWellKnownMessage(file)) { // For well-known messages we need third_party/protobuf and net/proto2 to @@ -1022,7 +1022,7 @@ class PROTOC_EXPORT Formatter { template std::string FieldComment(const T* field, const Options& options) { 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. // We don't want to print group bodies so we cut off after the first diff --git a/src/google/protobuf/compiler/cpp/unittest.inc b/src/google/protobuf/compiler/cpp/unittest.inc index b08cc950c2..a6552e1e1d 100644 --- a/src/google/protobuf/compiler/cpp/unittest.inc +++ b/src/google/protobuf/compiler/cpp/unittest.inc @@ -40,8 +40,8 @@ #include "google/protobuf/compiler/cpp/helpers.h" #include "google/protobuf/unittest_no_generic_services.pb.h" #include "google/protobuf/descriptor.pb.h" -#include "google/protobuf/testing/googletest.h" #include +#include #include "absl/base/casts.h" #include "absl/strings/substitute.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. namespace cpp_unittest { +using ::testing::UnorderedElementsAre; + void DoNothing() {} @@ -2080,19 +2082,18 @@ TEST(HELPERS_TEST_NAME, TestSCC) { UNITTEST::TestMutualRecursionA a; MessageSCCAnalyzer scc_analyzer((Options())); const SCC* scc = scc_analyzer.GetSCC(a.GetDescriptor()); - std::vector names; + std::vector names; names.reserve(scc->descriptors.size()); for (int i = 0; i < scc->descriptors.size(); i++) { names.push_back(scc->descriptors[i]->full_name()); } - std::string package = a.GetDescriptor()->file()->package(); - ASSERT_EQ(names.size(), 4); - std::sort(names.begin(), names.end()); - EXPECT_EQ(names[0], absl::StrCat(package, ".TestMutualRecursionA")); - EXPECT_EQ(names[1], absl::StrCat(package, ".TestMutualRecursionA.SubGroup")); - EXPECT_EQ(names[2], - absl::StrCat(package, ".TestMutualRecursionA.SubMessage")); - EXPECT_EQ(names[3], absl::StrCat(package, ".TestMutualRecursionB")); + const absl::string_view package = a.GetDescriptor()->file()->package(); + EXPECT_THAT(names, + UnorderedElementsAre( + absl::StrCat(package, ".TestMutualRecursionA"), + absl::StrCat(package, ".TestMutualRecursionA.SubGroup"), + absl::StrCat(package, ".TestMutualRecursionA.SubMessage"), + absl::StrCat(package, ".TestMutualRecursionB"))); MessageAnalysis result = scc_analyzer.GetSCCAnalysis(scc); EXPECT_EQ(result.is_recursive, true); diff --git a/src/google/protobuf/compiler/csharp/csharp_enum.cc b/src/google/protobuf/compiler/csharp/csharp_enum.cc index 7f2626cbd5..ac03775530 100644 --- a/src/google/protobuf/compiler/csharp/csharp_enum.cc +++ b/src/google/protobuf/compiler/csharp/csharp_enum.cc @@ -50,7 +50,7 @@ void EnumGenerator::Generate(io::Printer* printer) { if (descriptor_->value(i)->options().deprecated()) { 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 = GetEnumValueName(descriptor_->name(), descriptor_->value(i)->name()); // Make sure we don't get any duplicate names due to prefix removal. diff --git a/src/google/protobuf/compiler/csharp/csharp_helpers.cc b/src/google/protobuf/compiler/csharp/csharp_helpers.cc index 7a6a4b9d32..20588650bb 100644 --- a/src/google/protobuf/compiler/csharp/csharp_helpers.cc +++ b/src/google/protobuf/compiler/csharp/csharp_helpers.cc @@ -208,9 +208,9 @@ std::string GetFullExtensionName(const FieldDescriptor* descriptor) { // editions are like groups, but have a real name, so we use that. std::string GetFieldName(const FieldDescriptor* descriptor) { if (internal::cpp::IsGroupLike(*descriptor)) { - return descriptor->message_type()->name(); + return std::string(descriptor->message_type()->name()); } else { - return descriptor->name(); + return std::string(descriptor->name()); } } diff --git a/src/google/protobuf/compiler/csharp/csharp_helpers.h b/src/google/protobuf/compiler/csharp/csharp_helpers.h index a7158d23a8..a97a16a3c1 100644 --- a/src/google/protobuf/compiler/csharp/csharp_helpers.h +++ b/src/google/protobuf/compiler/csharp/csharp_helpers.h @@ -98,7 +98,7 @@ inline bool IsDescriptorOptionMessage(const Descriptor* descriptor) { if (!IsDescriptorProto(descriptor->file())) { return false; } - const std::string name = descriptor->name(); + const absl::string_view name = descriptor->name(); return name == "FileOptions" || name == "MessageOptions" || name == "FieldOptions" || diff --git a/src/google/protobuf/compiler/csharp/csharp_message.cc b/src/google/protobuf/compiler/csharp/csharp_message.cc index f8d57cf207..9cc23761dc 100644 --- a/src/google/protobuf/compiler/csharp/csharp_message.cc +++ b/src/google/protobuf/compiler/csharp/csharp_message.cc @@ -9,6 +9,7 @@ #include #include +#include #include "google/protobuf/compiler/code_generator.h" #include "absl/container/flat_hash_map.h" @@ -65,7 +66,9 @@ MessageGenerator::MessageGenerator(const Descriptor* descriptor, 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() { return GetClassName(descriptor_); diff --git a/src/google/protobuf/compiler/csharp/names.cc b/src/google/protobuf/compiler/csharp/names.cc index 7e9e345ff8..8d7f752f6b 100644 --- a/src/google/protobuf/compiler/csharp/names.cc +++ b/src/google/protobuf/compiler/csharp/names.cc @@ -38,10 +38,10 @@ absl::string_view StripDotProto(absl::string_view proto_file) { // Returns the Pascal-cased last part of the proto file. For example, // input of "google/protobuf/foo_bar.proto" would result in "FooBar". std::string GetFileNameBase(const FileDescriptor* descriptor) { - std::string proto_file = descriptor->name(); - int lastslash = proto_file.find_last_of('/'); - std::string base = proto_file.substr(lastslash + 1); - return UnderscoresToPascalCase(StripDotProto(base)); + const absl::string_view proto_file = descriptor->name(); + int lastslash = proto_file.find_last_of('/'); + const absl::string_view base = proto_file.substr(lastslash + 1); + return UnderscoresToPascalCase(StripDotProto(base)); } std::string ToCSharpName(absl::string_view name, const FileDescriptor* file) { diff --git a/src/google/protobuf/compiler/java/doc_comment.cc b/src/google/protobuf/compiler/java/doc_comment.cc index 2e194dd59d..ee4bc73cc4 100644 --- a/src/google/protobuf/compiler/java/doc_comment.cc +++ b/src/google/protobuf/compiler/java/doc_comment.cc @@ -19,6 +19,7 @@ #include #include "absl/strings/str_split.h" +#include "absl/strings/string_view.h" #include "google/protobuf/compiler/java/options.h" #include "google/protobuf/descriptor.h" #include "google/protobuf/descriptor.pb.h" @@ -29,7 +30,7 @@ namespace protobuf { namespace compiler { namespace java { -std::string EscapeJavadoc(const std::string& input) { +std::string EscapeJavadoc(absl::string_view input) { std::string result; result.reserve(input.size() * 2); @@ -87,7 +88,7 @@ std::string EscapeJavadoc(const std::string& input) { return result; } -static std::string EscapeKdoc(const std::string& input) { +static std::string EscapeKdoc(absl::string_view input) { std::string result; result.reserve(input.size() * 2); @@ -142,7 +143,7 @@ static void WriteDocCommentBodyForLocation(io::Printer* printer, comments = EscapeJavadoc(comments); } - std::vector lines = absl::StrSplit(comments, "\n"); + std::vector lines = absl::StrSplit(comments, '\n'); while (!lines.empty() && lines.back().empty()) { lines.pop_back(); } diff --git a/src/google/protobuf/compiler/java/doc_comment.h b/src/google/protobuf/compiler/java/doc_comment.h index 0820b16aeb..41c4d3126a 100644 --- a/src/google/protobuf/compiler/java/doc_comment.h +++ b/src/google/protobuf/compiler/java/doc_comment.h @@ -12,6 +12,7 @@ #ifndef 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/descriptor.h" @@ -71,7 +72,7 @@ void WriteMethodDocComment(io::Printer* printer, const MethodDescriptor* method, // Exposed for testing only. // 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 compiler diff --git a/src/google/protobuf/compiler/java/helpers.h b/src/google/protobuf/compiler/java/helpers.h index d99e085a65..3cd40e2d0e 100644 --- a/src/google/protobuf/compiler/java/helpers.h +++ b/src/google/protobuf/compiler/java/helpers.h @@ -108,7 +108,7 @@ std::string ExtraMessageOrBuilderInterfaces(const Descriptor* descriptor); // Get the unqualified Java class name for mutable messages. i.e. without // package or outer classnames. 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 diff --git a/src/google/protobuf/compiler/java/lite/message.cc b/src/google/protobuf/compiler/java/lite/message.cc index 1d240672d8..39222bbb9b 100644 --- a/src/google/protobuf/compiler/java/lite/message.cc +++ b/src/google/protobuf/compiler/java/lite/message.cc @@ -95,7 +95,7 @@ void ImmutableMessageLiteGenerator::GenerateInterface(io::Printer* printer) { {"deprecation", descriptor_->options().deprecated() ? "@java.lang.Deprecated " : ""}, {"extra_interfaces", ExtraMessageOrBuilderInterfaces(descriptor_)}, - {"classname", descriptor_->name()}, + {"classname", std::string(descriptor_->name())}, }; if (!context_->options().opensource_runtime) { diff --git a/src/google/protobuf/compiler/java/name_resolver.cc b/src/google/protobuf/compiler/java/name_resolver.cc index 5d270c2881..0446173e2a 100644 --- a/src/google/protobuf/compiler/java/name_resolver.cc +++ b/src/google/protobuf/compiler/java/name_resolver.cc @@ -55,7 +55,7 @@ std::string ClassNameWithoutPackage(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(); while (temp) { @@ -71,7 +71,7 @@ std::string ClassNameWithoutPackage(const EnumDescriptor* descriptor, // Doesn't append "Mutable" for enum type's name. const Descriptor* message_descriptor = descriptor->containing_type(); if (message_descriptor == nullptr) { - return descriptor->name(); + return std::string(descriptor->name()); } else { return absl::StrCat(ClassNameWithoutPackage(message_descriptor, immutable), ".", descriptor->name()); diff --git a/src/google/protobuf/compiler/java/shared_code_generator.cc b/src/google/protobuf/compiler/java/shared_code_generator.cc index d2bb9a5dd8..ddbdda5ac7 100644 --- a/src/google/protobuf/compiler/java/shared_code_generator.cc +++ b/src/google/protobuf/compiler/java/shared_code_generator.cc @@ -10,6 +10,7 @@ #include "google/protobuf/compiler/java/shared_code_generator.h" #include +#include #include "absl/strings/escaping.h" #include "absl/strings/str_cat.h" @@ -17,6 +18,7 @@ #include "google/protobuf/compiler/java/helpers.h" #include "google/protobuf/compiler/java/name_resolver.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/versions.h" #include "google/protobuf/descriptor.h" @@ -166,7 +168,7 @@ void SharedCodeGenerator::GenerateDescriptors(io::Printer* printer) { // Find out all dependencies. std::vector > dependencies; 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 classname = name_resolver_->GetDescriptorClassName(file_->dependency(i)); @@ -176,7 +178,7 @@ void SharedCodeGenerator::GenerateDescriptors(io::Printer* printer) { } else { 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)); } // ----------------------------------------------------------------- diff --git a/src/google/protobuf/compiler/objectivec/enum.cc b/src/google/protobuf/compiler/objectivec/enum.cc index c8bfcbc3df..1c4b86f657 100644 --- a/src/google/protobuf/compiler/objectivec/enum.cc +++ b/src/google/protobuf/compiler/objectivec/enum.cc @@ -170,7 +170,7 @@ void EnumGenerator::GenerateSource(io::Printer* printer) const { text_blob += short_name + '\0'; if (UnCamelCaseEnumShortName(short_name) != v->name()) { text_format_decode_data.AddString(enum_value_description_key, short_name, - v->name()); + std::string(v->name())); } } diff --git a/src/google/protobuf/compiler/objectivec/field.cc b/src/google/protobuf/compiler/objectivec/field.cc index c60158afb5..f72b6a99bb 100644 --- a/src/google/protobuf/compiler/objectivec/field.cc +++ b/src/google/protobuf/compiler/objectivec/field.cc @@ -137,8 +137,7 @@ bool HasNonZeroDefaultValue(const FieldDescriptor* field) { case FieldDescriptor::CPPTYPE_BOOL: return field->default_value_bool(); case FieldDescriptor::CPPTYPE_STRING: { - const std::string& default_string = field->default_value_string(); - return !default_string.empty(); + return !field->default_value_string().empty(); } case FieldDescriptor::CPPTYPE_ENUM: // The default value for an enum field is the first enum value, so there diff --git a/src/google/protobuf/compiler/objectivec/file.cc b/src/google/protobuf/compiler/objectivec/file.cc index fdd0670297..6020d41550 100644 --- a/src/google/protobuf/compiler/objectivec/file.cc +++ b/src/google/protobuf/compiler/objectivec/file.cc @@ -530,7 +530,7 @@ void FileGenerator::GenerateFile(io::Printer* p, GeneratedFileType file_type, // #import the headers for anything that a plain dependency of this // proto file (that means they were just an include, not a "public" // include). - absl::flat_hash_set public_import_names; + absl::flat_hash_set public_import_names; for (int i = 0; i < file_->public_dependency_count(); i++) { public_import_names.insert(file_->public_dependency(i)->name()); } diff --git a/src/google/protobuf/compiler/objectivec/helpers.cc b/src/google/protobuf/compiler/objectivec/helpers.cc index 12608f47d8..02d5db0845 100644 --- a/src/google/protobuf/compiler/objectivec/helpers.cc +++ b/src/google/protobuf/compiler/objectivec/helpers.cc @@ -405,7 +405,7 @@ bool HasWKTWithObjCCategory(const FileDescriptor* file) { // We don't check the name prefix or proto package because some files // (descriptor.proto), aren't shipped generated by the library, so this // 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" || name == "google/protobuf/duration.proto" || name == "google/protobuf/timestamp.proto") { @@ -419,7 +419,7 @@ bool IsWKTWithObjCCategory(const Descriptor* descriptor) { if (!HasWKTWithObjCCategory(descriptor->file())) { 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" || full_name == "google.protobuf.Duration" || full_name == "google.protobuf.Timestamp") { diff --git a/src/google/protobuf/compiler/objectivec/names.cc b/src/google/protobuf/compiler/objectivec/names.cc index 597e0ecbe8..01fdbfc9e8 100644 --- a/src/google/protobuf/compiler/objectivec/names.cc +++ b/src/google/protobuf/compiler/objectivec/names.cc @@ -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", // allowing the expected prefixes file. 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); @@ -574,9 +575,9 @@ std::string SanitizeNameForObjC(absl::string_view prefix, std::string NameFromFieldDescriptor(const FieldDescriptor* field) { if (internal::cpp::IsGroupLike(*field)) { - return field->message_type()->name(); + return std::string(field->message_type()->name()); } 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 // (descriptor.proto), aren't shipped generated by the library, so this // 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" || name == "google/protobuf/api.proto" || name == "google/protobuf/duration.proto" || @@ -1241,12 +1242,13 @@ bool ValidateObjCClassPrefix( bool has_prefix = file->options().has_objc_class_prefix(); bool have_expected_prefix_file = !expected_prefixes_path.empty(); - const std::string prefix = file->options().objc_class_prefix(); - const std::string package = file->package(); + const absl::string_view prefix = file->options().objc_class_prefix(); + const absl::string_view package = file->package(); // For files without packages, the can be registered as "no_package:PATH", // allowing the expected prefixes file. 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 // error cases, so it seems to be ok to use as a back door for warnings. diff --git a/src/google/protobuf/compiler/php/names.cc b/src/google/protobuf/compiler/php/names.cc index 135b55b94e..79371ab37b 100644 --- a/src/google/protobuf/compiler/php/names.cc +++ b/src/google/protobuf/compiler/php/names.cc @@ -49,7 +49,7 @@ bool IsReservedName(absl::string_view name) { return false; } -std::string ReservedNamePrefix(const std::string& classname, +std::string ReservedNamePrefix(const absl::string_view classname, const FileDescriptor* file) { if (IsReservedName(classname)) { if (file->package() == "google.protobuf") { @@ -65,7 +65,7 @@ std::string ReservedNamePrefix(const std::string& classname, namespace { template -std::string ClassNamePrefixImpl(const std::string& classname, +std::string ClassNamePrefixImpl(const absl::string_view classname, const DescriptorType* desc) { const std::string& prefix = (desc->file()->options()).php_class_prefix(); if (!prefix.empty()) { @@ -77,19 +77,20 @@ std::string ClassNamePrefixImpl(const std::string& classname, template 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(); while (containing != nullptr) { - classname = ClassNamePrefixImpl(containing->name(), desc) + containing->name() - + '\\' + classname; + classname = absl::StrCat(ClassNamePrefixImpl(containing->name(), desc), + containing->name(), "\\", classname); containing = containing->containing_type(); } return classname; } std::string GeneratedClassNameImpl(const ServiceDescriptor* desc) { - std::string classname = desc->name(); - return ClassNamePrefixImpl(classname, desc) + classname; + const absl::string_view classname = desc->name(); + return absl::StrCat(ClassNamePrefixImpl(classname, desc), classname); } } // namespace diff --git a/src/google/protobuf/compiler/php/names.h b/src/google/protobuf/compiler/php/names.h index 76220f8f2b..1d37488ff2 100644 --- a/src/google/protobuf/compiler/php/names.h +++ b/src/google/protobuf/compiler/php/names.h @@ -24,13 +24,13 @@ namespace php { PROTOC_EXPORT bool IsReservedName(absl::string_view name); // 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); // 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); -PROTOC_EXPORT std::string ClassNamePrefix(const std::string& classname, +PROTOC_EXPORT std::string ClassNamePrefix(absl::string_view classname, const EnumDescriptor* desc); // To skip reserved keywords in php, some generated classname are prefixed. diff --git a/src/google/protobuf/compiler/php/php_generator.cc b/src/google/protobuf/compiler/php/php_generator.cc index 006d6debd5..ef8cdcefe5 100644 --- a/src/google/protobuf/compiler/php/php_generator.cc +++ b/src/google/protobuf/compiler/php/php_generator.cc @@ -752,9 +752,10 @@ void GenerateEnumToPool(const EnumDescriptor* en, io::Printer* printer) { for (int i = 0; i < en->value_count(); i++) { const EnumValueDescriptor* value = en->value(i); - printer->Print("->value(\"^name^\", ^number^)\n", "name", - ConstantNamePrefix(value->name()) + value->name(), "number", - IntToString(value->number())); + printer->Print( + "->value(\"^name^\", ^number^)\n", "name", + absl::StrCat(ConstantNamePrefix(value->name()), value->name()), + "number", IntToString(value->number())); } printer->Print("->finalizeToPool();\n\n"); Outdent(printer); @@ -1172,17 +1173,19 @@ bool GenerateEnumFile(const FileDescriptor* file, const EnumDescriptor* en, hasReserved = true; } - printer.Print("const ^name^ = ^number^;\n", "name", prefix + value->name(), - "number", IntToString(value->number())); + printer.Print("const ^name^ = ^number^;\n", "name", + absl::StrCat(prefix, value->name()), "number", + IntToString(value->number())); } printer.Print("\nprivate static $valueToName = [\n"); Indent(&printer); for (int i = 0; i < en->value_count(); i++) { const EnumValueDescriptor* value = en->value(i); - printer.Print("self::^constant^ => '^name^',\n", "constant", - ConstantNamePrefix(value->name()) + value->name(), "name", - value->name()); + printer.Print( + "self::^constant^ => '^name^',\n", "constant", + absl::StrCat(ConstantNamePrefix(value->name()), value->name()), "name", + value->name()); } Outdent(&printer); printer.Print("];\n"); diff --git a/src/google/protobuf/compiler/python/generator.cc b/src/google/protobuf/compiler/python/generator.cc index dc4ac837c8..41be7df168 100644 --- a/src/google/protobuf/compiler/python/generator.cc +++ b/src/google/protobuf/compiler/python/generator.cc @@ -1433,7 +1433,7 @@ void Generator::FixOptionsForEnum(const EnumDescriptor& enum_descriptor, PrintDescriptorOptionsFixingCode( value_descriptor, proto.value(i), absl::StrFormat("%s.values_by_name[\"%s\"]", descriptor_name.c_str(), - value_descriptor.name().c_str())); + value_descriptor.name())); } } diff --git a/src/google/protobuf/compiler/python/helpers.cc b/src/google/protobuf/compiler/python/helpers.cc index 0a6a8062b7..63cc7ae1d6 100644 --- a/src/google/protobuf/compiler/python/helpers.cc +++ b/src/google/protobuf/compiler/python/helpers.cc @@ -94,7 +94,7 @@ std::string GeneratedCodeToBase64(const GeneratedCodeInfo& annotations) { template std::string NamePrefixedWithNestedTypes(const DescriptorT& descriptor, absl::string_view separator) { - std::string name = descriptor.name(); + std::string name = std::string(descriptor.name()); const Descriptor* parent = descriptor.containing_type(); if (parent != nullptr) { std::string prefix = NamePrefixedWithNestedTypes(*parent, separator); diff --git a/src/google/protobuf/compiler/python/pyi_generator.cc b/src/google/protobuf/compiler/python/pyi_generator.cc index 7f7bfeda08..323f93eba4 100644 --- a/src/google/protobuf/compiler/python/pyi_generator.cc +++ b/src/google/protobuf/compiler/python/pyi_generator.cc @@ -39,7 +39,7 @@ std::string PyiGenerator::ModuleLevelName(const DescriptorT& descriptor) const { std::string name = NamePrefixedWithNestedTypes(descriptor, "."); if (descriptor.file() != file_) { 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()) { std::string module_name = ModuleName(descriptor.file()->name()); std::vector tokens = absl::StrSplit(module_name, '.'); @@ -71,7 +71,7 @@ struct ImportModules { }; // 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) return (name == "google.protobuf.Any" || name == "google.protobuf.Duration" || @@ -132,7 +132,7 @@ void CheckImportModules(const Descriptor* descriptor, void PyiGenerator::PrintImportForDescriptor( const FileDescriptor& desc, absl::flat_hash_set* seen_aliases, bool* has_importlib) const { - const std::string& filename = desc.name(); + const absl::string_view filename = desc.name(); std::string module_name_owned = StrippedModuleName(filename); absl::string_view module_name(module_name_owned); 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 { - std::string enum_name = enum_descriptor.name(); + const absl::string_view enum_name = enum_descriptor.name(); printer_->Print( "class $enum_name$(int, metaclass=_enum_type_wrapper.EnumTypeWrapper):\n" " __slots__ = ()\n", @@ -385,7 +385,7 @@ void PyiGenerator::PrintMessage( if (!is_nested) { printer_->Print("\n"); } - std::string class_name = message_descriptor.name(); + const absl::string_view class_name = message_descriptor.name(); std::string extra_base; // A well-known type needs to inherit from its corresponding base class in // net/proto2/python/internal/well_known_types. @@ -484,7 +484,7 @@ void PyiGenerator::PrintMessage( has_key_words = true; continue; } - std::string field_name = field_des->name(); + std::string field_name = std::string(field_des->name()); if (is_first && field_name == "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