File: GrcErrorList.cpp

package info (click to toggle)
grcompiler 4.2-4
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 11,076 kB
  • ctags: 5,163
  • sloc: cpp: 45,565; sh: 4,451; ansic: 4,377; makefile: 185; xml: 175; perl: 127
file content (432 lines) | stat: -rw-r--r-- 13,574 bytes parent folder | download | duplicates (4)
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
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
/*--------------------------------------------------------------------*//*:Ignore this sentence.
Copyright (C) 1999, 2001 SIL International. All rights reserved.

Distributable under the terms of either the Common Public License or the
GNU Lesser General Public License, as specified in the LICENSING.txt file.

File: GrcErrorList.cpp
Responsibility: Sharon Correll
Last reviewed: Not yet.

Description:

Here are how the error and warning IDs are assigned:

global - highest error: 142 / highest warning: 511
		main.cpp
		GrpParser.g
		GrpLexer
		GrpParser
		GrcFont

parsing - highest error: 1183 / highest warning: 1513
		ParserTreeWalker
		PostParser

error checking:
	general - highest error: 2164 / highest warning: 2535
		GdlExpression
		GrcMasterTable
		GrcSymTable
		GdlNameDefn
		GdlObject
		GrcBinaryStream
		GrcEnv
		GdlRenderer
	rules - highest error: 3162 / highest warning: 3533
		ErrorCheckRules
		GdlRule
		GdlTablePass
		Fsm
	classes - highest error: 4147 / highest warning: 4517
		ErrorCheckClass
		GrcGlyphAttrMatrix
		GdlGlyphDefn
		GdlGlyphClassDefn

compilation:	highest error: 5101 / highest warning: 5505
		OutputToFont

test and debug: highest error: 6106 / highest warning: none (start at: 6500)
-------------------------------------------------------------------------------*//*:End Ignore*/

/***********************************************************************************************
	Include files
***********************************************************************************************/
#include "main.h"

#ifdef _MSC_VER
#pragma hdrstop
#endif
#undef THIS_FILE
DEFINE_THIS_FILE

/***********************************************************************************************
	Forward declarations
***********************************************************************************************/

/***********************************************************************************************
	Local Constants and static variables
***********************************************************************************************/

GrcErrorList g_errorList;

/***********************************************************************************************
	Methods
***********************************************************************************************/

/*----------------------------------------------------------------------------------------------
	Add an error or warning to the list.
----------------------------------------------------------------------------------------------*/
void GrcErrorList::AddItem(bool fFatal, int nID, GdlObject * pgdlObj, const GrpLineAndFile * plnf,
	std::string staMsg)
{
	GrpLineAndFile lnf(0, 0, "");
	if (plnf == NULL)
	{
		if (pgdlObj)
			lnf = pgdlObj->LineAndFile();
	}
	else
		lnf = *plnf;
	
	GrcErrorItem * perr = new GrcErrorItem(fFatal, nID, lnf, staMsg, pgdlObj);
	m_vperr.push_back(perr);

	if (fFatal)
		m_fFatalError = true;
}

void GrcErrorList::AddItem(bool fFatal, int nID, GdlObject * pgdlObj, const GrpLineAndFile * plnf,
	std::string * psta1, std::string * psta2, std::string * psta3, std::string * psta4,
	std::string * psta5, std::string * psta6, std::string * psta7, std::string * psta8)
{
	std::string staMsg;
	if (psta1)
		staMsg += *psta1;
	if (psta2)
		staMsg += *psta2;
	if (psta3)
		staMsg += *psta3;
	if (psta4)
		staMsg += *psta4;
	if (psta5)
		staMsg += *psta5;
	if (psta6)
		staMsg += *psta6;
	if (psta7)
		staMsg += *psta7;
	if (psta8)
		staMsg += *psta8;

	AddItem(fFatal, nID, pgdlObj, plnf, staMsg);
}

/*----------------------------------------------------------------------------------------------
	Sort the list of errors; remove duplicates.
----------------------------------------------------------------------------------------------*/
void GrcErrorList::SortErrors()
{
	std::vector<GrcErrorItem *> vperrSorted;

	GrcErrorItem * perrLast = NULL;

	while (m_vperr.size() > 0)
	{
		int iperrNext = 0;
		for (size_t iperrT = 1; iperrT < m_vperr.size(); iperrT++)
		{
			GrpLineAndFile * plnfNext = &(m_vperr[iperrNext]->m_lnf);
			///int nLineNext = m_vperr[iperrNext]->m_nLineNumber;
			std::string staNext = m_vperr[iperrNext]->m_staMsg;

			GrpLineAndFile * plnfT = &(m_vperr[iperrT]->m_lnf);
			///int nLineT = m_vperr[iperrT]->m_nLineNumber;
			std::string staT = m_vperr[iperrT]->m_staMsg;

			if (*plnfT < *plnfNext)
				iperrNext = iperrT;
			//	NB: The main purpose in sorting errors alphabetically is to get identical
			//	errors next to each other, so duplicates can be deleted.
			else if (*plnfT == *plnfNext && strcmp(staT.c_str(), staNext.c_str()) < 0)
				iperrNext = iperrT;
		}

		if (perrLast && perrLast->Equivalent(m_vperr[iperrNext]))
		{
			//	Leave out
			delete m_vperr[iperrNext];
		}
		else
		{
			vperrSorted.push_back(m_vperr[iperrNext]);
			perrLast = m_vperr[iperrNext];
		}
		m_vperr.erase(m_vperr.begin() + iperrNext);
	}

	m_vperr.clear();
	m_vperr.assign(vperrSorted.begin(), vperrSorted.end());
}


