Skip to content

Commit

Permalink
Breaking change: Upgrade return type of several string returning func…
Browse files Browse the repository at this point in the history
…tions 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
  • Loading branch information
protobuf-github-bot authored and copybara-github committed Jan 13, 2025
1 parent 1355ef2 commit d1990d9
Show file tree
Hide file tree
Showing 20 changed files with 64 additions and 70 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ void SetCordVariables(
"\"", absl::CEscape(descriptor->default_value_string()), "\"");
(*variables)["default_length"] =
absl::StrCat(descriptor->default_value_string().length());
(*variables)["full_name"] = descriptor->full_name();
(*variables)["full_name"] = std::string(descriptor->full_name());
// For one of Cords
(*variables)["default_variable_name"] = MakeDefaultName(descriptor);
(*variables)["default_variable_field"] = MakeDefaultFieldName(descriptor);
Expand Down
4 changes: 2 additions & 2 deletions src/google/protobuf/compiler/cpp/service.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ class ServiceGenerator {
const absl::flat_hash_map<absl::string_view, std::string>& vars,
const Options& options)
: descriptor_(descriptor), options_(&options), vars_(vars) {
vars_["classname"] = descriptor_->name();
vars_["full_name"] = descriptor_->full_name();
vars_["classname"] = std::string(descriptor_->name());
vars_["full_name"] = std::string(descriptor_->full_name());
}

