Automated rollback of commit 47e7983055.

PiperOrigin-RevId: 503007139
pull/11589/head
Mike Kruskal 2 years ago committed by Copybara-Service
parent 6fcc5228c7
commit 08b97c3120
  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,12 +30,8 @@
#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"
@ -58,17 +54,11 @@ 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, absl::string_view name1,
const FieldDescriptor* field2, absl::string_view name2,
bool IsConflicting(const FieldDescriptor* field1, const std::string& name1,
const FieldDescriptor* field2, const std::string& name2,
std::string* info) {
if (field1->is_repeated()) {
if (field2->is_repeated()) {
@ -76,18 +66,16 @@ bool IsConflicting(const FieldDescriptor* field1, absl::string_view name1,
return false;
} else {
// field1 is repeated, and field2 is not.
if (EqualWithSuffix(name1, "Count", name2)) {
*info = absl::StrCat("both repeated field \"", field1->name(),
"\" and singular ", "field \"", field2->name(),
"\" generate the method \"", "get", name1,
"Count()\"");
if (name1 + "Count" == name2) {
*info = "both repeated field \"" + field1->name() + "\" and singular " +
"field \"" + field2->name() + "\" generate the method \"" +
"get" + name1 + "Count()\"";
return true;
}
if (EqualWithSuffix(name1, "List", name2)) {
*info =
absl::StrCat("both repeated field \"", field1->name(),
"\" and singular ", "field \"", field2->name(),
"\" generate the method \"", "get", name1, "List()\"");
if (name1 + "List" == name2) {
*info = "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
@ -150,8 +138,8 @@ void Context::InitializeFieldGeneratorInfoForFields(
if (name == other_name) {
is_conflict[i] = is_conflict[j] = true;
conflict_reason[i] = conflict_reason[j] =
absl::StrCat("capitalized name of field \"", field->name(),
"\" conflicts with field \"", other->name(), "\"");
"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;
@ -172,8 +160,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]) {
absl::StrAppend(&info.name, field->number());
absl::StrAppend(&info.capitalized_name, field->number());
info.name += absl::StrCat(field->number());
info.capitalized_name += absl::StrCat(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"] =
absl::StrCat(GenerateSetBit(messageBitIndex), ";");
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"] =
absl::StrCat(GenerateSetBit(builderBitIndex), ";");
GenerateSetBit(builderBitIndex) + ";";
(*variables)["clear_has_field_bit_builder"] =
absl::StrCat(GenerateClearBit(builderBitIndex), ";");
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"] =
absl::StrCat(GenerateSetBit(messageBitIndex), ";");
GenerateSetBit(messageBitIndex) + ";";
(*variables)["clear_has_field_bit_message"] =
absl::StrCat(GenerateClearBit(messageBitIndex), ";");
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"] = std::string(FieldTypeName(GetType(descriptor)));
vars["type_constant"] = 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"] = absl::StrCat(singular_type, ".getDefaultInstance()");
vars["prototype"] = singular_type + ".getDefaultInstance()";
break;
case JAVATYPE_ENUM:
singular_type =
name_resolver->GetClassName(descriptor->enum_type(), immutable);
vars["enum_map"] = absl::StrCat(singular_type, ".internalGetValueMap()");
vars["enum_map"] = 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 = std::string(BoxedPrimitiveTypeName(java_type));
singular_type = BoxedPrimitiveTypeName(java_type);
break;
}
vars["type"] = descriptor->is_repeated()
? absl::StrCat("java.util.List<", singular_type, ">")
? "java.util.List<" + singular_type + ">"
: singular_type;
vars["singular_type"] = singular_type;
}

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

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

@ -34,9 +34,6 @@
#include "google/protobuf/compiler/java/generator.h"
#include <utility>
#include <vector>
#include <memory>
@ -75,25 +72,25 @@ bool JavaGenerator::Generate(const FileDescriptor* file,
file_options.opensource_runtime = opensource_runtime_;
for (auto& option : options) {
if (option.first == "output_list_file") {
file_options.output_list_file = option.second;
} else if (option.first == "immutable") {
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") {
file_options.generate_immutable_code = true;
} else if (option.first == "mutable") {
} else if (options[i].first == "mutable") {
file_options.generate_mutable_code = true;
} else if (option.first == "shared") {
} else if (options[i].first == "shared") {
file_options.generate_shared_code = true;
} else if (option.first == "lite") {
} else if (options[i].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 (option.first == "annotate_code") {
} else if (options[i].first == "annotate_code") {
file_options.annotate_code = true;
} else if (option.first == "annotation_list_file") {
file_options.annotation_list_file = option.second;
} else if (options[i].first == "annotation_list_file") {
file_options.annotation_list_file = options[i].second;
} else {
*error = absl::StrCat("Unknown generator option: ", option.first);
*error = "Unknown generator option: " + options[i].first;
return false;
}
}
@ -118,31 +115,35 @@ bool JavaGenerator::Generate(const FileDescriptor* file,
std::vector<std::string> all_annotations;
std::vector<std::unique_ptr<FileGenerator>> file_generators;
std::vector<FileGenerator*> file_generators;
if (file_options.generate_immutable_code) {
file_generators.emplace_back(
std::make_unique<FileGenerator>(file, file_options,
/* immutable = */ true));
file_generators.push_back(new FileGenerator(file, file_options,
/* immutable = */ true));
}
if (file_options.generate_mutable_code) {
file_generators.emplace_back(
std::make_unique<FileGenerator>(file, file_options,
/* mutable = */ false));
file_generators.push_back(new FileGenerator(file, file_options,
/* mutable = */ false));
}
for (auto& file_generator : file_generators) {
if (!file_generator->Validate(error)) {
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];
}
return false;
}
}
for (auto& file_generator : file_generators) {
for (int i = 0; i < file_generators.size(); ++i) {
FileGenerator* file_generator = file_generators[i];
std::string package_dir = JavaPackageToDir(file_generator->java_package());
std::string java_filename =
absl::StrCat(package_dir, file_generator->classname(), ".java");
std::string java_filename = package_dir;
java_filename += file_generator->classname();
java_filename += ".java";
all_files.push_back(java_filename);
std::string info_full_path = absl::StrCat(java_filename, ".pb.meta");
std::string info_full_path = java_filename + ".pb.meta";
if (file_options.annotate_code) {
all_annotations.push_back(info_full_path);
}
@ -171,6 +172,9 @@ 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,
absl::string_view annotation_file,
const std::string& annotation_file,
Options options) {
if (annotation_file.empty()) {
return;
@ -89,8 +89,7 @@ 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,
absl::string_view var_name, absl::string_view terminating_string,
bool enforce_lite) {
const char* var_name, const char* terminating_string, bool enforce_lite) {
std::string enum_verifier_string =
enforce_lite ? absl::StrCat(var_name, ".internalGetVerifier()")
: absl::StrCat(
@ -102,11 +101,12 @@ void PrintEnumVerifierLogic(
".forNumber(number) != null;\n"
" }\n"
" }");
printer->Print(variables,
absl::StrCat(enum_verifier_string, terminating_string));
printer->Print(
variables,
absl::StrCat(enum_verifier_string, terminating_string).c_str());
}
std::string UnderscoresToCamelCase(absl::string_view input,
std::string UnderscoresToCamelCase(const std::string& input,
bool cap_next_letter) {
GOOGLE_ABSL_CHECK(!input.empty());
std::string result;
@ -143,7 +143,7 @@ std::string UnderscoresToCamelCase(absl::string_view input,
return result;
}
std::string ToCamelCase(absl::string_view input, bool lower_first) {
std::string ToCamelCase(const std::string& input, bool lower_first) {
bool capitalize_next = !lower_first;
std::string result;
result.reserve(input.size());
@ -185,26 +185,25 @@ 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 (absl::string_view package : packages) {
std::vector<std::string> packages = absl::StrSplit(name, "."); // NOLINT
for (const std::string& package : packages) {
if (IsForbiddenKotlin(package)) {
escaped_packages.push_back(absl::StrCat("`", package, "`"));
escaped_packages.push_back("`" + package + "`");
} else {
escaped_packages.emplace_back(package);
escaped_packages.push_back(package);
}
}
return absl::StrJoin(escaped_packages, ".");
}
std::string UniqueFileScopeIdentifier(const Descriptor* descriptor) {
return absl::StrCat(
"static_", absl::StrReplaceAll(descriptor->full_name(), {{".", "_"}}));
return "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 absl::StrCat("_", fieldName);
return '_' + fieldName;
}
return fieldName;
}
@ -215,28 +214,31 @@ 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()) absl::StrAppend(&package_dir, "/");
if (!package_dir.empty()) package_dir += "/";
return package_dir;
}
std::string ExtraMessageInterfaces(const Descriptor* descriptor) {
return absl::StrCat("// @@protoc_insertion_point(message_implements:",
descriptor->full_name(), ")");
std::string interfaces = "// @@protoc_insertion_point(message_implements:" +
descriptor->full_name() + ")";
return interfaces;
}
std::string ExtraBuilderInterfaces(const Descriptor* descriptor) {
return absl::StrCat("// @@protoc_insertion_point(builder_implements:",
descriptor->full_name(), ")");
std::string interfaces = "// @@protoc_insertion_point(builder_implements:" +
descriptor->full_name() + ")";
return interfaces;
}
std::string ExtraMessageOrBuilderInterfaces(const Descriptor* descriptor) {
return absl::StrCat("// @@protoc_insertion_point(interface_extends:",
descriptor->full_name(), ")");
std::string interfaces = "// @@protoc_insertion_point(interface_extends:" +
descriptor->full_name() + ")";
return interfaces;
}
std::string FieldConstantName(const FieldDescriptor* field) {
std::string name = absl::StrCat(field->name(), "_FIELD_NUMBER");
std::string name = field->name() + "_FIELD_NUMBER";
absl::AsciiStrToUpper(&name);
return name;
}
@ -291,7 +293,7 @@ JavaType GetJavaType(const FieldDescriptor* field) {
return JAVATYPE_INT;
}
absl::string_view PrimitiveTypeName(JavaType type) {
const char* PrimitiveTypeName(JavaType type) {
switch (type) {
case JAVATYPE_INT:
return "int";
@ -308,23 +310,23 @@ absl::string_view PrimitiveTypeName(JavaType type) {
case JAVATYPE_BYTES:
return "com.google.protobuf.ByteString";
case JAVATYPE_ENUM:
return {};
return NULL;
case JAVATYPE_MESSAGE:
return {};
return NULL;
// No default because we want the compiler to complain if any new
// JavaTypes are added.
}
GOOGLE_ABSL_LOG(FATAL) << "Can't get here.";
return {};
return NULL;
}
absl::string_view PrimitiveTypeName(const FieldDescriptor* descriptor) {
const char* PrimitiveTypeName(const FieldDescriptor* descriptor) {
return PrimitiveTypeName(GetJavaType(descriptor));
}
absl::string_view BoxedPrimitiveTypeName(JavaType type) {
const char* BoxedPrimitiveTypeName(JavaType type) {
switch (type) {
case JAVATYPE_INT:
return "java.lang.Integer";
@ -341,23 +343,23 @@ absl::string_view BoxedPrimitiveTypeName(JavaType type) {
case JAVATYPE_BYTES:
return "com.google.protobuf.ByteString";
case JAVATYPE_ENUM:
return {};
return NULL;
case JAVATYPE_MESSAGE:
return {};
return NULL;
// No default because we want the compiler to complain if any new
// JavaTypes are added.
}
GOOGLE_ABSL_LOG(FATAL) << "Can't get here.";
return {};
return NULL;
}
absl::string_view BoxedPrimitiveTypeName(const FieldDescriptor* descriptor) {
const char* BoxedPrimitiveTypeName(const FieldDescriptor* descriptor) {
return BoxedPrimitiveTypeName(GetJavaType(descriptor));
}
absl::string_view KotlinTypeName(JavaType type) {
const char* KotlinTypeName(JavaType type) {
switch (type) {
case JAVATYPE_INT:
return "kotlin.Int";
@ -374,16 +376,16 @@ absl::string_view KotlinTypeName(JavaType type) {
case JAVATYPE_BYTES:
return "com.google.protobuf.ByteString";
case JAVATYPE_ENUM:
return {};
return NULL;
case JAVATYPE_MESSAGE:
return {};
return NULL;
// No default because we want the compiler to complain if any new
// JavaTypes are added.
}
GOOGLE_ABSL_LOG(FATAL) << "Can't get here.";
return {};
return NULL;
}
std::string GetOneofStoredType(const FieldDescriptor* field) {
@ -394,11 +396,11 @@ std::string GetOneofStoredType(const FieldDescriptor* field) {
case JAVATYPE_MESSAGE:
return ClassNameResolver().GetClassName(field->message_type(), true);
default:
return std::string(BoxedPrimitiveTypeName(javaType));
return BoxedPrimitiveTypeName(javaType);
}
}
absl::string_view FieldTypeName(FieldDescriptor::Type field_type) {
const char* FieldTypeName(FieldDescriptor::Type field_type) {
switch (field_type) {
case FieldDescriptor::TYPE_INT32:
return "INT32";
@ -442,10 +444,10 @@ absl::string_view FieldTypeName(FieldDescriptor::Type field_type) {
}
GOOGLE_ABSL_LOG(FATAL) << "Can't get here.";
return {};
return NULL;
}
bool AllAscii(absl::string_view text) {
bool AllAscii(const std::string& text) {
for (int i = 0; i < text.size(); i++) {
if ((text[i] & 0x80) != 0) {
return false;
@ -465,7 +467,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";
@ -478,7 +480,7 @@ std::string DefaultValue(const FieldDescriptor* field, bool immutable,
} else if (value != value) {
return "Double.NaN";
} else {
return absl::StrCat(io::SimpleDtoa(value), "D");
return io::SimpleDtoa(value) + "D";
}
}
case FieldDescriptor::CPPTYPE_FLOAT: {
@ -490,7 +492,7 @@ std::string DefaultValue(const FieldDescriptor* field, bool immutable,
} else if (value != value) {
return "Float.NaN";
} else {
return absl::StrCat(io::SimpleFtoa(value), "F");
return io::SimpleFtoa(value) + "F";
}
}
case FieldDescriptor::CPPTYPE_BOOL:
@ -508,8 +510,7 @@ 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::StrCat(
"\"", absl::CEscape(field->default_value_string()), "\"");
return "\"" + absl::CEscape(field->default_value_string()) + "\"";
} else {
// See comments in Internal.java for gory details.
return absl::Substitute(
@ -519,14 +520,12 @@ std::string DefaultValue(const FieldDescriptor* field, bool immutable,
}
case FieldDescriptor::CPPTYPE_ENUM:
return absl::StrCat(
name_resolver->GetClassName(field->enum_type(), immutable), ".",
field->default_value_enum()->name());
return name_resolver->GetClassName(field->enum_type(), immutable) + "." +
field->default_value_enum()->name();
case FieldDescriptor::CPPTYPE_MESSAGE:
return absl::StrCat(
name_resolver->GetClassName(field->message_type(), immutable),
".getDefaultInstance()");
return name_resolver->GetClassName(field->message_type(), immutable) +
".getDefaultInstance()";
// No default because we want the compiler to complain if any new
// types are added.
@ -573,7 +572,7 @@ bool IsByteStringWithCustomDefaultValue(const FieldDescriptor* field) {
field->default_value_string() != "";
}
constexpr absl::string_view bit_masks[] = {
const char* bit_masks[] = {
"0x00000001", "0x00000002", "0x00000004", "0x00000008",
"0x00000010", "0x00000020", "0x00000040", "0x00000080",
@ -588,7 +587,10 @@ constexpr absl::string_view bit_masks[] = {
};
std::string GetBitFieldName(int index) {
return absl::StrCat("bitField", index, "_");
std::string varName = "bitField";
varName += absl::StrCat(index);
varName += "_";
return varName;
}
std::string GetBitFieldNameForBit(int bitIndex) {
@ -597,19 +599,22 @@ std::string GetBitFieldNameForBit(int bitIndex) {
namespace {
std::string GenerateGetBitInternal(absl::string_view prefix, int bitIndex) {
std::string varName = absl::StrCat(prefix, GetBitFieldNameForBit(bitIndex));
std::string GenerateGetBitInternal(const std::string& prefix, int bitIndex) {
std::string varName = prefix + GetBitFieldNameForBit(bitIndex);
int bitInVarIndex = bitIndex % 32;
return absl::StrCat("((", varName, " & ", bit_masks[bitInVarIndex],
") != 0)");
std::string mask = bit_masks[bitInVarIndex];
std::string result = "((" + varName + " & " + mask + ") != 0)";
return result;
}
std::string GenerateSetBitInternal(absl::string_view prefix, int bitIndex) {
std::string varName = absl::StrCat(prefix, GetBitFieldNameForBit(bitIndex));
std::string GenerateSetBitInternal(const std::string& prefix, int bitIndex) {
std::string varName = prefix + GetBitFieldNameForBit(bitIndex);
int bitInVarIndex = bitIndex % 32;
return absl::StrCat(varName, " |= ", bit_masks[bitInVarIndex]);
std::string mask = bit_masks[bitInVarIndex];
std::string result = varName + " |= " + mask;
return result;
}
} // namespace
@ -626,8 +631,9 @@ std::string GenerateClearBit(int bitIndex) {
std::string varName = GetBitFieldNameForBit(bitIndex);
int bitInVarIndex = bitIndex % 32;
return absl::StrCat(varName, " = (", varName, " & ~",
bit_masks[bitInVarIndex], ")");
std::string mask = bit_masks[bitInVarIndex];
std::string result = varName + " = (" + varName + " & ~" + mask + ")";
return result;
}
std::string GenerateGetBitFromLocal(int bitIndex) {
@ -675,8 +681,8 @@ bool IsReferenceType(JavaType type) {
return false;
}
absl::string_view GetCapitalizedType(const FieldDescriptor* field,
bool immutable, Options options) {
const char* GetCapitalizedType(const FieldDescriptor* field, bool immutable,
Options options) {
switch (GetType(field)) {
case FieldDescriptor::TYPE_INT32:
return "Int32";
@ -721,7 +727,7 @@ absl::string_view GetCapitalizedType(const FieldDescriptor* field,
}
GOOGLE_ABSL_LOG(FATAL) << "Can't get here.";
return {};
return NULL;
}
// 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 = '$',
absl::string_view annotation_file = "",
const std::string& annotation_file = "",
Options options = {});
// If a GeneratedMessageLite contains non-lite enums, then its verifier
@ -77,12 +77,11 @@ 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,
absl::string_view var_name, absl::string_view terminating_string,
bool enforce_lite);
const char* var_name, const char* terminating_string, bool enforce_lite);
// Converts a name to camel-case. If cap_first_letter is true, capitalize the
// first letter.
std::string ToCamelCase(absl::string_view input, bool lower_first);
std::string ToCamelCase(const std::string& input, bool lower_first);
// Similar to UnderscoresToCamelCase, but guarantees that the result is a
// complete Java identifier by adding a _ if needed.
@ -185,8 +184,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,
absl::string_view suffix) {
return absl::StrCat(descriptor->name(), suffix, ".java.pb.meta");
const std::string& suffix) {
return descriptor->name() + suffix + ".java.pb.meta";
}
// Get the unqualified name that should be used for a field's field
@ -212,21 +211,21 @@ enum JavaType {
JavaType GetJavaType(const FieldDescriptor* field);
absl::string_view PrimitiveTypeName(JavaType type);
const char* 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.
absl::string_view BoxedPrimitiveTypeName(JavaType type);
const char* BoxedPrimitiveTypeName(JavaType type);
// Kotlin source does not distinguish between primitives and non-primitives,
// but does use Kotlin-specific qualified types for them.
absl::string_view KotlinTypeName(JavaType type);
const char* 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".
absl::string_view FieldTypeName(const FieldDescriptor::Type field_type);
const char* FieldTypeName(const FieldDescriptor::Type field_type);
class ClassNameResolver;
std::string DefaultValue(const FieldDescriptor* field, bool immutable,
@ -314,8 +313,8 @@ bool IsReferenceType(JavaType type);
// Returns the capitalized name for calling relative functions in
// CodedInputStream
absl::string_view GetCapitalizedType(const FieldDescriptor* field,
bool immutable, Options options);
const char* 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 = absl::StrCat("Unknown generator option: ", option.first);
*error = "Unknown generator option: " + option.first;
return false;
}
}
@ -101,10 +101,11 @@ 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 =
absl::StrCat(package_dir, file_generator->GetKotlinClassname(), ".kt");
std::string kotlin_filename = package_dir;
kotlin_filename += file_generator->GetKotlinClassname();
kotlin_filename += ".kt";
all_files.push_back(kotlin_filename);
std::string info_full_path = absl::StrCat(kotlin_filename, ".pb.meta");
std::string info_full_path = 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 std::string(boxed ? BoxedPrimitiveTypeName(GetJavaType(field))
: PrimitiveTypeName(GetJavaType(field)));
return 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 std::string(KotlinTypeName(GetJavaType(field)));
return KotlinTypeName(GetJavaType(field));
}
}
std::string WireType(const FieldDescriptor* field) {
return absl::StrCat("com.google.protobuf.WireFormat.FieldType.",
FieldTypeName(field->type()));
return "com.google.protobuf.WireFormat.FieldType." +
std::string(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"] = absl::StrCat(
name_resolver->GetImmutableClassName(descriptor->file()), ".internal_",
UniqueFileScopeIdentifier(descriptor->message_type()), "_descriptor, ");
(*variables)["descriptor"] =
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"] =
absl::StrCat(GenerateSetBit(builderBitIndex), ";");
GenerateSetBit(builderBitIndex) + ";";
(*variables)["clear_has_field_bit_builder"] =
absl::StrCat(GenerateClearBit(builderBitIndex), ";");
GenerateClearBit(builderBitIndex) + ";";
}
} // namespace

@ -31,7 +31,6 @@
#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"
@ -70,8 +69,8 @@ std::string TypeName(const FieldDescriptor* field,
} else if (GetJavaType(field) == JAVATYPE_ENUM) {
return name_resolver->GetImmutableClassName(field->enum_type());
} else {
return std::string(boxed ? BoxedPrimitiveTypeName(GetJavaType(field))
: PrimitiveTypeName(GetJavaType(field)));
return boxed ? BoxedPrimitiveTypeName(GetJavaType(field))
: PrimitiveTypeName(GetJavaType(field));
}
}
@ -82,13 +81,13 @@ std::string KotlinTypeName(const FieldDescriptor* field,
} else if (GetJavaType(field) == JAVATYPE_ENUM) {
return name_resolver->GetImmutableClassName(field->enum_type());
} else {
return std::string(KotlinTypeName(GetJavaType(field)));
return KotlinTypeName(GetJavaType(field));
}
}
std::string WireType(const FieldDescriptor* field) {
return absl::StrCat("com.google.protobuf.WireFormat.FieldType.",
FieldTypeName(field->type()));
return "com.google.protobuf.WireFormat.FieldType." +
std::string(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"] =
absl::StrCat(GenerateSetBit(builderBitIndex), ";");
GenerateSetBit(builderBitIndex) + ";";
(*variables)["clear_has_field_bit_builder"] =
absl::StrCat(GenerateClearBit(builderBitIndex), ";");
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"] =
absl::StrCat(GenerateSetBit(messageBitIndex), ";");
GenerateSetBit(messageBitIndex) + ";";
(*variables)["clear_has_field_bit_message"] =
absl::StrCat(GenerateClearBit(messageBitIndex), ";");
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
absl::string_view StripPackageName(absl::string_view full_name,
const FileDescriptor* file) {
std::string StripPackageName(const std::string& full_name,
const FileDescriptor* file) {
if (file->package().empty()) {
return full_name;
} else {
@ -72,8 +72,7 @@ absl::string_view StripPackageName(absl::string_view full_name,
// Get the name of a message's Java class without package name prefix.
std::string ClassNameWithoutPackage(const Descriptor* descriptor,
bool immutable) {
return std::string(
StripPackageName(descriptor->full_name(), descriptor->file()));
return StripPackageName(descriptor->full_name(), descriptor->file());
}
std::string ClassNameWithoutPackageKotlin(const Descriptor* descriptor) {
@ -81,7 +80,7 @@ std::string ClassNameWithoutPackageKotlin(const Descriptor* descriptor) {
const Descriptor* temp = descriptor->containing_type();
while (temp) {
result = absl::StrCat(temp->name(), "Kt.", result);
result = temp->name() + "Kt." + result;
temp = temp->containing_type();
}
return result;
@ -95,23 +94,23 @@ std::string ClassNameWithoutPackage(const EnumDescriptor* descriptor,
if (message_descriptor == NULL) {
return descriptor->name();
} else {
return absl::StrCat(ClassNameWithoutPackage(message_descriptor, immutable),
".", descriptor->name());
return 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) {
absl::string_view full_name =
std::string full_name =
StripPackageName(descriptor->full_name(), descriptor->file());
// We don't allow nested service definitions.
GOOGLE_ABSL_CHECK(!absl::StrContains(full_name, '.'));
return std::string(full_name);
GOOGLE_ABSL_CHECK(full_name.find('.') == std::string::npos);
return full_name;
}
// Return true if a and b are equals (case insensitive).
NameEquality CheckNameEquality(absl::string_view a, absl::string_view b) {
NameEquality CheckNameEquality(const std::string& a, const std::string& b) {
if (absl::AsciiStrToUpper(a) == absl::AsciiStrToUpper(b)) {
if (a == b) {
return NameEquality::EXACT_EQUAL;
@ -123,7 +122,7 @@ NameEquality CheckNameEquality(absl::string_view a, absl::string_view b) {
// Check whether a given message or its nested types has the given class name.
bool MessageHasConflictingClassName(const Descriptor* message,
absl::string_view classname,
const std::string& classname,
NameEquality equality_mode) {
if (CheckNameEquality(message->name(), classname) == equality_mode) {
return true;
@ -182,18 +181,18 @@ std::string ClassNameResolver::GetFileClassName(const FileDescriptor* file,
std::string ClassNameResolver::GetFileClassName(const FileDescriptor* file,
bool immutable, bool kotlin) {
if (kotlin) {
return absl::StrCat(GetFileImmutableClassName(file), "Kt");
return GetFileImmutableClassName(file) + "Kt";
} else if (immutable) {
return GetFileImmutableClassName(file);
} else {
return absl::StrCat("Mutable", GetFileImmutableClassName(file));
return "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,
absl::string_view classname,
const std::string& classname,
NameEquality equality_mode) {
for (int i = 0; i < file->enum_type_count(); i++) {
if (CheckNameEquality(file->enum_type(i)->name(), classname) ==
@ -221,7 +220,7 @@ std::string ClassNameResolver::GetDescriptorClassName(
if (options_.opensource_runtime) {
return GetFileImmutableClassName(file);
} else {
return absl::StrCat(GetFileImmutableClassName(file), "InternalDescriptors");
return GetFileImmutableClassName(file) + "InternalDescriptors";
}
}
@ -241,14 +240,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(
absl::string_view name_without_package, const FileDescriptor* file,
const std::string& 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(
absl::string_view name_without_package, const FileDescriptor* file,
const std::string& name_without_package, const FileDescriptor* file,
bool immutable, bool is_own_file, bool kotlin) {
std::string result;
if (is_own_file) {
@ -257,10 +256,10 @@ std::string ClassNameResolver::GetClassFullName(
result = GetClassName(file, immutable, kotlin);
}
if (!result.empty()) {
absl::StrAppend(&result, ".");
result += '.';
}
absl::StrAppend(&result, name_without_package);
if (kotlin) absl::StrAppend(&result, "Kt");
result += name_without_package;
if (kotlin) result += "Kt";
return result;
}
@ -302,13 +301,13 @@ std::string ClassNameResolver::GetClassName(const ServiceDescriptor* descriptor,
// Get the Java Class style full name of a message.
std::string ClassNameResolver::GetJavaClassFullName(
absl::string_view name_without_package, const FileDescriptor* file,
const std::string& name_without_package, const FileDescriptor* file,
bool immutable) {
return GetJavaClassFullName(name_without_package, file, immutable, false);
}
std::string ClassNameResolver::GetJavaClassFullName(
absl::string_view name_without_package, const FileDescriptor* file,
const std::string& name_without_package, const FileDescriptor* file,
bool immutable, bool kotlin) {
std::string result;
if (MultipleJavaFiles(file, immutable)) {
@ -329,15 +328,14 @@ std::string ClassNameResolver::GetExtensionIdentifierName(
std::string ClassNameResolver::GetExtensionIdentifierName(
const FieldDescriptor* descriptor, bool immutable, bool kotlin) {
return absl::StrCat(
GetClassName(descriptor->containing_type(), immutable, kotlin), ".",
descriptor->name());
return 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) ? absl::StrCat(name, "_") : name;
return IsForbiddenKotlin(name) ? name + "_" : name;
}
std::string ClassNameResolver::GetJavaImmutableClassName(
@ -372,14 +370,14 @@ std::string ClassNameResolver::GetJavaMutableClassName(
std::string ClassNameResolver::GetDowngradedFileClassName(
const FileDescriptor* file) {
return absl::StrCat("Downgraded", GetFileClassName(file, false));
return "Downgraded" + GetFileClassName(file, false);
}
std::string ClassNameResolver::GetDowngradedClassName(
const Descriptor* descriptor) {
return absl::StrCat(FileJavaPackage(descriptor->file(), true, options_), ".",
GetDowngradedFileClassName(descriptor->file()), ".",
ClassNameWithoutPackage(descriptor, false));
return 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,
absl::string_view classname,
const std::string& 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(absl::string_view name_without_package,
std::string GetClassFullName(const std::string& name_without_package,
const FileDescriptor* file, bool immutable,
bool is_own_file);
std::string GetClassFullName(absl::string_view name_without_package,
std::string GetClassFullName(const std::string& 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(absl::string_view name_without_package,
std::string GetJavaClassFullName(const std::string& name_without_package,
const FileDescriptor* file, bool immutable);
std::string GetJavaClassFullName(absl::string_view name_without_package,
std::string GetJavaClassFullName(const std::string& 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(absl::string_view field_name) {
bool IsForbidden(const std::string& 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.
absl::StrAppend(&field_name, "#");
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)) {
absl::StrAppend(&name, "_");
return 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(absl::string_view input,
bool cap_next_letter);
std::string UnderscoresToCamelCase(const std::string& name,
bool cap_first_letter);
// Requires:
// field != NULL
// Returns:

@ -81,17 +81,16 @@ 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(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));
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));
CommandLineInterface cli;
cli.SetInputsAreProtoPathRelative(true);
@ -101,9 +100,9 @@ TEST(JavaPluginTest, PluginTest) {
cli.RegisterGenerator("--java_out", &java_generator, "");
cli.RegisterGenerator("--test_out", &test_generator, "");
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());
std::string proto_path = "-I" + TestTempDir();
std::string java_out = "--java_out=" + TestTempDir();
std::string test_out = "--test_out=" + TestTempDir();
const char* argv[] = {"protoc", proto_path.c_str(), java_out.c_str(),
test_out.c_str(), "test.proto"};
@ -114,8 +113,8 @@ TEST(JavaPluginTest, PluginTest) {
// expect
std::string output;
GOOGLE_ABSL_CHECK_OK(File::GetContents(absl::StrCat(TestTempDir(), "/Test.java"),
&output, true));
GOOGLE_ABSL_CHECK_OK(File::GetContents(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"] = std::string(PrimitiveTypeName(javaType));
(*variables)["boxed_type"] = std::string(BoxedPrimitiveTypeName(javaType));
(*variables)["kt_type"] = std::string(KotlinTypeName(javaType));
(*variables)["type"] = PrimitiveTypeName(javaType);
(*variables)["boxed_type"] = BoxedPrimitiveTypeName(javaType);
(*variables)["kt_type"] = KotlinTypeName(javaType);
variables->insert({"field_type", (*variables)["type"]});
std::string name = (*variables)["name"];
@ -112,10 +112,10 @@ void SetPrimitiveVariables(
(*variables)["default_init"] =
IsDefaultValueJavaDefault(descriptor)
? ""
: absl::StrCat("= ", ImmutableDefaultValue(descriptor, name_resolver,
context->options()));
(*variables)["capitalized_type"] = std::string(GetCapitalizedType(
descriptor, /* immutable = */ true, context->options()));
: ("= " + ImmutableDefaultValue(descriptor, name_resolver,
context->options()));
(*variables)["capitalized_type"] = 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"] =
absl::StrCat(GenerateSetBitToLocal(messageBitIndex), ";");
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"] =
absl::StrCat(GenerateSetBit(builderBitIndex), ";");
GenerateSetBit(builderBitIndex) + ";";
(*variables)["clear_has_field_bit_builder"] =
absl::StrCat(GenerateClearBit(builderBitIndex), ";");
GenerateClearBit(builderBitIndex) + ";";
}
} // namespace
@ -506,7 +506,7 @@ void ImmutablePrimitiveFieldGenerator::GenerateHashCode(
}
std::string ImmutablePrimitiveFieldGenerator::GetBoxedType() const {
return std::string(BoxedPrimitiveTypeName(GetJavaType(descriptor_)));
return BoxedPrimitiveTypeName(GetJavaType(descriptor_));
}
// ===================================================================
@ -1069,7 +1069,7 @@ void RepeatedImmutablePrimitiveFieldGenerator::GenerateHashCode(
}
std::string RepeatedImmutablePrimitiveFieldGenerator::GetBoxedType() const {
return std::string(BoxedPrimitiveTypeName(GetJavaType(descriptor_)));
return BoxedPrimitiveTypeName(GetJavaType(descriptor_));
}
} // namespace java

@ -70,14 +70,14 @@ void SetPrimitiveVariables(
Context* context) {
SetCommonFieldVariables(descriptor, info, variables);
JavaType javaType = GetJavaType(descriptor);
(*variables)["type"] = std::string(PrimitiveTypeName(javaType));
(*variables)["boxed_type"] = std::string(BoxedPrimitiveTypeName(javaType));
(*variables)["kt_type"] = std::string(KotlinTypeName(javaType));
(*variables)["type"] = PrimitiveTypeName(javaType);
(*variables)["boxed_type"] = BoxedPrimitiveTypeName(javaType);
(*variables)["kt_type"] = KotlinTypeName(javaType);
variables->insert({"field_type", (*variables)["type"]});
(*variables)["default"] =
ImmutableDefaultValue(descriptor, name_resolver, context->options());
(*variables)["capitalized_type"] = std::string(GetCapitalizedType(
descriptor, /* immutable = */ true, context->options()));
(*variables)["capitalized_type"] = 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::StrCat(absl::AsciiStrToUpper(name), "_DEFAULT_VALUE");
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"] =
absl::StrCat(GenerateSetBit(messageBitIndex), ";");
GenerateSetBit(messageBitIndex) + ";";
(*variables)["clear_has_field_bit_message"] =
absl::StrCat(GenerateClearBit(messageBitIndex), ";");
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 std::string(BoxedPrimitiveTypeName(GetJavaType(descriptor_)));
return BoxedPrimitiveTypeName(GetJavaType(descriptor_));
}
// ===================================================================
@ -796,7 +796,7 @@ void RepeatedImmutablePrimitiveFieldLiteGenerator::GenerateInitializationCode(
}
std::string RepeatedImmutablePrimitiveFieldLiteGenerator::GetBoxedType() const {
return std::string(BoxedPrimitiveTypeName(GetJavaType(descriptor_)));
return 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 = absl::StrCat(package_dir, classname, ".java");
std::string filename = 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 = absl::StrCat(classname, ".java.pb.meta");
std::string info_full_path = absl::StrCat(filename, ".pb.meta");
std::string info_relative_path = classname + ".java.pb.meta";
std::string info_full_path = 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 = absl::StrCat(package, ".", classname);
full_name = 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"] = absl::StrCat(
"= ",
ImmutableDefaultValue(descriptor, name_resolver, context->options()));
(*variables)["default_init"] =
"= " +
ImmutableDefaultValue(descriptor, name_resolver, context->options());
(*variables)["capitalized_type"] = "String";
(*variables)["tag"] =
absl::StrCat(static_cast<int32_t>(WireFormat::MakeTag(descriptor)));
@ -79,15 +79,14 @@ void SetPrimitiveVariables(
WireFormat::TagSize(descriptor->number(), GetType(descriptor)));
(*variables)["null_check"] =
"if (value == null) { throw new NullPointerException(); }";
(*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");
(*variables)["isStringEmpty"] = "com.google.protobuf.GeneratedMessage" +
GeneratedCodeVersionSuffix() +
".isStringEmpty";
(*variables)["writeString"] = "com.google.protobuf.GeneratedMessage" +
GeneratedCodeVersionSuffix() + ".writeString";
(*variables)["computeStringSize"] = "com.google.protobuf.GeneratedMessage" +
GeneratedCodeVersionSuffix() +
".computeStringSize";
// TODO(birdo): Add @deprecated javadoc when generating javadoc is supported
// by the proto compiler
@ -109,7 +108,7 @@ void SetPrimitiveVariables(
// Note that these have a trailing ";".
(*variables)["set_has_field_bit_message"] =
absl::StrCat(GenerateSetBit(messageBitIndex), ";");
GenerateSetBit(messageBitIndex) + ";";
(*variables)["is_field_present_message"] = GenerateGetBit(messageBitIndex);
} else {
@ -131,9 +130,9 @@ void SetPrimitiveVariables(
(*variables)["get_has_field_bit_from_local"] =
GenerateGetBitFromLocal(builderBitIndex);
(*variables)["set_has_field_bit_builder"] =
absl::StrCat(GenerateSetBit(builderBitIndex), ";");
GenerateSetBit(builderBitIndex) + ";";
(*variables)["clear_has_field_bit_builder"] =
absl::StrCat(GenerateClearBit(builderBitIndex), ";");
GenerateClearBit(builderBitIndex) + ";";
}
} // namespace

@ -69,9 +69,9 @@ void SetPrimitiveVariables(
(*variables)["default"] =
ImmutableDefaultValue(descriptor, name_resolver, context->options());
(*variables)["default_init"] = absl::StrCat(
"= ",
ImmutableDefaultValue(descriptor, name_resolver, context->options()));
(*variables)["default_init"] =
"= " +
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"] =
absl::StrCat(GenerateSetBit(messageBitIndex), ";");
GenerateSetBit(messageBitIndex) + ";";
(*variables)["clear_has_field_bit_message"] =
absl::StrCat(GenerateClearBit(messageBitIndex), ";");
GenerateClearBit(messageBitIndex) + ";";
(*variables)["is_field_present_message"] = GenerateGetBit(messageBitIndex);
} else {

@ -6909,13 +6909,14 @@ 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;
}
}
@ -7447,7 +7448,8 @@ 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;
@ -7939,7 +7941,8 @@ 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") {
@ -7947,9 +7950,10 @@ 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;
@ -7957,7 +7961,8 @@ 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();
@ -7999,7 +8004,9 @@ 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.
@ -8013,7 +8020,8 @@ 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.
@ -8107,11 +8115,12 @@ 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) {
string_iter->append("test");
absl::StrAppend(&*string_iter, "test");
}
RepeatedPtrField<std::string>::const_iterator string_const_iter;
RepeatedPtrField<std::string>::const_iterator string_const_end;

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

@ -910,12 +910,15 @@ 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