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__)
26 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 // Prints "\n" or does nothing, depending on Msg contents of
37 // the previous call of this function.
38 static void newline(const Twine &Msg) {
39 // True if the previous error message contained "\n".
40 // We want to separate multi-line error messages with a newline.
45 Flag = (StringRef(Msg.str()).find('\n') != StringRef::npos);
48 static void print(StringRef S, raw_ostream::Colors C) {
49 *ErrorOS << Argv0 + ": ";
50 if (Config->ColorDiagnostics) {
51 ErrorOS->changeColor(C, true);
53 ErrorOS->resetColor();
59 void elf::log(const Twine &Msg) {
60 if (Config->Verbose) {
61 std::lock_guard<std::mutex> Lock(Mu);
62 outs() << Argv0 << ": " << Msg << "\n";
66 void elf::message(const Twine &Msg) {
67 std::lock_guard<std::mutex> Lock(Mu);
68 outs() << Msg << "\n";
72 void elf::warn(const Twine &Msg) {
73 if (Config->FatalWarnings) {
78 std::lock_guard<std::mutex> Lock(Mu);
80 print("warning: ", raw_ostream::MAGENTA);
81 *ErrorOS << Msg << "\n";
84 void elf::error(const Twine &Msg) {
85 std::lock_guard<std::mutex> Lock(Mu);
88 if (Config->ErrorLimit == 0 || ErrorCount < Config->ErrorLimit) {
89 print("error: ", raw_ostream::RED);
90 *ErrorOS << Msg << "\n";
91 } else if (ErrorCount == Config->ErrorLimit) {
92 print("error: ", raw_ostream::RED);
93 *ErrorOS << "too many errors emitted, stopping now"
94 << " (use -error-limit=0 to see all errors)\n";
95 if (Config->ExitEarly)
102 void elf::exitLld(int Val) {
103 // Dealloc/destroy ManagedStatic variables before calling
104 // _exit(). In a non-LTO build, this is a nop. In an LTO
105 // build allows us to get the output of -time-passes.
113 void elf::fatal(const Twine &Msg) {