-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdiagnostics.cpp
More file actions
94 lines (83 loc) · 2.78 KB
/
diagnostics.cpp
File metadata and controls
94 lines (83 loc) · 2.78 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
#include "diagnostics.h"
void DiagnosticEngine::report_error(const std::string& msg, SrcLoc loc) {
if (fatal_errors) {
// Legacy mode: first error throws immediately
if (sm && !loc.isInvalid()) {
throw std::runtime_error(sm->formatDiagnostic(DiagnosticLevel::Error, msg, loc));
}
throw std::runtime_error("error: " + msg);
}
Diagnostic diag;
diag.level = DiagnosticLevel::Error;
diag.message = msg;
diag.location = loc;
diag.suppressed = suppress_errors;
diagnostics.push_back(diag);
if (!suppress_errors) {
error_count++;
}
suppress_errors = true;
if (limit_reached()) {
throw FatalErrorLimitReached();
}
}
void DiagnosticEngine::report_warning(const std::string& msg, SrcLoc loc) {
Diagnostic diag;
diag.level = DiagnosticLevel::Warning;
diag.message = msg;
diag.location = loc;
diagnostics.push_back(diag);
warning_count++;
}
void DiagnosticEngine::report_note(const std::string& msg, SrcLoc loc) {
Diagnostic diag;
diag.level = DiagnosticLevel::Note;
diag.message = msg;
diag.location = loc;
diagnostics.push_back(diag);
}
void DiagnosticEngine::flush_diagnostics() const {
if (!emit_to_stderr) {
return;
}
for (const auto& diag : diagnostics) {
if (diag.suppressed) continue;
std::string formatted = format(diag.level, diag.message, diag.location);
std::cerr << formatted;
// formatDiagnostic may or may not end with newline
if (!formatted.empty() && formatted.back() != '\n') {
std::cerr << "\n";
}
}
if (limit_reached()) {
std::cerr << "fatal: too many errors emitted, stopping now\n";
}
}
std::string DiagnosticEngine::format(DiagnosticLevel level, const std::string& msg, SrcLoc loc) const {
if (sm && !loc.isInvalid()) {
return sm->formatDiagnostic(level, msg, loc);
}
std::string level_text;
switch (level) {
case DiagnosticLevel::Error: level_text = "error"; break;
case DiagnosticLevel::Warning: level_text = "warning"; break;
case DiagnosticLevel::Note: level_text = "note"; break;
}
return level_text + ": " + msg;
}
DiagnosticEngine::Checkpoint DiagnosticEngine::checkpoint() const {
Checkpoint cp;
cp.diagnostics_size = diagnostics.size();
cp.error_count = error_count;
cp.warning_count = warning_count;
cp.suppress_errors = suppress_errors;
return cp;
}
void DiagnosticEngine::restore(const Checkpoint& checkpoint) {
if (diagnostics.size() > checkpoint.diagnostics_size) {
diagnostics.resize(checkpoint.diagnostics_size);
}
error_count = checkpoint.error_count;
warning_count = checkpoint.warning_count;
suppress_errors = checkpoint.suppress_errors;
}