protobuf/src/google/protobuf/compiler/cpp/cpp_file.h
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 
35 #ifndef GOOGLE_PROTOBUF_COMPILER_CPP_FILE_H__
36 #define GOOGLE_PROTOBUF_COMPILER_CPP_FILE_H__
37 
38 #include <algorithm>
39 #include <memory>
40 #include <set>
41 #include <string>
42 #include <vector>
43 #include <google/protobuf/stubs/common.h>
44 #include <google/protobuf/compiler/cpp/cpp_field.h>
45 #include <google/protobuf/compiler/cpp/cpp_helpers.h>
46 #include <google/protobuf/compiler/cpp/cpp_options.h>
47 #include <google/protobuf/compiler/scc.h>
48 
49 namespace google {
50 namespace protobuf {
51 class FileDescriptor; // descriptor.h
52 namespace io {
53 class Printer; // printer.h
54 }
55 } // namespace protobuf
56 } // namespace google
57 
58 namespace google {
59 namespace protobuf {
60 namespace compiler {
61 namespace cpp {
62 
63 class EnumGenerator; // enum.h
64 class MessageGenerator; // message.h
65 class ServiceGenerator; // service.h
66 class ExtensionGenerator; // extension.h
67 
68 class FileGenerator {
69  public:
70  // See generator.cc for the meaning of dllexport_decl.
71  FileGenerator(const FileDescriptor* file, const Options& options);
73 
74  // Shared code between the two header generators below.
75  void GenerateHeader(io::Printer* printer);
76 
77  // info_path, if non-empty, should be the path (relative to printer's
78  // output) to the metadata file describing this proto header.
79  void GenerateProtoHeader(io::Printer* printer, const std::string& info_path);
80  // info_path, if non-empty, should be the path (relative to printer's
81  // output) to the metadata file describing this PB header.
82  void GeneratePBHeader(io::Printer* printer, const std::string& info_path);
83  void GenerateSource(io::Printer* printer);
84 
85  // The following member functions are used when the lite_implicit_weak_fields
86  // option is set. In this mode the code is organized a bit differently to
87  // promote better linker stripping of unused code. In particular, we generate
88  // one .cc file per message, one .cc file per extension, and a main pb.cc file
89  // containing everything else.
90 
91  int NumMessages() const { return message_generators_.size(); }
92  int NumExtensions() const { return extension_generators_.size(); }
93  // Generates the source file for one message.
94  void GenerateSourceForMessage(int idx, io::Printer* printer);
95  // Generates the source file for one extension.
96  void GenerateSourceForExtension(int idx, io::Printer* printer);
97  // Generates a source file containing everything except messages and
98  // extensions.
99  void GenerateGlobalSource(io::Printer* printer);
100 
101  private:
102  // Internal type used by GenerateForwardDeclarations (defined in file.cc).
103  class ForwardDeclarations;
104  struct CrossFileReferences;
105 
106  void IncludeFile(const std::string& google3_name, io::Printer* printer) {
107  DoIncludeFile(google3_name, false, printer);
108  }
109  void IncludeFileAndExport(const std::string& google3_name,
110  io::Printer* printer) {
111  DoIncludeFile(google3_name, true, printer);
112  }
113  void DoIncludeFile(const std::string& google3_name, bool do_export,
114  io::Printer* printer);
115 
117  const FileDescriptor* file);
119  CrossFileReferences* refs);
121  CrossFileReferences* refs);
122  void GenerateInternalForwardDeclarations(const CrossFileReferences& refs,
123  io::Printer* printer);
124  void GenerateSourceIncludes(io::Printer* printer);
125  void GenerateSourceDefaultInstance(int idx, io::Printer* printer);
126 
127  void GenerateInitForSCC(const SCC* scc, const CrossFileReferences& refs,
128  io::Printer* printer);
129  void GenerateTables(io::Printer* printer);
131 
132  // For other imports, generates their forward-declarations.
134 
135  // Generates top or bottom of a header file.
136  void GenerateTopHeaderGuard(io::Printer* printer, bool pb_h);
137  void GenerateBottomHeaderGuard(io::Printer* printer, bool pb_h);
138 
139  // Generates #include directives.
140  void GenerateLibraryIncludes(io::Printer* printer);
142 
143  // Generate a pragma to pull in metadata using the given info_path (if
144  // non-empty). info_path should be relative to printer's output.
145  void GenerateMetadataPragma(io::Printer* printer,
146  const std::string& info_path);
147 
148  // Generates a couple of different pieces before definitions:
150 
151  // Generates types for classes.
153 
154  void GenerateEnumDefinitions(io::Printer* printer);
155 
156  // Generates generic service definitions.
158 
159  // Generates extension identifiers.
161 
162  // Generates inline function definitions.
164 
166 
167  // Sometimes the names we use in a .proto file happen to be defined as
168  // macros on some platforms (e.g., macro/minor used in plugin.proto are
169  // defined as macros in sys/types.h on FreeBSD and a few other platforms).
170  // To make the generated code compile on these platforms, we either have to
171  // undef the macro for these few platforms, or rename the field name for all
172  // platforms. Since these names are part of protobuf public API, renaming is
173  // generally a breaking change so we prefer the #undef approach.
174  void GenerateMacroUndefs(io::Printer* printer);
175 
176  bool IsDepWeak(const FileDescriptor* dep) const {
177  if (weak_deps_.count(dep) != 0) {
179  return true;
180  }
181  return false;
182  }
183 
184  std::set<const FileDescriptor*> weak_deps_;
185 
186  const FileDescriptor* file_;
187  const Options options_;
188 
190 
191  std::map<std::string, std::string> variables_;
192 
193  // Contains the post-order walk of all the messages (and child messages) in
194  // this file. If you need a pre-order walk just reverse iterate.
195  std::vector<std::unique_ptr<MessageGenerator>> message_generators_;
196  std::vector<std::unique_ptr<EnumGenerator>> enum_generators_;
197  std::vector<std::unique_ptr<ServiceGenerator>> service_generators_;
198  std::vector<std::unique_ptr<ExtensionGenerator>> extension_generators_;
199 
201 };
202 
203 } // namespace cpp
204 } // namespace compiler
205 } // namespace protobuf
206 } // namespace google
207 
208 #endif // GOOGLE_PROTOBUF_COMPILER_CPP_FILE_H__
google::protobuf::compiler::cpp::FileGenerator::scc_analyzer_
MessageSCCAnalyzer scc_analyzer_
Definition: bloaty/third_party/protobuf/src/google/protobuf/compiler/cpp/cpp_file.h:187
google::protobuf::FieldDescriptor
Definition: bloaty/third_party/protobuf/src/google/protobuf/descriptor.h:515
google::protobuf::compiler::SCC
Definition: bloaty/third_party/protobuf/src/google/protobuf/compiler/scc.h:49
google::protobuf::compiler::cpp::FileGenerator::GenerateSource
void GenerateSource(io::Printer *printer)
Definition: bloaty/third_party/protobuf/src/google/protobuf/compiler/cpp/cpp_file.cc:651
google::protobuf::compiler::cpp::FileGenerator::GenerateHeader
void GenerateHeader(io::Printer *printer)
Definition: bloaty/third_party/protobuf/src/google/protobuf/compiler/cpp/cpp_file.cc:182
google::protobuf::compiler::cpp::FileGenerator::GenerateServiceDefinitions
void GenerateServiceDefinitions(io::Printer *printer)
Definition: bloaty/third_party/protobuf/src/google/protobuf/compiler/cpp/cpp_file.cc:1453
options
double_dict options[]
Definition: capstone_test.c:55
google::protobuf::compiler::cpp::FileGenerator::message_generators_
std::vector< std::unique_ptr< MessageGenerator > > message_generators_
Definition: bloaty/third_party/protobuf/src/google/protobuf/compiler/cpp/cpp_file.h:193
FileDescriptor
Definition: bloaty/third_party/protobuf/ruby/ext/google/protobuf_c/protobuf.h:128
testing::internal::string
::std::string string
Definition: bloaty/third_party/protobuf/third_party/googletest/googletest/include/gtest/internal/gtest-port.h:881
google::protobuf::compiler::cpp::FileGenerator::DoIncludeFile
void DoIncludeFile(const std::string &google3_name, bool do_export, io::Printer *printer)
Definition: bloaty/third_party/protobuf/src/google/protobuf/compiler/cpp/cpp_file.cc:328
google::protobuf::compiler::cpp::FileGenerator::GenerateDependencyIncludes
void GenerateDependencyIncludes(io::Printer *printer)
Definition: bloaty/third_party/protobuf/src/google/protobuf/compiler/cpp/cpp_file.cc:1381
google::protobuf::compiler::cpp::FileGenerator::GenerateProto2NamespaceEnumSpecializations
void GenerateProto2NamespaceEnumSpecializations(io::Printer *printer)
Definition: bloaty/third_party/protobuf/src/google/protobuf/compiler/cpp/cpp_file.cc:1511
google::protobuf::compiler::cpp::FileGenerator::IncludeFile
void IncludeFile(const std::string &google3_name, io::Printer *printer)
Definition: protobuf/src/google/protobuf/compiler/cpp/cpp_file.h:106
google::protobuf::compiler::cpp::FileGenerator::extension_generators_
std::vector< std::unique_ptr< ExtensionGenerator > > extension_generators_
Definition: bloaty/third_party/protobuf/src/google/protobuf/compiler/cpp/cpp_file.h:196
file
Definition: bloaty/third_party/zlib/examples/gzappend.c:170
google::protobuf::compiler::cpp::FileGenerator::service_generators_
std::vector< std::unique_ptr< ServiceGenerator > > service_generators_
Definition: bloaty/third_party/protobuf/src/google/protobuf/compiler/cpp/cpp_file.h:195
grpc::protobuf::io::Printer
GRPC_CUSTOM_PRINTER Printer
Definition: src/compiler/config.h:54
google::protobuf
Definition: bloaty/third_party/protobuf/benchmarks/util/data_proto2_to_proto3_util.h:12
google::protobuf::compiler::cpp::FileGenerator::GenerateExtensionIdentifiers
void GenerateExtensionIdentifiers(io::Printer *printer)
Definition: bloaty/third_party/protobuf/src/google/protobuf/compiler/cpp/cpp_file.cc:1472
google::protobuf::compiler::cpp::FileGenerator::GenerateTables
void GenerateTables(io::Printer *printer)
Definition: bloaty/third_party/protobuf/src/google/protobuf/compiler/cpp/cpp_file.cc:1021
google::protobuf::compiler::cpp::FileGenerator::GenerateSourceDefaultInstance
void GenerateSourceDefaultInstance(int idx, io::Printer *printer)
Definition: bloaty/third_party/protobuf/src/google/protobuf/compiler/cpp/cpp_file.cc:438
google::protobuf::compiler::cpp::FileGenerator::GeneratePBHeader
void GeneratePBHeader(io::Printer *printer, const std::string &info_path)
Definition: bloaty/third_party/protobuf/src/google/protobuf/compiler/cpp/cpp_file.cc:283
google::protobuf::compiler::cpp::FileGenerator::file_
const FileDescriptor * file_
Definition: bloaty/third_party/protobuf/src/google/protobuf/compiler/cpp/cpp_file.h:184
refs
std::vector< CordRep * > refs
Definition: cordz_info_statistics_test.cc:81
google::protobuf::compiler::cpp::FileGenerator::GenerateMessageDefinitions
void GenerateMessageDefinitions(io::Printer *printer)
Definition: bloaty/third_party/protobuf/src/google/protobuf/compiler/cpp/cpp_file.cc:1433
google::protobuf::compiler::cpp::FileGenerator::GenerateMetadataPragma
void GenerateMetadataPragma(io::Printer *printer, const std::string &info_path)
Definition: bloaty/third_party/protobuf/src/google/protobuf/compiler/cpp/cpp_file.cc:1366
google::protobuf::compiler::cpp::FileGenerator::GenerateGlobalStateFunctionDeclarations
void GenerateGlobalStateFunctionDeclarations(io::Printer *printer)
Definition: bloaty/third_party/protobuf/src/google/protobuf/compiler/cpp/cpp_file.cc:1398
google::protobuf::compiler::cpp::FileGenerator::FileGenerator
FileGenerator(const FileDescriptor *file, const Options &options)
Definition: bloaty/third_party/protobuf/src/google/protobuf/compiler/cpp/cpp_file.cc:94
google::protobuf::compiler::cpp::FileGenerator::options_
const Options options_
Definition: bloaty/third_party/protobuf/src/google/protobuf/compiler/cpp/cpp_file.h:185
google::protobuf::compiler::cpp::FileGenerator::GenerateBottomHeaderGuard
void GenerateBottomHeaderGuard(io::Printer *printer, bool pb_h)
Definition: bloaty/third_party/protobuf/src/google/protobuf/compiler/cpp/cpp_file.cc:1251
google::protobuf::compiler::cpp::FileGenerator::CreateHeaderInclude
std::string CreateHeaderInclude(const std::string &basename, const FileDescriptor *file)
Definition: bloaty/third_party/protobuf/src/google/protobuf/compiler/cpp/cpp_file.cc:357
google::protobuf::compiler::cpp::FileGenerator::GenerateInlineFunctionDefinitions
void GenerateInlineFunctionDefinitions(io::Printer *printer)
Definition: bloaty/third_party/protobuf/src/google/protobuf/compiler/cpp/cpp_file.cc:1481
io
google::protobuf::compiler::cpp::FileGenerator::IsDepWeak
bool IsDepWeak(const FileDescriptor *dep) const
Definition: protobuf/src/google/protobuf/compiler/cpp/cpp_file.h:176
cpp
Definition: third_party/bloaty/third_party/googletest/googlemock/scripts/generator/cpp/__init__.py:1
google::protobuf::compiler::cpp::Options
Definition: bloaty/third_party/protobuf/src/google/protobuf/compiler/cpp/cpp_options.h:52
google::protobuf::compiler::cpp::FileGenerator::GenerateTopHeaderGuard
void GenerateTopHeaderGuard(io::Printer *printer, bool pb_h)
Definition: bloaty/third_party/protobuf/src/google/protobuf/compiler/cpp/cpp_file.cc:1231
setup.idx
idx
Definition: third_party/bloaty/third_party/capstone/bindings/python/setup.py:197
google::protobuf::io::Printer
Definition: bloaty/third_party/protobuf/src/google/protobuf/io/printer.h:181
google::protobuf::compiler::cpp::Options::opensource_runtime
bool opensource_runtime
Definition: bloaty/third_party/protobuf/src/google/protobuf/compiler/cpp/cpp_options.h:63
field
const FieldDescriptor * field
Definition: bloaty/third_party/protobuf/src/google/protobuf/compiler/parser_unittest.cc:2692
google::protobuf::compiler::cpp::FileGenerator
Definition: bloaty/third_party/protobuf/src/google/protobuf/compiler/cpp/cpp_file.h:68
google::protobuf::compiler::cpp::FileGenerator::enum_generators_
std::vector< std::unique_ptr< EnumGenerator > > enum_generators_
Definition: bloaty/third_party/protobuf/src/google/protobuf/compiler/cpp/cpp_file.h:194
google::protobuf::compiler::cpp::MessageSCCAnalyzer
Definition: bloaty/third_party/protobuf/src/google/protobuf/compiler/cpp/cpp_helpers.h:519
google::protobuf::compiler::cpp::FileGenerator::GenerateMacroUndefs
void GenerateMacroUndefs(io::Printer *printer)
Definition: bloaty/third_party/protobuf/src/google/protobuf/compiler/cpp/cpp_file.cc:151
google::protobuf::compiler::cpp::FileGenerator::NumExtensions
int NumExtensions() const
Definition: protobuf/src/google/protobuf/compiler/cpp/cpp_file.h:92
google::protobuf::compiler::cpp::FileGenerator::GetCrossFileReferencesForField
void GetCrossFileReferencesForField(const FieldDescriptor *field, CrossFileReferences *refs)
Definition: bloaty/third_party/protobuf/src/google/protobuf/compiler/cpp/cpp_file.cc:472
google::protobuf::compiler::cpp::FileGenerator::GenerateGlobalSource
void GenerateGlobalSource(io::Printer *printer)
Definition: bloaty/third_party/protobuf/src/google/protobuf/compiler/cpp/cpp_file.cc:614
google::protobuf::compiler::cpp::FileGenerator::GenerateProtoHeader
void GenerateProtoHeader(io::Printer *printer, const std::string &info_path)
Definition: bloaty/third_party/protobuf/src/google/protobuf/compiler/cpp/cpp_file.cc:245
google::protobuf::compiler::cpp::FileGenerator::variables_
std::map< std::string, std::string > variables_
Definition: bloaty/third_party/protobuf/src/google/protobuf/compiler/cpp/cpp_file.h:189
google::protobuf::compiler::cpp::FileGenerator::GenerateReflectionInitializationCode
void GenerateReflectionInitializationCode(io::Printer *printer)
Definition: bloaty/third_party/protobuf/src/google/protobuf/compiler/cpp/cpp_file.cc:734
GOOGLE_CHECK
#define GOOGLE_CHECK(EXPRESSION)
Definition: bloaty/third_party/protobuf/src/google/protobuf/stubs/logging.h:153
google::protobuf::compiler::cpp::FileGenerator::GenerateSourceForMessage
void GenerateSourceForMessage(int idx, io::Printer *printer)
Definition: bloaty/third_party/protobuf/src/google/protobuf/compiler/cpp/cpp_file.cc:567
google::protobuf::compiler::cpp::FileGenerator::weak_deps_
std::set< const FileDescriptor * > weak_deps_
Definition: bloaty/third_party/protobuf/src/google/protobuf/compiler/cpp/cpp_file.h:181
google::protobuf::FileDescriptor
Definition: bloaty/third_party/protobuf/src/google/protobuf/descriptor.h:1320
google::protobuf::compiler::cpp::FileGenerator::~FileGenerator
~FileGenerator()
google::protobuf::compiler::cpp::FileGenerator::GenerateForwardDeclarations
void GenerateForwardDeclarations(io::Printer *printer)
Definition: bloaty/third_party/protobuf/src/google/protobuf/compiler/cpp/cpp_file.cc:1180
google::protobuf::compiler::cpp::FileGenerator::GOOGLE_DISALLOW_EVIL_CONSTRUCTORS
GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(FileGenerator)
google::protobuf::compiler::cpp::FileGenerator::GenerateEnumDefinitions
void GenerateEnumDefinitions(io::Printer *printer)
Definition: bloaty/third_party/protobuf/src/google/protobuf/compiler/cpp/cpp_file.cc:1446
google::protobuf::compiler::cpp::FileGenerator::GenerateInitForSCC
void GenerateInitForSCC(const SCC *scc, const CrossFileReferences &refs, io::Printer *printer)
Definition: bloaty/third_party/protobuf/src/google/protobuf/compiler/cpp/cpp_file.cc:927
google::protobuf::compiler::cpp::FileGenerator::GenerateSourceForExtension
void GenerateSourceForExtension(int idx, io::Printer *printer)
Definition: protobuf/src/google/protobuf/compiler/cpp/cpp_file.cc:594
google::protobuf::compiler::cpp::FileGenerator::GenerateSourceIncludes
void GenerateSourceIncludes(io::Printer *printer)
Definition: bloaty/third_party/protobuf/src/google/protobuf/compiler/cpp/cpp_file.cc:381
google::protobuf::compiler::cpp::FileGenerator::GenerateInternalForwardDeclarations
void GenerateInternalForwardDeclarations(const CrossFileReferences &refs, io::Printer *printer)
Definition: bloaty/third_party/protobuf/src/google/protobuf/compiler/cpp/cpp_file.cc:508
google::protobuf::compiler::cpp::FileGenerator::GetCrossFileReferencesForFile
void GetCrossFileReferencesForFile(const FileDescriptor *file, CrossFileReferences *refs)
Definition: bloaty/third_party/protobuf/src/google/protobuf/compiler/cpp/cpp_file.cc:489
google::protobuf::compiler::cpp::FileGenerator::GenerateLibraryIncludes
void GenerateLibraryIncludes(io::Printer *printer)
Definition: bloaty/third_party/protobuf/src/google/protobuf/compiler/cpp/cpp_file.cc:1257
compiler
Definition: bloaty/third_party/protobuf/src/google/protobuf/compiler/plugin.pb.cc:21
google
Definition: bloaty/third_party/protobuf/benchmarks/util/data_proto2_to_proto3_util.h:11
fix_build_deps.dep
string dep
Definition: fix_build_deps.py:439
google::protobuf::compiler::cpp::FileGenerator::NumMessages
int NumMessages() const
Definition: protobuf/src/google/protobuf/compiler/cpp/cpp_file.h:91
google::protobuf::compiler::cpp::FileGenerator::IncludeFileAndExport
void IncludeFileAndExport(const std::string &google3_name, io::Printer *printer)
Definition: protobuf/src/google/protobuf/compiler/cpp/cpp_file.h:109


grpc
Author(s):
autogenerated on Fri May 16 2025 02:58:05