protobuf/src/google/protobuf/compiler/cpp/cpp_extension.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/cpp/cpp_extension.h>
36 #include <map>
37 #include <google/protobuf/compiler/cpp/cpp_helpers.h>
38 #include <google/protobuf/descriptor.pb.h>
39 #include <google/protobuf/io/printer.h>
40 #include <google/protobuf/stubs/strutil.h>
41 
42 namespace google {
43 namespace protobuf {
44 namespace compiler {
45 namespace cpp {
46 
48  const Options& options,
49  MessageSCCAnalyzer* scc_analyzer)
50  : descriptor_(descriptor), options_(options), scc_analyzer_(scc_analyzer) {
51  // Construct type_traits_.
52  if (descriptor_->is_repeated()) {
53  type_traits_ = "Repeated";
54  }
55 
56  switch (descriptor_->cpp_type()) {
58  type_traits_.append("EnumTypeTraits< ");
59  type_traits_.append(ClassName(descriptor_->enum_type(), true));
60  type_traits_.append(", ");
61  type_traits_.append(ClassName(descriptor_->enum_type(), true));
62  type_traits_.append("_IsValid>");
63  break;
65  type_traits_.append("StringTypeTraits");
66  break;
68  type_traits_.append("MessageTypeTraits< ");
70  type_traits_.append(" >");
71  break;
72  default:
73  type_traits_.append("PrimitiveTypeTraits< ");
75  type_traits_.append(" >");
76  break;
77  }
79  variables_["extendee"] =
81  variables_["type_traits"] = type_traits_;
83  variables_["name"] = ResolveKeyword(name);
84  variables_["constant_name"] = FieldConstantName(descriptor_);
85  variables_["field_type"] =
86  StrCat(static_cast<int>(descriptor_->type()));
87  variables_["packed"] = descriptor_->is_packed() ? "true" : "false";
88 
89  std::string scope =
90  IsScoped() ? ClassName(descriptor_->extension_scope(), false) + "::" : "";
91  variables_["scope"] = scope;
92  variables_["scoped_name"] = ExtensionName(descriptor_);
93  variables_["number"] = StrCat(descriptor_->number());
94 }
95 
97 
98 bool ExtensionGenerator::IsScoped() const {
99  return descriptor_->extension_scope() != nullptr;
100 }
101 
103  Formatter format(printer, variables_);
104 
105  // If this is a class member, it needs to be declared "static". Otherwise,
106  // it needs to be "extern". In the latter case, it also needs the DLL
107  // export/import specifier.
108  std::string qualifier;
109  if (!IsScoped()) {
110  qualifier = "extern";
111  if (!options_.dllexport_decl.empty()) {
112  qualifier = options_.dllexport_decl + " " + qualifier;
113  }
114  } else {
115  qualifier = "static";
116  }
117 
118  format(
119  "static const int $constant_name$ = $number$;\n"
120  "$1$ ::$proto_ns$::internal::ExtensionIdentifier< $extendee$,\n"
121  " ::$proto_ns$::internal::$type_traits$, $field_type$, $packed$ >\n"
122  " ${2$$name$$}$;\n",
123  qualifier, descriptor_);
124 }
125 
127  // If we are building for lite with implicit weak fields, we want to skip over
128  // any custom options (i.e. extensions of messages from descriptor.proto).
129  // This prevents the creation of any unnecessary linker references to the
130  // descriptor messages.
133  "net/proto2/proto/descriptor.proto") {
134  return;
135  }
136 
137  Formatter format(printer, variables_);
138  std::string default_str;
139  // If this is a class member, it needs to be declared in its class scope.
141  // We need to declare a global string which will contain the default value.
142  // We cannot declare it at class scope because that would require exposing
143  // it in the header which would be annoying for other reasons. So we
144  // replace :: with _ in the name and declare it as a global.
145  default_str =
146  StringReplace(variables_["scoped_name"], "::", "_", true) + "_default";
147  format("const std::string $1$($2$);\n", default_str,
149  } else if (descriptor_->message_type()) {
150  // We have to initialize the default instance for extensions at registration
151  // time.
152  default_str =
153  FieldMessageTypeName(descriptor_, options_) + "::default_instance()";
154  } else {
155  default_str = DefaultValue(options_, descriptor_);
156  }
157 
158  // Likewise, class members need to declare the field constant variable.
159  if (IsScoped()) {
160  format(
161  "#if !defined(_MSC_VER) || (_MSC_VER >= 1900 && _MSC_VER < 1912)\n"
162  "const int $scope$$constant_name$;\n"
163  "#endif\n");
164  }
165 
166  format(
167  "PROTOBUF_ATTRIBUTE_INIT_PRIORITY "
168  "::$proto_ns$::internal::ExtensionIdentifier< $extendee$,\n"
169  " ::$proto_ns$::internal::$type_traits$, $field_type$, $packed$ >\n"
170  " $scoped_name$($constant_name$, $1$);\n",
171  default_str);
172 
173  // Register extension verify function if needed.
177  format(
178  "PROTOBUF_ATTRIBUTE_INIT_PRIORITY "
179  "::$proto_ns$::internal::RegisterExtensionVerify< $extendee$,\n"
180  " $1$, $number$> $2$_$name$_register;\n",
182  IsScoped() ? ClassName(descriptor_->extension_scope(), false) : "");
183  }
184 }
185 
186 } // namespace cpp
187 } // namespace compiler
188 } // namespace protobuf
189 } // namespace google
google::protobuf::compiler::cpp::ExtensionGenerator::~ExtensionGenerator
~ExtensionGenerator()
Definition: bloaty/third_party/protobuf/src/google/protobuf/compiler/cpp/cpp_extension.cc:107
google::protobuf::compiler::cpp::ExtensionGenerator::type_traits_
std::string type_traits_
Definition: bloaty/third_party/protobuf/src/google/protobuf/compiler/cpp/cpp_extension.h:78
descriptor_
string_view descriptor_
Definition: elf.cc:154
http2_test_server.format
format
Definition: http2_test_server.py:118
google::protobuf::FieldDescriptor
Definition: bloaty/third_party/protobuf/src/google/protobuf/descriptor.h:515
google::protobuf::FieldDescriptor::CPPTYPE_STRING
@ CPPTYPE_STRING
Definition: bloaty/third_party/protobuf/src/google/protobuf/descriptor.h:562
options
double_dict options[]
Definition: capstone_test.c:55
google::protobuf::compiler::cpp::FieldConstantName
std::string FieldConstantName(const FieldDescriptor *field)
Definition: bloaty/third_party/protobuf/src/google/protobuf/compiler/cpp/cpp_helpers.cc:457
google::protobuf::FieldDescriptor::message_type
const Descriptor * message_type
Definition: protobuf/src/google/protobuf/descriptor.h:936
testing::internal::string
::std::string string
Definition: bloaty/third_party/protobuf/third_party/googletest/googletest/include/gtest/internal/gtest-port.h:881
google::protobuf::FieldDescriptor::containing_type
const Descriptor * containing_type() const
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::QualifiedClassName
std::string QualifiedClassName(const Descriptor *d, const Options &options)
Definition: bloaty/third_party/protobuf/src/google/protobuf/compiler/cpp/cpp_helpers.cc:320
setup.name
name
Definition: setup.py:542
google::protobuf::FieldDescriptor::extension_scope
const Descriptor * extension_scope
Definition: protobuf/src/google/protobuf/descriptor.h:933
google::protobuf::StrCat
string StrCat(const AlphaNum &a, const AlphaNum &b)
Definition: bloaty/third_party/protobuf/src/google/protobuf/stubs/strutil.cc:1482
google::protobuf::FieldDescriptor::is_packed
bool is_packed() const
Definition: bloaty/third_party/protobuf/src/google/protobuf/descriptor.cc:2963
google::protobuf::compiler::cpp::ExtensionName
std::string ExtensionName(const FieldDescriptor *d)
Definition: protobuf/src/google/protobuf/compiler/cpp/cpp_helpers.cc:347
google::protobuf::compiler::cpp::ExtensionGenerator::scc_analyzer_
MessageSCCAnalyzer * scc_analyzer_
Definition: protobuf/src/google/protobuf/compiler/cpp/cpp_extension.h:83
google::protobuf::compiler::cpp::ExtensionGenerator::variables_
std::map< std::string, std::string > variables_
Definition: bloaty/third_party/protobuf/src/google/protobuf/compiler/cpp/cpp_extension.h:81
google::protobuf::compiler::cpp::Options::lite_implicit_weak_fields
bool lite_implicit_weak_fields
Definition: bloaty/third_party/protobuf/src/google/protobuf/compiler/cpp/cpp_options.h:61
google::protobuf::compiler::cpp::ResolveKeyword
std::string ResolveKeyword(const string &name)
Definition: bloaty/third_party/protobuf/src/google/protobuf/compiler/cpp/cpp_helpers.cc:409
google::protobuf::compiler::cpp::FieldMessageTypeName
std::string FieldMessageTypeName(const FieldDescriptor *field, const Options &options)
Definition: bloaty/third_party/protobuf/src/google/protobuf/compiler/cpp/cpp_helpers.cc:473
google::protobuf::FieldDescriptor::number
int number() const
google::protobuf::compiler::cpp::ExtensionGenerator::ExtensionGenerator
ExtensionGenerator(const FieldDescriptor *descriptor, const Options &options)
Definition: bloaty/third_party/protobuf/src/google/protobuf/compiler/cpp/cpp_extension.cc:59
google::protobuf::compiler::cpp::DefaultValue
std::string DefaultValue(const FieldDescriptor *field)
Definition: bloaty/third_party/protobuf/src/google/protobuf/compiler/cpp/cpp_helpers.cc:622
cpp
Definition: third_party/bloaty/third_party/googletest/googlemock/scripts/generator/cpp/__init__.py:1
google::protobuf::compiler::cpp::SetCommonVars
void SetCommonVars(const Options &options, std::map< std::string, std::string > *variables)
Definition: bloaty/third_party/protobuf/src/google/protobuf/compiler/cpp/cpp_helpers.cc:210
google::protobuf::compiler::cpp::Options
Definition: bloaty/third_party/protobuf/src/google/protobuf/compiler/cpp/cpp_options.h:52
google::protobuf::FieldDescriptor::enum_type
const EnumDescriptor * enum_type
Definition: protobuf/src/google/protobuf/descriptor.h:937
google::protobuf::io::Printer
Definition: bloaty/third_party/protobuf/src/google/protobuf/io/printer.h:181
google::protobuf::compiler::cpp::ExtensionGenerator::descriptor_
const FieldDescriptor * descriptor_
Definition: bloaty/third_party/protobuf/src/google/protobuf/compiler/cpp/cpp_extension.h:77
google::protobuf::FileDescriptor::name
const std::string & name() const
google::protobuf::FieldDescriptor::type
Type type() const
Definition: bloaty/third_party/protobuf/src/google/protobuf/descriptor.h:2052
google::protobuf::StringReplace
void StringReplace(const string &s, const string &oldsub, const string &newsub, bool replace_all, string *res)
Definition: bloaty/third_party/protobuf/src/google/protobuf/stubs/strutil.cc:148
options_
DebugStringOptions options_
Definition: bloaty/third_party/protobuf/src/google/protobuf/descriptor.cc:2390
google::protobuf::FieldDescriptor::name
const std::string & name() const
google::protobuf::compiler::cpp::ExtensionGenerator::GenerateDefinition
void GenerateDefinition(io::Printer *printer)
Definition: bloaty/third_party/protobuf/src/google/protobuf/compiler/cpp/cpp_extension.cc:137
google::protobuf::compiler::cpp::MessageSCCAnalyzer
Definition: bloaty/third_party/protobuf/src/google/protobuf/compiler/cpp/cpp_helpers.h:519
google::protobuf::FieldDescriptor::CPPTYPE_ENUM
@ CPPTYPE_ENUM
Definition: bloaty/third_party/protobuf/src/google/protobuf/descriptor.h:561
google::protobuf::compiler::cpp::ExtensionGenerator::options_
Options options_
Definition: bloaty/third_party/protobuf/src/google/protobuf/compiler/cpp/cpp_extension.h:79
google::protobuf::Descriptor::file
const FileDescriptor * file() const
google::protobuf::compiler::cpp::ClassName
std::string ClassName(const Descriptor *descriptor)
Definition: bloaty/third_party/protobuf/src/google/protobuf/compiler/cpp/cpp_helpers.cc:302
google::protobuf::compiler::cpp::PrimitiveTypeName
const char * PrimitiveTypeName(FieldDescriptor::CppType type)
Definition: bloaty/third_party/protobuf/src/google/protobuf/compiler/cpp/cpp_helpers.cc:488
google::protobuf::FieldDescriptor::CPPTYPE_MESSAGE
@ CPPTYPE_MESSAGE
Definition: bloaty/third_party/protobuf/src/google/protobuf/descriptor.h:563
google::protobuf::compiler::cpp::Options::dllexport_decl
std::string dllexport_decl
Definition: bloaty/third_party/protobuf/src/google/protobuf/compiler/cpp/cpp_options.h:53
google::protobuf::compiler::cpp::ExtensionGenerator::GenerateDeclaration
void GenerateDeclaration(io::Printer *printer) const
Definition: bloaty/third_party/protobuf/src/google/protobuf/compiler/cpp/cpp_extension.cc:113
google::protobuf::compiler::cpp::ShouldVerify
bool ShouldVerify(const Descriptor *descriptor, const Options &options, MessageSCCAnalyzer *scc_analyzer)
Definition: protobuf/src/google/protobuf/compiler/cpp/cpp_helpers.cc:954
descriptor
static const char descriptor[1336]
Definition: certs.upbdefs.c:16
google::protobuf::FieldDescriptor::is_repeated
bool is_repeated() const
Definition: bloaty/third_party/protobuf/src/google/protobuf/descriptor.h:2067
google::protobuf::FieldDescriptor::cpp_type
CppType cpp_type() const
Definition: bloaty/third_party/protobuf/src/google/protobuf/descriptor.h:2139
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
google::protobuf::compiler::cpp::ExtensionGenerator::IsScoped
bool IsScoped() const
Definition: bloaty/third_party/protobuf/src/google/protobuf/compiler/cpp/cpp_extension.cc:109
framework.helpers.highlighter.Formatter
Formatter
Definition: highlighter.py:53


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