1 //===- ErrorHandler.cpp ---------------------------------------------------===// 2 // 3 // The LLVM Linker 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 10 #include "lld/Common/ErrorHandler.h" 11 12 #include "lld/Common/Threads.h" 13 14 #include "llvm/ADT/Twine.h" 15 #include "llvm/IR/DiagnosticInfo.h" 16 #include "llvm/IR/DiagnosticPrinter.h" 17 #include "llvm/Support/ManagedStatic.h" 18 #include "llvm/Support/raw_ostream.h" 19 #include <mutex> 20 21 #if !defined(_MSC_VER) && !defined(__MINGW32__) 22 #include <unistd.h> 23 #endif 24 25 using namespace llvm; 26 using namespace lld; 27 28 // The functions defined in this file can be called from multiple threads, 29 // but outs() or errs() are not thread-safe. We protect them using a mutex. 30 static std::mutex Mu; 31 32 // Prints "\n" or does nothing, depending on Msg contents of 33 // the previous call of this function. 34 static void newline(raw_ostream *ErrorOS, const Twine &Msg) { 35 // True if the previous error message contained "\n". 36 // We want to separate multi-line error messages with a newline. 37 static bool Flag; 38 39 if (Flag) 40 *ErrorOS << "\n"; 41 Flag = StringRef(Msg.str()).contains('\n'); 42 } 43 44 ErrorHandler &lld::errorHandler() { 45 static ErrorHandler Handler; 46 return Handler; 47 } 48 49 void lld::exitLld(int Val) { 50 // Delete the output buffer so that any tempory file is deleted. 51 errorHandler().OutputBuffer.reset(); 52 53 // Dealloc/destroy ManagedStatic variables before calling 54 // _exit(). In a non-LTO build, this is a nop. In an LTO 55 // build allows us to get the output of -time-passes. 56 llvm_shutdown(); 57 58 outs().flush(); 59 errs().flush(); 60 _exit(Val); 61 } 62 63 void lld::diagnosticHandler(const DiagnosticInfo &DI) { 64 SmallString<128> S; 65 raw_svector_ostream OS(S); 66 DiagnosticPrinterRawOStream DP(OS); 67 DI.print(DP); 68 warn(S); 69 } 70 71 void lld::checkError(Error E) { 72 handleAllErrors(std::move(E), 73 [&](ErrorInfoBase &EIB) { error(EIB.message()); }); 74 } 75 76 void ErrorHandler::print(StringRef S, raw_ostream::Colors C) { 77 *ErrorOS << LogName << ": "; 78 if (ColorDiagnostics) { 79 ErrorOS->changeColor(C, true); 80 *ErrorOS << S; 81 ErrorOS->resetColor(); 82 } else { 83 *ErrorOS << S; 84 } 85 } 86 87 void ErrorHandler::log(const Twine &Msg) { 88 if (Verbose) { 89 std::lock_guard<std::mutex> Lock(Mu); 90 *ErrorOS << LogName << ": " << Msg << "\n"; 91 } 92 } 93 94 void ErrorHandler::message(const Twine &Msg) { 95 std::lock_guard<std::mutex> Lock(Mu); 96 outs() << Msg << "\n"; 97 outs().flush(); 98 } 99 100 void ErrorHandler::warn(const Twine &Msg) { 101 if (FatalWarnings) { 102 error(Msg); 103 return; 104 } 105 106 std::lock_guard<std::mutex> Lock(Mu); 107 newline(ErrorOS, Msg); 108 print("warning: ", raw_ostream::MAGENTA); 109 *ErrorOS << Msg << "\n"; 110 } 111 112 void ErrorHandler::error(const Twine &Msg) { 113 std::lock_guard<std::mutex> Lock(Mu); 114 newline(ErrorOS, Msg); 115 116 if (ErrorLimit == 0 || ErrorCount < ErrorLimit) { 117 print("error: ", raw_ostream::RED); 118 *ErrorOS << Msg << "\n"; 119 } else if (ErrorCount == ErrorLimit) { 120 print("error: ", raw_ostream::RED); 121 *ErrorOS << ErrorLimitExceededMsg << "\n"; 122 if (ExitEarly) 123 exitLld(1); 124 } 125 126 ++ErrorCount; 127 } 128 129 void ErrorHandler::fatal(const Twine &Msg) { 130 error(Msg); 131 exitLld(1); 132 } 133