protobuf/src/google/protobuf/compiler/objectivec/objectivec_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 #include <iostream>
32 
33 #include <google/protobuf/compiler/objectivec/objectivec_extension.h>
34 #include <google/protobuf/compiler/objectivec/objectivec_helpers.h>
35 #include <google/protobuf/descriptor.pb.h>
36 #include <google/protobuf/stubs/strutil.h>
37 #include <google/protobuf/io/printer.h>
38 
39 namespace google {
40 namespace protobuf {
41 namespace compiler {
42 namespace objectivec {
43 
46  : method_name_(ExtensionMethodName(descriptor)),
47  root_class_and_method_name_(root_class_name + "_" + method_name_),
49  if (descriptor->is_map()) {
50  // NOTE: src/google/protobuf/compiler/plugin.cc makes use of cerr for some
51  // error cases, so it seems to be ok to use as a back door for errors.
52  std::cerr << "error: Extension is a map<>!"
53  << " That used to be blocked by the compiler." << std::endl;
54  std::cerr.flush();
55  abort();
56  }
57 }
58 
60 
62  std::map<std::string, std::string> vars;
63  vars["method_name"] = method_name_;
65  vars["storage_attribute"] = " NS_RETURNS_NOT_RETAINED";
66  } else {
67  vars["storage_attribute"] = "";
68  }
69  SourceLocation location;
70  if (descriptor_->GetSourceLocation(&location)) {
71  vars["comments"] = BuildCommentsString(location, true);
72  } else {
73  vars["comments"] = "";
74  }
75  // Unlike normal message fields, check if the file for the extension was
76  // deprecated.
77  vars["deprecated_attribute"] = GetOptionalDeprecatedAttribute(descriptor_, descriptor_->file());
78  printer->Print(vars,
79  "$comments$"
80  "+ (GPBExtensionDescriptor *)$method_name$$storage_attribute$$deprecated_attribute$;\n");
81 }
82 
84  io::Printer* printer) {
85  std::map<std::string, std::string> vars;
86  vars["root_class_and_method_name"] = root_class_and_method_name_;
88  vars["extended_type"] = ObjCClass(containing_type);
89  vars["number"] = StrCat(descriptor_->number());
90 
91  std::vector<std::string> options;
92  if (descriptor_->is_repeated()) options.push_back("GPBExtensionRepeated");
93  if (descriptor_->is_packed()) options.push_back("GPBExtensionPacked");
95  options.push_back("GPBExtensionSetWireFormat");
96  }
97  vars["options"] = BuildFlagsString(FLAGTYPE_EXTENSION, options);
98 
100  if (objc_type == OBJECTIVECTYPE_MESSAGE) {
102  vars["type"] = ObjCClass(message_type);
103  } else {
104  vars["type"] = "Nil";
105  }
106 
107  vars["default_name"] = GPBGenericValueFieldName(descriptor_);
108  if (descriptor_->is_repeated()) {
109  vars["default"] = "nil";
110  } else {
111  vars["default"] = DefaultValue(descriptor_);
112  }
114  vars["extension_type"] = std::string("GPBDataType") + type;
115 
116  if (objc_type == OBJECTIVECTYPE_ENUM) {
117  vars["enum_desc_func_name"] =
118  EnumName(descriptor_->enum_type()) + "_EnumDescriptor";
119  } else {
120  vars["enum_desc_func_name"] = "NULL";
121  }
122 
123  printer->Print(vars,
124  "{\n"
125  " .defaultValue.$default_name$ = $default$,\n"
126  " .singletonName = GPBStringifySymbol($root_class_and_method_name$),\n"
127  " .extendedClass.clazz = $extended_type$,\n"
128  " .messageOrGroupClass.clazz = $type$,\n"
129  " .enumDescriptorFunc = $enum_desc_func_name$,\n"
130  " .fieldNumber = $number$,\n"
131  " .dataType = $extension_type$,\n"
132  " .options = $options$,\n"
133  "},\n");
134 }
135 
137  std::set<std::string>* fwd_decls) {
138  std::string extended_type = ClassName(descriptor_->containing_type());
139  fwd_decls->insert(ObjCClassDeclaration(extended_type));
141  if (objc_type == OBJECTIVECTYPE_MESSAGE) {
143  fwd_decls->insert(ObjCClassDeclaration(message_type));
144  }
145 }
146 
148  printer->Print(
149  "[registry addExtension:$root_class_and_method_name$];\n",
150  "root_class_and_method_name", root_class_and_method_name_);
151 }
152 
153 } // namespace objectivec
154 } // namespace compiler
155 } // namespace protobuf
156 } // namespace google
google::protobuf::compiler::objectivec::ExtensionGenerator::GenerateMembersHeader
void GenerateMembersHeader(io::Printer *printer)
Definition: bloaty/third_party/protobuf/src/google/protobuf/compiler/objectivec/objectivec_extension.cc:61
google::protobuf::io::Printer::Print
void Print(const std::map< std::string, std::string > &variables, const char *text)
Definition: bloaty/third_party/protobuf/src/google/protobuf/io/printer.cc:113
descriptor_
string_view descriptor_
Definition: elf.cc:154
google::protobuf::FieldDescriptor
Definition: bloaty/third_party/protobuf/src/google/protobuf/descriptor.h:515
google::protobuf::compiler::objectivec::ExtensionGenerator::method_name_
string method_name_
Definition: bloaty/third_party/protobuf/src/google/protobuf/compiler/objectivec/objectivec_extension.h:56
google::protobuf::compiler::objectivec::ObjCClassDeclaration
std::string ObjCClassDeclaration(const std::string &class_name)
Definition: protobuf/src/google/protobuf/compiler/objectivec/objectivec_helpers.cc:734
google::protobuf::compiler::objectivec::DefaultValue
string DefaultValue(const FieldDescriptor *field)
Definition: bloaty/third_party/protobuf/src/google/protobuf/compiler/objectivec/objectivec_helpers.cc:794
phone_pb2.message_type
message_type
Definition: phone_pb2.py:200
google::protobuf::compiler::objectivec::ObjCClass
std::string ObjCClass(const std::string &class_name)
Definition: protobuf/src/google/protobuf/compiler/objectivec/objectivec_helpers.cc:730
options
double_dict options[]
Definition: capstone_test.c:55
google::protobuf::FieldDescriptor::message_type
const Descriptor * message_type
Definition: protobuf/src/google/protobuf/descriptor.h:936
google::protobuf::compiler::objectivec::ClassName
string ClassName(const Descriptor *descriptor)
Definition: bloaty/third_party/protobuf/src/google/protobuf/compiler/objectivec/objectivec_helpers.cc:460
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::objectivec::ExtensionGenerator::root_class_and_method_name_
string root_class_and_method_name_
Definition: bloaty/third_party/protobuf/src/google/protobuf/compiler/objectivec/objectivec_extension.h:57
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::compiler::objectivec::ExtensionMethodName
string ExtensionMethodName(const FieldDescriptor *descriptor)
Definition: bloaty/third_party/protobuf/src/google/protobuf/compiler/objectivec/objectivec_helpers.cc:532
google::protobuf::FieldDescriptor::is_packed
bool is_packed() const
Definition: bloaty/third_party/protobuf/src/google/protobuf/descriptor.cc:2963
google::protobuf::Descriptor::options
const MessageOptions & options() const
google::protobuf::compiler::objectivec::ExtensionGenerator::GenerateRegistrationSource
void GenerateRegistrationSource(io::Printer *printer)
Definition: bloaty/third_party/protobuf/src/google/protobuf/compiler/objectivec/objectivec_extension.cc:136
google::protobuf::compiler::objectivec::OBJECTIVECTYPE_ENUM
@ OBJECTIVECTYPE_ENUM
Definition: bloaty/third_party/protobuf/src/google/protobuf/compiler/objectivec/objectivec_helpers.h:147
google::protobuf::FieldDescriptor::file
const FileDescriptor * file() const
google::protobuf::compiler::objectivec::GetOptionalDeprecatedAttribute
string GetOptionalDeprecatedAttribute(const TDescriptor *descriptor, const FileDescriptor *file=NULL, bool preSpace=true, bool postNewline=false)
Definition: bloaty/third_party/protobuf/src/google/protobuf/compiler/objectivec/objectivec_helpers.h:158
google::protobuf::compiler::objectivec::BuildFlagsString
string BuildFlagsString(const FlagType flag_type, const std::vector< string > &strings)
Definition: bloaty/third_party/protobuf/src/google/protobuf/compiler/objectivec/objectivec_helpers.cc:910
google::protobuf::compiler::objectivec::ExtensionGenerator::DetermineObjectiveCClassDefinitions
void DetermineObjectiveCClassDefinitions(std::set< std::string > *fwd_decls)
Definition: protobuf/src/google/protobuf/compiler/objectivec/objectivec_extension.cc:136
google::protobuf::FieldDescriptor::number
int number() const
phone_pb2.containing_type
containing_type
Definition: phone_pb2.py:199
google::protobuf::compiler::objectivec::GetCapitalizedType
string GetCapitalizedType(const FieldDescriptor *field)
Definition: bloaty/third_party/protobuf/src/google/protobuf/compiler/objectivec/objectivec_helpers.cc:619
google::protobuf::compiler::objectivec::FLAGTYPE_EXTENSION
@ FLAGTYPE_EXTENSION
Definition: bloaty/third_party/protobuf/src/google/protobuf/compiler/objectivec/objectivec_helpers.h:153
google::protobuf::FieldDescriptor::enum_type
const EnumDescriptor * enum_type
Definition: protobuf/src/google/protobuf/descriptor.h:937
google::protobuf::compiler::objectivec::ExtensionGenerator::~ExtensionGenerator
~ExtensionGenerator()
Definition: bloaty/third_party/protobuf/src/google/protobuf/compiler/objectivec/objectivec_extension.cc:59
google::protobuf::io::Printer
Definition: bloaty/third_party/protobuf/src/google/protobuf/io/printer.h:181
google::protobuf::compiler::objectivec::ObjectiveCType
ObjectiveCType
Definition: bloaty/third_party/protobuf/src/google/protobuf/compiler/objectivec/objectivec_helpers.h:137
google::protobuf::compiler::objectivec::OBJECTIVECTYPE_MESSAGE
@ OBJECTIVECTYPE_MESSAGE
Definition: bloaty/third_party/protobuf/src/google/protobuf/compiler/objectivec/objectivec_helpers.h:148
google::protobuf::compiler::objectivec::IsRetainedName
bool IsRetainedName(const string &name)
Definition: bloaty/third_party/protobuf/src/google/protobuf/compiler/objectivec/objectivec_helpers.cc:377
google::protobuf::compiler::objectivec::ExtensionGenerator::descriptor_
const FieldDescriptor * descriptor_
Definition: bloaty/third_party/protobuf/src/google/protobuf/compiler/objectivec/objectivec_extension.h:58
google::protobuf::FieldDescriptor::GetSourceLocation
bool GetSourceLocation(SourceLocation *out_location) const
Definition: bloaty/third_party/protobuf/src/google/protobuf/descriptor.cc:2978
grpc::protobuf::SourceLocation
GRPC_CUSTOM_SOURCELOCATION SourceLocation
Definition: include/grpcpp/impl/codegen/config_protobuf.h:90
google::protobuf::compiler::objectivec::EnumName
string EnumName(const EnumDescriptor *descriptor)
Definition: bloaty/third_party/protobuf/src/google/protobuf/compiler/objectivec/objectivec_helpers.cc:472
asyncio_get_stats.type
type
Definition: asyncio_get_stats.py:37
google::protobuf::compiler::objectivec::ExtensionGenerator::GenerateStaticVariablesInitialization
void GenerateStaticVariablesInitialization(io::Printer *printer)
Definition: bloaty/third_party/protobuf/src/google/protobuf/compiler/objectivec/objectivec_extension.cc:83
google::protobuf::compiler::objectivec::ExtensionGenerator::ExtensionGenerator
ExtensionGenerator(const string &root_class_name, const FieldDescriptor *descriptor)
Definition: bloaty/third_party/protobuf/src/google/protobuf/compiler/objectivec/objectivec_extension.cc:44
google::protobuf::compiler::objectivec::GetObjectiveCType
ObjectiveCType GetObjectiveCType(FieldDescriptor::Type field_type)
Definition: bloaty/third_party/protobuf/src/google/protobuf/compiler/objectivec/objectivec_helpers.cc:665
descriptor
static const char descriptor[1336]
Definition: certs.upbdefs.c:16
google::protobuf::compiler::objectivec::GPBGenericValueFieldName
string GPBGenericValueFieldName(const FieldDescriptor *field)
Definition: bloaty/third_party/protobuf/src/google/protobuf/compiler/objectivec/objectivec_helpers.cc:754
google::protobuf::FieldDescriptor::is_repeated
bool is_repeated() const
Definition: bloaty/third_party/protobuf/src/google/protobuf/descriptor.h:2067
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::objectivec::BuildCommentsString
string BuildCommentsString(const SourceLocation &location, bool prefer_single_line)
Definition: bloaty/third_party/protobuf/src/google/protobuf/compiler/objectivec/objectivec_helpers.cc:928
MessageOptions::message_set_wire_format
bool message_set_wire_format() const
Definition: bloaty/third_party/protobuf/src/google/protobuf/descriptor.pb.h:12061


grpc
Author(s):
autogenerated on Fri May 16 2025 02:59:34