Remove the non prefix validation options from Options.

The other generation options never were in the Options structure and
ImportWriter was the only thing that needed some the non validation options and
it already required directly passing the options. So refine Options to just be
validation options and bundle all the File related generation options into
a new FileGenerator specific GenerationOptions.
pull/9565/head
Thomas Van Lenten 3 years ago
parent 14e445bc34
commit d9048081b2
  1. 22
      src/google/protobuf/compiler/objectivec/objectivec_file.cc
  2. 14
      src/google/protobuf/compiler/objectivec/objectivec_file.h
  3. 13
      src/google/protobuf/compiler/objectivec/objectivec_generator.cc
  4. 8
      src/google/protobuf/compiler/objectivec/objectivec_helpers.cc
  5. 6
      src/google/protobuf/compiler/objectivec/objectivec_helpers.h

@ -31,6 +31,7 @@
#include <google/protobuf/compiler/objectivec/objectivec_file.h> #include <google/protobuf/compiler/objectivec/objectivec_file.h>
#include <google/protobuf/compiler/objectivec/objectivec_enum.h> #include <google/protobuf/compiler/objectivec/objectivec_enum.h>
#include <google/protobuf/compiler/objectivec/objectivec_extension.h> #include <google/protobuf/compiler/objectivec/objectivec_extension.h>
#include <google/protobuf/compiler/objectivec/objectivec_helpers.h>
#include <google/protobuf/compiler/objectivec/objectivec_message.h> #include <google/protobuf/compiler/objectivec/objectivec_message.h>
#include <google/protobuf/compiler/code_generator.h> #include <google/protobuf/compiler/code_generator.h>
#include <google/protobuf/io/printer.h> #include <google/protobuf/io/printer.h>
@ -185,11 +186,12 @@ bool IsDirectDependency(const FileDescriptor* dep, const FileDescriptor* file) {
} // namespace } // namespace
FileGenerator::FileGenerator(const FileDescriptor* file, const Options& options) FileGenerator::FileGenerator(const FileDescriptor* file,
const GenerationOptions& generation_options)
: file_(file), : file_(file),
generation_options_(generation_options),
root_class_name_(FileClassName(file)), root_class_name_(FileClassName(file)),
is_bundled_proto_(IsProtobufLibraryBundledProtoFile(file)), is_bundled_proto_(IsProtobufLibraryBundledProtoFile(file)) {
options_(options) {
for (int i = 0; i < file_->enum_type_count(); i++) { for (int i = 0; i < file_->enum_type_count(); i++) {
EnumGenerator* generator = new EnumGenerator(file_->enum_type(i)); EnumGenerator* generator = new EnumGenerator(file_->enum_type(i));
enum_generators_.emplace_back(generator); enum_generators_.emplace_back(generator);
@ -240,9 +242,9 @@ void FileGenerator::GenerateHeader(io::Printer* printer) {
// #import any headers for "public imports" in the proto file. // #import any headers for "public imports" in the proto file.
{ {
ImportWriter import_writer( ImportWriter import_writer(
options_.generate_for_named_framework, generation_options_.generate_for_named_framework,
options_.named_framework_to_proto_path_mappings_path, generation_options_.named_framework_to_proto_path_mappings_path,
options_.runtime_import_prefix, generation_options_.runtime_import_prefix,
is_bundled_proto_); is_bundled_proto_);
const std::string header_extension(kHeaderExtension); const std::string header_extension(kHeaderExtension);
for (int i = 0; i < file_->public_dependency_count(); i++) { for (int i = 0; i < file_->public_dependency_count(); i++) {
@ -354,9 +356,9 @@ void FileGenerator::GenerateSource(io::Printer* printer) {
{ {
ImportWriter import_writer( ImportWriter import_writer(
options_.generate_for_named_framework, generation_options_.generate_for_named_framework,
options_.named_framework_to_proto_path_mappings_path, generation_options_.named_framework_to_proto_path_mappings_path,
options_.runtime_import_prefix, generation_options_.runtime_import_prefix,
is_bundled_proto_); is_bundled_proto_);
const std::string header_extension(kHeaderExtension); const std::string header_extension(kHeaderExtension);
@ -600,7 +602,7 @@ void FileGenerator::PrintFileRuntimePreamble(
"\n", "\n",
"filename", file_->name()); "filename", file_->name());
ImportWriter::PrintRuntimeImports( ImportWriter::PrintRuntimeImports(
printer, headers_to_import, options_.runtime_import_prefix, true); printer, headers_to_import, generation_options_.runtime_import_prefix, true);
printer->Print("\n"); printer->Print("\n");
} }

@ -34,7 +34,6 @@
#include <string> #include <string>
#include <set> #include <set>
#include <vector> #include <vector>
#include <google/protobuf/compiler/objectivec/objectivec_helpers.h>
#include <google/protobuf/descriptor.h> #include <google/protobuf/descriptor.h>
#include <google/protobuf/io/printer.h> #include <google/protobuf/io/printer.h>
@ -49,7 +48,15 @@ class MessageGenerator;
class FileGenerator { class FileGenerator {
public: public:
FileGenerator(const FileDescriptor* file, const Options& options); struct GenerationOptions {
GenerationOptions() {}
std::string generate_for_named_framework;
std::string named_framework_to_proto_path_mappings_path;
std::string runtime_import_prefix;
};
FileGenerator(const FileDescriptor* file,
const GenerationOptions& generation_options);
~FileGenerator(); ~FileGenerator();
FileGenerator(const FileGenerator&) = delete; FileGenerator(const FileGenerator&) = delete;
@ -60,6 +67,7 @@ class FileGenerator {
private: private:
const FileDescriptor* file_; const FileDescriptor* file_;
const GenerationOptions& generation_options_;
std::string root_class_name_; std::string root_class_name_;
bool is_bundled_proto_; bool is_bundled_proto_;
@ -67,8 +75,6 @@ class FileGenerator {
std::vector<std::unique_ptr<MessageGenerator>> message_generators_; std::vector<std::unique_ptr<MessageGenerator>> message_generators_;
std::vector<std::unique_ptr<ExtensionGenerator>> extension_generators_; std::vector<std::unique_ptr<ExtensionGenerator>> extension_generators_;
const Options options_;
void PrintFileRuntimePreamble( void PrintFileRuntimePreamble(
io::Printer* printer, io::Printer* printer,
const std::vector<std::string>& headers_to_import) const; const std::vector<std::string>& headers_to_import) const;

@ -94,7 +94,8 @@ bool ObjectiveCGenerator::GenerateAll(
// //
// e.g. protoc ... --objc_opt=expected_prefixes=file.txt,generate_for_named_framework=MyFramework // e.g. protoc ... --objc_opt=expected_prefixes=file.txt,generate_for_named_framework=MyFramework
Options generation_options; Options validation_options;
FileGenerator::GenerationOptions generation_options;
std::vector<std::pair<std::string, std::string> > options; std::vector<std::pair<std::string, std::string> > options;
ParseGeneratorParameter(parameter, &options); ParseGeneratorParameter(parameter, &options);
@ -116,14 +117,14 @@ bool ObjectiveCGenerator::GenerateAll(
// //
// There is no validation that the prefixes are good prefixes, it is // There is no validation that the prefixes are good prefixes, it is
// assumed that they are when you create the file. // assumed that they are when you create the file.
generation_options.expected_prefixes_path = options[i].second; validation_options.expected_prefixes_path = options[i].second;
} else if (options[i].first == "expected_prefixes_suppressions") { } else if (options[i].first == "expected_prefixes_suppressions") {
// A semicolon delimited string that lists the paths of .proto files to // A semicolon delimited string that lists the paths of .proto files to
// exclude from the package prefix validations (expected_prefixes_path). // exclude from the package prefix validations (expected_prefixes_path).
// This is provided as an "out", to skip some files being checked. // This is provided as an "out", to skip some files being checked.
for (StringPiece split_piece : Split( for (StringPiece split_piece : Split(
options[i].second, ";", true)) { options[i].second, ";", true)) {
generation_options.expected_prefixes_suppressions.push_back( validation_options.expected_prefixes_suppressions.push_back(
std::string(split_piece)); std::string(split_piece));
} }
} else if (options[i].first == "prefixes_must_be_registered") { } else if (options[i].first == "prefixes_must_be_registered") {
@ -135,7 +136,7 @@ bool ObjectiveCGenerator::GenerateAll(
// tried to use a prefix that isn't registered. // tried to use a prefix that isn't registered.
// Default is "no". // Default is "no".
if (!StringToBool(options[i].second, if (!StringToBool(options[i].second,
&generation_options.prefixes_must_be_registered)) { &validation_options.prefixes_must_be_registered)) {
*error = "error: Unknown value for prefixes_must_be_registered: " + options[i].second; *error = "error: Unknown value for prefixes_must_be_registered: " + options[i].second;
return false; return false;
} }
@ -147,7 +148,7 @@ bool ObjectiveCGenerator::GenerateAll(
// raised if a files doesn't have one. // raised if a files doesn't have one.
// Default is "no". // Default is "no".
if (!StringToBool(options[i].second, if (!StringToBool(options[i].second,
&generation_options.require_prefixes)) { &validation_options.require_prefixes)) {
*error = "error: Unknown value for require_prefixes: " + options[i].second; *error = "error: Unknown value for require_prefixes: " + options[i].second;
return false; return false;
} }
@ -258,7 +259,7 @@ bool ObjectiveCGenerator::GenerateAll(
// ----------------------------------------------------------------- // -----------------------------------------------------------------
// Validate the objc prefix/package pairings. // Validate the objc prefix/package pairings.
if (!ValidateObjCClassPrefixes(files, generation_options, error)) { if (!ValidateObjCClassPrefixes(files, validation_options, error)) {
// *error will have been filled in. // *error will have been filled in.
return false; return false;
} }

@ -1298,16 +1298,16 @@ bool PackageToPrefixesCollector::ConsumeLine(
return true; return true;
} }
bool LoadExpectedPackagePrefixes(const Options& generation_options, bool LoadExpectedPackagePrefixes(const std::string& expected_prefixes_path,
std::map<std::string, std::string>* prefix_map, std::map<std::string, std::string>* prefix_map,
std::string* out_error) { std::string* out_error) {
if (generation_options.expected_prefixes_path.empty()) { if (expected_prefixes_path.empty()) {
return true; return true;
} }
PackageToPrefixesCollector collector("Expected prefixes", prefix_map); PackageToPrefixesCollector collector("Expected prefixes", prefix_map);
return ParseSimpleFile( return ParseSimpleFile(
generation_options.expected_prefixes_path, &collector, out_error); expected_prefixes_path, &collector, out_error);
} }
bool ValidateObjCClassPrefix( bool ValidateObjCClassPrefix(
@ -1465,7 +1465,7 @@ bool ValidateObjCClassPrefixes(const std::vector<const FileDescriptor*>& files,
// Load the expected package prefixes, if available, to validate against. // Load the expected package prefixes, if available, to validate against.
std::map<std::string, std::string> expected_package_prefixes; std::map<std::string, std::string> expected_package_prefixes;
if (!LoadExpectedPackagePrefixes(generation_options, if (!LoadExpectedPackagePrefixes(generation_options.expected_prefixes_path,
&expected_package_prefixes, &expected_package_prefixes,
out_error)) { out_error)) {
return false; return false;

@ -64,14 +64,12 @@ std::string PROTOC_EXPORT GetProtoPackagePrefixExceptionList();
void PROTOC_EXPORT SetProtoPackagePrefixExceptionList( void PROTOC_EXPORT SetProtoPackagePrefixExceptionList(
const std::string& file_path); const std::string& file_path);
// Generator options (see objectivec_generator.cc for a description of each): // Generator Prefix Validation Options (see objectivec_generator.cc for a
// description of each):
struct Options { struct Options {
Options(); Options();
std::string expected_prefixes_path; std::string expected_prefixes_path;
std::vector<std::string> expected_prefixes_suppressions; std::vector<std::string> expected_prefixes_suppressions;
std::string generate_for_named_framework;
std::string named_framework_to_proto_path_mappings_path;
std::string runtime_import_prefix;
bool prefixes_must_be_registered; bool prefixes_must_be_registered;
bool require_prefixes; bool require_prefixes;
}; };

Loading…
Cancel
Save