• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
31 // Author: kenton@google.com (Kenton Varda)
32 //  Based on original Protocol Buffers design by
33 //  Sanjay Ghemawat, Jeff Dean, and others.
34 
35 #include <google/protobuf/compiler/cpp/cpp_generator.h>
36 
37 #include <memory>
38 #include <utility>
39 #include <vector>
40 
41 #include <google/protobuf/stubs/strutil.h>
42 #include <google/protobuf/compiler/cpp/cpp_file.h>
43 #include <google/protobuf/compiler/cpp/cpp_helpers.h>
44 #include <google/protobuf/descriptor.pb.h>
45 #include <google/protobuf/io/printer.h>
46 #include <google/protobuf/io/zero_copy_stream.h>
47 
48 
49 
50 namespace google {
51 namespace protobuf {
52 namespace compiler {
53 namespace cpp {
54 
CppGenerator()55 CppGenerator::CppGenerator() {}
~CppGenerator()56 CppGenerator::~CppGenerator() {}
57 
Generate(const FileDescriptor * file,const std::string & parameter,GeneratorContext * generator_context,std::string * error) const58 bool CppGenerator::Generate(const FileDescriptor* file,
59                             const std::string& parameter,
60                             GeneratorContext* generator_context,
61                             std::string* error) const {
62   std::vector<std::pair<std::string, std::string> > options;
63   ParseGeneratorParameter(parameter, &options);
64 
65   // -----------------------------------------------------------------
66   // parse generator options
67 
68   // If the dllexport_decl option is passed to the compiler, we need to write
69   // it in front of every symbol that should be exported if this .proto is
70   // compiled into a Windows DLL.  E.g., if the user invokes the protocol
71   // compiler as:
72   //   protoc --cpp_out=dllexport_decl=FOO_EXPORT:outdir foo.proto
73   // then we'll define classes like this:
74   //   class FOO_EXPORT Foo {
75   //     ...
76   //   }
77   // FOO_EXPORT is a macro which should expand to __declspec(dllexport) or
78   // __declspec(dllimport) depending on what is being compiled.
79   //
80   Options file_options;
81 
82   file_options.opensource_runtime = opensource_runtime_;
83   file_options.runtime_include_base = runtime_include_base_;
84 
85   for (int i = 0; i < options.size(); i++) {
86     if (options[i].first == "dllexport_decl") {
87       file_options.dllexport_decl = options[i].second;
88     } else if (options[i].first == "safe_boundary_check") {
89       file_options.safe_boundary_check = true;
90     } else if (options[i].first == "annotate_headers") {
91       file_options.annotate_headers = true;
92     } else if (options[i].first == "annotation_pragma_name") {
93       file_options.annotation_pragma_name = options[i].second;
94     } else if (options[i].first == "annotation_guard_name") {
95       file_options.annotation_guard_name = options[i].second;
96     } else if (options[i].first == "speed") {
97       file_options.enforce_mode = EnforceOptimizeMode::kSpeed;
98     } else if (options[i].first == "lite") {
99       file_options.enforce_mode = EnforceOptimizeMode::kLiteRuntime;
100     } else if (options[i].first == "lite_implicit_weak_fields") {
101       file_options.enforce_mode = EnforceOptimizeMode::kLiteRuntime;
102       file_options.lite_implicit_weak_fields = true;
103       if (!options[i].second.empty()) {
104         file_options.num_cc_files =
105             strto32(options[i].second.c_str(), NULL, 10);
106       }
107     } else if (options[i].first == "table_driven_parsing") {
108       file_options.table_driven_parsing = true;
109     } else if (options[i].first == "table_driven_serialization") {
110       file_options.table_driven_serialization = true;
111     } else {
112       *error = "Unknown generator option: " + options[i].first;
113       return false;
114     }
115   }
116 
117   // The safe_boundary_check option controls behavior for Google-internal
118   // protobuf APIs.
119   if (file_options.safe_boundary_check && file_options.opensource_runtime) {
120     *error =
121         "The safe_boundary_check option is not supported outside of Google.";
122     return false;
123   }
124 
125   // -----------------------------------------------------------------
126 
127 
128   std::string basename = StripProto(file->name());
129 
130   if (MaybeBootstrap(file_options, generator_context, file_options.bootstrap,
131                      &basename)) {
132     return true;
133   }
134 
135   FileGenerator file_generator(file, file_options);
136 
137   // Generate header(s).
138   if (file_options.proto_h) {
139     std::unique_ptr<io::ZeroCopyOutputStream> output(
140         generator_context->Open(basename + ".proto.h"));
141     GeneratedCodeInfo annotations;
142     io::AnnotationProtoCollector<GeneratedCodeInfo> annotation_collector(
143         &annotations);
144     std::string info_path = basename + ".proto.h.meta";
145     io::Printer printer(
146         output.get(), '$',
147         file_options.annotate_headers ? &annotation_collector : NULL);
148     file_generator.GenerateProtoHeader(
149         &printer, file_options.annotate_headers ? info_path : "");
150     if (file_options.annotate_headers) {
151       std::unique_ptr<io::ZeroCopyOutputStream> info_output(
152           generator_context->Open(info_path));
153       annotations.SerializeToZeroCopyStream(info_output.get());
154     }
155   }
156 
157   {
158     std::unique_ptr<io::ZeroCopyOutputStream> output(
159         generator_context->Open(basename + ".pb.h"));
160     GeneratedCodeInfo annotations;
161     io::AnnotationProtoCollector<GeneratedCodeInfo> annotation_collector(
162         &annotations);
163     std::string info_path = basename + ".pb.h.meta";
164     io::Printer printer(
165         output.get(), '$',
166         file_options.annotate_headers ? &annotation_collector : NULL);
167     file_generator.GeneratePBHeader(
168         &printer, file_options.annotate_headers ? info_path : "");
169     if (file_options.annotate_headers) {
170       std::unique_ptr<io::ZeroCopyOutputStream> info_output(
171           generator_context->Open(info_path));
172       annotations.SerializeToZeroCopyStream(info_output.get());
173     }
174   }
175 
176   // Generate cc file(s).
177   if (UsingImplicitWeakFields(file, file_options)) {
178     {
179       // This is the global .cc file, containing
180       // enum/services/tables/reflection
181       std::unique_ptr<io::ZeroCopyOutputStream> output(
182           generator_context->Open(basename + ".pb.cc"));
183       io::Printer printer(output.get(), '$');
184       file_generator.GenerateGlobalSource(&printer);
185     }
186 
187     int num_cc_files = file_generator.NumMessages();
188 
189     // If we're using implicit weak fields then we allow the user to
190     // optionally specify how many files to generate, not counting the global
191     // pb.cc file. If we have more files than messages, then some files will
192     // be generated as empty placeholders.
193     if (file_options.num_cc_files > 0) {
194       GOOGLE_CHECK_LE(file_generator.NumMessages(), file_options.num_cc_files)
195           << "There must be at least as many numbered .cc files as messages.";
196       num_cc_files = file_options.num_cc_files;
197     }
198     for (int i = 0; i < num_cc_files; i++) {
199       std::unique_ptr<io::ZeroCopyOutputStream> output(
200           generator_context->Open(StrCat(basename, ".out/", i, ".cc")));
201       io::Printer printer(output.get(), '$');
202       if (i < file_generator.NumMessages()) {
203         file_generator.GenerateSourceForMessage(i, &printer);
204       }
205     }
206   } else {
207     std::unique_ptr<io::ZeroCopyOutputStream> output(
208         generator_context->Open(basename + ".pb.cc"));
209     io::Printer printer(output.get(), '$');
210     file_generator.GenerateSource(&printer);
211   }
212 
213   return true;
214 }
215 
216 }  // namespace cpp
217 }  // namespace compiler
218 }  // namespace protobuf
219 }  // namespace google
220