protoc-gen-proto2_to_proto3.cc 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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"
  6. #include "schema_proto2_to_proto3_util.h"
  7. #include "google/protobuf/compiler/plugin.h"
  8. using google::protobuf::FileDescriptorProto;
  9. using google::protobuf::FileDescriptor;
  10. using google::protobuf::DescriptorPool;
  11. using google::protobuf::io::Printer;
  12. using google::protobuf::util::SchemaGroupStripper;
  13. using google::protobuf::util::EnumScrubber;
  14. using google::protobuf::util::ExtensionStripper;
  15. using google::protobuf::util::FieldScrubber;
  16. namespace google {
  17. namespace protobuf {
  18. namespace compiler {
  19. namespace {
  20. string StripProto(string filename) {
  21. return filename.substr(0, filename.rfind(".proto"));
  22. }
  23. DescriptorPool* GetPool() {
  24. static DescriptorPool *pool = new DescriptorPool();
  25. return pool;
  26. }
  27. } // namespace
  28. class Proto2ToProto3Generator final : public CodeGenerator {
  29. public:
  30. bool GenerateAll(const std::vector<const FileDescriptor*>& files,
  31. const string& parameter,
  32. GeneratorContext* context,
  33. string* error) const {
  34. for (int i = 0; i < files.size(); i++) {
  35. for (auto file : files) {
  36. if (CanGenerate(file)) {
  37. Generate(file, parameter, context, error);
  38. break;
  39. }
  40. }
  41. }
  42. return true;
  43. }
  44. bool Generate(const FileDescriptor* file,
  45. const string& parameter,
  46. GeneratorContext* context,
  47. string* error) const {
  48. FileDescriptorProto new_file;
  49. file->CopyTo(&new_file);
  50. SchemaGroupStripper::StripFile(file, &new_file);
  51. EnumScrubber enum_scrubber;
  52. enum_scrubber.ScrubFile(&new_file);
  53. ExtensionStripper::StripFile(&new_file);
  54. FieldScrubber::ScrubFile(&new_file);
  55. new_file.set_syntax("proto3");
  56. string filename = file->name();
  57. string basename = StripProto(filename);
  58. std::vector<std::pair<string,string>> option_pairs;
  59. ParseGeneratorParameter(parameter, &option_pairs);
  60. std::unique_ptr<google::protobuf::io::ZeroCopyOutputStream> output(
  61. context->Open(basename + ".proto"));
  62. string content = GetPool()->BuildFile(new_file)->DebugString();
  63. Printer printer(output.get(), '$');
  64. printer.WriteRaw(content.c_str(), content.size());
  65. return true;
  66. }
  67. private:
  68. bool CanGenerate(const FileDescriptor* file) const {
  69. if (GetPool()->FindFileByName(file->name()) != nullptr) {
  70. return false;
  71. }
  72. for (int j = 0; j < file->dependency_count(); j++) {
  73. if (GetPool()->FindFileByName(file->dependency(j)->name()) == nullptr) {
  74. return false;
  75. }
  76. }
  77. for (int j = 0; j < file->public_dependency_count(); j++) {
  78. if (GetPool()->FindFileByName(
  79. file->public_dependency(j)->name()) == nullptr) {
  80. return false;
  81. }
  82. }
  83. for (int j = 0; j < file->weak_dependency_count(); j++) {
  84. if (GetPool()->FindFileByName(
  85. file->weak_dependency(j)->name()) == nullptr) {
  86. return false;
  87. }
  88. }
  89. return true;
  90. }
  91. };
  92. } // namespace compiler
  93. } // namespace protobuf
  94. } // namespace google
  95. int main(int argc, char* argv[]) {
  96. google::protobuf::compiler::Proto2ToProto3Generator generator;
  97. return google::protobuf::compiler::PluginMain(argc, argv, &generator);
  98. }