Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Latest commit

 

History

History
History
485 lines (379 loc) · 12.2 KB

File metadata and controls

485 lines (379 loc) · 12.2 KB
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
/**
*
* @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/CodeFrame.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/build/BuildErrorDetectors.hpp>
#include <algorithm>
#include <iostream>
#include <sstream>
#include <string>
#include <string_view>
#include <unordered_map>
#include <unordered_set>
#include <vector>
#include <fstream>
#include <cstdlib>
#include <cctype>
#include <vix/cli/Style.hpp>
#include <vix/cli/build/BuildStyle.hpp>
using namespace vix::cli::style;
namespace
{
namespace fs = std::filesystem;
void print_hint(std::string_view text)
{
if (text.empty())
return;
std::cerr << YELLOW
<< "hint: "
<< RESET
<< text
<< "\n";
}
std::vector<std::string> read_code_frame_lines(
const fs::path &file,
std::size_t line,
std::size_t contextLines,
std::size_t maxLineWidth)
{
std::vector<std::string> out;
if (file.empty() || line == 0)
return out;
std::ifstream in(file);
if (!in)
return out;
const std::size_t startLine =
line > contextLines ? line - contextLines : 1;
const std::size_t endLine = line + contextLines;
std::string current;
std::size_t currentLine = 0;
while (std::getline(in, current))
{
++currentLine;
if (currentLine < startLine)
continue;
if (currentLine > endLine)
break;
if (maxLineWidth > 0 && current.size() > maxLineWidth)
{
current = current.substr(0, maxLineWidth);
current += "...";
}
out.push_back(current);
}
return out;
}
std::string hint_for_compiler_error(
const vix::cli::errors::CompilerError &err)
{
const std::string &message = err.message;
if (message.find("No such file or directory") != std::string::npos ||
message.find("file not found") != std::string::npos)
{
return "Check that the header exists and that its directory is listed in include_dirs, target_include_directories, or your compiler include paths.";
}
if (message.find("fatal error:") != std::string::npos &&
message.find("#include") != std::string::npos)
{
return "A header could not be found. Check the include path or the header name.";
}
if (message.find("use of undeclared identifier 'std'") != std::string::npos)
{
return "Include the required standard header before using std names.";
}
if (message.find("use of undeclared identifier") != std::string::npos)
{
return "Declare the symbol before use, include the right header, or check the namespace.";
}
if (message.find("was not declared in this scope") != std::string::npos)
{
return "Declare the symbol before use, include the right header, or move the function definition above the call.";
}
if (message.find("does not name a type") != std::string::npos)
{
return "Check that the type is declared before use and that the correct header is included.";
}
if (message.find("expected ';'") != std::string::npos)
{
return "Add the missing semicolon, often on the previous line.";
}
if (message.find("expected primary-expression") != std::string::npos)
{
return "Check the expression syntax near this location, especially missing operators, parentheses, or variables.";
}
if (message.find("no matching function for call to") != std::string::npos)
{
const bool isVixJson =
message.find("vix::http::ResponseWrapper::json") != std::string::npos ||
message.find("ResponseWrapper::json") != std::string::npos;
if (isVixJson)
return "Response::json() expects one JSON value, not key/value arguments.";
return "Check argument types, overloads, const qualifiers, and references.";
}
if (message.find("undefined reference to") != std::string::npos)
{
return "A symbol is declared but not linked. Check missing .cpp files, libraries, or target_link_libraries.";
}
if (message.find("defined but not used") != std::string::npos)
{
return "Remove the unused function or mark it intentionally unused.";
}
return {};
}
vix::cli::build::BuildDiagnostic diagnostic_from_compiler_error(
const vix::cli::errors::CompilerError &err,
const std::string &contextMessage)
{
vix::cli::build::BuildDiagnostic diagnostic;
diagnostic.title =
contextMessage.empty()
? "Build failed"
: contextMessage;
diagnostic.error = err.message;
diagnostic.hint = hint_for_compiler_error(err);
diagnostic.location.file = err.file;
diagnostic.location.line =
err.line > 0 ? static_cast<std::size_t>(err.line) : 0;
diagnostic.location.column =
err.column > 0 ? static_cast<std::size_t>(err.column) : 0;
diagnostic.codeFrame.location = diagnostic.location;
diagnostic.codeFrame.lines =
read_code_frame_lines(
fs::path(err.file),
diagnostic.location.line,
2,
120);
return diagnostic;
}
bool handle_unrecognized_cli_option_as_script_runtime_args(
std::string_view log)
{
const std::size_t pos =
log.find("unrecognized command-line option");
if (pos == std::string_view::npos)
return false;
std::string option;
const std::size_t openQuote = log.find("", pos);
std::size_t closeQuote = std::string_view::npos;
if (openQuote != std::string_view::npos)
closeQuote = log.find("", openQuote + 1);
if (openQuote != std::string_view::npos &&
closeQuote != std::string_view::npos &&
closeQuote > openQuote + 1)
{
option =
std::string(log.substr(openQuote + 1, closeQuote - (openQuote + 1)));
}
if (option.size() < 3 || option.rfind("--", 0) != 0)
return false;
std::cerr << RED
<< "error: runtime argument passed as compiler flag"
<< RESET << "\n";
print_hint("use --args for runtime arguments in .cpp script mode");
std::cerr << GREEN
<< "at: "
<< RESET
<< "vix run <file.cpp> --args " << option << "\n";
return true;
}
std::size_t line_start(std::string_view text, std::size_t pos)
{
if (pos == std::string_view::npos)
return std::string_view::npos;
while (pos > 0 && text[pos - 1] != '\n')
--pos;
return pos;
}
std::size_t find_first_error_anchor(std::string_view log)
{
static constexpr std::string_view anchors[] = {
"FAILED:",
"ninja: build stopped:",
"fatal error:",
"error:",
"CMake Error",
"make: ***",
};
std::size_t best = std::string_view::npos;
for (const auto anchor : anchors)
{
const std::size_t pos = log.find(anchor);
if (pos == std::string_view::npos)
continue;
std::size_t start = line_start(log, pos);
if (start == std::string_view::npos)
start = 0;
if (best == std::string_view::npos || start < best)
best = start;
}
if (best == std::string_view::npos)
{
const std::size_t pos = log.find(": error:");
if (pos != std::string_view::npos)
{
const std::size_t start = line_start(log, pos);
best = start == std::string_view::npos ? 0 : start;
}
}
return best;
}
std::string trim_build_preamble(const std::string &log)
{
const std::string_view view(log);
const std::size_t start = find_first_error_anchor(view);
if (start == std::string_view::npos)
return log;
return std::string(view.substr(start));
}
} // 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))
return true;
if (vix::cli::errors::build::handleBuildErrors(cleanedLog))
return true;
if (RawLogDetectors::handleLinkerOrSanitizer(
cleanedLog,
sourceFile,
contextMessage))
{
return true;
}
vix::cli::build::print_build_error(
std::cerr,
contextMessage.empty() ? "Build failed" : contextMessage);
print_hint("run with --verbose to inspect the full build output");
const bool debugOutput = []()
{
const char *level = std::getenv("VIX_LOG_LEVEL");
if (!level || !*level)
return false;
std::string value(level);
for (char &c : value)
c = static_cast<char>(std::tolower(static_cast<unsigned char>(c)));
return value == "debug" || value == "trace";
}();
if (debugOutput && !cleanedLog.empty())
{
std::cerr << "\n"
<< GRAY
<< "compiler output:"
<< RESET
<< "\n";
std::cerr << cleanedLog;
if (cleanedLog.back() != '\n')
std::cerr << "\n";
}
else
{
print_hint("run with VIX_LOG_LEVEL=debug to inspect the full compiler output");
}
return false;
}
ErrorContext ctx{sourceFile, contextMessage, cleanedLog};
ErrorPipeline pipeline;
if (pipeline.tryHandle(errors, ctx))
return true;
std::unordered_map<std::string, int> counts;
counts.reserve(errors.size());
for (const auto &err : errors)
{
const std::string key = err.file + "|" + err.message;
++counts[key];
}
std::vector<CompilerError> unique;
unique.reserve(errors.size());
std::unordered_set<std::string> seen;
seen.reserve(errors.size());
for (const auto &err : errors)
{
const std::string key = err.file + "|" + err.message;
if (seen.insert(key).second)
unique.push_back(err);
}
if (unique.empty())
{
std::cerr << RED
<< "error: "
<< RESET
<< contextMessage
<< "\n";
print_hint("no unique compiler error was detected; inspect the raw compiler output");
if (!cleanedLog.empty())
std::cerr << cleanedLog << "\n";
return false;
}
std::cerr << RED
<< "error: "
<< RESET
<< contextMessage
<< "\n";
CodeFrameOptions codeFrameOptions;
codeFrameOptions.contextLines = 2;
codeFrameOptions.maxLineWidth = 120;
codeFrameOptions.tabWidth = 4;
const 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];
const vix::cli::build::BuildDiagnostic diagnostic =
::diagnostic_from_compiler_error(
err,
contextMessage);
vix::cli::build::print_build_diagnostic(
std::cerr,
diagnostic);
const std::string key = err.file + "|" + err.message;
const auto it = counts.find(key);
if (it != counts.end() && it->second > 1)
{
vix::cli::build::print_build_warning(
std::cerr,
std::to_string(it->second - 1) + " similar error(s) hidden");
}
}
if (unique.size() > maxToShow)
{
std::cerr << "\n"
<< GRAY
<< (unique.size() - maxToShow)
<< " more distinct error(s) hidden. Run with --verbose for full output."
<< RESET
<< "\n";
}
if (!sourceFile.empty())
{
vix::cli::build::print_build_info(
std::cerr,
"at: " + sourceFile.string());
}
return true;
}
} // namespace vix::cli
Morty Proxy This is a proxified and sanitized view of the page, visit original site.