YugabyteDB (2.13.1.0-b60, 21121d69985fbf76aa6958d8f04a9bfa936293b5)

Coverage Report

Created: 2022-03-22 16:43

/Users/deen/code/yugabyte-db/src/yb/gen_yrpc/protoc-gen-yrpc.cc
Line
Count
Source (jump to first uncovered line)
1
// Licensed to the Apache Software Foundation (ASF) under one
2
// or more contributor license agreements.  See the NOTICE file
3
// distributed with this work for additional information
4
// regarding copyright ownership.  The ASF licenses this file
5
// to you under the Apache License, Version 2.0 (the
6
// "License"); you may not use this file except in compliance
7
// with the License.  You may obtain a copy of the License at
8
//
9
//   http://www.apache.org/licenses/LICENSE-2.0
10
//
11
// Unless required by applicable law or agreed to in writing,
12
// software distributed under the License is distributed on an
13
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14
// KIND, either express or implied.  See the License for the
15
// specific language governing permissions and limitations
16
// under the License.
17
//
18
// The following only applies to changes made to this file as part of YugaByte development.
19
//
20
// Portions Copyright (c) YugaByte, Inc.
21
//
22
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
23
// in compliance with the License.  You may obtain a copy of the License at
24
//
25
// http://www.apache.org/licenses/LICENSE-2.0
26
//
27
// Unless required by applicable law or agreed to in writing, software distributed under the License
28
// is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
29
// or implied.  See the License for the specific language governing permissions and limitations
30
// under the License.
31
//
32
33
////////////////////////////////////////////////////////////////////////////////
34
// Example usage:
35
// protoc --plugin=protoc-gen-yrpc --yrpc_out . --proto_path . <file>.proto
36
////////////////////////////////////////////////////////////////////////////////
37
38
#include <functional>
39
40
#include <google/protobuf/compiler/code_generator.h>
41
#include <google/protobuf/compiler/plugin.h>
42
#include <google/protobuf/descriptor.h>
43
#include <google/protobuf/io/printer.h>
44
#include <google/protobuf/io/zero_copy_stream.h>
45
46
#include "yb/gen_yrpc/printer.h"
47
#include "yb/gen_yrpc/forward_generator.h"
48
#include "yb/gen_yrpc/messages_generator.h"
49
#include "yb/gen_yrpc/proxy_generator.h"
50
#include "yb/gen_yrpc/service_generator.h"
51
#include "yb/gen_yrpc/substitutions.h"
52
53
using namespace std::placeholders;
54
55
namespace yb {
56
namespace gen_yrpc {
57
58
template <class T>
59
struct HasMemberFunction_Source {
60
  typedef int Yes;
61
  typedef struct { Yes array[2]; } No;
62
  typedef typename std::remove_cv<typename std::remove_reference<T>::type>::type CleanedT;
63
64
  template <class U>
65
  static auto Test(YBPrinter* printer, const google::protobuf::FileDescriptor* file, U* u)
66
      -> decltype(u->Source(*printer, file), Yes(0)) {}
67
  static No Test(...) {}
68
69
  static constexpr bool value =
70
      sizeof(Test(nullptr, nullptr, static_cast<CleanedT*>(nullptr))) == sizeof(Yes);
71
};
72
73
class CodeGenerator : public google::protobuf::compiler::CodeGenerator {
74
 public:
75
0
  CodeGenerator() { }
76
77
0
  ~CodeGenerator() { }
78
79
  bool Generate(const google::protobuf::FileDescriptor *file,
80
        const std::string& parameter,
81
        google::protobuf::compiler::GeneratorContext *gen_context,
82
0
        std::string *error) const override {
83
84
0
    std::vector<std::pair<std::string, std::string>> params_temp;
85
0
    google::protobuf::compiler::ParseGeneratorParameter(parameter, &params_temp);
86
0
    std::map<std::string, std::string> params;
87
0
    for (const auto& p : params_temp) {
88
0
      params.emplace(p.first, p.second);
89
0
    }
90
91
0
    FileSubstitutions name_info(file);
92
93
0
    SubstitutionContext subs;
94
0
    subs.Push(name_info.Create());
95
96
0
    Generate<ForwardGenerator>(file, gen_context, &subs, name_info.forward());
97
98
0
    if (file->service_count() != 0) {
99
0
      Generate<ServiceGenerator>(file, gen_context, &subs, name_info.service());
100
0
      Generate<ProxyGenerator>(file, gen_context, &subs, name_info.proxy());
101
0
    }
102
103
0
    if (params.count("messages")) {
104
0
      Generate<MessagesGenerator>(
105
0
          file, gen_context, &subs, name_info.messages());
106
0
    }
107
108
0
    return true;
109
0
  }
110
111
 private:
112
  template <class Generator>
113
  void Generate(
114
      const google::protobuf::FileDescriptor *file,
115
      google::protobuf::compiler::GeneratorContext *gen_context,
116
0
      SubstitutionContext *subs, const std::string& fname) const {
117
0
    Generator generator;
118
0
    DoGenerate(
119
0
        file, gen_context, subs, fname + ".h",
120
0
        std::bind(&Generator::Header, &generator, _1, _2));
121
122
0
    GenerateSource(&generator, file, gen_context, subs, fname + ".cc");
123
0
  }
Unexecuted instantiation: void yb::gen_yrpc::CodeGenerator::Generate<yb::gen_yrpc::ForwardGenerator>(google::protobuf::FileDescriptor const*, google::protobuf::compiler::GeneratorContext*, yb::gen_yrpc::SubstitutionContext*, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&) const
Unexecuted instantiation: void yb::gen_yrpc::CodeGenerator::Generate<yb::gen_yrpc::ServiceGenerator>(google::protobuf::FileDescriptor const*, google::protobuf::compiler::GeneratorContext*, yb::gen_yrpc::SubstitutionContext*, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&) const
Unexecuted instantiation: void yb::gen_yrpc::CodeGenerator::Generate<yb::gen_yrpc::ProxyGenerator>(google::protobuf::FileDescriptor const*, google::protobuf::compiler::GeneratorContext*, yb::gen_yrpc::SubstitutionContext*, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&) const
Unexecuted instantiation: void yb::gen_yrpc::CodeGenerator::Generate<yb::gen_yrpc::MessagesGenerator>(google::protobuf::FileDescriptor const*, google::protobuf::compiler::GeneratorContext*, yb::gen_yrpc::SubstitutionContext*, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&) const
124
125
  template <class Generator>
126
  typename std::enable_if<HasMemberFunction_Source<Generator>::value, void>::type GenerateSource(
127
      Generator* generator,
128
      const google::protobuf::FileDescriptor *file,
129
      google::protobuf::compiler::GeneratorContext *gen_context,
130
0
      SubstitutionContext *subs, const std::string& fname) const {
131
0
    DoGenerate(
132
0
        file, gen_context, subs, fname,
133
0
        std::bind(&Generator::Source, generator, _1, _2));
134
0
  }
Unexecuted instantiation: std::__1::enable_if<HasMemberFunction_Source<yb::gen_yrpc::ServiceGenerator>::value, void>::type yb::gen_yrpc::CodeGenerator::GenerateSource<yb::gen_yrpc::ServiceGenerator>(yb::gen_yrpc::ServiceGenerator*, google::protobuf::FileDescriptor const*, google::protobuf::compiler::GeneratorContext*, yb::gen_yrpc::SubstitutionContext*, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&) const
Unexecuted instantiation: std::__1::enable_if<HasMemberFunction_Source<yb::gen_yrpc::ProxyGenerator>::value, void>::type yb::gen_yrpc::CodeGenerator::GenerateSource<yb::gen_yrpc::ProxyGenerator>(yb::gen_yrpc::ProxyGenerator*, google::protobuf::FileDescriptor const*, google::protobuf::compiler::GeneratorContext*, yb::gen_yrpc::SubstitutionContext*, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&) const
Unexecuted instantiation: std::__1::enable_if<HasMemberFunction_Source<yb::gen_yrpc::MessagesGenerator>::value, void>::type yb::gen_yrpc::CodeGenerator::GenerateSource<yb::gen_yrpc::MessagesGenerator>(yb::gen_yrpc::MessagesGenerator*, google::protobuf::FileDescriptor const*, google::protobuf::compiler::GeneratorContext*, yb::gen_yrpc::SubstitutionContext*, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&) const
135
136
  template <class Generator, class... Args>
137
  typename std::enable_if<!HasMemberFunction_Source<Generator>::value, void>::type GenerateSource(
138
0
      Generator* generator, Args&&... args) const {}
139
140
  template <class F>
141
  void DoGenerate(
142
      const google::protobuf::FileDescriptor *file,
143
      google::protobuf::compiler::GeneratorContext *gen_context,
144
0
      SubstitutionContext *subs, const std::string& fname, const F& generator) const {
145
0
    std::unique_ptr<google::protobuf::io::ZeroCopyOutputStream> output(gen_context->Open(fname));
146
0
    google::protobuf::io::Printer printer(output.get(), '$');
147
0
    YBPrinter yb_printer(&printer, subs);
148
0
    generator(yb_printer, file);
149
0
  }
Unexecuted instantiation: void yb::gen_yrpc::CodeGenerator::DoGenerate<std::__1::__bind<void (yb::gen_yrpc::ForwardGenerator::*)(yb::gen_yrpc::YBPrinter, google::protobuf::FileDescriptor const*), yb::gen_yrpc::ForwardGenerator*, std::__1::placeholders::__ph<1> const&, std::__1::placeholders::__ph<2> const&> >(google::protobuf::FileDescriptor const*, google::protobuf::compiler::GeneratorContext*, yb::gen_yrpc::SubstitutionContext*, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, std::__1::__bind<void (yb::gen_yrpc::ForwardGenerator::*)(yb::gen_yrpc::YBPrinter, google::protobuf::FileDescriptor const*), yb::gen_yrpc::ForwardGenerator*, std::__1::placeholders::__ph<1> const&, std::__1::placeholders::__ph<2> const&> const&) const
Unexecuted instantiation: void yb::gen_yrpc::CodeGenerator::DoGenerate<std::__1::__bind<void (yb::gen_yrpc::ServiceGenerator::*)(yb::gen_yrpc::YBPrinter, google::protobuf::FileDescriptor const*), yb::gen_yrpc::ServiceGenerator*, std::__1::placeholders::__ph<1> const&, std::__1::placeholders::__ph<2> const&> >(google::protobuf::FileDescriptor const*, google::protobuf::compiler::GeneratorContext*, yb::gen_yrpc::SubstitutionContext*, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, std::__1::__bind<void (yb::gen_yrpc::ServiceGenerator::*)(yb::gen_yrpc::YBPrinter, google::protobuf::FileDescriptor const*), yb::gen_yrpc::ServiceGenerator*, std::__1::placeholders::__ph<1> const&, std::__1::placeholders::__ph<2> const&> const&) const
Unexecuted instantiation: void yb::gen_yrpc::CodeGenerator::DoGenerate<std::__1::__bind<void (yb::gen_yrpc::ServiceGenerator::*)(yb::gen_yrpc::YBPrinter, google::protobuf::FileDescriptor const*), yb::gen_yrpc::ServiceGenerator*&, std::__1::placeholders::__ph<1> const&, std::__1::placeholders::__ph<2> const&> >(google::protobuf::FileDescriptor const*, google::protobuf::compiler::GeneratorContext*, yb::gen_yrpc::SubstitutionContext*, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, std::__1::__bind<void (yb::gen_yrpc::ServiceGenerator::*)(yb::gen_yrpc::YBPrinter, google::protobuf::FileDescriptor const*), yb::gen_yrpc::ServiceGenerator*&, std::__1::placeholders::__ph<1> const&, std::__1::placeholders::__ph<2> const&> const&) const
Unexecuted instantiation: void yb::gen_yrpc::CodeGenerator::DoGenerate<std::__1::__bind<void (yb::gen_yrpc::ProxyGenerator::*)(yb::gen_yrpc::YBPrinter, google::protobuf::FileDescriptor const*), yb::gen_yrpc::ProxyGenerator*, std::__1::placeholders::__ph<1> const&, std::__1::placeholders::__ph<2> const&> >(google::protobuf::FileDescriptor const*, google::protobuf::compiler::GeneratorContext*, yb::gen_yrpc::SubstitutionContext*, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, std::__1::__bind<void (yb::gen_yrpc::ProxyGenerator::*)(yb::gen_yrpc::YBPrinter, google::protobuf::FileDescriptor const*), yb::gen_yrpc::ProxyGenerator*, std::__1::placeholders::__ph<1> const&, std::__1::placeholders::__ph<2> const&> const&) const
Unexecuted instantiation: void yb::gen_yrpc::CodeGenerator::DoGenerate<std::__1::__bind<void (yb::gen_yrpc::ProxyGenerator::*)(yb::gen_yrpc::YBPrinter, google::protobuf::FileDescriptor const*), yb::gen_yrpc::ProxyGenerator*&, std::__1::placeholders::__ph<1> const&, std::__1::placeholders::__ph<2> const&> >(google::protobuf::FileDescriptor const*, google::protobuf::compiler::GeneratorContext*, yb::gen_yrpc::SubstitutionContext*, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, std::__1::__bind<void (yb::gen_yrpc::ProxyGenerator::*)(yb::gen_yrpc::YBPrinter, google::protobuf::FileDescriptor const*), yb::gen_yrpc::ProxyGenerator*&, std::__1::placeholders::__ph<1> const&, std::__1::placeholders::__ph<2> const&> const&) const
Unexecuted instantiation: void yb::gen_yrpc::CodeGenerator::DoGenerate<std::__1::__bind<void (yb::gen_yrpc::MessagesGenerator::*)(yb::gen_yrpc::YBPrinter, google::protobuf::FileDescriptor const*), yb::gen_yrpc::MessagesGenerator*, std::__1::placeholders::__ph<1> const&, std::__1::placeholders::__ph<2> const&> >(google::protobuf::FileDescriptor const*, google::protobuf::compiler::GeneratorContext*, yb::gen_yrpc::SubstitutionContext*, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, std::__1::__bind<void (yb::gen_yrpc::MessagesGenerator::*)(yb::gen_yrpc::YBPrinter, google::protobuf::FileDescriptor const*), yb::gen_yrpc::MessagesGenerator*, std::__1::placeholders::__ph<1> const&, std::__1::placeholders::__ph<2> const&> const&) const
Unexecuted instantiation: void yb::gen_yrpc::CodeGenerator::DoGenerate<std::__1::__bind<void (yb::gen_yrpc::MessagesGenerator::*)(yb::gen_yrpc::YBPrinter, google::protobuf::FileDescriptor const*), yb::gen_yrpc::MessagesGenerator*&, std::__1::placeholders::__ph<1> const&, std::__1::placeholders::__ph<2> const&> >(google::protobuf::FileDescriptor const*, google::protobuf::compiler::GeneratorContext*, yb::gen_yrpc::SubstitutionContext*, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, std::__1::__bind<void (yb::gen_yrpc::MessagesGenerator::*)(yb::gen_yrpc::YBPrinter, google::protobuf::FileDescriptor const*), yb::gen_yrpc::MessagesGenerator*&, std::__1::placeholders::__ph<1> const&, std::__1::placeholders::__ph<2> const&> const&) const
150
};
151
152
} // namespace gen_yrpc
153
} // namespace yb
154
155
18.6k
int main(int argc, char *argv[]) {
156
18.6k
  yb::gen_yrpc::CodeGenerator generator;
157
18.6k
  return google::protobuf::compiler::PluginMain(argc, argv, &generator);
158
18.6k
}