[ObjC] Wire generation options through Enum, Extension, Message, Oneof generators.

Swap out where the bool for stripping custom options was passed for
direct access to the generation options.

PiperOrigin-RevId: 568217321
pull/14183/head
Thomas Van Lenten 2 years ago committed by Copybara-Service
parent e57166b65a
commit cce7fc4f06
  1. 4
      src/google/protobuf/compiler/objectivec/enum.cc
  2. 4
      src/google/protobuf/compiler/objectivec/enum.h
  3. 4
      src/google/protobuf/compiler/objectivec/extension.cc
  4. 4
      src/google/protobuf/compiler/objectivec/extension.h
  5. 28
      src/google/protobuf/compiler/objectivec/file.cc
  6. 19
      src/google/protobuf/compiler/objectivec/message.cc
  7. 8
      src/google/protobuf/compiler/objectivec/message.h
  8. 4
      src/google/protobuf/compiler/objectivec/oneof.cc
  9. 4
      src/google/protobuf/compiler/objectivec/oneof.h

@ -16,6 +16,7 @@
#include "absl/strings/str_cat.h"
#include "google/protobuf/compiler/objectivec/helpers.h"
#include "google/protobuf/compiler/objectivec/names.h"
#include "google/protobuf/compiler/objectivec/options.h"
#include "google/protobuf/compiler/objectivec/text_format_decode_data.h"
#include "google/protobuf/descriptor.h"
#include "google/protobuf/io/printer.h"
@ -36,7 +37,8 @@ std::string SafelyPrintIntToCode(int v) {
}
} // namespace
EnumGenerator::EnumGenerator(const EnumDescriptor* descriptor)
EnumGenerator::EnumGenerator(const EnumDescriptor* descriptor,
const GenerationOptions& generation_options)
: descriptor_(descriptor), name_(EnumName(descriptor_)) {
// Track the names for the enum values, and if an alias overlaps a base
// value, skip making a name for it. Likewise if two alias overlap, the

@ -12,6 +12,7 @@
#include <vector>
#include "absl/container/flat_hash_set.h"
#include "google/protobuf/compiler/objectivec/options.h"
#include "google/protobuf/descriptor.h"
#include "google/protobuf/io/printer.h"
@ -22,7 +23,8 @@ namespace objectivec {
class EnumGenerator {
public:
explicit EnumGenerator(const EnumDescriptor* descriptor);
EnumGenerator(const EnumDescriptor* descriptor,
const GenerationOptions& generation_options);
~EnumGenerator() = default;
EnumGenerator(const EnumGenerator&) = delete;

@ -17,6 +17,7 @@
#include "absl/strings/string_view.h"
#include "google/protobuf/compiler/objectivec/helpers.h"
#include "google/protobuf/compiler/objectivec/names.h"
#include "google/protobuf/compiler/objectivec/options.h"
#include "google/protobuf/descriptor.h"
#include "google/protobuf/descriptor.pb.h"
#include "google/protobuf/io/printer.h"
@ -28,7 +29,8 @@ namespace objectivec {
ExtensionGenerator::ExtensionGenerator(
absl::string_view root_or_message_class_name,
const FieldDescriptor* descriptor)
const FieldDescriptor* descriptor,
const GenerationOptions& generation_options)
: method_name_(ExtensionMethodName(descriptor)),
full_method_name_(
absl::StrCat(root_or_message_class_name, "_", method_name_)),

@ -13,6 +13,7 @@
#include "absl/container/btree_set.h"
#include "absl/container/flat_hash_set.h"
#include "absl/strings/string_view.h"
#include "google/protobuf/compiler/objectivec/options.h"
#include "google/protobuf/descriptor.h"
#include "google/protobuf/io/printer.h"
@ -24,7 +25,8 @@ namespace objectivec {
class ExtensionGenerator {
public:
ExtensionGenerator(absl::string_view root_or_message_class_name,
const FieldDescriptor* descriptor);
const FieldDescriptor* descriptor,
const GenerationOptions& generation_options);
~ExtensionGenerator() = default;
ExtensionGenerator(const ExtensionGenerator&) = delete;

@ -119,10 +119,10 @@ void MakeDescriptors(
std::vector<std::unique_ptr<EnumGenerator>>* enum_generators,
std::vector<std::unique_ptr<ExtensionGenerator>>* extension_generators,
std::vector<std::unique_ptr<MessageGenerator>>* message_generators,
bool strip_custom_options) {
const GenerationOptions& generation_options) {
for (int i = 0; i < descriptor->enum_type_count(); i++) {
enum_generators->emplace_back(
std::make_unique<EnumGenerator>(descriptor->enum_type(i)));
enum_generators->emplace_back(std::make_unique<EnumGenerator>(
descriptor->enum_type(i), generation_options));
}
for (int i = 0; i < descriptor->nested_type_count(); i++) {
const Descriptor* message_type = descriptor->nested_type(i);
@ -132,12 +132,11 @@ void MakeDescriptors(
continue;
}
message_generators->emplace_back(std::make_unique<MessageGenerator>(
file_description_name, message_type));
message_generators->back()->AddExtensionGenerators(extension_generators,
strip_custom_options);
file_description_name, message_type, generation_options));
message_generators->back()->AddExtensionGenerators(extension_generators);
MakeDescriptors(message_type, file_description_name, enum_generators,
extension_generators, message_generators,
strip_custom_options);
generation_options);
}
}
@ -253,15 +252,15 @@ FileGenerator::FileGenerator(const FileDescriptor* file,
file_description_name_(FileClassName(file) + "_FileDescription"),
is_bundled_proto_(IsProtobufLibraryBundledProtoFile(file)) {
for (int i = 0; i < file_->enum_type_count(); i++) {
enum_generators_.emplace_back(
std::make_unique<EnumGenerator>(file_->enum_type(i)));
enum_generators_.emplace_back(std::make_unique<EnumGenerator>(
file_->enum_type(i), generation_options));
}
for (int i = 0; i < file_->extension_count(); i++) {
const FieldDescriptor* extension = file_->extension(i);
if (!generation_options.strip_custom_options ||
!ExtensionIsCustomOption(extension)) {
extension_generators_.push_back(
std::make_unique<ExtensionGenerator>(root_class_name_, extension));
extension_generators_.push_back(std::make_unique<ExtensionGenerator>(
root_class_name_, extension, generation_options));
}
}
file_scoped_extension_count_ = extension_generators_.size();
@ -273,12 +272,11 @@ FileGenerator::FileGenerator(const FileDescriptor* file,
continue;
}
message_generators_.emplace_back(std::make_unique<MessageGenerator>(
file_description_name_, message_type));
message_generators_.back()->AddExtensionGenerators(
&extension_generators_, generation_options.strip_custom_options);
file_description_name_, message_type, generation_options));
message_generators_.back()->AddExtensionGenerators(&extension_generators_);
MakeDescriptors(message_type, file_description_name_, &enum_generators_,
&extension_generators_, &message_generators_,
generation_options.strip_custom_options);
generation_options);
}
}

