forked from alberthdev/spasm-ng
-
Notifications
You must be signed in to change notification settings - Fork 1
/
errors.cpp
385 lines (332 loc) · 8.62 KB
/
errors.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
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
#include "stdafx.h"
#define _ERRORS_CPP
#include "errors.h"
#include "spasm.h"
#include "console.h"
#include "list.h"
typedef struct tagERRORINSTANCE
{
LPSTR lpszFileName;
int line_num; //-1 for no line
DWORD dwErrorCode;
int nSession;
bool fSuppressErrors;
bool fIsWarning;
int nPrintSession;
LPTSTR lpszErrorText;
LPTSTR lpszAnnotation;
} ERRORINSTANCE, *LPERRORINSTANCE;
typedef struct _errorlist {
LPERRORINSTANCE data;
struct _errorlist *next;
} errorlist_t;
errorlist_t *g_ErrorList;
int g_nErrorSession = 0;
static void PrintSPASMError(const LPERRORINSTANCE lpError)
{
if (mode & MODE_CODE_COUNTER)
{
return;
}
assert(lpError != NULL);
if ((lpError->dwErrorCode != SPASM_ERR_SUCCESS) || (lpError->lpszErrorText != NULL))
{
WORD orig_attributes = save_console_attributes();
set_console_attributes(lpError->fIsWarning ? COLOR_YELLOW : COLOR_RED);
if (lpError->lpszAnnotation != NULL)
{
printf("%s\n", lpError->lpszAnnotation);
}
printf("%s\n", lpError->lpszErrorText);
#ifdef WINVER
OutputDebugString(lpError->lpszErrorText);
OutputDebugString(_T("\n"));
#endif
restore_console_attributes(orig_attributes);
}
}
static LPERRORINSTANCE AllocErrorInstance()
{
LPERRORINSTANCE lpErr = (LPERRORINSTANCE) malloc(sizeof(ERRORINSTANCE));
lpErr->dwErrorCode = SPASM_ERR_SUCCESS;
lpErr->line_num = -1;
lpErr->lpszFileName = NULL;
//lpErr->fSuppressErrors = suppress_errors;
lpErr->nSession = g_nErrorSession;
lpErr->nPrintSession = -1;
lpErr->lpszErrorText = NULL;
lpErr->lpszAnnotation = NULL;
return lpErr;
}
static void FreeErrorInstance(LPERRORINSTANCE lpErr)
{
if (lpErr->lpszErrorText != NULL)
{
free(lpErr->lpszErrorText);
}
if (lpErr->lpszAnnotation != NULL)
{
free(lpErr->lpszAnnotation);
}
if (lpErr->lpszFileName != NULL)
{
free(lpErr->lpszFileName);
}
free(lpErr);
lpErr = NULL;
}
int StartSPASMErrorSession(void)
{
//suppress_errors = true;
return ++g_nErrorSession;
}
int GetSPASMErrorSessionErrorCount(int nSession)
{
int nCount = 0;
list_t *pList = (list_t *) g_ErrorList;
while ((pList != NULL) && ((LPERRORINSTANCE) pList->data)->nSession == nSession)
{
if (((LPERRORINSTANCE) pList->data)->dwErrorCode != SPASM_ERR_SUCCESS)
{
nCount++;
}
pList = pList->next;
}
return nCount;
}
bool IsSPASMErrorFatal(DWORD dwError)
{
return !(dwError == SPASM_ERR_LOCAL_LABEL_FORWARD_REF ||
dwError == SPASM_ERR_LABEL_NOT_FOUND ||
dwError == SPASM_ERR_INDEX_OFFSET_EXCEEDED ||
dwError == SPASM_ERR_SUCCESS ||
dwError == SPASM_ERR_RECURSION_DEPTH);
}
bool IsSPASMErrorSessionFatal(int nSession)
{
bool fIsFatal = false;
list_t *pList = (list_t *) g_ErrorList;
while ((pList != NULL) && ((LPERRORINSTANCE) pList->data)->nSession == nSession)
{
LPERRORINSTANCE lpError = (LPERRORINSTANCE) pList->data;
DWORD dwError = lpError->dwErrorCode;
if (IsSPASMErrorFatal(dwError))
{
fIsFatal = true;
break;
}
pList = pList->next;
}
return fIsFatal;
}
static void ReplayErrorRecursive(const list_t *pList, bool fFatalOnly)
{
if (pList == NULL)
return;
ReplayErrorRecursive(pList->next, fFatalOnly);
if (((LPERRORINSTANCE) pList->data)->nSession == 1)
{
LPERRORINSTANCE lpError = (LPERRORINSTANCE) pList->data;
if (fFatalOnly)
{
if (!IsSPASMErrorFatal(lpError->dwErrorCode))
{
return;
}
}
PrintSPASMError(lpError);
}
}
void ReplaySPASMErrorSession(int nSession, bool fFatalOnly)
{
if (nSession == 1)
{
ReplayErrorRecursive((list_t *) g_ErrorList, fFatalOnly);
}
else
{
list_t *pList = (list_t *) g_ErrorList;
while ((pList != NULL) && ((LPERRORINSTANCE) pList->data)->nSession == nSession)
{
// Move it up to the next error level
((LPERRORINSTANCE) pList->data)->nSession--;
pList = pList->next;
}
}
}
void ReplayFatalSPASMErrorSession(int nSession)
{
ReplaySPASMErrorSession(nSession, true);
}
bool IsErrorInSPASMErrorSession(int nSession, DWORD dwErrorCode)
{
list_t *pList = (list_t *) g_ErrorList;
while ((pList != NULL) && ((LPERRORINSTANCE) pList->data)->nSession == nSession)
{
LPERRORINSTANCE lpError = (LPERRORINSTANCE) pList->data;
if (lpError->dwErrorCode == dwErrorCode)
{
return true;
}
pList = pList->next;
}
return false;
}
void AddSPASMErrorSessionAnnotation(int nSession, LPCTSTR lpszFormat, ...)
{
va_list valist;
va_start(valist, lpszFormat);
TCHAR szBuffer[256];
TCHAR szDescription[128] = _T("An error occurred");
StringCchVPrintf(szDescription, ARRAYSIZE(szDescription), lpszFormat, valist);
StringCchPrintf(szBuffer, ARRAYSIZE(szBuffer), _T("%s:%d: %s"),
curr_input_file, line_num, szDescription);
va_end(valist);
list_t *pList = (list_t *) g_ErrorList;
while (pList != NULL)
{
LPERRORINSTANCE lpErr = (LPERRORINSTANCE) pList->data;
if (lpErr->nSession >= nSession)
{
if (lpErr->lpszAnnotation != NULL)
{
free(lpErr->lpszAnnotation);
}
lpErr->lpszAnnotation = _tcsdup(szBuffer);
}
pList = pList->next;
}
}
void EndSPASMErrorSession(int nSession)
{
list_t *pList = (list_t *) g_ErrorList;
list_t *pPrev = NULL, *old_list = NULL;
while ((pList != NULL) && ((LPERRORINSTANCE) pList->data)->nSession == nSession)
{
LPERRORINSTANCE lpErr = (LPERRORINSTANCE) pList->data;
if (pPrev == NULL)
{
g_ErrorList = (errorlist_t *) pList->next;
}
else
{
pPrev->next = pList->next;
}
FreeErrorInstance(lpErr);
list_t *pListOld = pList;
pList = pList->next;
list_free_node(pListOld);
//assert(pList != NULL);
if (pList == NULL)
break;
}
g_nErrorSession--;
}
void ClearSPASMErrorSessions()
{
if (g_ErrorList != NULL)
{
list_free((list_t *) g_ErrorList, true, (void (*)(void *)) FreeErrorInstance);
}
g_nErrorSession = 0;
g_ErrorList = NULL;
}
void FreeSPASMErrorSessions(void)
{
list_t *pList = (list_t *) g_ErrorList;
list_t *pNext = NULL;
while (pList)
{
LPERRORINSTANCE lpErr = (LPERRORINSTANCE) pList->data;
pNext = pList->next;
FreeErrorInstance(lpErr);
list_free_node(pList);
pList = pNext;
}
}
#ifdef _TEST
DWORD GetLastSPASMError()
{
list_t *pList = (list_t *) g_ErrorList;
while (pList != NULL)
{
LPERRORINSTANCE lpError = (LPERRORINSTANCE) pList->data;
if (lpError->dwErrorCode != SPASM_ERR_SUCCESS)
{
return lpError->dwErrorCode;
}
pList = pList->next;
}
return SPASM_ERR_SUCCESS;
}
int GetLastSPASMErrorLine()
{
list_t *pList = (list_t *) g_ErrorList;
while (pList != NULL)
{
LPERRORINSTANCE lpError = (LPERRORINSTANCE) pList->data;
if (lpError->dwErrorCode != SPASM_ERR_SUCCESS)
{
return lpError->line_num;
}
pList = pList->next;
}
return SPASM_ERR_SUCCESS;
}
#endif
static void SetLastSPASMProblem(DWORD dwErrorCode, bool fIsWarning, va_list valist)
{
if (dwErrorCode == SPASM_ERR_SUCCESS)
{
return;
}
LPERRORINSTANCE lpErr = AllocErrorInstance();
lpErr->dwErrorCode = dwErrorCode;
lpErr->line_num = line_num;
lpErr->lpszFileName = _strdup(curr_input_file);
//lpErr->fSuppressErrors = suppress_errors;
lpErr->fIsWarning = fIsWarning;
TCHAR szBuffer[256];
TCHAR szDescription[128] = _T("An error occurred");
for (int i = 0; i < ARRAYSIZE(g_ErrorCodes); i++)
{
if (g_ErrorCodes[i].dwCode == lpErr->dwErrorCode)
{
StringCchVPrintf(szDescription, ARRAYSIZE(szDescription),
g_ErrorCodes[i].lpszDescription, valist);
break;
}
}
LPCTSTR lpszProblemType = (fIsWarning) ? _T("warning") : _T("error");
LPCTSTR lpszProblemCode = (fIsWarning) ? _T("SW") : _T("SE");
if (lpErr->line_num != -1)
{
StringCchPrintf(szBuffer, ARRAYSIZE(szBuffer), _T("%s:%d: %s %s%03X: %s"),
lpErr->lpszFileName, lpErr->line_num, lpszProblemType, lpszProblemCode, lpErr->dwErrorCode, szDescription);
}
else
{
StringCchPrintf(szBuffer, ARRAYSIZE(szBuffer), _T("%s: %s %s%03X: %s"),
lpErr->lpszFileName, lpszProblemType, lpszProblemCode, lpErr->dwErrorCode, szDescription);
}
lpErr->lpszErrorText = _strdup(szBuffer);
g_ErrorList = (errorlist_t *) list_prepend((list_t *) g_ErrorList, (LPVOID) lpErr);
//if (suppress_errors == false)
//{
//PrintSPASMError(lpErr);
//}
}
void SetLastSPASMWarning(DWORD dwErrorCode, ...)
{
va_list valist;
va_start(valist, dwErrorCode);
SetLastSPASMProblem(dwErrorCode, true, valist);
va_end(valist);
}
void SetLastSPASMError(DWORD dwErrorCode, ...)
{
va_list valist;
va_start(valist, dwErrorCode);
SetLastSPASMProblem(dwErrorCode, false, valist);
va_end(valist);
}