/*----------------------------------------------------------------------------------------------
	Return the number of errors for the given line, along with the index of the first one.
	Assumes the list is sorted.
----------------------------------------------------------------------------------------------*/
int GrcErrorList::ErrorsAtLine(int nLine)
{
	int iBogus;
	return ErrorsAtLine(nLine, &iBogus);
}

int GrcErrorList::ErrorsAtLine(int nLine, int * piperrFirst)
{
	int iperrFirst = 0;
	while (iperrFirst < signed(m_vperr.size()) && m_vperr[iperrFirst]->PreProcessedLine() < nLine)
		iperrFirst++;

	if (iperrFirst >= signed(m_vperr.size()) || nLine < m_vperr[iperrFirst]->PreProcessedLine())
	{
		*piperrFirst = -1;
		return 0;
	}

	int cperr = 1;
	while (iperrFirst + cperr < signed(m_vperr.size()) &&
		m_vperr[iperrFirst + cperr]->PreProcessedLine() == nLine)
	{
		cperr++;
	}

	*piperrFirst = iperrFirst;
	return cperr;
}

/*----------------------------------------------------------------------------------------------
	Write the errors to a text file.
----------------------------------------------------------------------------------------------*/
void GrcErrorList::WriteErrorsToFile(std::string staFileName,
	std::string staGdlFile, std::string staInputFontFile,
	std::string staOutputFile, std::string staOutputFamily, std::string staVersion,
	bool fSepCtrlFile)
{
	std::ofstream strmOut;
	strmOut.open(staFileName.c_str());
	if (strmOut.fail())
	{
		g_errorList.AddError(106, NULL,
			"Error in writing to file ",
			std::string(staFileName));
		return;
	}

	WriteErrorsToStream(strmOut, staGdlFile, staInputFontFile, staOutputFile, staOutputFamily,
		staVersion, fSepCtrlFile);
	strmOut.close();
}

/*----------------------------------------------------------------------------------------------
	Write the errors to an output stream.
----------------------------------------------------------------------------------------------*/
void GrcErrorList::WriteErrorsToStream(std::ostream& strmOut,
	std::string staGdlFile, std::string staInputFontFile,
	std::string staOutputFile, std::string staOutputFamily, std::string staVersion, bool fSepCtrlFile)
{
	strmOut << "Graphite Compilation Results\n\n";
	strmOut << "GDL file: " << staGdlFile << "\n";
	strmOut << "Input font file: " << staInputFontFile << "\n";
	strmOut << "Output font file: " << staOutputFile << "\n";
	strmOut << "Output font family: " << staOutputFamily << "\n";
	strmOut << "Silf table version: " << staVersion << "\n";
	strmOut << "Create separate control file: " << (fSepCtrlFile ? "yes" : "no") << "\n";
	strmOut << "\n*******************************************************\n\n";

	int cError = 0;
	int cWarning = 0;
	int cWarningIgnored = 0;
	for (size_t iperr = 0; iperr < m_vperr.size(); iperr++)
	{
		GrcErrorItem * perr = m_vperr[iperr];

		if (!perr->m_fFatal && IgnoreWarning(perr->m_nID))
		{
			cWarningIgnored++;
			continue;	// ignore this warning
		}

		if (perr->m_lnf.File() != "" ||
			(perr->m_lnf.OriginalLine() != 0 &&
				perr->m_lnf.OriginalLine() != kMaxFileLineNumber))
		{
			strmOut << perr->m_lnf.File() << "(" << perr->m_lnf.OriginalLine() << ") : ";
		}

		if (!perr->m_fMsgIncludesFatality)
		{
			strmOut << ((perr->m_fFatal) ? "error(" : "warning(") << perr->m_nID << "): ";
		}
		(perr->m_fFatal) ? cError++ : cWarning++;

		strmOut << perr->m_staMsg.data() << "\n";
	}

	if (m_vperr.size() > 0)
		strmOut << "\n*******************************************************\n\n";
	if (AnyFatalErrors())
	{
		strmOut << "Compilation failed";
	}
	else
	{
		WriteTableVersionsGenerated(strmOut);
		strmOut << "Compilation succeeded";
	}
	strmOut << " - " << cError << " error" << ((cError == 1) ? ", " : "s, ")
		<< cWarning << " warning" << ((cWarning == 1) ? "" : "s");
	if (cWarningIgnored > 0)
		strmOut << " (" << cWarningIgnored << " warning" << ((cWarningIgnored == 1) ? "" : "s")
			<< " ignored)";
}

