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/Support/Error.h" 16 #include "llvm/Support/ManagedStatic.h" 17 #include "llvm/Support/raw_ostream.h" 18 #include <mutex> 19 20 #if !defined(_MSC_VER) && !defined(__MINGW32__) 21 #include <unistd.h> 22 #endif 23 24 using namespace llvm; 25 using namespace lld; 26 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. 29 static std::mutex Mu; 30 31 // Prints "\n" or does nothing, depending on Msg contents of 32 // the previous call of this function. 33 static void newline(raw_ostream *ErrorOS, const Twine &Msg) { 34 // True if the previous error message contained "\n". 35 // We want to separate multi-line error messages with a newline. 36 static bool Flag; 37 38 if (Flag) 39 *ErrorOS << "\n"; 40 Flag = StringRef(Msg.str()).contains('\n'); 41 } 42 43 ErrorHandler &lld::errorHandler() { 44 static ErrorHandler Handler; 45 return Handler; 46 } 47 48 void lld::exitLld(int Val) { 49 // Dealloc/destroy ManagedStatic variables before calling 50 // _exit(). In a non-LTO build, this is a nop. In an LTO 51 // build allows us to get the output of -time-passes. 52 llvm_shutdown(); 53 54 outs().flush(); 55 errs().flush(); 56 _exit(Val); 57 } 58 59 void ErrorHandler::print(StringRef S, raw_ostream::Colors C) { 60 *ErrorOS << LogName << ": "; 61 if (ColorDiagnostics) { 62 ErrorOS->changeColor(C, true); 63 *ErrorOS << S; 64 ErrorOS->resetColor(); 65 } else { 66 *ErrorOS << S; 67 } 68 } 69 70 void ErrorHandler::log(const Twine &Msg) { 71 if (Verbose) { 72 std::lock_guard<std::mutex> Lock(Mu); 73 outs() << LogName << ": " << Msg << "\n"; 74 outs().flush(); 75 } 76 } 77 78 void ErrorHandler::message(const Twine &Msg) { 79 std::lock_guard<std::mutex> Lock(Mu); 80 outs() << Msg << "\n"; 81 outs().flush(); 82 } 83 84 void ErrorHandler::warn(const Twine &Msg) { 85 if (FatalWarnings) { 86 error(Msg); 87 return; 88 } 89 90 std::lock_guard<std::mutex> Lock(Mu); 91 newline(ErrorOS, Msg); 92 print("warning: ", raw_ostream::MAGENTA); 93 *ErrorOS << Msg << "\n"; 94 } 95 96 void ErrorHandler::error(const Twine &Msg) { 97 std::lock_guard<std::mutex> Lock(Mu); 98 newline(ErrorOS, Msg); 99 100 if (ErrorLimit == 0 || ErrorCount < ErrorLimit) { 101 print("error: ", raw_ostream::RED); 102 *ErrorOS << Msg << "\n"; 103 } else if (ErrorCount == ErrorLimit) { 104 print("error: ", raw_ostream::RED); 105 *ErrorOS << ErrorLimitExceededMsg << "\n"; 106 if (ExitEarly) 107 exitLld(1); 108 } 109 110 ++ErrorCount; 111 } 112 113 void ErrorHandler::fatal(const Twine &Msg) { 114 error(Msg); 115 exitLld(1); 116 } 117