-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathErrorHandler.cpp
More file actions
306 lines (255 loc) · 8.09 KB
/
ErrorHandler.cpp
File metadata and controls
306 lines (255 loc) · 8.09 KB
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
305
306
/**
*
* @file ErrorHandler.cpp
* @author Gaspard Kirira
*
* Copyright 2025, Gaspard Kirira. All rights reserved.
* https://github.com/vixcpp/vix
* Use of this source code is governed by a MIT license
* that can be found in the License file.
*
* Vix.cpp
*
*/
#include <vix/cli/ErrorHandler.hpp>
#include <vix/cli/errors/ClangGccParser.hpp>
#include <vix/cli/errors/CompilerError.hpp>
#include <vix/cli/errors/ErrorContext.hpp>
#include <vix/cli/errors/ErrorPipeline.hpp>
#include <vix/cli/errors/RawLogDetectors.hpp>
#include <vix/cli/errors/CodeFrame.hpp>
#include <vix/cli/errors/build/BuildErrorDetectors.hpp>
#include <vix/utils/Env.hpp>
#include <iostream>
#include <sstream>
#include <unordered_map>
#include <unordered_set>
#include <algorithm>
#include <cstring>
#include <vix/cli/Style.hpp>
using namespace vix::cli::style;
namespace
{
static void printSingleError(const vix::cli::errors::CompilerError &err)
{
std::ostringstream oss;
oss << err.file << ":" << err.line << ":" << err.column << "\n";
oss << " error: " << err.message << "\n";
error(oss.str());
}
static bool handle_unrecognized_cli_option_as_script_runtime_args(std::string_view log)
{
// Exemple:
// c++: error: unrecognized command-line option ‘--config’; did you mean ...
const std::size_t p = log.find("unrecognized command-line option");
if (p == std::string_view::npos)
return false;
std::string opt;
{
std::size_t q1 = log.find("‘", p);
std::size_t q2 = std::string_view::npos;
if (q1 != std::string_view::npos)
q2 = log.find("’", q1 + 1);
if (q1 != std::string_view::npos && q2 != std::string_view::npos && q2 > q1 + 1)
opt = std::string(log.substr(q1 + 1, q2 - (q1 + 1)));
}
if (opt.size() >= 3 && opt.rfind("--", 0) == 0)
{
error("Script build failed: you passed runtime args as compiler flags.");
hint("In .cpp script mode, everything after `--` is treated as compiler/linker flags.");
hint("Use repeatable --args for runtime arguments.");
if (!opt.empty())
{
std::cerr << GRAY << "example: vix run main.cpp --args " << opt << " --args <value>\n"
<< RESET;
}
return true;
}
return false;
}
static bool hints_verbose_enabled() noexcept
{
const char *lvl = vix::utils::vix_getenv("VIX_LOG_LEVEL");
if (!lvl || !*lvl)
return false;
return std::strcmp(lvl, "debug") == 0 ||
std::strcmp(lvl, "trace") == 0;
}
static std::size_t line_start(std::string_view s, std::size_t pos)
{
if (pos == std::string_view::npos)
return std::string_view::npos;
while (pos > 0 && s[pos - 1] != '\n')
--pos;
return pos;
}
static std::size_t find_first_error_anchor(std::string_view log)
{
static constexpr std::string_view anchors[] = {
"FAILED:", // ninja
"ninja: build stopped:", // ninja
"error:", // gcc/clang
"fatal error:", // gcc/clang
"CMake Error", // cmake configure
"make: ***" // make
};
std::size_t best = std::string_view::npos;
for (auto a : anchors)
{
std::size_t p = log.find(a);
if (p == std::string_view::npos)
continue;
std::size_t ls = line_start(log, p);
if (ls == std::string_view::npos)
ls = 0;
if (best == std::string_view::npos || ls < best)
best = ls;
}
if (best == std::string_view::npos)
{
std::size_t p = log.find(": error:");
if (p != std::string_view::npos)
{
std::size_t ls = line_start(log, p);
best = (ls == std::string_view::npos) ? 0 : ls;
}
}
return best;
}
static std::string trim_build_preamble(const std::string &log)
{
std::string_view v(log);
const std::size_t start = find_first_error_anchor(v);
if (start == std::string_view::npos)
return log;
return std::string(v.substr(start));
}
static void printHints(const vix::cli::errors::CompilerError &err)
{
const std::string &msg = err.message;
const bool verbose = hints_verbose_enabled();
auto h = [](const std::string &s)
{
std::cerr << "\n"
<< YELLOW << "hint: " << RESET << s << "\n";
};
if (msg.find("use of undeclared identifier 'std'") != std::string::npos)
{
h("std is not visible here (include the required standard header)");
if (verbose)
std::cerr << GRAY << "e.g. #include <iostream>\n"
<< RESET;
return;
}
if (msg.find("expected ';'") != std::string::npos)
{
h("missing ';' (often the previous line)");
return;
}
if (msg.find("no matching function for call to") != std::string::npos)
{
const bool isVixJson =
(msg.find("vix::vhttp::ResponseWrapper::json") != std::string::npos) ||
(msg.find("ResponseWrapper::json") != std::string::npos);
if (isVixJson)
{
h("Response::json() expects one JSON value (not key,value)");
if (verbose)
std::cerr << GRAY << "e.g. res.json({\"message\", \"Hello\"});\n"
<< RESET;
return;
}
h("no matching overload (check argument types and qualifiers)");
return;
}
}
} // namespace
namespace vix::cli
{
bool ErrorHandler::printBuildErrors(
const std::string &buildLog,
const fs::path &sourceFile,
const std::string &contextMessage)
{
using namespace vix::cli::errors;
const std::string cleanedLog = ::trim_build_preamble(buildLog);
auto errors = ClangGccParser::parse(cleanedLog);
if (errors.empty())
{
if (handle_unrecognized_cli_option_as_script_runtime_args(cleanedLog))
{
std::cerr << "\n"
<< GRAY << "compiler output:\n"
<< RESET;
std::cerr << cleanedLog << "\n";
return true;
}
if (RawLogDetectors::handleLinkerOrSanitizer(cleanedLog, sourceFile, contextMessage))
return true;
if (vix::cli::errors::build::handleBuildErrors(cleanedLog))
return true;
error(contextMessage + " (see compiler output below):");
std::cerr << cleanedLog << "\n";
return false;
}
ErrorContext ctx{sourceFile, contextMessage, cleanedLog};
ErrorPipeline pipeline;
if (pipeline.tryHandle(errors, ctx))
return true;
std::unordered_map<std::string, int> counts;
for (const auto &e : errors)
{
std::string key = e.file + "|" + e.message;
counts[key]++;
}
std::vector<CompilerError> unique;
unique.reserve(errors.size());
std::unordered_set<std::string> seen;
for (const auto &e : errors)
{
std::string key = e.file + "|" + e.message;
if (seen.insert(key).second)
unique.push_back(e);
}
if (unique.empty())
{
error(contextMessage + " (no unique errors found, see compiler output below):");
std::cerr << buildLog << "\n";
return false;
}
error(contextMessage + ":");
CodeFrameOptions cf;
cf.contextLines = 2;
cf.maxLineWidth = 120;
cf.tabWidth = 4;
std::size_t maxToShow = std::min<std::size_t>(unique.size(), 3);
for (std::size_t i = 0; i < maxToShow; ++i)
{
const auto &err = unique[i];
std::cerr << "\n";
::printSingleError(err);
::printHints(err);
ErrorContext frameCtx{
err.file,
contextMessage,
cleanedLog};
printCodeFrame(err, frameCtx, cf);
const std::string key = err.file + "|" + err.message;
auto it = counts.find(key);
if (it != counts.end() && it->second > 1)
{
std::cerr << GRAY
<< "\n (" << (it->second - 1)
<< " similar error(s) hidden)\n"
<< RESET;
}
}
if (unique.size() > maxToShow)
{
std::cerr << "\n… " << (unique.size() - maxToShow)
<< " more distinct errors hidden. Run the build manually for full output.\n";
}
std::cerr << "\nSource file: " << sourceFile << "\n";
return true;
}
} // namespace vix::cli