aboutsummaryrefslogtreecommitdiff
path: root/llvm/lib/Support/WithColor.cpp
diff options
context:
space:
mode:
authorDimitry Andric <dim@FreeBSD.org>2020-07-26 19:36:28 +0000
committerDimitry Andric <dim@FreeBSD.org>2020-07-26 19:36:28 +0000
commitcfca06d7963fa0909f90483b42a6d7d194d01e08 (patch)
tree209fb2a2d68f8f277793fc8df46c753d31bc853b /llvm/lib/Support/WithColor.cpp
parent706b4fc47bbc608932d3b491ae19a3b9cde9497b (diff)
Notes
Diffstat (limited to 'llvm/lib/Support/WithColor.cpp')
-rw-r--r--llvm/lib/Support/WithColor.cpp48
1 files changed, 37 insertions, 11 deletions
diff --git a/llvm/lib/Support/WithColor.cpp b/llvm/lib/Support/WithColor.cpp
index 345dd9cf3949..cb5f413d44b7 100644
--- a/llvm/lib/Support/WithColor.cpp
+++ b/llvm/lib/Support/WithColor.cpp
@@ -7,7 +7,7 @@
//===----------------------------------------------------------------------===//
#include "llvm/Support/WithColor.h"
-#include "llvm/Support/raw_ostream.h"
+#include "llvm/Support/CommandLine.h"
using namespace llvm;
@@ -18,8 +18,8 @@ static cl::opt<cl::boolOrDefault>
cl::desc("Use colors in output (default=autodetect)"),
cl::init(cl::BOU_UNSET));
-WithColor::WithColor(raw_ostream &OS, HighlightColor Color, bool DisableColors)
- : OS(OS), DisableColors(DisableColors) {
+WithColor::WithColor(raw_ostream &OS, HighlightColor Color, ColorMode Mode)
+ : OS(OS), Mode(Mode) {
// Detect color from terminal type unless the user passed the --color option.
if (colorsEnabled()) {
switch (Color) {
@@ -69,7 +69,9 @@ raw_ostream &WithColor::error(raw_ostream &OS, StringRef Prefix,
bool DisableColors) {
if (!Prefix.empty())
OS << Prefix << ": ";
- return WithColor(OS, HighlightColor::Error, DisableColors).get()
+ return WithColor(OS, HighlightColor::Error,
+ DisableColors ? ColorMode::Disable : ColorMode::Auto)
+ .get()
<< "error: ";
}
@@ -77,7 +79,9 @@ raw_ostream &WithColor::warning(raw_ostream &OS, StringRef Prefix,
bool DisableColors) {
if (!Prefix.empty())
OS << Prefix << ": ";
- return WithColor(OS, HighlightColor::Warning, DisableColors).get()
+ return WithColor(OS, HighlightColor::Warning,
+ DisableColors ? ColorMode::Disable : ColorMode::Auto)
+ .get()
<< "warning: ";
}
@@ -85,23 +89,33 @@ raw_ostream &WithColor::note(raw_ostream &OS, StringRef Prefix,
bool DisableColors) {
if (!Prefix.empty())
OS << Prefix << ": ";
- return WithColor(OS, HighlightColor::Note, DisableColors).get() << "note: ";
+ return WithColor(OS, HighlightColor::Note,
+ DisableColors ? ColorMode::Disable : ColorMode::Auto)
+ .get()
+ << "note: ";
}
raw_ostream &WithColor::remark(raw_ostream &OS, StringRef Prefix,
bool DisableColors) {
if (!Prefix.empty())
OS << Prefix << ": ";
- return WithColor(OS, HighlightColor::Remark, DisableColors).get()
+ return WithColor(OS, HighlightColor::Remark,
+ DisableColors ? ColorMode::Disable : ColorMode::Auto)
+ .get()
<< "remark: ";
}
bool WithColor::colorsEnabled() {
- if (DisableColors)
+ switch (Mode) {
+ case ColorMode::Enable:
+ return true;
+ case ColorMode::Disable:
return false;
- if (UseColor == cl::BOU_UNSET)
- return OS.has_colors();
- return UseColor == cl::BOU_TRUE;
+ case ColorMode::Auto:
+ return UseColor == cl::BOU_UNSET ? OS.has_colors()
+ : UseColor == cl::BOU_TRUE;
+ }
+ llvm_unreachable("All cases handled above.");
}
WithColor &WithColor::changeColor(raw_ostream::Colors Color, bool Bold,
@@ -118,3 +132,15 @@ WithColor &WithColor::resetColor() {
}
WithColor::~WithColor() { resetColor(); }
+
+void WithColor::defaultErrorHandler(Error Err) {
+ handleAllErrors(std::move(Err), [](ErrorInfoBase &Info) {
+ WithColor::error() << Info.message() << '\n';
+ });
+}
+
+void WithColor::defaultWarningHandler(Error Warning) {
+ handleAllErrors(std::move(Warning), [](ErrorInfoBase &Info) {
+ WithColor::warning() << Info.message() << '\n';
+ });
+}