Include error number in ErrTab
[fw/sdcc] / support / Util / SDCCerr.c
1 /*
2  * SDCCerr - Standard error handler
3  *
4  *  This program is free software; you can redistribute it and/or modify
5  *  it under the terms of the GNU General Public License as published by
6  *  the Free Software Foundation; either version 2 of the License, or
7  *  (at your option) any later version.
8  *
9  *  This program is distributed in the hope that it will be useful,
10  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  *  GNU Library General Public License for more details.
13  *
14  *  You should have received a copy of the GNU General Public License
15  *  along with this program; if not, write to the Free Software
16  *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17  */
18
19 #include <stdio.h>
20
21 #include "SDCCerr.h"
22
23
24 #define USE_STDOUT_FOR_ERRORS           0
25
26 #if USE_STDOUT_FOR_ERRORS
27 #define DEFAULT_ERROR_OUT       stdout
28 #else
29 #define DEFAULT_ERROR_OUT       stderr
30 #endif
31
32 static struct {
33     ERROR_LOG_LEVEL logLevel;
34     FILE *out;
35 } _SDCCERRG;
36
37 #define    PEDANTIC 0
38 #define    WARNING  1
39 #define    ERROR    2
40
41 extern char *filename ;
42 extern int lineno ;
43 extern int fatalError ;
44
45 /* Currently the errIndex field must match the position of the 
46  * entry in the array. It is only included in order to make 
47  * human error lookup easier.
48  */
49 struct  
50 {
51     int         errIndex;
52     int         errType;
53     char        *errText;
54 } ErrTab [] =
55 {
56 { E_DUPLICATE, ERROR,
57    "error *** Duplicate symbol '%s', symbol IGNORED\n" },
58 { E_SYNTAX_ERROR, ERROR,
59    "error *** Syntax Error Declaration ignored\n" },
60 { E_CONST_EXPECTED, ERROR,
61    "error *** Constant Expected Found Variable\n" },
62 { E_OUT_OF_MEM, ERROR,
63    "error *** 'malloc' failed file '%s' for size %ld\n" },
64 { E_FILE_OPEN_ERR, ERROR,
65    "error *** 'fopen' failed on file '%s'\n" },
66 { E_INVALID_OCLASS, ERROR,
67    "error *** Internal Error Oclass invalid '%s'\n" },
68 { E_CANNOT_ALLOC, ERROR,
69    "error *** Cannot allocate variable '%s'.\n" },
70 { E_OLD_STYLE, ERROR,
71    "error *** Old style C declaration. IGNORED '%s'\n" },
72 { E_STACK_OUT, ERROR,
73    "error *** Out of stack Space. '%s' not allocated\n" },
74 { E_INTERNAL_ERROR, ERROR,
75    "error *** FATAL Compiler Internal Error in file '%s' line number '%d' : %s \nContact Author with source code\n" },
76 { E_LVALUE_REQUIRED, ERROR,
77    "error *** 'lvalue' required for '%s' operation .\n" },
78 { E_TMPFILE_FAILED, ERROR,
79    "error *** Creation of temp file failed\n" },
80 { E_FUNCTION_EXPECTED, ERROR,
81    "error *** Function expected here '%s'\n" },
82 { E_USING_ERROR, ERROR,
83    "error *** 'using', 'interrupt' or 'reentrant' must follow a function definiton .'%s'\n" },
84 { E_SFR_INIT, ERROR,
85    "error *** Absolute address & initial value both cannot be specified for\n a 'sfr','sbit' storage class, initial value ignored '%s'\n" },
86 { E_INIT_IGNORED, WARNING,
87    "warning *** Variable in the storage class cannot be initialized.'%s'\n" },
88 { E_AUTO_ASSUMED, WARNING,
89    "warning *** storage class not allowed for automatic variable '%s' in reentrant function\n" },
90 { E_AUTO_ABSA, ERROR,
91    "error *** absolute address not allowed for automatic var '%s' in reentrant function \n" },
92 { E_INIT_WRONG, WARNING,
93    "warning *** Initializer different levels of indirections\n" },
94 { E_FUNC_REDEF, ERROR,
95    "error *** Function name '%s' redefined \n" },
96 { E_ID_UNDEF, ERROR,
97    "error *** Undefined identifier '%s'\n" },
98 { W_STACK_OVERFLOW, WARNING,
99    "warning *** stack exceeds 256 bytes for function '%s'\n" },
100 { E_NEED_ARRAY_PTR, ERROR,
101    "error *** Array or pointer required for '%s' operation \n" },
102 { E_IDX_NOT_INT, ERROR,
103    "error *** Array index not an integer\n" },
104 { E_ARRAY_BOUND, ERROR,
105    "error *** Array bound Exceeded, assuming zero\n" },
106 { E_STRUCT_UNION, ERROR,
107    "error *** Structure/Union expected left of '.%s'\n" },
108 { E_NOT_MEMBER, ERROR,
109    "error *** '%s' not a structure/union member\n" },
110 { E_PTR_REQD, ERROR,
111    "error *** Pointer required\n" },
112 { E_UNARY_OP, ERROR,
113    "error *** 'unary %c': illegal operand\n" },
114 { E_CONV_ERR, ERROR,
115    "error *** convertion error: integral promotion failed\n" },
116 { E_INT_REQD, ERROR,
117    "error *** type must be INT for bit field definition\n" },
118 { E_BITFLD_SIZE, ERROR,
119    "error *** bit field size greater than 16 . assuming 16\n" },
120 { E_TRUNCATION, WARNING,
121    "warning *** high order truncation might occur\n" },
122 { E_CODE_WRITE, ERROR,
123    "error *** Attempt to assign value to a constant variable %s\n" },
124 { E_LVALUE_CONST, ERROR,
125    "error *** Lvalue specifies constant object\n" },
126 { E_ILLEGAL_ADDR, ERROR,
127    "error *** '&' illegal operand , %s\n" },
128 { E_CAST_ILLEGAL, ERROR,
129    "error *** illegal cast (cast cannot be aggregate)\n" },
130 { E_MULT_INTEGRAL, ERROR,
131    "error *** '*' bad operand\n" },
132 { E_ARG_ERROR, ERROR,
133    "error *** Argument count error, argument ignored\n" },
134 { E_ARG_COUNT, ERROR,
135    "error *** Function was expecting more arguments\n" },
136 { E_FUNC_EXPECTED, ERROR,
137    "error *** Function name expected '%s'.ANSI style declaration REQUIRED\n" },
138 { E_PLUS_INVALID, ERROR,
139    "error *** invalid operand '%s'\n" },
140 { E_PTR_PLUS_PTR, ERROR,
141    "error *** pointer + pointer invalid\n" },
142 { E_SHIFT_OP_INVALID, ERROR,
143    "error *** invalid operand for shift operator\n" },
144 { E_COMPARE_OP, ERROR,
145    "error *** compare operand cannot be struct/union\n" },
146 { E_BITWISE_OP, ERROR,
147    "error *** operand invalid for bitwise operation\n" },
148 { E_ANDOR_OP, ERROR,
149    "error *** Invalid operand for '&&' or '||'\n" },
150 { E_TYPE_MISMATCH, WARNING,
151    "warning *** indirections to different types %s %s \n" },
152 { E_AGGR_ASSIGN, ERROR,
153    "error *** cannot assign values to aggregates\n" },
154 { E_ARRAY_DIRECT, ERROR,
155    "error *** bit Arrays can be accessed by literal index only\n" },
156 { E_BIT_ARRAY, ERROR,
157    "error *** Array or Pointer to bit|sbit|sfr not allowed.'%s'\n" },
158 { E_DUPLICATE_TYPEDEF, ERROR,
159    "error *** typedef/enum '%s' duplicate.Previous definiton Ignored\n" },
160 { E_ARG_TYPE, ERROR,
161    "error *** Actual Argument type different from declaration %d\n" },
162 { E_RET_VALUE, ERROR,
163    "error *** Function return value mismatch\n" },
164 { E_FUNC_AGGR, ERROR,
165    "error *** Function cannot return aggregate. Func body ignored\n" },
166 { E_FUNC_DEF, ERROR,
167    "error *** ANSI Style declaration needed\n" },
168 { E_DUPLICATE_LABEL, ERROR,
169    "error *** Label name redefined '%s'\n" },
170 { E_LABEL_UNDEF, ERROR,
171    "error *** Label undefined '%s'\n" },
172 { E_FUNC_VOID, ERROR,
173    "error *** void function returning value\n" },
174 { E_VOID_FUNC, ERROR,
175    "error *** function '%s' must return value\n" },
176 { E_RETURN_MISMATCH, WARNING,
177    "warning *** function return value mismatch\n" },
178 { E_CASE_CONTEXT, ERROR,
179    "error *** 'case/default' found without 'switch'.statement ignored\n" },
180 { E_CASE_CONSTANT, ERROR,
181    "error *** 'case' expression not constant. statement ignored\n" },
182 { E_BREAK_CONTEXT, ERROR,
183    "error *** 'break/continue' statement out of context\n" },
184 { E_SWITCH_AGGR, ERROR,
185    "error *** nonintegral used in switch expression\n" },
186 { E_FUNC_BODY, ERROR,
187    "error *** function '%s' already has body\n" },
188 { E_UNKNOWN_SIZE, ERROR,
189    "error *** attempt to allocate variable of unknown size '%s'\n" },
190 { E_AUTO_AGGR_INIT, ERROR,
191    "error *** aggregate 'auto' variable '%s' cannot be initialized\n" },
192 { E_INIT_COUNT, ERROR,
193    "error *** too many initializers\n" },
194 { E_INIT_STRUCT, ERROR,
195    "error *** struct/union/array '%s' :initialization needs curly braces\n" },
196 { E_INIT_NON_ADDR, ERROR,
197    "error *** non-address initialization expression\n" },
198 { E_INT_DEFINED, ERROR,
199    "error *** interrupt no '%d' already has a service routine '%s'\n" },
200 { E_INT_ARGS, ERROR,
201    "error *** interrupt routine cannot have arguments, arguments ingored\n" },
202 { E_INCLUDE_MISSING, ERROR,
203    "error *** critical compiler #include file missing.          \n" },
204 { E_NO_MAIN, ERROR,
205    "error *** function 'main' undefined\n" },
206 { E_EXTERN_INIT, ERROR,
207    "error *** 'extern' variable '%s' cannot be initialised      \n" },
208 { E_PRE_PROC_FAILED, ERROR,
209    "error *** Pre-Processor %s\n" },
210 { E_DUP_FAILED, ERROR,
211    "error *** _dup call failed\n" },
212 { E_INCOMPAT_CAST, WARNING,
213    "warning *** pointer being cast to incompatible type \n" },
214 { E_LOOP_ELIMINATE, WARNING,
215    "warning *** 'while' loop with 'zero' constant.loop eliminated\n" },
216 { W_NO_SIDE_EFFECTS, WARNING,
217    "warning *** %s expression has NO side effects.expr eliminated\n" },
218 { E_CONST_TOO_LARGE, PEDANTIC,
219    "warning *** constant value '%s', out of range.\n" },
220 { W_BAD_COMPARE, WARNING,
221    "warning *** comparison will either, ALWAYs succeed or ALWAYs fail\n" },
222 { E_TERMINATING, ERROR,
223    "error *** Compiler Terminating , contact author with source\n" },
224 { W_LOCAL_NOINIT, WARNING,
225    "warning *** 'auto' variable '%s' may be used before initialization at %s(%d)\n" },
226 { W_NO_REFERENCE, WARNING,
227    "warning *** in function %s unreferenced %s : '%s'\n" },
228 { E_OP_UNKNOWN_SIZE, ERROR,
229    "error *** unknown size for operand\n" },
230 { W_LONG_UNSUPPORTED, WARNING,
231    "warning *** '%s' 'long' not supported , declared as 'int' .\n" },
232 { W_LITERAL_GENERIC, WARNING,
233    "warning *** LITERAL value being cast to '_generic' pointer\n" },
234 { E_SFR_ADDR_RANGE, ERROR,
235    "error *** %s '%s' address out of range\n" },
236 { E_BITVAR_STORAGE, ERROR,
237    "error *** storage class CANNOT be specified for bit variable '%s'\n" },
238 { W_EXTERN_MISMATCH, WARNING,
239    "warning *** extern definition for '%s' mismatches with declaration.\n" },
240 { E_NONRENT_ARGS, WARNING,
241    "warning *** Functions called via pointers must be 'reentrant' to take arguments\n" },
242 { W_DOUBLE_UNSUPPORTED, WARNING,
243    "warning *** type 'double' not supported assuming 'float'\n" },
244 { W_IF_NEVER_TRUE, WARNING,
245    "warning *** if-statement condition always false.if-statement not generated\n" },
246 { W_FUNC_NO_RETURN, WARNING,
247    "warning *** no 'return' statement found for function '%s'\n" },
248 { W_PRE_PROC_WARNING, WARNING,
249    "warning *** Pre-Processor %s\n" },
250 { W_STRUCT_AS_ARG, WARNING,
251    "warning *** structure '%s' passed as function argument changed to pointer\n" },
252 { E_PREV_DEF_CONFLICT, ERROR,
253    "error *** conflict with previous definition of '%s' for attribute '%s'\n" },
254 { E_CODE_NO_INIT, ERROR,
255    "error *** variable '%s' declared in code space must have initialiser\n" },
256 { E_OPS_INTEGRAL, ERROR,
257    "error *** operands not integral for assignment operation\n" },
258 { E_TOO_MANY_PARMS, ERROR,
259    "error *** too many parameters \n" },
260 { E_TOO_FEW_PARMS, ERROR,
261    "error *** too few parameters\n" },
262 { E_FUNC_NO_CODE, ERROR,
263    "error *** code not generated for '%s' due to previous errors\n" },
264 { E_TYPE_MISMATCH_PARM, WARNING,
265    "warning *** type mismatch for parameter number %d\n" },
266 { E_INVALID_FLOAT_CONST, ERROR,
267    "error *** invalid float constant '%s'\n" },
268 { E_INVALID_OP, ERROR,
269    "error *** invalid operand for '%s' operation\n" },
270 { E_SWITCH_NON_INTEGER, ERROR,
271    "error *** switch value not an integer\n" },
272 { E_CASE_NON_INTEGER, ERROR,
273    "error *** case label not an integer\n" },
274 { E_FUNC_TOO_LARGE, WARNING,
275    "warning *** function '%s' too large for global optimization\n" },
276 { W_CONTROL_FLOW, PEDANTIC,
277    "warning *** conditional flow changed by optimizer '%s(%d)':so said EVELYN the modified DOG\n" },
278 { W_PTR_TYPE_INVALID, WARNING,
279    "warning *** invalid type specifier for pointer type specifier ignored\n" },
280 { W_IMPLICIT_FUNC, WARNING,
281    "warning *** function '%s' implicit declaration\n" },
282 { E_CONTINUE, WARNING,
283    "warning *** %s" },
284 { W_TOOMANY_SPILS, WARNING,
285    "info *** %s extended by %d bytes for compiler temp(s) :in function  '%s': %s \n" },
286 { W_UNKNOWN_PRAGMA, WARNING,
287    "warning *** unknown or unsupported #pragma directive '%s'\n" },
288 { W_SHIFT_CHANGED, PEDANTIC,
289    "warning *** %s shifting more than size of object changed to zero\n" },
290 { W_UNKNOWN_OPTION, WARNING,
291    "warning *** unknown compiler option '%s' ignored\n" },
292 { W_UNSUPP_OPTION, WARNING,
293    "warning *** option '%s' no longer supported  '%s' \n" },
294 { W_UNKNOWN_FEXT, WARNING,
295    "warning *** don't know what to do with file '%s'. file extension unsupported\n" },
296 { W_TOO_MANY_SRC, WARNING,
297    "warning *** cannot compile more than one source file . file '%s' ignored\n" },
298 { I_CYCLOMATIC, WARNING,
299    "info *** function '%s', # edges %d , # nodes %d , cyclomatic complexity %d\n" },
300 { E_DIVIDE_BY_ZERO, ERROR,
301    "error *** dividing by ZERO\n" },
302 { E_FUNC_BIT, ERROR,
303    "error *** function cannot return 'bit'\n" },
304 { E_CAST_ZERO, ERROR,
305    "error *** casting from to type 'void' is illegal\n" },
306 { W_CONST_RANGE, WARNING,
307    "warning *** constant is out of range %s\n" },
308 { W_CODE_UNREACH, PEDANTIC,
309    "warning *** unreachable code %s(%d)\n" },
310 { W_NONPTR2_GENPTR, WARNING,
311    "warning *** non-pointer type cast to _generic pointer\n" },
312 { W_POSSBUG, WARNING,
313    "warning *** possible code generation error at line %d,\n send source to sandeep.dutta@usa.net\n" },
314 { W_PTR_ASSIGN, WARNING,
315    "warning *** pointer types incompatible \n" },
316 { W_UNKNOWN_MODEL, WARNING,
317    "warning *** unknown memory model at %s : %d\n" },
318 { E_UNKNOWN_TARGET, ERROR,
319    "error   *** cannot generate code for target '%s'\n" },
320 { W_INDIR_BANKED, WARNING,
321    "warning *** Indirect call to a banked function not implemented.\n" },
322 { W_UNSUPPORTED_MODEL, WARNING,
323    "warning *** Model '%s' not supported for %s, ignored.\n" },
324 { W_BANKED_WITH_NONBANKED, WARNING,
325    "warning *** Both banked and nonbanked attributes used.  nonbanked wins.\n" },
326 { W_BANKED_WITH_STATIC, WARNING,
327    "warning *** Both banked and static used.  static wins.\n" },
328 { W_INT_TO_GEN_PTR_CAST, WARNING,
329    "warning *** converting integer type to generic pointer: assuming XDATA\n" },
330 { W_ESC_SEQ_OOR_FOR_CHAR, WARNING,
331    "warning *** escape sequence out of range for char.\n" },
332 { E_INVALID_HEX, ERROR,
333    "error   *** \\x used with no following hex digits.\n" },
334 };
335 /*
336 -------------------------------------------------------------------------------
337 SetErrorOut - Set the error output file
338
339 -------------------------------------------------------------------------------
340 */
341
342 FILE *SetErrorOut(FILE *NewErrorOut)
343
344 {
345     _SDCCERRG.out = NewErrorOut ;
346
347 return NewErrorOut ;
348 }
349
350 void
351 setErrorLogLevel (ERROR_LOG_LEVEL level)
352 {
353     _SDCCERRG.logLevel = level;
354 }
355
356 /*
357 -------------------------------------------------------------------------------
358 vwerror - Output a standard eror message with variable number of arguements
359
360 -------------------------------------------------------------------------------
361 */
362
363 void vwerror (int errNum, va_list marker)
364 {
365     if (_SDCCERRG.out == NULL) {
366         _SDCCERRG.out = DEFAULT_ERROR_OUT;
367     }
368
369     if (ErrTab[errNum].errIndex != errNum)
370     {
371         fprintf(_SDCCERRG.out, 
372                 "*** Internal error: error table entry for %d inconsistent.", errNum);
373     }
374
375     if (ErrTab[errNum].errType >= _SDCCERRG.logLevel) {
376         if ( ErrTab[errNum].errType == ERROR )
377             fatalError++ ;
378   
379         if ( filename && lineno ) {
380             fprintf(_SDCCERRG.out, "%s(%d):",filename,lineno);
381         } else if (lineno) {
382             fprintf(_SDCCERRG.out, "at %d:", lineno);
383         }
384     
385         vfprintf(_SDCCERRG.out, ErrTab[errNum].errText,marker);
386     }
387     else {
388         // Below the logging level, drop.
389     }
390 }
391 /*
392 -------------------------------------------------------------------------------
393 werror - Output a standard eror message with variable number of arguements
394
395 -------------------------------------------------------------------------------
396 */
397
398 void werror (int errNum, ... )
399 {
400     va_list     marker;
401     va_start(marker,errNum);
402     vwerror(errNum, marker);
403     va_end( marker );
404 }
405