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(CodeGeneratorResponse* response, | 65 GeneratorResponseContext( |
66 const vector<const FileDescriptor*>& parsed_files) | 66 const Version& compiler_version, |
67 : response_(response), | 67 CodeGeneratorResponse* response, |
| 68 const std::vector<const FileDescriptor*>& parsed_files) |
| 69 : compiler_version_(compiler_version), |
| 70 response_(response), |
68 parsed_files_(parsed_files) {} | 71 parsed_files_(parsed_files) {} |
69 virtual ~GeneratorResponseContext() {} | 72 virtual ~GeneratorResponseContext() {} |
70 | 73 |
71 // implements GeneratorContext -------------------------------------- | 74 // implements GeneratorContext -------------------------------------- |
72 | 75 |
73 virtual io::ZeroCopyOutputStream* Open(const string& filename) { | 76 virtual io::ZeroCopyOutputStream* Open(const string& filename) { |
74 CodeGeneratorResponse::File* file = response_->add_file(); | 77 CodeGeneratorResponse::File* file = response_->add_file(); |
75 file->set_name(filename); | 78 file->set_name(filename); |
76 return new io::StringOutputStream(file->mutable_content()); | 79 return new io::StringOutputStream(file->mutable_content()); |
77 } | 80 } |
78 | 81 |
79 virtual io::ZeroCopyOutputStream* OpenForInsert( | 82 virtual io::ZeroCopyOutputStream* OpenForInsert( |
80 const string& filename, const string& insertion_point) { | 83 const string& filename, const string& insertion_point) { |
81 CodeGeneratorResponse::File* file = response_->add_file(); | 84 CodeGeneratorResponse::File* file = response_->add_file(); |
82 file->set_name(filename); | 85 file->set_name(filename); |
83 file->set_insertion_point(insertion_point); | 86 file->set_insertion_point(insertion_point); |
84 return new io::StringOutputStream(file->mutable_content()); | 87 return new io::StringOutputStream(file->mutable_content()); |
85 } | 88 } |
86 | 89 |
87 void ListParsedFiles(vector<const FileDescriptor*>* output) { | 90 void ListParsedFiles(std::vector<const FileDescriptor*>* output) { |
88 *output = parsed_files_; | 91 *output = parsed_files_; |
89 } | 92 } |
90 | 93 |
| 94 void GetCompilerVersion(Version* version) const { |
| 95 *version = compiler_version_; |
| 96 } |
| 97 |
91 private: | 98 private: |
| 99 Version compiler_version_; |
92 CodeGeneratorResponse* response_; | 100 CodeGeneratorResponse* response_; |
93 const vector<const FileDescriptor*>& parsed_files_; | 101 const std::vector<const FileDescriptor*>& parsed_files_; |
94 }; | 102 }; |
95 | 103 |
96 bool GenerateCode(const CodeGeneratorRequest& request, | 104 bool GenerateCode(const CodeGeneratorRequest& request, |
97 const CodeGenerator& generator, CodeGeneratorResponse* response, | 105 const CodeGenerator& generator, CodeGeneratorResponse* response, |
98 string* error_msg) { | 106 string* error_msg) { |
99 DescriptorPool pool; | 107 DescriptorPool pool; |
100 for (int i = 0; i < request.proto_file_size(); i++) { | 108 for (int i = 0; i < request.proto_file_size(); i++) { |
101 const FileDescriptor* file = pool.BuildFile(request.proto_file(i)); | 109 const FileDescriptor* file = pool.BuildFile(request.proto_file(i)); |
102 if (file == NULL) { | 110 if (file == NULL) { |
103 // BuildFile() already wrote an error message. | 111 // BuildFile() already wrote an error message. |
104 return false; | 112 return false; |
105 } | 113 } |
106 } | 114 } |
107 | 115 |
108 vector<const FileDescriptor*> parsed_files; | 116 std::vector<const FileDescriptor*> parsed_files; |
109 for (int i = 0; i < request.file_to_generate_size(); i++) { | 117 for (int i = 0; i < request.file_to_generate_size(); i++) { |
110 parsed_files.push_back(pool.FindFileByName(request.file_to_generate(i))); | 118 parsed_files.push_back(pool.FindFileByName(request.file_to_generate(i))); |
111 if (parsed_files.back() == NULL) { | 119 if (parsed_files.back() == NULL) { |
112 *error_msg = "protoc asked plugin to generate a file but " | 120 *error_msg = "protoc asked plugin to generate a file but " |
113 "did not provide a descriptor for the file: " + | 121 "did not provide a descriptor for the file: " + |
114 request.file_to_generate(i); | 122 request.file_to_generate(i); |
115 return false; | 123 return false; |
116 } | 124 } |
117 } | 125 } |
118 | 126 |
119 GeneratorResponseContext context(response, parsed_files); | 127 GeneratorResponseContext context( |
| 128 request.compiler_version(), response, parsed_files); |
120 | 129 |
121 if (generator.HasGenerateAll()) { | 130 string error; |
122 string error; | 131 bool succeeded = generator.GenerateAll( |
123 bool succeeded = generator.GenerateAll( | 132 parsed_files, request.parameter(), &context, &error); |
124 parsed_files, request.parameter(), &context, &error); | |
125 | 133 |
126 if (!succeeded && error.empty()) { | 134 if (!succeeded && error.empty()) { |
127 error = "Code generator returned false but provided no error " | 135 error = "Code generator returned false but provided no error " |
128 "description."; | 136 "description."; |
129 } | 137 } |
130 if (!error.empty()) { | 138 if (!error.empty()) { |
131 response->set_error(error); | 139 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 } | |
150 } | 140 } |
151 | 141 |
152 return true; | 142 return true; |
153 } | 143 } |
154 | 144 |
155 int PluginMain(int argc, char* argv[], const CodeGenerator* generator) { | 145 int PluginMain(int argc, char* argv[], const CodeGenerator* generator) { |
156 | 146 |
157 if (argc > 1) { | 147 if (argc > 1) { |
158 std::cerr << argv[0] << ": Unknown option: " << argv[1] << std::endl; | 148 std::cerr << argv[0] << ": Unknown option: " << argv[1] << std::endl; |
159 return 1; | 149 return 1; |
(...skipping 25 matching lines...) Expand all Loading... |
185 } | 175 } |
186 return 1; | 176 return 1; |
187 } | 177 } |
188 | 178 |
189 return 0; | 179 return 0; |
190 } | 180 } |
191 | 181 |
192 } // namespace compiler | 182 } // namespace compiler |
193 } // namespace protobuf | 183 } // namespace protobuf |
194 } // namespace google | 184 } // namespace google |
OLD | NEW |