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   waitForBackgroundThreads();
50   // Dealloc/destroy ManagedStatic variables before calling
51   // _exit(). In a non-LTO build, this is a nop. In an LTO
52   // build allows us to get the output of -time-passes.
53   llvm_shutdown();
54 
55   outs().flush();
56   errs().flush();
57   _exit(Val);
58 }
59 
60 void ErrorHandler::print(StringRef S, raw_ostream::Colors C) {
61   *ErrorOS << LogName << ": ";
62   if (ColorDiagnostics) {
63     ErrorOS->changeColor(C, true);
64     *ErrorOS << S;
65     ErrorOS->resetColor();
66   } else {
67     *ErrorOS << S;
68   }
69 }
70 
71 void ErrorHandler::log(const Twine &Msg) {
72   if (Verbose) {
73     std::lock_guard<std::mutex> Lock(Mu);
74     outs() << LogName << ": " << Msg << "\n";
75     outs().flush();
76   }
77 }
78 
79 void ErrorHandler::message(const Twine &Msg) {
80   std::lock_guard<std::mutex> Lock(Mu);
81   outs() << Msg << "\n";
82   outs().flush();
83 }
84 
85 void ErrorHandler::warn(const Twine &Msg) {
86   if (FatalWarnings) {
87     error(Msg);
88     return;
89   }
90 
91   std::lock_guard<std::mutex> Lock(Mu);
92   newline(ErrorOS, Msg);
93   print("warning: ", raw_ostream::MAGENTA);
94   *ErrorOS << Msg << "\n";
95 }
96 
97 void ErrorHandler::error(const Twine &Msg) {
98   std::lock_guard<std::mutex> Lock(Mu);
99   newline(ErrorOS, Msg);
100 
101   if (ErrorLimit == 0 || ErrorCount < ErrorLimit) {
102     print("error: ", raw_ostream::RED);
103     *ErrorOS << Msg << "\n";
104   } else if (ErrorCount == ErrorLimit) {
105     print("error: ", raw_ostream::RED);
106     *ErrorOS << ErrorLimitExceededMsg << "\n";
107     if (ExitEarly)
108       exitLld(1);
109   }
110 
111   ++ErrorCount;
112 }
113 
114 void ErrorHandler::fatal(const Twine &Msg) {
115   error(Msg);
116   exitLld(1);
117 }
118