| 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) { |
| 67 |
EmitFileWarning(file, 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) { |
| 103 |
EmitFileWarning(file, 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 |
|
| 115 |
protobuf::FileDescriptorProto file_proto; |
| 116 |
file->CopyTo(&file_proto); |
| 117 |
std::string file_data; |
| 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, |
| 163 |
protoc::GeneratorContext* context, |
| 164 |
std::string* error) const { |
| 165 |
std::vector<std::pair<std::string, std::string>> params; |
| 166 |
google::protobuf::compiler::ParseGeneratorParameter(parameter, ¶ms); |
| 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 |
} |