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