protobuf
src
google
protobuf
compiler
objectivec
objectivec_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
#include <iostream>
32
#include <
google/protobuf/compiler/objectivec/objectivec_generator.h
>
33
#include <
google/protobuf/compiler/objectivec/objectivec_file.h
>
34
#include <
google/protobuf/compiler/objectivec/objectivec_helpers.h
>
35
#include <
google/protobuf/io/printer.h
>
36
#include <
google/protobuf/io/zero_copy_stream.h
>
37
#include <
google/protobuf/stubs/strutil.h
>
38
39
namespace
google
{
40
namespace
protobuf {
41
namespace
compiler
{
42
namespace
objectivec {
43
44
ObjectiveCGenerator::ObjectiveCGenerator
() {}
45
46
ObjectiveCGenerator::~ObjectiveCGenerator
() {}
47
48
bool
ObjectiveCGenerator::HasGenerateAll
()
const
{
49
return
true
;
50
}
51
52
bool
ObjectiveCGenerator::Generate
(
const
FileDescriptor
* file,
53
const
string
& parameter,
54
GeneratorContext
* context,
55
string
*
error
)
const
{
56
*
error
=
"Unimplemented Generate() method. Call GenerateAll() instead."
;
57
return
false
;
58
}
59
60
bool
ObjectiveCGenerator::GenerateAll
(
const
std::vector<const FileDescriptor*>& files,
61
const
string
& parameter,
62
GeneratorContext
* context,
63
string
*
error
)
const
{
64
// -----------------------------------------------------------------
65
// Parse generator options. These options are passed to the compiler using the
66
// --objc_opt flag. The options are passed as a comma separated list of
67
// options along with their values. If the option appears multiple times, only
68
// the last value will be considered.
69
//
70
// e.g. protoc ... --objc_opt=expected_prefixes=file.txt,generate_for_named_framework=MyFramework
71
72
Options
generation_options;
73
74
std::vector<std::pair<string, string> >
options
;
75
ParseGeneratorParameter
(parameter, &
options
);
76
for
(
int
i
= 0;
i
<
options
.size();
i
++) {
77
if
(
options
[
i
].
first
==
"expected_prefixes_path"
) {
78
// Path to find a file containing the expected prefixes
79
// (objc_class_prefix "PREFIX") for proto packages (package NAME). The
80
// generator will then issue warnings/errors if in the proto files being
81
// generated the option is not listed/wrong/etc in the file.
82
//
83
// The format of the file is:
84
// - An entry is a line of "package=prefix".
85
// - Comments start with "#".
86
// - A comment can go on a line after a expected package/prefix pair.
87
// (i.e. - "package=prefix # comment")
88
//
89
// There is no validation that the prefixes are good prefixes, it is
90
// assumed that they are when you create the file.
91
generation_options.
expected_prefixes_path
=
options
[
i
].second;
92
}
else
if
(
options
[
i
].
first
==
"expected_prefixes_suppressions"
) {
93
// A semicolon delimited string that lists the paths of .proto files to
94
// exclude from the package prefix validations (expected_prefixes_path).
95
// This is provided as an "out", to skip some files being checked.
96
SplitStringUsing
(
options
[
i
].second,
";"
,
97
&generation_options.
expected_prefixes_suppressions
);
98
}
else
if
(
options
[
i
].
first
==
"generate_for_named_framework"
) {
99
// The name of the framework that protos are being generated for. This
100
// will cause the #import statements to be framework based using this
101
// name (i.e. - "#import <NAME/proto.pbobjc.h>).
102
//
103
// NOTE: If this option is used with
104
// named_framework_to_proto_path_mappings_path, then this is effectively
105
// the "default" framework name used for everything that wasn't mapped by
106
// the mapping file.
107
generation_options.
generate_for_named_framework
=
options
[
i
].second;
108
}
else
if
(
options
[
i
].
first
==
"named_framework_to_proto_path_mappings_path"
) {
109
// Path to find a file containing the list of framework names and proto
110
// files. The generator uses this to decide if a proto file
111
// referenced should use a framework style import vs. a user level import
112
// (#import <FRAMEWORK/file.pbobjc.h> vs #import "dir/file.pbobjc.h").
113
//
114
// The format of the file is:
115
// - An entry is a line of "frameworkName: file.proto, dir/file2.proto".
116
// - Comments start with "#".
117
// - A comment can go on a line after a expected package/prefix pair.
118
// (i.e. - "frameworkName: file.proto # comment")
119
//
120
// Any number of files can be listed for a framework, just separate them
121
// with commas.
122
//
123
// There can be multiple lines listing the same frameworkName incase it
124
// has a lot of proto files included in it; having multiple lines makes
125
// things easier to read. If a proto file is not configured in the
126
// mappings file, it will use the default framework name if one was passed
127
// with generate_for_named_framework, or the relative path to it's include
128
// path otherwise.
129
generation_options.
named_framework_to_proto_path_mappings_path
=
options
[
i
].second;
130
}
else
{
131
*
error
=
"error: Unknown generator option: "
+
options
[
i
].first;
132
return
false
;
133
}
134
}
135
136
// -----------------------------------------------------------------
137
138
// Validate the objc prefix/package pairings.
139
if
(!
ValidateObjCClassPrefixes
(files, generation_options,
error
)) {
140
// *error will have been filled in.
141
return
false
;
142
}
143
144
for
(
int
i
= 0;
i
< files.size();
i
++) {
145
const
FileDescriptor
* file = files[
i
];
146
FileGenerator
file_generator(file, generation_options);
147
string
filepath =
FilePath
(file);
148
149
// Generate header.
150
{
151
std::unique_ptr<io::ZeroCopyOutputStream>
output
(
152
context->
Open
(filepath +
".pbobjc.h"
));
153
io::Printer
printer(
output
.get(),
'$'
);
154
file_generator.
GenerateHeader
(&printer);
155
}
156
157
// Generate m file.
158
{
159
std::unique_ptr<io::ZeroCopyOutputStream>
output
(
160
context->
Open
(filepath +
".pbobjc.m"
));
161
io::Printer
printer(
output
.get(),
'$'
);
162
file_generator.
GenerateSource
(&printer);
163
}
164
}
165
166
return
true
;
167
}
168
169
}
// namespace objectivec
170
}
// namespace compiler
171
}
// namespace protobuf
172
}
// namespace google
zero_copy_stream.h
google::protobuf::compiler::objectivec::Options::named_framework_to_proto_path_mappings_path
string named_framework_to_proto_path_mappings_path
Definition:
objectivec_helpers.h:55
google::protobuf::compiler::objectivec::ObjectiveCGenerator::GenerateAll
bool GenerateAll(const std::vector< const FileDescriptor * > &files, const string ¶meter, GeneratorContext *context, string *error) const
Definition:
objectivec_generator.cc:60
google::protobuf::compiler::objectivec::Options::generate_for_named_framework
string generate_for_named_framework
Definition:
objectivec_helpers.h:54
options
Message * options
Definition:
src/google/protobuf/descriptor.cc:3119
google::protobuf::compiler::GeneratorContext::Open
virtual io::ZeroCopyOutputStream * Open(const std::string &filename)=0
google::protobuf::compiler::objectivec::Options
Definition:
objectivec_helpers.h:50
objectivec_generator.h
objectivec_helpers.h
error
Definition:
cJSON.c:88
google::protobuf::compiler::objectivec::FilePath
string FilePath(const FileDescriptor *file)
Definition:
objectivec_helpers.cc:404
google::protobuf::compiler::objectivec::ObjectiveCGenerator::~ObjectiveCGenerator
~ObjectiveCGenerator()
Definition:
objectivec_generator.cc:46
strutil.h
google::protobuf::compiler::objectivec::Options::expected_prefixes_suppressions
std::vector< string > expected_prefixes_suppressions
Definition:
objectivec_helpers.h:53
google::protobuf::compiler::objectivec::FileGenerator::GenerateSource
void GenerateSource(io::Printer *printer)
Definition:
objectivec_file.cc:338
printer.h
google::protobuf::compiler::objectivec::Options::expected_prefixes_path
string expected_prefixes_path
Definition:
objectivec_helpers.h:52
google::protobuf::compiler::objectivec::ObjectiveCGenerator::HasGenerateAll
bool HasGenerateAll() const
Definition:
objectivec_generator.cc:48
google::protobuf::compiler::objectivec::ValidateObjCClassPrefixes
bool ValidateObjCClassPrefixes(const std::vector< const FileDescriptor * > &files, const Options &generation_options, string *out_error)
Definition:
objectivec_helpers.cc:1202
google::protobuf::compiler::objectivec::ObjectiveCGenerator::ObjectiveCGenerator
ObjectiveCGenerator()
Definition:
objectivec_generator.cc:44
google::protobuf::io::Printer
Definition:
printer.h:181
objectivec_file.h
i
int i
Definition:
gmock-matchers_test.cc:764
google::protobuf::compiler::objectivec::FileGenerator::GenerateHeader
void GenerateHeader(io::Printer *printer)
Definition:
objectivec_file.cc:211
google::protobuf::compiler::objectivec::FileGenerator
Definition:
objectivec_file.h:50
google::protobuf::SplitStringUsing
void SplitStringUsing(const string &full, const char *delim, std::vector< string > *result)
Definition:
strutil.cc:229
google::protobuf::compiler::ParseGeneratorParameter
void ParseGeneratorParameter(const std::string &text, std::vector< std::pair< std::string, std::string > > *output)
Definition:
code_generator.cc:101
first
GLint first
Definition:
glcorearb.h:2830
google::protobuf::FileDescriptor
Definition:
src/google/protobuf/descriptor.h:1320
output
const upb_json_parsermethod const upb_symtab upb_sink * output
Definition:
ruby/ext/google/protobuf_c/upb.h:10503
google::protobuf::compiler::GeneratorContext
Definition:
code_generator.h:119
google::protobuf::compiler::objectivec::ObjectiveCGenerator::Generate
bool Generate(const FileDescriptor *file, const string ¶meter, GeneratorContext *context, string *error) const
Definition:
objectivec_generator.cc:52
compiler
Definition:
plugin.pb.cc:22
google
Definition:
data_proto2_to_proto3_util.h:11
libaditof
Author(s):
autogenerated on Wed May 21 2025 02:06:57