llvm.org GIT mirror llvm / f28e3c5
Add line tracking support to FormattedStream - previously formatted_raw_ostream tracked columns, now it tracks lines too - used by (upcoming) DebugIR pass to know the line number to connect to each IR instruction git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@181463 91177308-0d34-0410-b5e6-96231b3b80d8 Daniel Malea 7 years ago
2 changed file(s) with 42 addition(s) and 29 deletion(s). Raw diff Collapse all Expand all
1515 #define LLVM_SUPPORT_FORMATTEDSTREAM_H
1616
1717 #include "llvm/Support/raw_ostream.h"
18 #include
1819
1920 namespace llvm {
2021
2122 /// formatted_raw_ostream - A raw_ostream that wraps another one and keeps track
22 /// of column position, allowing padding out to specific column boundaries.
23 /// of line and column position, allowing padding out to specific column
24 /// boundaries and querying the number of lines written to the stream.
2325 ///
2426 class formatted_raw_ostream : public raw_ostream {
2527 public:
4345 ///
4446 bool DeleteStream;
4547
46 /// ColumnScanned - The current output column of the data that's
48 /// Position - The current output column and line of the data that's
4749 /// been flushed and the portion of the buffer that's been
48 /// scanned. The column scheme is zero-based.
50 /// scanned. The line and column scheme is zero-based.
4951 ///
50 unsigned ColumnScanned;
52 std::pair Position;
5153
5254 /// Scanned - This points to one past the last character in the
5355 /// buffer we've scanned.
6567 return TheStream->tell();
6668 }
6769
68 /// ComputeColumn - Examine the given output buffer and figure out which
69 /// column we end up in after output.
70 /// ComputePosition - Examine the given output buffer and figure out the new
71 /// position after output.
7072 ///
71 void ComputeColumn(const char *Ptr, size_t size);
73 void ComputePosition(const char *Ptr, size_t size);
7274
7375 public:
7476 /// formatted_raw_ostream - Open the specified file for
8284 /// underneath it.
8385 ///
8486 formatted_raw_ostream(raw_ostream &Stream, bool Delete = false)
85 : raw_ostream(), TheStream(0), DeleteStream(false), ColumnScanned(0) {
87 : raw_ostream(), TheStream(0), DeleteStream(false), Position(0, 0) {
8688 setStream(Stream, Delete);
8789 }
8890 explicit formatted_raw_ostream()
89 : raw_ostream(), TheStream(0), DeleteStream(false), ColumnScanned(0) {
91 : raw_ostream(), TheStream(0), DeleteStream(false), Position(0, 0) {
9092 Scanned = 0;
9193 }
9294
121123 /// \param NewCol - The column to move to.
122124 formatted_raw_ostream &PadToColumn(unsigned NewCol);
123125
126 /// getColumn - Return the column number
127 unsigned getColumn() { return Position.first; }
128
129 /// getLine - Return the line number
130 unsigned getLine() { return Position.second; }
131
124132 private:
125133 void releaseStream() {
126134 // Delete the stream if needed. Otherwise, transfer the buffer
1616
1717 using namespace llvm;
1818
19 /// CountColumns - Examine the given char sequence and figure out which
20 /// column we end up in after output.
19 /// UpdatePosition - Examine the given char sequence and figure out which
20 /// column we end up in after output, and how many line breaks are contained.
2121 ///
22 static unsigned CountColumns(unsigned Column, const char *Ptr, size_t Size) {
23 // Keep track of the current column by scanning the string for
22 static void UpdatePosition(std::pair &Position, const char *Ptr, size_t Size) {
23 unsigned &Column = Position.first;
24 unsigned &Line = Position.second;
25
26 // Keep track of the current column and line by scanning the string for
2427 // special characters
25
2628 for (const char *End = Ptr + Size; Ptr != End; ++Ptr) {
2729 ++Column;
28 if (*Ptr == '\n' || *Ptr == '\r')
30 switch (*Ptr) {
31 case '\n':
32 Line += 1;
33 case '\r':
2934 Column = 0;
30 else if (*Ptr == '\t')
35 break;
36 case '\t':
3137 // Assumes tab stop = 8 characters.
3238 Column += (8 - (Column & 0x7)) & 0x7;
39 break;
40 }
3341 }
34
35 return Column;
3642 }
3743
38 /// ComputeColumn - Examine the current output and figure out which
39 /// column we end up in after output.
40 void formatted_raw_ostream::ComputeColumn(const char *Ptr, size_t Size) {
44 /// ComputePosition - Examine the current output and update line and column
45 /// counts.
46 void formatted_raw_ostream::ComputePosition(const char *Ptr, size_t Size) {
4147 // If our previous scan pointer is inside the buffer, assume we already
4248 // scanned those bytes. This depends on raw_ostream to not change our buffer
4349 // in unexpected ways.
44 if (Ptr <= Scanned && Scanned <= Ptr + Size) {
50 if (Ptr <= Scanned && Scanned <= Ptr + Size)
4551 // Scan all characters added since our last scan to determine the new
4652 // column.
47 ColumnScanned = CountColumns(ColumnScanned, Scanned,
48 Size - (Scanned - Ptr));
49 } else
50 ColumnScanned = CountColumns(ColumnScanned, Ptr, Size);
53 UpdatePosition(Position, Scanned, Size - (Scanned - Ptr));
54 else
55 UpdatePosition(Position, Ptr, Size);
5156
5257 // Update the scanning pointer.
5358 Scanned = Ptr + Size;
5964 ///
6065 formatted_raw_ostream &formatted_raw_ostream::PadToColumn(unsigned NewCol) {
6166 // Figure out what's in the buffer and add it to the column count.
62 ComputeColumn(getBufferStart(), GetNumBytesInBuffer());
67 ComputePosition(getBufferStart(), GetNumBytesInBuffer());
6368
6469 // Output spaces until we reach the desired column.
65 indent(std::max(int(NewCol - ColumnScanned), 1));
70 indent(std::max(int(NewCol - getColumn()), 1));
6671 return *this;
6772 }
6873
6974 void formatted_raw_ostream::write_impl(const char *Ptr, size_t Size) {
7075 // Figure out what's in the buffer and add it to the column count.
71 ComputeColumn(Ptr, Size);
76 ComputePosition(Ptr, Size);
7277
7378 // Write the data to the underlying stream (which is unbuffered, so
7479 // the data will be immediately written out).