1 //===- Error.cpp ----------------------------------------------------------===//
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
13 #include "llvm/ADT/Twine.h"
14 #include "llvm/Support/Error.h"
15 #include "llvm/Support/ManagedStatic.h"
16 #include "llvm/Support/Process.h"
17 #include "llvm/Support/raw_ostream.h"
20 #if !defined(_MSC_VER) && !defined(__MINGW32__)
27 // The functions defined in this file can be called from multiple threads,
28 // but outs() or errs() are not thread-safe. We protect them using a mutex.
36 static LLVM_ATTRIBUTE_NORETURN void exitLld(int Val) {
37 // Dealloc/destroy ManagedStatic variables before calling
38 // _exit(). In a non-LTO build, this is a nop. In an LTO
39 // build allows us to get the output of -time-passes.
47 static void print(StringRef S, raw_ostream::Colors C) {
48 *ErrorOS << Argv0 + ": ";
49 if (Config->ColorDiagnostics) {
50 ErrorOS->changeColor(C, true);
52 ErrorOS->resetColor();
58 void log(const Twine &Msg) {
59 if (Config->Verbose) {
60 std::lock_guard<std::mutex> Lock(Mu);
61 outs() << Argv0 << ": " << Msg << "\n";
65 void message(const Twine &Msg) {
66 std::lock_guard<std::mutex> Lock(Mu);
67 outs() << Msg << "\n";
71 void error(const Twine &Msg) {
72 std::lock_guard<std::mutex> Lock(Mu);
74 if (Config->ErrorLimit == 0 || ErrorCount < Config->ErrorLimit) {
75 print("error: ", raw_ostream::RED);
76 *ErrorOS << Msg << "\n";
77 } else if (ErrorCount == Config->ErrorLimit) {
78 print("error: ", raw_ostream::RED);
79 *ErrorOS << "too many errors emitted, stopping now"
80 << " (use /ERRORLIMIT:0 to see all errors)\n";
87 void fatal(const Twine &Msg) {
88 if (Config->ColorDiagnostics) {
89 errs().changeColor(raw_ostream::RED, /*bold=*/true);
95 errs() << Msg << "\n";
99 void fatal(std::error_code EC, const Twine &Msg) {
100 fatal(Msg + ": " + EC.message());
103 void fatal(llvm::Error &Err, const Twine &Msg) {
104 fatal(errorToErrorCode(std::move(Err)), Msg);
107 void warn(const Twine &Msg) {
108 std::lock_guard<std::mutex> Lock(Mu);
109 print("warning: ", raw_ostream::MAGENTA);
110 *ErrorOS << Msg << "\n";