Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(88)

Side by Side Diff: components/tracing/tools/proto_zero_plugin/proto_zero_generator.cc

Issue 2201393002: Tracing V2: Import support for protobuf plugin. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: style Created 4 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « components/tracing/test/proto_zero_generation_unittest.cc ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2016 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "proto_zero_generator.h" 5 #include "proto_zero_generator.h"
6 6
7 #include <map> 7 #include <map>
8 #include <memory> 8 #include <memory>
9 #include <set>
9 #include <string> 10 #include <string>
10 11
11 #include "third_party/protobuf/src/google/protobuf/descriptor.h" 12 #include "third_party/protobuf/src/google/protobuf/descriptor.h"
12 #include "third_party/protobuf/src/google/protobuf/io/printer.h" 13 #include "third_party/protobuf/src/google/protobuf/io/printer.h"
13 #include "third_party/protobuf/src/google/protobuf/io/zero_copy_stream.h" 14 #include "third_party/protobuf/src/google/protobuf/io/zero_copy_stream.h"
14 #include "third_party/protobuf/src/google/protobuf/stubs/strutil.h" 15 #include "third_party/protobuf/src/google/protobuf/stubs/strutil.h"
15 16
16 namespace tracing { 17 namespace tracing {
17 namespace proto { 18 namespace proto {
18 19
19 using google::protobuf::Descriptor; // Message descriptor. 20 using google::protobuf::Descriptor; // Message descriptor.
20 using google::protobuf::EnumDescriptor; 21 using google::protobuf::EnumDescriptor;
21 using google::protobuf::EnumValueDescriptor; 22 using google::protobuf::EnumValueDescriptor;
22 using google::protobuf::FieldDescriptor; 23 using google::protobuf::FieldDescriptor;
23 using google::protobuf::FileDescriptor; 24 using google::protobuf::FileDescriptor;
24 using google::protobuf::compiler::GeneratorContext; 25 using google::protobuf::compiler::GeneratorContext;
25 using google::protobuf::io::Printer; 26 using google::protobuf::io::Printer;
26 using google::protobuf::io::ZeroCopyOutputStream; 27 using google::protobuf::io::ZeroCopyOutputStream;
27 28
28 using google::protobuf::Split; 29 using google::protobuf::Split;
29 using google::protobuf::StripPrefixString; 30 using google::protobuf::StripPrefixString;
30 using google::protobuf::StripString; 31 using google::protobuf::StripString;
31 using google::protobuf::StripSuffixString; 32 using google::protobuf::StripSuffixString;
32 using google::protobuf::UpperString; 33 using google::protobuf::UpperString;
33 34
34 namespace { 35 namespace {
35 36
37 inline std::string ProtoStubName(const FileDescriptor* proto) {
38 return StripSuffixString(proto->name(), ".proto") + ".pbzero";
39 }
40
36 class GeneratorJob { 41 class GeneratorJob {
37 public: 42 public:
38 GeneratorJob(const FileDescriptor *file, 43 GeneratorJob(const FileDescriptor *file,
39 Printer* stub_h_printer, 44 Printer* stub_h_printer,
40 Printer* stub_cc_printer) 45 Printer* stub_cc_printer)
41 : source_(file), 46 : source_(file),
42 stub_h_(stub_h_printer), 47 stub_h_(stub_h_printer),
43 stub_cc_(stub_cc_printer) {} 48 stub_cc_(stub_cc_printer) {}
44 49
45 bool GenerateStubs() { 50 bool GenerateStubs() {
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after
95 const EnumDescriptor* enumeration = field->enum_type(); 100 const EnumDescriptor* enumeration = field->enum_type();
96 101
97 for (int i = 0; i < enumeration->value_count(); ++i) { 102 for (int i = 0; i < enumeration->value_count(); ++i) {
98 int32_t value = enumeration->value(i)->number(); 103 int32_t value = enumeration->value(i)->number();
99 if (value < 0 || value > 0x7F) 104 if (value < 0 || value > 0x7F)
100 return false; 105 return false;
101 } 106 }
102 return true; 107 return true;
103 } 108 }
104 109
105 void Preprocess() { 110 void CollectDescriptors() {
106 // Package name maps to a series of namespaces.
107 package_ = source_->package();
108 namespaces_ = Split(package_, ".");
109 full_namespace_prefix_ = "::";
110 for (const std::string& ns : namespaces_)
111 full_namespace_prefix_ += ns + "::";
112
113 // Collect message descriptors in DFS order. 111 // Collect message descriptors in DFS order.
114 std::vector<const Descriptor*> stack; 112 std::vector<const Descriptor*> stack;
115 for (int i = 0; i < source_->message_type_count(); ++i) 113 for (int i = 0; i < source_->message_type_count(); ++i)
116 stack.push_back(source_->message_type(i)); 114 stack.push_back(source_->message_type(i));
117 115
118 while (!stack.empty()) { 116 while (!stack.empty()) {
119 const Descriptor* message = stack.back(); 117 const Descriptor* message = stack.back();
120 stack.pop_back(); 118 stack.pop_back();
121 messages_.push_back(message); 119 messages_.push_back(message);
122 for (int i = 0; i < message->nested_type_count(); ++i) { 120 for (int i = 0; i < message->nested_type_count(); ++i) {
123 stack.push_back(message->nested_type(i)); 121 stack.push_back(message->nested_type(i));
124 } 122 }
125 } 123 }
126 124
127 // Collect enums. 125 // Collect enums.
128 for (int i = 0; i < source_->enum_type_count(); ++i) 126 for (int i = 0; i < source_->enum_type_count(); ++i)
129 enums_.push_back(source_->enum_type(i)); 127 enums_.push_back(source_->enum_type(i));
130 128
131 for (const Descriptor* message : messages_) { 129 for (const Descriptor* message : messages_) {
132 for (int i = 0; i < message->enum_type_count(); ++i) { 130 for (int i = 0; i < message->enum_type_count(); ++i) {
133 enums_.push_back(message->enum_type(i)); 131 enums_.push_back(message->enum_type(i));
134 } 132 }
135 } 133 }
136 } 134 }
137 135
136 void CollectDependencies() {
137 // Public import basically means that callers only need to import this
138 // proto in order to use the stuff publicly imported by this proto.
139 for (int i = 0; i < source_->public_dependency_count(); ++i)
140 public_imports_.insert(source_->public_dependency(i));
141
142 if (source_->weak_dependency_count() > 0)
143 Abort("Weak imports are not supported.");
144
145 // Sanity check. Collect public imports (of collected imports) in DFS order.
146 // Visibilty for current proto:
147 // - all imports listed in current proto,
148 // - public imports of everything imported (recursive).
149 std::vector<const FileDescriptor*> stack;
150 for (int i = 0; i < source_->dependency_count(); ++i) {
151 const FileDescriptor* import = source_->dependency(i);
152 stack.push_back(import);
153 if (public_imports_.count(import) == 0) {
154 private_imports_.insert(import);
155 }
156 }
157
158 while (!stack.empty()) {
159 const FileDescriptor* import = stack.back();
160 stack.pop_back();
161 // Having imports under different packages leads to unnecessary
162 // complexity with namespaces.
163 if (import->package() != package_)
164 Abort("Imported proto must be in the same package.");
165
166 for (int i = 0; i < import->public_dependency_count(); ++i) {
167 stack.push_back(import->public_dependency(i));
168 }
169 }
170
171 // Collect descriptors of messages and enums used in current proto.
172 // It will be used to generate necessary forward declarations and performed
173 // sanity check guarantees that everything lays in the same namespace.
174 for (const Descriptor* message : messages_) {
175 for (int i = 0; i < message->field_count(); ++i) {
176 const FieldDescriptor* field = message->field(i);
177
178 if (field->type() == FieldDescriptor::TYPE_MESSAGE) {
179 if (public_imports_.count(field->message_type()->file()) == 0) {
180 // Avoid multiple forward declarations since
181 // public imports have been already included.
182 referenced_messages_.insert(field->message_type());
183 }
184 } else if (field->type() == FieldDescriptor::TYPE_ENUM) {
185 if (public_imports_.count(field->enum_type()->file()) == 0) {
186 referenced_enums_.insert(field->enum_type());
187 }
188 }
189 }
190 }
191 }
192
193 void Preprocess() {
194 // Package name maps to a series of namespaces.
195 package_ = source_->package();
196 namespaces_ = Split(package_, ".");
197 full_namespace_prefix_ = "::";
198 for (const std::string& ns : namespaces_)
199 full_namespace_prefix_ += ns + "::";
200 CollectDescriptors();
201 CollectDependencies();
202 }
203
138 // Print top header, namespaces and forward declarations. 204 // Print top header, namespaces and forward declarations.
139 void GeneratePrologue() { 205 void GeneratePrologue() {
140 std::string greeting = 206 std::string greeting =
141 "// Autogenerated. DO NOT EDIT.\n" 207 "// Autogenerated. DO NOT EDIT.\n"
142 "// Protobuf compiler (protoc) has generated these stubs with\n" 208 "// Protobuf compiler (protoc) has generated these stubs with\n"
143 "// //components/tracing/tools/proto_zero_plugin.\n"; 209 "// //components/tracing/tools/proto_zero_plugin.\n";
144 std::string guard = package_ + "_" + source_->name() + "_H_"; 210 std::string guard = package_ + "_" + source_->name() + "_H_";
145 UpperString(&guard); 211 UpperString(&guard);
146 StripString(&guard, ".-", '_'); 212 StripString(&guard, ".-", '_');
147 213
148 stub_h_->Print( 214 stub_h_->Print(
149 "$greeting$\n" 215 "$greeting$\n"
150 "#ifndef $guard$\n" 216 "#ifndef $guard$\n"
151 "#define $guard$\n\n" 217 "#define $guard$\n\n"
152 "#include <stddef.h>\n" 218 "#include <stddef.h>\n"
153 "#include <stdint.h>\n\n" 219 "#include <stdint.h>\n\n"
154 "#include \"components/tracing/core/proto_zero_message.h\"\n\n", 220 "#include \"components/tracing/core/proto_zero_message.h\"\n",
155 "greeting", greeting, 221 "greeting", greeting,
156 "guard", guard); 222 "guard", guard);
157 stub_cc_->Print( 223 stub_cc_->Print(
158 "$greeting$\n" 224 "$greeting$\n"
159 "// This file intentionally left blank.\n", 225 "#include \"$name$.h\"\n",
160 "greeting", greeting); 226 "greeting", greeting,
227 "name", ProtoStubName(source_));
228
229 // Print includes for public imports.
230 for (const FileDescriptor* dependency : public_imports_) {
231 // Dependency name could contatin slashes but importing from upper-level
232 // directories is not possible anyway since build system process each
233 // proto file individually. Hence proto lookup path always equal to the
234 // directory where particular proto file is located and protoc does not
235 // allow reference to upper directory (aka ..) in import path.
236 //
237 // Laconically said:
238 // - source_->name() may never have slashes,
239 // - dependency->name() may have slashes but always reffers to inner path.
240 stub_h_->Print(
241 "#include \"$name$.h\"\n",
242 "name", ProtoStubName(dependency));
243 }
244 stub_h_->Print("\n");
245
246 // Print includes for private imports to .cc file.
247 for (const FileDescriptor* dependency : private_imports_) {
248 stub_cc_->Print(
249 "#include \"$name$.h\"\n",
250 "name", ProtoStubName(dependency));
251 }
252 stub_cc_->Print("\n");
161 253
162 // Print namespaces. 254 // Print namespaces.
163 for (const std::string& ns : namespaces_) 255 for (const std::string& ns : namespaces_) {
164 stub_h_->Print("namespace $ns$ {\n", "ns", ns); 256 stub_h_->Print("namespace $ns$ {\n", "ns", ns);
257 stub_cc_->Print("namespace $ns$ {\n", "ns", ns);
258 }
165 stub_h_->Print("\n"); 259 stub_h_->Print("\n");
260 stub_cc_->Print("\n");
261
166 // Print forward declarations. 262 // Print forward declarations.
167 for (const Descriptor* message : messages_) { 263 for (const Descriptor* message : referenced_messages_) {
168 stub_h_->Print( 264 stub_h_->Print(
169 "class $class$;\n", 265 "class $class$;\n",
170 "class", GetCppClassName(message)); 266 "class", GetCppClassName(message));
171 } 267 }
268 for (const EnumDescriptor* enumeration : referenced_enums_) {
269 stub_h_->Print(
270 "enum $class$ : int32_t;\n",
271 "class", GetCppClassName(enumeration));
272 }
172 stub_h_->Print("\n"); 273 stub_h_->Print("\n");
173 } 274 }
174 275
175 void GenerateEnumDescriptor(const EnumDescriptor* enumeration) { 276 void GenerateEnumDescriptor(const EnumDescriptor* enumeration) {
176 stub_h_->Print( 277 stub_h_->Print(
177 "enum $class$ {\n", 278 "enum $class$ : int32_t {\n",
178 "class", GetCppClassName(enumeration)); 279 "class", GetCppClassName(enumeration));
179 stub_h_->Indent(); 280 stub_h_->Indent();
180 281
181 std::string value_name_prefix; 282 std::string value_name_prefix;
182 if (enumeration->containing_type() != nullptr) 283 if (enumeration->containing_type() != nullptr)
183 value_name_prefix = GetCppClassName(enumeration) + "_"; 284 value_name_prefix = GetCppClassName(enumeration) + "_";
184 285
185 for (int i = 0; i < enumeration->value_count(); ++i) { 286 for (int i = 0; i < enumeration->value_count(); ++i) {
186 const EnumValueDescriptor* value = enumeration->value(i); 287 const EnumValueDescriptor* value = enumeration->value(i);
187 stub_h_->Print( 288 stub_h_->Print(
(...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after
295 setter["appender"] = appender; 396 setter["appender"] = appender;
296 setter["cpp_type"] = cpp_type; 397 setter["cpp_type"] = cpp_type;
297 stub_h_->Print( 398 stub_h_->Print(
298 setter, 399 setter,
299 "void $action$_$name$($cpp_type$ value) {\n" 400 "void $action$_$name$($cpp_type$ value) {\n"
300 " // $appender$($id$, value);\n" 401 " // $appender$($id$, value);\n"
301 "}\n"); 402 "}\n");
302 } 403 }
303 404
304 void GenerateNestedMessageFieldDescriptor(const FieldDescriptor* field) { 405 void GenerateNestedMessageFieldDescriptor(const FieldDescriptor* field) {
406 std::string action = field->is_repeated() ? "add" : "set";
407 std::string inner_class = GetCppClassName(field->message_type());
408 std::string outer_class = GetCppClassName(field->containing_type());
409
305 stub_h_->Print( 410 stub_h_->Print(
306 "$class$* $action$_$name$() {\n" 411 "$inner_class$* $action$_$name$();\n",
307 " return BeginNestedMessage<$class$>($id$);\n" 412 "name", field->name(),
308 "}\n", 413 "action", action,
414 "inner_class", inner_class);
415 stub_cc_->Print(
416 "$inner_class$* $outer_class$::$action$_$name$() {\n"
417 " return BeginNestedMessage<$inner_class$>($id$);\n"
418 "}\n\n",
309 "id", std::to_string(field->number()), 419 "id", std::to_string(field->number()),
310 "name", field->name(), 420 "name", field->name(),
311 "action", field->is_repeated() ? "add" : "set", 421 "action", action,
312 "class", GetCppClassName(field->message_type())); 422 "inner_class", inner_class,
423 "outer_class", outer_class);
313 } 424 }
314 425
315 void GenerateMessageDescriptor(const Descriptor* message) { 426 void GenerateMessageDescriptor(const Descriptor* message) {
316 stub_h_->Print( 427 stub_h_->Print(
317 "class $name$ : public ::tracing::v2::ProtoZeroMessage {\n" 428 "class $name$ : public ::tracing::v2::ProtoZeroMessage {\n"
318 " public:\n", 429 " public:\n",
319 "name", GetCppClassName(message)); 430 "name", GetCppClassName(message));
320 stub_h_->Indent(); 431 stub_h_->Indent();
321 432
322 // Using statements for nested messages. 433 // Using statements for nested messages.
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after
366 } 477 }
367 } 478 }
368 479
369 stub_h_->Outdent(); 480 stub_h_->Outdent();
370 stub_h_->Print("};\n\n"); 481 stub_h_->Print("};\n\n");
371 } 482 }
372 483
373 void GenerateEpilogue() { 484 void GenerateEpilogue() {
374 for (unsigned i = 0; i < namespaces_.size(); ++i) { 485 for (unsigned i = 0; i < namespaces_.size(); ++i) {
375 stub_h_->Print("} // Namespace.\n"); 486 stub_h_->Print("} // Namespace.\n");
487 stub_cc_->Print("} // Namespace.\n");
376 } 488 }
377 stub_h_->Print("#endif // Include guard.\n"); 489 stub_h_->Print("#endif // Include guard.\n");
378 } 490 }
379 491
380 const FileDescriptor* const source_; 492 const FileDescriptor* const source_;
381 Printer* const stub_h_; 493 Printer* const stub_h_;
382 Printer* const stub_cc_; 494 Printer* const stub_cc_;
383 std::string error_; 495 std::string error_;
384 496
385 std::string package_; 497 std::string package_;
386 std::vector<std::string> namespaces_; 498 std::vector<std::string> namespaces_;
387 std::string full_namespace_prefix_; 499 std::string full_namespace_prefix_;
388 std::vector<const Descriptor*> messages_; 500 std::vector<const Descriptor*> messages_;
389 std::vector<const EnumDescriptor*> enums_; 501 std::vector<const EnumDescriptor*> enums_;
502
503 std::set<const FileDescriptor*> public_imports_;
504 std::set<const FileDescriptor*> private_imports_;
505 std::set<const Descriptor*> referenced_messages_;
506 std::set<const EnumDescriptor*> referenced_enums_;
390 }; 507 };
391 508
392 } // namespace 509 } // namespace
393 510
394 ProtoZeroGenerator::ProtoZeroGenerator() { 511 ProtoZeroGenerator::ProtoZeroGenerator() {
395 } 512 }
396 513
397 ProtoZeroGenerator::~ProtoZeroGenerator() { 514 ProtoZeroGenerator::~ProtoZeroGenerator() {
398 } 515 }
399 516
400 bool ProtoZeroGenerator::Generate(const FileDescriptor* file, 517 bool ProtoZeroGenerator::Generate(const FileDescriptor* file,
401 const std::string& options, 518 const std::string& options,
402 GeneratorContext* context, 519 GeneratorContext* context,
403 std::string* error) const { 520 std::string* error) const {
404 521
405 const std::string proto_stubs_name =
406 StripSuffixString(file->name(), ".proto") + ".pbzero";
407
408 const std::unique_ptr<ZeroCopyOutputStream> stub_h_file_stream( 522 const std::unique_ptr<ZeroCopyOutputStream> stub_h_file_stream(
409 context->Open(proto_stubs_name + ".h")); 523 context->Open(ProtoStubName(file) + ".h"));
410 const std::unique_ptr<ZeroCopyOutputStream> stub_cc_file_stream( 524 const std::unique_ptr<ZeroCopyOutputStream> stub_cc_file_stream(
411 context->Open(proto_stubs_name + ".cc")); 525 context->Open(ProtoStubName(file) + ".cc"));
412 526
413 // Variables are delimited by $. 527 // Variables are delimited by $.
414 Printer stub_h_printer(stub_h_file_stream.get(), '$'); 528 Printer stub_h_printer(stub_h_file_stream.get(), '$');
415 Printer stub_cc_printer(stub_cc_file_stream.get(), '$'); 529 Printer stub_cc_printer(stub_cc_file_stream.get(), '$');
416 530
417 GeneratorJob job(file, &stub_h_printer, &stub_cc_printer); 531 GeneratorJob job(file, &stub_h_printer, &stub_cc_printer);
418 if (!job.GenerateStubs()) { 532 if (!job.GenerateStubs()) {
419 *error = job.GetFirstError(); 533 *error = job.GetFirstError();
420 return false; 534 return false;
421 } 535 }
422 return true; 536 return true;
423 } 537 }
424 538
425 } // namespace proto 539 } // namespace proto
426 } // namespace tracing 540 } // namespace tracing
OLDNEW
« no previous file with comments | « components/tracing/test/proto_zero_generation_unittest.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698