YugabyteDB (2.13.0.0-b42, bfc6a6643e7399ac8a0e81d06a3ee6d6571b33ab)

Coverage Report

Created: 2022-03-09 17:30

/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: _ZNK2yb8gen_yrpc13CodeGenerator8GenerateINS0_16ForwardGeneratorEEEvPKN6google8protobuf14FileDescriptorEPNS5_8compiler16GeneratorContextEPNS0_19SubstitutionContextERKNSt3__112basic_stringIcNSE_11char_traitsIcEENSE_9allocatorIcEEEE
Unexecuted instantiation: _ZNK2yb8gen_yrpc13CodeGenerator8GenerateINS0_16ServiceGeneratorEEEvPKN6google8protobuf14FileDescriptorEPNS5_8compiler16GeneratorContextEPNS0_19SubstitutionContextERKNSt3__112basic_stringIcNSE_11char_traitsIcEENSE_9allocatorIcEEEE
Unexecuted instantiation: _ZNK2yb8gen_yrpc13CodeGenerator8GenerateINS0_14ProxyGeneratorEEEvPKN6google8protobuf14FileDescriptorEPNS5_8compiler16GeneratorContextEPNS0_19SubstitutionContextERKNSt3__112basic_stringIcNSE_11char_traitsIcEENSE_9allocatorIcEEEE
Unexecuted instantiation: _ZNK2yb8gen_yrpc13CodeGenerator8GenerateINS0_17MessagesGeneratorEEEvPKN6google8protobuf14FileDescriptorEPNS5_8compiler16GeneratorContextEPNS0_19SubstitutionContextERKNSt3__112basic_stringIcNSE_11char_traitsIcEENSE_9allocatorIcEEEE
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: _ZNK2yb8gen_yrpc13CodeGenerator14GenerateSourceINS0_16ServiceGeneratorEEENSt3__19enable_ifIXsr24HasMemberFunction_SourceIT_EE5valueEvE4typeEPS6_PKN6google8protobuf14FileDescriptorEPNSB_8compiler16GeneratorContextEPNS0_19SubstitutionContextERKNS4_12basic_stringIcNS4_11char_traitsIcEENS4_9allocatorIcEEEE
Unexecuted instantiation: _ZNK2yb8gen_yrpc13CodeGenerator14GenerateSourceINS0_14ProxyGeneratorEEENSt3__19enable_ifIXsr24HasMemberFunction_SourceIT_EE5valueEvE4typeEPS6_PKN6google8protobuf14FileDescriptorEPNSB_8compiler16GeneratorContextEPNS0_19SubstitutionContextERKNS4_12basic_stringIcNS4_11char_traitsIcEENS4_9allocatorIcEEEE
Unexecuted instantiation: _ZNK2yb8gen_yrpc13CodeGenerator14GenerateSourceINS0_17MessagesGeneratorEEENSt3__19enable_ifIXsr24HasMemberFunction_SourceIT_EE5valueEvE4typeEPS6_PKN6google8protobuf14FileDescriptorEPNSB_8compiler16GeneratorContextEPNS0_19SubstitutionContextERKNS4_12basic_stringIcNS4_11char_traitsIcEENS4_9allocatorIcEEEE
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: _ZNK2yb8gen_yrpc13CodeGenerator10DoGenerateINSt3__16__bindIMNS0_16ForwardGeneratorEFvNS0_9YBPrinterEPKN6google8protobuf14FileDescriptorEEJPS5_RKNS3_12placeholders4__phILi1EEERKNSG_ILi2EEEEEEEEvSB_PNS8_8compiler16GeneratorContextEPNS0_19SubstitutionContextERKNS3_12basic_stringIcNS3_11char_traitsIcEENS3_9allocatorIcEEEERKT_
Unexecuted instantiation: _ZNK2yb8gen_yrpc13CodeGenerator10DoGenerateINSt3__16__bindIMNS0_16ServiceGeneratorEFvNS0_9YBPrinterEPKN6google8protobuf14FileDescriptorEEJPS5_RKNS3_12placeholders4__phILi1EEERKNSG_ILi2EEEEEEEEvSB_PNS8_8compiler16GeneratorContextEPNS0_19SubstitutionContextERKNS3_12basic_stringIcNS3_11char_traitsIcEENS3_9allocatorIcEEEERKT_
Unexecuted instantiation: _ZNK2yb8gen_yrpc13CodeGenerator10DoGenerateINSt3__16__bindIMNS0_16ServiceGeneratorEFvNS0_9YBPrinterEPKN6google8protobuf14FileDescriptorEEJRPS5_RKNS3_12placeholders4__phILi1EEERKNSH_ILi2EEEEEEEEvSB_PNS8_8compiler16GeneratorContextEPNS0_19SubstitutionContextERKNS3_12basic_stringIcNS3_11char_traitsIcEENS3_9allocatorIcEEEERKT_
Unexecuted instantiation: _ZNK2yb8gen_yrpc13CodeGenerator10DoGenerateINSt3__16__bindIMNS0_14ProxyGeneratorEFvNS0_9YBPrinterEPKN6google8protobuf14FileDescriptorEEJPS5_RKNS3_12placeholders4__phILi1EEERKNSG_ILi2EEEEEEEEvSB_PNS8_8compiler16GeneratorContextEPNS0_19SubstitutionContextERKNS3_12basic_stringIcNS3_11char_traitsIcEENS3_9allocatorIcEEEERKT_
Unexecuted instantiation: _ZNK2yb8gen_yrpc13CodeGenerator10DoGenerateINSt3__16__bindIMNS0_14ProxyGeneratorEFvNS0_9YBPrinterEPKN6google8protobuf14FileDescriptorEEJRPS5_RKNS3_12placeholders4__phILi1EEERKNSH_ILi2EEEEEEEEvSB_PNS8_8compiler16GeneratorContextEPNS0_19SubstitutionContextERKNS3_12basic_stringIcNS3_11char_traitsIcEENS3_9allocatorIcEEEERKT_
Unexecuted instantiation: _ZNK2yb8gen_yrpc13CodeGenerator10DoGenerateINSt3__16__bindIMNS0_17MessagesGeneratorEFvNS0_9YBPrinterEPKN6google8protobuf14FileDescriptorEEJPS5_RKNS3_12placeholders4__phILi1EEERKNSG_ILi2EEEEEEEEvSB_PNS8_8compiler16GeneratorContextEPNS0_19SubstitutionContextERKNS3_12basic_stringIcNS3_11char_traitsIcEENS3_9allocatorIcEEEERKT_
Unexecuted instantiation: _ZNK2yb8gen_yrpc13CodeGenerator10DoGenerateINSt3__16__bindIMNS0_17MessagesGeneratorEFvNS0_9YBPrinterEPKN6google8protobuf14FileDescriptorEEJRPS5_RKNS3_12placeholders4__phILi1EEERKNSH_ILi2EEEEEEEEvSB_PNS8_8compiler16GeneratorContextEPNS0_19SubstitutionContextERKNS3_12basic_stringIcNS3_11char_traitsIcEENS3_9allocatorIcEEEERKT_
150
};
151
152
} // namespace gen_yrpc
153
} // namespace yb
154
155
13.2k
int main(int argc, char *argv[]) {
156
13.2k
  yb::gen_yrpc::CodeGenerator generator;
157
13.2k
  return google::protobuf::compiler::PluginMain(argc, argv, &generator);
158
13.2k
}