@@ -52,6 +52,10 @@ class formatted_raw_ostream : public raw_ostream {
5252 // / have the rest of it.
5353 SmallString<4 > PartialUTF8Char;
5454
55+ // / DisableScan - Temporarily disable scanning of output. Used to ignore color
56+ // / codes.
57+ bool DisableScan;
58+
5559 void write_impl (const char *Ptr, size_t Size) override ;
5660
5761 // / current_pos - Return the current position within the stream,
@@ -89,9 +93,33 @@ class formatted_raw_ostream : public raw_ostream {
8993 SetUnbuffered ();
9094 TheStream->SetUnbuffered ();
9195
96+ enable_colors (TheStream->colors_enabled ());
97+
9298 Scanned = nullptr ;
9399 }
94100
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+
95123public:
96124 // / formatted_raw_ostream - Open the specified file for
97125 // / writing. If an error occurs, information about the error is
@@ -104,12 +132,12 @@ class formatted_raw_ostream : public raw_ostream {
104132 // / underneath it.
105133 // /
106134 formatted_raw_ostream (raw_ostream &Stream)
107- : TheStream(nullptr ), Position(0 , 0 ) {
135+ : TheStream(nullptr ), Position(0 , 0 ), DisableScan( false ) {
108136 setStream (Stream);
109137 }
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 ) { }
113141
114142 ~formatted_raw_ostream () override {
115143 flush ();
@@ -136,17 +164,26 @@ class formatted_raw_ostream : public raw_ostream {
136164 }
137165
138166 raw_ostream &resetColor () override {
139- TheStream->resetColor ();
167+ if (colors_enabled ()) {
168+ DisableScanScope S (this );
169+ raw_ostream::resetColor ();
170+ }
140171 return *this ;
141172 }
142173
143174 raw_ostream &reverseColor () override {
144- TheStream->reverseColor ();
175+ if (colors_enabled ()) {
176+ DisableScanScope S (this );
177+ raw_ostream::reverseColor ();
178+ }
145179 return *this ;
146180 }
147181
148182 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+ }
150187 return *this ;
151188 }
152189
0 commit comments