diff options
author | Feng Xiao <xfxyjwf@gmail.com> | 2018-08-08 17:00:41 -0700 |
---|---|---|
committer | Feng Xiao <xfxyjwf@gmail.com> | 2018-08-08 17:00:41 -0700 |
commit | 6bbe197e9c1b6fc38cbdc45e3bf83fa7ced792a3 (patch) | |
tree | e575738adf52d24b883cca5e8928a5ded31caba1 /src/google/protobuf/io/printer.h | |
parent | e7746f487cb9cca685ffb1b3d7dccc5554b618a4 (diff) | |
download | protobuf-6bbe197e9c1b6fc38cbdc45e3bf83fa7ced792a3.tar.gz protobuf-6bbe197e9c1b6fc38cbdc45e3bf83fa7ced792a3.tar.bz2 protobuf-6bbe197e9c1b6fc38cbdc45e3bf83fa7ced792a3.zip |
Down-integrate from google3.
Diffstat (limited to 'src/google/protobuf/io/printer.h')
-rw-r--r-- | src/google/protobuf/io/printer.h | 105 |
1 files changed, 59 insertions, 46 deletions
diff --git a/src/google/protobuf/io/printer.h b/src/google/protobuf/io/printer.h index d11745ce..236aed43 100644 --- a/src/google/protobuf/io/printer.h +++ b/src/google/protobuf/io/printer.h @@ -51,12 +51,19 @@ class ZeroCopyOutputStream; // zero_copy_stream.h // Records annotations about a Printer's output. class LIBPROTOBUF_EXPORT AnnotationCollector { public: + // Annotation is a ofset range and a payload pair. + typedef std::pair<std::pair<size_t, size_t>, string> Annotation; + // Records that the bytes in file_path beginning with begin_offset and ending // before end_offset are associated with the SourceCodeInfo-style path. virtual void AddAnnotation(size_t begin_offset, size_t end_offset, const string& file_path, const std::vector<int>& path) = 0; + // TODO(gerbens) I don't see why we need virtuals here. Just a vector of + // range, payload pairs stored in a context should suffice. + virtual void AddAnnotationNew(Annotation& a) {} + virtual ~AnnotationCollector() {} }; @@ -84,6 +91,13 @@ class AnnotationProtoCollector : public AnnotationCollector { annotation->set_begin(begin_offset); annotation->set_end(end_offset); } + // Override for AnnotationCollector::AddAnnotation. + virtual void AddAnnotationNew(Annotation& a) { + auto* annotation = annotation_proto_->add_annotation(); + annotation->ParseFromString(a.second); + annotation->set_begin(a.first.first); + annotation->set_end(a.first.second); + } private: // The protocol buffer to which new annotations should be added. @@ -229,51 +243,11 @@ class LIBPROTOBUF_EXPORT Printer { void Print(const std::map<string, string>& variables, const char* text); // Like the first Print(), except the substitutions are given as parameters. - void Print(const char* text); - // Like the first Print(), except the substitutions are given as parameters. - void Print(const char* text, const char* variable, const string& value); - // Like the first Print(), except the substitutions are given as parameters. - void Print(const char* text, const char* variable1, const string& value1, - const char* variable2, const string& value2); - // Like the first Print(), except the substitutions are given as parameters. - void Print(const char* text, const char* variable1, const string& value1, - const char* variable2, const string& value2, - const char* variable3, const string& value3); - // Like the first Print(), except the substitutions are given as parameters. - void Print(const char* text, const char* variable1, const string& value1, - const char* variable2, const string& value2, - const char* variable3, const string& value3, - const char* variable4, const string& value4); - // Like the first Print(), except the substitutions are given as parameters. - void Print(const char* text, const char* variable1, const string& value1, - const char* variable2, const string& value2, - const char* variable3, const string& value3, - const char* variable4, const string& value4, - const char* variable5, const string& value5); - // Like the first Print(), except the substitutions are given as parameters. - void Print(const char* text, const char* variable1, const string& value1, - const char* variable2, const string& value2, - const char* variable3, const string& value3, - const char* variable4, const string& value4, - const char* variable5, const string& value5, - const char* variable6, const string& value6); - // Like the first Print(), except the substitutions are given as parameters. - void Print(const char* text, const char* variable1, const string& value1, - const char* variable2, const string& value2, - const char* variable3, const string& value3, - const char* variable4, const string& value4, - const char* variable5, const string& value5, - const char* variable6, const string& value6, - const char* variable7, const string& value7); - // Like the first Print(), except the substitutions are given as parameters. - void Print(const char* text, const char* variable1, const string& value1, - const char* variable2, const string& value2, - const char* variable3, const string& value3, - const char* variable4, const string& value4, - const char* variable5, const string& value5, - const char* variable6, const string& value6, - const char* variable7, const string& value7, - const char* variable8, const string& value8); + template <typename... Args> + void Print(const char* text, const Args&... args) { + std::map<string, string> vars; + PrintInternal(&vars, text, args...); + } // Indent text by two spaces. After calling Indent(), two spaces will be // inserted at the beginning of each line of text. Indent() may be called @@ -296,6 +270,14 @@ class LIBPROTOBUF_EXPORT Printer { // This method does not look for newlines to add indentation. void WriteRaw(const char* data, int size); + // FormatInternal is a helper function not meant to use directly, use + // compiler::cpp::Formatter instead. This function is meant to support + // formatting text using named variables (eq. "$foo$) from a lookup map (vars) + // and variables directly supplied by arguments (eq "$1$" meaning first + // argument which is the zero index element of args). + void FormatInternal(const std::vector<string>& args, + const std::map<string, string>& vars, const char* format); + // True if any write to the underlying stream failed. (We don't just // crash in this case because this is an I/O failure, not a programming // error.) @@ -311,9 +293,40 @@ class LIBPROTOBUF_EXPORT Printer { void Annotate(const char* begin_varname, const char* end_varname, const string& file_path, const std::vector<int>& path); + // Base case + void PrintInternal(std::map<string, string>* vars, const char* text) { + Print(*vars, text); + } + + template <typename... Args> + void PrintInternal(std::map<string, string>* vars, const char* text, + const char* key, const string& value, + const Args&... args) { + (*vars)[key] = value; + PrintInternal(vars, text, args...); + } + // Copy size worth of bytes from data to buffer_. void CopyToBuffer(const char* data, int size); + void push_back(char c) { + if (failed_) return; + if (buffer_size_ == 0) { + if (!Next()) return; + } + *buffer_++ = c; + buffer_size_--; + offset_++; + } + + bool Next(); + + inline void IndentIfAtStart(); + const char* WriteVariable( + const std::vector<string>& args, const std::map<string, string>& vars, + const char* format, int* arg_index, + std::vector<AnnotationCollector::Annotation>* annotations); + const char variable_delimiter_; ZeroCopyOutputStream* const output_; @@ -358,6 +371,6 @@ class LIBPROTOBUF_EXPORT Printer { } // namespace io } // namespace protobuf - } // namespace google + #endif // GOOGLE_PROTOBUF_IO_PRINTER_H__ |