printer.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 <cctype>
36 
41 
42 namespace google {
43 namespace protobuf {
44 namespace io {
45 
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 
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 
68  // Only BackUp() if we have called Next() at least once and never failed.
69  if (buffer_size_ > 0 && !failed_) {
71  }
72 }
73 
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 
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 
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.
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 
185 void Printer::Indent() { indent_ += " "; }
186 
188  if (indent_.empty()) {
189  GOOGLE_LOG(DFATAL) << " Outdent() without matching Indent().";
190  return;
191  }
192 
193  indent_.resize(indent_.size() - 2);
194 }
195 
197  WriteRaw(data.data(), data.size());
198 }
199 
200 void Printer::PrintRaw(const char* data) {
201  if (failed_) return;
202  WriteRaw(data, strlen(data));
203 }
204 
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 
229 }
230 
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 
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_);
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 
270  if (at_start_of_line_) {
271  CopyToBuffer(indent_.data(), indent_.size());
272  at_start_of_line_ = false;
273  }
274 }
275 
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 
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_;
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
zero_copy_stream.h
google::protobuf::io::Printer::Print
void Print(const std::map< std::string, std::string > &variables, const char *text)
Definition: printer.cc:112
GOOGLE_CHECK_EQ
#define GOOGLE_CHECK_EQ(A, B)
Definition: logging.h:156
google::protobuf::io::Printer::substitutions_
std::map< std::string, std::pair< size_t, size_t > > substitutions_
Definition: printer.h:357
end
GLuint GLuint end
Definition: glcorearb.h:2858
google::protobuf::io::Printer::WriteRaw
void WriteRaw(const char *data, int size)
Definition: printer.cc:205
google::protobuf::io::Printer::annotation_collector_
AnnotationCollector *const annotation_collector_
Definition: printer.h:373
NULL
NULL
Definition: test_security_zap.cpp:405
FATAL
const int FATAL
Definition: log_severity.h:60
google::protobuf::io::Printer::Printer
Printer(ZeroCopyOutputStream *output, char variable_delimiter)
Definition: printer.cc:46
google::protobuf::io::Printer::line_start_variables_
std::vector< std::string > line_start_variables_
Definition: printer.h:362
google::protobuf::io::Printer::variable_delimiter_
const char variable_delimiter_
Definition: printer.h:336
google::protobuf::io::Printer::PrintRaw
void PrintRaw(const std::string &data)
Definition: printer.cc:196
string
GLsizei const GLchar *const * string
Definition: glcorearb.h:3083
idx
static uint32_t idx(tarjan *t, const upb_refcounted *r)
Definition: ruby/ext/google/protobuf_c/upb.c:5925
output_
std::string output_
Definition: text_format.cc:1531
google::protobuf::io::Printer::indent_
std::string indent_
Definition: printer.h:346
google::protobuf::io::Printer::Indent
void Indent()
Definition: printer.cc:185
range
GLenum GLint * range
Definition: glcorearb.h:3963
begin
static size_t begin(const upb_table *t)
Definition: php/ext/google/protobuf/upb.c:4898
google::protobuf::io::Printer::WriteVariable
const char * WriteVariable(const std::vector< std::string > &args, const std::map< std::string, std::string > &vars, const char *format, int *arg_index, std::vector< AnnotationCollector::Annotation > *annotations)
Definition: printer.cc:306
google::protobuf::io::Printer::push_back
void push_back(char c)
Definition: printer.h:317
google::protobuf::io::Printer::~Printer
~Printer()
Definition: printer.cc:67
path
GLsizei const GLchar ** path
Definition: glcorearb.h:3658
google::protobuf::io::Printer::buffer_
char * buffer_
Definition: printer.h:339
start
GLuint start
Definition: glcorearb.h:2858
format
GLint GLint GLsizei GLint GLenum format
Definition: glcorearb.h:2773
printer.h
GOOGLE_LOG
#define GOOGLE_LOG(LEVEL)
Definition: logging.h:146
size
#define size
Definition: glcorearb.h:2944
google::protobuf::io::Printer::Annotate
void Annotate(const char *varname, const SomeDescriptor *descriptor)
Definition: printer.h:199
GOOGLE_CHECK
#define GOOGLE_CHECK(EXPRESSION)
Definition: logging.h:153
i
int i
Definition: gmock-matchers_test.cc:764
google::protobuf::io::Printer::output_
ZeroCopyOutputStream *const output_
Definition: printer.h:338
google::protobuf::io::AnnotationCollector
Definition: printer.h:54
common.h
size
GLsizeiptr size
Definition: glcorearb.h:2943
google::protobuf::io::Printer::GetSubstitutionRange
bool GetSubstitutionRange(const char *varname, std::pair< size_t, size_t > *range)
Definition: printer.cc:74
google::protobuf::io::ZeroCopyOutputStream
Definition: zero_copy_stream.h:183
logging.h
GOOGLE_CHECK_GE
#define GOOGLE_CHECK_GE(A, B)
Definition: logging.h:161
google::protobuf::io::Printer::IndentIfAtStart
void IndentIfAtStart()
Definition: printer.cc:269
data
GLint GLenum GLsizei GLsizei GLsizei GLint GLsizei const GLvoid * data
Definition: glcorearb.h:2879
google::protobuf::io::Printer::failed_
bool failed_
Definition: printer.h:348
google::protobuf::io::Printer::buffer_size_
int buffer_size_
Definition: printer.h:340
true
#define true
Definition: cJSON.c:65
google::protobuf::io::AnnotationCollector::AddAnnotationNew
virtual void AddAnnotationNew(Annotation &a)
Definition: printer.h:67
buffer_
static uint8 buffer_[kBufferSize]
Definition: coded_stream_unittest.cc:136
google::protobuf::io::ZeroCopyOutputStream::Next
virtual bool Next(void **data, int *size)=0
google::protobuf::io::Printer::at_start_of_line_
bool at_start_of_line_
Definition: printer.h:347
google::protobuf::io::Printer::CopyToBuffer
void CopyToBuffer(const char *data, int size)
Definition: printer.cc:243
output
const upb_json_parsermethod const upb_symtab upb_sink * output
Definition: ruby/ext/google/protobuf_c/upb.h:10503
google::protobuf::io::Printer::offset_
size_t offset_
Definition: printer.h:344
false
#define false
Definition: cJSON.c:70
google::protobuf::io::AnnotationCollector::AddAnnotation
virtual void AddAnnotation(size_t begin_offset, size_t end_offset, const std::string &file_path, const std::vector< int > &path)=0
a
GLboolean GLboolean GLboolean GLboolean a
Definition: glcorearb.h:3228
google::protobuf::io::Printer::Next
bool Next()
Definition: printer.cc:231
google::protobuf::io::Printer::Outdent
void Outdent()
Definition: printer.cc:187
google::protobuf::io::Printer::FormatInternal
void FormatInternal(const std::vector< std::string > &args, const std::map< std::string, std::string > &vars, const char *format)
Definition: printer.cc:276
it
MapIter it
Definition: php/ext/google/protobuf/map.c:205
google
Definition: data_proto2_to_proto3_util.h:11
benchmarks.python.py_benchmark.args
args
Definition: py_benchmark.py:24
google::protobuf::io::ZeroCopyOutputStream::BackUp
virtual void BackUp(int count)=0


libaditof
Author(s):
autogenerated on Wed May 21 2025 02:06:57