• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Protocol Buffers - Google's data interchange format
2 // Copyright 2023 Google LLC.  All rights reserved.
3 //
4 // Use of this source code is governed by a BSD-style
5 // license that can be found in the LICENSE file or at
6 // https://developers.google.com/open-source/licenses/bsd
7 
8 #include <cstddef>
9 #include <string>
10 
11 #include "google/protobuf/descriptor.upb.h"
12 #include "absl/strings/escaping.h"
13 #include "absl/strings/string_view.h"
14 #include "absl/strings/substitute.h"
15 #include "upb/mem/arena.hpp"
16 #include "upb/reflection/def.hpp"
17 #include "upb/util/def_to_proto.h"
18 #include "upb_generator/common.h"
19 #include "upb_generator/common/names.h"
20 #include "upb_generator/file_layout.h"
21 #include "upb_generator/minitable/names.h"
22 #include "upb_generator/plugin.h"
23 #include "upb_generator/reflection/names.h"
24 
25 namespace upb {
26 namespace generator {
27 namespace {
28 
29 struct Options {
30   std::string dllexport_decl;
31 };
32 
DefInitSymbol(upb::FileDefPtr file)33 std::string DefInitSymbol(upb::FileDefPtr file) {
34   return ReflectionFileSymbol(file.name());
35 }
36 
DefHeaderFilename(upb::FileDefPtr file)37 static std::string DefHeaderFilename(upb::FileDefPtr file) {
38   return StripExtension(file.name()) + ".upbdefs.h";
39 }
40 
DefSourceFilename(upb::FileDefPtr file)41 static std::string DefSourceFilename(upb::FileDefPtr file) {
42   return StripExtension(file.name()) + ".upbdefs.c";
43 }
44 
GenerateMessageDefAccessor(upb::MessageDefPtr d,Output & output)45 void GenerateMessageDefAccessor(upb::MessageDefPtr d, Output& output) {
46   output("UPB_INLINE const upb_MessageDef *$0(upb_DefPool *s) {\n",
47          ReflectionGetMessageSymbol(d.full_name()));
48   output("  _upb_DefPool_LoadDefInit(s, &$0);\n", DefInitSymbol(d.file()));
49   output("  return upb_DefPool_FindMessageByName(s, \"$0\");\n", d.full_name());
50   output("}\n");
51   output("\n");
52 }
53 
WriteDefHeader(upb::FileDefPtr file,const Options & options,Output & output)54 void WriteDefHeader(upb::FileDefPtr file, const Options& options,
55                     Output& output) {
56   output(FileWarning(file.name()));
57 
58   output(
59       "#ifndef $0_UPBDEFS_H_\n"
60       "#define $0_UPBDEFS_H_\n\n"
61       "#include \"upb/reflection/def.h\"\n"
62       "#include \"upb/reflection/internal/def_pool.h\"\n"
63       "\n"
64       "#include \"upb/port/def.inc\" // Must be last.\n"
65       "#ifdef __cplusplus\n"
66       "extern \"C\" {\n"
67       "#endif\n\n",
68       IncludeGuard(file.name()));
69 
70   output("extern$1 _upb_DefPool_Init $0;\n", DefInitSymbol(file),
71          PadPrefix(options.dllexport_decl));
72   output("\n");
73 
74   for (auto msg : SortedMessages(file)) {
75     GenerateMessageDefAccessor(msg, output);
76   }
77 
78   output(
79       "#ifdef __cplusplus\n"
80       "}  /* extern \"C\" */\n"
81       "#endif\n"
82       "\n"
83       "#include \"upb/port/undef.inc\"\n"
84       "\n"
85       "#endif  /* $0_UPBDEFS_H_ */\n",
86       IncludeGuard(file.name()));
87 }
88 
WriteDefSource(upb::FileDefPtr file,const Options & options,Output & output)89 void WriteDefSource(upb::FileDefPtr file, const Options& options,
90                     Output& output) {
91   output(FileWarning(file.name()));
92 
93   output("#include \"upb/reflection/def.h\"\n");
94   output("#include \"$0\"\n", DefHeaderFilename(file));
95   output("#include \"$0\"\n", MiniTableHeaderFilename(file.name()));
96   output("\n");
97 
98   for (int i = 0; i < file.dependency_count(); i++) {
99     output("extern$1 _upb_DefPool_Init $0;\n",
100            DefInitSymbol(file.dependency(i)),
101            PadPrefix(options.dllexport_decl));
102   }
103 
104   upb::Arena arena;
105   google_protobuf_FileDescriptorProto* file_proto =
106       upb_FileDef_ToProto(file.ptr(), arena.ptr());
107   size_t serialized_size;
108   const char* serialized = google_protobuf_FileDescriptorProto_serialize(
109       file_proto, arena.ptr(), &serialized_size);
110   absl::string_view file_data(serialized, serialized_size);
111 
112   output("static const char descriptor[$0] = {", serialized_size);
113 
114   // C90 only guarantees that strings can be up to 509 characters, and some
115   // implementations have limits here (for example, MSVC only allows 64k:
116   // https://docs.microsoft.com/en-us/cpp/error-messages/compiler-errors-1/fatal-error-c1091.
117   // So we always emit an array instead of a string.
118   for (size_t i = 0; i < serialized_size;) {
119     for (size_t j = 0; j < 25 && i < serialized_size; ++i, ++j) {
120       output("'$0', ", absl::CEscape(file_data.substr(i, 1)));
121     }
122     output("\n");
123   }
124   output("};\n\n");
125 
126   output("static _upb_DefPool_Init *deps[$0] = {\n",
127          file.dependency_count() + 1);
128   for (int i = 0; i < file.dependency_count(); i++) {
129     output("  &$0,\n", DefInitSymbol(file.dependency(i)));
130   }
131   output("  NULL\n");
132   output("};\n");
133   output("\n");
134 
135   output("_upb_DefPool_Init $0 = {\n", DefInitSymbol(file));
136   output("  deps,\n");
137   output("  &$0,\n", MiniTableFileVarName(file.name()));
138   output("  \"$0\",\n", file.name());
139   output("  UPB_STRINGVIEW_INIT(descriptor, $0)\n", file_data.size());
140   output("};\n");
141 }
142 
GenerateFile(upb::FileDefPtr file,const Options & options,Plugin * plugin)143 void GenerateFile(upb::FileDefPtr file, const Options& options,
144                   Plugin* plugin) {
145   Output h_def_output;
146   WriteDefHeader(file, options, h_def_output);
147   plugin->AddOutputFile(DefHeaderFilename(file), h_def_output.output());
148 
149   Output c_def_output;
150   WriteDefSource(file, options, c_def_output);
151   plugin->AddOutputFile(DefSourceFilename(file), c_def_output.output());
152 }
153 
ParseOptions(Plugin * plugin,Options * options)154 bool ParseOptions(Plugin* plugin, Options* options) {
155   for (const auto& pair : ParseGeneratorParameter(plugin->parameter())) {
156     if (pair.first == "dllexport_decl") {
157       options->dllexport_decl = pair.second;
158     } else {
159       plugin->SetError(absl::Substitute("Unknown parameter: $0", pair.first));
160       return false;
161     }
162   }
163 
164   return true;
165 }
166 
167 }  // namespace
168 }  // namespace generator
169 }  // namespace upb
170 
main(int argc,char ** argv)171 int main(int argc, char** argv) {
172   upb::generator::Plugin plugin;
173   upb::generator::Options options;
174   if (!ParseOptions(&plugin, &options)) return 0;
175   plugin.GenerateFiles([&](upb::FileDefPtr file) {
176     upb::generator::GenerateFile(file, options, &plugin);
177   });
178   return 0;
179 }
180