protoc-gen-upbdefs.cc
Go to the documentation of this file.
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 
26 #include <memory>
27 
28 #include "google/protobuf/compiler/code_generator.h"
29 #include "google/protobuf/compiler/plugin.h"
30 #include "google/protobuf/descriptor.h"
31 #include "google/protobuf/descriptor.pb.h"
32 #include "upbc/common.h"
33 
34 namespace upbc {
35 namespace {
36 
37 namespace protoc = ::google::protobuf::compiler;
38 namespace protobuf = ::google::protobuf;
39 
40 std::string DefInitSymbol(const protobuf::FileDescriptor* file) {
41  return ToCIdent(file->name()) + "_upbdefinit";
42 }
43 
44 static std::string DefHeaderFilename(std::string proto_filename) {
45  return StripExtension(proto_filename) + ".upbdefs.h";
46 }
47 
48 static std::string DefSourceFilename(std::string proto_filename) {
49  return StripExtension(proto_filename) + ".upbdefs.c";
50 }
51 
52 void GenerateMessageDefAccessor(const protobuf::Descriptor* d, Output& output) {
53  output("UPB_INLINE const upb_MessageDef *$0_getmsgdef(upb_DefPool *s) {\n",
54  ToCIdent(d->full_name()));
55  output(" _upb_DefPool_LoadDefInit(s, &$0);\n", DefInitSymbol(d->file()));
56  output(" return upb_DefPool_FindMessageByName(s, \"$0\");\n",
57  d->full_name());
58  output("}\n");
59  output("\n");
60 
61  for (int i = 0; i < d->nested_type_count(); i++) {
62  GenerateMessageDefAccessor(d->nested_type(i), output);
63  }
64 }
65 
66 void WriteDefHeader(const protobuf::FileDescriptor* file, Output& output) {
68 
69  output(
70  "#ifndef $0_UPBDEFS_H_\n"
71  "#define $0_UPBDEFS_H_\n\n"
72  "#include \"upb/def.h\"\n"
73  "#include \"upb/port_def.inc\"\n"
74  "#ifdef __cplusplus\n"
75  "extern \"C\" {\n"
76  "#endif\n\n",
77  ToPreproc(file->name()));
78 
79  output("#include \"upb/def.h\"\n");
80  output("\n");
81  output("#include \"upb/port_def.inc\"\n");
82  output("\n");
83 
84  output("extern _upb_DefPool_Init $0;\n", DefInitSymbol(file));
85  output("\n");
86 
87  for (int i = 0; i < file->message_type_count(); i++) {
88  GenerateMessageDefAccessor(file->message_type(i), output);
89  }
90 
91  output(
92  "#ifdef __cplusplus\n"
93  "} /* extern \"C\" */\n"
94  "#endif\n"
95  "\n"
96  "#include \"upb/port_undef.inc\"\n"
97  "\n"
98  "#endif /* $0_UPBDEFS_H_ */\n",
99  ToPreproc(file->name()));
100 }
101 
102 void WriteDefSource(const protobuf::FileDescriptor* file, Output& output) {
104 
105  output("#include \"upb/def.h\"\n");
106  output("#include \"$0\"\n", DefHeaderFilename(file->name()));
107  output("#include \"$0\"\n", HeaderFilename(file));
108  output("\n");
109 
110  for (int i = 0; i < file->dependency_count(); i++) {
111  output("extern _upb_DefPool_Init $0;\n",
112  DefInitSymbol(file->dependency(i)));
113  }
114 
116  file->CopyTo(&file_proto);
118  file_proto.SerializeToString(&file_data);
119 
120  output("static const char descriptor[$0] = {", file_data.size());
121 
122  // C90 only guarantees that strings can be up to 509 characters, and some
123  // implementations have limits here (for example, MSVC only allows 64k:
124  // https://docs.microsoft.com/en-us/cpp/error-messages/compiler-errors-1/fatal-error-c1091.
125  // So we always emit an array instead of a string.
126  for (size_t i = 0; i < file_data.size();) {
127  for (size_t j = 0; j < 25 && i < file_data.size(); ++i, ++j) {
128  output("'$0', ", absl::CEscape(file_data.substr(i, 1)));
129  }
130  output("\n");
131  }
132  output("};\n\n");
133 
134  output("static _upb_DefPool_Init *deps[$0] = {\n",
135  file->dependency_count() + 1);
136  for (int i = 0; i < file->dependency_count(); i++) {
137  output(" &$0,\n", DefInitSymbol(file->dependency(i)));
138  }
139  output(" NULL\n");
140  output("};\n");
141  output("\n");
142 
143  output("_upb_DefPool_Init $0 = {\n", DefInitSymbol(file));
144  output(" deps,\n");
145  output(" &$0,\n", FileLayoutName(file));
146  output(" \"$0\",\n", file->name());
147  output(" UPB_STRINGVIEW_INIT(descriptor, $0)\n", file_data.size());
148  output("};\n");
149 }
150 
151 class Generator : public protoc::CodeGenerator {
152  ~Generator() override {}
153  bool Generate(const protobuf::FileDescriptor* file,
154  const std::string& parameter, protoc::GeneratorContext* context,
155  std::string* error) const override;
156  uint64_t GetSupportedFeatures() const override {
157  return FEATURE_PROTO3_OPTIONAL;
158  }
159 };
160 
161 bool Generator::Generate(const protobuf::FileDescriptor* file,
162  const std::string& parameter,
164  std::string* error) const {
165  std::vector<std::pair<std::string, std::string>> params;
167 
168  for (const auto& pair : params) {
169  *error = "Unknown parameter: " + pair.first;
170  return false;
171  }
172 
173  std::unique_ptr<protobuf::io::ZeroCopyOutputStream> h_output_stream(
174  context->Open(DefHeaderFilename(file->name())));
175  Output h_def_output(h_output_stream.get());
176  WriteDefHeader(file, h_def_output);
177 
178  std::unique_ptr<protobuf::io::ZeroCopyOutputStream> c_output_stream(
179  context->Open(DefSourceFilename(file->name())));
180  Output c_def_output(c_output_stream.get());
181  WriteDefSource(file, c_def_output);
182 
183  return true;
184 }
185 
186 } // namespace
187 } // namespace upbc
188 
189 int main(int argc, char** argv) {
190  std::unique_ptr<google::protobuf::compiler::CodeGenerator> generator(
191  new upbc::Generator());
192  return google::protobuf::compiler::PluginMain(argc, argv, generator.get());
193 }
upbc::EmitFileWarning
void EmitFileWarning(const protobuf::FileDescriptor *file, Output &output)
Definition: upb/upbc/common.cc:53
absl::str_format_internal::LengthMod::j
@ j
google::protobuf::compiler::CodeGenerator
Definition: bloaty/third_party/protobuf/src/google/protobuf/compiler/code_generator.h:68
testing::internal::string
::std::string string
Definition: bloaty/third_party/protobuf/third_party/googletest/googletest/include/gtest/internal/gtest-port.h:881
error
grpc_error_handle error
Definition: retry_filter.cc:499
upbc
Definition: upb/upbc/common.cc:30
file
Definition: bloaty/third_party/zlib/examples/gzappend.c:170
google::protobuf
Definition: bloaty/third_party/protobuf/benchmarks/util/data_proto2_to_proto3_util.h:12
main
int main(int argc, char **argv)
Definition: protoc-gen-upbdefs.cc:189
common.h
absl::CEscape
std::string CEscape(absl::string_view src)
Definition: abseil-cpp/absl/strings/escaping.cc:854
upbc::StripExtension
std::string StripExtension(absl::string_view fname)
Definition: upb/upbc/common.cc:37
google::protobuf::compiler::PluginMain
int PluginMain(int argc, char *argv[], const CodeGenerator *generator)
Definition: bloaty/third_party/protobuf/src/google/protobuf/compiler/plugin.cc:147
upbc::HeaderFilename
std::string HeaderFilename(const google::protobuf::FileDescriptor *file)
Definition: upb/upbc/common.cc:73
versiongenerate.file_data
string file_data
Definition: bloaty/third_party/protobuf/third_party/googletest/googletest/xcode/Scripts/versiongenerate.py:84
google::protobuf::compiler
Definition: bloaty/third_party/protobuf/benchmarks/util/protoc-gen-gogoproto.cc:19
gmock_output_test.output
output
Definition: bloaty/third_party/googletest/googlemock/test/gmock_output_test.py:175
uint64_t
unsigned __int64 uint64_t
Definition: stdint-msvc2008.h:90
d
static const fe d
Definition: curve25519_tables.h:19
upbc::ToPreproc
std::string ToPreproc(absl::string_view str)
Definition: upb/upbc/common.cc:49
upbc::FileLayoutName
std::string FileLayoutName(const google::protobuf::FileDescriptor *file)
Definition: upb/upbc/common.cc:69
absl::types_internal::Generator
GeneratorType< Fun > Generator(Fun fun, const char *description)
Definition: abseil-cpp/absl/types/internal/conformance_testing.h:96
google::protobuf::Descriptor
Definition: bloaty/third_party/protobuf/src/google/protobuf/descriptor.h:231
google::protobuf::compiler::ParseGeneratorParameter
void ParseGeneratorParameter(const std::string &text, std::vector< std::pair< std::string, std::string > > *output)
Definition: bloaty/third_party/protobuf/src/google/protobuf/compiler/code_generator.cc:101
google::protobuf::FileDescriptor
Definition: bloaty/third_party/protobuf/src/google/protobuf/descriptor.h:1320
grpc::protobuf::FileDescriptorProto
GRPC_CUSTOM_FILEDESCRIPTORPROTO FileDescriptorProto
Definition: include/grpcpp/impl/codegen/config_protobuf.h:86
context
grpc::ClientContext context
Definition: istio_echo_server_lib.cc:61
file::name
char * name
Definition: bloaty/third_party/zlib/examples/gzappend.c:176
google::protobuf::compiler::GeneratorContext
Definition: bloaty/third_party/protobuf/src/google/protobuf/compiler/code_generator.h:119
pair
std::pair< std::string, std::string > pair
Definition: abseil-cpp/absl/container/internal/raw_hash_set_benchmark.cc:78
upbc::ToCIdent
std::string ToCIdent(absl::string_view str)
Definition: upb/upbc/common.cc:45
i
uint64_t i
Definition: abseil-cpp/absl/container/btree_benchmark.cc:230


grpc
Author(s):
autogenerated on Fri May 16 2025 02:59:48