summaryrefslogtreecommitdiffstats
path: root/lld/ELF/Error.cpp
diff options
context:
space:
mode:
authorRui Ueyama <ruiu@google.com>2016-11-25 20:27:32 +0000
committerRui Ueyama <ruiu@google.com>2016-11-25 20:27:32 +0000
commit8c8818a58cff7641a9449249c12651fd3eb6980b (patch)
treec886538f736160feddb02ec8520e31b504a1950a /lld/ELF/Error.cpp
parent6066641423cdc3c4c87f15b90e58cd2928df1b48 (diff)
downloadbcm5719-llvm-8c8818a58cff7641a9449249c12651fd3eb6980b.tar.gz
bcm5719-llvm-8c8818a58cff7641a9449249c12651fd3eb6980b.zip
Support -color-diagnostics={auto,always,never}.
-color-diagnostics=auto is default because that's the same as Clang's default. When color is enabled, error or warning messages are colored like this. error: <bold>ld.lld</bold> <red>error:</red> foo.o: no such file warning: <bold>ld.lld</bold> <magenta>warning:</magenta> foo.o: no such file Differential Revision: https://reviews.llvm.org/D27117 llvm-svn: 287949
Diffstat (limited to 'lld/ELF/Error.cpp')
-rw-r--r--lld/ELF/Error.cpp34
1 files changed, 30 insertions, 4 deletions
diff --git a/lld/ELF/Error.cpp b/lld/ELF/Error.cpp
index a230bf6dadd..2b5cfe9bc70 100644
--- a/lld/ELF/Error.cpp
+++ b/lld/ELF/Error.cpp
@@ -13,6 +13,7 @@
#include "llvm/ADT/Twine.h"
#include "llvm/Support/Error.h"
#include "llvm/Support/ManagedStatic.h"
+#include "llvm/Support/Process.h"
#include "llvm/Support/raw_ostream.h"
#include <mutex>
@@ -20,6 +21,7 @@
#include <unistd.h>
#endif
+using namespace lld::elf;
using namespace llvm;
namespace lld {
@@ -32,6 +34,26 @@ StringRef elf::Argv0;
// but outs() or errs() are not thread-safe. We protect them using a mutex.
static std::mutex Mu;
+static bool useColor() {
+ if (Config->ColorDiagnostics == ColorPolicy::Always)
+ return true;
+ if (Config->ColorDiagnostics == ColorPolicy::Never)
+ return false;
+ return ErrorOS == &errs() && sys::Process::StandardErrHasColors();
+}
+
+static void print(StringRef S, raw_ostream::Colors C) {
+ if (useColor()) {
+ ErrorOS->changeColor(raw_ostream::WHITE, /*Bold=*/true);
+ *ErrorOS << Argv0 + ": ";
+ ErrorOS->changeColor(C, true);
+ *ErrorOS << S;
+ ErrorOS->resetColor();
+ } else {
+ *ErrorOS << Argv0 + ": " << S;
+ }
+}
+
void elf::log(const Twine &Msg) {
std::lock_guard<std::mutex> Lock(Mu);
if (Config->Verbose)
@@ -44,16 +66,19 @@ void elf::warn(const Twine &Msg) {
return;
}
std::lock_guard<std::mutex> Lock(Mu);
- *ErrorOS << Argv0 << ": warning: " << Msg << "\n";
+ print("warning: ", raw_ostream::MAGENTA);
+ *ErrorOS << Msg << "\n";
}
void elf::error(const Twine &Msg) {
std::lock_guard<std::mutex> Lock(Mu);
if (Config->ErrorLimit == 0 || ErrorCount < Config->ErrorLimit) {
- *ErrorOS << Argv0 << ": error: " << Msg << "\n";
+ print("error: ", raw_ostream::RED);
+ *ErrorOS << Msg << "\n";
} else if (ErrorCount == Config->ErrorLimit) {
- *ErrorOS << Argv0 << ": error: too many errors emitted, stopping now"
+ print("error: ", raw_ostream::RED);
+ *ErrorOS << "too many errors emitted, stopping now"
<< " (use -error-limit=0 to see all errors)\n";
if (Config->ExitEarly)
exitLld(1);
@@ -79,7 +104,8 @@ void elf::exitLld(int Val) {
void elf::fatal(const Twine &Msg) {
std::lock_guard<std::mutex> Lock(Mu);
- *ErrorOS << Argv0 << ": error: " << Msg << "\n";
+ print("error: ", raw_ostream::RED);
+ *ErrorOS << Msg << "\n";
exitLld(1);
}
OpenPOWER on IntegriCloud