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

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: fix use after free in tests 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
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 84 matching lines...) Expand 10 before | Expand all | Expand 10 after
294 stub_h_->Outdent(); 306 stub_h_->Outdent();
295 stub_h_->Print("};\n\n"); 307 stub_h_->Print("};\n\n");
296 } 308 }
297 309
298 void GenerateSimpleFieldDescriptor(const FieldDescriptor* field) { 310 void GenerateSimpleFieldDescriptor(const FieldDescriptor* field) {
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
316 if (field->is_repeated()) {
317 setter["dcheck"] = "";
318 } else {
319 setter["dcheck"] =
320 std::string(" DCheckSealField(") + setter["id"] + ");\n " ;
321 }
322
304 std::string appender; 323 std::string appender;
305 std::string cpp_type; 324 std::string cpp_type;
306 325
307 switch (field->type()) { 326 switch (field->type()) {
308 case FieldDescriptor::TYPE_BOOL: { 327 case FieldDescriptor::TYPE_BOOL: {
309 appender = "AppendBool"; 328 appender = "AppendTinyVarInt";
310 cpp_type = "bool"; 329 cpp_type = "bool";
311 break; 330 break;
312 } 331 }
313 case FieldDescriptor::TYPE_INT32: { 332 case FieldDescriptor::TYPE_INT32: {
314 appender = "AppendInt32"; 333 appender = "AppendVarInt";
315 cpp_type = "int32_t"; 334 cpp_type = "int32_t";
316 break; 335 break;
317 } 336 }
318 case FieldDescriptor::TYPE_INT64: { 337 case FieldDescriptor::TYPE_INT64: {
319 appender = "AppendInt64"; 338 appender = "AppendVarInt";
320 cpp_type = "int64_t"; 339 cpp_type = "int64_t";
321 break; 340 break;
322 } 341 }
323 case FieldDescriptor::TYPE_UINT32: { 342 case FieldDescriptor::TYPE_UINT32: {
324 appender = "AppendUint32"; 343 appender = "AppendVarInt";
325 cpp_type = "uint32_t"; 344 cpp_type = "uint32_t";
326 break; 345 break;
327 } 346 }
328 case FieldDescriptor::TYPE_UINT64: { 347 case FieldDescriptor::TYPE_UINT64: {
329 appender = "AppendUint64"; 348 appender = "AppendVarInt";
330 cpp_type = "uint64_t"; 349 cpp_type = "uint64_t";
331 break; 350 break;
332 } 351 }
333 case FieldDescriptor::TYPE_SINT32: { 352 case FieldDescriptor::TYPE_SINT32: {
334 appender = "AppendSint32"; 353 appender = "AppendSignedVarInt";
335 cpp_type = "int32_t"; 354 cpp_type = "int32_t";
336 break; 355 break;
337 } 356 }
338 case FieldDescriptor::TYPE_SINT64: { 357 case FieldDescriptor::TYPE_SINT64: {
339 appender = "AppendSint64"; 358 appender = "AppendSignedVarInt";
340 cpp_type = "int64_t"; 359 cpp_type = "int64_t";
341 break; 360 break;
342 } 361 }
343 case FieldDescriptor::TYPE_FIXED32: { 362 case FieldDescriptor::TYPE_FIXED32: {
344 appender = "AppendFixed32"; 363 appender = "AppendFixed";
345 cpp_type = "uint32_t"; 364 cpp_type = "uint32_t";
346 break; 365 break;
347 } 366 }
348 case FieldDescriptor::TYPE_FIXED64: { 367 case FieldDescriptor::TYPE_FIXED64: {
349 appender = "AppendFixed64"; 368 appender = "AppendFixed";
350 cpp_type = "uint64_t"; 369 cpp_type = "uint64_t";
351 break; 370 break;
352 } 371 }
353 case FieldDescriptor::TYPE_SFIXED32: { 372 case FieldDescriptor::TYPE_SFIXED32: {
354 appender = "AppendSfixed32"; 373 appender = "AppendFixed";
355 cpp_type = "int32_t"; 374 cpp_type = "int32_t";
356 break; 375 break;
357 } 376 }
358 case FieldDescriptor::TYPE_SFIXED64: { 377 case FieldDescriptor::TYPE_SFIXED64: {
359 appender = "AppendSfixed64"; 378 appender = "AppendFixed";
360 cpp_type = "int64_t"; 379 cpp_type = "int64_t";
361 break; 380 break;
362 } 381 }
363 case FieldDescriptor::TYPE_FLOAT: { 382 case FieldDescriptor::TYPE_FLOAT: {
364 appender = "AppendFloat"; 383 appender = "AppendFixed";
365 cpp_type = "float"; 384 cpp_type = "float";
366 break; 385 break;
367 } 386 }
368 case FieldDescriptor::TYPE_DOUBLE: { 387 case FieldDescriptor::TYPE_DOUBLE: {
369 appender = "AppendDouble"; 388 appender = "AppendFixed";
370 cpp_type = "double"; 389 cpp_type = "double";
371 break; 390 break;
372 } 391 }
373 case FieldDescriptor::TYPE_ENUM: { 392 case FieldDescriptor::TYPE_ENUM: {
374 appender = IsTinyEnumField(field) ? "AppendTinyNumber" : "AppendInt32"; 393 appender = IsTinyEnumField(field) ? "AppendTinyVarInt" : "AppendVarInt";
375 cpp_type = GetCppClassName(field->enum_type(), true); 394 cpp_type = GetCppClassName(field->enum_type(), true);
376 break; 395 break;
377 } 396 }
378 case FieldDescriptor::TYPE_STRING: { 397 case FieldDescriptor::TYPE_STRING: {
379 appender = "AppendString"; 398 appender = "AppendString";
380 cpp_type = "const char*"; 399 cpp_type = "const char*";
381 break; 400 break;
382 } 401 }
383 case FieldDescriptor::TYPE_BYTES: { 402 case FieldDescriptor::TYPE_BYTES: {
384 stub_h_->Print( 403 stub_h_->Print(
385 setter, 404 setter,
386 "void $action$_$name$(const uint8_t* data, size_t size) {\n" 405 "void $action$_$name$(const uint8_t* data, size_t size) {\n"
387 " // AppendBytes($id$, data, size);\n" 406 "$dcheck$ AppendBytes($id$, data, size);\n"
388 "}\n"); 407 "}\n");
389 return; 408 return;
390 } 409 }
391 default: { 410 default: {
392 Abort("Unsupported field type."); 411 Abort("Unsupported field type.");
393 return; 412 return;
394 } 413 }
395 } 414 }
396 setter["appender"] = appender; 415 setter["appender"] = appender;
397 setter["cpp_type"] = cpp_type; 416 setter["cpp_type"] = cpp_type;
398 stub_h_->Print( 417 stub_h_->Print(
399 setter, 418 setter,
400 "void $action$_$name$($cpp_type$ value) {\n" 419 "void $action$_$name$($cpp_type$ value) {\n"
401 " // $appender$($id$, value);\n" 420 "$dcheck$ $appender$($id$, value);\n"
402 "}\n"); 421 "}\n");
403 } 422 }
404 423
405 void GenerateNestedMessageFieldDescriptor(const FieldDescriptor* field) { 424 void GenerateNestedMessageFieldDescriptor(const FieldDescriptor* field) {
406 std::string action = field->is_repeated() ? "add" : "set"; 425 std::string action = field->is_repeated() ? "add" : "set";
407 std::string inner_class = GetCppClassName(field->message_type()); 426 std::string inner_class = GetCppClassName(field->message_type());
408 std::string outer_class = GetCppClassName(field->containing_type()); 427 std::string outer_class = GetCppClassName(field->containing_type());
409 428
410 stub_h_->Print( 429 stub_h_->Print(
411 "$inner_class$* $action$_$name$();\n", 430 "$inner_class$* $action$_$name$();\n",
(...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after
512 } 531 }
513 stub_h_->Print("#endif // Include guard.\n"); 532 stub_h_->Print("#endif // Include guard.\n");
514 } 533 }
515 534
516 const FileDescriptor* const source_; 535 const FileDescriptor* const source_;
517 Printer* const stub_h_; 536 Printer* const stub_h_;
518 Printer* const stub_cc_; 537 Printer* const stub_cc_;
519 std::string error_; 538 std::string error_;
520 539
521 std::string package_; 540 std::string package_;
541 std::string wrapper_namespace_;
522 std::vector<std::string> namespaces_; 542 std::vector<std::string> namespaces_;
523 std::string full_namespace_prefix_; 543 std::string full_namespace_prefix_;
524 std::vector<const Descriptor*> messages_; 544 std::vector<const Descriptor*> messages_;
525 std::vector<const EnumDescriptor*> enums_; 545 std::vector<const EnumDescriptor*> enums_;
526 546
527 std::set<const FileDescriptor*> public_imports_; 547 std::set<const FileDescriptor*> public_imports_;
528 std::set<const FileDescriptor*> private_imports_; 548 std::set<const FileDescriptor*> private_imports_;
529 std::set<const Descriptor*> referenced_messages_; 549 std::set<const Descriptor*> referenced_messages_;
530 std::set<const EnumDescriptor*> referenced_enums_; 550 std::set<const EnumDescriptor*> referenced_enums_;
531 }; 551 };
(...skipping 12 matching lines...) Expand all
544 std::string* error) const { 564 std::string* error) const {
545 565
546 const std::unique_ptr<ZeroCopyOutputStream> stub_h_file_stream( 566 const std::unique_ptr<ZeroCopyOutputStream> stub_h_file_stream(
547 context->Open(ProtoStubName(file) + ".h")); 567 context->Open(ProtoStubName(file) + ".h"));
548 const std::unique_ptr<ZeroCopyOutputStream> stub_cc_file_stream( 568 const std::unique_ptr<ZeroCopyOutputStream> stub_cc_file_stream(
549 context->Open(ProtoStubName(file) + ".cc")); 569 context->Open(ProtoStubName(file) + ".cc"));
550 570
551 // Variables are delimited by $. 571 // Variables are delimited by $.
552 Printer stub_h_printer(stub_h_file_stream.get(), '$'); 572 Printer stub_h_printer(stub_h_file_stream.get(), '$');
553 Printer stub_cc_printer(stub_cc_file_stream.get(), '$'); 573 Printer stub_cc_printer(stub_cc_file_stream.get(), '$');
574 GeneratorJob job(file, &stub_h_printer, &stub_cc_printer);
554 575
555 GeneratorJob job(file, &stub_h_printer, &stub_cc_printer); 576 // Parse additional options.
577 for (const std::string& option : Split(options, ",")) {
578 std::vector<std::string> option_pair = Split(option, "=");
579 job.SetOption(option_pair[0], option_pair[1]);
580 }
581
556 if (!job.GenerateStubs()) { 582 if (!job.GenerateStubs()) {
557 *error = job.GetFirstError(); 583 *error = job.GetFirstError();
558 return false; 584 return false;
559 } 585 }
560 return true; 586 return true;
561 } 587 }
562 588
563 } // namespace proto 589 } // namespace proto
564 } // namespace tracing 590 } // namespace tracing
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698