protoc-gen-proto2_to_proto3.cc
Go to the documentation of this file.
7 
9 
10 using google::protobuf::FileDescriptorProto;
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,
40  GeneratorContext* context,
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 
54  bool Generate(const FileDescriptor* file,
55  const string& parameter,
56  GeneratorContext* context,
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 }
zero_copy_stream.h
main
int main(int argc, char *argv[])
Definition: protoc-gen-proto2_to_proto3.cc:112
schema_proto2_to_proto3_util.h
google::protobuf::compiler::Proto2ToProto3Generator::GenerateAll
bool GenerateAll(const std::vector< const FileDescriptor * > &files, const string &parameter, GeneratorContext *context, string *error) const
Definition: protoc-gen-proto2_to_proto3.cc:38
google::protobuf::io::Printer::WriteRaw
void WriteRaw(const char *data, int size)
Definition: printer.cc:205
google::protobuf::compiler::CodeGenerator
Definition: code_generator.h:68
google::protobuf::python::cdescriptor_pool::FindFileByName
static PyObject * FindFileByName(PyObject *self, PyObject *arg)
Definition: descriptor_pool.cc:258
google::protobuf::compiler::cpp::StripProto
std::string StripProto(const std::string &filename)
Definition: cpp_helpers.cc:474
google::protobuf::compiler::GeneratorContext::Open
virtual io::ZeroCopyOutputStream * Open(const std::string &filename)=0
FileDescriptorProto::set_syntax
void set_syntax(const std::string &value)
Definition: descriptor.pb.h:7105
google::protobuf::util::ExtensionStripper
Definition: schema_proto2_to_proto3_util.h:133
plugin.h
google::protobuf::util::EnumScrubber
Definition: schema_proto2_to_proto3_util.h:77
google::protobuf::util::SchemaGroupStripper
Definition: schema_proto2_to_proto3_util.h:22
google::protobuf::compiler::PluginMain
int PluginMain(int argc, char *argv[], const CodeGenerator *generator)
Definition: plugin.cc:147
error
Definition: cJSON.c:88
FileDescriptor
struct FileDescriptor FileDescriptor
Definition: ruby/ext/google/protobuf_c/protobuf.h:56
google::protobuf::util::EnumScrubber::ScrubFile
void ScrubFile(FileDescriptorProto *file)
Definition: schema_proto2_to_proto3_util.h:84
google::protobuf::FileDescriptor::public_dependency_count
int public_dependency_count() const
google::protobuf::compiler::Proto2ToProto3Generator::CanGenerate
bool CanGenerate(const FileDescriptor *file) const
Definition: protoc-gen-proto2_to_proto3.cc:83
printer.h
code_generator.h
FileDescriptorProto
Definition: descriptor.pb.h:501
google::protobuf::util::FieldScrubber
Definition: schema_proto2_to_proto3_util.h:152
google::protobuf::FileDescriptor::weak_dependency
const FileDescriptor * weak_dependency(int index) const
Definition: src/google/protobuf/descriptor.h:2171
google::protobuf::compiler::Proto2ToProto3Generator
Definition: protoc-gen-proto2_to_proto3.cc:36
google::protobuf::FileDescriptor::CopyTo
void CopyTo(FileDescriptorProto *proto) const
Definition: src/google/protobuf/descriptor.cc:2010
google::protobuf::io::Printer
Definition: printer.h:181
pool
InternalDescriptorPool * pool
Definition: php/ext/google/protobuf/protobuf.h:798
i
int i
Definition: gmock-matchers_test.cc:764
google::protobuf::FileDescriptor::name
const std::string & name() const
google::protobuf::compiler::Proto2ToProto3Generator::Generate
bool Generate(const FileDescriptor *file, const string &parameter, GeneratorContext *context, string *error) const
Definition: protoc-gen-proto2_to_proto3.cc:54
google::protobuf::python::file_descriptor::GetPool
static PyObject * GetPool(PyFileDescriptor *self, void *closure)
Definition: python/google/protobuf/pyext/descriptor.cc:1330
google::protobuf::FileDescriptor::dependency
const FileDescriptor * dependency(int index) const
Definition: src/google/protobuf/descriptor.cc:7271
descriptor.h
google::protobuf::compiler::ParseGeneratorParameter
void ParseGeneratorParameter(const std::string &text, std::vector< std::pair< std::string, std::string > > *output)
Definition: code_generator.cc:101
google::protobuf::FileDescriptor
Definition: src/google/protobuf/descriptor.h:1320
descriptor.pb.h
output
const upb_json_parsermethod const upb_symtab upb_sink * output
Definition: ruby/ext/google/protobuf_c/upb.h:10503
DescriptorPool
Definition: ruby/ext/google/protobuf_c/protobuf.h:109
google::protobuf::compiler::GeneratorContext
Definition: code_generator.h:119
DescriptorPool
struct DescriptorPool DescriptorPool
Definition: php/ext/google/protobuf/protobuf.h:629
google::protobuf::FileDescriptor::public_dependency
const FileDescriptor * public_dependency(int index) const
Definition: src/google/protobuf/descriptor.h:2166
compiler
Definition: plugin.pb.cc:22
google
Definition: data_proto2_to_proto3_util.h:11
google::protobuf::FileDescriptor::weak_dependency_count
int weak_dependency_count() const
google::protobuf::FileDescriptor::dependency_count
int dependency_count() const


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