-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDiagnosticEngine.cpp
More file actions
185 lines (164 loc) · 4.45 KB
/
Copy pathDiagnosticEngine.cpp
File metadata and controls
185 lines (164 loc) · 4.45 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
#include "DiagnosticEngine.h"
#include <sstream>
#include <cstdio>
namespace QLang
{
void DiagnosticEngine::report( const Diagnostic &diag )
{
// Dedupe by (file,line,col,code): panic-mode recovery and repeated passes can
// re-surface the same problem; one buffered copy is enough.
for ( const Diagnostic &existing : mDiags )
{
if ( existing.location.file == diag.location.file &&
existing.location.line == diag.location.line &&
existing.location.col == diag.location.col &&
existing.code == diag.code )
return;
}
// Cap the buffer so recovery cannot spew an unbounded phantom cascade.
if ( mDiags.size() >= kMaxDiags )
{
mTruncated = true;
return;
}
mDiags.push_back( diag );
}
void DiagnosticEngine::error( const SourceLocation &loc, const std::string &message,
const std::string &code )
{
Diagnostic diag;
diag.severity = Severity::Error;
diag.location = loc;
diag.message = message;
diag.code = code;
report( diag );
}
void DiagnosticEngine::warning( const SourceLocation &loc, const std::string &message,
const std::string &code )
{
Diagnostic diag;
diag.severity = Severity::Warning;
diag.location = loc;
diag.message = message;
diag.code = code;
report( diag );
}
void DiagnosticEngine::reportCompileError( const CompileError &err )
{
Diagnostic diag;
diag.severity = Severity::Error;
diag.location = err.getLocation();
diag.message = err.getMessage();
diag.code = "syntax";
report( diag );
// Compiler-internal throw-site coordinates are for compiler developers and
// appear only under --debug-compiler, rendered by finish() (human mode).
if ( mDebugCompiler && mDebugTail.empty() )
{
std::ostringstream oss;
oss << "[debug] thrown at " << err.getInternalFile() << ":"
<< err.getInternalLine();
mDebugTail = oss.str();
}
}
Severity DiagnosticEngine::effectiveSeverity( const Diagnostic &d ) const
{
if ( mWerror && d.severity == Severity::Warning )
return Severity::Error;
return d.severity;
}
bool DiagnosticEngine::hasErrors() const
{
for ( const Diagnostic &d : mDiags )
{
if ( effectiveSeverity( d ) == Severity::Error )
return true;
}
return false;
}
static const char *severityLabel( Severity s )
{
return ( s == Severity::Error ) ? "error" : "warning";
}
// Minimal JSON string escaping for the fields we emit (message/file/code).
static std::string jsonEscape( const std::string &in )
{
std::string out;
out.reserve( in.size() + 2 );
for ( char c : in )
{
switch ( c )
{
case '"': out += "\\\""; break;
case '\\': out += "\\\\"; break;
case '\n': out += "\\n"; break;
case '\r': out += "\\r"; break;
case '\t': out += "\\t"; break;
default:
if ( static_cast<unsigned char>( c ) < 0x20 )
{
char buf[8];
std::snprintf( buf, sizeof( buf ), "\\u%04x", c );
out += buf;
}
else
{
out += c;
}
}
}
return out;
}
void DiagnosticEngine::renderHuman()
{
for ( const Diagnostic &d : mDiags )
{
const SourceLocation &loc = d.location;
// Human label reflects the diagnostic's own severity; -Werror changes the
// exit code (hasErrors), not the printed label.
mOut << loc.file << ":" << loc.line << ":" << loc.col
<< ": " << severityLabel( d.severity ) << ": " << d.message << std::endl;
for ( const Note ¬e : d.notes )
{
const SourceLocation &nloc = note.location;
mOut << nloc.file << ":" << nloc.line << ":" << nloc.col
<< ": note: " << note.message << std::endl;
}
}
if ( mTruncated )
mOut << "note: too many diagnostics; further reports suppressed" << std::endl;
if ( !mDebugTail.empty() )
mOut << mDebugTail << std::endl;
}
void DiagnosticEngine::renderJson()
{
// A single JSON array; the sole content written to mOut in --json mode so a
// `2>&1 | python3 -c json.load` consumer parses cleanly.
mOut << "[";
for ( std::size_t i = 0; i < mDiags.size(); i++ )
{
const Diagnostic &d = mDiags[i];
if ( i > 0 )
mOut << ",";
mOut << "{"
<< "\"severity\":\"" << severityLabel( d.severity ) << "\","
<< "\"file\":\"" << jsonEscape( d.location.file ) << "\","
<< "\"line\":" << d.location.line << ","
<< "\"col\":" << d.location.col << ","
<< "\"message\":\"" << jsonEscape( d.message ) << "\","
<< "\"code\":\"" << jsonEscape( d.code ) << "\""
<< "}";
}
mOut << "]" << std::endl;
}
void DiagnosticEngine::finish()
{
if ( mFinished )
return;
mFinished = true;
if ( mJson )
renderJson();
else
renderHuman();
}
} // namespace QLang