Automated rollback of commit 08b97c3120.

PiperOrigin-RevId: 503277235
pull/11600/head
Mike Kruskal 2 years ago committed by Copybara-Service
parent 5b27b4f300
commit 119ea4ca9e
  1. 40
      src/google/protobuf/compiler/java/context.cc
  2. 6
      src/google/protobuf/compiler/java/enum_field.cc
  3. 4
      src/google/protobuf/compiler/java/enum_field_lite.cc
  4. 10
      src/google/protobuf/compiler/java/extension.cc
  5. 27
      src/google/protobuf/compiler/java/field.cc
  6. 24
      src/google/protobuf/compiler/java/file.cc
  7. 58
      src/google/protobuf/compiler/java/generator.cc
  8. 136
      src/google/protobuf/compiler/java/helpers.cc
  9. 23
      src/google/protobuf/compiler/java/helpers.h
  10. 9
      src/google/protobuf/compiler/java/kotlin_generator.cc
  11. 20
      src/google/protobuf/compiler/java/map_field.cc
  12. 11
      src/google/protobuf/compiler/java/map_field_lite.cc
  13. 4
      src/google/protobuf/compiler/java/message_field.cc
  14. 4
      src/google/protobuf/compiler/java/message_field_lite.cc
  15. 60
      src/google/protobuf/compiler/java/name_resolver.cc
  16. 10
      src/google/protobuf/compiler/java/name_resolver.h
  17. 6
      src/google/protobuf/compiler/java/names.cc
  18. 4
      src/google/protobuf/compiler/java/names.h
  19. 31
      src/google/protobuf/compiler/java/plugin_unittest.cc
  20. 24
      src/google/protobuf/compiler/java/primitive_field.cc
  21. 22
      src/google/protobuf/compiler/java/primitive_field_lite.cc
  22. 8
      src/google/protobuf/compiler/java/shared_code_generator.cc
  23. 29
      src/google/protobuf/compiler/java/string_field.cc
  24. 10
      src/google/protobuf/compiler/java/string_field_lite.cc
  25. 45
      src/google/protobuf/descriptor.cc
  26. 2
      src/google/protobuf/extension_set_unittest.cc
  27. 5
      src/google/protobuf/test_util2.h
  28. 9
      src/google/protobuf/text_format.cc

