1 //===- ErrorHandler.cpp ---------------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include "lld/Common/ErrorHandler.h"
10 
11 #include "lld/Common/Threads.h"
12 
13 #include "llvm/ADT/Twine.h"
14 #include "llvm/IR/DiagnosticInfo.h"
15 #include "llvm/IR/DiagnosticPrinter.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   // Delete any temporary file, while keeping the memory mapping open.
50   if (errorHandler().OutputBuffer)
51     errorHandler().OutputBuffer->discard();
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   switch (DI.getSeverity()) {
69   case DS_Error:
70     error(S);
71     break;
72   case DS_Warning:
73     warn(S);
74     break;
75   case DS_Remark:
76   case DS_Note:
77     message(S);
78     break;
79   }
80 }
81 
82 void lld::checkError(Error E) {
83   handleAllErrors(std::move(E),
84                   [&](ErrorInfoBase &EIB) { error(EIB.message()); });
85 }
86 
87 void ErrorHandler::print(StringRef S, raw_ostream::Colors C) {
88   *ErrorOS << LogName << ": ";
89   if (ColorDiagnostics) {
90     ErrorOS->changeColor(C, true);
91     *ErrorOS << S;
92     ErrorOS->resetColor();
93   } else {
94     *ErrorOS << S;
95   }
96 }
97 
98 void ErrorHandler::log(const Twine &Msg) {
99   if (Verbose) {
100     std::lock_guard<std::mutex> Lock(Mu);
101     *ErrorOS << LogName << ": " << Msg << "\n";
102   }
103 }
104 
105 void ErrorHandler::message(const Twine &Msg) {
106   std::lock_guard<std::mutex> Lock(Mu);
107   outs() << Msg << "\n";
108   outs().flush();
109 }
110 
111 void ErrorHandler::warn(const Twine &Msg) {
112   if (FatalWarnings) {
113     error(Msg);
114     return;
115   }
116 
117   std::lock_guard<std::mutex> Lock(Mu);
118   newline(ErrorOS, Msg);
119   print("warning: ", raw_ostream::MAGENTA);
120   *ErrorOS << Msg << "\n";
121 }
122 
123 void ErrorHandler::error(const Twine &Msg) {
124   std::lock_guard<std::mutex> Lock(Mu);
125   newline(ErrorOS, Msg);
126 
127   if (ErrorLimit == 0 || ErrorCount < ErrorLimit) {
128     print("error: ", raw_ostream::RED);
129     *ErrorOS << Msg << "\n";
130   } else if (ErrorCount == ErrorLimit) {
131     print("error: ", raw_ostream::RED);
132     *ErrorOS << ErrorLimitExceededMsg << "\n";
133     if (ExitEarly)
134       exitLld(1);
135   }
136 
137   ++ErrorCount;
138 }
139 
140 void ErrorHandler::fatal(const Twine &Msg) {
141   error(Msg);
142   exitLld(1);
143 }
144