protoc-gen-upbdefs.cc 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. // Copyright (c) 2009-2021, Google LLC
  2. // All rights reserved.
  3. //
  4. // Redistribution and use in source and binary forms, with or without
  5. // modification, are permitted provided that the following conditions are met:
  6. // * Redistributions of source code must retain the above copyright
  7. // notice, this list of conditions and the following disclaimer.
  8. // * Redistributions in binary form must reproduce the above copyright
  9. // notice, this list of conditions and the following disclaimer in the
  10. // documentation and/or other materials provided with the distribution.
  11. // * Neither the name of Google LLC nor the
  12. // names of its contributors may be used to endorse or promote products
  13. // derived from this software without specific prior written permission.
  14. //
  15. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  18. // ARE DISCLAIMED. IN NO EVENT SHALL Google LLC BE LIABLE FOR ANY DIRECT,
  19. // INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  20. // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  21. // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  22. // ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  23. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  24. // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. #include <memory>
  26. #include "google/protobuf/compiler/code_generator.h"
  27. #include "google/protobuf/compiler/plugin.h"
  28. #include "google/protobuf/descriptor.h"
  29. #include "google/protobuf/descriptor.pb.h"
  30. #include "upbc/common.h"
  31. namespace upbc {
  32. namespace {
  33. namespace protoc = ::google::protobuf::compiler;
  34. namespace protobuf = ::google::protobuf;
  35. std::string DefInitSymbol(const protobuf::FileDescriptor* file) {
  36. return ToCIdent(file->name()) + "_upbdefinit";
  37. }
  38. static std::string DefHeaderFilename(std::string proto_filename) {
  39. return StripExtension(proto_filename) + ".upbdefs.h";
  40. }
  41. static std::string DefSourceFilename(std::string proto_filename) {
  42. return StripExtension(proto_filename) + ".upbdefs.c";
  43. }
  44. void GenerateMessageDefAccessor(const protobuf::Descriptor* d, Output& output) {
  45. output("UPB_INLINE const upb_MessageDef *$0_getmsgdef(upb_DefPool *s) {\n",
  46. ToCIdent(d->full_name()));
  47. output(" _upb_DefPool_LoadDefInit(s, &$0);\n", DefInitSymbol(d->file()));
  48. output(" return upb_DefPool_FindMessageByName(s, \"$0\");\n",
  49. d->full_name());
  50. output("}\n");
  51. output("\n");
  52. for (int i = 0; i < d->nested_type_count(); i++) {
  53. GenerateMessageDefAccessor(d->nested_type(i), output);
  54. }
  55. }
  56. void WriteDefHeader(const protobuf::FileDescriptor* file, Output& output) {
  57. EmitFileWarning(file, output);
  58. output(
  59. "#ifndef $0_UPBDEFS_H_\n"
  60. "#define $0_UPBDEFS_H_\n\n"
  61. "#include \"upb/def.h\"\n"
  62. "#include \"upb/port_def.inc\"\n"
  63. "#ifdef __cplusplus\n"
  64. "extern \"C\" {\n"
  65. "#endif\n\n",
  66. ToPreproc(file->name()));
  67. output("#include \"upb/def.h\"\n");
  68. output("\n");
  69. output("#include \"upb/port_def.inc\"\n");
  70. output("\n");
  71. output("extern _upb_DefPool_Init $0;\n", DefInitSymbol(file));
  72. output("\n");
  73. for (int i = 0; i < file->message_type_count(); i++) {
  74. GenerateMessageDefAccessor(file->message_type(i), output);
  75. }
  76. output(
  77. "#ifdef __cplusplus\n"
  78. "} /* extern \"C\" */\n"
  79. "#endif\n"
  80. "\n"
  81. "#include \"upb/port_undef.inc\"\n"
  82. "\n"
  83. "#endif /* $0_UPBDEFS_H_ */\n",
  84. ToPreproc(file->name()));
  85. }
  86. void WriteDefSource(const protobuf::FileDescriptor* file, Output& output) {
  87. EmitFileWarning(file, output);
  88. output("#include \"upb/def.h\"\n");
  89. output("#include \"$0\"\n", DefHeaderFilename(file->name()));
  90. output("#include \"$0\"\n", HeaderFilename(file));
  91. output("\n");
  92. for (int i = 0; i < file->dependency_count(); i++) {
  93. output("extern _upb_DefPool_Init $0;\n",
  94. DefInitSymbol(file->dependency(i)));
  95. }
  96. protobuf::FileDescriptorProto file_proto;
  97. file->CopyTo(&file_proto);
  98. std::string file_data;
  99. file_proto.SerializeToString(&file_data);
  100. output("static const char descriptor[$0] = {", file_data.size());
  101. // C90 only guarantees that strings can be up to 509 characters, and some
  102. // implementations have limits here (for example, MSVC only allows 64k:
  103. // https://docs.microsoft.com/en-us/cpp/error-messages/compiler-errors-1/fatal-error-c1091.
  104. // So we always emit an array instead of a string.
  105. for (size_t i = 0; i < file_data.size();) {
  106. for (size_t j = 0; j < 25 && i < file_data.size(); ++i, ++j) {
  107. output("'$0', ", absl::CEscape(file_data.substr(i, 1)));
  108. }
  109. output("\n");
  110. }
  111. output("};\n\n");
  112. output("static _upb_DefPool_Init *deps[$0] = {\n",
  113. file->dependency_count() + 1);
  114. for (int i = 0; i < file->dependency_count(); i++) {
  115. output(" &$0,\n", DefInitSymbol(file->dependency(i)));
  116. }
  117. output(" NULL\n");
  118. output("};\n");
  119. output("\n");
  120. output("_upb_DefPool_Init $0 = {\n", DefInitSymbol(file));
  121. output(" deps,\n");
  122. output(" &$0,\n", FileLayoutName(file));
  123. output(" \"$0\",\n", file->name());
  124. output(" UPB_STRINGVIEW_INIT(descriptor, $0)\n", file_data.size());
  125. output("};\n");
  126. }
  127. class Generator : public protoc::CodeGenerator {
  128. ~Generator() override {}
  129. bool Generate(const protobuf::FileDescriptor* file,
  130. const std::string& parameter, protoc::GeneratorContext* context,
  131. std::string* error) const override;
  132. uint64_t GetSupportedFeatures() const override {
  133. return FEATURE_PROTO3_OPTIONAL;
  134. }
  135. };
  136. bool Generator::Generate(const protobuf::FileDescriptor* file,
  137. const std::string& parameter,
  138. protoc::GeneratorContext* context,
  139. std::string* error) const {
  140. std::vector<std::pair<std::string, std::string>> params;
  141. google::protobuf::compiler::ParseGeneratorParameter(parameter, &params);
  142. for (const auto& pair : params) {
  143. *error = "Unknown parameter: " + pair.first;
  144. return false;
  145. }
  146. Output h_def_output(context->Open(DefHeaderFilename(file->name())));
  147. WriteDefHeader(file, h_def_output);
  148. Output c_def_output(context->Open(DefSourceFilename(file->name())));
  149. WriteDefSource(file, c_def_output);
  150. return true;
  151. }
  152. } // namespace
  153. } // namespace upbc
  154. int main(int argc, char** argv) {
  155. std::unique_ptr<google::protobuf::compiler::CodeGenerator> generator(
  156. new upbc::Generator());
  157. return google::protobuf::compiler::PluginMain(argc, argv, generator.get());
  158. }