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

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

Issue 2240043004: Tracing V2: Fully-functional plugin. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: rebase Created 4 years, 3 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 <set>
10 #include <string> 10 #include <string>
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after
51 Preprocess(); 51 Preprocess();
52 GeneratePrologue(); 52 GeneratePrologue();
53 for (const EnumDescriptor* enumeration: enums_) 53 for (const EnumDescriptor* enumeration: enums_)
54 GenerateEnumDescriptor(enumeration); 54 GenerateEnumDescriptor(enumeration);
55 for (const Descriptor* message : messages_) 55 for (const Descriptor* message : messages_)
56 GenerateMessageDescriptor(message); 56 GenerateMessageDescriptor(message);
57 GenerateEpilogue(); 57 GenerateEpilogue();
58 return error_.empty(); 58 return error_.empty();
59 } 59 }
60 60
61 void SetOption(const std::string& name, const std::string& value) {
62 if (name == "wrapper_namespace") {
63 wrapper_namespace_ = value;
64 } else {
65 Abort(std::string() + "Unknown plugin option '" + name + "'.");
66 }
67 }
68
61 // If generator fails to produce stubs for a particular proto definitions 69 // If generator fails to produce stubs for a particular proto definitions
62 // it finishes with undefined output and writes the first error occured. 70 // it finishes with undefined output and writes the first error occured.
63 const std::string& GetFirstError() const { 71 const std::string& GetFirstError() const {
64 return error_; 72 return error_;
65 } 73 }
66 74
67 private: 75 private:
68 // Only the first error will be recorded. 76 // Only the first error will be recorded.
69 void Abort(const std::string& reason) { 77 void Abort(const std::string& reason) {
70 if (error_.empty()) 78 if (error_.empty())
(...skipping 116 matching lines...) Expand 10 before | Expand all | Expand 10 after
187 } 195 }
188 } 196 }
189 } 197 }
190 } 198 }
191 } 199 }
192 200
193 void Preprocess() { 201 void Preprocess() {
194 // Package name maps to a series of namespaces. 202 // Package name maps to a series of namespaces.
195 package_ = source_->package(); 203 package_ = source_->package();
196 namespaces_ = Split(package_, "."); 204 namespaces_ = Split(package_, ".");
205 if (!wrapper_namespace_.empty())
206 namespaces_.insert(namespaces_.begin(), wrapper_namespace_);
207
197 full_namespace_prefix_ = "::"; 208 full_namespace_prefix_ = "::";
198 for (const std::string& ns : namespaces_) 209 for (const std::string& ns : namespaces_)
199 full_namespace_prefix_ += ns + "::"; 210 full_namespace_prefix_ += ns + "::";
211
200 CollectDescriptors(); 212 CollectDescriptors();
201 CollectDependencies(); 213 CollectDependencies();
202 } 214 }
203 215
204 // Print top header, namespaces and forward declarations. 216 // Print top header, namespaces and forward declarations.
205 void GeneratePrologue() { 217 void GeneratePrologue() {
206 std::string greeting = 218 std::string greeting =
207 "// Autogenerated. DO NOT EDIT.\n" 219 "// Autogenerated. DO NOT EDIT.\n"
208 "// Protobuf compiler (protoc) has generated these stubs with\n" 220 "// Protobuf compiler (protoc) has generated these stubs with\n"
209 "// //components/tracing/tools/proto_zero_plugin.\n"; 221 "// //components/tracing/tools/proto_zero_plugin.\n";
(...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after
299 std::map<std::string, std::string> setter; 311 std::map<std::string, std::string> setter;
300 setter["id"] = std::to_string(field->number()); 312 setter["id"] = std::to_string(field->number());
301 setter["name"] = field->name(); 313 setter["name"] = field->name();
302 setter["action"] = field->is_repeated() ? "add" : "set"; 314 setter["action"] = field->is_repeated() ? "add" : "set";
303 315
304 std::string appender; 316 std::string appender;
305 std::string cpp_type; 317 std::string cpp_type;
306 318
307 switch (field->type()) { 319 switch (field->type()) {
308 case FieldDescriptor::TYPE_BOOL: { 320 case FieldDescriptor::TYPE_BOOL: {
309 appender = "AppendBool"; 321 appender = "AppendTinyVarInt";
310 cpp_type = "bool"; 322 cpp_type = "bool";
311 break; 323 break;
312 } 324 }
313 case FieldDescriptor::TYPE_INT32: { 325 case FieldDescriptor::TYPE_INT32: {
314 appender = "AppendInt32"; 326 appender = "AppendVarInt";
315 cpp_type = "int32_t"; 327 cpp_type = "int32_t";
316 break; 328 break;
317 } 329 }
318 case FieldDescriptor::TYPE_INT64: { 330 case FieldDescriptor::TYPE_INT64: {
319 appender = "AppendInt64"; 331 appender = "AppendVarInt";
320 cpp_type = "int64_t"; 332 cpp_type = "int64_t";
321 break; 333 break;
322 } 334 }
323 case FieldDescriptor::TYPE_UINT32: { 335 case FieldDescriptor::TYPE_UINT32: {
324 appender = "AppendUint32"; 336 appender = "AppendVarInt";
325 cpp_type = "uint32_t"; 337 cpp_type = "uint32_t";
326 break; 338 break;
327 } 339 }
328 case FieldDescriptor::TYPE_UINT64: { 340 case FieldDescriptor::TYPE_UINT64: {
329 appender = "AppendUint64"; 341 appender = "AppendVarInt";
330 cpp_type = "uint64_t"; 342 cpp_type = "uint64_t";
331 break; 343 break;
332 } 344 }
333 case FieldDescriptor::TYPE_SINT32: { 345 case FieldDescriptor::TYPE_SINT32: {
334 appender = "AppendSint32"; 346 appender = "AppendSignedVarInt";
335 cpp_type = "int32_t"; 347 cpp_type = "int32_t";
336 break; 348 break;
337 } 349 }
338 case FieldDescriptor::TYPE_SINT64: { 350 case FieldDescriptor::TYPE_SINT64: {
339 appender = "AppendSint64"; 351 appender = "AppendSignedVarInt";
340 cpp_type = "int64_t"; 352 cpp_type = "int64_t";
341 break; 353 break;
342 } 354 }
343 case FieldDescriptor::TYPE_FIXED32: { 355 case FieldDescriptor::TYPE_FIXED32: {
344 appender = "AppendFixed32"; 356 appender = "AppendFixed";
345 cpp_type = "uint32_t"; 357 cpp_type = "uint32_t";
346 break; 358 break;
347 } 359 }
348 case FieldDescriptor::TYPE_FIXED64: { 360 case FieldDescriptor::TYPE_FIXED64: {
349 appender = "AppendFixed64"; 361 appender = "AppendFixed";
350 cpp_type = "uint64_t"; 362 cpp_type = "uint64_t";
351 break; 363 break;
352 } 364 }
353 case FieldDescriptor::TYPE_SFIXED32: { 365 case FieldDescriptor::TYPE_SFIXED32: {
354 appender = "AppendSfixed32"; 366 appender = "AppendFixed";
355 cpp_type = "int32_t"; 367 cpp_type = "int32_t";
356 break; 368 break;
357 } 369 }
358 case FieldDescriptor::TYPE_SFIXED64: { 370 case FieldDescriptor::TYPE_SFIXED64: {
359 appender = "AppendSfixed64"; 371 appender = "AppendFixed";
360 cpp_type = "int64_t"; 372 cpp_type = "int64_t";
361 break; 373 break;
362 } 374 }
363 case FieldDescriptor::TYPE_FLOAT: { 375 case FieldDescriptor::TYPE_FLOAT: {
364 appender = "AppendFloat"; 376 appender = "AppendFixed";
365 cpp_type = "float"; 377 cpp_type = "float";
366 break; 378 break;
367 } 379 }
368 case FieldDescriptor::TYPE_DOUBLE: { 380 case FieldDescriptor::TYPE_DOUBLE: {
369 appender = "AppendDouble"; 381 appender = "AppendFixed";
370 cpp_type = "double"; 382 cpp_type = "double";
371 break; 383 break;
372 } 384 }
373 case FieldDescriptor::TYPE_ENUM: { 385 case FieldDescriptor::TYPE_ENUM: {
374 appender = IsTinyEnumField(field) ? "AppendTinyNumber" : "AppendInt32"; 386 appender = IsTinyEnumField(field) ? "AppendTinyVarInt" : "AppendVarInt";
375 cpp_type = GetCppClassName(field->enum_type(), true); 387 cpp_type = GetCppClassName(field->enum_type(), true);
376 break; 388 break;
377 } 389 }
378 case FieldDescriptor::TYPE_STRING: { 390 case FieldDescriptor::TYPE_STRING: {
379 appender = "AppendString"; 391 appender = "AppendString";
380 cpp_type = "const char*"; 392 cpp_type = "const char*";
381 break; 393 break;
382 } 394 }
383 case FieldDescriptor::TYPE_BYTES: { 395 case FieldDescriptor::TYPE_BYTES: {
384 stub_h_->Print( 396 stub_h_->Print(
385 setter, 397 setter,
386 "void $action$_$name$(const uint8_t* data, size_t size) {\n" 398 "void $action$_$name$(const uint8_t* data, size_t size) {\n"
387 " // AppendBytes($id$, data, size);\n" 399 " AppendBytes($id$, data, size);\n"
388 "}\n"); 400 "}\n");
389 return; 401 return;
390 } 402 }
391 default: { 403 default: {
392 Abort("Unsupported field type."); 404 Abort("Unsupported field type.");
393 return; 405 return;
394 } 406 }
395 } 407 }
396 setter["appender"] = appender; 408 setter["appender"] = appender;
397 setter["cpp_type"] = cpp_type; 409 setter["cpp_type"] = cpp_type;
398 stub_h_->Print( 410 stub_h_->Print(
399 setter, 411 setter,
400 "void $action$_$name$($cpp_type$ value) {\n" 412 "void $action$_$name$($cpp_type$ value) {\n"
401 " // $appender$($id$, value);\n" 413 " $appender$($id$, value);\n"
402 "}\n"); 414 "}\n");
403 } 415 }
404 416
405 void GenerateNestedMessageFieldDescriptor(const FieldDescriptor* field) { 417 void GenerateNestedMessageFieldDescriptor(const FieldDescriptor* field) {
406 std::string action = field->is_repeated() ? "add" : "set"; 418 std::string action = field->is_repeated() ? "add" : "set";
407 std::string inner_class = GetCppClassName(field->message_type()); 419 std::string inner_class = GetCppClassName(field->message_type());
408 std::string outer_class = GetCppClassName(field->containing_type()); 420 std::string outer_class = GetCppClassName(field->containing_type());
409 421
410 stub_h_->Print( 422 stub_h_->Print(
411 "$inner_class$* $action$_$name$();\n", 423 "$inner_class$* $action$_$name$();\n",
(...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after
512 } 524 }
513 stub_h_->Print("#endif // Include guard.\n"); 525 stub_h_->Print("#endif // Include guard.\n");
514 } 526 }
515 527
516 const FileDescriptor* const source_; 528 const FileDescriptor* const source_;
517 Printer* const stub_h_; 529 Printer* const stub_h_;
518 Printer* const stub_cc_; 530 Printer* const stub_cc_;
519 std::string error_; 531 std::string error_;
520 532
521 std::string package_; 533 std::string package_;
534 std::string wrapper_namespace_;
522 std::vector<std::string> namespaces_; 535 std::vector<std::string> namespaces_;
523 std::string full_namespace_prefix_; 536 std::string full_namespace_prefix_;
524 std::vector<const Descriptor*> messages_; 537 std::vector<const Descriptor*> messages_;
525 std::vector<const EnumDescriptor*> enums_; 538 std::vector<const EnumDescriptor*> enums_;
526 539
527 std::set<const FileDescriptor*> public_imports_; 540 std::set<const FileDescriptor*> public_imports_;
528 std::set<const FileDescriptor*> private_imports_; 541 std::set<const FileDescriptor*> private_imports_;
529 std::set<const Descriptor*> referenced_messages_; 542 std::set<const Descriptor*> referenced_messages_;
530 std::set<const EnumDescriptor*> referenced_enums_; 543 std::set<const EnumDescriptor*> referenced_enums_;
531 }; 544 };
(...skipping 12 matching lines...) Expand all
544 std::string* error) const { 557 std::string* error) const {
545 558
546 const std::unique_ptr<ZeroCopyOutputStream> stub_h_file_stream( 559 const std::unique_ptr<ZeroCopyOutputStream> stub_h_file_stream(
547 context->Open(ProtoStubName(file) + ".h")); 560 context->Open(ProtoStubName(file) + ".h"));
548 const std::unique_ptr<ZeroCopyOutputStream> stub_cc_file_stream( 561 const std::unique_ptr<ZeroCopyOutputStream> stub_cc_file_stream(
549 context->Open(ProtoStubName(file) + ".cc")); 562 context->Open(ProtoStubName(file) + ".cc"));
550 563
551 // Variables are delimited by $. 564 // Variables are delimited by $.
552 Printer stub_h_printer(stub_h_file_stream.get(), '$'); 565 Printer stub_h_printer(stub_h_file_stream.get(), '$');
553 Printer stub_cc_printer(stub_cc_file_stream.get(), '$'); 566 Printer stub_cc_printer(stub_cc_file_stream.get(), '$');
567 GeneratorJob job(file, &stub_h_printer, &stub_cc_printer);
554 568
555 GeneratorJob job(file, &stub_h_printer, &stub_cc_printer); 569 // Parse additional options.
570 for (const std::string& option : Split(options, ",")) {
571 std::vector<std::string> option_pair = Split(option, "=");
572 job.SetOption(option_pair[0], option_pair[1]);
573 }
574
556 if (!job.GenerateStubs()) { 575 if (!job.GenerateStubs()) {
557 *error = job.GetFirstError(); 576 *error = job.GetFirstError();
558 return false; 577 return false;
559 } 578 }
560 return true; 579 return true;
561 } 580 }
562 581
563 } // namespace proto 582 } // namespace proto
564 } // namespace tracing 583 } // 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