llvm.org GIT mirror llvm / ed560ae
Revert r367649: Improve raw_ostream so that you can "write" colors using operator<< This reverts commit r367649 in an attempt to unbreak Windows bots. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@367658 91177308-0d34-0410-b5e6-96231b3b80d8 Rui Ueyama 1 year, 2 months ago
12 changed file(s) with 39 addition(s) and 89 deletion(s). Raw diff Collapse all Expand all
120120 return *this;
121121 }
122122
123 raw_ostream &changeColor(Color C, bool Bold, bool BG) override {
124 TheStream->changeColor(C, Bold, BG);
123 raw_ostream &changeColor(enum Colors Color, bool Bold, bool BG) override {
124 TheStream->changeColor(Color, Bold, BG);
125125 return *this;
126126 }
127127
5252 /// @param BG If true, change the background, default: change foreground
5353 /// @param DisableColors Whether to ignore color changes regardless of -color
5454 /// and support in OS
55 WithColor(raw_ostream &OS, raw_ostream::Color Color = raw_ostream::SAVEDCOLOR,
55 WithColor(raw_ostream &OS,
56 raw_ostream::Colors Color = raw_ostream::SAVEDCOLOR,
5657 bool Bold = false, bool BG = false, bool DisableColors = false)
5758 : OS(OS), DisableColors(DisableColors) {
5859 changeColor(Color, Bold, BG);
100101 /// change only the bold attribute, and keep colors untouched
101102 /// @param Bold Bold/brighter text, default false
102103 /// @param BG If true, change the background, default: change foreground
103 WithColor &changeColor(raw_ostream::Color Color, bool Bold = false,
104 WithColor &changeColor(raw_ostream::Colors Color, bool Bold = false,
104105 bool BG = false);
105106
106107 /// Reset the colors to terminal defaults. Call this when you are done
7171
7272 public:
7373 // color order matches ANSI escape sequence, don't change
74 enum class Color {
74 enum Colors {
7575 BLACK = 0,
7676 RED,
7777 GREEN,
8080 MAGENTA,
8181 CYAN,
8282 WHITE,
83 SAVEDCOLOR,
84 RESET,
83 SAVEDCOLOR
8584 };
86
87 static const Color BLACK = Color::BLACK;
88 static const Color RED = Color::RED;
89 static const Color GREEN = Color::GREEN;
90 static const Color YELLOW = Color::YELLOW;
91 static const Color BLUE = Color::BLUE;
92 static const Color MAGENTA = Color::MAGENTA;
93 static const Color CYAN = Color::CYAN;
94 static const Color WHITE = Color::WHITE;
95 static const Color SAVEDCOLOR = Color::SAVEDCOLOR;
96 static const Color RESET = Color::RESET;
9785
9886 explicit raw_ostream(bool unbuffered = false)
9987 : BufferMode(unbuffered ? Unbuffered : InternalBuffer) {
225213 /// Output \p N in hexadecimal, without any prefix or padding.
226214 raw_ostream &write_hex(unsigned long long N);
227215
228 // Change the foreground color of text.
229 raw_ostream &operator<<(Color C);
230
231216 /// Output a formatted UUID with dash separators.
232217 using uuid_t = uint8_t[16];
233218 raw_ostream &write_uuid(const uuid_t UUID);
262247
263248 /// Changes the foreground color of text that will be output from this point
264249 /// forward.
265 /// @param C ANSI color to use, the special SAVEDCOLOR can be used to
250 /// @param Color ANSI color to use, the special SAVEDCOLOR can be used to
266251 /// change only the bold attribute, and keep colors untouched
267252 /// @param Bold bold/brighter text, default false
268253 /// @param BG if true change the background, default: change foreground
269254 /// @returns itself so it can be used within << invocations
270 virtual raw_ostream &changeColor(Color C, bool Bold = false,
255 virtual raw_ostream &changeColor(enum Colors Color,
256 bool Bold = false,
271257 bool BG = false) {
272 (void)C;
258 (void)Color;
273259 (void)Bold;
274260 (void)BG;
275261 return *this;
289275
290276 /// This function determines if this stream is displayed and supports colors.
291277 virtual bool has_colors() const { return is_displayed(); }
292
293 // Enable or disable colors. Once disable_colors() is called,
294 // changeColor() has no effect until enable_colors() is called.
295 virtual void enable_colors() {}
296 virtual void disable_colors() {}
297278
298279 //===--------------------------------------------------------------------===//
299280 // Subclass Interface
385366 bool ShouldClose;
386367
387368 bool SupportsSeeking;
388
389 bool ColorEnabled;
390369
391370 #ifdef _WIN32
392371 /// True if this fd refers to a Windows console device. Mintty and other
452431 /// to the offset specified from the beginning of the file.
453432 uint64_t seek(uint64_t off);
454433
455 raw_ostream &changeColor(Color C, bool bold = false,
456 bool bg = false) override;
434 raw_ostream &changeColor(enum Colors colors, bool bold=false,
435 bool bg=false) override;
457436 raw_ostream &resetColor() override;
458437
459438 raw_ostream &reverseColor() override;
461440 bool is_displayed() const override;
462441
463442 bool has_colors() const override;
464
465 void enable_colors() override { ColorEnabled = true; }
466
467 void disable_colors() override { ColorEnabled = false; }
468443
469444 std::error_code error() const { return EC; }
470445
2121 : OS(OS), DisableColors(DisableColors) {
2222 // Detect color from terminal type unless the user passed the --color option.
2323 if (colorsEnabled()) {
24 OS.enable_colors();
25
2624 switch (Color) {
2725 case HighlightColor::Address:
2826 OS.changeColor(raw_ostream::YELLOW);
105103 return UseColor == cl::BOU_TRUE;
106104 }
107105
108 WithColor &WithColor::changeColor(raw_ostream::Color C, bool Bold, bool BG) {
106 WithColor &WithColor::changeColor(raw_ostream::Colors Color, bool Bold,
107 bool BG) {
109108 if (colorsEnabled())
110 OS.changeColor(C, Bold, BG);
109 OS.changeColor(Color, Bold, BG);
111110 return *this;
112111 }
113112
6464
6565 using namespace llvm;
6666
67 const raw_ostream::Color raw_ostream::BLACK;
68 const raw_ostream::Color raw_ostream::RED;
69 const raw_ostream::Color raw_ostream::GREEN;
70 const raw_ostream::Color raw_ostream::YELLOW;
71 const raw_ostream::Color raw_ostream::BLUE;
72 const raw_ostream::Color raw_ostream::MAGENTA;
73 const raw_ostream::Color raw_ostream::CYAN;
74 const raw_ostream::Color raw_ostream::WHITE;
75 const raw_ostream::Color raw_ostream::SAVEDCOLOR;
76 const raw_ostream::Color raw_ostream::RESET;
77
7867 raw_ostream::~raw_ostream() {
7968 // raw_ostream's subclasses should take care to flush the buffer
8069 // in their destructors.
140129
141130 raw_ostream &raw_ostream::write_hex(unsigned long long N) {
142131 llvm::write_hex(*this, N, HexPrintStyle::Lower);
143 return *this;
144 }
145
146 raw_ostream &raw_ostream::operator<<(Color C) {
147 if (C == Color::RESET)
148 resetColor();
149 else
150 changeColor(C);
151132 return *this;
152133 }
153134
570551 /// FD is the file descriptor that this writes to. If ShouldClose is true, this
571552 /// closes the file when the stream is destroyed.
572553 raw_fd_ostream::raw_fd_ostream(int fd, bool shouldClose, bool unbuffered)
573 : raw_pwrite_stream(unbuffered), FD(fd), ShouldClose(shouldClose),
574 ColorEnabled(sys::Process::FileDescriptorHasColors(fd)) {
575 if (FD < 0) {
554 : raw_pwrite_stream(unbuffered), FD(fd), ShouldClose(shouldClose) {
555 if (FD < 0 ) {
576556 ShouldClose = false;
577557 return;
578558 }
801781 #endif
802782 }
803783
804 raw_ostream &raw_fd_ostream::changeColor(Color color, bool bold, bool bg) {
805 if (!ColorEnabled)
806 return *this;
807
784 raw_ostream &raw_fd_ostream::changeColor(enum Colors colors, bool bold,
785 bool bg) {
808786 if (sys::Process::ColorNeedsFlush())
809787 flush();
810788 const char *colorcode =
811 (color == Color::SAVEDCOLOR)
812 ? sys::Process::OutputBold(bg)
813 : sys::Process::OutputColor(static_cast(color), bold, bg);
789 (colors == SAVEDCOLOR) ? sys::Process::OutputBold(bg)
790 : sys::Process::OutputColor(colors, bold, bg);
814791 if (colorcode) {
815792 size_t len = strlen(colorcode);
816793 write(colorcode, len);
821798 }
822799
823800 raw_ostream &raw_fd_ostream::resetColor() {
824 if (!ColorEnabled)
825 return *this;
826
827801 if (sys::Process::ColorNeedsFlush())
828802 flush();
829803 const char *colorcode = sys::Process::ResetColor();
837811 }
838812
839813 raw_ostream &raw_fd_ostream::reverseColor() {
840 if (!ColorEnabled)
841 return *this;
842
843814 if (sys::Process::ColorNeedsFlush())
844815 flush();
845816 const char *colorcode = sys::Process::OutputReverse();
110110 /// Return the color which correponds to the coverage percentage of a
111111 /// certain metric.
112112 template
113 raw_ostream::Color determineCoveragePercentageColor(const T &Info) {
113 raw_ostream::Colors determineCoveragePercentageColor(const T &Info) {
114114 if (Info.isFullyCovered())
115115 return raw_ostream::GREEN;
116116 return Info.getPercentCovered() >= 80.0 ? raw_ostream::YELLOW
4545
4646 /// Change the output's stream color if the colors are enabled.
4747 ColoredRawOstream colored_ostream(raw_ostream &OS,
48 raw_ostream::Color Color) const {
48 raw_ostream::Colors Color) const {
4949 return llvm::colored_ostream(OS, Color, Colors);
5050 }
5151
4646 /// Change the color of the output stream if the `IsColorUsed` flag
4747 /// is true. Returns an object that resets the color when destroyed.
4848 inline ColoredRawOstream colored_ostream(raw_ostream &OS,
49 raw_ostream::Color Color,
49 raw_ostream::Colors Color,
5050 bool IsColorUsed = true,
5151 bool Bold = false, bool BG = false) {
5252 if (IsColorUsed)
100100 auto *WrappedSegment = LCS.getWrappedSegment();
101101 CoverageSegmentArray Segments = LCS.getLineSegments();
102102
103 Optional> Highlight;
103 Optionals> Highlight;
104104 SmallVector, 2> HighlightedRanges;
105105
106106 // The first segment overlaps from a previous line, so we treat it specially.
8282 }
8383 }
8484
85 if (argc > 1)
86 errs() << raw_ostream::RED << "Unrecognized command: " << argv[1] << ".\n\n"
87 << raw_ostream::RESET;
88
85 if (argc > 1) {
86 if (sys::Process::StandardErrHasColors())
87 errs().changeColor(raw_ostream::RED);
88 errs() << "Unrecognized command: " << argv[1] << ".\n\n";
89 if (sys::Process::StandardErrHasColors())
90 errs().resetColor();
91 }
8992 helpMain(argc, argv);
9093 return 1;
9194 }
102102 LastCycle = std::max(LastCycle, CurrentCycle);
103103 }
104104
105 static raw_ostream::Color chooseColor(unsigned CumulativeCycles,
106 unsigned Executions, int BufferSize) {
105 static raw_ostream::Colors chooseColor(unsigned CumulativeCycles,
106 unsigned Executions, int BufferSize) {
107107 if (CumulativeCycles && BufferSize < 0)
108108 return raw_ostream::MAGENTA;
109109 unsigned Size = static_cast(BufferSize);
119119 if (!OS.has_colors())
120120 return;
121121
122 raw_ostream::Color Color = chooseColor(Cycles, Executions, BufferSize);
122 raw_ostream::Colors Color = chooseColor(Cycles, Executions, BufferSize);
123123 if (Color == raw_ostream::SAVEDCOLOR) {
124124 OS.resetColor();
125125 return;
137137 /// The starting char (before tildes) for marking the line.
138138 char Lead;
139139 /// What color to use for this annotation.
140 raw_ostream::Color Color;
140 raw_ostream::Colors Color;
141141 /// A note to follow the marker, or empty string if none.
142142 std::string Note;
143143 MarkerStyle() {}
144 MarkerStyle(char Lead, raw_ostream::Color Color, const std::string &Note = "")
144 MarkerStyle(char Lead, raw_ostream::Colors Color,
145 const std::string &Note = "")
145146 : Lead(Lead), Color(Color), Note(Note) {}
146147 };
147148