OLD | NEW |
1 // Protocol Buffers - Google's data interchange format | 1 // Protocol Buffers - Google's data interchange format |
2 // Copyright 2008 Google Inc. All rights reserved. | 2 // Copyright 2008 Google Inc. All rights reserved. |
3 // https://developers.google.com/protocol-buffers/ | 3 // https://developers.google.com/protocol-buffers/ |
4 // | 4 // |
5 // Redistribution and use in source and binary forms, with or without | 5 // Redistribution and use in source and binary forms, with or without |
6 // modification, are permitted provided that the following conditions are | 6 // modification, are permitted provided that the following conditions are |
7 // met: | 7 // met: |
8 // | 8 // |
9 // * Redistributions of source code must retain the above copyright | 9 // * Redistributions of source code must retain the above copyright |
10 // notice, this list of conditions and the following disclaimer. | 10 // notice, this list of conditions and the following disclaimer. |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
55 #include <google/protobuf/descriptor.h> | 55 #include <google/protobuf/descriptor.h> |
56 #include <google/protobuf/io/zero_copy_stream_impl.h> | 56 #include <google/protobuf/io/zero_copy_stream_impl.h> |
57 | 57 |
58 | 58 |
59 namespace google { | 59 namespace google { |
60 namespace protobuf { | 60 namespace protobuf { |
61 namespace compiler { | 61 namespace compiler { |
62 | 62 |
63 class GeneratorResponseContext : public GeneratorContext { | 63 class GeneratorResponseContext : public GeneratorContext { |
64 public: | 64 public: |
65 GeneratorResponseContext( | 65 GeneratorResponseContext(CodeGeneratorResponse* response, |
66 const Version& compiler_version, | 66 const vector<const FileDescriptor*>& parsed_files) |
67 CodeGeneratorResponse* response, | 67 : response_(response), |
68 const std::vector<const FileDescriptor*>& parsed_files) | |
69 : compiler_version_(compiler_version), | |
70 response_(response), | |
71 parsed_files_(parsed_files) {} | 68 parsed_files_(parsed_files) {} |
72 virtual ~GeneratorResponseContext() {} | 69 virtual ~GeneratorResponseContext() {} |
73 | 70 |
74 // implements GeneratorContext -------------------------------------- | 71 // implements GeneratorContext -------------------------------------- |
75 | 72 |
76 virtual io::ZeroCopyOutputStream* Open(const string& filename) { | 73 virtual io::ZeroCopyOutputStream* Open(const string& filename) { |
77 CodeGeneratorResponse::File* file = response_->add_file(); | 74 CodeGeneratorResponse::File* file = response_->add_file(); |
78 file->set_name(filename); | 75 file->set_name(filename); |
79 return new io::StringOutputStream(file->mutable_content()); | 76 return new io::StringOutputStream(file->mutable_content()); |
80 } | 77 } |
81 | 78 |
82 virtual io::ZeroCopyOutputStream* OpenForInsert( | 79 virtual io::ZeroCopyOutputStream* OpenForInsert( |
83 const string& filename, const string& insertion_point) { | 80 const string& filename, const string& insertion_point) { |
84 CodeGeneratorResponse::File* file = response_->add_file(); | 81 CodeGeneratorResponse::File* file = response_->add_file(); |
85 file->set_name(filename); | 82 file->set_name(filename); |
86 file->set_insertion_point(insertion_point); | 83 file->set_insertion_point(insertion_point); |
87 return new io::StringOutputStream(file->mutable_content()); | 84 return new io::StringOutputStream(file->mutable_content()); |
88 } | 85 } |
89 | 86 |
90 void ListParsedFiles(std::vector<const FileDescriptor*>* output) { | 87 void ListParsedFiles(vector<const FileDescriptor*>* output) { |
91 *output = parsed_files_; | 88 *output = parsed_files_; |
92 } | 89 } |
93 | 90 |
94 void GetCompilerVersion(Version* version) const { | |
95 *version = compiler_version_; | |
96 } | |
97 | |
98 private: | 91 private: |
99 Version compiler_version_; | |
100 CodeGeneratorResponse* response_; | 92 CodeGeneratorResponse* response_; |
101 const std::vector<const FileDescriptor*>& parsed_files_; | 93 const vector<const FileDescriptor*>& parsed_files_; |
102 }; | 94 }; |
103 | 95 |
104 bool GenerateCode(const CodeGeneratorRequest& request, | 96 bool GenerateCode(const CodeGeneratorRequest& request, |
105 const CodeGenerator& generator, CodeGeneratorResponse* response, | 97 const CodeGenerator& generator, CodeGeneratorResponse* response, |
106 string* error_msg) { | 98 string* error_msg) { |
107 DescriptorPool pool; | 99 DescriptorPool pool; |
108 for (int i = 0; i < request.proto_file_size(); i++) { | 100 for (int i = 0; i < request.proto_file_size(); i++) { |
109 const FileDescriptor* file = pool.BuildFile(request.proto_file(i)); | 101 const FileDescriptor* file = pool.BuildFile(request.proto_file(i)); |
110 if (file == NULL) { | 102 if (file == NULL) { |
111 // BuildFile() already wrote an error message. | 103 // BuildFile() already wrote an error message. |
112 return false; | 104 return false; |
113 } | 105 } |
114 } | 106 } |
115 | 107 |
116 std::vector<const FileDescriptor*> parsed_files; | 108 vector<const FileDescriptor*> parsed_files; |
117 for (int i = 0; i < request.file_to_generate_size(); i++) { | 109 for (int i = 0; i < request.file_to_generate_size(); i++) { |
118 parsed_files.push_back(pool.FindFileByName(request.file_to_generate(i))); | 110 parsed_files.push_back(pool.FindFileByName(request.file_to_generate(i))); |
119 if (parsed_files.back() == NULL) { | 111 if (parsed_files.back() == NULL) { |
120 *error_msg = "protoc asked plugin to generate a file but " | 112 *error_msg = "protoc asked plugin to generate a file but " |
121 "did not provide a descriptor for the file: " + | 113 "did not provide a descriptor for the file: " + |
122 request.file_to_generate(i); | 114 request.file_to_generate(i); |
123 return false; | 115 return false; |
124 } | 116 } |
125 } | 117 } |
126 | 118 |
127 GeneratorResponseContext context( | 119 GeneratorResponseContext context(response, parsed_files); |
128 request.compiler_version(), response, parsed_files); | |
129 | 120 |
130 string error; | 121 if (generator.HasGenerateAll()) { |
131 bool succeeded = generator.GenerateAll( | 122 string error; |
132 parsed_files, request.parameter(), &context, &error); | 123 bool succeeded = generator.GenerateAll( |
| 124 parsed_files, request.parameter(), &context, &error); |
133 | 125 |
134 if (!succeeded && error.empty()) { | 126 if (!succeeded && error.empty()) { |
135 error = "Code generator returned false but provided no error " | 127 error = "Code generator returned false but provided no error " |
136 "description."; | 128 "description."; |
137 } | 129 } |
138 if (!error.empty()) { | 130 if (!error.empty()) { |
139 response->set_error(error); | 131 response->set_error(error); |
| 132 } |
| 133 } else { |
| 134 for (int i = 0; i < parsed_files.size(); i++) { |
| 135 const FileDescriptor* file = parsed_files[i]; |
| 136 |
| 137 string error; |
| 138 bool succeeded = generator.Generate( |
| 139 file, request.parameter(), &context, &error); |
| 140 |
| 141 if (!succeeded && error.empty()) { |
| 142 error = "Code generator returned false but provided no error " |
| 143 "description."; |
| 144 } |
| 145 if (!error.empty()) { |
| 146 response->set_error(file->name() + ": " + error); |
| 147 break; |
| 148 } |
| 149 } |
140 } | 150 } |
141 | 151 |
142 return true; | 152 return true; |
143 } | 153 } |
144 | 154 |
145 int PluginMain(int argc, char* argv[], const CodeGenerator* generator) { | 155 int PluginMain(int argc, char* argv[], const CodeGenerator* generator) { |
146 | 156 |
147 if (argc > 1) { | 157 if (argc > 1) { |
148 std::cerr << argv[0] << ": Unknown option: " << argv[1] << std::endl; | 158 std::cerr << argv[0] << ": Unknown option: " << argv[1] << std::endl; |
149 return 1; | 159 return 1; |
(...skipping 25 matching lines...) Expand all Loading... |
175 } | 185 } |
176 return 1; | 186 return 1; |
177 } | 187 } |
178 | 188 |
179 return 0; | 189 return 0; |
180 } | 190 } |
181 | 191 |
182 } // namespace compiler | 192 } // namespace compiler |
183 } // namespace protobuf | 193 } // namespace protobuf |
184 } // namespace google | 194 } // namespace google |
OLD | NEW |