@ -30,8 +30,12 @@
#include "google/protobuf/compiler/java/context.h"
#include <string>
#include "google/protobuf/stubs/logging.h"
#include "absl/strings/str_cat.h"
#include "absl/strings/string_view.h"
#include "absl/strings/strip.h"
#include "google/protobuf/compiler/java/field.h"
#include "google/protobuf/compiler/java/helpers.h"
#include "google/protobuf/compiler/java/name_resolver.h"
@ -54,11 +58,17 @@ ClassNameResolver* Context::GetNameResolver() const {
}
namespace {
bool EqualWithSuffix(absl::string_view name1, absl::string_view suffix,
absl::string_view name2) {
if (!absl::ConsumeSuffix(&name2, suffix)) return false;
return name1 == name2;
}
// Whether two fields have conflicting accessors (assuming name1 and name2
// are different). name1 and name2 are field1 and field2's camel-case name
// respectively.
bool IsConflicting(const FieldDescriptor* field1, const std::string& name1,
const FieldDescriptor* field2, const std::string& name2,
bool IsConflicting(const FieldDescriptor* field1, absl::string_view name1,
const FieldDescriptor* field2, absl::string_view name2,
std::string* info) {
if (field1->is_repeated()) {
if (field2->is_repeated()) {
@ -66,16 +76,18 @@ bool IsConflicting(const FieldDescriptor* field1, const std::string& name1,
return false;
} else {
// field1 is repeated, and field2 is not.
if (name1 + "Count" == name2) {
*info = "both repeated field \"" + field1->name() + "\" and singular " +
"field \"" + field2->name() + "\" generate the method \"" +
"get" + name1 + "Count()\"";
if (EqualWithSuffix(name1, "Count", name2)) {
*info = absl::StrCat("both repeated field \"", field1->name(),
"\" and singular ", "field \"", field2->name(),
"\" generate the method \"", "get", name1,
"Count()\"");
return true;
}
if (name1 + "List" == name2) {
*info = "both repeated field \"" + field1->name() + "\" and singular " +
"field \"" + field2->name() + "\" generate the method \"" +
"get" + name1 + "List()\"";
if (EqualWithSuffix(name1, "List", name2)) {
*info =
absl::StrCat("both repeated field \"", field1->name(),
"\" and singular ", "field \"", field2->name(),
"\" generate the method \"", "get", name1, "List()\"");
return true;
}
// Well, there are obviously many more conflicting cases, but it probably
@ -138,8 +150,8 @@ void Context::InitializeFieldGeneratorInfoForFields(
if (name == other_name) {
is_conflict[i] = is_conflict[j] = true;
conflict_reason[i] = conflict_reason[j] =
"capitalized name of field \"" + field->name() +
"\" conflicts with field \"" + other->name() + "\"";
absl::StrCat("capitalized name of field \"", field->name(),
"\" conflicts with field \"", other->name(), "\"");
} else if (IsConflicting(field, name, other, other_name,
&conflict_reason[j])) {
is_conflict[i] = is_conflict[j] = true;
@ -160,8 +172,8 @@ void Context::InitializeFieldGeneratorInfoForFields(
// For fields conflicting with some other fields, we append the field
// number to their field names in generated code to avoid conflicts.
if (is_conflict[i]) {
info.name += absl::StrCat(field->number());
info.capitalized_name += absl::StrCat(field->number());
absl::StrAppend(&info.name, field->number());
absl::StrAppend(&info.capitalized_name, field->number());
info.disambiguated_reason = conflict_reason[i];
}
field_generator_info_map_[field] = info;

@ -93,7 +93,7 @@ void SetEnumVariables(
(*variables)["get_has_field_bit_message"] = GenerateGetBit(messageBitIndex);
// Note that these have a trailing ";".
(*variables)["set_has_field_bit_message"] =
GenerateSetBit(messageBitIndex) + ";";
absl::StrCat(GenerateSetBit(messageBitIndex), ";");
(*variables)["set_has_field_bit_to_local"] =
GenerateSetBitToLocal(messageBitIndex);
(*variables)["is_field_present_message"] = GenerateGetBit(messageBitIndex);
@ -114,9 +114,9 @@ void SetEnumVariables(
// Note that these have a trailing ";".
(*variables)["set_has_field_bit_builder"] =
GenerateSetBit(builderBitIndex) + ";";
absl::StrCat(GenerateSetBit(builderBitIndex), ";");
(*variables)["clear_has_field_bit_builder"] =
GenerateClearBit(builderBitIndex) + ";";
absl::StrCat(GenerateClearBit(builderBitIndex), ";");
(*variables)["get_has_field_bit_from_local"] =
GenerateGetBitFromLocal(builderBitIndex);

@ -109,9 +109,9 @@ void SetEnumVariables(
// Note that these have a trailing ";".
(*variables)["set_has_field_bit_message"] =
GenerateSetBit(messageBitIndex) + ";";
absl::StrCat(GenerateSetBit(messageBitIndex), ";");
(*variables)["clear_has_field_bit_message"] =
GenerateClearBit(messageBitIndex) + ";";
absl::StrCat(GenerateClearBit(messageBitIndex), ";");
(*variables)["is_field_present_message"] = GenerateGetBit(messageBitIndex);
} else {

@ -83,7 +83,7 @@ void ExtensionGenerator::InitTemplateVars(
? ""
: DefaultValue(descriptor, immutable, name_resolver,
context->options());
vars["type_constant"] = FieldTypeName(GetType(descriptor));
vars["type_constant"] = std::string(FieldTypeName(GetType(descriptor)));
vars["packed"] = descriptor->is_packed() ? "true" : "false";
vars["enum_map"] = "null";
vars["prototype"] = "null";
@ -94,12 +94,12 @@ void ExtensionGenerator::InitTemplateVars(
case JAVATYPE_MESSAGE:
singular_type =
name_resolver->GetClassName(descriptor->message_type(), immutable);
vars["prototype"] = singular_type + ".getDefaultInstance()";
vars["prototype"] = absl::StrCat(singular_type, ".getDefaultInstance()");
break;
case JAVATYPE_ENUM:
singular_type =
name_resolver->GetClassName(descriptor->enum_type(), immutable);
vars["enum_map"] = singular_type + ".internalGetValueMap()";
vars["enum_map"] = absl::StrCat(singular_type, ".internalGetValueMap()");
break;
case JAVATYPE_STRING:
singular_type = "java.lang.String";
@ -108,11 +108,11 @@ void ExtensionGenerator::InitTemplateVars(
singular_type = immutable ? "com.google.protobuf.ByteString" : "byte[]";
break;
default:
singular_type = BoxedPrimitiveTypeName(java_type);
singular_type = std::string(BoxedPrimitiveTypeName(java_type));
break;
}
vars["type"] = descriptor->is_repeated()
? "java.util.List<" + singular_type + ">"
? absl::StrCat("java.util.List<", singular_type, ">")
: singular_type;
vars["singular_type"] = singular_type;
}

@ -259,24 +259,26 @@ void SetCommonFieldVariables(
// empty string.
(*variables)["{"] = "";
(*variables)["}"] = "";
(*variables)["kt_name"] =
IsForbiddenKotlin(info->name) ? info->name + "_" : info->name;
(*variables)["kt_capitalized_name"] = IsForbiddenKotlin(info->name)
? info->capitalized_name + "_"
: info->capitalized_name;
(*variables)["kt_name"] = IsForbiddenKotlin(info->name)
? absl::StrCat(info->name, "_")
: info->name;
(*variables)["kt_capitalized_name"] =
IsForbiddenKotlin(info->name) ? absl::StrCat(info->capitalized_name, "_")
: info->capitalized_name;
if (!descriptor->is_repeated()) {
(*variables)["annotation_field_type"] = FieldTypeName(descriptor->type());
(*variables)["annotation_field_type"] =
std::string(FieldTypeName(descriptor->type()));
} else if (GetJavaType(descriptor) == JAVATYPE_MESSAGE &&
IsMapEntry(descriptor->message_type())) {
(*variables)["annotation_field_type"] =
std::string(FieldTypeName(descriptor->type())) + "MAP";
absl::StrCat(FieldTypeName(descriptor->type()), "MAP");
} else {
(*variables)["annotation_field_type"] =
std::string(FieldTypeName(descriptor->type())) + "_LIST";
absl::StrCat(FieldTypeName(descriptor->type()), "_LIST");
if (descriptor->is_packed()) {
variables->insert(
{"annotation_field_type",
absl::StrCat((*variables)["annotation_field_type"], "_PACKED")});
absl::StrCat(FieldTypeName(descriptor->type()), "_LIST_PACKED")});
}
}
}
@ -290,10 +292,11 @@ void SetCommonOneofVariables(
absl::StrCat(descriptor->containing_oneof()->index());
(*variables)["oneof_stored_type"] = GetOneofStoredType(descriptor);
(*variables)["set_oneof_case_message"] =
info->name + "Case_ = " + absl::StrCat(descriptor->number());
(*variables)["clear_oneof_case_message"] = info->name + "Case_ = 0";
absl::StrCat(info->name, "Case_ = ", descriptor->number());
(*variables)["clear_oneof_case_message"] =
absl::StrCat(info->name, "Case_ = 0");
(*variables)["has_oneof_case_message"] =
info->name + "Case_ == " + absl::StrCat(descriptor->number());
absl::StrCat(info->name, "Case_ == ", descriptor->number());
}
void PrintExtraFieldInfo(

@ -273,7 +273,8 @@ void FileGenerator::Generate(io::Printer* printer) {
"package", java_package_);
}
PrintGeneratedAnnotation(
printer, '$', options_.annotate_code ? classname_ + ".java.pb.meta" : "",
printer, '$',
options_.annotate_code ? absl::StrCat(classname_, ".java.pb.meta") : "",
options_);
if (!options_.opensource_runtime) {
@ -552,12 +553,14 @@ void FileGenerator::GenerateDescriptorInitializationCodeForMutable(
for (const FieldDescriptor* field : extensions) {
std::string scope;
if (field->extension_scope() != NULL) {
scope = name_resolver_->GetMutableClassName(field->extension_scope()) +
".getDescriptor()";
scope = absl::StrCat(
name_resolver_->GetMutableClassName(field->extension_scope()),
".getDescriptor()");
} else {
scope = FileJavaPackage(field->file(), true, options_) + "." +
name_resolver_->GetDescriptorClassName(field->file()) +
".descriptor";
scope =
absl::StrCat(FileJavaPackage(field->file(), true, options_), ".",
name_resolver_->GetDescriptorClassName(field->file()),
".descriptor");
}
if (field->cpp_type() == FieldDescriptor::CPPTYPE_MESSAGE) {
printer->Print(
@ -606,9 +609,9 @@ static void GenerateSibling(
GeneratorClass* generator,
void (GeneratorClass::*pfn)(io::Printer* printer)) {
std::string filename =
package_dir + descriptor->name() + name_suffix + ".java";
absl::StrCat(package_dir, descriptor->name(), name_suffix, ".java");
file_list->push_back(filename);
std::string info_full_path = filename + ".pb.meta";
std::string info_full_path = absl::StrCat(filename, ".pb.meta");
GeneratedCodeInfo annotations;
io::AnnotationProtoCollector<GeneratedCodeInfo> annotation_collector(
&annotations);
@ -717,9 +720,10 @@ void FileGenerator::GenerateKotlinSiblings(
auto open_file = [context](const std::string& filename) {
return std::unique_ptr<io::ZeroCopyOutputStream>(context->Open(filename));
};
std::string filename = package_dir + descriptor->name() + "Kt.kt";
std::string filename =
absl::StrCat(package_dir, descriptor->name(), "Kt.kt");
file_list->push_back(filename);
std::string info_full_path = filename + ".pb.meta";
std::string info_full_path = absl::StrCat(filename, ".pb.meta");
GeneratedCodeInfo annotations;
io::AnnotationProtoCollector<GeneratedCodeInfo> annotation_collector(
&annotations);

@ -34,6 +34,9 @@
#include "google/protobuf/compiler/java/generator.h"
#include <utility>
#include <vector>
#include <memory>
@ -72,25 +75,25 @@ bool JavaGenerator::Generate(const FileDescriptor* file,
file_options.opensource_runtime = opensource_runtime_;
for (int i = 0; i < options.size(); i++) {
if (options[i].first == "output_list_file") {
file_options.output_list_file = options[i].second;
} else if (options[i].first == "immutable") {
for (auto& option : options) {
if (option.first == "output_list_file") {
file_options.output_list_file = option.second;
} else if (option.first == "immutable") {
file_options.generate_immutable_code = true;
} else if (options[i].first == "mutable") {
} else if (option.first == "mutable") {
file_options.generate_mutable_code = true;
} else if (options[i].first == "shared") {
} else if (option.first == "shared") {
file_options.generate_shared_code = true;
} else if (options[i].first == "lite") {
} else if (option.first == "lite") {
// Note: Java Lite does not guarantee API/ABI stability. We may choose to
// break existing API in order to boost performance / reduce code size.
file_options.enforce_lite = true;
} else if (options[i].first == "annotate_code") {
} else if (option.first == "annotate_code") {
file_options.annotate_code = true;
} else if (options[i].first == "annotation_list_file") {
file_options.annotation_list_file = options[i].second;
} else if (option.first == "annotation_list_file") {
file_options.annotation_list_file = option.second;
} else {
*error = "Unknown generator option: " + options[i].first;
*error = absl::StrCat("Unknown generator option: ", option.first);
return false;
}
}
@ -115,35 +118,31 @@ bool JavaGenerator::Generate(const FileDescriptor* file,
std::vector<std::string> all_annotations;
std::vector<FileGenerator*> file_generators;
std::vector<std::unique_ptr<FileGenerator>> file_generators;
if (file_options.generate_immutable_code) {
file_generators.push_back(new FileGenerator(file, file_options,
/* immutable = */ true));
file_generators.emplace_back(
std::make_unique<FileGenerator>(file, file_options,
/* immutable = */ true));
}
if (file_options.generate_mutable_code) {
file_generators.push_back(new FileGenerator(file, file_options,
/* mutable = */ false));
file_generators.emplace_back(
std::make_unique<FileGenerator>(file, file_options,
/* mutable = */ false));
}
for (int i = 0; i < file_generators.size(); ++i) {
if (!file_generators[i]->Validate(error)) {
for (int j = 0; j < file_generators.size(); ++j) {
delete file_generators[j];
}
for (auto& file_generator : file_generators) {
if (!file_generator->Validate(error)) {
return false;
}
}
for (int i = 0; i < file_generators.size(); ++i) {
FileGenerator* file_generator = file_generators[i];
for (auto& file_generator : file_generators) {
std::string package_dir = JavaPackageToDir(file_generator->java_package());
std::string java_filename = package_dir;
java_filename += file_generator->classname();
java_filename += ".java";
std::string java_filename =
absl::StrCat(package_dir, file_generator->classname(), ".java");
all_files.push_back(java_filename);
std::string info_full_path = java_filename + ".pb.meta";
std::string info_full_path = absl::StrCat(java_filename, ".pb.meta");
if (file_options.annotate_code) {
all_annotations.push_back(info_full_path);
}
@ -172,9 +171,6 @@ bool JavaGenerator::Generate(const FileDescriptor* file,
}
for (int i = 0; i < file_generators.size(); ++i) {
delete file_generators[i];
}
file_generators.clear();
// Generate output list if requested.

@ -72,7 +72,7 @@ const char kThinSeparator[] =
"// -------------------------------------------------------------------\n";
void PrintGeneratedAnnotation(io::Printer* printer, char delimiter,
const std::string& annotation_file,
absl::string_view annotation_file,
Options options) {
if (annotation_file.empty()) {
return;
@ -89,7 +89,8 @@ void PrintGeneratedAnnotation(io::Printer* printer, char delimiter,
void PrintEnumVerifierLogic(
io::Printer* printer, const FieldDescriptor* descriptor,
const absl::flat_hash_map<absl::string_view, std::string>& variables,
const char* var_name, const char* terminating_string, bool enforce_lite) {
absl::string_view var_name, absl::string_view terminating_string,
bool enforce_lite) {
std::string enum_verifier_string =
enforce_lite ? absl::StrCat(var_name, ".internalGetVerifier()")
: absl::StrCat(
@ -101,12 +102,11 @@ void PrintEnumVerifierLogic(
".forNumber(number) != null;\n"
" }\n"
" }");
printer->Print(
variables,
absl::StrCat(enum_verifier_string, terminating_string).c_str());
printer->Print(variables,
absl::StrCat(enum_verifier_string, terminating_string));
}
std::string UnderscoresToCamelCase(const std::string& input,
std::string UnderscoresToCamelCase(absl::string_view input,
bool cap_next_letter) {
GOOGLE_ABSL_CHECK(!input.empty());
std::string result;
@ -143,7 +143,7 @@ std::string UnderscoresToCamelCase(const std::string& input,
return result;
}
std::string ToCamelCase(const std::string& input, bool lower_first) {
std::string ToCamelCase(absl::string_view input, bool lower_first) {
bool capitalize_next = !lower_first;
std::string result;
result.reserve(input.size());
@ -185,25 +185,26 @@ bool IsForbiddenKotlin(absl::string_view field_name) {
std::string EscapeKotlinKeywords(std::string name) {
std::vector<std::string> escaped_packages;
std::vector<std::string> packages = absl::StrSplit(name, "."); // NOLINT
for (const std::string& package : packages) {
std::vector<std::string> packages = absl::StrSplit(name, "."); // NOLINT
for (absl::string_view package : packages) {
if (IsForbiddenKotlin(package)) {
escaped_packages.push_back("`" + package + "`");
escaped_packages.push_back(absl::StrCat("`", package, "`"));
} else {
escaped_packages.push_back(package);
escaped_packages.emplace_back(package);
}
}
return absl::StrJoin(escaped_packages, ".");
}
std::string UniqueFileScopeIdentifier(const Descriptor* descriptor) {
return "static_" + absl::StrReplaceAll(descriptor->full_name(), {{".", "_"}});
return absl::StrCat(
"static_", absl::StrReplaceAll(descriptor->full_name(), {{".", "_"}}));
}
std::string CamelCaseFieldName(const FieldDescriptor* field) {
std::string fieldName = UnderscoresToCamelCase(field);
if ('0' <= fieldName[0] && fieldName[0] <= '9') {
return '_' + fieldName;
return absl::StrCat("_", fieldName);
}
return fieldName;
}
@ -214,31 +215,28 @@ std::string FileClassName(const FileDescriptor* file, bool immutable) {
std::string JavaPackageToDir(std::string package_name) {
std::string package_dir = absl::StrReplaceAll(package_name, {{".", "/"}});
if (!package_dir.empty()) package_dir += "/";
if (!package_dir.empty()) absl::StrAppend(&package_dir, "/");
return package_dir;
}
std::string ExtraMessageInterfaces(const Descriptor* descriptor) {
std::string interfaces = "// @@protoc_insertion_point(message_implements:" +
descriptor->full_name() + ")";
return interfaces;
return absl::StrCat("// @@protoc_insertion_point(message_implements:",
descriptor->full_name(), ")");
}
std::string ExtraBuilderInterfaces(const Descriptor* descriptor) {
std::string interfaces = "// @@protoc_insertion_point(builder_implements:" +
descriptor->full_name() + ")";
return interfaces;
return absl::StrCat("// @@protoc_insertion_point(builder_implements:",
descriptor->full_name(), ")");
}
std::string ExtraMessageOrBuilderInterfaces(const Descriptor* descriptor) {
std::string interfaces = "// @@protoc_insertion_point(interface_extends:" +
descriptor->full_name() + ")";
return interfaces;
return absl::StrCat("// @@protoc_insertion_point(interface_extends:",
descriptor->full_name(), ")");
}
std::string FieldConstantName(const FieldDescriptor* field) {
std::string name = field->name() + "_FIELD_NUMBER";
std::string name = absl::StrCat(field->name(), "_FIELD_NUMBER");
absl::AsciiStrToUpper(&name);
return name;
}
@ -293,7 +291,7 @@ JavaType GetJavaType(const FieldDescriptor* field) {
return JAVATYPE_INT;
}
const char* PrimitiveTypeName(JavaType type) {
absl::string_view PrimitiveTypeName(JavaType type) {
switch (type) {
case JAVATYPE_INT:
return "int";
@ -310,23 +308,23 @@ const char* PrimitiveTypeName(JavaType type) {
case JAVATYPE_BYTES:
return "com.google.protobuf.ByteString";
case JAVATYPE_ENUM:
return NULL;
return {};
case JAVATYPE_MESSAGE:
return NULL;
return {};
// No default because we want the compiler to complain if any new
// JavaTypes are added.
}
GOOGLE_ABSL_LOG(FATAL) << "Can't get here.";
return NULL;
return {};
}
const char* PrimitiveTypeName(const FieldDescriptor* descriptor) {
absl::string_view PrimitiveTypeName(const FieldDescriptor* descriptor) {
return PrimitiveTypeName(GetJavaType(descriptor));
}
const char* BoxedPrimitiveTypeName(JavaType type) {
absl::string_view BoxedPrimitiveTypeName(JavaType type) {
switch (type) {
case JAVATYPE_INT:
return "java.lang.Integer";
@ -343,23 +341,23 @@ const char* BoxedPrimitiveTypeName(JavaType type) {
case JAVATYPE_BYTES:
return "com.google.protobuf.ByteString";
case JAVATYPE_ENUM:
return NULL;
return {};
case JAVATYPE_MESSAGE:
return NULL;
return {};
// No default because we want the compiler to complain if any new
// JavaTypes are added.
}
GOOGLE_ABSL_LOG(FATAL) << "Can't get here.";
return NULL;
return {};
}
const char* BoxedPrimitiveTypeName(const FieldDescriptor* descriptor) {
absl::string_view BoxedPrimitiveTypeName(const FieldDescriptor* descriptor) {
return BoxedPrimitiveTypeName(GetJavaType(descriptor));
}
const char* KotlinTypeName(JavaType type) {
absl::string_view KotlinTypeName(JavaType type) {
switch (type) {
case JAVATYPE_INT:
return "kotlin.Int";
@ -376,16 +374,16 @@ const char* KotlinTypeName(JavaType type) {
case JAVATYPE_BYTES:
return "com.google.protobuf.ByteString";
case JAVATYPE_ENUM:
return NULL;
return {};
case JAVATYPE_MESSAGE:
return NULL;
return {};
// No default because we want the compiler to complain if any new
// JavaTypes are added.
}
GOOGLE_ABSL_LOG(FATAL) << "Can't get here.";
return NULL;
return {};
}
std::string GetOneofStoredType(const FieldDescriptor* field) {
@ -396,11 +394,11 @@ std::string GetOneofStoredType(const FieldDescriptor* field) {
case JAVATYPE_MESSAGE:
return ClassNameResolver().GetClassName(field->message_type(), true);
default:
return BoxedPrimitiveTypeName(javaType);
return std::string(BoxedPrimitiveTypeName(javaType));
}
}
const char* FieldTypeName(FieldDescriptor::Type field_type) {
absl::string_view FieldTypeName(FieldDescriptor::Type field_type) {
switch (field_type) {
case FieldDescriptor::TYPE_INT32:
return "INT32";
@ -444,10 +442,10 @@ const char* FieldTypeName(FieldDescriptor::Type field_type) {
}
GOOGLE_ABSL_LOG(FATAL) << "Can't get here.";
return NULL;
return {};
}
bool AllAscii(const std::string& text) {
bool AllAscii(absl::string_view text) {
for (int i = 0; i < text.size(); i++) {
if ((text[i] & 0x80) != 0) {
return false;
@ -467,7 +465,7 @@ std::string DefaultValue(const FieldDescriptor* field, bool immutable,
// Need to print as a signed int since Java has no unsigned.
return absl::StrCat(static_cast<int32_t>(field->default_value_uint32()));
case FieldDescriptor::CPPTYPE_INT64:
return absl::StrCat(field->default_value_int64()) + "L";
return absl::StrCat(field->default_value_int64(), "L");
case FieldDescriptor::CPPTYPE_UINT64:
return absl::StrCat(static_cast<int64_t>(field->default_value_uint64())) +
"L";
@ -480,7 +478,7 @@ std::string DefaultValue(const FieldDescriptor* field, bool immutable,
} else if (value != value) {
return "Double.NaN";
} else {
return io::SimpleDtoa(value) + "D";
return absl::StrCat(io::SimpleDtoa(value), "D");
}
}
case FieldDescriptor::CPPTYPE_FLOAT: {
@ -492,7 +490,7 @@ std::string DefaultValue(const FieldDescriptor* field, bool immutable,
} else if (value != value) {
return "Float.NaN";
} else {
return io::SimpleFtoa(value) + "F";
return absl::StrCat(io::SimpleFtoa(value), "F");
}
}
case FieldDescriptor::CPPTYPE_BOOL:
@ -510,7 +508,8 @@ std::string DefaultValue(const FieldDescriptor* field, bool immutable,
} else {
if (AllAscii(field->default_value_string())) {
// All chars are ASCII. In this case CEscape() works fine.
return "\"" + absl::CEscape(field->default_value_string()) + "\"";
return absl::StrCat(
"\"", absl::CEscape(field->default_value_string()), "\"");
} else {
// See comments in Internal.java for gory details.
return absl::Substitute(
@ -520,12 +519,14 @@ std::string DefaultValue(const FieldDescriptor* field, bool immutable,
}
case FieldDescriptor::CPPTYPE_ENUM:
return name_resolver->GetClassName(field->enum_type(), immutable) + "." +
field->default_value_enum()->name();
return absl::StrCat(
name_resolver->GetClassName(field->enum_type(), immutable), ".",
field->default_value_enum()->name());
case FieldDescriptor::CPPTYPE_MESSAGE:
return name_resolver->GetClassName(field->message_type(), immutable) +
".getDefaultInstance()";
return absl::StrCat(
name_resolver->GetClassName(field->message_type(), immutable),
".getDefaultInstance()");
// No default because we want the compiler to complain if any new
// types are added.
@ -572,7 +573,7 @@ bool IsByteStringWithCustomDefaultValue(const FieldDescriptor* field) {
field->default_value_string() != "";
}
const char* bit_masks[] = {
constexpr absl::string_view bit_masks[] = {
"0x00000001", "0x00000002", "0x00000004", "0x00000008",
"0x00000010", "0x00000020", "0x00000040", "0x00000080",
@ -587,10 +588,7 @@ const char* bit_masks[] = {
};
std::string GetBitFieldName(int index) {
std::string varName = "bitField";
varName += absl::StrCat(index);
varName += "_";
return varName;
return absl::StrCat("bitField", index, "_");
}
std::string GetBitFieldNameForBit(int bitIndex) {
@ -599,22 +597,19 @@ std::string GetBitFieldNameForBit(int bitIndex) {
namespace {
std::string GenerateGetBitInternal(const std::string& prefix, int bitIndex) {
std::string varName = prefix + GetBitFieldNameForBit(bitIndex);
std::string GenerateGetBitInternal(absl::string_view prefix, int bitIndex) {
std::string varName = absl::StrCat(prefix, GetBitFieldNameForBit(bitIndex));
int bitInVarIndex = bitIndex % 32;
std::string mask = bit_masks[bitInVarIndex];
std::string result = "((" + varName + " & " + mask + ") != 0)";
return result;
return absl::StrCat("((", varName, " & ", bit_masks[bitInVarIndex],
") != 0)");
}
std::string GenerateSetBitInternal(const std::string& prefix, int bitIndex) {
std::string varName = prefix + GetBitFieldNameForBit(bitIndex);
std::string GenerateSetBitInternal(absl::string_view prefix, int bitIndex) {
std::string varName = absl::StrCat(prefix, GetBitFieldNameForBit(bitIndex));
int bitInVarIndex = bitIndex % 32;
std::string mask = bit_masks[bitInVarIndex];
std::string result = varName + " |= " + mask;
return result;
return absl::StrCat(varName, " |= ", bit_masks[bitInVarIndex]);
}
} // namespace
@ -631,9 +626,8 @@ std::string GenerateClearBit(int bitIndex) {
std::string varName = GetBitFieldNameForBit(bitIndex);
int bitInVarIndex = bitIndex % 32;
std::string mask = bit_masks[bitInVarIndex];
std::string result = varName + " = (" + varName + " & ~" + mask + ")";
return result;
return absl::StrCat(varName, " = (", varName, " & ~",
bit_masks[bitInVarIndex], ")");
}
std::string GenerateGetBitFromLocal(int bitIndex) {
@ -681,8 +675,8 @@ bool IsReferenceType(JavaType type) {
return false;
}
const char* GetCapitalizedType(const FieldDescriptor* field, bool immutable,
Options options) {
absl::string_view GetCapitalizedType(const FieldDescriptor* field,
bool immutable, Options options) {
switch (GetType(field)) {
case FieldDescriptor::TYPE_INT32:
return "Int32";
@ -727,7 +721,7 @@ const char* GetCapitalizedType(const FieldDescriptor* field, bool immutable,
}
GOOGLE_ABSL_LOG(FATAL) << "Can't get here.";
return NULL;
return {};
}
// For encodings with fixed sizes, returns that size in bytes. Otherwise

@ -69,7 +69,7 @@ bool IsForbiddenKotlin(absl::string_view field_name);
// annotation_file should be generated from the filename of the source file
// being annotated (which in turn must be a Java identifier plus ".java").
void PrintGeneratedAnnotation(io::Printer* printer, char delimiter = '$',
const std::string& annotation_file = "",
absl::string_view annotation_file = "",
Options options = {});
// If a GeneratedMessageLite contains non-lite enums, then its verifier
@ -77,11 +77,12 @@ void PrintGeneratedAnnotation(io::Printer* printer, char delimiter = '$',
void PrintEnumVerifierLogic(
io::Printer* printer, const FieldDescriptor* descriptor,
const absl::flat_hash_map<absl::string_view, std::string>& variables,
const char* var_name, const char* terminating_string, bool enforce_lite);
absl::string_view var_name, absl::string_view terminating_string,
bool enforce_lite);
// Converts a name to camel-case. If cap_first_letter is true, capitalize the
// first letter.
std::string ToCamelCase(const std::string& input, bool lower_first);
std::string ToCamelCase(absl::string_view input, bool lower_first);
// Similar to UnderscoresToCamelCase, but guarantees that the result is a
// complete Java identifier by adding a _ if needed.
@ -184,8 +185,8 @@ inline bool IsOwnFile(const ServiceDescriptor* descriptor, bool immutable) {
// (e.g.) be "OrBuilder" for some generated interfaces.
template <typename Descriptor>
std::string AnnotationFileName(const Descriptor* descriptor,
const std::string& suffix) {
return descriptor->name() + suffix + ".java.pb.meta";
absl::string_view suffix) {
return absl::StrCat(descriptor->name(), suffix, ".java.pb.meta");
}
// Get the unqualified name that should be used for a field's field
@ -211,21 +212,21 @@ enum JavaType {
JavaType GetJavaType(const FieldDescriptor* field);
const char* PrimitiveTypeName(JavaType type);
absl::string_view PrimitiveTypeName(JavaType type);
// Get the fully-qualified class name for a boxed primitive type, e.g.
// "java.lang.Integer" for JAVATYPE_INT. Returns NULL for enum and message
// types.
const char* BoxedPrimitiveTypeName(JavaType type);
absl::string_view BoxedPrimitiveTypeName(JavaType type);
// Kotlin source does not distinguish between primitives and non-primitives,
// but does use Kotlin-specific qualified types for them.
const char* KotlinTypeName(JavaType type);
absl::string_view KotlinTypeName(JavaType type);
// Get the name of the java enum constant representing this type. E.g.,
// "INT32" for FieldDescriptor::TYPE_INT32. The enum constant's full
// name is "com.google.protobuf.WireFormat.FieldType.INT32".
const char* FieldTypeName(const FieldDescriptor::Type field_type);
absl::string_view FieldTypeName(const FieldDescriptor::Type field_type);
class ClassNameResolver;
std::string DefaultValue(const FieldDescriptor* field, bool immutable,
@ -313,8 +314,8 @@ bool IsReferenceType(JavaType type);
// Returns the capitalized name for calling relative functions in
// CodedInputStream
const char* GetCapitalizedType(const FieldDescriptor* field, bool immutable,
Options options);
absl::string_view GetCapitalizedType(const FieldDescriptor* field,
bool immutable, Options options);
// For encodings with fixed sizes, returns that size in bytes. Otherwise
// returns -1.

@ -78,7 +78,7 @@ bool KotlinGenerator::Generate(const FileDescriptor* file,
} else if (option.first == "annotation_list_file") {
file_options.annotation_list_file = option.second;
} else {
*error = "Unknown generator option: " + option.first;
*error = absl::StrCat("Unknown generator option: ", option.first);
return false;
}
}
@ -101,11 +101,10 @@ bool KotlinGenerator::Generate(const FileDescriptor* file,
return std::unique_ptr<io::ZeroCopyOutputStream>(context->Open(filename));
};
std::string package_dir = JavaPackageToDir(file_generator->java_package());
std::string kotlin_filename = package_dir;
kotlin_filename += file_generator->GetKotlinClassname();
kotlin_filename += ".kt";
std::string kotlin_filename =
absl::StrCat(package_dir, file_generator->GetKotlinClassname(), ".kt");
all_files.push_back(kotlin_filename);
std::string info_full_path = kotlin_filename + ".pb.meta";
std::string info_full_path = absl::StrCat(kotlin_filename, ".pb.meta");
if (file_options.annotate_code) {
all_annotations.push_back(info_full_path);
}

@ -68,8 +68,8 @@ std::string TypeName(const FieldDescriptor* field,
} else if (GetJavaType(field) == JAVATYPE_ENUM) {
return name_resolver->GetImmutableClassName(field->enum_type());
} else {
return boxed ? BoxedPrimitiveTypeName(GetJavaType(field))
: PrimitiveTypeName(GetJavaType(field));
return std::string(boxed ? BoxedPrimitiveTypeName(GetJavaType(field))
: PrimitiveTypeName(GetJavaType(field)));
}
}
@ -80,13 +80,13 @@ std::string KotlinTypeName(const FieldDescriptor* field,
} else if (GetJavaType(field) == JAVATYPE_ENUM) {
return name_resolver->GetImmutableClassName(field->enum_type());
} else {
return KotlinTypeName(GetJavaType(field));
return std::string(KotlinTypeName(GetJavaType(field)));
}
}
std::string WireType(const FieldDescriptor* field) {
return "com.google.protobuf.WireFormat.FieldType." +
std::string(FieldTypeName(field->type()));
return absl::StrCat("com.google.protobuf.WireFormat.FieldType.",
FieldTypeName(field->type()));
}
void SetMessageVariables(
@ -190,18 +190,18 @@ void SetMessageVariables(
{"default_entry", absl::StrCat((*variables)["capitalized_name"],
"DefaultEntryHolder.defaultEntry")});
variables->insert({"map_field_parameter", (*variables)["default_entry"]});
(*variables)["descriptor"] =
name_resolver->GetImmutableClassName(descriptor->file()) + ".internal_" +
UniqueFileScopeIdentifier(descriptor->message_type()) + "_descriptor, ";
(*variables)["descriptor"] = absl::StrCat(
name_resolver->GetImmutableClassName(descriptor->file()), ".internal_",
UniqueFileScopeIdentifier(descriptor->message_type()), "_descriptor, ");
(*variables)["ver"] = GeneratedCodeVersionSuffix();
(*variables)["get_has_field_bit_builder"] = GenerateGetBit(builderBitIndex);
(*variables)["get_has_field_bit_from_local"] =
GenerateGetBitFromLocal(builderBitIndex);
(*variables)["set_has_field_bit_builder"] =
GenerateSetBit(builderBitIndex) + ";";
absl::StrCat(GenerateSetBit(builderBitIndex), ";");
(*variables)["clear_has_field_bit_builder"] =
GenerateClearBit(builderBitIndex) + ";";
absl::StrCat(GenerateClearBit(builderBitIndex), ";");
}
} // namespace

@ -31,6 +31,7 @@
#include "google/protobuf/compiler/java/map_field_lite.h"
#include <cstdint>
#include <string>
#include "google/protobuf/compiler/java/context.h"
#include "google/protobuf/compiler/java/doc_comment.h"
@ -69,8 +70,8 @@ std::string TypeName(const FieldDescriptor* field,
} else if (GetJavaType(field) == JAVATYPE_ENUM) {
return name_resolver->GetImmutableClassName(field->enum_type());
} else {
return boxed ? BoxedPrimitiveTypeName(GetJavaType(field))
: PrimitiveTypeName(GetJavaType(field));
return std::string(boxed ? BoxedPrimitiveTypeName(GetJavaType(field))
: PrimitiveTypeName(GetJavaType(field)));
}
}
@ -81,13 +82,13 @@ std::string KotlinTypeName(const FieldDescriptor* field,
} else if (GetJavaType(field) == JAVATYPE_ENUM) {
return name_resolver->GetImmutableClassName(field->enum_type());
} else {
return KotlinTypeName(GetJavaType(field));
return std::string(KotlinTypeName(GetJavaType(field)));
}
}
std::string WireType(const FieldDescriptor* field) {
return "com.google.protobuf.WireFormat.FieldType." +
std::string(FieldTypeName(field->type()));
return absl::StrCat("com.google.protobuf.WireFormat.FieldType.",
FieldTypeName(field->type()));
}
void SetMessageVariables(

@ -111,9 +111,9 @@ void SetMessageVariables(
(*variables)["get_has_field_bit_builder"] = GenerateGetBit(builderBitIndex);
(*variables)["set_has_field_bit_builder"] =
GenerateSetBit(builderBitIndex) + ";";
absl::StrCat(GenerateSetBit(builderBitIndex), ";");
(*variables)["clear_has_field_bit_builder"] =
GenerateClearBit(builderBitIndex) + ";";
absl::StrCat(GenerateClearBit(builderBitIndex), ";");
(*variables)["get_has_field_bit_from_local"] =
GenerateGetBitFromLocal(builderBitIndex);
}

@ -88,9 +88,9 @@ void SetMessageVariables(
// Note that these have a trailing ";".
(*variables)["set_has_field_bit_message"] =
GenerateSetBit(messageBitIndex) + ";";
absl::StrCat(GenerateSetBit(messageBitIndex), ";");
(*variables)["clear_has_field_bit_message"] =
GenerateClearBit(messageBitIndex) + ";";
absl::StrCat(GenerateClearBit(messageBitIndex), ";");
(*variables)["is_field_present_message"] = GenerateGetBit(messageBitIndex);
} else {

@ -59,8 +59,8 @@ const char* kOuterClassNameSuffix = "OuterClass";
// Full name : foo.Bar.Baz
// Package name: foo
// After strip : Bar.Baz
std::string StripPackageName(const std::string& full_name,
const FileDescriptor* file) {
absl::string_view StripPackageName(absl::string_view full_name,
const FileDescriptor* file) {
if (file->package().empty()) {
return full_name;
} else {
@ -72,7 +72,8 @@ std::string StripPackageName(const std::string& full_name,
// Get the name of a message's Java class without package name prefix.
std::string ClassNameWithoutPackage(const Descriptor* descriptor,
bool immutable) {
return StripPackageName(descriptor->full_name(), descriptor->file());
return std::string(
StripPackageName(descriptor->full_name(), descriptor->file()));
}
std::string ClassNameWithoutPackageKotlin(const Descriptor* descriptor) {
@ -80,7 +81,7 @@ std::string ClassNameWithoutPackageKotlin(const Descriptor* descriptor) {
const Descriptor* temp = descriptor->containing_type();
while (temp) {
result = temp->name() + "Kt." + result;
result = absl::StrCat(temp->name(), "Kt.", result);
temp = temp->containing_type();
}
return result;
@ -94,23 +95,23 @@ std::string ClassNameWithoutPackage(const EnumDescriptor* descriptor,
if (message_descriptor == NULL) {
return descriptor->name();
} else {
return ClassNameWithoutPackage(message_descriptor, immutable) + "." +
descriptor->name();
return absl::StrCat(ClassNameWithoutPackage(message_descriptor, immutable),
".", descriptor->name());
}
}
// Get the name of a service's Java class without package name prefix.
std::string ClassNameWithoutPackage(const ServiceDescriptor* descriptor,
bool immutable) {
std::string full_name =
absl::string_view full_name =
StripPackageName(descriptor->full_name(), descriptor->file());
// We don't allow nested service definitions.
GOOGLE_ABSL_CHECK(full_name.find('.') == std::string::npos);
return full_name;
GOOGLE_ABSL_CHECK(!absl::StrContains(full_name, '.'));
return std::string(full_name);
}
// Return true if a and b are equals (case insensitive).
NameEquality CheckNameEquality(const std::string& a, const std::string& b) {
NameEquality CheckNameEquality(absl::string_view a, absl::string_view b) {
if (absl::AsciiStrToUpper(a) == absl::AsciiStrToUpper(b)) {
if (a == b) {
return NameEquality::EXACT_EQUAL;
@ -122,7 +123,7 @@ NameEquality CheckNameEquality(const std::string& a, const std::string& b) {
// Check whether a given message or its nested types has the given class name.
bool MessageHasConflictingClassName(const Descriptor* message,
const std::string& classname,
absl::string_view classname,
NameEquality equality_mode) {
if (CheckNameEquality(message->name(), classname) == equality_mode) {
return true;
@ -181,18 +182,18 @@ std::string ClassNameResolver::GetFileClassName(const FileDescriptor* file,
std::string ClassNameResolver::GetFileClassName(const FileDescriptor* file,
bool immutable, bool kotlin) {
if (kotlin) {
return GetFileImmutableClassName(file) + "Kt";
return absl::StrCat(GetFileImmutableClassName(file), "Kt");
} else if (immutable) {
return GetFileImmutableClassName(file);
} else {
return "Mutable" + GetFileImmutableClassName(file);
return absl::StrCat("Mutable", GetFileImmutableClassName(file));
}
}
// Check whether there is any type defined in the proto file that has
// the given class name.
bool ClassNameResolver::HasConflictingClassName(const FileDescriptor* file,
const std::string& classname,
absl::string_view classname,
NameEquality equality_mode) {
for (int i = 0; i < file->enum_type_count(); i++) {
if (CheckNameEquality(file->enum_type(i)->name(), classname) ==
@ -220,7 +221,7 @@ std::string ClassNameResolver::GetDescriptorClassName(
if (options_.opensource_runtime) {
return GetFileImmutableClassName(file);
} else {
return GetFileImmutableClassName(file) + "InternalDescriptors";
return absl::StrCat(GetFileImmutableClassName(file), "InternalDescriptors");
}
}
@ -240,14 +241,14 @@ std::string ClassNameResolver::GetClassName(const FileDescriptor* descriptor,
// Get the full name of a Java class by prepending the Java package name
// or outer class name.
std::string ClassNameResolver::GetClassFullName(
const std::string& name_without_package, const FileDescriptor* file,
absl::string_view name_without_package, const FileDescriptor* file,
bool immutable, bool is_own_file) {
return GetClassFullName(name_without_package, file, immutable, is_own_file,
false);
}
std::string ClassNameResolver::GetClassFullName(
const std::string& name_without_package, const FileDescriptor* file,
absl::string_view name_without_package, const FileDescriptor* file,
bool immutable, bool is_own_file, bool kotlin) {
std::string result;
if (is_own_file) {
@ -256,10 +257,10 @@ std::string ClassNameResolver::GetClassFullName(
result = GetClassName(file, immutable, kotlin);
}
if (!result.empty()) {
result += '.';
absl::StrAppend(&result, ".");
}
result += name_without_package;
if (kotlin) result += "Kt";
absl::StrAppend(&result, name_without_package);
if (kotlin) absl::StrAppend(&result, "Kt");
return result;
}
@ -301,13 +302,13 @@ std::string ClassNameResolver::GetClassName(const ServiceDescriptor* descriptor,
// Get the Java Class style full name of a message.
std::string ClassNameResolver::GetJavaClassFullName(
const std::string& name_without_package, const FileDescriptor* file,
absl::string_view name_without_package, const FileDescriptor* file,
bool immutable) {
return GetJavaClassFullName(name_without_package, file, immutable, false);
}
std::string ClassNameResolver::GetJavaClassFullName(
const std::string& name_without_package, const FileDescriptor* file,
absl::string_view name_without_package, const FileDescriptor* file,
bool immutable, bool kotlin) {
std::string result;
if (MultipleJavaFiles(file, immutable)) {
@ -328,14 +329,15 @@ std::string ClassNameResolver::GetExtensionIdentifierName(
std::string ClassNameResolver::GetExtensionIdentifierName(
const FieldDescriptor* descriptor, bool immutable, bool kotlin) {
return GetClassName(descriptor->containing_type(), immutable, kotlin) + "." +
descriptor->name();
return absl::StrCat(
GetClassName(descriptor->containing_type(), immutable, kotlin), ".",
descriptor->name());
}
std::string ClassNameResolver::GetKotlinFactoryName(
const Descriptor* descriptor) {
std::string name = ToCamelCase(descriptor->name(), /* lower_first = */ true);
return IsForbiddenKotlin(name) ? name + "_" : name;
return IsForbiddenKotlin(name) ? absl::StrCat(name, "_") : name;
}
std::string ClassNameResolver::GetJavaImmutableClassName(
@ -370,14 +372,14 @@ std::string ClassNameResolver::GetJavaMutableClassName(
std::string ClassNameResolver::GetDowngradedFileClassName(
const FileDescriptor* file) {
return "Downgraded" + GetFileClassName(file, false);
return absl::StrCat("Downgraded", GetFileClassName(file, false));
}
std::string ClassNameResolver::GetDowngradedClassName(
const Descriptor* descriptor) {
return FileJavaPackage(descriptor->file(), true, options_) + "." +
GetDowngradedFileClassName(descriptor->file()) + "." +
ClassNameWithoutPackage(descriptor, false);
return absl::StrCat(FileJavaPackage(descriptor->file(), true, options_), ".",
GetDowngradedFileClassName(descriptor->file()), ".",
ClassNameWithoutPackage(descriptor, false));
}
} // namespace java

@ -78,7 +78,7 @@ class ClassNameResolver {
// Check whether there is any type defined in the proto file that has
// the given class name.
bool HasConflictingClassName(const FileDescriptor* file,
const std::string& classname,
absl::string_view classname,
NameEquality equality_mode);
// Gets the name of the outer class that holds descriptor information.
@ -132,10 +132,10 @@ class ClassNameResolver {
// Get the full name of a Java class by prepending the Java package name
// or outer class name.
std::string GetClassFullName(const std::string& name_without_package,
std::string GetClassFullName(absl::string_view name_without_package,
const FileDescriptor* file, bool immutable,
bool is_own_file);
std::string GetClassFullName(const std::string& name_without_package,
std::string GetClassFullName(absl::string_view name_without_package,
const FileDescriptor* file, bool immutable,
bool is_own_file, bool kotlin);
@ -143,9 +143,9 @@ class ClassNameResolver {
private:
// Get the Java Class style full name of a message.
std::string GetJavaClassFullName(const std::string& name_without_package,
std::string GetJavaClassFullName(absl::string_view name_without_package,
const FileDescriptor* file, bool immutable);
std::string GetJavaClassFullName(const std::string& name_without_package,
std::string GetJavaClassFullName(absl::string_view name_without_package,
const FileDescriptor* file, bool immutable,
bool kotlin);
// Caches the result to provide better performance.

@ -74,7 +74,7 @@ bool IsReservedName(absl::string_view name) {
return kReservedNames.contains(name);
}
bool IsForbidden(const std::string& field_name) {
bool IsForbidden(absl::string_view field_name) {
// Names that should be avoided (in UpperCamelCase format).
// Using them will cause the compiler to generate accessors whose names
// collide with methods defined in base classes.
@ -113,7 +113,7 @@ std::string FieldName(const FieldDescriptor* field) {
if (IsForbidden(field_name)) {
// Append a trailing "#" to indicate that the name should be decorated to
// avoid collision with other names.
field_name += "#";
absl::StrAppend(&field_name, "#");
}
return field_name;
}
@ -180,7 +180,7 @@ std::string UnderscoresToCamelCase(const MethodDescriptor* method) {
std::string UnderscoresToCamelCaseCheckReserved(const FieldDescriptor* field) {
std::string name = UnderscoresToCamelCase(field);
if (IsReservedName(name)) {
return name + "_";
absl::StrAppend(&name, "_");
}
return name;
}

@ -103,8 +103,8 @@ std::string CapitalizedFieldName(const FieldDescriptor* descriptor);
// Returns:
// Converts a name to camel-case. If cap_first_letter is true, capitalize the
// first letter.
std::string UnderscoresToCamelCase(const std::string& name,
bool cap_first_letter);
std::string UnderscoresToCamelCase(absl::string_view input,
bool cap_next_letter);
// Requires:
// field != NULL
// Returns:

@ -81,16 +81,17 @@ class TestGenerator : public CodeGenerator {
// not verify that they are correctly-placed; that would require actually
// compiling the output which is a bit more than I care to do for this test.
TEST(JavaPluginTest, PluginTest) {
GOOGLE_ABSL_CHECK_OK(File::SetContents(TestTempDir() + "/test.proto",
"syntax = \"proto2\";\n"
"package foo;\n"
"option java_package = \"\";\n"
"option java_outer_classname = \"Test\";\n"
"message Bar {\n"
" message Baz {}\n"
"}\n"
"enum Qux { BLAH = 1; }\n",
true));
GOOGLE_ABSL_CHECK_OK(
File::SetContents(absl::StrCat(TestTempDir(), "/test.proto"),
"syntax = \"proto2\";\n"
"package foo;\n"
"option java_package = \"\";\n"
"option java_outer_classname = \"Test\";\n"
"message Bar {\n"
" message Baz {}\n"
"}\n"
"enum Qux { BLAH = 1; }\n",
true));
CommandLineInterface cli;
cli.SetInputsAreProtoPathRelative(true);
@ -100,9 +101,9 @@ TEST(JavaPluginTest, PluginTest) {
cli.RegisterGenerator("--java_out", &java_generator, "");
cli.RegisterGenerator("--test_out", &test_generator, "");
std::string proto_path = "-I" + TestTempDir();
std::string java_out = "--java_out=" + TestTempDir();
std::string test_out = "--test_out=" + TestTempDir();
std::string proto_path = absl::StrCat("-I", TestTempDir());
std::string java_out = absl::StrCat("--java_out=", TestTempDir());
std::string test_out = absl::StrCat("--test_out=", TestTempDir());
const char* argv[] = {"protoc", proto_path.c_str(), java_out.c_str(),
test_out.c_str(), "test.proto"};
@ -113,8 +114,8 @@ TEST(JavaPluginTest, PluginTest) {
// expect
std::string output;
GOOGLE_ABSL_CHECK_OK(File::GetContents(TestTempDir() + "/Test.java", &output,
true));
GOOGLE_ABSL_CHECK_OK(File::GetContents(absl::StrCat(TestTempDir(), "/Test.java"),
&output, true));
std::vector<std::string> lines = absl::StrSplit(output, "\n");
bool found_generated_annotation = false;
bool found_do_not_edit = false;

@ -64,9 +64,9 @@ void SetPrimitiveVariables(
SetCommonFieldVariables(descriptor, info, variables);
JavaType javaType = GetJavaType(descriptor);
(*variables)["type"] = PrimitiveTypeName(javaType);
(*variables)["boxed_type"] = BoxedPrimitiveTypeName(javaType);
(*variables)["kt_type"] = KotlinTypeName(javaType);
(*variables)["type"] = std::string(PrimitiveTypeName(javaType));
(*variables)["boxed_type"] = std::string(BoxedPrimitiveTypeName(javaType));
(*variables)["kt_type"] = std::string(KotlinTypeName(javaType));
variables->insert({"field_type", (*variables)["type"]});
std::string name = (*variables)["name"];
@ -112,10 +112,10 @@ void SetPrimitiveVariables(
(*variables)["default_init"] =
IsDefaultValueJavaDefault(descriptor)
? ""
: ("= " + ImmutableDefaultValue(descriptor, name_resolver,
context->options()));
(*variables)["capitalized_type"] = GetCapitalizedType(
descriptor, /* immutable = */ true, context->options());
: absl::StrCat("= ", ImmutableDefaultValue(descriptor, name_resolver,
context->options()));
(*variables)["capitalized_type"] = std::string(GetCapitalizedType(
descriptor, /* immutable = */ true, context->options()));
(*variables)["tag"] =
absl::StrCat(static_cast<int32_t>(WireFormat::MakeTag(descriptor)));
(*variables)["tag_size"] = absl::StrCat(
@ -146,7 +146,7 @@ void SetPrimitiveVariables(
(*variables)["get_has_field_bit_message"] = GenerateGetBit(messageBitIndex);
// Note that these have a trailing ";".
(*variables)["set_has_field_bit_to_local"] =
GenerateSetBitToLocal(messageBitIndex) + ";";
absl::StrCat(GenerateSetBitToLocal(messageBitIndex), ";");
(*variables)["is_field_present_message"] = GenerateGetBit(messageBitIndex);
} else {
(*variables)["set_has_field_bit_to_local"] = "";
@ -182,9 +182,9 @@ void SetPrimitiveVariables(
(*variables)["get_has_field_bit_from_local"] =
GenerateGetBitFromLocal(builderBitIndex);
(*variables)["set_has_field_bit_builder"] =
GenerateSetBit(builderBitIndex) + ";";
absl::StrCat(GenerateSetBit(builderBitIndex), ";");
(*variables)["clear_has_field_bit_builder"] =
GenerateClearBit(builderBitIndex) + ";";
absl::StrCat(GenerateClearBit(builderBitIndex), ";");
}
} // namespace
@ -506,7 +506,7 @@ void ImmutablePrimitiveFieldGenerator::GenerateHashCode(
}
std::string ImmutablePrimitiveFieldGenerator::GetBoxedType() const {
return BoxedPrimitiveTypeName(GetJavaType(descriptor_));
return std::string(BoxedPrimitiveTypeName(GetJavaType(descriptor_)));
}
// ===================================================================
@ -1069,7 +1069,7 @@ void RepeatedImmutablePrimitiveFieldGenerator::GenerateHashCode(
}
std::string RepeatedImmutablePrimitiveFieldGenerator::GetBoxedType() const {
return BoxedPrimitiveTypeName(GetJavaType(descriptor_));
return std::string(BoxedPrimitiveTypeName(GetJavaType(descriptor_)));
}
} // namespace java

@ -70,14 +70,14 @@ void SetPrimitiveVariables(
Context* context) {
SetCommonFieldVariables(descriptor, info, variables);
JavaType javaType = GetJavaType(descriptor);
(*variables)["type"] = PrimitiveTypeName(javaType);
(*variables)["boxed_type"] = BoxedPrimitiveTypeName(javaType);
(*variables)["kt_type"] = KotlinTypeName(javaType);
(*variables)["type"] = std::string(PrimitiveTypeName(javaType));
(*variables)["boxed_type"] = std::string(BoxedPrimitiveTypeName(javaType));
(*variables)["kt_type"] = std::string(KotlinTypeName(javaType));
variables->insert({"field_type", (*variables)["type"]});
(*variables)["default"] =
ImmutableDefaultValue(descriptor, name_resolver, context->options());
(*variables)["capitalized_type"] = GetCapitalizedType(
descriptor, /* immutable = */ true, context->options());
(*variables)["capitalized_type"] = std::string(GetCapitalizedType(
descriptor, /* immutable = */ true, context->options()));
(*variables)["tag"] =
absl::StrCat(static_cast<int32_t>(WireFormat::MakeTag(descriptor)));
(*variables)["tag_size"] = absl::StrCat(
@ -126,7 +126,7 @@ void SetPrimitiveVariables(
if (javaType == JAVATYPE_BYTES) {
(*variables)["bytes_default"] =
absl::AsciiStrToUpper(name) + "_DEFAULT_VALUE";
absl::StrCat(absl::AsciiStrToUpper(name), "_DEFAULT_VALUE");
}
if (IsReferenceType(javaType)) {
@ -157,9 +157,9 @@ void SetPrimitiveVariables(
// Note that these have a trailing ";".
(*variables)["set_has_field_bit_message"] =
GenerateSetBit(messageBitIndex) + ";";
absl::StrCat(GenerateSetBit(messageBitIndex), ";");
(*variables)["clear_has_field_bit_message"] =
GenerateClearBit(messageBitIndex) + ";";
absl::StrCat(GenerateClearBit(messageBitIndex), ";");
(*variables)["is_field_present_message"] = GenerateGetBit(messageBitIndex);
} else {
@ -182,7 +182,7 @@ void SetPrimitiveVariables(
default:
variables->insert(
{"is_field_present_message",
absl::StrCat(name, "_ != " + (*variables)["default"])});
absl::StrCat(name, "_ != ", (*variables)["default"])});
break;
}
}
@ -394,7 +394,7 @@ void ImmutablePrimitiveFieldLiteGenerator::GenerateInitializationCode(
}
std::string ImmutablePrimitiveFieldLiteGenerator::GetBoxedType() const {
return BoxedPrimitiveTypeName(GetJavaType(descriptor_));
return std::string(BoxedPrimitiveTypeName(GetJavaType(descriptor_)));
}
// ===================================================================
@ -796,7 +796,7 @@ void RepeatedImmutablePrimitiveFieldLiteGenerator::GenerateInitializationCode(
}
std::string RepeatedImmutablePrimitiveFieldLiteGenerator::GetBoxedType() const {
return BoxedPrimitiveTypeName(GetJavaType(descriptor_));
return std::string(BoxedPrimitiveTypeName(GetJavaType(descriptor_)));
}
} // namespace java

@ -66,7 +66,7 @@ void SharedCodeGenerator::Generate(
if (HasDescriptorMethods(file_, options_.enforce_lite)) {
// Generate descriptors.
std::string classname = name_resolver_->GetDescriptorClassName(file_);
std::string filename = package_dir + classname + ".java";
std::string filename = absl::StrCat(package_dir, classname, ".java");
file_list->push_back(filename);
std::unique_ptr<io::ZeroCopyOutputStream> output(context->Open(filename));
GeneratedCodeInfo annotations;
@ -75,8 +75,8 @@ void SharedCodeGenerator::Generate(
std::unique_ptr<io::Printer> printer(
new io::Printer(output.get(), '$',
options_.annotate_code ? &annotation_collector : NULL));
std::string info_relative_path = classname + ".java.pb.meta";
std::string info_full_path = filename + ".pb.meta";
std::string info_relative_path = absl::StrCat(classname, ".java.pb.meta");
std::string info_full_path = absl::StrCat(filename, ".pb.meta");
printer->Print(
"// Generated by the protocol buffer compiler. DO NOT EDIT!\n"
"// source: $filename$\n"
@ -177,7 +177,7 @@ void SharedCodeGenerator::GenerateDescriptors(io::Printer* printer) {
if (package.empty()) {
full_name = classname;
} else {
full_name = package + "." + classname;
full_name = absl::StrCat(package, ".", classname);
}
dependencies.push_back(std::make_pair(filename, full_name));
}

@ -69,9 +69,9 @@ void SetPrimitiveVariables(
(*variables)["default"] =
ImmutableDefaultValue(descriptor, name_resolver, context->options());
(*variables)["default_init"] =
"= " +
ImmutableDefaultValue(descriptor, name_resolver, context->options());
(*variables)["default_init"] = absl::StrCat(
"= ",
ImmutableDefaultValue(descriptor, name_resolver, context->options()));
(*variables)["capitalized_type"] = "String";
(*variables)["tag"] =
absl::StrCat(static_cast<int32_t>(WireFormat::MakeTag(descriptor)));
@ -79,14 +79,15 @@ void SetPrimitiveVariables(
WireFormat::TagSize(descriptor->number(), GetType(descriptor)));
(*variables)["null_check"] =
"if (value == null) { throw new NullPointerException(); }";
(*variables)["isStringEmpty"] = "com.google.protobuf.GeneratedMessage" +
GeneratedCodeVersionSuffix() +
".isStringEmpty";
(*variables)["writeString"] = "com.google.protobuf.GeneratedMessage" +
GeneratedCodeVersionSuffix() + ".writeString";
(*variables)["computeStringSize"] = "com.google.protobuf.GeneratedMessage" +
GeneratedCodeVersionSuffix() +
".computeStringSize";
(*variables)["isStringEmpty"] =
absl::StrCat("com.google.protobuf.GeneratedMessage",
GeneratedCodeVersionSuffix(), ".isStringEmpty");
(*variables)["writeString"] =
absl::StrCat("com.google.protobuf.GeneratedMessage",
GeneratedCodeVersionSuffix(), ".writeString");
(*variables)["computeStringSize"] =
absl::StrCat("com.google.protobuf.GeneratedMessage",
GeneratedCodeVersionSuffix(), ".computeStringSize");
// TODO(birdo): Add @deprecated javadoc when generating javadoc is supported
// by the proto compiler
@ -108,7 +109,7 @@ void SetPrimitiveVariables(
// Note that these have a trailing ";".
(*variables)["set_has_field_bit_message"] =
GenerateSetBit(messageBitIndex) + ";";
absl::StrCat(GenerateSetBit(messageBitIndex), ";");
(*variables)["is_field_present_message"] = GenerateGetBit(messageBitIndex);
} else {
@ -130,9 +131,9 @@ void SetPrimitiveVariables(
(*variables)["get_has_field_bit_from_local"] =
GenerateGetBitFromLocal(builderBitIndex);
(*variables)["set_has_field_bit_builder"] =
GenerateSetBit(builderBitIndex) + ";";
absl::StrCat(GenerateSetBit(builderBitIndex), ";");
(*variables)["clear_has_field_bit_builder"] =
GenerateClearBit(builderBitIndex) + ";";
absl::StrCat(GenerateClearBit(builderBitIndex), ";");
}
} // namespace

@ -69,9 +69,9 @@ void SetPrimitiveVariables(
(*variables)["default"] =
ImmutableDefaultValue(descriptor, name_resolver, context->options());
(*variables)["default_init"] =
"= " +
ImmutableDefaultValue(descriptor, name_resolver, context->options());
(*variables)["default_init"] = absl::StrCat(
"= ",
ImmutableDefaultValue(descriptor, name_resolver, context->options()));
(*variables)["capitalized_type"] = "java.lang.String";
(*variables)["tag"] =
absl::StrCat(static_cast<int32_t>(WireFormat::MakeTag(descriptor)));
@ -109,9 +109,9 @@ void SetPrimitiveVariables(
// Note that these have a trailing ";".
(*variables)["set_has_field_bit_message"] =
GenerateSetBit(messageBitIndex) + ";";
absl::StrCat(GenerateSetBit(messageBitIndex), ";");
(*variables)["clear_has_field_bit_message"] =
GenerateClearBit(messageBitIndex) + ";";
absl::StrCat(GenerateClearBit(messageBitIndex), ";");
(*variables)["is_field_present_message"] = GenerateGetBit(messageBitIndex);
} else {

@ -6917,14 +6917,13 @@ void DescriptorBuilder::ValidateFileOptions(FileDescriptor* file,
if (!IsLite(file)) {
for (int i = 0; i < file->dependency_count(); i++) {
if (IsLite(file->dependency(i))) {
AddError(file->dependency(i)->name(), proto,
DescriptorPool::ErrorCollector::IMPORT,
absl::StrCat("Files that do not use optimize_for = "
"LITE_RUNTIME cannot import "
"files which do use this option. This file is "
"not lite, but it "
"imports \"",
file->dependency(i)->name(), "\" which is."));
AddError(
file->dependency(i)->name(), proto,
DescriptorPool::ErrorCollector::IMPORT,
absl::StrCat("Files that do not use optimize_for = LITE_RUNTIME "
"cannot import files which do use this option. This "
"file is not lite, but it imports \"",
file->dependency(i)->name(), "\" which is."));
break;
}
}
@ -7456,8 +7455,7 @@ bool DescriptorBuilder::OptionInterpreter::InterpretSingleOption(
}
if (uninterpreted_option_->name(0).name_part() == "uninterpreted_option") {
return AddNameError(
"Option must not use reserved name "
"\"uninterpreted_option\".");
"Option must not use reserved name \"uninterpreted_option\".");
}
const Descriptor* options_descriptor = nullptr;
@ -7949,8 +7947,7 @@ bool DescriptorBuilder::OptionInterpreter::SetOptionValue(
uint64_t value;
if (!uninterpreted_option_->has_identifier_value()) {
return AddValueError(
absl::StrCat("Value must be identifier for boolean option "
"\"",
absl::StrCat("Value must be identifier for boolean option \"",
option_field->full_name(), "\"."));
}
if (uninterpreted_option_->identifier_value() == "true") {
@ -7958,10 +7955,9 @@ bool DescriptorBuilder::OptionInterpreter::SetOptionValue(
} else if (uninterpreted_option_->identifier_value() == "false") {
value = 0;
} else {
return AddValueError(
absl::StrCat("Value must be \"true\" or \"false\" for boolean "
"option \"",
option_field->full_name(), "\"."));
return AddValueError(absl::StrCat(
"Value must be \"true\" or \"false\" for boolean option \"",
option_field->full_name(), "\"."));
}
unknown_fields->AddVarint(option_field->number(), value);
break;
@ -7969,8 +7965,7 @@ bool DescriptorBuilder::OptionInterpreter::SetOptionValue(
case FieldDescriptor::CPPTYPE_ENUM: {
if (!uninterpreted_option_->has_identifier_value()) {
return AddValueError(
absl::StrCat("Value must be identifier for enum-valued option "
"\"",
absl::StrCat("Value must be identifier for enum-valued option \"",
option_field->full_name(), "\"."));
}
const EnumDescriptor* enum_type = option_field->enum_type();
@ -8012,9 +8007,7 @@ bool DescriptorBuilder::OptionInterpreter::SetOptionValue(
return AddValueError(
absl::StrCat("Enum type \"", option_field->enum_type()->full_name(),
"\" has no value named \"", value_name,
"\" for "
"option \"",
option_field->full_name(), "\"."));
"\" for option \"", option_field->full_name(), "\"."));
} else {
// Sign-extension is not a problem, since we cast directly from int32_t
// to uint64_t, without first going through uint32_t.
@ -8028,8 +8021,7 @@ bool DescriptorBuilder::OptionInterpreter::SetOptionValue(
case FieldDescriptor::CPPTYPE_STRING:
if (!uninterpreted_option_->has_string_value()) {
return AddValueError(
absl::StrCat("Value must be quoted string for string option "
"\"",
absl::StrCat("Value must be quoted string for string option \"",
option_field->full_name(), "\"."));
}
// The string has already been unquoted and unescaped by the parser.
@ -8123,12 +8115,11 @@ bool DescriptorBuilder::OptionInterpreter::SetAggregateOption(
if (!uninterpreted_option_->has_aggregate_value()) {
return AddValueError(
absl::StrCat("Option \"", option_field->full_name(),
"\" is a message. To set the entire message, use "
"syntax like \"",
"\" is a message. "
"To set the entire message, use syntax like \"",
option_field->name(),
" = { <proto text format> }\". "
"To set fields within it, use "
"syntax like \"",
"To set fields within it, use syntax like \"",
option_field->name(), ".foo = value\"."));
}

@ -1072,7 +1072,7 @@ TEST(ExtensionSetTest, RepeatedFields) {
.MutableRepeatedExtension(unittest::repeated_string_extension)
->end();
string_iter != string_end; ++string_iter) {
absl::StrAppend(&*string_iter, "test");
string_iter->append("test");
}
RepeatedPtrField<std::string>::const_iterator string_const_iter;
RepeatedPtrField<std::string>::const_iterator string_const_end;

@ -61,9 +61,8 @@ inline std::string TranslatePathToOpensource(absl::string_view google3_path) {
}
inline std::string MaybeTranslatePath(absl::string_view google3_path) {
std::string path(google3_path);
path = TranslatePathToOpensource(path);
return path;
return TranslatePathToOpensource(google3_path);
return std::string(google3_path);
}
inline std::string TestSourceDir() {

@ -910,15 +910,12 @@ class TextFormat::Parser::ParserImpl {
return true;
} else if (!allow_unknown_enum_) {
ReportError(absl::StrCat("Unknown enumeration value of \"", value,
"\" for "
"field \"",
field->name(), "\"."));
"\" for field \"", field->name(), "\"."));
return false;
} else {
ReportWarning(absl::StrCat("Unknown enumeration value of \"", value,
"\" for "
"field \"",
field->name(), "\"."));
"\" for field \"", field->name(),
"\"."));
return true;
}
}

Loading…
Cancel
Save