YugabyteDB (2.13.1.0-b60, 21121d69985fbf76aa6958d8f04a9bfa936293b5)

Coverage Report

Created: 2022-03-22 16:43

/Users/deen/code/yugabyte-db/src/yb/util/protoc-gen-insertions.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
// Simple protoc plugin which inserts some code at the top of each generated protobuf.
33
// Currently, this just adds an include of protobuf-annotations.h, a file which hooks up
34
// the protobuf concurrency annotations to our TSAN annotations.
35
#include <google/protobuf/compiler/code_generator.h>
36
#include <google/protobuf/compiler/plugin.h>
37
#include <google/protobuf/descriptor.h>
38
#include <google/protobuf/io/printer.h>
39
#include <google/protobuf/io/zero_copy_stream.h>
40
41
#include "yb/gutil/strings/strip.h"
42
#include "yb/gutil/strings/substitute.h"
43
44
using google::protobuf::io::ZeroCopyOutputStream;
45
using google::protobuf::io::Printer;
46
47
namespace yb {
48
49
static const char* const kIncludeToInsert = "#include \"yb/util/protobuf-annotations.h\"\n";
50
static const char* const kProtoExtension = ".proto";
51
52
class InsertAnnotations : public ::google::protobuf::compiler::CodeGenerator {
53
  virtual bool Generate(const google::protobuf::FileDescriptor *file,
54
                        const std::string &/*param*/,
55
                        google::protobuf::compiler::GeneratorContext *gen_context,
56
14
                        std::string *error) const override {
57
58
    // Determine the file name we will substitute into.
59
14
    string path_no_extension;
60
14
    if (!TryStripSuffixString(file->name(), kProtoExtension, &path_no_extension)) {
61
0
      *error = strings::Substitute("file name $0 did not end in $1", file->name(), kProtoExtension);
62
0
      return false;
63
0
    }
64
14
    string pb_file = path_no_extension + ".pb.cc";
65
66
    // Actually insert the new #include
67
14
    std::unique_ptr<ZeroCopyOutputStream> inserter(gen_context->OpenForInsert(pb_file, "includes"));
68
14
    Printer printer(inserter.get(), '$');
69
14
    printer.Print(kIncludeToInsert);
70
71
14
    if (printer.failed()) {
72
0
      *error = "Failed to print to output file";
73
0
      return false;
74
0
    }
75
76
14
    return true;
77
14
  }
78
};
79
80
} // namespace yb
81
82
18.6k
int main(int argc, char *argv[]) {
83
18.6k
  yb::InsertAnnotations generator;
84
18.6k
  return google::protobuf::compiler::PluginMain(argc, argv, &generator);
85
18.6k
}