@@ -52,6 +52,10 @@ class formatted_raw_ostream : public raw_ostream {
52
52
// / have the rest of it.
53
53
SmallString<4 > PartialUTF8Char;
54
54
55
+ // / DisableScan - Temporarily disable scanning of output. Used to ignore color
56
+ // / codes.
57
+ bool DisableScan;
58
+
55
59
void write_impl (const char *Ptr , size_t Size ) override ;
56
60
57
61
// / current_pos - Return the current position within the stream,
@@ -89,9 +93,33 @@ class formatted_raw_ostream : public raw_ostream {
89
93
SetUnbuffered ();
90
94
TheStream->SetUnbuffered ();
91
95
96
+ enable_colors (TheStream->colors_enabled ());
97
+
92
98
Scanned = nullptr ;
93
99
}
94
100
101
+ void PreDisableScan () {
102
+ assert (!DisableScan);
103
+ ComputePosition (getBufferStart (), GetNumBytesInBuffer ());
104
+ assert (PartialUTF8Char.empty ());
105
+ DisableScan = true ;
106
+ }
107
+
108
+ void PostDisableScan () {
109
+ assert (DisableScan);
110
+ DisableScan = false ;
111
+ Scanned = getBufferStart () + GetNumBytesInBuffer ();
112
+ }
113
+
114
+ struct DisableScanScope {
115
+ formatted_raw_ostream *S;
116
+
117
+ DisableScanScope (formatted_raw_ostream *FRO) : S(FRO) {
118
+ S->PreDisableScan ();
119
+ }
120
+ ~DisableScanScope () { S->PostDisableScan (); }
121
+ };
122
+
95
123
public:
96
124
// / formatted_raw_ostream - Open the specified file for
97
125
// / writing. If an error occurs, information about the error is
@@ -104,12 +132,12 @@ class formatted_raw_ostream : public raw_ostream {
104
132
// / underneath it.
105
133
// /
106
134
formatted_raw_ostream (raw_ostream &Stream)
107
- : TheStream(nullptr ), Position(0 , 0 ) {
135
+ : TheStream(nullptr ), Position(0 , 0 ), DisableScan( false ) {
108
136
setStream (Stream);
109
137
}
110
- explicit formatted_raw_ostream () : TheStream( nullptr ), Position( 0 , 0 ) {
111
- Scanned = nullptr ;
112
- }
138
+ explicit formatted_raw_ostream ()
139
+ : TheStream( nullptr ), Position( 0 , 0 ), Scanned( nullptr ),
140
+ DisableScan( false ) { }
113
141
114
142
~formatted_raw_ostream () override {
115
143
flush ();
@@ -136,17 +164,26 @@ class formatted_raw_ostream : public raw_ostream {
136
164
}
137
165
138
166
raw_ostream &resetColor () override {
139
- TheStream->resetColor ();
167
+ if (colors_enabled ()) {
168
+ DisableScanScope S (this );
169
+ raw_ostream::resetColor ();
170
+ }
140
171
return *this ;
141
172
}
142
173
143
174
raw_ostream &reverseColor () override {
144
- TheStream->reverseColor ();
175
+ if (colors_enabled ()) {
176
+ DisableScanScope S (this );
177
+ raw_ostream::reverseColor ();
178
+ }
145
179
return *this ;
146
180
}
147
181
148
182
raw_ostream &changeColor (enum Colors Color, bool Bold, bool BG) override {
149
- TheStream->changeColor (Color, Bold, BG);
183
+ if (colors_enabled ()) {
184
+ DisableScanScope S (this );
185
+ raw_ostream::changeColor (Color, Bold, BG);
186
+ }
150
187
return *this ;
151
188
}
152
189
0 commit comments