upbc.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 <google/protobuf/compiler/plugin.h>
27 #include <google/protobuf/io/printer.h>
28 
29 #include "absl/strings/str_replace.h"
30 #include "absl/strings/string_view.h"
31 #include "absl/strings/substitute.h"
32 #include "google/protobuf/compiler/code_generator.h"
33 #include "google/protobuf/descriptor.h"
34 #include "google/protobuf/descriptor.pb.h"
35 
36 namespace protoc = ::google::protobuf::compiler;
37 namespace protobuf = ::google::protobuf;
38 
41  const std::string& parameter, protoc::GeneratorContext* context,
42  std::string* error) const override;
43 };
44 
46  size_t lastdot = fname.find_last_of('.');
47  if (lastdot == std::string::npos) {
48  return std::string(fname);
49  }
50  return std::string(fname.substr(0, lastdot));
51 }
52 
54  return StripExtension(file->name()) + "_pb.lua";
55 }
56 
58  std::string ret = StripExtension(file->name()) + "_pb";
59  return absl::StrReplaceAll(ret, {{"/", "."}});
60 }
61 
62 static void PrintHexDigit(char digit, protobuf::io::Printer* printer) {
63  char text;
64  if (digit < 10) {
65  text = '0' + digit;
66  } else {
67  text = 'A' + (digit - 10);
68  }
69  printer->WriteRaw(&text, 1);
70 }
71 
72 static void PrintString(int max_cols, absl::string_view* str,
73  protobuf::io::Printer* printer) {
74  printer->Print("\'");
75  while (max_cols > 0 && !str->empty()) {
76  char ch = (*str)[0];
77  if (ch == '\\') {
78  printer->PrintRaw("\\\\");
79  max_cols--;
80  } else if (ch == '\'') {
81  printer->PrintRaw("\\'");
82  max_cols--;
83  } else if (isprint(ch)) {
84  printer->WriteRaw(&ch, 1);
85  max_cols--;
86  } else {
87  unsigned char byte = ch;
88  printer->PrintRaw("\\x");
89  PrintHexDigit(byte >> 4, printer);
90  PrintHexDigit(byte & 15, printer);
91  max_cols -= 4;
92  }
93  str->remove_prefix(1);
94  }
95  printer->Print("\'");
96 }
97 
99  const std::string& /* parameter */,
101  std::string* /* error */) const {
104  protobuf::io::Printer printer(out, '$');
105 
106  for (int i = 0; i < file->dependency_count(); i++) {
107  const protobuf::FileDescriptor* dep = file->dependency(i);
108  printer.Print("require('$name$')\n", "name", ModuleName(dep));
109  }
110 
111  printer.Print("local upb = require('upb')\n");
112 
114  file->CopyTo(&file_proto);
116  file_proto.SerializeToString(&file_data);
117 
118  printer.Print("local descriptor = table.concat({\n");
120  while (!data.empty()) {
121  printer.Print(" ");
122  PrintString(72, &data, &printer);
123  printer.Print(",\n");
124  }
125  printer.Print("})\n");
126 
127  printer.Print("return upb._generated_module(descriptor)\n");
128 
129  return true;
130 }
131 
132 int main(int argc, char** argv) {
133  LuaGenerator generator;
134  return google::protobuf::compiler::PluginMain(argc, argv, &generator);
135 }
xds_interop_client.str
str
Definition: xds_interop_client.py:487
filename
const char * filename
Definition: bloaty/third_party/zlib/contrib/minizip/ioapi.h:135
gen_build_yaml.out
dictionary out
Definition: src/benchmark/gen_build_yaml.py:24
Filename
static std::string Filename(const protobuf::FileDescriptor *file)
Definition: upbc.cc:53
google::protobuf::compiler::CodeGenerator
Definition: bloaty/third_party/protobuf/src/google/protobuf/compiler/code_generator.h:68
absl::string_view
Definition: abseil-cpp/absl/strings/string_view.h:167
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
file
Definition: bloaty/third_party/zlib/examples/gzappend.c:170
grpc::protobuf::io::Printer
GRPC_CUSTOM_PRINTER Printer
Definition: src/compiler/config.h:54
google::protobuf
Definition: bloaty/third_party/protobuf/benchmarks/util/data_proto2_to_proto3_util.h:12
grpc::protobuf::io::ZeroCopyOutputStream
GRPC_CUSTOM_ZEROCOPYOUTPUTSTREAM ZeroCopyOutputStream
Definition: include/grpcpp/impl/codegen/config_protobuf.h:100
LuaGenerator
Definition: upbc.cc:39
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
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
StripExtension
static std::string StripExtension(absl::string_view fname)
Definition: upbc.cc:45
gen_server_registered_method_bad_client_test_body.text
def text
Definition: gen_server_registered_method_bad_client_test_body.py:50
main
int main(int argc, char **argv)
Definition: upbc.cc:132
google::protobuf::isprint
bool isprint(char c)
Definition: bloaty/third_party/protobuf/src/google/protobuf/stubs/strutil.cc:79
ModuleName
static std::string ModuleName(const protobuf::FileDescriptor *file)
Definition: upbc.cc:57
PrintString
static void PrintString(int max_cols, absl::string_view *str, protobuf::io::Printer *printer)
Definition: upbc.cc:72
LuaGenerator::Generate
bool Generate(const protobuf::FileDescriptor *file, const std::string &parameter, protoc::GeneratorContext *context, std::string *error) const override
Definition: upbc.cc:98
data
char data[kBufferLength]
Definition: abseil-cpp/absl/strings/internal/str_format/float_conversion.cc:1006
ret
UniquePtr< SSL_SESSION > ret
Definition: ssl_x509.cc:1029
absl::StrReplaceAll
std::string StrReplaceAll(absl::string_view s, strings_internal::FixedMapping replacements)
Definition: abseil-cpp/absl/strings/str_replace.cc:71
absl::string_view::find_last_of
size_type find_last_of(string_view s, size_type pos=npos) const noexcept
Definition: abseil-cpp/absl/strings/string_view.cc:165
google::protobuf::FileDescriptor
Definition: bloaty/third_party/protobuf/src/google/protobuf/descriptor.h:1320
PrintHexDigit
static void PrintHexDigit(char digit, protobuf::io::Printer *printer)
Definition: upbc.cc:62
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
ch
char ch
Definition: bloaty/third_party/googletest/googlemock/test/gmock-matchers_test.cc:3621
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
absl::string_view::substr
constexpr string_view substr(size_type pos=0, size_type n=npos) const
Definition: abseil-cpp/absl/strings/string_view.h:399
fix_build_deps.dep
string dep
Definition: fix_build_deps.py:439
i
uint64_t i
Definition: abseil-cpp/absl/container/btree_benchmark.cc:230


grpc
Author(s):
autogenerated on Thu Mar 13 2025 03:01:47