diff options
author | Joel E. Denny <jdenny.ornl@gmail.com> | 2018-10-24 21:46:42 +0000 |
---|---|---|
committer | Joel E. Denny <jdenny.ornl@gmail.com> | 2018-10-24 21:46:42 +0000 |
commit | 3e66509f6cf4c6eb48b931e719ef6e2426758247 (patch) | |
tree | 7d479b70f07fa414345f18b2707ea993a25d1aa8 /llvm/lib/Support/SourceMgr.cpp | |
parent | 096e2497b5e2b816c7e5ecd172ac5043e81b0c56 (diff) | |
download | bcm5719-llvm-3e66509f6cf4c6eb48b931e719ef6e2426758247.tar.gz bcm5719-llvm-3e66509f6cf4c6eb48b931e719ef6e2426758247.zip |
[SourceMgr][FileCheck] Obey -color by extending WithColor
(Relands r344930, reverted in r344935, and now hopefully fixed for
Windows.)
While this change specifically targets FileCheck, it affects any tool
using the same SourceMgr facilities.
Previously, -color was documented in FileCheck's -help output, but
-color had no effect. Now, -color obeys its documentation: it forces
colors to be used in FileCheck diagnostics even when stderr is not a
terminal.
-color is especially helpful when combined with FileCheck's -v, which
can produce a long series of diagnostics that you might wish to pipe
to a pager, such as less -R. The WithColor extensions here will also
help to clean up color usage in FileCheck's annotated dump of input,
which is proposed in D52999.
Reviewed By: JDevlieghere, zturner
Differential Revision: https://reviews.llvm.org/D53419
llvm-svn: 345202
Diffstat (limited to 'llvm/lib/Support/SourceMgr.cpp')
-rw-r--r-- | llvm/lib/Support/SourceMgr.cpp | 108 |
1 files changed, 45 insertions, 63 deletions
diff --git a/llvm/lib/Support/SourceMgr.cpp b/llvm/lib/Support/SourceMgr.cpp index 582e2cf6c11..a55ad881d01 100644 --- a/llvm/lib/Support/SourceMgr.cpp +++ b/llvm/lib/Support/SourceMgr.cpp @@ -24,6 +24,7 @@ #include "llvm/Support/MemoryBuffer.h" #include "llvm/Support/Path.h" #include "llvm/Support/SMLoc.h" +#include "llvm/Support/WithColor.h" #include "llvm/Support/raw_ostream.h" #include <algorithm> #include <cassert> @@ -370,65 +371,48 @@ static bool isNonASCII(char c) { return c & 0x80; } -void SMDiagnostic::print(const char *ProgName, raw_ostream &S, bool ShowColors, - bool ShowKindLabel) const { - // Display colors only if OS supports colors. - ShowColors &= S.has_colors(); +void SMDiagnostic::print(const char *ProgName, raw_ostream &OS, + bool ShowColors, bool ShowKindLabel) const { + { + WithColor S(OS, raw_ostream::SAVEDCOLOR, true, false, !ShowColors); - if (ShowColors) - S.changeColor(raw_ostream::SAVEDCOLOR, true); + if (ProgName && ProgName[0]) + S << ProgName << ": "; - if (ProgName && ProgName[0]) - S << ProgName << ": "; + if (!Filename.empty()) { + if (Filename == "-") + S << "<stdin>"; + else + S << Filename; - if (!Filename.empty()) { - if (Filename == "-") - S << "<stdin>"; - else - S << Filename; - - if (LineNo != -1) { - S << ':' << LineNo; - if (ColumnNo != -1) - S << ':' << (ColumnNo+1); + if (LineNo != -1) { + S << ':' << LineNo; + if (ColumnNo != -1) + S << ':' << (ColumnNo + 1); + } + S << ": "; } - S << ": "; } if (ShowKindLabel) { switch (Kind) { case SourceMgr::DK_Error: - if (ShowColors) - S.changeColor(raw_ostream::RED, true); - S << "error: "; + WithColor::error(OS, "", !ShowColors); break; case SourceMgr::DK_Warning: - if (ShowColors) - S.changeColor(raw_ostream::MAGENTA, true); - S << "warning: "; + WithColor::warning(OS, "", !ShowColors); break; case SourceMgr::DK_Note: - if (ShowColors) - S.changeColor(raw_ostream::BLACK, true); - S << "note: "; + WithColor::note(OS, "", !ShowColors); break; case SourceMgr::DK_Remark: - if (ShowColors) - S.changeColor(raw_ostream::BLUE, true); - S << "remark: "; + WithColor::remark(OS, "", !ShowColors); break; } - - if (ShowColors) { - S.resetColor(); - S.changeColor(raw_ostream::SAVEDCOLOR, true); - } } - S << Message << '\n'; - - if (ShowColors) - S.resetColor(); + WithColor(OS, raw_ostream::SAVEDCOLOR, true, false, !ShowColors) + << Message << '\n'; if (LineNo == -1 || ColumnNo == -1) return; @@ -439,7 +423,7 @@ void SMDiagnostic::print(const char *ProgName, raw_ostream &S, bool ShowColors, // expanding them later, and bail out rather than show incorrect ranges and // misaligned fixits for any other odd characters. if (find_if(LineContents, isNonASCII) != LineContents.end()) { - printSourceLine(S, LineContents); + printSourceLine(OS, LineContents); return; } size_t NumColumns = LineContents.size(); @@ -473,29 +457,27 @@ void SMDiagnostic::print(const char *ProgName, raw_ostream &S, bool ShowColors, // least. CaretLine.erase(CaretLine.find_last_not_of(' ')+1); - printSourceLine(S, LineContents); + printSourceLine(OS, LineContents); - if (ShowColors) - S.changeColor(raw_ostream::GREEN, true); + { + WithColor S(OS, raw_ostream::GREEN, true, false, !ShowColors); - // Print out the caret line, matching tabs in the source line. - for (unsigned i = 0, e = CaretLine.size(), OutCol = 0; i != e; ++i) { - if (i >= LineContents.size() || LineContents[i] != '\t') { - S << CaretLine[i]; - ++OutCol; - continue; - } + // Print out the caret line, matching tabs in the source line. + for (unsigned i = 0, e = CaretLine.size(), OutCol = 0; i != e; ++i) { + if (i >= LineContents.size() || LineContents[i] != '\t') { + S << CaretLine[i]; + ++OutCol; + continue; + } - // Okay, we have a tab. Insert the appropriate number of characters. - do { - S << CaretLine[i]; - ++OutCol; - } while ((OutCol % TabStop) != 0); + // Okay, we have a tab. Insert the appropriate number of characters. + do { + S << CaretLine[i]; + ++OutCol; + } while ((OutCol % TabStop) != 0); + } + S << '\n'; } - S << '\n'; - - if (ShowColors) - S.resetColor(); // Print out the replacement line, matching tabs in the source line. if (FixItInsertionLine.empty()) @@ -503,14 +485,14 @@ void SMDiagnostic::print(const char *ProgName, raw_ostream &S, bool ShowColors, for (size_t i = 0, e = FixItInsertionLine.size(), OutCol = 0; i < e; ++i) { if (i >= LineContents.size() || LineContents[i] != '\t') { - S << FixItInsertionLine[i]; + OS << FixItInsertionLine[i]; ++OutCol; continue; } // Okay, we have a tab. Insert the appropriate number of characters. do { - S << FixItInsertionLine[i]; + OS << FixItInsertionLine[i]; // FIXME: This is trying not to break up replacements, but then to re-sync // with the tabs between replacements. This will fail, though, if two // fix-it replacements are exactly adjacent, or if a fix-it contains a @@ -521,5 +503,5 @@ void SMDiagnostic::print(const char *ProgName, raw_ostream &S, bool ShowColors, ++OutCol; } while (((OutCol % TabStop) != 0) && i != e); } - S << '\n'; + OS << '\n'; } |