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/raw_ostream.h"
19 #if !defined(_MSC_VER) && !defined(__MINGW32__)
23 using namespace lld::elf;
28 uint64_t elf::ErrorCount;
29 raw_ostream *elf::ErrorOS;
32 // The functions defined in this file can be called from multiple threads,
33 // but outs() or errs() are not thread-safe. We protect them using a mutex.
36 static void print(StringRef S, raw_ostream::Colors C) {
37 *ErrorOS << Argv0 + ": ";
38 if (Config->ColorDiagnostics) {
39 ErrorOS->changeColor(C, true);
41 ErrorOS->resetColor();
47 void elf::log(const Twine &Msg) {
48 std::lock_guard<std::mutex> Lock(Mu);
50 outs() << Argv0 << ": " << Msg << "\n";
53 void elf::warn(const Twine &Msg) {
54 if (Config->FatalWarnings) {
58 std::lock_guard<std::mutex> Lock(Mu);
59 print("warning: ", raw_ostream::MAGENTA);
60 *ErrorOS << Msg << "\n";
63 void elf::error(const Twine &Msg) {
64 std::lock_guard<std::mutex> Lock(Mu);
66 if (Config->ErrorLimit == 0 || ErrorCount < Config->ErrorLimit) {
67 print("error: ", raw_ostream::RED);
68 *ErrorOS << Msg << "\n";
69 } else if (ErrorCount == Config->ErrorLimit) {
70 print("error: ", raw_ostream::RED);
71 *ErrorOS << "too many errors emitted, stopping now"
72 << " (use -error-limit=0 to see all errors)\n";
73 if (Config->ExitEarly)
80 void elf::error(std::error_code EC, const Twine &Prefix) {
81 error(Prefix + ": " + EC.message());
84 void elf::exitLld(int Val) {
85 // Dealloc/destroy ManagedStatic variables before calling
86 // _exit(). In a non-LTO build, this is a nop. In an LTO
87 // build allows us to get the output of -time-passes.
95 void elf::fatal(const Twine &Msg) {
96 std::lock_guard<std::mutex> Lock(Mu);
97 print("error: ", raw_ostream::RED);
98 *ErrorOS << Msg << "\n";
102 void elf::fatal(std::error_code EC, const Twine &Prefix) {
103 fatal(Prefix + ": " + EC.message());
106 void elf::fatal(Error &E, const Twine &Prefix) {
107 fatal(Prefix + ": " + llvm::toString(std::move(E)));