@ -25,6 +25,7 @@
#include "google/protobuf/compiler/objectivec/helpers.h"
#include "google/protobuf/compiler/objectivec/names.h"
#include "google/protobuf/compiler/objectivec/oneof.h"
#include "google/protobuf/compiler/objectivec/options.h"
#include "google/protobuf/compiler/objectivec/text_format_decode_data.h"
#include "google/protobuf/descriptor.h"
#include "google/protobuf/descriptor.pb.h"
@ -192,9 +193,11 @@ const FieldDescriptor** SortFieldsByStorageSize(const Descriptor* descriptor) {
} // namespace
MessageGenerator::MessageGenerator(const std::string& file_description_name,
const Descriptor* descriptor)
const Descriptor* descriptor,
const GenerationOptions& generation_options)
: file_description_name_(file_description_name),
descriptor_(descriptor),
generation_options_(generation_options),
field_generators_(descriptor),
class_name_(ClassName(descriptor_)),
deprecated_attribute_(
@ -202,8 +205,8 @@ MessageGenerator::MessageGenerator(const std::string& file_description_name,
ABSL_DCHECK(!descriptor->options().map_entry())
<< "error: MessageGenerator create of a map<>!";
for (int i = 0; i < descriptor_->real_oneof_decl_count(); i++) {
oneof_generators_.push_back(
std::make_unique<OneofGenerator>(descriptor_->real_oneof_decl(i)));
oneof_generators_.push_back(std::make_unique<OneofGenerator>(
descriptor_->real_oneof_decl(i), generation_options));
}
// Assign has bits:
@ -233,13 +236,13 @@ MessageGenerator::MessageGenerator(const std::string& file_description_name,
}
void MessageGenerator::AddExtensionGenerators(
std::vector<std::unique_ptr<ExtensionGenerator>>* extension_generators,
bool strip_custom_options) {
std::vector<std::unique_ptr<ExtensionGenerator>>* extension_generators) {
for (int i = 0; i < descriptor_->extension_count(); i++) {
const FieldDescriptor* extension = descriptor_->extension(i);
if (!strip_custom_options || !ExtensionIsCustomOption(extension)) {
extension_generators->push_back(
std::make_unique<ExtensionGenerator>(class_name_, extension));
if (!generation_options_.strip_custom_options ||
!ExtensionIsCustomOption(extension)) {
extension_generators->push_back(std::make_unique<ExtensionGenerator>(
class_name_, extension, generation_options_));
extension_generators_.push_back(extension_generators->back().get());
}
}

@ -17,6 +17,7 @@
#include "absl/container/flat_hash_set.h"
#include "google/protobuf/compiler/objectivec/field.h"
#include "google/protobuf/compiler/objectivec/oneof.h"
#include "google/protobuf/compiler/objectivec/options.h"
#include "google/protobuf/descriptor.h"
#include "google/protobuf/io/printer.h"
@ -30,15 +31,15 @@ class ExtensionGenerator;
class MessageGenerator {
public:
MessageGenerator(const std::string& file_description_name,
const Descriptor* descriptor);
const Descriptor* descriptor,
const GenerationOptions& generation_options);
~MessageGenerator() = default;
MessageGenerator(const MessageGenerator&) = delete;
MessageGenerator& operator=(const MessageGenerator&) = delete;
void AddExtensionGenerators(
std::vector<std::unique_ptr<ExtensionGenerator>>* extension_generators,
bool strip_custom_options);
std::vector<std::unique_ptr<ExtensionGenerator>>* extension_generators);
void GenerateMessageHeader(io::Printer* printer) const;
void GenerateSource(io::Printer* printer) const;
@ -55,6 +56,7 @@ class MessageGenerator {
private:
const std::string file_description_name_;
const Descriptor* descriptor_;
const GenerationOptions& generation_options_;
FieldGeneratorMap field_generators_;
const std::string class_name_;
const std::string deprecated_attribute_;

@ -12,6 +12,7 @@
#include "absl/strings/str_cat.h"
#include "google/protobuf/compiler/objectivec/helpers.h"
#include "google/protobuf/compiler/objectivec/names.h"
#include "google/protobuf/compiler/objectivec/options.h"
#include "google/protobuf/descriptor.h"
#include "google/protobuf/io/printer.h"
@ -20,7 +21,8 @@ namespace protobuf {
namespace compiler {
namespace objectivec {
OneofGenerator::OneofGenerator(const OneofDescriptor* descriptor)
OneofGenerator::OneofGenerator(const OneofDescriptor* descriptor,
const GenerationOptions& generation_options)
: descriptor_(descriptor) {
variables_["enum_name"] = OneofEnumName(descriptor_);
variables_["name"] = OneofName(descriptor_);

@ -12,6 +12,7 @@
#include "absl/container/flat_hash_map.h"
#include "absl/strings/string_view.h"
#include "google/protobuf/compiler/objectivec/options.h"
#include "google/protobuf/descriptor.h"
#include "google/protobuf/io/printer.h"
@ -22,7 +23,8 @@ namespace objectivec {
class OneofGenerator {
public:
explicit OneofGenerator(const OneofDescriptor* descriptor);
OneofGenerator(const OneofDescriptor* descriptor,
const GenerationOptions& generation_options);
~OneofGenerator() = default;
OneofGenerator(const OneofGenerator&) = delete;

Loading…
Cancel
Save