Ver código fonte

Remove the compiler options from ImportWriter.

Breaks the tie of the ImportWriter to the ObjC generation, allow grpc to use
a different extension and to relay the values they need for these.

- Pass in the two framework options to the ctor.
- Pass in the header extension to AddFile.
Thomas Van Lenten 9 anos atrás
pai
commit
290d26b462

+ 13 - 5
src/google/protobuf/compiler/objectivec/objectivec_file.cc

@@ -51,6 +51,8 @@ namespace protobuf {
 // runtime being used.
 const int32 GOOGLE_PROTOBUF_OBJC_GEN_VERSION = 30001;
 
+const char* kHeaderExtension = ".pbobjc.h";
+
 namespace compiler {
 namespace objectivec {
 
@@ -100,13 +102,16 @@ void FileGenerator::GenerateHeader(io::Printer *printer) {
 
   // #import any headers for "public imports" in the proto file.
   {
-    ImportWriter import_writer(options_);
+    ImportWriter import_writer(
+        options_.generate_for_named_framework,
+        options_.named_framework_to_proto_path_mappings_path);
     const vector<FileGenerator *> &dependency_generators = DependencyGenerators();
+    const string header_extension(kHeaderExtension);
     for (vector<FileGenerator *>::const_iterator iter =
              dependency_generators.begin();
          iter != dependency_generators.end(); ++iter) {
       if ((*iter)->IsPublicDependency()) {
-        import_writer.AddFile((*iter)->file_);
+        import_writer.AddFile((*iter)->file_, header_extension);
       }
     }
     import_writer.Print(printer);
@@ -208,10 +213,13 @@ void FileGenerator::GenerateSource(io::Printer *printer) {
   PrintFileRuntimePreamble(printer, "GPBProtocolBuffers_RuntimeSupport.h");
 
   {
-    ImportWriter import_writer(options_);
+    ImportWriter import_writer(
+        options_.generate_for_named_framework,
+        options_.named_framework_to_proto_path_mappings_path);
+    const string header_extension(kHeaderExtension);
 
     // #import the header for this proto file.
-    import_writer.AddFile(file_);
+    import_writer.AddFile(file_, header_extension);
 
     // #import the headers for anything that a plain dependency of this proto
     // file (that means they were just an include, not a "public" include).
@@ -221,7 +229,7 @@ void FileGenerator::GenerateSource(io::Printer *printer) {
              dependency_generators.begin();
          iter != dependency_generators.end(); ++iter) {
       if (!(*iter)->IsPublicDependency()) {
-        import_writer.AddFile((*iter)->file_);
+        import_writer.AddFile((*iter)->file_, header_extension);
       }
     }
 

+ 23 - 12
src/google/protobuf/compiler/objectivec/objectivec_helpers.cc

@@ -1434,14 +1434,25 @@ bool ParseSimpleFile(
   return parser.Finish();
 }
 
-void ImportWriter::AddFile(const FileDescriptor* file) {
-  const string extension(".pbobjc.h");
+ImportWriter::ImportWriter(
+  const string& generate_for_named_framework,
+  const string& named_framework_to_proto_path_mappings_path)
+    : generate_for_named_framework_(generate_for_named_framework),
+      named_framework_to_proto_path_mappings_path_(
+          named_framework_to_proto_path_mappings_path),
+      need_to_parse_mapping_file_(true) {
+}
+
+ImportWriter::~ImportWriter() {}
+
+void ImportWriter::AddFile(const FileDescriptor* file,
+                           const string& header_extension) {
   const string file_path(FilePath(file));
 
   if (IsProtobufLibraryBundledProtoFile(file)) {
     protobuf_framework_imports_.push_back(
-        FilePathBasename(file) + extension);
-    protobuf_non_framework_imports_.push_back(file_path + extension);
+        FilePathBasename(file) + header_extension);
+    protobuf_non_framework_imports_.push_back(file_path + header_extension);
     return;
   }
 
@@ -1455,18 +1466,18 @@ void ImportWriter::AddFile(const FileDescriptor* file) {
   if (proto_lookup != proto_file_to_framework_name_.end()) {
     other_framework_imports_.push_back(
         proto_lookup->second + "/" +
-        FilePathBasename(file) + extension);
+        FilePathBasename(file) + header_extension);
     return;
   }
 
-  if (!options_.generate_for_named_framework.empty()) {
+  if (!generate_for_named_framework_.empty()) {
     other_framework_imports_.push_back(
-        options_.generate_for_named_framework + "/" +
-        FilePathBasename(file) + extension);
+        generate_for_named_framework_ + "/" +
+        FilePathBasename(file) + header_extension);
     return;
   }
 
-  other_imports_.push_back(file_path + extension);
+  other_imports_.push_back(file_path + header_extension);
 }
 
 void ImportWriter::Print(io::Printer* printer) const {
@@ -1534,15 +1545,15 @@ void ImportWriter::Print(io::Printer* printer) const {
 
 void ImportWriter::ParseFrameworkMappings() {
   need_to_parse_mapping_file_ = false;
-  if (options_.named_framework_to_proto_path_mappings_path.empty()) {
+  if (named_framework_to_proto_path_mappings_path_.empty()) {
     return;  // Nothing to do.
   }
 
   ProtoFrameworkCollector collector(&proto_file_to_framework_name_);
   string parse_error;
-  if (!ParseSimpleFile(options_.named_framework_to_proto_path_mappings_path,
+  if (!ParseSimpleFile(named_framework_to_proto_path_mappings_path_,
                        &collector, &parse_error)) {
-    cerr << "error parsing " << options_.named_framework_to_proto_path_mappings_path
+    cerr << "error parsing " << named_framework_to_proto_path_mappings_path_
          << " : " << parse_error << endl;
     cerr.flush();
   }

+ 6 - 5
src/google/protobuf/compiler/objectivec/objectivec_helpers.h

@@ -230,11 +230,11 @@ bool ParseSimpleFile(
 // import statements.
 class LIBPROTOC_EXPORT ImportWriter {
  public:
-  ImportWriter(const Options& options)
-      : options_(options),
-        need_to_parse_mapping_file_(true) {}
+  ImportWriter(const string& generate_for_named_framework,
+               const string& named_framework_to_proto_path_mappings_path);
+  ~ImportWriter();
 
-  void AddFile(const FileDescriptor* file);
+  void AddFile(const FileDescriptor* file, const string& header_extension);
   void Print(io::Printer *printer) const;
 
  private:
@@ -251,7 +251,8 @@ class LIBPROTOC_EXPORT ImportWriter {
 
   void ParseFrameworkMappings();
 
-  const Options options_;
+  const string generate_for_named_framework_;
+  const string named_framework_to_proto_path_mappings_path_;
   map<string, string> proto_file_to_framework_name_;
   bool need_to_parse_mapping_file_;