-
Notifications
You must be signed in to change notification settings - Fork 4
/
GnErrors.cpp
297 lines (268 loc) · 7.87 KB
/
GnErrors.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
/*
* Copyright 2019 Rochus Keller <mailto:[email protected]>
*
* This file is part of the GN parser library.
*
* The following is the license that applies to this copy of the
* library. For a license to use the library under conditions
* other than those described here, please email to [email protected].
*
* GNU General Public License Usage
* This file may be used under the terms of the GNU General Public
* License (GPL) versions 2.0 or 3.0 as published by the Free Software
* Foundation and appearing in the file LICENSE.GPL included in
* the packaging of this file. Please review the following information
* to ensure GNU General Public Licensing requirements will be met:
* http://www.fsf.org/licensing/licenses/info/GPLv2.html and
* http://www.gnu.org/copyleft/gpl.html.
*/
#include "GnErrors.h"
#include "GnSynTree.h"
#include <QtDebug>
#include <QFileInfo>
using namespace Gn;
Errors::Errors(QObject *parent, bool threadExclusive) :
QObject(parent),
d_numOfErrs(0),d_numOfWrns(0),d_showWarnings(true),d_threadExclusive(threadExclusive),
d_reportToConsole(false),d_record(false),d_numOfSyntaxErrs(0)
{
}
void Errors::error(Errors::Source s, const SynTree* st, const QString& msg)
{
Q_ASSERT( st != 0 );
error( s, st->d_tok.d_sourcePath, st->d_tok.d_lineNr, st->d_tok.d_colNr, msg );
}
void Errors::error(Errors::Source s, const QString& file, int line, int col, const QString& msg)
{
if( !d_threadExclusive ) d_lock.lockForWrite();
bool inserted = true;
Entry e;
e.d_col = col;
e.d_line = line;
e.d_msg = msg;
e.d_source = s;
e.d_file = file;
if( d_record )
{
EntryList& l = d_errs[file];
const int count = l.size();
l.insert(e);
inserted = count != l.size();
}
if( d_reportToConsole && inserted )
log(e,true);
if( inserted || !d_reportToConsole )
{
d_numOfErrs++;
if( s == Syntax )
d_numOfSyntaxErrs++;
}
if( !d_threadExclusive ) d_lock.unlock();
}
void Errors::warning(Errors::Source s, const SynTree* st, const QString& msg)
{
Q_ASSERT( st != 0 );
warning( s, st->d_tok.d_sourcePath, st->d_tok.d_lineNr, st->d_tok.d_colNr, msg );
}
void Errors::warning(Errors::Source s, const QString& file, int line, int col, const QString& msg)
{
if( !d_threadExclusive ) d_lock.lockForWrite();
bool inserted = true;
if( d_showWarnings )
{
Entry e;
e.d_col = col;
e.d_line = line;
e.d_msg = msg;
e.d_source = s;
e.d_file = file;
if( d_record )
{
EntryList& l = d_wrns[file];
const int count = l.size();
l.insert(e);
inserted = count != l.size();
}
if( d_reportToConsole && inserted )
log(e,false);
}
if( inserted )
d_numOfWrns++;
if( !d_threadExclusive ) d_lock.unlock();
}
bool Errors::showWarnings() const
{
if( !d_threadExclusive ) d_lock.lockForRead();
const bool res = d_showWarnings;
if( !d_threadExclusive ) d_lock.unlock();
return res;
}
void Errors::setShowWarnings(bool on)
{
if( !d_threadExclusive ) d_lock.lockForWrite();
d_showWarnings = on;
if( !d_threadExclusive ) d_lock.unlock();
}
bool Errors::reportToConsole() const
{
if( !d_threadExclusive ) d_lock.lockForRead();
const bool res = d_reportToConsole;
if( !d_threadExclusive ) d_lock.unlock();
return res;
}
void Errors::setReportToConsole(bool on)
{
if( !d_threadExclusive ) d_lock.lockForWrite();
d_reportToConsole = on;
if( !d_threadExclusive ) d_lock.unlock();
}
bool Errors::record() const
{
if( !d_threadExclusive ) d_lock.lockForRead();
const bool res = d_record;
if( !d_threadExclusive ) d_lock.unlock();
return res;
}
void Errors::setRecord(bool on)
{
if( !d_threadExclusive ) d_lock.lockForWrite();
d_record = on;
if( !d_threadExclusive ) d_lock.unlock();
}
quint32 Errors::getErrCount() const
{
if( !d_threadExclusive ) d_lock.lockForRead();
const quint32 res = d_numOfErrs;
if( !d_threadExclusive ) d_lock.unlock();
return res;
}
quint32 Errors::getWrnCount() const
{
if( !d_threadExclusive ) d_lock.lockForRead();
const quint32 res = d_numOfWrns;
if( !d_threadExclusive ) d_lock.unlock();
return res;
}
Errors::EntryList Errors::getErrors(const QString& file) const
{
EntryList res;
if( !d_threadExclusive ) d_lock.lockForRead();
res = d_errs.value(file);
if( !d_threadExclusive ) d_lock.unlock();
return res;
}
Errors::EntryList Errors::getWarnings(const QString& file) const
{
EntryList res;
if( !d_threadExclusive ) d_lock.lockForRead();
res = d_wrns.value(file);
if( !d_threadExclusive ) d_lock.unlock();
return res;
}
Errors::EntriesByFile Errors::getWarnings() const
{
EntriesByFile res;
if( !d_threadExclusive ) d_lock.lockForRead();
res = d_wrns;
if( !d_threadExclusive ) d_lock.unlock();
return res;
}
Errors::EntriesByFile Errors::getErrors() const
{
EntriesByFile res;
if( !d_threadExclusive ) d_lock.lockForRead();
res = d_errs;
if( !d_threadExclusive ) d_lock.unlock();
return res;
}
void Errors::clear()
{
if( !d_threadExclusive ) d_lock.lockForWrite();
d_numOfErrs = 0;
d_numOfWrns = 0;
d_numOfSyntaxErrs = 0;
d_errs.clear();
d_wrns.clear();
if( !d_threadExclusive ) d_lock.unlock();
}
void Errors::clearFile(const QString& file)
{
if( !d_threadExclusive ) d_lock.lockForWrite();
d_numOfErrs -= d_errs[file].size();
d_numOfSyntaxErrs = 0;
d_errs.remove(file);
d_numOfWrns -= d_wrns[file].size();
d_wrns.remove(file);
if( !d_threadExclusive ) d_lock.unlock();
}
void Errors::clearFiles(const QStringList& files)
{
if( !d_threadExclusive ) d_lock.lockForWrite();
foreach( const QString& file, files )
{
d_numOfErrs -= d_errs[file].size();
d_errs.remove(file);
d_numOfWrns -= d_wrns[file].size();
d_wrns.remove(file);
}
if( !d_threadExclusive ) d_lock.unlock();
}
void Errors::update(const Errors& rhs, bool overwrite)
{
if( !d_threadExclusive ) d_lock.lockForWrite();
if( !rhs.d_threadExclusive ) rhs.d_lock.lockForRead();
bool doLog = d_reportToConsole && !rhs.d_reportToConsole;
if( overwrite )
{
d_errs = rhs.d_errs;
d_wrns = rhs.d_wrns;
d_numOfSyntaxErrs = rhs.d_numOfSyntaxErrs;
d_numOfErrs = rhs.d_numOfErrs;
d_numOfWrns = rhs.d_numOfWrns;
}else
{
EntriesByFile::const_iterator i;
for( i = rhs.d_errs.begin(); i != rhs.d_errs.end(); ++i )
{
d_errs[ i.key() ] = i.value();
if( doLog ) foreach( const Entry& e, i.value() ) log(e,true);
}
d_numOfErrs = 0;
d_numOfSyntaxErrs = 0;
for( i = d_errs.begin(); i != d_errs.end(); ++i )
d_numOfErrs += i.value().size();
for( i = rhs.d_wrns.begin(); i != rhs.d_wrns.end(); ++i )
{
d_wrns[ i.key() ] = i.value();
if( doLog ) foreach( const Entry& e, i.value() ) log(e,false);
}
d_numOfWrns = 0;
for( i = d_wrns.begin(); i != d_wrns.end(); ++i )
d_numOfWrns += i.value().size();
}
if( !rhs.d_threadExclusive ) rhs.d_lock.unlock();
if( !d_threadExclusive ) d_lock.unlock();
}
const char* Errors::sourceName(int s)
{
switch(s)
{
case Lexer:
return "Lexer";
case Syntax:
return "Syntax";
case Semantics:
return "Semantics";
default:
return "";
}
}
void Errors::log(const Errors::Entry& e, bool isErr)
{
if( isErr )
qCritical() << d_root.relativeFilePath(e.d_file) << ":" << e.d_line << ":" << e.d_col << ": error:" <<
e.d_msg.toUtf8().data();
else
qWarning() << d_root.relativeFilePath(e.d_file) << ":" << e.d_line << ":" << e.d_col << ": warning:" <<
e.d_msg.toUtf8().data();
}