protobuf/benchmarks/util/protoc-gen-gogoproto.cc
Go to the documentation of this file.
1 #include "google/protobuf/compiler/code_generator.h"
2 #include "google/protobuf/io/zero_copy_stream.h"
3 #include "google/protobuf/io/printer.h"
4 #include "google/protobuf/descriptor.h"
5 #include "google/protobuf/descriptor.pb.h"
7 
8 #include "google/protobuf/compiler/plugin.h"
9 
16 
17 namespace google {
18 namespace protobuf {
19 namespace compiler {
20 
21 namespace {
22 
23 string StripProto(string filename) {
24  if (filename.substr(filename.size() - 11) == ".protodevel") {
25  // .protodevel
26  return filename.substr(0, filename.size() - 11);
27  } else {
28  // .proto
29  return filename.substr(0, filename.size() - 6);
30  }
31 }
32 
33 DescriptorPool new_pool_;
34 
35 } // namespace
36 
37 class GoGoProtoGenerator : public CodeGenerator {
38  public:
39  virtual bool GenerateAll(const std::vector<const FileDescriptor*>& files,
40  const string& parameter,
42  string* error) const {
43  for (int i = 0; i < files.size(); i++) {
44  for (auto file : files) {
45  bool can_generate =
46  (new_pool_.FindFileByName(file->name()) == nullptr);
47  for (int j = 0; j < file->dependency_count(); j++) {
48  can_generate &= (new_pool_.FindFileByName(
49  file->dependency(j)->name()) != nullptr);
50  }
51  for (int j = 0; j < file->public_dependency_count(); j++) {
52  can_generate &= (new_pool_.FindFileByName(
53  file->public_dependency(j)->name()) != nullptr);
54  }
55  for (int j = 0; j < file->weak_dependency_count(); j++) {
56  can_generate &= (new_pool_.FindFileByName(
57  file->weak_dependency(j)->name()) != nullptr);
58  }
59  if (can_generate) {
60  Generate(file, parameter, context, error);
61  break;
62  }
63  }
64  }
65 
66  return true;
67  }
68 
69  virtual bool Generate(const FileDescriptor* file,
70  const string& parameter,
72  string* error) const {
73  FileDescriptorProto new_file;
74  file->CopyTo(&new_file);
75  SchemaGroupStripper::StripFile(file, &new_file);
76 
77  EnumScrubber enum_scrubber;
78  enum_scrubber.ScrubFile(&new_file);
79 
80  string filename = file->name();
81  string basename = StripProto(filename);
82 
83  std::vector<std::pair<string,string>> option_pairs;
84  ParseGeneratorParameter(parameter, &option_pairs);
85 
86  std::unique_ptr<google::protobuf::io::ZeroCopyOutputStream> output(
87  context->Open(basename + ".proto"));
88  string content = new_pool_.BuildFile(new_file)->DebugString();
89  Printer printer(output.get(), '$');
90  printer.WriteRaw(content.c_str(), content.size());
91 
92  return true;
93  }
94 };
95 
96 } // namespace compiler
97 } // namespace protobuf
98 } // namespace google
99 
100 int main(int argc, char* argv[]) {
102  return google::protobuf::compiler::PluginMain(argc, argv, &generator);
103 }
check_grpcio_tools.content
content
Definition: check_grpcio_tools.py:26
filename
const char * filename
Definition: bloaty/third_party/zlib/contrib/minizip/ioapi.h:135
google::protobuf::io::Printer::WriteRaw
void WriteRaw(const char *data, int size)
Definition: bloaty/third_party/protobuf/src/google/protobuf/io/printer.cc:206
error
grpc_error_handle error
Definition: retry_filter.cc:499
file
Definition: bloaty/third_party/zlib/examples/gzappend.c:170
grpc::protobuf::io::Printer
GRPC_CUSTOM_PRINTER Printer
Definition: src/compiler/config.h:54
google::protobuf
Definition: bloaty/third_party/protobuf/benchmarks/util/data_proto2_to_proto3_util.h:12
grpc::protobuf::DescriptorPool
GRPC_CUSTOM_DESCRIPTORPOOL DescriptorPool
Definition: include/grpcpp/impl/codegen/config_protobuf.h:82
google::protobuf::util::EnumScrubber
Definition: bloaty/third_party/protobuf/benchmarks/util/schema_proto2_to_proto3_util.h:77
google::protobuf::util::SchemaGroupStripper
Definition: bloaty/third_party/protobuf/benchmarks/util/schema_proto2_to_proto3_util.h:22
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::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::util::EnumScrubber::ScrubFile
void ScrubFile(FileDescriptorProto *file)
Definition: bloaty/third_party/protobuf/benchmarks/util/schema_proto2_to_proto3_util.h:84
schema_proto2_to_proto3_util.h
FileDescriptorProto
Definition: bloaty/third_party/protobuf/src/google/protobuf/descriptor.pb.h:501
main
int main(int argc, char *argv[])
Definition: protobuf/benchmarks/util/protoc-gen-gogoproto.cc:100
google::protobuf::compiler::GoGoProtoGenerator::Generate
virtual bool Generate(const FileDescriptor *file, const string &parameter, GeneratorContext *context, string *error) const
Definition: bloaty/third_party/protobuf/benchmarks/util/protoc-gen-gogoproto.cc:69
google::protobuf::io::Printer
Definition: bloaty/third_party/protobuf/src/google/protobuf/io/printer.h:181
google::protobuf::compiler::StripProto
std::string StripProto(const std::string &filename)
Definition: protobuf/src/google/protobuf/compiler/code_generator.cc:127
google::protobuf::compiler::ParseGeneratorParameter
void ParseGeneratorParameter(const std::string &text, std::vector< std::pair< std::string, std::string > > *output)
Definition: bloaty/third_party/protobuf/src/google/protobuf/compiler/code_generator.cc:101
google::protobuf::compiler::GoGoProtoGenerator
Definition: bloaty/third_party/protobuf/benchmarks/util/protoc-gen-gogoproto.cc:37
FileDescriptor
struct FileDescriptor FileDescriptor
Definition: bloaty/third_party/protobuf/ruby/ext/google/protobuf_c/protobuf.h:56
google::protobuf::FileDescriptor
Definition: bloaty/third_party/protobuf/src/google/protobuf/descriptor.h:1320
grpc::protobuf::FileDescriptorProto
GRPC_CUSTOM_FILEDESCRIPTORPROTO FileDescriptorProto
Definition: include/grpcpp/impl/codegen/config_protobuf.h:86
context
grpc::ClientContext context
Definition: istio_echo_server_lib.cc:61
google::protobuf::compiler::GoGoProtoGenerator::GenerateAll
virtual bool GenerateAll(const std::vector< const FileDescriptor * > &files, const string &parameter, GeneratorContext *context, string *error) const
Definition: protobuf/benchmarks/util/protoc-gen-gogoproto.cc:39
file::name
char * name
Definition: bloaty/third_party/zlib/examples/gzappend.c:176
DescriptorPool
Definition: bloaty/third_party/protobuf/ruby/ext/google/protobuf_c/protobuf.h:110
google::protobuf::compiler::GeneratorContext
Definition: bloaty/third_party/protobuf/src/google/protobuf/compiler/code_generator.h:119
amalgamate.files
list files
Definition: amalgamate.py:115
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:47