protobuf/src/google/protobuf/compiler/code_generator.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 // Defines the abstract interface implemented by each of the language-specific
36 // code generators.
37 
38 #ifndef GOOGLE_PROTOBUF_COMPILER_CODE_GENERATOR_H__
39 #define GOOGLE_PROTOBUF_COMPILER_CODE_GENERATOR_H__
40 
41 #include <string>
42 #include <utility>
43 #include <vector>
44 #include <google/protobuf/stubs/common.h>
45 
46 #include <google/protobuf/port_def.inc>
47 
48 namespace google {
49 namespace protobuf {
50 
51 namespace io {
53 }
54 class FileDescriptor;
55 class GeneratedCodeInfo;
56 
57 namespace compiler {
58 class AccessInfoMap;
59 
60 class Version;
61 
62 // Defined in this file.
63 class CodeGenerator;
64 class GeneratorContext;
65 
66 // The abstract interface to a class which generates code implementing a
67 // particular proto file in a particular language. A number of these may
68 // be registered with CommandLineInterface to support various languages.
69 class PROTOC_EXPORT CodeGenerator {
70  public:
71  inline CodeGenerator() {}
72  virtual ~CodeGenerator();
73 
74  // Generates code for the given proto file, generating one or more files in
75  // the given output directory.
76  //
77  // A parameter to be passed to the generator can be specified on the command
78  // line. This is intended to be used to pass generator specific parameters.
79  // It is empty if no parameter was given. ParseGeneratorParameter (below),
80  // can be used to accept multiple parameters within the single parameter
81  // command line flag.
82  //
83  // Returns true if successful. Otherwise, sets *error to a description of
84  // the problem (e.g. "invalid parameter") and returns false.
85  virtual bool Generate(const FileDescriptor* file,
86  const std::string& parameter,
87  GeneratorContext* generator_context,
88  std::string* error) const = 0;
89 
90  // Generates code for all given proto files.
91  //
92  // WARNING: The canonical code generator design produces one or two output
93  // files per input .proto file, and we do not wish to encourage alternate
94  // designs.
95  //
96  // A parameter is given as passed on the command line, as in |Generate()|
97  // above.
98  //
99  // Returns true if successful. Otherwise, sets *error to a description of
100  // the problem (e.g. "invalid parameter") and returns false.
101  virtual bool GenerateAll(const std::vector<const FileDescriptor*>& files,
102  const std::string& parameter,
103  GeneratorContext* generator_context,
104  std::string* error) const;
105 
106  // This must be kept in sync with plugin.proto. See that file for
107  // documentation on each value.
108  enum Feature {
109  FEATURE_PROTO3_OPTIONAL = 1,
110  };
111 
112  // Implement this to indicate what features this code generator supports.
113  //
114  // This must be a bitwise OR of values from the Feature enum above (or zero).
115  virtual uint64_t GetSupportedFeatures() const { return 0; }
116 
117  // This is no longer used, but this class is part of the opensource protobuf
118  // library, so it has to remain to keep vtables the same for the current
119  // version of the library. When protobufs does a api breaking change, the
120  // method can be removed.
121  virtual bool HasGenerateAll() const { return true; }
122 
123  private:
125 };
126 
127 // CodeGenerators generate one or more files in a given directory. This
128 // abstract interface represents the directory to which the CodeGenerator is
129 // to write and other information about the context in which the Generator
130 // runs.
131 class PROTOC_EXPORT GeneratorContext {
132  public:
133  inline GeneratorContext() {
134  }
135  virtual ~GeneratorContext();
136 
137  // Opens the given file, truncating it if it exists, and returns a
138  // ZeroCopyOutputStream that writes to the file. The caller takes ownership
139  // of the returned object. This method never fails (a dummy stream will be
140  // returned instead).
141  //
142  // The filename given should be relative to the root of the source tree.
143  // E.g. the C++ generator, when generating code for "foo/bar.proto", will
144  // generate the files "foo/bar.pb.h" and "foo/bar.pb.cc"; note that
145  // "foo/" is included in these filenames. The filename is not allowed to
146  // contain "." or ".." components.
147  virtual io::ZeroCopyOutputStream* Open(const std::string& filename) = 0;
148 
149  // Similar to Open() but the output will be appended to the file if exists
150  virtual io::ZeroCopyOutputStream* OpenForAppend(const std::string& filename);
151 
152  // Creates a ZeroCopyOutputStream which will insert code into the given file
153  // at the given insertion point. See plugin.proto (plugin.pb.h) for more
154  // information on insertion points. The default implementation
155  // assert-fails -- it exists only for backwards-compatibility.
156  //
157  // WARNING: This feature is currently EXPERIMENTAL and is subject to change.
158  virtual io::ZeroCopyOutputStream* OpenForInsert(
159  const std::string& filename, const std::string& insertion_point);
160 
161  // Similar to OpenForInsert, but if `info` is non-empty, will open (or create)
162  // filename.pb.meta and insert info at the appropriate place with the
163  // necessary shifts. The default implementation ignores `info`.
164  //
165  // WARNING: This feature will be REMOVED in the near future.
166  virtual io::ZeroCopyOutputStream* OpenForInsertWithGeneratedCodeInfo(
167  const std::string& filename, const std::string& insertion_point,
168  const google::protobuf::GeneratedCodeInfo& info);
169 
170  // Returns a vector of FileDescriptors for all the files being compiled
171  // in this run. Useful for languages, such as Go, that treat files
172  // differently when compiled as a set rather than individually.
173  virtual void ListParsedFiles(std::vector<const FileDescriptor*>* output);
174 
175  // Retrieves the version number of the protocol compiler associated with
176  // this GeneratorContext.
177  virtual void GetCompilerVersion(Version* version) const;
178 
179 
180  private:
182 };
183 
184 // The type GeneratorContext was once called OutputDirectory. This typedef
185 // provides backward compatibility.
187 
188 // Several code generators treat the parameter argument as holding a
189 // list of options separated by commas. This helper function parses
190 // a set of comma-delimited name/value pairs: e.g.,
191 // "foo=bar,baz,qux=corge"
192 // parses to the pairs:
193 // ("foo", "bar"), ("baz", ""), ("qux", "corge")
194 PROTOC_EXPORT void ParseGeneratorParameter(
195  const std::string&, std::vector<std::pair<std::string, std::string> >*);
196 
197 // Strips ".proto" or ".protodevel" from the end of a filename.
198 PROTOC_EXPORT std::string StripProto(const std::string& filename);
199 
200 } // namespace compiler
201 } // namespace protobuf
202 } // namespace google
203 
204 #include <google/protobuf/port_undef.inc>
205 
206 #endif // GOOGLE_PROTOBUF_COMPILER_CODE_GENERATOR_H__
google::protobuf::compiler::OutputDirectory
GeneratorContext OutputDirectory
Definition: bloaty/third_party/protobuf/src/google/protobuf/compiler/code_generator.h:165
filename
const char * filename
Definition: bloaty/third_party/zlib/contrib/minizip/ioapi.h:135
GOOGLE_DISALLOW_EVIL_CONSTRUCTORS
#define GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(TypeName)
Definition: bloaty/third_party/protobuf/src/google/protobuf/stubs/macros.h:40
google::protobuf::compiler::CodeGenerator
Definition: bloaty/third_party/protobuf/src/google/protobuf/compiler/code_generator.h:68
google::protobuf::compiler::CodeGenerator::GetSupportedFeatures
virtual uint64_t GetSupportedFeatures() const
Definition: protobuf/src/google/protobuf/compiler/code_generator.h:115
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
error
grpc_error_handle error
Definition: retry_filter.cc:499
file
Definition: bloaty/third_party/zlib/examples/gzappend.c:170
google::protobuf
Definition: bloaty/third_party/protobuf/benchmarks/util/data_proto2_to_proto3_util.h:12
grpc::protobuf::io::ZeroCopyOutputStream
GRPC_CUSTOM_ZEROCOPYOUTPUTSTREAM ZeroCopyOutputStream
Definition: include/grpcpp/impl/codegen/config_protobuf.h:100
version
Definition: version.py:1
google::protobuf::compiler::CodeGenerator::HasGenerateAll
virtual bool HasGenerateAll() const
Definition: protobuf/src/google/protobuf/compiler/code_generator.h:121
grpc::Version
std::string Version()
Return gRPC library version.
Definition: version_cc.cc:28
google::protobuf::compiler::CodeGenerator::Feature
Feature
Definition: protobuf/src/google/protobuf/compiler/code_generator.h:108
grpc::protobuf::compiler::GeneratorContext
GRPC_CUSTOM_GENERATORCONTEXT GeneratorContext
Definition: src/compiler/config.h:42
grpc::protobuf::compiler::CodeGenerator
GRPC_CUSTOM_CODEGENERATOR CodeGenerator
Definition: src/compiler/config.h:41
gmock_output_test.output
output
Definition: bloaty/third_party/googletest/googlemock/test/gmock_output_test.py:175
uint64_t
unsigned __int64 uint64_t
Definition: stdint-msvc2008.h:90
io
google::protobuf::compiler::GeneratorContext::GeneratorContext
GeneratorContext()
Definition: protobuf/src/google/protobuf/compiler/code_generator.h:133
google::protobuf::compiler::CodeGenerator::CodeGenerator
CodeGenerator()
Definition: protobuf/src/google/protobuf/compiler/code_generator.h:71
google::protobuf::compiler::StripProto
std::string StripProto(const std::string &filename)
Definition: protobuf/src/google/protobuf/compiler/code_generator.cc:127
google::protobuf::io::ZeroCopyOutputStream
Definition: bloaty/third_party/protobuf/src/google/protobuf/io/zero_copy_stream.h:183
google::protobuf::compiler::ParseGeneratorParameter
void ParseGeneratorParameter(const std::string &text, std::vector< std::pair< std::string, std::string > > *output)
Definition: bloaty/third_party/protobuf/src/google/protobuf/compiler/code_generator.cc:101
google::protobuf::FileDescriptor
Definition: bloaty/third_party/protobuf/src/google/protobuf/descriptor.h:1320
GeneratedCodeInfo
Definition: bloaty/third_party/protobuf/src/google/protobuf/descriptor.pb.h:7087
google::protobuf::compiler::GeneratorContext
Definition: bloaty/third_party/protobuf/src/google/protobuf/compiler/code_generator.h:119
amalgamate.files
list files
Definition: amalgamate.py:115
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


grpc
Author(s):
autogenerated on Fri May 16 2025 02:57:56