]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm-project/lld/Common/ErrorHandler.cpp
MFC r355940:
[FreeBSD/FreeBSD.git] / contrib / llvm-project / lld / Common / ErrorHandler.cpp
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 #include <regex>
20
21 #if !defined(_MSC_VER) && !defined(__MINGW32__)
22 #include <unistd.h>
23 #endif
24
25 using namespace llvm;
26 using namespace lld;
27
28 // The functions defined in this file can be called from multiple threads,
29 // but outs() or errs() are not thread-safe. We protect them using a mutex.
30 static std::mutex mu;
31
32 // Prints "\n" or does nothing, depending on Msg contents of
33 // the previous call of this function.
34 static void newline(raw_ostream *errorOS, const Twine &msg) {
35   // True if the previous error message contained "\n".
36   // We want to separate multi-line error messages with a newline.
37   static bool flag;
38
39   if (flag)
40     *errorOS << "\n";
41   flag = StringRef(msg.str()).contains('\n');
42 }
43
44 ErrorHandler &lld::errorHandler() {
45   static ErrorHandler handler;
46   return handler;
47 }
48
49 void lld::exitLld(int val) {
50   // Delete any temporary file, while keeping the memory mapping open.
51   if (errorHandler().outputBuffer)
52     errorHandler().outputBuffer->discard();
53
54   // Dealloc/destroy ManagedStatic variables before calling
55   // _exit(). In a non-LTO build, this is a nop. In an LTO
56   // build allows us to get the output of -time-passes.
57   llvm_shutdown();
58
59   outs().flush();
60   errs().flush();
61   _exit(val);
62 }
63
64 void lld::diagnosticHandler(const DiagnosticInfo &di) {
65   SmallString<128> s;
66   raw_svector_ostream os(s);
67   DiagnosticPrinterRawOStream dp(os);
68   di.print(dp);
69   switch (di.getSeverity()) {
70   case DS_Error:
71     error(s);
72     break;
73   case DS_Warning:
74     warn(s);
75     break;
76   case DS_Remark:
77   case DS_Note:
78     message(s);
79     break;
80   }
81 }
82
83 void lld::checkError(Error e) {
84   handleAllErrors(std::move(e),
85                   [&](ErrorInfoBase &eib) { error(eib.message()); });
86 }
87
88 static std::string getLocation(std::string msg, std::string defaultMsg) {
89   static std::vector<std::regex> Regexes{
90       std::regex(R"(^undefined symbol:.*\n>>> referenced by (\S+):(\d+)\n.*)"),
91       std::regex(R"(^undefined symbol:.*\n>>> referenced by (.*):)"),
92       std::regex(
93           R"(^duplicate symbol: .*\n>>> defined in (\S+)\n>>> defined in.*)"),
94       std::regex(
95           R"(^duplicate symbol: .*\n>>> defined at (\S+):(\d+).*)"),
96       std::regex(
97           R"(.*\n>>> defined in .*\n>>> referenced by (\S+):(\d+))"),
98       std::regex(
99           R"(^undefined (internal|hidden|protected) symbol: .*\n>>> referenced by (\S+):(\d+)\n.*)"),
100       std::regex(R"((\S+):(\d+): unclosed quote)"),
101   };
102
103   std::smatch Match;
104   for (std::regex &Re : Regexes) {
105     if (std::regex_search(msg, Match, Re)) {
106       return Match.size() > 2 ? Match.str(1) + "(" + Match.str(2) + ")"
107                               : Match.str(1);
108     }
109   }
110   return defaultMsg;
111 }
112
113 void ErrorHandler::printHeader(StringRef s, raw_ostream::Colors c,
114                                const Twine &msg) {
115
116   if (vsDiagnostics) {
117     // A Visual Studio-style error message starts with an error location.
118     // If a location cannot be extracted then we default to LogName.
119     *errorOS << getLocation(msg.str(), logName) << ": ";
120   } else {
121     *errorOS << logName << ": ";
122   }
123
124   if (colorDiagnostics) {
125     errorOS->changeColor(c, true);
126     *errorOS << s;
127     errorOS->resetColor();
128   } else {
129     *errorOS << s;
130   }
131 }
132
133 void ErrorHandler::log(const Twine &msg) {
134   if (verbose) {
135     std::lock_guard<std::mutex> lock(mu);
136     *errorOS << logName << ": " << msg << "\n";
137   }
138 }
139
140 void ErrorHandler::message(const Twine &msg) {
141   std::lock_guard<std::mutex> lock(mu);
142   outs() << msg << "\n";
143   outs().flush();
144 }
145
146 void ErrorHandler::warn(const Twine &msg) {
147   if (fatalWarnings) {
148     error(msg);
149     return;
150   }
151
152   std::lock_guard<std::mutex> lock(mu);
153   newline(errorOS, msg);
154   printHeader("warning: ", raw_ostream::MAGENTA, msg);
155   *errorOS << msg << "\n";
156 }
157
158 void ErrorHandler::error(const Twine &msg) {
159   std::lock_guard<std::mutex> lock(mu);
160   newline(errorOS, msg);
161
162   if (errorLimit == 0 || errorCount < errorLimit) {
163     printHeader("error: ", raw_ostream::RED, msg);
164     *errorOS << msg << "\n";
165   } else if (errorCount == errorLimit) {
166     printHeader("error: ", raw_ostream::RED, msg);
167     *errorOS << errorLimitExceededMsg << "\n";
168     if (exitEarly)
169       exitLld(1);
170   }
171
172   ++errorCount;
173 }
174
175 void ErrorHandler::fatal(const Twine &msg) {
176   error(msg);
177   exitLld(1);
178 }