OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #include "proto_zero_generator.h" | |
6 | |
7 #include <memory> | |
8 #include <string> | |
9 | |
10 #include "third_party/protobuf/src/google/protobuf/descriptor.h" | |
11 #include "third_party/protobuf/src/google/protobuf/io/printer.h" | |
12 #include "third_party/protobuf/src/google/protobuf/io/zero_copy_stream.h" | |
13 #include "third_party/protobuf/src/google/protobuf/stubs/strutil.h" | |
14 | |
15 namespace tracing { | |
16 namespace proto { | |
17 | |
18 using google::protobuf::FileDescriptor; | |
19 using google::protobuf::StripSuffixString; | |
20 using google::protobuf::compiler::GeneratorContext; | |
21 using google::protobuf::io::Printer; | |
22 using google::protobuf::io::ZeroCopyOutputStream; | |
23 | |
24 namespace { | |
25 | |
26 class GeneratorJob { | |
27 public: | |
28 GeneratorJob(const FileDescriptor *file, | |
29 Printer* stub_h_printer, | |
30 Printer* stub_cc_printer) | |
31 : file_(file), | |
32 stub_h_(stub_h_printer), | |
33 stub_cc_(stub_cc_printer) {} | |
34 | |
35 bool GenerateStubs() { | |
36 stub_h_->Print( | |
37 "// Autogenerated. DO NOT EDIT.\n" | |
38 "// Generated by: //components/tracing/proto_zero_plugin.\n\n" | |
39 "// Package: $package$\n", | |
40 "package", file_->package()); | |
41 stub_cc_->Print( | |
42 "// Autogenerated. DO NOT EDIT.\n" | |
43 "// Generated by: //components/tracing/proto_zero_plugin.\n\n" | |
44 "// This file intentionally left blank.\n"); | |
45 // TODO(kraynov) Implement in the next CL (crbug.com/608721). | |
46 return true; | |
47 } | |
48 | |
49 // If generator fails to produce stubs for a particular proto definitions | |
50 // it finishes with undefined output and writes the first error occured. | |
51 const std::string& GetFirstError() const { | |
52 return error_; | |
53 } | |
54 | |
55 private: | |
56 // Only the first error will be recorded. | |
57 void Abort(const std::string& reason) { | |
58 if (error_.empty()) { | |
59 error_ = reason; | |
60 } | |
61 } | |
62 | |
63 const FileDescriptor* const file_; | |
64 Printer* const stub_h_; | |
65 Printer* const stub_cc_; | |
66 std::string error_; | |
67 }; | |
68 | |
69 } | |
Primiano Tucci (use gerrit)
2016/07/13 16:52:32
nit: add " // namespace" comment
| |
70 | |
71 ProtoZeroGenerator::ProtoZeroGenerator() { | |
72 } | |
73 | |
74 ProtoZeroGenerator::~ProtoZeroGenerator() { | |
75 } | |
76 | |
77 bool ProtoZeroGenerator::Generate(const FileDescriptor* file, | |
78 const std::string& options, | |
79 GeneratorContext* context, | |
80 std::string* error) const { | |
81 | |
82 const std::string proto_stubs_name = | |
83 StripSuffixString(file->name(), ".proto") + ".zeropb"; | |
84 | |
85 const std::unique_ptr<ZeroCopyOutputStream> stub_h_file_stream( | |
86 context->Open(proto_stubs_name + ".h")); | |
87 const std::unique_ptr<ZeroCopyOutputStream> stub_cc_file_stream( | |
88 context->Open(proto_stubs_name + ".cc")); | |
89 | |
90 // Variables are delimited by $. | |
91 Printer stub_h_printer(stub_h_file_stream.get(), '$'); | |
92 Printer stub_cc_printer(stub_cc_file_stream.get(), '$'); | |
93 | |
94 GeneratorJob job(file, &stub_h_printer, &stub_cc_printer); | |
95 if (!job.GenerateStubs()) { | |
96 *error = job.GetFirstError(); | |
97 return false; | |
98 } | |
99 return true; | |
100 } | |
101 | |
102 } // namespace proto | |
103 } // namespace tracing | |
OLD | NEW |