cpp_generator.cc 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. // Protocol Buffers - Google's data interchange format
  2. // Copyright 2008 Google Inc. All rights reserved.
  3. // https://developers.google.com/protocol-buffers/
  4. //
  5. // Redistribution and use in source and binary forms, with or without
  6. // modification, are permitted provided that the following conditions are
  7. // met:
  8. //
  9. // * Redistributions of source code must retain the above copyright
  10. // notice, this list of conditions and the following disclaimer.
  11. // * Redistributions in binary form must reproduce the above
  12. // copyright notice, this list of conditions and the following disclaimer
  13. // in the documentation and/or other materials provided with the
  14. // distribution.
  15. // * Neither the name of Google Inc. nor the names of its
  16. // contributors may be used to endorse or promote products derived from
  17. // this software without specific prior written permission.
  18. //
  19. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  20. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  21. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  22. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  23. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  24. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  25. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  26. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  27. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  28. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  29. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  30. // Author: kenton@google.com (Kenton Varda)
  31. // Based on original Protocol Buffers design by
  32. // Sanjay Ghemawat, Jeff Dean, and others.
  33. #include <google/protobuf/compiler/cpp/cpp_generator.h>
  34. #include <vector>
  35. #include <memory>
  36. #include <utility>
  37. #include <google/protobuf/stubs/strutil.h>
  38. #include <google/protobuf/compiler/cpp/cpp_file.h>
  39. #include <google/protobuf/compiler/cpp/cpp_helpers.h>
  40. #include <google/protobuf/descriptor.pb.h>
  41. #include <google/protobuf/io/printer.h>
  42. #include <google/protobuf/io/zero_copy_stream.h>
  43. namespace google {
  44. namespace protobuf {
  45. namespace compiler {
  46. namespace cpp {
  47. CppGenerator::CppGenerator() {}
  48. CppGenerator::~CppGenerator() {}
  49. bool CppGenerator::Generate(const FileDescriptor* file,
  50. const string& parameter,
  51. GeneratorContext* generator_context,
  52. string* error) const {
  53. std::vector<std::pair<string, string> > options;
  54. ParseGeneratorParameter(parameter, &options);
  55. // -----------------------------------------------------------------
  56. // parse generator options
  57. // If the dllexport_decl option is passed to the compiler, we need to write
  58. // it in front of every symbol that should be exported if this .proto is
  59. // compiled into a Windows DLL. E.g., if the user invokes the protocol
  60. // compiler as:
  61. // protoc --cpp_out=dllexport_decl=FOO_EXPORT:outdir foo.proto
  62. // then we'll define classes like this:
  63. // class FOO_EXPORT Foo {
  64. // ...
  65. // }
  66. // FOO_EXPORT is a macro which should expand to __declspec(dllexport) or
  67. // __declspec(dllimport) depending on what is being compiled.
  68. //
  69. Options file_options;
  70. for (int i = 0; i < options.size(); i++) {
  71. if (options[i].first == "dllexport_decl") {
  72. file_options.dllexport_decl = options[i].second;
  73. } else if (options[i].first == "safe_boundary_check") {
  74. file_options.safe_boundary_check = true;
  75. } else if (options[i].first == "annotate_headers") {
  76. file_options.annotate_headers = true;
  77. } else if (options[i].first == "annotation_pragma_name") {
  78. file_options.annotation_pragma_name = options[i].second;
  79. } else if (options[i].first == "annotation_guard_name") {
  80. file_options.annotation_guard_name = options[i].second;
  81. } else if (options[i].first == "lite") {
  82. file_options.enforce_lite = true;
  83. } else if (options[i].first == "lite_implicit_weak_fields") {
  84. file_options.lite_implicit_weak_fields = true;
  85. if (!options[i].second.empty()) {
  86. file_options.num_cc_files = strto32(options[i].second.c_str(),
  87. NULL, 10);
  88. }
  89. } else if (options[i].first == "table_driven_parsing") {
  90. file_options.table_driven_parsing = true;
  91. } else if (options[i].first == "table_driven_serialization") {
  92. file_options.table_driven_serialization = true;
  93. } else {
  94. *error = "Unknown generator option: " + options[i].first;
  95. return false;
  96. }
  97. }
  98. // The safe_boundary_check option controls behavior for Google-internal
  99. // protobuf APIs.
  100. if (file_options.safe_boundary_check) {
  101. *error =
  102. "The safe_boundary_check option is not supported outside of Google.";
  103. return false;
  104. }
  105. // -----------------------------------------------------------------
  106. string basename = StripProto(file->name());
  107. FileGenerator file_generator(file, file_options);
  108. // Generate header(s).
  109. if (file_options.proto_h) {
  110. std::unique_ptr<io::ZeroCopyOutputStream> output(
  111. generator_context->Open(basename + ".proto.h"));
  112. GeneratedCodeInfo annotations;
  113. io::AnnotationProtoCollector<GeneratedCodeInfo> annotation_collector(
  114. &annotations);
  115. string info_path = basename + ".proto.h.meta";
  116. io::Printer printer(output.get(), '$', file_options.annotate_headers
  117. ? &annotation_collector
  118. : NULL);
  119. file_generator.GenerateProtoHeader(
  120. &printer, file_options.annotate_headers ? info_path : "");
  121. if (file_options.annotate_headers) {
  122. std::unique_ptr<io::ZeroCopyOutputStream> info_output(
  123. generator_context->Open(info_path));
  124. annotations.SerializeToZeroCopyStream(info_output.get());
  125. }
  126. }
  127. {
  128. std::unique_ptr<io::ZeroCopyOutputStream> output(
  129. generator_context->Open(basename + ".pb.h"));
  130. GeneratedCodeInfo annotations;
  131. io::AnnotationProtoCollector<GeneratedCodeInfo> annotation_collector(
  132. &annotations);
  133. string info_path = basename + ".pb.h.meta";
  134. io::Printer printer(output.get(), '$', file_options.annotate_headers
  135. ? &annotation_collector
  136. : NULL);
  137. file_generator.GeneratePBHeader(
  138. &printer, file_options.annotate_headers ? info_path : "");
  139. if (file_options.annotate_headers) {
  140. std::unique_ptr<io::ZeroCopyOutputStream> info_output(
  141. generator_context->Open(info_path));
  142. annotations.SerializeToZeroCopyStream(info_output.get());
  143. }
  144. }
  145. // Generate cc file(s).
  146. if (UsingImplicitWeakFields(file, file_options)) {
  147. {
  148. // This is the global .cc file, containing enum/services/tables/reflection
  149. std::unique_ptr<io::ZeroCopyOutputStream> output(
  150. generator_context->Open(basename + ".pb.cc"));
  151. io::Printer printer(output.get(), '$');
  152. file_generator.GenerateGlobalSource(&printer);
  153. }
  154. int num_cc_files = file_generator.NumMessages();
  155. // If we're using implicit weak fields then we allow the user to optionally
  156. // specify how many files to generate, not counting the global pb.cc file.
  157. // If we have more files than messages, then some files will be generated as
  158. // empty placeholders.
  159. if (file_options.num_cc_files > 0) {
  160. GOOGLE_CHECK_LE(file_generator.NumMessages(), file_options.num_cc_files)
  161. << "There must be at least as many numbered .cc files as messages.";
  162. num_cc_files = file_options.num_cc_files;
  163. }
  164. for (int i = 0; i < num_cc_files; i++) {
  165. // TODO(gerbens) Agree on naming scheme.
  166. std::unique_ptr<io::ZeroCopyOutputStream> output(
  167. generator_context->Open(basename + "." + SimpleItoa(i) + ".cc"));
  168. io::Printer printer(output.get(), '$');
  169. if (i < file_generator.NumMessages()) {
  170. file_generator.GenerateSourceForMessage(i, &printer);
  171. }
  172. }
  173. } else {
  174. std::unique_ptr<io::ZeroCopyOutputStream> output(
  175. generator_context->Open(basename + ".pb.cc"));
  176. io::Printer printer(output.get(), '$');
  177. file_generator.GenerateSource(&printer);
  178. }
  179. return true;
  180. }
  181. } // namespace cpp
  182. } // namespace compiler
  183. } // namespace protobuf
  184. } // namespace google