-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathError.cpp
304 lines (274 loc) · 6.18 KB
/
Error.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
//
// Licensed under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
// Copyright (c) 2023 Vinnie Falco (vinnie.falco@gmail.com)
//
// Official repository: https://github.com/cppalliance/mrdocs
//
#include "lib/Support/Error.hpp"
#include <mrdocs/Support/Path.hpp>
#include <mrdocs/Version.hpp>
#include <llvm/Support/Mutex.h>
#include <llvm/Support/raw_ostream.h>
#include <llvm/Support/Signals.h>
#include <cstdlib>
#include <mutex>
#ifdef _MSC_VER
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <debugapi.h>
#include <crtdbg.h>
#include <sstream>
#endif
namespace SourceFileNames {
extern char const* getFileName(char const*) noexcept;
} // SourceFileNames
namespace clang {
namespace mrdocs {
//------------------------------------------------
//
// Error
//
//------------------------------------------------
std::string
Error::
formatWhere(
source_location const& loc)
{
return fmt::format("{}:{}",
::SourceFileNames::getFileName(
loc.file_name()),
loc.line());
#if 0
return fmt::format(
"{}@{}({})",
loc.function_name(),
::SourceFileNames::getFileName(
loc.file_name()),
loc.line());
#endif
}
std::string
Error::
formatMessage(
std::string_view const& reason,
std::string_view const& where)
{
std::string result;
result = reason;
result.append(" (");
result.append(where);
result.append(")");
return result;
}
Error::
Error(
std::string reason,
source_location loc)
: where_(formatWhere(loc))
, reason_(std::move(reason))
, message_(formatMessage(reason_, where_))
, loc_(loc)
{
MRDOCS_ASSERT(! message_.empty());
}
Error::
Error(
std::error_code const& ec,
source_location loc)
{
if(! ec)
return;
where_ = formatWhere(loc);
reason_ = ec.message();
message_ = formatMessage(reason_, where_);
loc_ = loc;
}
Error::
Error(
std::exception const& ex)
{
std::string_view s(ex.what());
if(s.empty())
s = "unknown exception";
reason_ = s;
message_ = s;
}
Error::
Error(
std::vector<Error> const& errors,
source_location loc)
{
MRDOCS_ASSERT(errors.size() > 0);
if(errors.size() == 1)
{
*this = errors.front();
return;
}
where_ = formatWhere(loc);
reason_ = fmt::format(
"{} errors occurred:\n", errors.size());
for(auto const& err : errors)
{
reason_.append(" ");
reason_.append(err.message());
reason_.push_back('\n');
}
message_ = formatMessage(reason_, where_);
loc_ = loc;
}
void Error::Throw() const&
{
MRDOCS_ASSERT(failed());
throw Exception(*this);
}
void Error::Throw() &&
{
MRDOCS_ASSERT(failed());
throw Exception(std::move(*this));
}
//------------------------------------------------
SourceLocation::
SourceLocation(
source_location const& loc) noexcept
: file_(files::getSourceFilename(loc.file_name()))
, line_(loc.line())
, col_(loc.column())
, func_(loc.function_name())
{
}
//------------------------------------------------
//
// Reporting
//
//------------------------------------------------
namespace report {
static llvm::sys::Mutex mutex_;
static Level level_ = Level::debug;
static bool sourceLocationWarnings_ = true;
constinit Results results{};
static
void
print_impl(
std::string const& s)
{
llvm::errs() << s << '\n';
#ifdef _MSC_VER
if(::IsDebuggerPresent() != 0)
{
::OutputDebugStringA(s.c_str());
::OutputDebugStringA("\n");
}
#endif
}
void
setMinimumLevel(
Level level) noexcept
{
level_ = level;
}
void
setSourceLocationWarnings(bool b) noexcept
{
sourceLocationWarnings_ = b;
}
void
print(
std::string const& text)
{
print_impl(text);
}
void
print(
Level level,
std::string const& text,
source_location const* loc,
Error const* e)
{
call_impl(level,
[&](llvm::raw_ostream& os)
{
os << text;
}, loc, e);
}
//------------------------------------------------
Level
getLevel(unsigned level) noexcept
{
switch(level)
{
case 0: return Level::debug;
case 1: return Level::info;
case 2: return Level::warn;
case 3: return Level::error;
default:
return Level::fatal;
}
}
void
call_impl(
Level level,
std::function<void(llvm::raw_ostream&)> f,
source_location const* loc,
Error const* e)
{
std::string s;
if(level >= level_)
{
llvm::raw_string_ostream os(s);
f(os);
if(sourceLocationWarnings_ &&
loc && (
level == Level::warn ||
level == Level::error ||
level == Level::fatal))
{
os << "\n\n";
os << "An issue occurred during execution.\n";
os << "If you believe this is a bug, please report it at https://github.com/cppalliance/mrdocs/issues\n"
"with the following details:\n";
os << fmt::format(" MrDocs Version: {} (Build: {})\n", project_version, project_version_build);
if (e)
{
os << fmt::format(
" Error Location: `{}` at line {}\n",
::SourceFileNames::getFileName(e->location().file_name()),
e->location().line());
}
os << fmt::format(
" Reported From: `{}` at line {}\n",
::SourceFileNames::getFileName(loc->file_name()),
loc->line());
// VFALCO attach a stack trace for Level::fatal
}
os << '\n';
}
std::lock_guard<llvm::sys::Mutex> lock(mutex_);
if(! s.empty())
llvm::errs() << s;
switch(level)
{
case Level::debug:
++results.debugCount;
break;
case Level::info:
++results.infoCount;
break;
case Level::warn:
++results.warnCount;
break;
case Level::error:
++results.errorCount;
break;
case Level::fatal:
++results.fatalCount;
break;
default:
MRDOCS_UNREACHABLE();
}
}
} // report
} // mrdocs
} // clang