plugin.cc
Go to the documentation of this file.
1 // Protocol Buffers - Google's data interchange format
2 // Copyright 2008 Google Inc. All rights reserved.
3 // https://developers.google.com/protocol-buffers/
4 //
5 // Redistribution and use in source and binary forms, with or without
6 // modification, are permitted provided that the following conditions are
7 // met:
8 //
9 // * Redistributions of source code must retain the above copyright
10 // notice, this list of conditions and the following disclaimer.
11 // * Redistributions in binary form must reproduce the above
12 // copyright notice, this list of conditions and the following disclaimer
13 // in the documentation and/or other materials provided with the
14 // distribution.
15 // * Neither the name of Google Inc. nor the names of its
16 // contributors may be used to endorse or promote products derived from
17 // this software without specific prior written permission.
18 //
19 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 
31 // Author: kenton@google.com (Kenton Varda)
32 
34 
35 #include <iostream>
36 #include <set>
37 
38 #ifdef _WIN32
39 #include <fcntl.h>
40 #else
41 #include <unistd.h>
42 #endif
43 
51 
52 
53 namespace google {
54 namespace protobuf {
55 namespace compiler {
56 
57 #if defined(_WIN32)
58 // DO NOT include <io.h>, instead create functions in io_win32.{h,cc} and import
59 // them like we do below.
60 using google::protobuf::io::win32::setmode;
61 #endif
62 
64  public:
66  const Version& compiler_version, CodeGeneratorResponse* response,
67  const std::vector<const FileDescriptor*>& parsed_files)
68  : compiler_version_(compiler_version),
70  parsed_files_(parsed_files) {}
72 
73  // implements GeneratorContext --------------------------------------
74 
75  virtual io::ZeroCopyOutputStream* Open(const std::string& filename) {
76  CodeGeneratorResponse::File* file = response_->add_file();
77  file->set_name(filename);
78  return new io::StringOutputStream(file->mutable_content());
79  }
80 
82  const std::string& filename, const std::string& insertion_point) {
83  CodeGeneratorResponse::File* file = response_->add_file();
84  file->set_name(filename);
85  file->set_insertion_point(insertion_point);
86  return new io::StringOutputStream(file->mutable_content());
87  }
88 
89  void ListParsedFiles(std::vector<const FileDescriptor*>* output) {
91  }
92 
95  }
96 
97  private:
99  CodeGeneratorResponse* response_;
100  const std::vector<const FileDescriptor*>& parsed_files_;
101 };
102 
103 bool GenerateCode(const CodeGeneratorRequest& request,
104  const CodeGenerator& generator,
105  CodeGeneratorResponse* response, std::string* error_msg) {
107  for (int i = 0; i < request.proto_file_size(); i++) {
108  const FileDescriptor* file = pool.BuildFile(request.proto_file(i));
109  if (file == NULL) {
110  // BuildFile() already wrote an error message.
111  return false;
112  }
113  }
114 
115  std::vector<const FileDescriptor*> parsed_files;
116  for (int i = 0; i < request.file_to_generate_size(); i++) {
117  parsed_files.push_back(pool.FindFileByName(request.file_to_generate(i)));
118  if (parsed_files.back() == NULL) {
119  *error_msg =
120  "protoc asked plugin to generate a file but "
121  "did not provide a descriptor for the file: " +
122  request.file_to_generate(i);
123  return false;
124  }
125  }
126 
127  GeneratorResponseContext context(request.compiler_version(), response,
128  parsed_files);
129 
130 
132  bool succeeded = generator.GenerateAll(parsed_files, request.parameter(),
133  &context, &error);
134 
135  if (!succeeded && error.empty()) {
136  error =
137  "Code generator returned false but provided no error "
138  "description.";
139  }
140  if (!error.empty()) {
141  response->set_error(error);
142  }
143 
144  return true;
145 }
146 
147 int PluginMain(int argc, char* argv[], const CodeGenerator* generator) {
148 
149  if (argc > 1) {
150  std::cerr << argv[0] << ": Unknown option: " << argv[1] << std::endl;
151  return 1;
152  }
153 
154 #ifdef _WIN32
155  setmode(STDIN_FILENO, _O_BINARY);
156  setmode(STDOUT_FILENO, _O_BINARY);
157 #endif
158 
159  CodeGeneratorRequest request;
160  if (!request.ParseFromFileDescriptor(STDIN_FILENO)) {
161  std::cerr << argv[0] << ": protoc sent unparseable request to plugin."
162  << std::endl;
163  return 1;
164  }
165 
166  std::string error_msg;
167  CodeGeneratorResponse response;
168 
169  if (GenerateCode(request, *generator, &response, &error_msg)) {
170  if (!response.SerializeToFileDescriptor(STDOUT_FILENO)) {
171  std::cerr << argv[0] << ": Error writing to stdout." << std::endl;
172  return 1;
173  }
174  } else {
175  if (!error_msg.empty()) {
176  std::cerr << argv[0] << ": " << error_msg << std::endl;
177  }
178  return 1;
179  }
180 
181  return 0;
182 }
183 
184 } // namespace compiler
185 } // namespace protobuf
186 } // namespace google
response
const std::string response
google::protobuf::compiler::GeneratorResponseContext::ListParsedFiles
void ListParsedFiles(std::vector< const FileDescriptor * > *output)
Definition: plugin.cc:89
google::protobuf::compiler::GeneratorResponseContext::~GeneratorResponseContext
virtual ~GeneratorResponseContext()
Definition: plugin.cc:71
zero_copy_stream_impl.h
google::protobuf::compiler::CodeGenerator
Definition: code_generator.h:68
NULL
NULL
Definition: test_security_zap.cpp:405
string
GLsizei const GLchar *const * string
Definition: glcorearb.h:3083
plugin.h
google::protobuf::compiler::PluginMain
int PluginMain(int argc, char *argv[], const CodeGenerator *generator)
Definition: plugin.cc:147
error
Definition: cJSON.c:88
google::protobuf::DescriptorPool
Definition: src/google/protobuf/descriptor.h:1539
google::protobuf::compiler::GenerateCode
bool GenerateCode(const CodeGeneratorRequest &request, const CodeGenerator &generator, CodeGeneratorResponse *response, std::string *error_msg)
Definition: plugin.cc:103
google::protobuf::compiler::GeneratorResponseContext::Open
virtual io::ZeroCopyOutputStream * Open(const std::string &filename)
Definition: plugin.cc:75
code_generator.h
google::protobuf::compiler::GeneratorResponseContext::OpenForInsert
virtual io::ZeroCopyOutputStream * OpenForInsert(const std::string &filename, const std::string &insertion_point)
Definition: plugin.cc:81
google::protobuf::compiler::GeneratorResponseContext::GeneratorResponseContext
GeneratorResponseContext(const Version &compiler_version, CodeGeneratorResponse *response, const std::vector< const FileDescriptor * > &parsed_files)
Definition: plugin.cc:65
pool
InternalDescriptorPool * pool
Definition: php/ext/google/protobuf/protobuf.h:798
i
int i
Definition: gmock-matchers_test.cc:764
google::protobuf::io::StringOutputStream
Definition: zero_copy_stream_impl_lite.h:131
google::protobuf.internal.api_implementation.Version
def Version()
Definition: api_implementation.py:147
common.h
google::protobuf::compiler::GeneratorResponseContext::response_
CodeGeneratorResponse * response_
Definition: plugin.cc:99
google::protobuf::io::ZeroCopyOutputStream
Definition: zero_copy_stream.h:183
logging.h
descriptor.h
io_win32.h
google::protobuf::FileDescriptor
Definition: src/google/protobuf/descriptor.h:1320
google::protobuf::compiler::GeneratorResponseContext::parsed_files_
const std::vector< const FileDescriptor * > & parsed_files_
Definition: plugin.cc:100
output
const upb_json_parsermethod const upb_symtab upb_sink * output
Definition: ruby/ext/google/protobuf_c/upb.h:10503
google::protobuf::compiler::CodeGenerator::GenerateAll
virtual bool GenerateAll(const std::vector< const FileDescriptor * > &files, const std::string &parameter, GeneratorContext *generator_context, std::string *error) const
Definition: code_generator.cc:49
version
static struct @0 version
google::protobuf::compiler::GeneratorContext
Definition: code_generator.h:119
google::protobuf::compiler::GeneratorResponseContext
Definition: plugin.cc:63
google::protobuf::compiler::GeneratorResponseContext::GetCompilerVersion
void GetCompilerVersion(Version *version) const
Definition: plugin.cc:93
google::protobuf::compiler::GeneratorResponseContext::compiler_version_
Version compiler_version_
Definition: plugin.cc:98
compiler
Definition: plugin.pb.cc:22
google
Definition: data_proto2_to_proto3_util.h:11
plugin.pb.h


libaditof
Author(s):
autogenerated on Wed May 21 2025 02:06:57