Move *FieldGeneator over to Printer::Emit.

Minor updates to how comments are output to have a method
so folks can directly call Emit to handle multline things correctly.

PiperOrigin-RevId: 546921123
pull/13254/head
Thomas Van Lenten 2 years ago committed by Copybara-Service
parent b01794cb3b
commit aa72b19bf9
  1. 11
      src/google/protobuf/compiler/objectivec/enum_field.cc
  2. 2
      src/google/protobuf/compiler/objectivec/enum_field.h
  3. 171
      src/google/protobuf/compiler/objectivec/field.cc
  4. 2
      src/google/protobuf/compiler/objectivec/field.h
  5. 11
      src/google/protobuf/compiler/objectivec/helpers.cc
  6. 13
      src/google/protobuf/compiler/objectivec/map_field.cc
  7. 2
      src/google/protobuf/compiler/objectivec/map_field.h

@ -142,12 +142,11 @@ RepeatedEnumFieldGenerator::RepeatedEnumFieldGenerator(
variables_["array_storage_type"] = "GPBEnumArray";
}
void RepeatedEnumFieldGenerator::FinishInitialization() {
RepeatedFieldGenerator::FinishInitialization();
std::string name = variables_["name"];
std::string storage_type = variables_["storage_type"];
variables_["array_comment"] =
absl::StrCat("// |", name, "| contains |", storage_type, "|\n");
void RepeatedEnumFieldGenerator::EmitArrayComment(io::Printer* printer) const {
auto vars = printer->WithVars(variables_);
printer->Emit(R"objc(
// |$name$| contains |$storage_type$|
)objc");
}
// NOTE: RepeatedEnumFieldGenerator::DetermineForwardDeclarations isn't needed

@ -62,7 +62,7 @@ class RepeatedEnumFieldGenerator : public RepeatedFieldGenerator {
friend FieldGenerator* FieldGenerator::Make(const FieldDescriptor* field);
public:
void FinishInitialization() override;
void EmitArrayComment(io::Printer* printer) const override;
protected:
explicit RepeatedEnumFieldGenerator(const FieldDescriptor* descriptor);

@ -30,8 +30,6 @@
#include "google/protobuf/compiler/objectivec/field.h"
#include <iostream>
#include <ostream>
#include <string>
#include <vector>
@ -69,12 +67,6 @@ void SetCommonFieldVariables(
UnCamelCaseFieldName(camel_case_name, descriptor));
const bool needs_custom_name = (raw_field_name != un_camel_case_name);
SourceLocation location;
if (descriptor->GetSourceLocation(&location)) {
(*variables)["comments"] = BuildCommentsString(location, true);
} else {
(*variables)["comments"] = "\n";
}
const std::string& classname = ClassName(descriptor->containing_type());
(*variables)["classname"] = classname;
(*variables)["name"] = camel_case_name;
@ -222,7 +214,8 @@ FieldGenerator::FieldGenerator(const FieldDescriptor* descriptor)
}
void FieldGenerator::GenerateFieldNumberConstant(io::Printer* printer) const {
printer->Print(variables_, "$field_number_name$ = $field_number$,\n");
auto vars = printer->WithVars(variables_);
printer->Emit("$field_number_name$ = $field_number$,\n");
}
void FieldGenerator::GenerateCFunctionDeclarations(io::Printer* printer) const {
@ -248,36 +241,27 @@ void FieldGenerator::DetermineObjectiveCClassDefinitions(
void FieldGenerator::GenerateFieldDescription(io::Printer* printer,
bool include_default) const {
// Printed in the same order as the structure decl.
if (include_default) {
// clang-format off
printer->Print(
variables_,
"{\n"
" .defaultValue.$default_name$ = $default$,\n"
" .core.name = \"$name$\",\n"
" .core.dataTypeSpecific.$dataTypeSpecific_name$ = $dataTypeSpecific_value$,\n"
" .core.number = $field_number_name$,\n"
" .core.hasIndex = $has_index$,\n"
" .core.offset = $storage_offset_value$,$storage_offset_comment$\n"
" .core.flags = $fieldflags$,\n"
" .core.dataType = GPBDataType$field_type$,\n"
"},\n");
// clang-format on
} else {
// clang-format off
printer->Print(
variables_,
"{\n"
" .name = \"$name$\",\n"
" .dataTypeSpecific.$dataTypeSpecific_name$ = $dataTypeSpecific_value$,\n"
" .number = $field_number_name$,\n"
" .hasIndex = $has_index$,\n"
" .offset = $storage_offset_value$,$storage_offset_comment$\n"
" .flags = $fieldflags$,\n"
" .dataType = GPBDataType$field_type$,\n"
"},\n");
// clang-format on
}
auto vars = printer->WithVars(variables_);
printer->Emit(
{{"prefix", include_default ? ".core" : ""},
{"maybe_default",
[&] {
if (include_default) {
printer->Emit(".defaultValue.$default_name$ = $default$,\n");
}
}}},
R"objc(
{
$maybe_default$,
$prefix$.name = "$name$",
$prefix$.dataTypeSpecific.$dataTypeSpecific_name$ = $dataTypeSpecific_value$,
$prefix$.number = $field_number_name$,
$prefix$.hasIndex = $has_index$,
$prefix$.offset = $storage_offset_value$,$storage_offset_comment$
$prefix$.flags = $fieldflags$,
$prefix$.dataType = GPBDataType$field_type$,
},
)objc");
}
void FieldGenerator::SetRuntimeHasBit(int has_index) {
@ -321,33 +305,34 @@ SingleFieldGenerator::SingleFieldGenerator(const FieldDescriptor* descriptor)
void SingleFieldGenerator::GenerateFieldStorageDeclaration(
io::Printer* printer) const {
printer->Print(variables_, "$storage_type$ $name$;\n");
auto vars = printer->WithVars(variables_);
printer->Emit("$storage_type$ $name$;\n");
}
void SingleFieldGenerator::GeneratePropertyDeclaration(
io::Printer* printer) const {
printer->Print(variables_, "$comments$");
// clang-format off
printer->Print(
variables_,
"@property(nonatomic, readwrite) $property_type$ $name$$deprecated_attribute$;\n");
// clang-format on
auto vars = printer->WithVars(variables_);
printer->Emit(
{{"comments", [&] { EmitCommentsString(printer, descriptor_, true); }}},
R"objc(
$comments$
@property(nonatomic, readwrite) $property_type$ $name$$deprecated_attribute$;
)objc");
if (WantsHasProperty()) {
// clang-format off
printer->Print(
variables_,
"@property(nonatomic, readwrite) BOOL has$capitalized_name$$deprecated_attribute$;\n");
// clang-format on
printer->Emit(R"objc(
@property(nonatomic, readwrite) BOOL has$capitalized_name$$deprecated_attribute$;
)objc");
}
printer->Print("\n");
printer->Emit("\n");
}
void SingleFieldGenerator::GeneratePropertyImplementation(
io::Printer* printer) const {
auto vars = printer->WithVars(variables_);
if (WantsHasProperty()) {
printer->Print(variables_, "@dynamic has$capitalized_name$, $name$;\n");
printer->Emit("@dynamic has$capitalized_name$, $name$;\n");
} else {
printer->Print(variables_, "@dynamic $name$;\n");
printer->Emit("@dynamic $name$;\n");
}
}
@ -369,7 +354,8 @@ ObjCObjFieldGenerator::ObjCObjFieldGenerator(const FieldDescriptor* descriptor)
void ObjCObjFieldGenerator::GenerateFieldStorageDeclaration(
io::Printer* printer) const {
printer->Print(variables_, "$storage_type$ *$name$;\n");
auto vars = printer->WithVars(variables_);
printer->Emit("$storage_type$ *$name$;\n");
}
void ObjCObjFieldGenerator::GeneratePropertyDeclaration(
@ -378,36 +364,32 @@ void ObjCObjFieldGenerator::GeneratePropertyDeclaration(
// it uses pointers and deals with Objective-C's rules around storage name
// conventions (init*, new*, etc.)
printer->Print(variables_, "$comments$");
// clang-format off
printer->Print(
variables_,
"@property(nonatomic, readwrite, $property_storage_attribute$, null_resettable) $property_type$ *$name$$storage_attribute$$deprecated_attribute$;\n");
// clang-format on
auto vars = printer->WithVars(variables_);
printer->Emit(
{{"comments", [&] { EmitCommentsString(printer, descriptor_, true); }}},
R"objc(
$comments$
@property(nonatomic, readwrite, $property_storage_attribute$, null_resettable) $property_type$ *$name$$storage_attribute$$deprecated_attribute$;
)objc");
if (WantsHasProperty()) {
// clang-format off
printer->Print(
variables_,
"/** Test to see if @c $name$ has been set. */\n"
"@property(nonatomic, readwrite) BOOL has$capitalized_name$$deprecated_attribute$;\n");
// clang-format on
printer->Emit(R"objc(
/** Test to see if @c $name$ has been set. */
@property(nonatomic, readwrite) BOOL has$capitalized_name$$deprecated_attribute$;
)objc");
}
if (IsInitName(variables_.find("name")->second)) {
// If property name starts with init we need to annotate it to get past ARC.
// http://stackoverflow.com/questions/18723226/how-do-i-annotate-an-objective-c-property-with-an-objc-method-family/18723227#18723227
printer->Print(variables_,
"- ($property_type$ *)$name$ "
"GPB_METHOD_FAMILY_NONE$deprecated_attribute$;\n");
printer->Emit(R"objc(
- ($property_type$ *)$name$ GPB_METHOD_FAMILY_NONE$deprecated_attribute$;
)objc");
}
printer->Print("\n");
printer->Emit("\n");
}
RepeatedFieldGenerator::RepeatedFieldGenerator(
const FieldDescriptor* descriptor)
: ObjCObjFieldGenerator(descriptor) {
// Default to no comment and let the cases needing it fill it in.
variables_["array_comment"] = "";
}
: ObjCObjFieldGenerator(descriptor) {}
void RepeatedFieldGenerator::FinishInitialization() {
FieldGenerator::FinishInitialization();
@ -418,12 +400,14 @@ void RepeatedFieldGenerator::FinishInitialization() {
void RepeatedFieldGenerator::GenerateFieldStorageDeclaration(
io::Printer* printer) const {
printer->Print(variables_, "$array_storage_type$ *$name$;\n");
auto vars = printer->WithVars(variables_);
printer->Emit("$array_storage_type$ *$name$;\n");
}
void RepeatedFieldGenerator::GeneratePropertyImplementation(
io::Printer* printer) const {
printer->Print(variables_, "@dynamic $name$, $name$_Count;\n");
auto vars = printer->WithVars(variables_);
printer->Emit("@dynamic $name$, $name$_Count;\n");
}
void RepeatedFieldGenerator::GeneratePropertyDeclaration(
@ -434,30 +418,35 @@ void RepeatedFieldGenerator::GeneratePropertyDeclaration(
// dealing with needing Objective-C's rules around storage name conventions
// (init*, new*, etc.)
// clang-format off
printer->Print(
variables_,
"$comments$"
"$array_comment$"
"@property(nonatomic, readwrite, strong, null_resettable) $array_property_type$ *$name$$storage_attribute$$deprecated_attribute$;\n"
"/** The number of items in @c $name$ without causing the container to be created. */\n"
"@property(nonatomic, readonly) NSUInteger $name$_Count$deprecated_attribute$;\n");
// clang-format on
auto vars = printer->WithVars(variables_);
printer->Emit(
{{"comments", [&] { EmitCommentsString(printer, descriptor_, true); }},
{"array_comment", [&] { EmitArrayComment(printer); }}},
R"objc(
$comments$
$array_comment$
@property(nonatomic, readwrite, strong, null_resettable) $array_property_type$ *$name$$storage_attribute$$deprecated_attribute$;
/** The number of items in @c $name$ without causing the container to be created. */
@property(nonatomic, readonly) NSUInteger $name$_Count$deprecated_attribute$;
)objc");
if (IsInitName(variables_.find("name")->second)) {
// If property name starts with init we need to annotate it to get past ARC.
// http://stackoverflow.com/questions/18723226/how-do-i-annotate-an-objective-c-property-with-an-objc-method-family/18723227#18723227
// clang-format off
printer->Print(variables_,
"- ($array_property_type$ *)$name$ GPB_METHOD_FAMILY_NONE$deprecated_attribute$;\n");
// clang-format on
printer->Emit(R"objc(
- ($array_property_type$ *)$name$ GPB_METHOD_FAMILY_NONE$deprecated_attribute$;
)objc");
}
printer->Print("\n");
printer->Emit("\n");
}
bool RepeatedFieldGenerator::RuntimeUsesHasBit() const {
return false; // The array (or map/dict) having anything is what is used.
}
void RepeatedFieldGenerator::EmitArrayComment(io::Printer* printer) const {
// Nothing for the default
}
FieldGeneratorMap::FieldGeneratorMap(const Descriptor* descriptor)
: descriptor_(descriptor),
field_generators_(descriptor->field_count()),

@ -155,6 +155,8 @@ class RepeatedFieldGenerator : public ObjCObjFieldGenerator {
bool RuntimeUsesHasBit() const override;
virtual void EmitArrayComment(io::Printer* printer) const;
protected:
explicit RepeatedFieldGenerator(const FieldDescriptor* descriptor);
void FinishInitialization() override;

@ -360,7 +360,14 @@ std::vector<std::string> ExtractAndEscapeLines(const SourceLocation& location) {
lines.reserve(raw_lines.size());
for (absl::string_view l : raw_lines) {
lines.push_back(absl::StrReplaceAll(
absl::StripPrefix(l, " "),
// Strip any trailing whitespace to avoid any warnings on the generated
// code; but only stip one leading white space as that tends to be
// carried over from the .proto file, and we don't want extra spaces,
// the formatting below will ensure there is a space.
// NOTE: There could be >1 leading whitespace if the .proto file has
// formatted comments (see the WKTs), so we maintain any additional
// leading whitespace.
absl::StripTrailingAsciiWhitespace(absl::StripPrefix(l, " ")),
{// HeaderDoc and appledoc use '\' and '@' for markers; escape them.
{"\\", "\\\\"},
{"@", "\\@"},
@ -419,7 +426,7 @@ void EmitCommentsString(io::Printer* printer, const SourceLocation& location,
{"lines",
[&] {
for (absl::string_view line : lines) {
printer->Emit({{"text", absl::StripAsciiWhitespace(line)}}, R"(
printer->Emit({{"text", line}}, R"(
*$ text$
)");
}

@ -150,17 +150,18 @@ MapFieldGenerator::MapFieldGenerator(const FieldDescriptor* descriptor)
value_field_generator_->variable("dataTypeSpecific_value");
}
void MapFieldGenerator::FinishInitialization() {
RepeatedFieldGenerator::FinishInitialization();
void MapFieldGenerator::EmitArrayComment(io::Printer* printer) const {
// Use the array_comment support in RepeatedFieldGenerator to output what the
// values in the map are.
const FieldDescriptor* value_descriptor =
descriptor_->message_type()->map_value();
if (GetObjectiveCType(value_descriptor) == OBJECTIVECTYPE_ENUM) {
std::string name = variables_["name"];
variables_["array_comment"] =
absl::StrCat("// |", name, "| values are |",
value_field_generator_->variable("storage_type"), "|\n");
printer->Emit(
{{"name", variable("name")},
{"storage_type", value_field_generator_->variable("storage_type")}},
R"objc(
// |$name$| values are |$storage_type$|
)objc");
}
}

@ -46,7 +46,7 @@ class MapFieldGenerator : public RepeatedFieldGenerator {
friend FieldGenerator* FieldGenerator::Make(const FieldDescriptor* field);
public:
void FinishInitialization() override;
void EmitArrayComment(io::Printer* printer) const override;
MapFieldGenerator(const MapFieldGenerator&) = delete;
MapFieldGenerator& operator=(const MapFieldGenerator&) = delete;

Loading…
Cancel
Save