Skip to content

Commit d1990d9

Browse files
Breaking change: Upgrade return type of several string returning functions to absl::string_view.
This includes (but not limited to) `MessageLite::GetTypeName`, `UnknownField::length_delimited`, and all the name functions in descriptors like `FieldDescriptor::full_name`. Part of the changes announced in https://protobuf.dev/news/2024-10-02/#descriptor-apis PiperOrigin-RevId: 715013326
1 parent 1355ef2 commit d1990d9

20 files changed

+64
-70
lines changed

src/google/protobuf/compiler/cpp/field_generators/cord_field.cc

+1-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ void SetCordVariables(
3939
"\"", absl::CEscape(descriptor->default_value_string()), "\"");
4040
(*variables)["default_length"] =
4141
absl::StrCat(descriptor->default_value_string().length());
42-
(*variables)["full_name"] = descriptor->full_name();
42+
(*variables)["full_name"] = std::string(descriptor->full_name());
4343
// For one of Cords
4444
(*variables)["default_variable_name"] = MakeDefaultName(descriptor);
4545
(*variables)["default_variable_field"] = MakeDefaultFieldName(descriptor);

src/google/protobuf/compiler/cpp/service.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@ class ServiceGenerator {
3232
const absl::flat_hash_map<absl::string_view, std::string>& vars,
3333
const Options& options)
3434
: descriptor_(descriptor), options_(&options), vars_(vars) {
35-
vars_["classname"] = descriptor_->name();
36-
vars_["full_name"] = descriptor_->full_name();
35+
vars_["classname"] = std::string(descriptor_->name());
36+
vars_["full_name"] = std::string(descriptor_->full_name());
3737
}
3838

3939
ServiceGenerator(const ServiceGenerator&) = delete;

src/google/protobuf/compiler/csharp/csharp_field_base.cc