ServiceGenerator(const ServiceGenerator&) = delete;
Expand Down
2 changes: 1 addition & 1 deletion src/google/protobuf/compiler/csharp/csharp_field_base.cc
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ void FieldGeneratorBase::SetCommonFieldVariables(
(*variables)["type_name"] = type_name();
(*variables)["extended_type"] = GetClassName(descriptor_->containing_type());
(*variables)["name"] = name();
(*variables)["descriptor_name"] = descriptor_->name();
(*variables)["descriptor_name"] = std::string(descriptor_->name());
(*variables)["default_value"] = default_value();
(*variables)["capitalized_type_name"] = capitalized_type_name();
(*variables)["number"] = number();
Expand Down
2 changes: 1 addition & 1 deletion src/google/protobuf/compiler/csharp/csharp_message.cc
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ void MessageGenerator::Generate(io::Printer* printer) {
const OneofDescriptor* oneof = descriptor_->oneof_decl(i);
vars["name"] = UnderscoresToCamelCase(oneof->name(), false);
vars["property_name"] = UnderscoresToCamelCase(oneof->name(), true);
vars["original_name"] = oneof->name();
vars["original_name"] = std::string(oneof->name());
printer->Print(vars,
"private object $name$_;\n"
"/// <summary>Enum of possible cases for the "
Expand Down
2 changes: 1 addition & 1 deletion src/google/protobuf/compiler/java/doc_comment.cc
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ static void WriteDebugString(io::Printer* printer, const FieldDescriptor* field,
const Options options, const bool kdoc) {
std::string field_comment = FirstLineOf(field->DebugString());
if (options.strip_nonfunctional_codegen) {
field_comment = field->name();
field_comment = std::string(field->name());
}
if (kdoc) {
printer->Print(" * `$def$`\n", "def", EscapeKdoc(field_comment));
Expand Down
5 changes: 3 additions & 2 deletions src/google/protobuf/compiler/java/field_common.cc
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,10 @@ namespace java {
void SetCommonFieldVariables(
const FieldDescriptor* descriptor, const FieldGeneratorInfo* info,
absl::flat_hash_map<absl::string_view, std::string>* variables) {
(*variables)["field_name"] = descriptor->name();
(*variables)["field_name"] = std::string(descriptor->name());
(*variables)["name"] = info->name;
(*variables)["classname"] = descriptor->containing_type()->name();
(*variables)["classname"] =
std::string(descriptor->containing_type()->name());
(*variables)["capitalized_name"] = info->capitalized_name;
(*variables)["disambiguated_reason"] = info->disambiguated_reason;
(*variables)["constant_name"] = FieldConstantName(descriptor);
Expand Down
9 changes: 4 additions & 5 deletions src/google/protobuf/compiler/java/file.cc
Original file line number Diff line number Diff line change
Expand Up @@ -205,12 +205,11 @@ bool FileGenerator::Validate(std::string* error) {
// end up overwriting the outer class with one of the inner ones.
if (name_resolver_->HasConflictingClassName(file_, classname_,
NameEquality::EXACT_EQUAL)) {
error->assign(file_->name());
error->append(
*error = absl::StrCat(
file_->name(),
": Cannot generate Java output because the file's outer class name, "
"\"");
error->append(classname_);
error->append(
"\"",
classname_,
"\", matches the name of one of the types declared inside it. "
"Please either rename the type or use the java_outer_classname "
"option to specify a different outer class name for the .proto file.");
Expand Down
10 changes: 5 additions & 5 deletions src/google/protobuf/compiler/java/full/enum.cc
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ void EnumNonLiteGenerator::Generate(io::Printer* printer) {

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

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

for (int i = 0; i < descriptor_->value_count(); i++) {
absl::flat_hash_map<absl::string_view, std::string> vars;
vars["name"] = descriptor_->value(i)->name();
vars["name"] = std::string(descriptor_->value(i)->name());
vars["number"] = absl::StrCat(descriptor_->value(i)->number());
vars["{"] = "";
vars["}"] = "";
Expand Down
2 changes: 1 addition & 1 deletion src/google/protobuf/compiler/java/full/message.cc
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,7 @@ void ImmutableMessageGenerator::Generate(io::Printer* printer) {

absl::flat_hash_map<absl::string_view, std::string> variables;
variables["static"] = is_own_file ? "" : "static ";
variables["classname"] = descriptor_->name();
variables["classname"] = std::string(descriptor_->name());
variables["extra_interfaces"] = ExtraMessageInterfaces(descriptor_);
variables["deprecation"] =
descriptor_->options().deprecated() ? "@java.lang.Deprecated " : "";
Expand Down
10 changes: 5 additions & 5 deletions src/google/protobuf/compiler/java/lite/enum.cc
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ void EnumLiteGenerator::Generate(io::Printer* printer) {

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

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

for (int i = 0; i < descriptor_->value_count(); i++) {
absl::flat_hash_map<absl::string_view, std::string> vars;
vars["name"] = descriptor_->value(i)->name();
vars["name"] = std::string(descriptor_->value(i)->name());
vars["number"] = absl::StrCat(descriptor_->value(i)->number());
vars["{"] = "";
vars["}"] = "";
Expand Down
2 changes: 1 addition & 1 deletion src/google/protobuf/compiler/java/lite/message.cc
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ void ImmutableMessageLiteGenerator::Generate(io::Printer* printer) {
absl::flat_hash_map<absl::string_view, std::string> variables = {{"{", ""},
{"}", ""}};
variables["static"] = is_own_file ? " " : " static ";
variables["classname"] = descriptor_->name();
variables["classname"] = std::string(descriptor_->name());
variables["extra_interfaces"] = ExtraMessageInterfaces(descriptor_);
variables["deprecation"] =
descriptor_->options().deprecated() ? "@java.lang.Deprecated " : "";
Expand Down
4 changes: 2 additions & 2 deletions src/google/protobuf/compiler/java/name_resolver.cc
Original file line number Diff line number Diff line change
Expand Up @@ -142,9 +142,9 @@ std::string ClassNameResolver::GetFileDefaultImmutableClassName(
std::string basename;
std::string::size_type last_slash = file->name().find_last_of('/');
if (last_slash == std::string::npos) {
basename = file->name();
basename = std::string(file->name());
} else {
basename = file->name().substr(last_slash + 1);
basename = std::string(file->name().substr(last_slash + 1));
}
// foo_bar_baz.proto -> FooBarBaz
std::string ret = UnderscoresToCamelCase(StripProto(basename), true);
Expand Down
6 changes: 3 additions & 3 deletions src/google/protobuf/compiler/java/names.cc
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,9 @@ std::string FieldName(const FieldDescriptor* field) {
// of the group type. In Java, though, we would like to retain the original
// capitalization of the type name.
if (internal::cpp::IsGroupLike(*field)) {
field_name = field->message_type()->name();
field_name = std::string(field->message_type()->name());
} else {
field_name = field->name();
field_name = std::string(field->name());
}
if (IsForbidden(field_name)) {
// Append a trailing "#" to indicate that the name should be decorated to
Expand Down Expand Up @@ -130,7 +130,7 @@ std::string FileJavaPackage(const FileDescriptor* file, bool immutable,
result = DefaultPackage(options);
if (!file->package().empty()) {
if (!result.empty()) result += '.';
result += file->package();
absl::StrAppend(&result, file->package());
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/google/protobuf/compiler/objectivec/field.cc
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,9 @@ void SetCommonFieldVariables(
std::string camel_case_name = FieldName(descriptor);
std::string raw_field_name;
if (internal::cpp::IsGroupLike(*descriptor)) {
raw_field_name = descriptor->message_type()->name();
raw_field_name = std::string(descriptor->message_type()->name());
} else {
raw_field_name = descriptor->name();
raw_field_name = std::string(descriptor->name());
}
// The logic here has to match -[GGPBFieldDescriptor textFormatName].
const std::string un_camel_case_name(
Expand Down
38 changes: 19 additions & 19 deletions src/google/protobuf/compiler/python/generator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -562,8 +562,8 @@ void Generator::PrintResolvedFeatures() const {
void Generator::PrintFileDescriptor() const {
absl::flat_hash_map<absl::string_view, std::string> m;
m["descriptor_name"] = kDescriptorKey;
m["name"] = file_->name();
m["package"] = file_->package();
m["name"] = std::string(file_->name());
m["package"] = std::string(file_->package());
m["syntax"] = GetLegacySyntaxName(GetEdition(*file_));
m["edition"] = Edition_Name(GetEdition(*file_));
m["options"] = OptionsValue(proto_.options().SerializeAsString());
Expand Down Expand Up @@ -645,8 +645,8 @@ void Generator::PrintEnum(const EnumDescriptor& enum_descriptor,
std::string module_level_descriptor_name =
ModuleLevelDescriptorName(enum_descriptor);
m["descriptor_name"] = module_level_descriptor_name;
m["name"] = enum_descriptor.name();
m["full_name"] = enum_descriptor.full_name();
m["name"] = std::string(enum_descriptor.name());
m["full_name"] = std::string(enum_descriptor.full_name());
m["file"] = kDescriptorKey;
const char enum_descriptor_template[] =
"$descriptor_name$ = _descriptor.EnumDescriptor(\n"
Expand Down Expand Up @@ -720,7 +720,7 @@ void Generator::PrintServiceDescriptor(
const ServiceDescriptor& descriptor) const {
absl::flat_hash_map<absl::string_view, std::string> m;
m["service_name"] = ModuleLevelServiceDescriptorName(descriptor);
m["name"] = descriptor.name();
m["name"] = std::string(descriptor.name());
m["file"] = kDescriptorKey;
printer_->Print(m, "$service_name$ = $file$.services_by_name['$name$']\n");
}
Expand Down Expand Up @@ -770,8 +770,8 @@ void Generator::PrintServiceStub(const ServiceDescriptor& descriptor) const {
void Generator::PrintDescriptor(const Descriptor& message_descriptor,
const DescriptorProto& proto) const {
absl::flat_hash_map<absl::string_view, std::string> m;
m["name"] = message_descriptor.name();
m["full_name"] = message_descriptor.full_name();
m["name"] = std::string(message_descriptor.name());
m["full_name"] = std::string(message_descriptor.full_name());
m["file"] = kDescriptorKey;

PrintNestedDescriptors(message_descriptor, proto);
Expand Down Expand Up @@ -836,8 +836,8 @@ void Generator::PrintDescriptor(const Descriptor& message_descriptor,
for (int i = 0; i < message_descriptor.oneof_decl_count(); ++i) {
const OneofDescriptor* desc = message_descriptor.oneof_decl(i);
m.clear();
m["name"] = desc->name();
m["full_name"] = desc->full_name();
m["name"] = std::string(desc->name());
m["full_name"] = std::string(desc->full_name());
m["index"] = absl::StrCat(desc->index());
options_string =
OptionsValue(proto.oneof_decl(i).options().SerializeAsString());
Expand Down Expand Up @@ -980,9 +980,9 @@ void Generator::FixForeignFieldsInDescriptor(
absl::flat_hash_map<absl::string_view, std::string> m;
const OneofDescriptor* oneof = descriptor.oneof_decl(i);
m["descriptor_name"] = ModuleLevelDescriptorName(descriptor);
m["oneof_name"] = oneof->name();
m["oneof_name"] = std::string(oneof->name());
for (int j = 0; j < oneof->field_count(); ++j) {
m["field_name"] = oneof->field(j)->name();
m["field_name"] = std::string(oneof->field(j)->name());
printer_->Print(
m,
"$descriptor_name$.oneofs_by_name['$oneof_name$'].fields.append(\n"
Expand All @@ -998,7 +998,7 @@ void Generator::FixForeignFieldsInDescriptor(
void Generator::AddMessageToFileDescriptor(const Descriptor& descriptor) const {
absl::flat_hash_map<absl::string_view, std::string> m;
m["descriptor_name"] = kDescriptorKey;
m["message_name"] = descriptor.name();
m["message_name"] = std::string(descriptor.name());
m["message_descriptor_name"] = ModuleLevelDescriptorName(descriptor);
const char file_descriptor_template[] =
"$descriptor_name$.message_types_by_name['$message_name$'] = "
Expand All @@ -1010,7 +1010,7 @@ void Generator::AddServiceToFileDescriptor(
const ServiceDescriptor& descriptor) const {
absl::flat_hash_map<absl::string_view, std::string> m;
m["descriptor_name"] = kDescriptorKey;
m["service_name"] = descriptor.name();
m["service_name"] = std::string(descriptor.name());
m["service_descriptor_name"] = ModuleLevelServiceDescriptorName(descriptor);
const char file_descriptor_template[] =
"$descriptor_name$.services_by_name['$service_name$'] = "
Expand All @@ -1022,7 +1022,7 @@ void Generator::AddEnumToFileDescriptor(
const EnumDescriptor& descriptor) const {
absl::flat_hash_map<absl::string_view, std::string> m;
m["descriptor_name"] = kDescriptorKey;
m["enum_name"] = descriptor.name();
m["enum_name"] = std::string(descriptor.name());
m["enum_descriptor_name"] = ModuleLevelDescriptorName(descriptor);
const char file_descriptor_template[] =
"$descriptor_name$.enum_types_by_name['$enum_name$'] = "
Expand All @@ -1034,7 +1034,7 @@ void Generator::AddExtensionToFileDescriptor(
const FieldDescriptor& descriptor) const {
absl::flat_hash_map<absl::string_view, std::string> m;
m["descriptor_name"] = kDescriptorKey;
m["field_name"] = descriptor.name();
m["field_name"] = std::string(descriptor.name());
m["resolved_name"] = ResolveKeyword(descriptor.name());
const char file_descriptor_template[] =
"$descriptor_name$.extensions_by_name['$field_name$'] = "
Expand Down Expand Up @@ -1143,7 +1143,7 @@ void Generator::PrintEnumValueDescriptor(
std::string options_string;
proto.options().SerializeToString(&options_string);
absl::flat_hash_map<absl::string_view, std::string> m;
m["name"] = descriptor.name();
m["name"] = std::string(descriptor.name());
m["index"] = absl::StrCat(descriptor.index());
m["number"] = absl::StrCat(descriptor.number());
m["options"] = OptionsValue(options_string);
Expand All @@ -1161,8 +1161,8 @@ void Generator::PrintFieldDescriptor(const FieldDescriptor& field,
std::string options_string;
proto.options().SerializeToString(&options_string);
absl::flat_hash_map<absl::string_view, std::string> m;
m["name"] = field.name();
m["full_name"] = field.full_name();
m["name"] = std::string(field.name());
m["full_name"] = std::string(field.full_name());
m["index"] = absl::StrCat(field.index());
m["number"] = absl::StrCat(field.number());
m["type"] = absl::StrCat(field.type());
Expand Down Expand Up @@ -1456,7 +1456,7 @@ void Generator::FixOptionsForField(const FieldDescriptor& field,
if (field.is_extension()) {
if (field.extension_scope() == nullptr) {
// Top level extensions.
field_name = field.name();
field_name = std::string(field.name());
} else {
field_name = FieldReferencingExpression(field.extension_scope(), field,
"extensions_by_name");
Expand Down
2 changes: 1 addition & 1 deletion src/google/protobuf/compiler/ruby/ruby_generator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ int GeneratePackageModules(const FileDescriptor* file, io::Printer* printer) {
<< " 'A::B::C' and not 'A.B.C'";
}
} else {
package_name = file->package();
package_name = std::string(file->package());
}

// Use the appropriate delimiter
Expand Down
Loading

0 comments on commit d1990d9

Please sign in to comment.