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 // Utility class for writing text to a ZeroCopyOutputStream. 36 37 #ifndef GOOGLE_PROTOBUF_IO_PRINTER_H__ 38 #define GOOGLE_PROTOBUF_IO_PRINTER_H__ 39 40 #include <string> 41 #include <map> 42 #include <vector> 43 #include <google/protobuf/stubs/common.h> 44 45 namespace google { 46 namespace protobuf { 47 namespace io { 48 49 class ZeroCopyOutputStream; // zero_copy_stream.h 50 51 // Records annotations about a Printer's output. 52 class LIBPROTOBUF_EXPORT AnnotationCollector { 53 public: 54 // Records that the bytes in file_path beginning with begin_offset and ending 55 // before end_offset are associated with the SourceCodeInfo-style path. 56 virtual void AddAnnotation(size_t begin_offset, size_t end_offset, 57 const string& file_path, 58 const vector<int>& path) = 0; 59 ~AnnotationCollector()60 virtual ~AnnotationCollector() {} 61 }; 62 63 // Records annotations about a Printer's output to the given protocol buffer, 64 // assuming that the buffer has an ::Annotation message exposing path, 65 // source_file, begin and end fields. 66 template <typename AnnotationProto> 67 class AnnotationProtoCollector : public AnnotationCollector { 68 public: 69 // annotation_proto is the protocol buffer to which new Annotations should be 70 // added. It is not owned by the AnnotationProtoCollector. AnnotationProtoCollector(AnnotationProto * annotation_proto)71 explicit AnnotationProtoCollector(AnnotationProto* annotation_proto) 72 : annotation_proto_(annotation_proto) {} 73 74 // Override for AnnotationCollector::AddAnnotation. AddAnnotation(size_t begin_offset,size_t end_offset,const string & file_path,const vector<int> & path)75 virtual void AddAnnotation(size_t begin_offset, size_t end_offset, 76 const string& file_path, const vector<int>& path) { 77 typename AnnotationProto::Annotation* annotation = 78 annotation_proto_->add_annotation(); 79 for (int i = 0; i < path.size(); ++i) { 80 annotation->add_path(path[i]); 81 } 82 annotation->set_source_file(file_path); 83 annotation->set_begin(begin_offset); 84 annotation->set_end(end_offset); 85 } 86 87 private: 88 // The protocol buffer to which new annotations should be added. 89 AnnotationProto* const annotation_proto_; 90 }; 91 92 // This simple utility class assists in code generation. It basically 93 // allows the caller to define a set of variables and then output some 94 // text with variable substitutions. Example usage: 95 // 96 // Printer printer(output, '$'); 97 // map<string, string> vars; 98 // vars["name"] = "Bob"; 99 // printer.Print(vars, "My name is $name$."); 100 // 101 // The above writes "My name is Bob." to the output stream. 102 // 103 // Printer aggressively enforces correct usage, crashing (with assert failures) 104 // in the case of undefined variables in debug builds. This helps greatly in 105 // debugging code which uses it. 106 // 107 // If a Printer is constructed with an AnnotationCollector, it will provide it 108 // with annotations that connect the Printer's output to paths that can identify 109 // various descriptors. In the above example, if person_ is a descriptor that 110 // identifies Bob, we can associate the output string "My name is Bob." with 111 // a source path pointing to that descriptor with: 112 // 113 // printer.Annotate("name", person_); 114 // 115 // The AnnotationCollector will be sent an annotation linking the output range 116 // covering "Bob" to the logical path provided by person_. Tools may use 117 // this association to (for example) link "Bob" in the output back to the 118 // source file that defined the person_ descriptor identifying Bob. 119 // 120 // Annotate can only examine variables substituted during the last call to 121 // Print. It is invalid to refer to a variable that was used multiple times 122 // in a single Print call. 123 // 124 // In full generality, one may specify a range of output text using a beginning 125 // substitution variable and an ending variable. The resulting annotation will 126 // span from the first character of the substituted value for the beginning 127 // variable to the last character of the substituted value for the ending 128 // variable. For example, the Annotate call above is equivalent to this one: 129 // 130 // printer.Annotate("name", "name", person_); 131 // 132 // This is useful if multiple variables combine to form a single span of output 133 // that should be annotated with the same source path. For example: 134 // 135 // Printer printer(output, '$'); 136 // map<string, string> vars; 137 // vars["first"] = "Alice"; 138 // vars["last"] = "Smith"; 139 // printer.Print(vars, "My name is $first$ $last$."); 140 // printer.Annotate("first", "last", person_); 141 // 142 // This code would associate the span covering "Alice Smith" in the output with 143 // the person_ descriptor. 144 // 145 // Note that the beginning variable must come before (or overlap with, in the 146 // case of zero-sized substitution values) the ending variable. 147 // 148 // It is also sometimes useful to use variables with zero-sized values as 149 // markers. This avoids issues with multiple references to the same variable 150 // and also allows annotation ranges to span literal text from the Print 151 // templates: 152 // 153 // Printer printer(output, '$'); 154 // map<string, string> vars; 155 // vars["foo"] = "bar"; 156 // vars["function"] = "call"; 157 // vars["mark"] = ""; 158 // printer.Print(vars, "$function$($foo$,$foo$)$mark$"); 159 // printer.Annotate("function", "rmark", call_); 160 // 161 // This code associates the span covering "call(bar,bar)" in the output with the 162 // call_ descriptor. 163 164 class LIBPROTOBUF_EXPORT Printer { 165 public: 166 // Create a printer that writes text to the given output stream. Use the 167 // given character as the delimiter for variables. 168 Printer(ZeroCopyOutputStream* output, char variable_delimiter); 169 170 // Create a printer that writes text to the given output stream. Use the 171 // given character as the delimiter for variables. If annotation_collector 172 // is not null, Printer will provide it with annotations about code written 173 // to the stream. annotation_collector is not owned by Printer. 174 Printer(ZeroCopyOutputStream* output, char variable_delimiter, 175 AnnotationCollector* annotation_collector); 176 177 ~Printer(); 178 179 // Link a subsitution variable emitted by the last call to Print to the object 180 // described by descriptor. 181 template <typename SomeDescriptor> Annotate(const char * varname,const SomeDescriptor * descriptor)182 void Annotate(const char* varname, const SomeDescriptor* descriptor) { 183 Annotate(varname, varname, descriptor); 184 } 185 186 // Link the output range defined by the substitution variables as emitted by 187 // the last call to Print to the object described by descriptor. The range 188 // begins at begin_varname's value and ends after the last character of the 189 // value substituted for end_varname. 190 template <typename SomeDescriptor> Annotate(const char * begin_varname,const char * end_varname,const SomeDescriptor * descriptor)191 void Annotate(const char* begin_varname, const char* end_varname, 192 const SomeDescriptor* descriptor) { 193 if (annotation_collector_ == NULL) { 194 // Annotations aren't turned on for this Printer, so don't pay the cost 195 // of building the location path. 196 return; 197 } 198 vector<int> path; 199 descriptor->GetLocationPath(&path); 200 Annotate(begin_varname, end_varname, descriptor->file()->name(), path); 201 } 202 203 // Link a subsitution variable emitted by the last call to Print to the file 204 // with path file_name. Annotate(const char * varname,const string & file_name)205 void Annotate(const char* varname, const string& file_name) { 206 Annotate(varname, varname, file_name); 207 } 208 209 // Link the output range defined by the substitution variables as emitted by 210 // the last call to Print to the file with path file_name. The range begins 211 // at begin_varname's value and ends after the last character of the value 212 // substituted for end_varname. Annotate(const char * begin_varname,const char * end_varname,const string & file_name)213 void Annotate(const char* begin_varname, const char* end_varname, 214 const string& file_name) { 215 if (annotation_collector_ == NULL) { 216 // Annotations aren't turned on for this Printer. 217 return; 218 } 219 vector<int> empty_path; 220 Annotate(begin_varname, end_varname, file_name, empty_path); 221 } 222 223 // Print some text after applying variable substitutions. If a particular 224 // variable in the text is not defined, this will crash. Variables to be 225 // substituted are identified by their names surrounded by delimiter 226 // characters (as given to the constructor). The variable bindings are 227 // defined by the given map. 228 void Print(const map<string, string>& variables, const char* text); 229 230 // Like the first Print(), except the substitutions are given as parameters. 231 void Print(const char* text); 232 // Like the first Print(), except the substitutions are given as parameters. 233 void Print(const char* text, const char* variable, const string& value); 234 // Like the first Print(), except the substitutions are given as parameters. 235 void Print(const char* text, const char* variable1, const string& value1, 236 const char* variable2, const string& value2); 237 // Like the first Print(), except the substitutions are given as parameters. 238 void Print(const char* text, const char* variable1, const string& value1, 239 const char* variable2, const string& value2, 240 const char* variable3, const string& value3); 241 // Like the first Print(), except the substitutions are given as parameters. 242 void Print(const char* text, const char* variable1, const string& value1, 243 const char* variable2, const string& value2, 244 const char* variable3, const string& value3, 245 const char* variable4, const string& value4); 246 // Like the first Print(), except the substitutions are given as parameters. 247 void Print(const char* text, const char* variable1, const string& value1, 248 const char* variable2, const string& value2, 249 const char* variable3, const string& value3, 250 const char* variable4, const string& value4, 251 const char* variable5, const string& value5); 252 // Like the first Print(), except the substitutions are given as parameters. 253 void Print(const char* text, const char* variable1, const string& value1, 254 const char* variable2, const string& value2, 255 const char* variable3, const string& value3, 256 const char* variable4, const string& value4, 257 const char* variable5, const string& value5, 258 const char* variable6, const string& value6); 259 // Like the first Print(), except the substitutions are given as parameters. 260 void Print(const char* text, const char* variable1, const string& value1, 261 const char* variable2, const string& value2, 262 const char* variable3, const string& value3, 263 const char* variable4, const string& value4, 264 const char* variable5, const string& value5, 265 const char* variable6, const string& value6, 266 const char* variable7, const string& value7); 267 // Like the first Print(), except the substitutions are given as parameters. 268 void Print(const char* text, const char* variable1, const string& value1, 269 const char* variable2, const string& value2, 270 const char* variable3, const string& value3, 271 const char* variable4, const string& value4, 272 const char* variable5, const string& value5, 273 const char* variable6, const string& value6, 274 const char* variable7, const string& value7, 275 const char* variable8, const string& value8); 276 277 // Indent text by two spaces. After calling Indent(), two spaces will be 278 // inserted at the beginning of each line of text. Indent() may be called 279 // multiple times to produce deeper indents. 280 void Indent(); 281 282 // Reduces the current indent level by two spaces, or crashes if the indent 283 // level is zero. 284 void Outdent(); 285 286 // Write a string to the output buffer. 287 // This method does not look for newlines to add indentation. 288 void PrintRaw(const string& data); 289 290 // Write a zero-delimited string to output buffer. 291 // This method does not look for newlines to add indentation. 292 void PrintRaw(const char* data); 293 294 // Write some bytes to the output buffer. 295 // This method does not look for newlines to add indentation. 296 void WriteRaw(const char* data, int size); 297 298 // True if any write to the underlying stream failed. (We don't just 299 // crash in this case because this is an I/O failure, not a programming 300 // error.) failed()301 bool failed() const { return failed_; } 302 303 private: 304 // Link the output range defined by the substitution variables as emitted by 305 // the last call to Print to the object found at the SourceCodeInfo-style path 306 // in a file with path file_path. The range begins at the start of 307 // begin_varname's value and ends after the last character of the value 308 // substituted for end_varname. Note that begin_varname and end_varname 309 // may refer to the same variable. 310 void Annotate(const char* begin_varname, const char* end_varname, 311 const string& file_path, const vector<int>& path); 312 313 const char variable_delimiter_; 314 315 ZeroCopyOutputStream* const output_; 316 char* buffer_; 317 int buffer_size_; 318 // The current position, in bytes, in the output stream. This is equivalent 319 // to the total number of bytes that have been written so far. This value is 320 // used to calculate annotation ranges in the substitutions_ map below. 321 size_t offset_; 322 323 string indent_; 324 bool at_start_of_line_; 325 bool failed_; 326 327 // A map from variable name to [start, end) offsets in the output buffer. 328 // These refer to the offsets used for a variable after the last call to 329 // Print. If a variable was used more than once, the entry used in 330 // this map is set to a negative-length span. For singly-used variables, the 331 // start offset is the beginning of the substitution; the end offset is the 332 // last byte of the substitution plus one (such that (end - start) is the 333 // length of the substituted string). 334 map<string, pair<size_t, size_t> > substitutions_; 335 336 // Returns true and sets range to the substitution range in the output for 337 // varname if varname was used once in the last call to Print. If varname 338 // was not used, or if it was used multiple times, returns false (and 339 // fails a debug assertion). 340 bool GetSubstitutionRange(const char* varname, pair<size_t, size_t>* range); 341 342 // If non-null, annotation_collector_ is used to store annotations about 343 // generated code. 344 AnnotationCollector* const annotation_collector_; 345 346 GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(Printer); 347 }; 348 349 } // namespace io 350 } // namespace protobuf 351 352 } // namespace google 353 #endif // GOOGLE_PROTOBUF_IO_PRINTER_H__ 354