+1-1
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ void FieldGeneratorBase::SetCommonFieldVariables(
7272
(*variables)["type_name"] = type_name();
7373
(*variables)["extended_type"] = GetClassName(descriptor_->containing_type());
7474
(*variables)["name"] = name();
75-
(*variables)["descriptor_name"] = descriptor_->name();
75+
(*variables)["descriptor_name"] = std::string(descriptor_->name());
7676
(*variables)["default_value"] = default_value();
7777
(*variables)["capitalized_type_name"] = capitalized_type_name();
7878
(*variables)["number"] = number();

src/google/protobuf/compiler/csharp/csharp_message.cc

+1-1
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@ void MessageGenerator::Generate(io::Printer* printer) {
212212
const OneofDescriptor* oneof = descriptor_->oneof_decl(i);
213213
vars["name"] = UnderscoresToCamelCase(oneof->name(), false);
214214
vars["property_name"] = UnderscoresToCamelCase(oneof->name(), true);
215-
vars["original_name"] = oneof->name();
215+
vars["original_name"] = std::string(oneof->name());
216216
printer->Print(vars,
217217
"private object $name$_;\n"
218218
"/// <summary>Enum of possible cases for the "

src/google/protobuf/compiler/java/doc_comment.cc

+1-1
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ static void WriteDebugString(io::Printer* printer, const FieldDescriptor* field,
210210
const Options options, const bool kdoc) {
211211
std::string field_comment = FirstLineOf(field->DebugString());
212212
if (options.strip_nonfunctional_codegen) {
213-
field_comment = field->name();
213+
field_comment = std::string(field->name());
214214
}
215215
if (kdoc) {
216216
printer->Print(" * `$def$`\n", "def", EscapeKdoc(field_comment));

src/google/protobuf/compiler/java/field_common.cc

+3-2
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,10 @@ namespace java {
1616
void SetCommonFieldVariables(
1717
const FieldDescriptor* descriptor, const FieldGeneratorInfo* info,
1818
absl::flat_hash_map<absl::string_view, std::string>* variables) {
19-
(*variables)["field_name"] = descriptor->name();
19+
(*variables)["field_name"] = std::string(descriptor->name());
2020
(*variables)["name"] = info->name;
21-
(*variables)["classname"] = descriptor->containing_type()->name();
21+
(*variables)["classname"] =
22+
std::string(descriptor->containing_type()->name());
2223
(*variables)["capitalized_name"] = info->capitalized_name;
2324
(*variables)["disambiguated_reason"] = info->disambiguated_reason;
2425
(*variables)["constant_name"] = FieldConstantName(descriptor);

src/google/protobuf/compiler/java/file.cc

+4-5
Original file line numberDiff line numberDiff line change
@@ -205,12 +205,11 @@ bool FileGenerator::Validate(std::string* error) {
205205
// end up overwriting the outer class with one of the inner ones.
206206
if (name_resolver_->HasConflictingClassName(file_, classname_,
207207
NameEquality::EXACT_EQUAL)) {
208-
error->assign(file_->name());
209-
error->append(
208+
*error = absl::StrCat(
209+
file_->name(),
210210
": Cannot generate Java output because the file's outer class name, "
211-
"\"");
212-
error->append(classname_);
213-
error->append(
211+
"\"",
212+
classname_,
214213
"\", matches the name of one of the types declared inside it. "
215214
"Please either rename the type or use the java_outer_classname "
216215
"option to specify a different outer class name for the .proto file.");

src/google/protobuf/compiler/java/full/enum.cc

+5-5
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ void EnumNonLiteGenerator::Generate(io::Printer* printer) {
8585

8686
for (const EnumValueDescriptor* value : canonical_values_) {
8787
absl::flat_hash_map<absl::string_view, std::string> vars;
88-
vars["name"] = value->name();
88+
vars["name"] = std::string(value->name());
8989
vars["index"] = absl::StrCat(value->index());
9090
vars["number"] = absl::StrCat(value->number());
9191
WriteEnumValueDocComment(printer, value, context_->options());
@@ -124,9 +124,9 @@ void EnumNonLiteGenerator::Generate(io::Printer* printer) {
124124

125125
for (const Alias& alias : aliases_) {
126126
absl::flat_hash_map<absl::string_view, std::string> vars;
127-
vars["classname"] = descriptor_->name();
128-
vars["name"] = alias.value->name();
129-
vars["canonical_name"] = alias.canonical_value->name();
127+
vars["classname"] = std::string(descriptor_->name());
128+
vars["name"] = std::string(alias.value->name());
129+
vars["canonical_name"] = std::string(alias.canonical_value->name());
130130
WriteEnumValueDocComment(printer, alias.value, context_->options());
131131
printer->Print(
132132
vars, "public static final $classname$ $name$ = $canonical_name$;\n");
@@ -135,7 +135,7 @@ void EnumNonLiteGenerator::Generate(io::Printer* printer) {
135135

136136
for (int i = 0; i < descriptor_->value_count(); i++) {
137137
absl::flat_hash_map<absl::string_view, std::string> vars;
138-
vars["name"] = descriptor_->value(i)->name();
138+
vars["name"] = std::string(descriptor_->value(i)->name());
139139
vars["number"] = absl::StrCat(descriptor_->value(i)->number());
140140
vars["{"] = "";
141141
vars["}"] = "";

src/google/protobuf/compiler/java/full/message.cc

+1-1
Original file line numberDiff line numberDiff line change
@@ -294,7 +294,7 @@ void ImmutableMessageGenerator::Generate(io::Printer* printer) {
294294

295295
absl::flat_hash_map<absl::string_view, std::string> variables;
296296
variables["static"] = is_own_file ? "" : "static ";
297-
variables["classname"] = descriptor_->name();
297+
variables["classname"] = std::string(descriptor_->name());
298298
variables["extra_interfaces"] = ExtraMessageInterfaces(descriptor_);
299299
variables["deprecation"] =
300300
descriptor_->options().deprecated() ? "@java.lang.Deprecated " : "";

src/google/protobuf/compiler/java/lite/enum.cc

+5-5
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ void EnumLiteGenerator::Generate(io::Printer* printer) {
7171

7272
for (const EnumValueDescriptor* value : canonical_values_) {
7373
absl::flat_hash_map<absl::string_view, std::string> vars;
74-
vars["name"] = value->name();
74+
vars["name"] = std::string(value->name());
7575
vars["number"] = absl::StrCat(value->number());
7676
WriteEnumValueDocComment(printer, value, context_->options());
7777
if (value->options().deprecated()) {
@@ -94,9 +94,9 @@ void EnumLiteGenerator::Generate(io::Printer* printer) {
9494

9595
for (const Alias& alias : aliases_) {
9696
absl::flat_hash_map<absl::string_view, std::string> vars;
97-
vars["classname"] = descriptor_->name();
98-
vars["name"] = alias.value->name();
99-
vars["canonical_name"] = alias.canonical_value->name();
97+
vars["classname"] = std::string(descriptor_->name());
98+
vars["name"] = std::string(alias.value->name());
99+
vars["canonical_name"] = std::string(alias.canonical_value->name());
100100
WriteEnumValueDocComment(printer, alias.value, context_->options());
101101
printer->Print(
102102
vars, "public static final $classname$ $name$ = $canonical_name$;\n");
@@ -105,7 +105,7 @@ void EnumLiteGenerator::Generate(io::Printer* printer) {
105105

106106
for (int i = 0; i < descriptor_->value_count(); i++) {
107107
absl::flat_hash_map<absl::string_view, std::string> vars;
108-
vars["name"] = descriptor_->value(i)->name();
108+
vars["name"] = std::string(descriptor_->value(i)->name());
109109
vars["number"] = absl::StrCat(descriptor_->value(i)->number());
110110
vars["{"] = "";
111111
vars["}"] = "";

src/google/protobuf/compiler/java/lite/message.cc

+1-1
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ void ImmutableMessageLiteGenerator::Generate(io::Printer* printer) {
149149
absl::flat_hash_map<absl::string_view, std::string> variables = {{"{", ""},
150150
{"}", ""}};
151151
variables["static"] = is_own_file ? " " : " static ";
152-
variables["classname"] = descriptor_->name();
152+
variables["classname"] = std::string(descriptor_->name());
153153
variables["extra_interfaces"] = ExtraMessageInterfaces(descriptor_);
154154
variables["deprecation"] =
155155
descriptor_->options().deprecated() ? "@java.lang.Deprecated " : "";

src/google/protobuf/compiler/java/name_resolver.cc

+2-2
Original file line numberDiff line numberDiff line change
@@ -142,9 +142,9 @@ std::string ClassNameResolver::GetFileDefaultImmutableClassName(
142142
std::string basename;
143143
std::string::size_type last_slash = file->name().find_last_of('/');
144144
if (last_slash == std::string::npos) {
145-
basename = file->name();
145+
basename = std::string(file->name());
146146
} else {
147-
basename = file->name().substr(last_slash + 1);
147+
basename = std::string(file->name().substr(last_slash + 1));
148148
}
149149
// foo_bar_baz.proto -> FooBarBaz
150150
std::string ret = UnderscoresToCamelCase(StripProto(basename), true);

src/google/protobuf/compiler/java/names.cc

+3-3
Original file line numberDiff line numberDiff line change
@@ -86,9 +86,9 @@ std::string FieldName(const FieldDescriptor* field) {
8686
// of the group type. In Java, though, we would like to retain the original
8787
// capitalization of the type name.
8888
if (internal::cpp::IsGroupLike(*field)) {
89-
field_name = field->message_type()->name();
89+
field_name = std::string(field->message_type()->name());
9090
} else {
91-
field_name = field->name();
91+
field_name = std::string(field->name());
9292
}
9393
if (IsForbidden(field_name)) {
9494
// Append a trailing "#" to indicate that the name should be decorated to
@@ -130,7 +130,7 @@ std::string FileJavaPackage(const FileDescriptor* file, bool immutable,
130130
result = DefaultPackage(options);
131131
if (!file->package().empty()) {
132132
if (!result.empty()) result += '.';
133-
result += file->package();
133+
absl::StrAppend(&result, file->package());
134134
}
135135
}
136136

src/google/protobuf/compiler/objectivec/field.cc

+2-2
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,9 @@ void SetCommonFieldVariables(
4141
std::string camel_case_name = FieldName(descriptor);
4242
std::string raw_field_name;
4343
if (internal::cpp::IsGroupLike(*descriptor)) {
44-
raw_field_name = descriptor->message_type()->name();
44+
raw_field_name = std::string(descriptor->message_type()->name());
4545
} else {
46-
raw_field_name = descriptor->name();
46+
raw_field_name = std::string(descriptor->name());
4747
}
4848
// The logic here has to match -[GGPBFieldDescriptor textFormatName].
4949
const std::string un_camel_case_name(

src/google/protobuf/compiler/python/generator.cc

+19-19
Original file line numberDiff line numberDiff line change
@@ -562,8 +562,8 @@ void Generator::PrintResolvedFeatures() const {
562562
void Generator::PrintFileDescriptor() const {
563563
absl::flat_hash_map<absl::string_view, std::string> m;
564564
m["descriptor_name"] = kDescriptorKey;
565-
m["name"] = file_->name();
566-
m["package"] = file_->package();
565+
m["name"] = std::string(file_->name());
566+
m["package"] = std::string(file_->package());
567567
m["syntax"] = GetLegacySyntaxName(GetEdition(*file_));
568568
m["edition"] = Edition_Name(GetEdition(*file_));
569569
m["options"] = OptionsValue(proto_.options().SerializeAsString());
@@ -645,8 +645,8 @@ void Generator::PrintEnum(const EnumDescriptor& enum_descriptor,
645645
std::string module_level_descriptor_name =
646646
ModuleLevelDescriptorName(enum_descriptor);
647647
m["descriptor_name"] = module_level_descriptor_name;
648-
m["name"] = enum_descriptor.name();
649-
m["full_name"] = enum_descriptor.full_name();
648+
m["name"] = std::string(enum_descriptor.name());
649+
m["full_name"] = std::string(enum_descriptor.full_name());
650650
m["file"] = kDescriptorKey;
651651
const char enum_descriptor_template[] =
652652
"$descriptor_name$ = _descriptor.EnumDescriptor(\n"
@@ -720,7 +720,7 @@ void Generator::PrintServiceDescriptor(
720720
const ServiceDescriptor& descriptor) const {
721721
absl::flat_hash_map<absl::string_view, std::string> m;
722722
m["service_name"] = ModuleLevelServiceDescriptorName(descriptor);
723-
m["name"] = descriptor.name();
723+
m["name"] = std::string(descriptor.name());
724724
m["file"] = kDescriptorKey;
725725
printer_->Print(m, "$service_name$ = $file$.services_by_name['$name$']\n");
726726
}
@@ -770,8 +770,8 @@ void Generator::PrintServiceStub(const ServiceDescriptor& descriptor) const {
770770
void Generator::PrintDescriptor(const Descriptor& message_descriptor,
771771
const DescriptorProto& proto) const {
772772
absl::flat_hash_map<absl::string_view, std::string> m;
773-
m["name"] = message_descriptor.name();
774-
m["full_name"] = message_descriptor.full_name();
773+
m["name"] = std::string(message_descriptor.name());
774+
m["full_name"] = std::string(message_descriptor.full_name());
775775
m["file"] = kDescriptorKey;
776776

777777
PrintNestedDescriptors(message_descriptor, proto);
@@ -836,8 +836,8 @@ void Generator::PrintDescriptor(const Descriptor& message_descriptor,
836836
for (int i = 0; i < message_descriptor.oneof_decl_count(); ++i) {
837837
const OneofDescriptor* desc = message_descriptor.oneof_decl(i);
838838
m.clear();
839-
m["name"] = desc->name();
840-
m["full_name"] = desc->full_name();
839+
m["name"] = std::string(desc->name());
840+
m["full_name"] = std::string(desc->full_name());
841841
m["index"] = absl::StrCat(desc->index());
842842
options_string =
843843
OptionsValue(proto.oneof_decl(i).options().SerializeAsString());
@@ -980,9 +980,9 @@ void Generator::FixForeignFieldsInDescriptor(
980980
absl::flat_hash_map<absl::string_view, std::string> m;
981981
const OneofDescriptor* oneof = descriptor.oneof_decl(i);
982982
m["descriptor_name"] = ModuleLevelDescriptorName(descriptor);
983-
m["oneof_name"] = oneof->name();
983+
m["oneof_name"] = std::string(oneof->name());
984984
for (int j = 0; j < oneof->field_count(); ++j) {
985-
m["field_name"] = oneof->field(j)->name();
985+
m["field_name"] = std::string(oneof->field(j)->name());
986986
printer_->Print(
987987
m,
988988
"$descriptor_name$.oneofs_by_name['$oneof_name$'].fields.append(\n"
@@ -998,7 +998,7 @@ void Generator::FixForeignFieldsInDescriptor(
998998
void Generator::AddMessageToFileDescriptor(const Descriptor& descriptor) const {
999999
absl::flat_hash_map<absl::string_view, std::string> m;
10001000
m["descriptor_name"] = kDescriptorKey;
1001-
m["message_name"] = descriptor.name();
1001+
m["message_name"] = std::string(descriptor.name());
10021002
m["message_descriptor_name"] = ModuleLevelDescriptorName(descriptor);
10031003
const char file_descriptor_template[] =
10041004
"$descriptor_name$.message_types_by_name['$message_name$'] = "
@@ -1010,7 +1010,7 @@ void Generator::AddServiceToFileDescriptor(
10101010
const ServiceDescriptor& descriptor) const {
10111011
absl::flat_hash_map<absl::string_view, std::string> m;
10121012
m["descriptor_name"] = kDescriptorKey;
1013-
m["service_name"] = descriptor.name();
1013+
m["service_name"] = std::string(descriptor.name());
10141014
m["service_descriptor_name"] = ModuleLevelServiceDescriptorName(descriptor);
10151015
const char file_descriptor_template[] =
10161016
"$descriptor_name$.services_by_name['$service_name$'] = "
@@ -1022,7 +1022,7 @@ void Generator::AddEnumToFileDescriptor(
10221022
const EnumDescriptor& descriptor) const {
10231023
absl::flat_hash_map<absl::string_view, std::string> m;
10241024
m["descriptor_name"] = kDescriptorKey;
1025-
m["enum_name"] = descriptor.name();
1025+
m["enum_name"] = std::string(descriptor.name());
10261026
m["enum_descriptor_name"] = ModuleLevelDescriptorName(descriptor);
10271027
const char file_descriptor_template[] =
10281028
"$descriptor_name$.enum_types_by_name['$enum_name$'] = "
@@ -1034,7 +1034,7 @@ void Generator::AddExtensionToFileDescriptor(
10341034
const FieldDescriptor& descriptor) const {
10351035
absl::flat_hash_map<absl::string_view, std::string> m;
10361036
m["descriptor_name"] = kDescriptorKey;
1037-
m["field_name"] = descriptor.name();
1037+
m["field_name"] = std::string(descriptor.name());
10381038
m["resolved_name"] = ResolveKeyword(descriptor.name());
10391039
const char file_descriptor_template[] =
10401040
"$descriptor_name$.extensions_by_name['$field_name$'] = "
@@ -1143,7 +1143,7 @@ void Generator::PrintEnumValueDescriptor(
11431143
std::string options_string;
11441144
proto.options().SerializeToString(&options_string);
11451145
absl::flat_hash_map<absl::string_view, std::string> m;
1146-
m["name"] = descriptor.name();
1146+
m["name"] = std::string(descriptor.name());
11471147
m["index"] = absl::StrCat(descriptor.index());
11481148
m["number"] = absl::StrCat(descriptor.number());
11491149
m["options"] = OptionsValue(options_string);
@@ -1161,8 +1161,8 @@ void Generator::PrintFieldDescriptor(const FieldDescriptor& field,
11611161
std::string options_string;
11621162
proto.options().SerializeToString(&options_string);
11631163
absl::flat_hash_map<absl::string_view, std::string> m;
1164-
m["name"] = field.name();
1165-
m["full_name"] = field.full_name();
1164+
m["name"] = std::string(field.name());
1165+
m["full_name"] = std::string(field.full_name());
11661166
m["index"] = absl::StrCat(field.index());
11671167
m["number"] = absl::StrCat(field.number());
11681168
m["type"] = absl::StrCat(field.type());
@@ -1456,7 +1456,7 @@ void Generator::FixOptionsForField(const FieldDescriptor& field,
14561456
if (field.is_extension()) {
14571457
if (field.extension_scope() == nullptr) {
14581458
// Top level extensions.
1459-
field_name = field.name();
1459+
field_name = std::string(field.name());
14601460
} else {
14611461
field_name = FieldReferencingExpression(field.extension_scope(), field,
14621462
"extensions_by_name");

src/google/protobuf/compiler/ruby/ruby_generator.cc

+1-1
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ int GeneratePackageModules(const FileDescriptor* file, io::Printer* printer) {
170170
<< " 'A::B::C' and not 'A.B.C'";
171171
}
172172
} else {
173-
package_name = file->package();
173+
package_name = std::string(file->package());
174174
}
175175

176176
// Use the appropriate delimiter

0 commit comments

Comments
 (0)