protobuf/benchmarks/util/protoc-gen-proto2_to_proto3.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 
18 
19 namespace google {
20 namespace protobuf {
21 namespace compiler {
22 
23 namespace {
24 
25 string StripProto(string filename) {
26  return filename.substr(0, filename.rfind(".proto"));
27 }
28 
30  static DescriptorPool *pool = new DescriptorPool();
31  return pool;
32 }
33 
34 } // namespace
35 
36 class Proto2ToProto3Generator final : public CodeGenerator {
37  public:
38  bool GenerateAll(const std::vector<const FileDescriptor*>& files,
39  const string& parameter,
41  string* error) const {
42  for (int i = 0; i < files.size(); i++) {
43  for (auto file : files) {
44  if (CanGenerate(file)) {
45  Generate(file, parameter, context, error);
46  break;
47  }
48  }
49  }
50 
51  return true;
52  }
53 
55  const string& parameter,
57  string* error) const {
58  FileDescriptorProto new_file;
59  file->CopyTo(&new_file);
60  SchemaGroupStripper::StripFile(file, &new_file);
61 
62  EnumScrubber enum_scrubber;
63  enum_scrubber.ScrubFile(&new_file);
64  ExtensionStripper::StripFile(&new_file);
65  FieldScrubber::ScrubFile(&new_file);
66  new_file.set_syntax("proto3");
67 
68  string filename = file->name();
69  string basename = StripProto(filename);
70 
71  std::vector<std::pair<string,string>> option_pairs;
72  ParseGeneratorParameter(parameter, &option_pairs);
73 
74  std::unique_ptr<google::protobuf::io::ZeroCopyOutputStream> output(
75  context->Open(basename + ".proto"));
76  string content = GetPool()->BuildFile(new_file)->DebugString();
77  Printer printer(output.get(), '$');
78  printer.WriteRaw(content.c_str(), content.size());
79 
80  return true;
81  }
82  private:
83  bool CanGenerate(const FileDescriptor* file) const {
84  if (GetPool()->FindFileByName(file->name()) != nullptr) {
85  return false;
86  }
87  for (int j = 0; j < file->dependency_count(); j++) {
88  if (GetPool()->FindFileByName(file->dependency(j)->name()) == nullptr) {
89  return false;
90  }
91  }
92  for (int j = 0; j < file->public_dependency_count(); j++) {
93  if (GetPool()->FindFileByName(
94  file->public_dependency(j)->name()) == nullptr) {
95  return false;
96  }
97  }
98  for (int j = 0; j < file->weak_dependency_count(); j++) {
99  if (GetPool()->FindFileByName(
100  file->weak_dependency(j)->name()) == nullptr) {
101  return false;
102  }
103  }
104  return true;
105  }
106 };
107 
108 } // namespace compiler
109 } // namespace protobuf
110 } // namespace google
111 
112 int main(int argc, char* argv[]) {
114  return google::protobuf::compiler::PluginMain(argc, argv, &generator);
115 }
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::compiler::Proto2ToProto3Generator::GenerateAll
bool GenerateAll(const std::vector< const FileDescriptor * > &files, const string &parameter, GeneratorContext *context, string *error) const
Definition: protobuf/benchmarks/util/protoc-gen-proto2_to_proto3.cc:38
google::protobuf::io::Printer::WriteRaw
void WriteRaw(const char *data, int size)
Definition: bloaty/third_party/protobuf/src/google/protobuf/io/printer.cc:206
google::protobuf::python::cdescriptor_pool::FindFileByName
static PyObject * FindFileByName(PyObject *self, PyObject *arg)
Definition: bloaty/third_party/protobuf/python/google/protobuf/pyext/descriptor_pool.cc:258
FileDescriptorProto::set_syntax
void set_syntax(const std::string &value)
Definition: bloaty/third_party/protobuf/src/google/protobuf/descriptor.pb.h:7971
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
google::protobuf::util::ExtensionStripper
Definition: bloaty/third_party/protobuf/benchmarks/util/schema_proto2_to_proto3_util.h:133
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
main
int main(int argc, char *argv[])
Definition: protobuf/benchmarks/util/protoc-gen-proto2_to_proto3.cc:112
google::protobuf::util::EnumScrubber::ScrubFile
void ScrubFile(FileDescriptorProto *file)
Definition: bloaty/third_party/protobuf/benchmarks/util/schema_proto2_to_proto3_util.h:84
google::protobuf::compiler::Proto2ToProto3Generator::CanGenerate
bool CanGenerate(const FileDescriptor *file) const
Definition: bloaty/third_party/protobuf/benchmarks/util/protoc-gen-proto2_to_proto3.cc:83
schema_proto2_to_proto3_util.h
FileDescriptorProto
Definition: bloaty/third_party/protobuf/src/google/protobuf/descriptor.pb.h:501
google::protobuf::util::FieldScrubber
Definition: bloaty/third_party/protobuf/benchmarks/util/schema_proto2_to_proto3_util.h:152
google::protobuf::compiler::Proto2ToProto3Generator
Definition: bloaty/third_party/protobuf/benchmarks/util/protoc-gen-proto2_to_proto3.cc:36
google::protobuf::io::Printer
Definition: bloaty/third_party/protobuf/src/google/protobuf/io/printer.h:181
google::protobuf::compiler::Proto2ToProto3Generator::Generate
bool Generate(const FileDescriptor *file, const string &parameter, GeneratorContext *context, string *error) const
Definition: bloaty/third_party/protobuf/benchmarks/util/protoc-gen-proto2_to_proto3.cc:54
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
FileDescriptor
struct FileDescriptor FileDescriptor
Definition: bloaty/third_party/protobuf/ruby/ext/google/protobuf_c/protobuf.h:56
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
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
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
GetPool
static DescriptorPool * GetPool(const zval *this_ptr)
Definition: protobuf/php/ext/google/protobuf/def.c:729
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