protobuf/src/google/protobuf/compiler/code_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 
35 #include <google/protobuf/compiler/code_generator.h>
36 
37 #include <google/protobuf/stubs/logging.h>
38 #include <google/protobuf/stubs/common.h>
39 #include <google/protobuf/compiler/plugin.pb.h>
40 #include <google/protobuf/descriptor.h>
41 #include <google/protobuf/stubs/strutil.h>
42 
43 namespace google {
44 namespace protobuf {
45 namespace compiler {
46 
48 
49 bool CodeGenerator::GenerateAll(const std::vector<const FileDescriptor*>& files,
50  const std::string& parameter,
51  GeneratorContext* generator_context,
52  std::string* error) const {
53  // Default implementation is just to call the per file method, and prefix any
54  // error string with the file to provide context.
55  bool succeeded = true;
56  for (int i = 0; i < files.size(); i++) {
57  const FileDescriptor* file = files[i];
58  succeeded = Generate(file, parameter, generator_context, error);
59  if (!succeeded && error && error->empty()) {
60  *error =
61  "Code generator returned false but provided no error "
62  "description.";
63  }
64  if (error && !error->empty()) {
65  *error = file->name() + ": " + *error;
66  break;
67  }
68  if (!succeeded) {
69  break;
70  }
71  }
72  return succeeded;
73 }
74 
76 
78  const std::string& filename) {
79  return NULL;
80 }
81 
83  const std::string& filename, const std::string& insertion_point) {
84  GOOGLE_LOG(FATAL) << "This GeneratorContext does not support insertion.";
85  return NULL; // make compiler happy
86 }
87 
89  const std::string& filename, const std::string& insertion_point,
90  const google::protobuf::GeneratedCodeInfo& /*info*/) {
91  return OpenForInsert(filename, insertion_point);
92 }
93 
95  std::vector<const FileDescriptor*>* output) {
96  GOOGLE_LOG(FATAL) << "This GeneratorContext does not support ListParsedFiles";
97 }
98 
100  version->set_major(GOOGLE_PROTOBUF_VERSION / 1000000);
101  version->set_minor(GOOGLE_PROTOBUF_VERSION / 1000 % 1000);
102  version->set_patch(GOOGLE_PROTOBUF_VERSION % 1000);
104 }
105 
106 // Parses a set of comma-delimited name/value pairs.
108  const std::string& text,
109  std::vector<std::pair<std::string, std::string> >* output) {
110  std::vector<std::string> parts = Split(text, ",", true);
111 
112  for (int i = 0; i < parts.size(); i++) {
113  std::string::size_type equals_pos = parts[i].find_first_of('=');
114  std::pair<std::string, std::string> value;
115  if (equals_pos == std::string::npos) {
116  value.first = parts[i];
117  value.second = "";
118  } else {
119  value.first = parts[i].substr(0, equals_pos);
120  value.second = parts[i].substr(equals_pos + 1);
121  }
122  output->push_back(value);
123  }
124 }
125 
126 // Strips ".proto" or ".protodevel" from the end of a filename.
128  if (HasSuffixString(filename, ".protodevel")) {
129  return StripSuffixString(filename, ".protodevel");
130  } else {
131  return StripSuffixString(filename, ".proto");
132  }
133 }
134 
135 } // namespace compiler
136 } // namespace protobuf
137 } // namespace google
filename
const char * filename
Definition: bloaty/third_party/zlib/contrib/minizip/ioapi.h:135
google::protobuf::value
const Descriptor::ReservedRange value
Definition: bloaty/third_party/protobuf/src/google/protobuf/descriptor.h:1954
google::protobuf::compiler::GeneratorContext::GetCompilerVersion
virtual void GetCompilerVersion(Version *version) const
Definition: bloaty/third_party/protobuf/src/google/protobuf/compiler/code_generator.cc:93
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
grpc::Version
std::string Version()
Return gRPC library version.
Definition: version_cc.cc:28
google::protobuf::compiler::GeneratorContext::ListParsedFiles
virtual void ListParsedFiles(std::vector< const FileDescriptor * > *output)
Definition: bloaty/third_party/protobuf/src/google/protobuf/compiler/code_generator.cc:88
grpc::protobuf::compiler::GeneratorContext
GRPC_CUSTOM_GENERATORCONTEXT GeneratorContext
Definition: src/compiler/config.h:42
GOOGLE_PROTOBUF_VERSION
#define GOOGLE_PROTOBUF_VERSION
Definition: bloaty/third_party/protobuf/src/google/protobuf/stubs/common.h:84
gen_server_registered_method_bad_client_test_body.text
def text
Definition: gen_server_registered_method_bad_client_test_body.py:50
gmock_output_test.output
output
Definition: bloaty/third_party/googletest/googlemock/test/gmock_output_test.py:175
google::protobuf::compiler::GeneratorContext::OpenForInsert
virtual io::ZeroCopyOutputStream * OpenForInsert(const std::string &filename, const std::string &insertion_point)
Definition: bloaty/third_party/protobuf/src/google/protobuf/compiler/code_generator.cc:82
google::protobuf::compiler::GeneratorContext::~GeneratorContext
virtual ~GeneratorContext()
Definition: bloaty/third_party/protobuf/src/google/protobuf/compiler/code_generator.cc:75
google::protobuf::compiler::GeneratorContext::OpenForAppend
virtual io::ZeroCopyOutputStream * OpenForAppend(const std::string &filename)
Definition: bloaty/third_party/protobuf/src/google/protobuf/compiler/code_generator.cc:77
google::protobuf::compiler::GeneratorContext::OpenForInsertWithGeneratedCodeInfo
virtual io::ZeroCopyOutputStream * OpenForInsertWithGeneratedCodeInfo(const std::string &filename, const std::string &insertion_point, const google::protobuf::GeneratedCodeInfo &info)
Definition: protobuf/src/google/protobuf/compiler/code_generator.cc:88
FATAL
#define FATAL(msg)
Definition: task.h:88
GOOGLE_PROTOBUF_VERSION_SUFFIX
#define GOOGLE_PROTOBUF_VERSION_SUFFIX
Definition: bloaty/third_party/protobuf/src/google/protobuf/stubs/common.h:87
google::protobuf::compiler::CodeGenerator::~CodeGenerator
virtual ~CodeGenerator()
Definition: bloaty/third_party/protobuf/src/google/protobuf/compiler/code_generator.cc:47
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::Split
std::vector< string > Split(const string &full, const char *delim, bool skip_empty=true)
Definition: bloaty/third_party/protobuf/src/google/protobuf/stubs/strutil.h:235
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::compiler::CodeGenerator::Generate
virtual bool Generate(const FileDescriptor *file, const std::string &parameter, GeneratorContext *generator_context, std::string *error) const =0
google::protobuf::HasSuffixString
bool HasSuffixString(const string &str, const string &suffix)
Definition: bloaty/third_party/protobuf/src/google/protobuf/stubs/strutil.h:137
file::name
char * name
Definition: bloaty/third_party/zlib/examples/gzappend.c:176
google::protobuf::StripSuffixString
string StripSuffixString(const string &str, const string &suffix)
Definition: bloaty/third_party/protobuf/src/google/protobuf/stubs/strutil.h:143
google::protobuf::compiler::CodeGenerator::GenerateAll
virtual bool GenerateAll(const std::vector< const FileDescriptor * > &files, const std::string &parameter, GeneratorContext *generator_context, std::string *error) const
Definition: bloaty/third_party/protobuf/src/google/protobuf/compiler/code_generator.cc:49
GOOGLE_LOG
#define GOOGLE_LOG(LEVEL)
Definition: bloaty/third_party/protobuf/src/google/protobuf/stubs/logging.h:146
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
i
uint64_t i
Definition: abseil-cpp/absl/container/btree_benchmark.cc:230


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