37 #ifndef GOOGLE_PROTOBUF_IO_PRINTER_H__
38 #define GOOGLE_PROTOBUF_IO_PRINTER_H__
45 #include <google/protobuf/stubs/common.h>
46 #include <google/protobuf/port_def.inc>
55 class PROTOBUF_EXPORT AnnotationCollector {
62 virtual void AddAnnotation(
size_t begin_offset,
size_t end_offset,
64 const std::vector<int>&
path) = 0;
76 template <
typename AnnotationProto>
77 class AnnotationProtoCollector :
public AnnotationCollector {
87 const std::vector<int>&
path)
override {
88 typename AnnotationProto::Annotation* annotation =
90 for (
int i = 0;
i <
path.size(); ++
i) {
91 annotation->add_path(
path[
i]);
93 annotation->set_source_file(file_path);
94 annotation->set_begin(begin_offset);
100 annotation->ParseFromString(
a.second);
101 annotation->set_begin(
a.first.first);
102 annotation->set_end(
a.first.second);
182 class PROTOBUF_EXPORT
Printer {
193 AnnotationCollector* annotation_collector);
199 template <
typename SomeDescriptor>
208 template <
typename SomeDescriptor>
209 void Annotate(
const char* begin_varname,
const char* end_varname,
211 if (annotation_collector_ == NULL) {
216 std::vector<int>
path;
218 Annotate(begin_varname, end_varname,
descriptor->file()->name(),
path);
224 Annotate(varname, varname, file_name);
231 void Annotate(
const char* begin_varname,
const char* end_varname,
233 if (annotation_collector_ == NULL) {
237 std::vector<int> empty_path;
238 Annotate(begin_varname, end_varname, file_name, empty_path);
246 void Print(
const std::map<std::string, std::string>& variables,
250 template <
typename...
Args>
252 std::map<std::string, std::string> vars;
253 PrintInternal(&vars,
text,
args...);
271 void PrintRaw(
const char*
data);
275 void WriteRaw(
const char*
data,
int size);
282 void FormatInternal(
const std::vector<std::string>&
args,
283 const std::map<std::string, std::string>& vars,
298 void Annotate(
const char* begin_varname,
const char* end_varname,
307 template <
typename...
Args>
316 void CopyToBuffer(
const char*
data,
int size);
320 if (buffer_size_ == 0) {
330 inline void IndentIfAtStart();
331 const char* WriteVariable(
332 const std::vector<std::string>&
args,
333 const std::map<std::string, std::string>& vars,
const char*
format,
335 std::vector<AnnotationCollector::Annotation>* annotations);
337 const char variable_delimiter_;
348 bool at_start_of_line_;
358 std::map<std::string, std::pair<size_t, size_t> > substitutions_;
363 std::vector<std::string> line_start_variables_;
369 bool GetSubstitutionRange(
const char* varname,
370 std::pair<size_t, size_t>*
range);
383 #include <google/protobuf/port_undef.inc>
385 #endif // GOOGLE_PROTOBUF_IO_PRINTER_H__