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 <cctype>
36
37 #include <google/protobuf/stubs/logging.h>
38 #include <google/protobuf/stubs/common.h>
39 #include <google/protobuf/io/printer.h>
40 #include <google/protobuf/io/zero_copy_stream.h>
41
42 namespace google {
43 namespace protobuf {
44 namespace io {
45
Printer(ZeroCopyOutputStream * output,char variable_delimiter)46 Printer::Printer(ZeroCopyOutputStream* output, char variable_delimiter)
47 : variable_delimiter_(variable_delimiter),
48 output_(output),
49 buffer_(NULL),
50 buffer_size_(0),
51 offset_(0),
52 at_start_of_line_(true),
53 failed_(false),
54 annotation_collector_(NULL) {}
55
Printer(ZeroCopyOutputStream * output,char variable_delimiter,AnnotationCollector * annotation_collector)56 Printer::Printer(ZeroCopyOutputStream* output, char variable_delimiter,
57 AnnotationCollector* annotation_collector)
58 : variable_delimiter_(variable_delimiter),
59 output_(output),
60 buffer_(NULL),
61 buffer_size_(0),
62 offset_(0),
63 at_start_of_line_(true),
64 failed_(false),
65 annotation_collector_(annotation_collector) {}
66
~Printer()67 Printer::~Printer() {
68 // Only BackUp() if we have called Next() at least once and never failed.
69 if (buffer_size_ > 0 && !failed_) {
70 output_->BackUp(buffer_size_);
71 }
72 }
73
GetSubstitutionRange(const char * varname,std::pair<size_t,size_t> * range)74 bool Printer::GetSubstitutionRange(const char* varname,
75 std::pair<size_t, size_t>* range) {
76 std::map<std::string, std::pair<size_t, size_t> >::const_iterator iter =
77 substitutions_.find(varname);
78 if (iter == substitutions_.end()) {
79 GOOGLE_LOG(DFATAL) << " Undefined variable in annotation: " << varname;
80 return false;
81 }
82 if (iter->second.first > iter->second.second) {
83 GOOGLE_LOG(DFATAL) << " Variable used for annotation used multiple times: "
84 << varname;
85 return false;
86 }
87 *range = iter->second;
88 return true;
89 }
90
Annotate(const char * begin_varname,const char * end_varname,const std::string & file_path,const std::vector<int> & path)91 void Printer::Annotate(const char* begin_varname, const char* end_varname,
92 const std::string& file_path,
93 const std::vector<int>& path) {
94 if (annotation_collector_ == NULL) {
95 // Can't generate signatures with this Printer.
96 return;
97 }
98 std::pair<size_t, size_t> begin, end;
99 if (!GetSubstitutionRange(begin_varname, &begin) ||
100 !GetSubstitutionRange(end_varname, &end)) {
101 return;
102 }
103 if (begin.first > end.second) {
104 GOOGLE_LOG(DFATAL) << " Annotation has negative length from " << begin_varname
105 << " to " << end_varname;
106 } else {
107 annotation_collector_->AddAnnotation(begin.first, end.second, file_path,
108 path);
109 }
110 }
111
Print(const std::map<std::string,std::string> & variables,const char * text)112 void Printer::Print(const std::map<std::string, std::string>& variables,
113 const char* text) {
114 int size = strlen(text);
115 int pos = 0; // The number of bytes we've written so far.
116 substitutions_.clear();
117 line_start_variables_.clear();
118
119 for (int i = 0; i < size; i++) {
120 if (text[i] == '\n') {
121 // Saw newline. If there is more text, we may need to insert an indent
122 // here. So, write what we have so far, including the '\n'.
123 WriteRaw(text + pos, i - pos + 1);
124 pos = i + 1;
125
126 // Setting this true will cause the next WriteRaw() to insert an indent
127 // first.
128 at_start_of_line_ = true;
129 line_start_variables_.clear();
130
131 } else if (text[i] == variable_delimiter_) {
132 // Saw the start of a variable name.
133
134 // Write what we have so far.
135 WriteRaw(text + pos, i - pos);
136 pos = i + 1;
137
138 // Find closing delimiter.
139 const char* end = strchr(text + pos, variable_delimiter_);
140 if (end == NULL) {
141 GOOGLE_LOG(DFATAL) << " Unclosed variable name.";
142 end = text + pos;
143 }
144 int endpos = end - text;
145
146 std::string varname(text + pos, endpos - pos);
147 if (varname.empty()) {
148 // Two delimiters in a row reduce to a literal delimiter character.
149 WriteRaw(&variable_delimiter_, 1);
150 } else {
151 // Replace with the variable's value.
152 std::map<std::string, std::string>::const_iterator iter =
153 variables.find(varname);
154 if (iter == variables.end()) {
155 GOOGLE_LOG(DFATAL) << " Undefined variable: " << varname;
156 } else {
157 if (at_start_of_line_ && iter->second.empty()) {
158 line_start_variables_.push_back(varname);
159 }
160 WriteRaw(iter->second.data(), iter->second.size());
161 std::pair<std::map<std::string, std::pair<size_t, size_t> >::iterator,
162 bool>
163 inserted = substitutions_.insert(std::make_pair(
164 varname,
165 std::make_pair(offset_ - iter->second.size(), offset_)));
166 if (!inserted.second) {
167 // This variable was used multiple times. Make its span have
168 // negative length so we can detect it if it gets used in an
169 // annotation.
170 inserted.first->second = std::make_pair(1, 0);
171 }
172 }
173 }
174
175 // Advance past this variable.
176 i = endpos;
177 pos = endpos + 1;
178 }
179 }
180
181 // Write the rest.
182 WriteRaw(text + pos, size - pos);
183 }
184
Indent()185 void Printer::Indent() { indent_ += " "; }
186
Outdent()187 void Printer::Outdent() {
188 if (indent_.empty()) {
189 GOOGLE_LOG(DFATAL) << " Outdent() without matching Indent().";
190 return;
191 }
192
193 indent_.resize(indent_.size() - 2);
194 }
195
PrintRaw(const std::string & data)196 void Printer::PrintRaw(const std::string& data) {
197 WriteRaw(data.data(), data.size());
198 }
199
PrintRaw(const char * data)200 void Printer::PrintRaw(const char* data) {
201 if (failed_) return;
202 WriteRaw(data, strlen(data));
203 }
204
WriteRaw(const char * data,int size)205 void Printer::WriteRaw(const char* data, int size) {
206 if (failed_) return;
207 if (size == 0) return;
208
209 if (at_start_of_line_ && (size > 0) && (data[0] != '\n')) {
210 // Insert an indent.
211 at_start_of_line_ = false;
212 CopyToBuffer(indent_.data(), indent_.size());
213 if (failed_) return;
214 // Fix up empty variables (e.g., "{") that should be annotated as
215 // coming after the indent.
216 for (std::vector<std::string>::iterator i = line_start_variables_.begin();
217 i != line_start_variables_.end(); ++i) {
218 substitutions_[*i].first += indent_.size();
219 substitutions_[*i].second += indent_.size();
220 }
221 }
222
223 // If we're going to write any data, clear line_start_variables_, since
224 // we've either updated them in the block above or they no longer refer to
225 // the current line.
226 line_start_variables_.clear();
227
228 CopyToBuffer(data, size);
229 }
230
Next()231 bool Printer::Next() {
232 do {
233 void* void_buffer;
234 if (!output_->Next(&void_buffer, &buffer_size_)) {
235 failed_ = true;
236 return false;
237 }
238 buffer_ = reinterpret_cast<char*>(void_buffer);
239 } while (buffer_size_ == 0);
240 return true;
241 }
242
CopyToBuffer(const char * data,int size)243 void Printer::CopyToBuffer(const char* data, int size) {
244 if (failed_) return;
245 if (size == 0) return;
246
247 while (size > buffer_size_) {
248 // Data exceeds space in the buffer. Copy what we can and request a
249 // new buffer.
250 if (buffer_size_ > 0) {
251 memcpy(buffer_, data, buffer_size_);
252 offset_ += buffer_size_;
253 data += buffer_size_;
254 size -= buffer_size_;
255 }
256 void* void_buffer;
257 failed_ = !output_->Next(&void_buffer, &buffer_size_);
258 if (failed_) return;
259 buffer_ = reinterpret_cast<char*>(void_buffer);
260 }
261
262 // Buffer is big enough to receive the data; copy it.
263 memcpy(buffer_, data, size);
264 buffer_ += size;
265 buffer_size_ -= size;
266 offset_ += size;
267 }
268
IndentIfAtStart()269 void Printer::IndentIfAtStart() {
270 if (at_start_of_line_) {
271 CopyToBuffer(indent_.data(), indent_.size());
272 at_start_of_line_ = false;
273 }
274 }
275
FormatInternal(const std::vector<std::string> & args,const std::map<std::string,std::string> & vars,const char * format)276 void Printer::FormatInternal(const std::vector<std::string>& args,
277 const std::map<std::string, std::string>& vars,
278 const char* format) {
279 auto save = format;
280 int arg_index = 0;
281 std::vector<AnnotationCollector::Annotation> annotations;
282 while (*format) {
283 char c = *format++;
284 switch (c) {
285 case '$':
286 format = WriteVariable(args, vars, format, &arg_index, &annotations);
287 continue;
288 case '\n':
289 at_start_of_line_ = true;
290 line_start_variables_.clear();
291 break;
292 default:
293 IndentIfAtStart();
294 break;
295 }
296 push_back(c);
297 }
298 if (arg_index != args.size()) {
299 GOOGLE_LOG(FATAL) << " Unused arguments. " << save;
300 }
301 if (!annotations.empty()) {
302 GOOGLE_LOG(FATAL) << " Annotation range is not-closed, expect $}$. " << save;
303 }
304 }
305
WriteVariable(const std::vector<string> & args,const std::map<std::string,std::string> & vars,const char * format,int * arg_index,std::vector<AnnotationCollector::Annotation> * annotations)306 const char* Printer::WriteVariable(
307 const std::vector<string>& args,
308 const std::map<std::string, std::string>& vars, const char* format,
309 int* arg_index, std::vector<AnnotationCollector::Annotation>* annotations) {
310 auto start = format;
311 auto end = strchr(format, '$');
312 if (!end) {
313 GOOGLE_LOG(FATAL) << " Unclosed variable name.";
314 }
315 format = end + 1;
316 if (end == start) {
317 // "$$" is an escape for just '$'
318 IndentIfAtStart();
319 push_back('$');
320 return format;
321 }
322 if (*start == '{') {
323 GOOGLE_CHECK(std::isdigit(start[1]));
324 GOOGLE_CHECK_EQ(end - start, 2);
325 int idx = start[1] - '1';
326 if (idx < 0 || idx >= args.size()) {
327 GOOGLE_LOG(FATAL) << "Annotation ${" << idx + 1 << "$ is out of bounds.";
328 }
329 if (idx > *arg_index) {
330 GOOGLE_LOG(FATAL) << "Annotation arg must be in correct order as given. Expected"
331 << " ${" << (*arg_index) + 1 << "$ got ${" << idx + 1 << "$.";
332 } else if (idx == *arg_index) {
333 (*arg_index)++;
334 }
335 IndentIfAtStart();
336 annotations->push_back({{offset_, 0}, args[idx]});
337 return format;
338 } else if (*start == '}') {
339 GOOGLE_CHECK(annotations);
340 if (annotations->empty()) {
341 GOOGLE_LOG(FATAL) << "Unexpected end of annotation found.";
342 }
343 auto& a = annotations->back();
344 a.first.second = offset_;
345 if (annotation_collector_) annotation_collector_->AddAnnotationNew(a);
346 annotations->pop_back();
347 return format;
348 }
349 auto start_var = start;
350 while (start_var < end && *start_var == ' ') start_var++;
351 if (start_var == end) {
352 GOOGLE_LOG(FATAL) << " Empty variable.";
353 }
354 auto end_var = end;
355 while (start_var < end_var && *(end_var - 1) == ' ') end_var--;
356 std::string var_name{
357 start_var, static_cast<std::string::size_type>(end_var - start_var)};
358 std::string sub;
359 if (std::isdigit(var_name[0])) {
360 GOOGLE_CHECK_EQ(var_name.size(), 1); // No need for multi-digits
361 int idx = var_name[0] - '1'; // Start counting at 1
362 GOOGLE_CHECK_GE(idx, 0);
363 if (idx >= args.size()) {
364 GOOGLE_LOG(FATAL) << "Argument $" << idx + 1 << "$ is out of bounds.";
365 }
366 if (idx > *arg_index) {
367 GOOGLE_LOG(FATAL) << "Arguments must be used in same order as given. Expected $"
368 << (*arg_index) + 1 << "$ got $" << idx + 1 << "$.";
369 } else if (idx == *arg_index) {
370 (*arg_index)++;
371 }
372 sub = args[idx];
373 } else {
374 auto it = vars.find(var_name);
375 if (it == vars.end()) {
376 GOOGLE_LOG(FATAL) << " Unknown variable: " << var_name << ".";
377 }
378 sub = it->second;
379 }
380
381 // By returning here in case of empty we also skip possible spaces inside
382 // the $...$, i.e. "void$ dllexpor$ f();" -> "void f();" in the empty case.
383 if (sub.empty()) return format;
384
385 // We're going to write something non-empty so we need a possible indent.
386 IndentIfAtStart();
387
388 // Write the possible spaces in front.
389 CopyToBuffer(start, start_var - start);
390 // Write a non-empty substituted variable.
391 CopyToBuffer(sub.c_str(), sub.size());
392 // Finish off with writing possible trailing spaces.
393 CopyToBuffer(end_var, end - end_var);
394 return format;
395 }
396
397 } // namespace io
398 } // namespace protobuf
399 } // namespace google
400