Chromium Code Reviews| OLD | NEW |
|---|---|
| 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 <string> | 9 #include <string> |
| 10 | 10 |
| (...skipping 15 matching lines...) Expand all Loading... | |
| 26 using google::protobuf::io::ZeroCopyOutputStream; | 26 using google::protobuf::io::ZeroCopyOutputStream; |
| 27 | 27 |
| 28 using google::protobuf::Split; | 28 using google::protobuf::Split; |
| 29 using google::protobuf::StripPrefixString; | 29 using google::protobuf::StripPrefixString; |
| 30 using google::protobuf::StripString; | 30 using google::protobuf::StripString; |
| 31 using google::protobuf::StripSuffixString; | 31 using google::protobuf::StripSuffixString; |
| 32 using google::protobuf::UpperString; | 32 using google::protobuf::UpperString; |
| 33 | 33 |
| 34 namespace { | 34 namespace { |
| 35 | 35 |
| 36 inline std::string ProtoStubName(const FileDescriptor* proto) { | |
| 37 return StripSuffixString(proto->name(), ".proto") + ".pbzero"; | |
| 38 } | |
| 39 | |
| 36 class GeneratorJob { | 40 class GeneratorJob { |
| 37 public: | 41 public: |
| 38 GeneratorJob(const FileDescriptor *file, | 42 GeneratorJob(const FileDescriptor *file, |
| 39 Printer* stub_h_printer, | 43 Printer* stub_h_printer, |
| 40 Printer* stub_cc_printer) | 44 Printer* stub_cc_printer) |
| 41 : source_(file), | 45 : source_(file), |
| 42 stub_h_(stub_h_printer), | 46 stub_h_(stub_h_printer), |
| 43 stub_cc_(stub_cc_printer) {} | 47 stub_cc_(stub_cc_printer) {} |
| 44 | 48 |
| 45 bool GenerateStubs() { | 49 bool GenerateStubs() { |
| (...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 144 std::string guard = package_ + "_" + source_->name() + "_H_"; | 148 std::string guard = package_ + "_" + source_->name() + "_H_"; |
| 145 UpperString(&guard); | 149 UpperString(&guard); |
| 146 StripString(&guard, ".-", '_'); | 150 StripString(&guard, ".-", '_'); |
| 147 | 151 |
| 148 stub_h_->Print( | 152 stub_h_->Print( |
| 149 "$greeting$\n" | 153 "$greeting$\n" |
| 150 "#ifndef $guard$\n" | 154 "#ifndef $guard$\n" |
| 151 "#define $guard$\n\n" | 155 "#define $guard$\n\n" |
| 152 "#include <stddef.h>\n" | 156 "#include <stddef.h>\n" |
| 153 "#include <stdint.h>\n\n" | 157 "#include <stdint.h>\n\n" |
| 154 "#include \"components/tracing/core/proto_zero_message.h\"\n\n", | 158 "#include \"components/tracing/core/proto_zero_message.h\"\n", |
| 155 "greeting", greeting, | 159 "greeting", greeting, |
| 156 "guard", guard); | 160 "guard", guard); |
| 157 stub_cc_->Print( | 161 stub_cc_->Print( |
| 158 "$greeting$\n" | 162 "$greeting$\n" |
| 159 "// This file intentionally left blank.\n", | 163 "// This file intentionally left blank.\n", |
| 160 "greeting", greeting); | 164 "greeting", greeting); |
| 161 | 165 |
| 166 // Print dependencies. | |
| 167 if (source_->weak_dependency_count() > 0) | |
| 168 Abort("Weak imports are not supported."); | |
| 169 for (int i = 0; i < source_->dependency_count(); ++i) { | |
| 170 const FileDescriptor* dependency = source_->dependency(i); | |
| 171 if (dependency->package() != package_) | |
| 172 Abort("Imported proto must be in the same package."); | |
|
Primiano Tucci (use gerrit)
2016/08/03 15:36:28
out of curiosity, why?
kraynov
2016/08/04 12:03:54
It would be complex to deal with namespaces in thi
| |
| 173 | |
| 174 // Dependency name could contatin slashes but importing from upper-level | |
| 175 // directories is not possible anyway since build system process each | |
| 176 // proto file individually. Hence proto lookup path always equal to the | |
| 177 // directory where particular proto file is located and protoc does not | |
| 178 // allow reference to upper directory (aka ..) in import path. | |
| 179 // | |
| 180 // Laconiacally said: | |
|
Primiano Tucci (use gerrit)
2016/08/03 15:36:28
nit: s/Laconiacally/Laconically/
| |
| 181 // - source_->name() may never have slashes, | |
| 182 // - dependency->name() may have slashes but always reffers to inner path. | |
| 183 stub_h_->Print( | |
| 184 "#include \"$name$.h\"\n", | |
| 185 "name", ProtoStubName(dependency)); | |
| 186 } | |
| 187 stub_h_->Print("\n"); | |
| 188 | |
| 162 // Print namespaces. | 189 // Print namespaces. |
| 163 for (const std::string& ns : namespaces_) | 190 for (const std::string& ns : namespaces_) |
| 164 stub_h_->Print("namespace $ns$ {\n", "ns", ns); | 191 stub_h_->Print("namespace $ns$ {\n", "ns", ns); |
| 165 stub_h_->Print("\n"); | 192 stub_h_->Print("\n"); |
| 166 // Print forward declarations. | 193 // Print forward declarations. |
| 167 for (const Descriptor* message : messages_) { | 194 for (const Descriptor* message : messages_) { |
| 168 stub_h_->Print( | 195 stub_h_->Print( |
| 169 "class $class$;\n", | 196 "class $class$;\n", |
| 170 "class", GetCppClassName(message)); | 197 "class", GetCppClassName(message)); |
| 171 } | 198 } |
| (...skipping 223 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 395 } | 422 } |
| 396 | 423 |
| 397 ProtoZeroGenerator::~ProtoZeroGenerator() { | 424 ProtoZeroGenerator::~ProtoZeroGenerator() { |
| 398 } | 425 } |
| 399 | 426 |
| 400 bool ProtoZeroGenerator::Generate(const FileDescriptor* file, | 427 bool ProtoZeroGenerator::Generate(const FileDescriptor* file, |
| 401 const std::string& options, | 428 const std::string& options, |
| 402 GeneratorContext* context, | 429 GeneratorContext* context, |
| 403 std::string* error) const { | 430 std::string* error) const { |
| 404 | 431 |
| 405 const std::string proto_stubs_name = | |
| 406 StripSuffixString(file->name(), ".proto") + ".pbzero"; | |
| 407 | |
| 408 const std::unique_ptr<ZeroCopyOutputStream> stub_h_file_stream( | 432 const std::unique_ptr<ZeroCopyOutputStream> stub_h_file_stream( |
| 409 context->Open(proto_stubs_name + ".h")); | 433 context->Open(ProtoStubName(file) + ".h")); |
| 410 const std::unique_ptr<ZeroCopyOutputStream> stub_cc_file_stream( | 434 const std::unique_ptr<ZeroCopyOutputStream> stub_cc_file_stream( |
| 411 context->Open(proto_stubs_name + ".cc")); | 435 context->Open(ProtoStubName(file) + ".cc")); |
| 412 | 436 |
| 413 // Variables are delimited by $. | 437 // Variables are delimited by $. |
| 414 Printer stub_h_printer(stub_h_file_stream.get(), '$'); | 438 Printer stub_h_printer(stub_h_file_stream.get(), '$'); |
| 415 Printer stub_cc_printer(stub_cc_file_stream.get(), '$'); | 439 Printer stub_cc_printer(stub_cc_file_stream.get(), '$'); |
| 416 | 440 |
| 417 GeneratorJob job(file, &stub_h_printer, &stub_cc_printer); | 441 GeneratorJob job(file, &stub_h_printer, &stub_cc_printer); |
| 418 if (!job.GenerateStubs()) { | 442 if (!job.GenerateStubs()) { |
| 419 *error = job.GetFirstError(); | 443 *error = job.GetFirstError(); |
| 420 return false; | 444 return false; |
| 421 } | 445 } |
| 422 return true; | 446 return true; |
| 423 } | 447 } |
| 424 | 448 |
| 425 } // namespace proto | 449 } // namespace proto |
| 426 } // namespace tracing | 450 } // namespace tracing |
| OLD | NEW |