protobuf/src/google/protobuf/compiler/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 
33 #include <google/protobuf/compiler/plugin.h>
34 
35 #include <iostream>
36 #include <set>
37 
38 #ifdef _WIN32
39 #include <fcntl.h>
40 #else
41 #include <unistd.h>
42 #endif
43 
44 #include <google/protobuf/stubs/logging.h>
45 #include <google/protobuf/stubs/common.h>
46 #include <google/protobuf/compiler/plugin.pb.h>
47 #include <google/protobuf/compiler/code_generator.h>
48 #include <google/protobuf/io/zero_copy_stream_impl.h>
49 #include <google/protobuf/descriptor.h>
50 #include <google/protobuf/io/io_win32.h>
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 
63 class GeneratorResponseContext : public GeneratorContext {
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 
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,
83  const std::string& insertion_point) override {
84  CodeGeneratorResponse::File* file = response_->add_file();
85  file->set_name(filename);
86  file->set_insertion_point(insertion_point);
87  return new io::StringOutputStream(file->mutable_content());
88  }
89 
91  const std::string& filename, const std::string& insertion_point,
92  const google::protobuf::GeneratedCodeInfo& info) override {
93  CodeGeneratorResponse::File* file = response_->add_file();
94  file->set_name(filename);
95  file->set_insertion_point(insertion_point);
96  *file->mutable_generated_code_info() = info;
97  return new io::StringOutputStream(file->mutable_content());
98  }
99 
100  void ListParsedFiles(std::vector<const FileDescriptor*>* output) override {
102  }
103 
104  void GetCompilerVersion(Version* version) const override {
106  }
107 
108  private:
110  CodeGeneratorResponse* response_;
111  const std::vector<const FileDescriptor*>& parsed_files_;
112 };
113 
114 bool GenerateCode(const CodeGeneratorRequest& request,
115  const CodeGenerator& generator,
116  CodeGeneratorResponse* response, std::string* error_msg) {
118  for (int i = 0; i < request.proto_file_size(); i++) {
119  const FileDescriptor* file = pool.BuildFile(request.proto_file(i));
120  if (file == NULL) {
121  // BuildFile() already wrote an error message.
122  return false;
123  }
124  }
125 
126  std::vector<const FileDescriptor*> parsed_files;
127  for (int i = 0; i < request.file_to_generate_size(); i++) {
128  parsed_files.push_back(pool.FindFileByName(request.file_to_generate(i)));
129  if (parsed_files.back() == NULL) {
130  *error_msg =
131  "protoc asked plugin to generate a file but "
132  "did not provide a descriptor for the file: " +
133  request.file_to_generate(i);
134  return false;
135  }
136  }
137 
138  GeneratorResponseContext context(request.compiler_version(), response,
139  parsed_files);
140 
141 
143  bool succeeded = generator.GenerateAll(parsed_files, request.parameter(),
144  &context, &error);
145 
146  response->set_supported_features(generator.GetSupportedFeatures());
147 
148  if (!succeeded && error.empty()) {
149  error =
150  "Code generator returned false but provided no error "
151  "description.";
152  }
153  if (!error.empty()) {
154  response->set_error(error);
155  }
156 
157  return true;
158 }
159 
160 int PluginMain(int argc, char* argv[], const CodeGenerator* generator) {
161 
162  if (argc > 1) {
163  std::cerr << argv[0] << ": Unknown option: " << argv[1] << std::endl;
164  return 1;
165  }
166 
167 #ifdef _WIN32
168  setmode(STDIN_FILENO, _O_BINARY);
169  setmode(STDOUT_FILENO, _O_BINARY);
170 #endif
171 
172  CodeGeneratorRequest request;
173  if (!request.ParseFromFileDescriptor(STDIN_FILENO)) {
174  std::cerr << argv[0] << ": protoc sent unparseable request to plugin."
175  << std::endl;
176  return 1;
177  }
178 
179 
180  std::string error_msg;
181  CodeGeneratorResponse response;
182 
183  if (GenerateCode(request, *generator, &response, &error_msg)) {
184  if (!response.SerializeToFileDescriptor(STDOUT_FILENO)) {
185  std::cerr << argv[0] << ": Error writing to stdout." << std::endl;
186  return 1;
187  }
188  } else {
189  if (!error_msg.empty()) {
190  std::cerr << argv[0] << ": " << error_msg << std::endl;
191  }
192  return 1;
193  }
194 
195  return 0;
196 }
197 
198 } // namespace compiler
199 } // namespace protobuf
200 } // namespace google
filename
const char * filename
Definition: bloaty/third_party/zlib/contrib/minizip/ioapi.h:135
google::protobuf::compiler::GeneratorResponseContext::OpenForInsertWithGeneratedCodeInfo
io::ZeroCopyOutputStream * OpenForInsertWithGeneratedCodeInfo(const std::string &filename, const std::string &insertion_point, const google::protobuf::GeneratedCodeInfo &info) override
Definition: protobuf/src/google/protobuf/compiler/plugin.cc:90
google::protobuf::compiler::GeneratorResponseContext::~GeneratorResponseContext
virtual ~GeneratorResponseContext()
Definition: protobuf/src/google/protobuf/compiler/plugin.cc:71
google::protobuf::compiler::GeneratorResponseContext::ListParsedFiles
void ListParsedFiles(std::vector< const FileDescriptor * > *output) override
Definition: protobuf/src/google/protobuf/compiler/plugin.cc:100
benchmark.request
request
Definition: benchmark.py:77
testing::internal::string
::std::string string
Definition: bloaty/third_party/protobuf/third_party/googletest/googletest/include/gtest/internal/gtest-port.h:881
error
grpc_error_handle error
Definition: retry_filter.cc:499
file
Definition: bloaty/third_party/zlib/examples/gzappend.c:170
google::protobuf
Definition: bloaty/third_party/protobuf/benchmarks/util/data_proto2_to_proto3_util.h:12
version
Definition: version.py:1
google::protobuf::compiler::GeneratorResponseContext::OpenForInsert
io::ZeroCopyOutputStream * OpenForInsert(const std::string &filename, const std::string &insertion_point) override
Definition: protobuf/src/google/protobuf/compiler/plugin.cc:81
grpc::Version
std::string Version()
Return gRPC library version.
Definition: version_cc.cc:28
google::protobuf::compiler::PluginMain
int PluginMain(int argc, char *argv[], const CodeGenerator *generator)
Definition: bloaty/third_party/protobuf/src/google/protobuf/compiler/plugin.cc:147
grpc::protobuf::io::StringOutputStream
GRPC_CUSTOM_STRINGOUTPUTSTREAM StringOutputStream
Definition: src/compiler/config.h:56
grpc::protobuf::compiler::GeneratorContext
GRPC_CUSTOM_GENERATORCONTEXT GeneratorContext
Definition: src/compiler/config.h:42
grpc::protobuf::compiler::CodeGenerator
GRPC_CUSTOM_CODEGENERATOR CodeGenerator
Definition: src/compiler/config.h:41
gmock_output_test.output
output
Definition: bloaty/third_party/googletest/googlemock/test/gmock_output_test.py:175
google::protobuf::DescriptorPool
Definition: bloaty/third_party/protobuf/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: bloaty/third_party/protobuf/src/google/protobuf/compiler/plugin.cc:103
google::protobuf::compiler::GeneratorResponseContext::GeneratorResponseContext
GeneratorResponseContext(const Version &compiler_version, CodeGeneratorResponse *response, const std::vector< const FileDescriptor * > &parsed_files)
Definition: protobuf/src/google/protobuf/compiler/plugin.cc:65
google::protobuf::compiler::GeneratorResponseContext::parsed_files_
const std::vector< const FileDescriptor * > & parsed_files_
Definition: bloaty/third_party/protobuf/src/google/protobuf/compiler/plugin.cc:100
google::protobuf::compiler::GeneratorResponseContext::Open
io::ZeroCopyOutputStream * Open(const std::string &filename) override
Definition: protobuf/src/google/protobuf/compiler/plugin.cc:75
google::protobuf::compiler::GeneratorResponseContext::response_
CodeGeneratorResponse * response_
Definition: bloaty/third_party/protobuf/src/google/protobuf/compiler/plugin.cc:99
asyncio_get_stats.response
response
Definition: asyncio_get_stats.py:28
google::protobuf::io::ZeroCopyOutputStream
Definition: bloaty/third_party/protobuf/src/google/protobuf/io/zero_copy_stream.h:183
pool
InternalDescriptorPool * pool
Definition: bloaty/third_party/protobuf/php/ext/google/protobuf/protobuf.h:807
google::protobuf::FileDescriptor
Definition: bloaty/third_party/protobuf/src/google/protobuf/descriptor.h:1320
context
grpc::ClientContext context
Definition: istio_echo_server_lib.cc:61
google::protobuf::compiler::GeneratorResponseContext::GetCompilerVersion
void GetCompilerVersion(Version *version) const override
Definition: protobuf/src/google/protobuf/compiler/plugin.cc:104
google::protobuf::compiler::GeneratorResponseContext::compiler_version_
Version compiler_version_
Definition: bloaty/third_party/protobuf/src/google/protobuf/compiler/plugin.cc:98
compiler
Definition: bloaty/third_party/protobuf/src/google/protobuf/compiler/plugin.pb.cc:21
google
Definition: bloaty/third_party/protobuf/benchmarks/util/data_proto2_to_proto3_util.h:11
i
uint64_t i
Definition: abseil-cpp/absl/container/btree_benchmark.cc:230


grpc
Author(s):
autogenerated on Fri May 16 2025 02:59:43