1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214
|
// Protocol Buffers - Google's data interchange format
// Copyright 2023 Google LLC. All rights reserved.
// https://developers.google.com/protocol-buffers/
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
// * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above
// copyright notice, this list of conditions and the following disclaimer
// in the documentation and/or other materials provided with the
// distribution.
// * Neither the name of Google LLC nor the names of its
// contributors may be used to endorse or promote products derived from
// this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#ifndef UPB_UPB_GENERATOR_PLUGIN_H_
#define UPB_UPB_GENERATOR_PLUGIN_H_
#include <stdio.h>
#include <string>
#include <vector>
#ifdef _WIN32
#include <fcntl.h>
#include <io.h>
#endif
// begin:google_only
// #ifndef UPB_BOOTSTRAP_STAGE0
// #include "google/protobuf/descriptor.upb.h"
// #include "google/protobuf/compiler/plugin.upb.h"
// #else
// #include "google/protobuf/compiler/plugin.upb.h"
// #include "google/protobuf/descriptor.upb.h"
// #endif
// end:google_only
// begin:github_only
#include "google/protobuf/compiler/plugin.upb.h"
#include "google/protobuf/descriptor.upb.h"
// end:github_only
#include "absl/container/flat_hash_set.h"
#include "absl/log/absl_log.h"
#include "absl/strings/str_split.h"
#include "absl/strings/string_view.h"
#include "upb/reflection/def.hpp"
// Must be last.
#include "upb/port/def.inc"
namespace upb {
namespace generator {
inline std::vector<std::pair<std::string, std::string>> ParseGeneratorParameter(
const absl::string_view text) {
std::vector<std::pair<std::string, std::string>> ret;
for (absl::string_view sp : absl::StrSplit(text, ',', absl::SkipEmpty())) {
std::string::size_type equals_pos = sp.find_first_of('=');
std::pair<std::string, std::string> value;
if (equals_pos == std::string::npos) {
value.first = std::string(sp);
} else {
value.first = std::string(sp.substr(0, equals_pos));
value.second = std::string(sp.substr(equals_pos + 1));
}
ret.push_back(std::move(value));
}
return ret;
}
class Plugin {
public:
Plugin() { ReadRequest(); }
~Plugin() { WriteResponse(); }
absl::string_view parameter() const {
return ToStringView(
UPB_DESC(compiler_CodeGeneratorRequest_parameter)(request_));
}
template <class T>
void GenerateFilesRaw(T&& func) {
absl::flat_hash_set<absl::string_view> files_to_generate;
size_t size;
const upb_StringView* file_to_generate = UPB_DESC(
compiler_CodeGeneratorRequest_file_to_generate)(request_, &size);
for (size_t i = 0; i < size; i++) {
files_to_generate.insert(
{file_to_generate[i].data, file_to_generate[i].size});
}
const UPB_DESC(FileDescriptorProto)* const* files =
UPB_DESC(compiler_CodeGeneratorRequest_proto_file)(request_, &size);
for (size_t i = 0; i < size; i++) {
upb::Status status;
absl::string_view name =
ToStringView(UPB_DESC(FileDescriptorProto_name)(files[i]));
func(files[i], files_to_generate.contains(name));
}
}
template <class T>
void GenerateFiles(T&& func) {
GenerateFilesRaw(
[this, &func](const UPB_DESC(FileDescriptorProto) * file_proto,
bool generate) {
upb::Status status;
upb::FileDefPtr file = pool_.AddFile(file_proto, &status);
if (!file) {
absl::string_view name =
ToStringView(UPB_DESC(FileDescriptorProto_name)(file_proto));
ABSL_LOG(FATAL) << "Couldn't add file " << name
<< " to DefPool: " << status.error_message();
}
if (generate) func(file);
});
}
void SetError(absl::string_view error) {
char* data =
static_cast<char*>(upb_Arena_Malloc(arena_.ptr(), error.size()));
memcpy(data, error.data(), error.size());
UPB_DESC(compiler_CodeGeneratorResponse_set_error)
(response_, upb_StringView_FromDataAndSize(data, error.size()));
}
void AddOutputFile(absl::string_view filename, absl::string_view content) {
UPB_DESC(compiler_CodeGeneratorResponse_File)* file = UPB_DESC(
compiler_CodeGeneratorResponse_add_file)(response_, arena_.ptr());
UPB_DESC(compiler_CodeGeneratorResponse_File_set_name)
(file, StringDup(filename));
UPB_DESC(compiler_CodeGeneratorResponse_File_set_content)
(file, StringDup(content));
}
private:
upb::Arena arena_;
upb::DefPool pool_;
UPB_DESC(compiler_CodeGeneratorRequest) * request_;
UPB_DESC(compiler_CodeGeneratorResponse) * response_;
static absl::string_view ToStringView(upb_StringView sv) {
return absl::string_view(sv.data, sv.size);
}
upb_StringView StringDup(absl::string_view s) {
char* data =
reinterpret_cast<char*>(upb_Arena_Malloc(arena_.ptr(), s.size()));
memcpy(data, s.data(), s.size());
return upb_StringView_FromDataAndSize(data, s.size());
}
std::string ReadAllStdinBinary() {
std::string data;
#ifdef _WIN32
_setmode(_fileno(stdin), _O_BINARY);
_setmode(_fileno(stdout), _O_BINARY);
#endif
char buf[4096];
while (size_t len = fread(buf, 1, sizeof(buf), stdin)) {
data.append(buf, len);
}
return data;
}
void ReadRequest() {
std::string data = ReadAllStdinBinary();
request_ = UPB_DESC(compiler_CodeGeneratorRequest_parse)(
data.data(), data.size(), arena_.ptr());
if (!request_) {
ABSL_LOG(FATAL) << "Failed to parse CodeGeneratorRequest";
}
response_ = UPB_DESC(compiler_CodeGeneratorResponse_new)(arena_.ptr());
UPB_DESC(compiler_CodeGeneratorResponse_set_supported_features)
(response_,
UPB_DESC(compiler_CodeGeneratorResponse_FEATURE_PROTO3_OPTIONAL));
}
void WriteResponse() {
size_t size;
char* serialized = UPB_DESC(compiler_CodeGeneratorResponse_serialize)(
response_, arena_.ptr(), &size);
if (!serialized) {
ABSL_LOG(FATAL) << "Failed to serialize CodeGeneratorResponse";
}
if (fwrite(serialized, 1, size, stdout) != size) {
ABSL_LOG(FATAL) << "Failed to write response to stdout";
}
}
};
} // namespace generator
} // namespace upb
#include "upb/port/undef.inc"
#endif // UPB_UPB_GENERATOR_PLUGIN_H_
|