1 
2 #include <memory>
3 
4 #include "google/protobuf/compiler/code_generator.h"
5 #include "google/protobuf/compiler/plugin.h"
6 #include "google/protobuf/descriptor.h"
7 #include "google/protobuf/descriptor.pb.h"
8 #include "upbc/common.h"
9 
10 namespace upbc {
11 namespace {
12 
13 namespace protoc = ::google::protobuf::compiler;
14 namespace protobuf = ::google::protobuf;
15 
DefInitSymbol(const protobuf::FileDescriptor * file)16 std::string DefInitSymbol(const protobuf::FileDescriptor *file) {
17   return ToCIdent(file->name()) + "_upbdefinit";
18 }
19 
DefHeaderFilename(std::string proto_filename)20 static std::string DefHeaderFilename(std::string proto_filename) {
21   return StripExtension(proto_filename) + ".upbdefs.h";
22 }
23 
DefSourceFilename(std::string proto_filename)24 static std::string DefSourceFilename(std::string proto_filename) {
25   return StripExtension(proto_filename) + ".upbdefs.c";
26 }
27 
GenerateMessageDefAccessor(const protobuf::Descriptor * d,Output & output)28 void GenerateMessageDefAccessor(const protobuf::Descriptor* d, Output& output) {
29   output("UPB_INLINE const upb_msgdef *$0_getmsgdef(upb_symtab *s) {\n",
30          ToCIdent(d->full_name()));
31   output("  _upb_symtab_loaddefinit(s, &$0);\n", DefInitSymbol(d->file()));
32   output("  return upb_symtab_lookupmsg(s, \"$0\");\n", d->full_name());
33   output("}\n");
34   output("\n");
35 
36   for (int i = 0; i < d->nested_type_count(); i++) {
37     GenerateMessageDefAccessor(d->nested_type(i), output);
38   }
39 }
40 
WriteDefHeader(const protobuf::FileDescriptor * file,Output & output)41 void WriteDefHeader(const protobuf::FileDescriptor* file, Output& output) {
42   EmitFileWarning(file, output);
43 
44   output(
45       "#ifndef $0_UPBDEFS_H_\n"
46       "#define $0_UPBDEFS_H_\n\n"
47       "#include \"upb/def.h\"\n"
48       "#include \"upb/port_def.inc\"\n"
49       "#ifdef __cplusplus\n"
50       "extern \"C\" {\n"
51       "#endif\n\n",
52       ToPreproc(file->name()));
53 
54   output("#include \"upb/def.h\"\n");
55   output("\n");
56   output("#include \"upb/port_def.inc\"\n");
57   output("\n");
58 
59   output("extern upb_def_init $0;\n", DefInitSymbol(file));
60   output("\n");
61 
62   for (int i = 0; i < file->message_type_count(); i++) {
63     GenerateMessageDefAccessor(file->message_type(i), output);
64   }
65 
66   output(
67       "#ifdef __cplusplus\n"
68       "}  /* extern \"C\" */\n"
69       "#endif\n"
70       "\n"
71       "#include \"upb/port_undef.inc\"\n"
72       "\n"
73       "#endif  /* $0_UPBDEFS_H_ */\n",
74       ToPreproc(file->name()));
75 }
76 
77 
WriteDefSource(const protobuf::FileDescriptor * file,Output & output)78 void WriteDefSource(const protobuf::FileDescriptor* file, Output& output) {
79   EmitFileWarning(file, output);
80 
81   output("#include \"upb/def.h\"\n");
82   output("#include \"$0\"\n", DefHeaderFilename(file->name()));
83   output("\n");
84 
85   for (int i = 0; i < file->dependency_count(); i++) {
86     output("extern upb_def_init $0;\n", DefInitSymbol(file->dependency(i)));
87   }
88 
89   std::vector<const protobuf::Descriptor*> file_messages =
90       SortedMessages(file);
91 
92   for (auto message : file_messages) {
93     output("extern const upb_msglayout $0;\n", MessageInit(message));
94   }
95   output("\n");
96 
97   if (!file_messages.empty()) {
98     output("static const upb_msglayout *layouts[$0] = {\n", file_messages.size());
99     for (auto message : file_messages) {
100       output("  &$0,\n", MessageInit(message));
101     }
102     output("};\n");
103     output("\n");
104   }
105 
106   protobuf::FileDescriptorProto file_proto;
107   file->CopyTo(&file_proto);
108   std::string file_data;
109   file_proto.SerializeToString(&file_data);
110 
111   output("static const char descriptor[$0] = {", file_data.size());
112 
113   // C90 only guarantees that strings can be up to 509 characters, and some
114   // implementations have limits here (for example, MSVC only allows 64k:
115   // https://docs.microsoft.com/en-us/cpp/error-messages/compiler-errors-1/fatal-error-c1091.
116   // So we always emit an array instead of a string.
117   for (size_t i = 0; i < file_data.size();) {
118     for (size_t j = 0; j < 25 && i < file_data.size(); ++i, ++j) {
119       output("'$0', ", absl::CEscape(file_data.substr(i, 1)));
120     }
121     output("\n");
122   }
123   output("};\n\n");
124 
125   output("static upb_def_init *deps[$0] = {\n", file->dependency_count() + 1);
126   for (int i = 0; i < file->dependency_count(); i++) {
127     output("  &$0,\n", DefInitSymbol(file->dependency(i)));
128   }
129   output("  NULL\n");
130   output("};\n");
131   output("\n");
132 
133   output("upb_def_init $0 = {\n", DefInitSymbol(file));
134   output("  deps,\n");
135   if (file_messages.empty()) {
136     output("  NULL,\n");
137   } else {
138     output("  layouts,\n");
139   }
140   output("  \"$0\",\n", file->name());
141   output("  UPB_STRVIEW_INIT(descriptor, $0)\n", file_data.size());
142   output("};\n");
143 }
144 
145 class Generator : public protoc::CodeGenerator {
~Generator()146   ~Generator() override {}
147   bool Generate(const protobuf::FileDescriptor* file,
148                 const std::string& parameter, protoc::GeneratorContext* context,
149                 std::string* error) const override;
GetSupportedFeatures() const150   uint64_t GetSupportedFeatures() const override {
151     return FEATURE_PROTO3_OPTIONAL;
152   }
153 };
154 
Generate(const protobuf::FileDescriptor * file,const std::string & parameter,protoc::GeneratorContext * context,std::string * error) const155 bool Generator::Generate(const protobuf::FileDescriptor* file,
156                          const std::string& parameter,
157                          protoc::GeneratorContext* context,
158                          std::string* error) const {
159   std::vector<std::pair<std::string, std::string>> params;
160   google::protobuf::compiler::ParseGeneratorParameter(parameter, &params);
161 
162   for (const auto& pair : params) {
163     *error = "Unknown parameter: " + pair.first;
164     return false;
165   }
166 
167   Output h_def_output(context->Open(DefHeaderFilename(file->name())));
168   WriteDefHeader(file, h_def_output);
169 
170   Output c_def_output(context->Open(DefSourceFilename(file->name())));
171   WriteDefSource(file, c_def_output);
172 
173   return true;
174 }
175 
176 }  // namespace
177 }  // namespace upbc
178 
main(int argc,char ** argv)179 int main(int argc, char** argv) {
180   std::unique_ptr<google::protobuf::compiler::CodeGenerator> generator(
181       new upbc::Generator());
182   return google::protobuf::compiler::PluginMain(argc, argv, generator.get());
183 }
184