cpp_generator.cc
Go to the documentation of this file.
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 
36 
37 #include <memory>
38 #include <utility>
39 #include <vector>
40 
47 
48 
49 
50 namespace google {
51 namespace protobuf {
52 namespace compiler {
53 namespace cpp {
54 
57 
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 
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") {
98  } else if (options[i].first == "lite") {
100  } else if (options[i].first == "lite_implicit_weak_fields") {
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;
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;
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
zero_copy_stream.h
google::protobuf::compiler::cpp::EnforceOptimizeMode::kLiteRuntime
@ kLiteRuntime
google::protobuf::compiler::cpp::Options::bootstrap
bool bootstrap
Definition: cpp_options.h:62
NULL
NULL
Definition: test_security_zap.cpp:405
google::protobuf::compiler::cpp::FileGenerator::GenerateSource
void GenerateSource(io::Printer *printer)
Definition: cpp_file.cc:629
google::protobuf::StrCat
string StrCat(const AlphaNum &a, const AlphaNum &b)
Definition: strutil.cc:1480
google::protobuf::compiler::cpp::StripProto
std::string StripProto(const std::string &filename)
Definition: cpp_helpers.cc:474
options
Message * options
Definition: src/google/protobuf/descriptor.cc:3119
google::protobuf::compiler::GeneratorContext::Open
virtual io::ZeroCopyOutputStream * Open(const std::string &filename)=0
google::protobuf::compiler::cpp::CppGenerator::runtime_include_base_
std::string runtime_include_base_
Definition: cpp_generator.h:88
string
GLsizei const GLchar *const * string
Definition: glcorearb.h:3083
cpp_generator.h
google::protobuf::compiler::cpp::Options::annotate_headers
bool annotate_headers
Definition: cpp_options.h:57
error
Definition: cJSON.c:88
cpp_helpers.h
google::protobuf::compiler::cpp::FileGenerator::GeneratePBHeader
void GeneratePBHeader(io::Printer *printer, const std::string &info_path)
Definition: cpp_file.cc:283
google::protobuf::compiler::cpp::Options::safe_boundary_check
bool safe_boundary_check
Definition: cpp_options.h:54
google::protobuf::compiler::cpp::UsingImplicitWeakFields
bool UsingImplicitWeakFields(const FileDescriptor *file, const Options &options)
Definition: cpp_helpers.cc:1118
strutil.h
google::protobuf::compiler::cpp::Options::lite_implicit_weak_fields
bool lite_implicit_weak_fields
Definition: cpp_options.h:61
google::protobuf::compiler::cpp::Options::proto_h
bool proto_h
Definition: cpp_options.h:55
printer.h
google::protobuf::compiler::cpp::Options::table_driven_serialization
bool table_driven_serialization
Definition: cpp_options.h:60
cpp
Definition: third_party/googletest/googlemock/scripts/generator/cpp/__init__.py:1
google::protobuf::compiler::cpp::Options
Definition: cpp_options.h:52
cpp_file.h
GOOGLE_CHECK_LE
#define GOOGLE_CHECK_LE(A, B)
Definition: logging.h:159
google::protobuf::compiler::cpp::CppGenerator::Generate
bool Generate(const FileDescriptor *file, const std::string &parameter, GeneratorContext *generator_context, std::string *error) const
Definition: cpp_generator.cc:58
google::protobuf::io::Printer
Definition: printer.h:181
google::protobuf::compiler::cpp::CppGenerator::CppGenerator
CppGenerator()
Definition: cpp_generator.cc:55
google::protobuf::compiler::cpp::Options::opensource_runtime
bool opensource_runtime
Definition: cpp_options.h:63
google::protobuf::compiler::cpp::Options::num_cc_files
int num_cc_files
Definition: cpp_options.h:65
i
int i
Definition: gmock-matchers_test.cc:764
google::protobuf::FileDescriptor::name
const std::string & name() const
google::protobuf::compiler::cpp::FileGenerator
Definition: cpp_file.h:68
google::protobuf::compiler::cpp::Options::annotation_guard_name
std::string annotation_guard_name
Definition: cpp_options.h:67
google::protobuf::strto32
int32 strto32(const char *nptr, char **endptr, int base)
Definition: strutil.h:358
google::protobuf::compiler::cpp::FileGenerator::GenerateGlobalSource
void GenerateGlobalSource(io::Printer *printer)
Definition: cpp_file.cc:592
google::protobuf::compiler::cpp::FileGenerator::GenerateProtoHeader
void GenerateProtoHeader(io::Printer *printer, const std::string &info_path)
Definition: cpp_file.cc:245
google::protobuf::compiler::ParseGeneratorParameter
void ParseGeneratorParameter(const std::string &text, std::vector< std::pair< std::string, std::string > > *output)
Definition: code_generator.cc:101
google::protobuf::compiler::cpp::EnforceOptimizeMode::kSpeed
@ kSpeed
google::protobuf::compiler::cpp::Options::enforce_mode
EnforceOptimizeMode enforce_mode
Definition: cpp_options.h:58
first
GLint first
Definition: glcorearb.h:2830
google::protobuf::compiler::cpp::FileGenerator::GenerateSourceForMessage
void GenerateSourceForMessage(int idx, io::Printer *printer)
Definition: cpp_file.cc:542
google::protobuf::FileDescriptor
Definition: src/google/protobuf/descriptor.h:1320
google::protobuf::io::AnnotationProtoCollector
Definition: printer.h:76
google::protobuf::compiler::cpp::Options::annotation_pragma_name
std::string annotation_pragma_name
Definition: cpp_options.h:66
google::protobuf::compiler::cpp::Options::table_driven_parsing
bool table_driven_parsing
Definition: cpp_options.h:59
google::protobuf::compiler::cpp::Options::dllexport_decl
std::string dllexport_decl
Definition: cpp_options.h:53
descriptor.pb.h
output
const upb_json_parsermethod const upb_symtab upb_sink * output
Definition: ruby/ext/google/protobuf_c/upb.h:10503
GeneratedCodeInfo
Definition: descriptor.pb.h:6355
google::protobuf::compiler::cpp::CppGenerator::~CppGenerator
~CppGenerator()
Definition: cpp_generator.cc:56
google::protobuf::compiler::GeneratorContext
Definition: code_generator.h:119
google::protobuf::compiler::cpp::CppGenerator::opensource_runtime_
bool opensource_runtime_
Definition: cpp_generator.h:87
compiler
Definition: plugin.pb.cc:22
google
Definition: data_proto2_to_proto3_util.h:11
google::protobuf::compiler::cpp::Options::runtime_include_base
std::string runtime_include_base
Definition: cpp_options.h:64
google::protobuf::compiler::cpp::FileGenerator::NumMessages
int NumMessages() const
Definition: cpp_file.h:85
google::protobuf::compiler::cpp::MaybeBootstrap
bool MaybeBootstrap(const Options &options, GeneratorContext *generator_context, bool bootstrap_flag, std::string *basename)
Definition: cpp_helpers.cc:1267


libaditof
Author(s):
autogenerated on Wed May 21 2025 02:06:49