Prepare the code for migrating return types from `const std::string&` to `absl::string_view`. PiperOrigin-RevId: 651520812
diff --git a/hpb_generator/BUILD b/hpb_generator/BUILD index 63cfe8b..7ca5c59 100644 --- a/hpb_generator/BUILD +++ b/hpb_generator/BUILD
@@ -103,5 +103,6 @@ ":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 3827425..f89afc9 100644 --- a/hpb_generator/gen_utils.cc +++ b/hpb_generator/gen_utils.cc
@@ -12,6 +12,7 @@ #include <vector> #include "absl/strings/ascii.h" +#include "absl/strings/string_view.h" namespace google::protobuf::hpb_generator { @@ -102,7 +103,7 @@ 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 3cf8a85..a83500c 100644 --- a/hpb_generator/gen_utils.h +++ b/hpb_generator/gen_utils.h
@@ -12,6 +12,7 @@ #include <vector> #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<const protobuf::FieldDescriptor*> 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 b3145f3..5ea3da9 100644 --- a/hpb_generator/names.cc +++ b/hpb_generator/names.cc
@@ -9,6 +9,7 @@ #include <string> +#include "absl/strings/string_view.h" #include "upb_generator/keywords.h" namespace google::protobuf::hpb_generator { @@ -21,11 +22,11 @@ "::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 1ee01f5..cd95c8b 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 <cstdint> -#include <cstdlib> -#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 <errno.h> +#include <limits.h> #include <stdio.h> #include <stdlib.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 #undef major #endif @@ -45,20 +36,10 @@ #endif #include <fcntl.h> #include <sys/stat.h> + #ifndef _MSC_VER #include <unistd.h> #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__) #include <mach-o/dyld.h> @@ -66,27 +47,44 @@ #include <sys/sysctl.h> #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 @@ // 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<FieldRange>& 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 d61a622..c40a5df 100644 --- a/src/google/protobuf/compiler/cpp/enum.cc +++ b/src/google/protobuf/compiler/cpp/enum.cc
@@ -41,7 +41,7 @@ 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 1d95d61..4dbfe5a 100644 --- a/src/google/protobuf/compiler/cpp/extension.cc +++ b/src/google/protobuf/compiler/cpp/extension.cc
@@ -63,8 +63,7 @@ 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<int>(descriptor_->type()));
diff --git a/src/google/protobuf/compiler/cpp/file.cc b/src/google/protobuf/compiler/cpp/file.cc index aeca550..246e09f 100644 --- a/src/google/protobuf/compiler/cpp/file.cc +++ b/src/google/protobuf/compiler/cpp/file.cc
@@ -59,7 +59,7 @@ absl::flat_hash_map<absl::string_view, std::string> 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 @@ } // 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 f221614..c6a6c56 100644 --- a/src/google/protobuf/compiler/cpp/helpers.cc +++ b/src/google/protobuf/compiler/cpp/helpers.cc
@@ -529,7 +529,7 @@ } 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 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 @@ 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 1aadfbe..95c2b6b 100644 --- a/src/google/protobuf/compiler/cpp/helpers.h +++ b/src/google/protobuf/compiler/cpp/helpers.h
@@ -583,7 +583,7 @@ 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 @@ template <typename T> 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 b08cc95..a6552e1 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 <gtest/gtest.h> +#include <gmock/gmock.h> #include "absl/base/casts.h" #include "absl/strings/substitute.h" #include "google/protobuf/arena.h" @@ -66,6 +66,8 @@ // 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 @@ UNITTEST::TestMutualRecursionA a; MessageSCCAnalyzer scc_analyzer((Options())); const SCC* scc = scc_analyzer.GetSCC(a.GetDescriptor()); - std::vector<std::string> names; + std::vector<absl::string_view> 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 7f2626c..ac03775 100644 --- a/src/google/protobuf/compiler/csharp/csharp_enum.cc +++ b/src/google/protobuf/compiler/csharp/csharp_enum.cc
@@ -50,7 +50,7 @@ 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 7a6a4b9..2058865 100644 --- a/src/google/protobuf/compiler/csharp/csharp_helpers.cc +++ b/src/google/protobuf/compiler/csharp/csharp_helpers.cc
@@ -208,9 +208,9 @@ // 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 a7158d2..a97a16a 100644 --- a/src/google/protobuf/compiler/csharp/csharp_helpers.h +++ b/src/google/protobuf/compiler/csharp/csharp_helpers.h
@@ -98,7 +98,7 @@ 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 f8d57cf..9cc2376 100644 --- a/src/google/protobuf/compiler/csharp/csharp_message.cc +++ b/src/google/protobuf/compiler/csharp/csharp_message.cc
@@ -9,6 +9,7 @@ #include <algorithm> #include <sstream> +#include <string> #include "google/protobuf/compiler/code_generator.h" #include "absl/container/flat_hash_map.h" @@ -65,7 +66,9 @@ 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 7e9e345..8d7f752 100644 --- a/src/google/protobuf/compiler/csharp/names.cc +++ b/src/google/protobuf/compiler/csharp/names.cc
@@ -38,10 +38,10 @@ // 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 2e194dd..ee4bc73 100644 --- a/src/google/protobuf/compiler/java/doc_comment.cc +++ b/src/google/protobuf/compiler/java/doc_comment.cc
@@ -19,6 +19,7 @@ #include <vector> #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 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 @@ 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 @@ 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()) { lines.pop_back(); }
diff --git a/src/google/protobuf/compiler/java/doc_comment.h b/src/google/protobuf/compiler/java/doc_comment.h index 0820b16..41c4d31 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 @@ // 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 d99e085..3cd40e2 100644 --- a/src/google/protobuf/compiler/java/helpers.h +++ b/src/google/protobuf/compiler/java/helpers.h
@@ -108,7 +108,7 @@ // 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 1d24067..39222bb 100644 --- a/src/google/protobuf/compiler/java/lite/message.cc +++ b/src/google/protobuf/compiler/java/lite/message.cc
@@ -95,7 +95,7 @@ {"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 5d270c2..0446173 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 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 @@ // 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 d2bb9a5..ddbdda5 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 <memory> +#include <utility> #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 @@ // Find out all dependencies. std::vector<std::pair<std::string, std::string> > 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 @@ } 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 c8bfcbc..1c4b86f 100644 --- a/src/google/protobuf/compiler/objectivec/enum.cc +++ b/src/google/protobuf/compiler/objectivec/enum.cc
@@ -170,7 +170,7 @@ 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 c60158a..f72b6a9 100644 --- a/src/google/protobuf/compiler/objectivec/field.cc +++ b/src/google/protobuf/compiler/objectivec/field.cc
@@ -137,8 +137,7 @@ 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 fdd0670..6020d41 100644 --- a/src/google/protobuf/compiler/objectivec/file.cc +++ b/src/google/protobuf/compiler/objectivec/file.cc
@@ -530,7 +530,7 @@ // #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<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++) { 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 12608f4..02d5db0 100644 --- a/src/google/protobuf/compiler/objectivec/helpers.cc +++ b/src/google/protobuf/compiler/objectivec/helpers.cc
@@ -405,7 +405,7 @@ // 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 @@ 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 597e0ec..01fdbfc 100644 --- a/src/google/protobuf/compiler/objectivec/names.cc +++ b/src/google/protobuf/compiler/objectivec/names.cc
@@ -163,11 +163,12 @@ } } - 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 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 @@ // 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 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 135b55b..79371ab 100644 --- a/src/google/protobuf/compiler/php/names.cc +++ b/src/google/protobuf/compiler/php/names.cc
@@ -49,7 +49,7 @@ 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 @@ namespace { template <typename DescriptorType> -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 @@ template <typename DescriptorType> 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 76220f8..1d37488 100644 --- a/src/google/protobuf/compiler/php/names.h +++ b/src/google/protobuf/compiler/php/names.h
@@ -24,13 +24,13 @@ 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 006d6de..ef8cdce 100644 --- a/src/google/protobuf/compiler/php/php_generator.cc +++ b/src/google/protobuf/compiler/php/php_generator.cc
@@ -752,9 +752,10 @@ 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 @@ 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 dc4ac83..41be7df 100644 --- a/src/google/protobuf/compiler/python/generator.cc +++ b/src/google/protobuf/compiler/python/generator.cc
@@ -1433,7 +1433,7 @@ 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 0a6a806..63cc7ae 100644 --- a/src/google/protobuf/compiler/python/helpers.cc +++ b/src/google/protobuf/compiler/python/helpers.cc
@@ -94,7 +94,7 @@ template <typename DescriptorT> 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 7f7bfed..323f93e 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 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<absl::string_view> tokens = absl::StrSplit(module_name, '.'); @@ -71,7 +71,7 @@ }; // 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 PyiGenerator::PrintImportForDescriptor( const FileDescriptor& desc, absl::flat_hash_set<std::string>* 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 @@ } 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 @@ 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 @@ 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