/*----------------------------------------------------------------------------------------------
	Output the table version numbers to the error file.
----------------------------------------------------------------------------------------------*/
void GrcErrorList::WriteTableVersionsGenerated(std::ostream& strmOut)
{
	strmOut << "Table versions generated:\n";

	int fxd = g_cman.TableVersion(ktiSilf);
	strmOut << "  Silf: " << VersionString(fxd) << "\n";
	fxd = g_cman.TableVersion(ktiGloc);
	strmOut << "  Gloc: " << VersionString(fxd) << "\n";
	fxd = g_cman.TableVersion(ktiGlat);
	strmOut << "  Glat: " << VersionString(fxd) << "\n";
	fxd = g_cman.TableVersion(ktiFeat);
	strmOut << "  Feat: " << VersionString(fxd) << "\n";
	fxd = g_cman.TableVersion(ktiSill);
	strmOut << "  Sill: " << VersionString(fxd) << "\n\n";

	fxd = g_cman.CompilerVersion();
	strmOut << "Minimal compiler version required: " << VersionString(fxd) << "\n";

	strmOut << "\n*******************************************************\n\n";
}

/*----------------------------------------------------------------------------------------------
	Return the number of non-fatal errors.
----------------------------------------------------------------------------------------------*/
int GrcErrorList::NumberOfWarnings()
{
	int cerrRet = 0;
	for (size_t ierr = 0; ierr < m_vperr.size(); ierr++)
	{
		if (!IsFatal(ierr))
			cerrRet++;
	}
	return cerrRet;
}


/*----------------------------------------------------------------------------------------------
	Return the number of non-fatal errors that were not ignored.
----------------------------------------------------------------------------------------------*/
int GrcErrorList::NumberOfWarningsGiven()
{
	int cerrRet = 0;
	for (size_t ierr = 0; ierr < m_vperr.size(); ierr++)
	{
		if (!IsFatal(ierr) && !IgnoreWarning(m_vperr[ierr]->m_nID))
			cerrRet++;
	}
	return cerrRet;
}


/*----------------------------------------------------------------------------------------------
	Store or remove an indication that the given warning should be ignored in the output.
----------------------------------------------------------------------------------------------*/
void GrcErrorList::SetIgnoreWarning(int nWarning, bool f)
{
	int iFound = -1;
	for (size_t i = 0; i < m_vnIgnoreWarnings.size(); i++)
	{
		if (m_vnIgnoreWarnings[i] == nWarning)
		{
			iFound = i;
			break;
		}
	}
	if (f)
	{
		if (iFound == -1)
			m_vnIgnoreWarnings.push_back(nWarning);
	}
	else
	{
		if (iFound > -1)
			m_vnIgnoreWarnings.erase(m_vnIgnoreWarnings.begin() + iFound);
	}
}

/*----------------------------------------------------------------------------------------------
	Remove all indications of any warnings that should be ignored in the output.
----------------------------------------------------------------------------------------------*/
void GrcErrorList::ClearIgnoreWarnings()
{
	m_vnIgnoreWarnings.clear();
}


/*----------------------------------------------------------------------------------------------
	Store or remove an indication that the given warning should be ignored in the output.
----------------------------------------------------------------------------------------------*/
bool GrcErrorList::IgnoreWarning(int nWarning)
{
	for (size_t i = 0; i < m_vnIgnoreWarnings.size(); i++)
	{
		if (m_vnIgnoreWarnings[i] == nWarning)
			return true;
	}
	return false;
}

/*----------------------------------------------------------------------------------------------
	Global functions for use by the parser.
----------------------------------------------------------------------------------------------*/
void AddGlobalError(bool fFatal, int nID, std::string msg, int nLine)
{
	if (fFatal)
		g_errorList.AddError(nID, NULL, msg.c_str(), GrpLineAndFile(nLine, 0, ""));
	else
		g_errorList.AddWarning(nID, NULL, msg.c_str(), GrpLineAndFile(nLine, 0, ""));
}


void AddGlobalError(bool fFatal, int nID, std::string msg, GrpLineAndFile const& lnf)
{
	if (fFatal)
		g_errorList.AddError(nID, NULL, msg.c_str(), lnf);
	else
		g_errorList.AddWarning(nID, NULL, msg.c_str(), lnf);
}


/*----------------------------------------------------------------------------------------------
	Set the error file name to use the same path as the GDL file.
----------------------------------------------------------------------------------------------*/
void GrcErrorList::SetFileNameFromGdlFile(char * pchGdlFile)
{
	char * pchEnd = pchGdlFile + strlen(pchGdlFile);
	while (*pchEnd != '\\')
	{
		if (pchEnd <= pchGdlFile)
		{
			m_strErrFile = "gdlerr.txt";
			return;
		}
		pchEnd--;
	}
	m_strErrFile.assign(pchGdlFile, (pchEnd - pchGdlFile + 1));
	m_strErrFile.append("gdlerr.txt");
}