/
hqlerror.cpp
299 lines (252 loc) · 9.4 KB
/
hqlerror.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
298
299
/*##############################################################################
Copyright (C) 2011 HPCC Systems.
All rights reserved. This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
############################################################################## */
#include "jlog.hpp"
#include "hqlerror.hpp"
#include "hqlerrors.hpp"
void MultiErrorReceiver::reportError(int errNo, const char* msg, const char * filename, int lineno, int column, int position)
{
Owned<IECLError> err = createECLError(errNo,msg,filename,lineno,column,position);
report(err);
}
void MultiErrorReceiver::reportWarning(int warnNo, const char* msg, const char * filename, int lineno, int column, int position)
{
Owned<IECLError> warn = createECLWarning(warnNo,msg,filename,lineno,column,position);
report(warn);
}
void MultiErrorReceiver::report(IECLError* error)
{
msgs.append(*LINK(error));
if (error->isError())
errs++;
else
warns++;
StringBuffer msg;
DBGLOG("reportError(%d:%d) : %s", error->getLine(), error->getColumn(), error->errorMessage(msg).str());
}
StringBuffer& MultiErrorReceiver::toString(StringBuffer& buf)
{
ForEachItemIn(i, msgs)
{
IECLError* error=item(i);
error->toString(buf);
buf.append('\n');
}
return buf;
}
IECLError *MultiErrorReceiver::firstError()
{
ForEachItemIn(i, msgs)
{
IECLError* error = item(i);
if (error->isError())
return error;
}
return NULL;
}
CECLError::CECLError(bool _isError, int _no, const char* _msg, const char* _filename, int _lineno, int _column, int _position):
msg(_msg), filename(_filename)
{
iserror = _isError;
no = _no;
lineno = _lineno;
column = _column;
position = _position;
}
StringBuffer& CECLError::toString(StringBuffer& buf)
{
buf.append(filename);
if(lineno && column)
buf.append('(').append(lineno).append(',').append(column).append(')');
buf.append(" : ");
buf.append(no).append(": ").append(msg);
return buf;
}
extern HQL_API IECLError *createECLError(bool isError, int errNo, const char *msg, const char * filename, int lineno, int column, int pos)
{
return new CECLError(isError,errNo,msg,filename,lineno,column,pos);
}
extern HQL_API IECLError * changeErrorType(bool isError, IECLError * error)
{
StringBuffer msg;
return new CECLError(isError,
error->errorCode(), error->errorMessage(msg).str(), error->getFilename(),
error->getLine(), error->getColumn(), error->getPosition());
}
extern HQL_API void reportErrors(IErrorReceiver & receiver, IECLErrorArray & errors)
{
ForEachItemIn(i, errors)
receiver.report(&errors.item(i));
}
//---------------------------------------------------------------------------------------------------------------------
class HQL_API FileErrorReceiver : public CInterface, implements IErrorReceiver
{
public:
IMPLEMENT_IINTERFACE;
int errcount;
int warncount;
FILE *f;
FileErrorReceiver(FILE *_f)
{
errcount = 0;
warncount = 0;
f = _f;
}
virtual void reportError(int errNo, const char *msg, const char * filename, int _lineno, int _column, int _pos)
{
errcount++;
if (!filename) filename = "";
fprintf(f, "%s(%d,%d): error C%04d: %s\n", filename, _lineno, _column, errNo, msg);
}
virtual void reportWarning(int warnNo, const char *msg, const char * filename, int _lineno, int _column, int _pos)
{
warncount++;
if (!filename) filename = *unknownAtom;
fprintf(f, "%s(%d,%d): warning C%04d: %s\n", filename, _lineno, _column, warnNo, msg);
}
virtual void report(IECLError* e)
{
expandReportError(this, e);
}
virtual size32_t errCount() { return errcount; };
virtual size32_t warnCount() { return warncount; };
};
extern HQL_API IErrorReceiver *createFileErrorReceiver(FILE *f)
{
return new FileErrorReceiver(f);
}
//---------------------------------------------------------------------------------------------------------------------
void ThrowingErrorReceiver::reportError(int errNo, const char *msg, const char *filename, int lineno, int column, int pos)
{
throw createECLError(errNo, msg, filename, lineno, column, pos);
}
void ThrowingErrorReceiver::report(IECLError* error)
{
expandReportError(this, error);
}
void ThrowingErrorReceiver::reportWarning(int warnNo, const char *msg, const char *filename, int lineno, int column, int pos)
{
}
//---------------------------------------------------------------------------------------------------------------------
void reportErrorVa(IErrorReceiver * errors, int errNo, const ECLlocation & loc, const char* format, va_list args)
{
StringBuffer msg;
msg.valist_appendf(format, args);
if (errors)
errors->reportError(errNo, msg.str(), loc.sourcePath->str(), loc.lineno, loc.column, loc.position);
else
throw createECLError(errNo, msg.str(), loc.sourcePath->str(), loc.lineno, loc.column, loc.position);
}
void reportError(IErrorReceiver * errors, int errNo, const ECLlocation & loc, const char * format, ...)
{
va_list args;
va_start(args, format);
reportErrorVa(errors, errNo, loc, format, args);
va_end(args);
}
void expandReportError(IErrorReceiver * errors, IECLError* error)
{
StringBuffer msg;
error->errorMessage(msg);
if (error->isError())
errors->reportError(error->errorCode(), msg.str(), error->getFilename(), error->getLine(), error->getColumn(), error->getPosition());
else
errors->reportWarning(error->errorCode(), msg.str(), error->getFilename(), error->getLine(), error->getColumn(), error->getPosition());
}
//---------------------------------------------------------------------------------------------------------------------
class IndirectErrorReceiver : public CInterface, implements IErrorReceiver
{
public:
IndirectErrorReceiver(IErrorReceiver * _prev) : prev(_prev) {}
IMPLEMENT_IINTERFACE
virtual void reportError(int errNo, const char *msg, const char *filename, int lineno, int column, int pos)
{
prev->reportError(errNo, msg, filename, lineno, column, pos);
}
virtual void report(IECLError* err)
{
prev->report(err);
}
virtual void reportWarning(int warnNo, const char *msg, const char *filename, int lineno, int column, int pos)
{
prev->reportWarning(warnNo, msg, filename, lineno, column, pos);
}
virtual size32_t errCount()
{
return prev->errCount();
}
virtual size32_t warnCount()
{
return prev->warnCount();
}
protected:
Linked<IErrorReceiver> prev;
};
class ErrorInserter : public IndirectErrorReceiver
{
public:
ErrorInserter(IErrorReceiver * _prev, IECLError * _error) : IndirectErrorReceiver(_prev), error(_error) {}
virtual void reportError(int errNo, const char *msg, const char *filename, int lineno, int column, int pos)
{
flush();
IndirectErrorReceiver::reportError(errNo, msg, filename, lineno, column, pos);
}
virtual void report(IECLError* err)
{
flush();
IndirectErrorReceiver::report(err);
}
virtual void reportWarning(int warnNo, const char *msg, const char *filename, int lineno, int column, int pos)
{
flush();
IndirectErrorReceiver::reportWarning(warnNo, msg, filename, lineno, column, pos);
}
protected:
void flush()
{
if (error)
{
IndirectErrorReceiver::report(error);
error.clear();
}
}
protected:
Linked<IECLError> error;
};
//---------------------------------------------------------------------------------------------------------------------
void checkEclVersionCompatible(Owned<IErrorReceiver> & errors, const char * eclVersion)
{
if (eclVersion)
{
unsigned major, minor, subminor;
if (extractVersion(major, minor, subminor, eclVersion))
{
if (major != LANGUAGE_VERSION_MAJOR)
throwError2(HQLERR_VersionMismatch, eclVersion, LANGUAGE_VERSION);
if (minor != LANGUAGE_VERSION_MINOR)
{
StringBuffer msg;
msg.appendf("Mismatch in minor version number (%s v %s)", eclVersion, LANGUAGE_VERSION);
errors->reportWarning(HQLERR_VersionMismatch, msg.str(), NULL, 0, 0, 0);
}
else if (subminor != LANGUAGE_VERSION_MINOR)
{
//This adds the warning if any other warnings occur.
StringBuffer msg;
msg.appendf("Mismatch in subminor version number (%s v %s)", eclVersion, LANGUAGE_VERSION);
Owned<IECLError> warning = createECLWarning(HQLERR_VersionMismatch, msg.str(), NULL, 0, 0);
errors.setown(new ErrorInserter(errors, warning));
}
}
}
}