generator_helpers.h
Go to the documentation of this file.
1 /*
2  *
3  * Copyright 2015 gRPC authors.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  */
18 
19 #ifndef GRPC_INTERNAL_COMPILER_GENERATOR_HELPERS_H
20 #define GRPC_INTERNAL_COMPILER_GENERATOR_HELPERS_H
21 
22 #include <iostream>
23 #include <map>
24 #include <sstream>
25 #include <string>
26 #include <vector>
27 
28 #include "src/compiler/config.h"
29 
30 namespace grpc_generator {
31 
33  if (filename->length() >= suffix.length()) {
34  size_t suffix_pos = filename->length() - suffix.length();
35  if (filename->compare(suffix_pos, std::string::npos, suffix) == 0) {
36  filename->resize(filename->size() - suffix.size());
37  return true;
38  }
39  }
40 
41  return false;
42 }
43 
45  if (name->length() >= prefix.length()) {
46  if (name->substr(0, prefix.size()) == prefix) {
47  *name = name->substr(prefix.size());
48  return true;
49  }
50  }
51  return false;
52 }
53 
55  if (!StripSuffix(&filename, ".protodevel")) {
56  StripSuffix(&filename, ".proto");
57  }
58  return filename;
59 }
60 
62  const std::string& to, bool replace_all) {
63  size_t pos = 0;
64 
65  do {
66  pos = str.find(from, pos);
67  if (pos == std::string::npos) {
68  break;
69  }
70  str.replace(pos, from.length(), to);
71  pos += to.length();
72  } while (replace_all);
73 
74  return str;
75 }
76 
78  const std::string& to) {
79  return StringReplace(str, from, to, true);
80 }
81 
82 inline std::vector<std::string> tokenize(const std::string& input,
83  const std::string& delimiters) {
84  std::vector<std::string> tokens;
85  size_t pos, last_pos = 0;
86 
87  for (;;) {
88  bool done = false;
89  pos = input.find_first_of(delimiters, last_pos);
90  if (pos == std::string::npos) {
91  done = true;
92  pos = input.length();
93  }
94 
95  tokens.push_back(input.substr(last_pos, pos - last_pos));
96  if (done) return tokens;
97 
98  last_pos = pos + 1;
99  }
100 }
101 
103  if (s.empty()) {
104  return s;
105  }
106  s[0] = ::toupper(s[0]);
107  return s;
108 }
109 
111  if (s.empty()) {
112  return s;
113  }
114  s[0] = ::tolower(s[0]);
115  return s;
116 }
117 
119  std::vector<std::string> tokens = tokenize(str, "_");
120  std::string result = "";
121  for (unsigned int i = 0; i < tokens.size(); i++) {
122  result += CapitalizeFirstLetter(tokens[i]);
123  }
124  return result;
125 }
126 
128  const grpc::protobuf::FileDescriptor* file, bool include_package_path) {
129  std::vector<std::string> tokens = tokenize(StripProto(file->name()), "/");
130  std::string result = "";
131  if (include_package_path) {
132  for (unsigned int i = 0; i < tokens.size() - 1; i++) {
133  result += tokens[i] + "/";
134  }
135  }
136  result += LowerUnderscoreToUpperCamel(tokens.back());
137  return result;
138 }
139 
142  return FileNameInUpperCamel(file, true);
143 }
144 
150 };
151 
154  if (method->client_streaming()) {
155  if (method->server_streaming()) {
157  } else {
159  }
160  } else {
161  if (method->server_streaming()) {
163  } else {
165  }
166  }
167 }
168 
169 inline void Split(const std::string& s, char /*delim*/,
170  std::vector<std::string>* append_to) {
171  std::istringstream iss(s);
172  std::string piece;
173  while (std::getline(iss, piece)) {
174  append_to->push_back(piece);
175  }
176 }
177 
182 };
183 
184 // Get all the raw comments and append each line without newline to out.
185 template <typename DescriptorType>
186 inline void GetComment(const DescriptorType* desc, CommentType type,
187  std::vector<std::string>* out) {
189  if (!desc->GetSourceLocation(&location)) {
190  return;
191  }
193  const std::string& comments = type == COMMENTTYPE_LEADING
194  ? location.leading_comments
195  : location.trailing_comments;
196  Split(comments, '\n', out);
197  } else if (type == COMMENTTYPE_LEADING_DETACHED) {
198  for (unsigned int i = 0; i < location.leading_detached_comments.size();
199  i++) {
200  Split(location.leading_detached_comments[i], '\n', out);
201  out->push_back("");
202  }
203  } else {
204  std::cerr << "Unknown comment type " << type << std::endl;
205  abort();
206  }
207 }
208 
209 // Each raw comment line without newline is appended to out.
210 // For file level leading and detached leading comments, we return comments
211 // above syntax line. Return nothing for trailing comments.
212 template <>
214  CommentType type, std::vector<std::string>* out) {
215  if (type == COMMENTTYPE_TRAILING) {
216  return;
217  }
219  std::vector<int> path;
220  path.push_back(grpc::protobuf::FileDescriptorProto::kSyntaxFieldNumber);
221  if (!desc->GetSourceLocation(path, &location)) {
222  return;
223  }
224  if (type == COMMENTTYPE_LEADING) {
225  Split(location.leading_comments, '\n', out);
226  } else if (type == COMMENTTYPE_LEADING_DETACHED) {
227  for (unsigned int i = 0; i < location.leading_detached_comments.size();
228  i++) {
229  Split(location.leading_detached_comments[i], '\n', out);
230  out->push_back("");
231  }
232  } else {
233  std::cerr << "Unknown comment type " << type << std::endl;
234  abort();
235  }
236 }
237 
238 // Add prefix and newline to each comment line and concatenate them together.
239 // Make sure there is a space after the prefix unless the line is empty.
241  const std::vector<std::string>& in, const std::string& prefix) {
242  std::ostringstream oss;
243  for (auto it = in.begin(); it != in.end(); it++) {
244  const std::string& elem = *it;
245  if (elem.empty()) {
246  oss << prefix << "\n";
247  } else if (elem[0] == ' ') {
248  oss << prefix << elem << "\n";
249  } else {
250  oss << prefix << " " << elem << "\n";
251  }
252  }
253  return oss.str();
254 }
255 
256 template <typename DescriptorType>
257 inline std::string GetPrefixedComments(const DescriptorType* desc, bool leading,
258  const std::string& prefix) {
259  std::vector<std::string> out;
260  if (leading) {
263  std::vector<std::string> leading;
265  &leading);
266  out.insert(out.end(), leading.begin(), leading.end());
267  } else {
269  &out);
270  }
272 }
273 
274 } // namespace grpc_generator
275 
276 #endif // GRPC_INTERNAL_COMPILER_GENERATOR_HELPERS_H
xds_interop_client.str
str
Definition: xds_interop_client.py:487
_gevent_test_main.result
result
Definition: _gevent_test_main.py:96
filename
const char * filename
Definition: bloaty/third_party/zlib/contrib/minizip/ioapi.h:135
gen_build_yaml.out
dictionary out
Definition: src/benchmark/gen_build_yaml.py:24
regen-readme.it
it
Definition: regen-readme.py:15
pos
int pos
Definition: libuv/docs/code/tty-gravity/main.c:11
grpc_generator::StringReplace
std::string StringReplace(std::string str, const std::string &from, const std::string &to, bool replace_all)
Definition: generator_helpers.h:61
grpc_generator::LowerUnderscoreToUpperCamel
std::string LowerUnderscoreToUpperCamel(std::string str)
Definition: generator_helpers.h:118
grpc::protobuf::FileDescriptor
GRPC_CUSTOM_FILEDESCRIPTOR FileDescriptor
Definition: include/grpcpp/impl/codegen/config_protobuf.h:85
grpc_generator::GetPrefixedComments
std::string GetPrefixedComments(const DescriptorType *desc, bool leading, const std::string &prefix)
Definition: generator_helpers.h:257
config.h
elem
Timer elem
Definition: event_engine/iomgr_event_engine/timer_heap_test.cc:109
testing::internal::string
::std::string string
Definition: bloaty/third_party/protobuf/third_party/googletest/googletest/include/gtest/internal/gtest-port.h:881
file
Definition: bloaty/third_party/zlib/examples/gzappend.c:170
grpc_generator::COMMENTTYPE_TRAILING
@ COMMENTTYPE_TRAILING
Definition: generator_helpers.h:180
grpc_generator::METHODTYPE_SERVER_STREAMING
@ METHODTYPE_SERVER_STREAMING
Definition: generator_helpers.h:148
setup.name
name
Definition: setup.py:542
grpc_generator::CommentType
CommentType
Definition: generator_helpers.h:178
check_documentation.path
path
Definition: check_documentation.py:57
to
size_t to
Definition: abseil-cpp/absl/container/internal/layout_test.cc:1385
grpc_generator::GetComment
void GetComment(const DescriptorType *desc, CommentType type, std::vector< std::string > *out)
Definition: generator_helpers.h:186
grpc_generator::GetMethodType
MethodType GetMethodType(const grpc::protobuf::MethodDescriptor *method)
Definition: generator_helpers.h:152
grpc_generator::tokenize
std::vector< std::string > tokenize(const std::string &input, const std::string &delimiters)
Definition: generator_helpers.h:82
from
size_t from
Definition: abseil-cpp/absl/container/internal/layout_test.cc:1384
in
const char * in
Definition: third_party/abseil-cpp/absl/strings/internal/str_format/parser_test.cc:391
grpc_generator::METHODTYPE_BIDI_STREAMING
@ METHODTYPE_BIDI_STREAMING
Definition: generator_helpers.h:149
grpc_generator::COMMENTTYPE_LEADING_DETACHED
@ COMMENTTYPE_LEADING_DETACHED
Definition: generator_helpers.h:181
grpc_generator::MethodType
MethodType
Definition: generator_helpers.h:145
grpc_generator::CapitalizeFirstLetter
std::string CapitalizeFirstLetter(std::string s)
Definition: generator_helpers.h:102
grpc::protobuf::MethodDescriptor
GRPC_CUSTOM_METHODDESCRIPTOR MethodDescriptor
Definition: include/grpcpp/impl/codegen/config_protobuf.h:87
grpc_generator::Split
void Split(const std::string &s, char, std::vector< std::string > *append_to)
Definition: generator_helpers.h:169
done
struct tab * done
Definition: bloaty/third_party/zlib/examples/enough.c:176
grpc_generator::METHODTYPE_NO_STREAMING
@ METHODTYPE_NO_STREAMING
Definition: generator_helpers.h:146
grpc_generator::GenerateCommentsWithPrefix
std::string GenerateCommentsWithPrefix(const std::vector< std::string > &in, const std::string &prefix)
Definition: generator_helpers.h:240
grpc_generator::METHODTYPE_CLIENT_STREAMING
@ METHODTYPE_CLIENT_STREAMING
Definition: generator_helpers.h:147
grpc_generator::LowercaseFirstLetter
std::string LowercaseFirstLetter(std::string s)
Definition: generator_helpers.h:110
suffix
unsigned char suffix[65536]
Definition: bloaty/third_party/zlib/examples/gun.c:164
grpc_generator::StripSuffix
bool StripSuffix(std::string *filename, const std::string &suffix)
Definition: generator_helpers.h:32
grpc_generator::StripProto
std::string StripProto(std::string filename)
Definition: generator_helpers.h:54
grpc_generator::FileNameInUpperCamel
std::string FileNameInUpperCamel(const grpc::protobuf::FileDescriptor *file, bool include_package_path)
Definition: generator_helpers.h:127
prefix
static const char prefix[]
Definition: head_of_line_blocking.cc:28
desc
#define desc
Definition: bloaty/third_party/protobuf/src/google/protobuf/extension_set.h:338
grpc_generator::COMMENTTYPE_LEADING
@ COMMENTTYPE_LEADING
Definition: generator_helpers.h:179
input
std::string input
Definition: bloaty/third_party/protobuf/src/google/protobuf/io/tokenizer_unittest.cc:197
grpc::protobuf::SourceLocation
GRPC_CUSTOM_SOURCELOCATION SourceLocation
Definition: include/grpcpp/impl/codegen/config_protobuf.h:90
asyncio_get_stats.type
type
Definition: asyncio_get_stats.py:37
file::name
char * name
Definition: bloaty/third_party/zlib/examples/gzappend.c:176
method
NSString * method
Definition: ProtoMethod.h:28
grpc_generator
Definition: generator_helpers.h:30
i
uint64_t i
Definition: abseil-cpp/absl/container/btree_benchmark.cc:230
grpc_generator::StripPrefix
bool StripPrefix(std::string *name, const std::string &prefix)
Definition: generator_helpers.h:44


grpc
Author(s):
autogenerated on Thu Mar 13 2025 02:59:26