Synchronise with NetBSD: ANSIfy.
[dragonfly.git] / usr.bin / xlint / lint1 / err.c
1 /*      $NetBSD: err.c,v 1.8 1995/10/02 17:37:00 jpo Exp $      */
2
3 /*
4  * Copyright (c) 1994, 1995 Jochen Pohl
5  * All Rights Reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. All advertising materials mentioning features or use of this software
16  *    must display the following acknowledgement:
17  *      This product includes software developed by Jochen Pohl for
18  *      The NetBSD Project.
19  * 4. The name of the author may not be used to endorse or promote products
20  *    derived from this software without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
23  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
24  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
25  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
26  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
27  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
31  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  *
33  * $NetBSD: err.c,v 1.8 1995/10/02 17:37:00 jpo Exp $
34  * $DragonFly: src/usr.bin/xlint/lint1/err.c,v 1.6 2004/07/07 12:13:26 asmodai Exp $
35  */
36
37 /* number of errors found */
38 int     nerr;
39
40 /* number of syntax errors */
41 int     sytxerr;
42
43 #include <stdlib.h>
44 #include <stdarg.h>
45
46 #include "lint1.h"
47
48 static  const   char *basename(const char *);
49 static  void    verror(int, va_list);
50 static  void    vwarning(int, va_list);
51
52
53 const   char *msgs[] = {
54         "syntax error: empty declaration",                            /* 0 */
55         "old style declaration; add int",                             /* 1 */
56         "empty declaration",                                          /* 2 */
57         "%s declared in argument declaration list",                   /* 3 */
58         "illegal type combination",                                   /* 4 */
59         "modifying typedef with '%s'; only qualifiers allowed",       /* 5 */
60         "use 'double' instead of 'long float'",                       /* 6 */
61         "only one storage class allowed",                             /* 7 */
62         "illegal storage class",                                      /* 8 */
63         "only register valid as formal parameter storage class",      /* 9 */
64         "duplicate '%s'",                                             /* 10 */
65         "bit-field initializer out of range",                         /* 11 */
66         "compiler takes size of function",                            /* 12 */
67         "incomplete enum type: %s",                                   /* 13 */
68         "compiler takes alignment of function",                       /* 14 */
69         "function returns illegal type",                              /* 15 */
70         "array of function is illegal",                               /* 16 */
71         "null dimension",                                             /* 17 */
72         "illegal use of 'void'",                                      /* 18 */
73         "void type for %s",                                           /* 19 */
74         "zero or negative array dimension",                           /* 20 */
75         "redeclaration of formal parameter %s",                       /* 21 */
76         "incomplete or misplaced function definition",                /* 22 */
77         "undefined label %s",                                         /* 23 */
78         "cannot initialize function: %s",                             /* 24 */
79         "cannot initialize typedef: %s",                              /* 25 */
80         "cannot initialize extern declaration: %s",                   /* 26 */
81         "redeclaration of %s",                                        /* 27 */
82         "redefinition of %s",                                         /* 28 */
83         "previously declared extern, becomes static: %s",             /* 29 */
84         "redeclaration of %s; ANSI C requires static",                /* 30 */
85         "incomplete structure or union %s: %s",                       /* 31 */
86         "argument type defaults to 'int': %s",                        /* 32 */
87         "duplicate member name: %s",                                  /* 33 */
88         "nonportable bit-field type",                                 /* 34 */
89         "illegal bit-field type",                                     /* 35 */
90         "illegal bit-field size",                                     /* 36 */
91         "zero size bit-field",                                        /* 37 */
92         "function illegal in structure or union",                     /* 38 */
93         "illegal zero sized structure member: %s",                    /* 39 */
94         "unknown size: %s",                                           /* 40 */
95         "illegal use of bit-field",                                   /* 41 */
96         "forward reference to enum type",                             /* 42 */
97         "redefinition hides earlier one: %s",                         /* 43 */
98         "declaration introduces new type in ANSI C: %s %s",           /* 44 */
99         "base type is really '%s %s'",                                /* 45 */
100         "(%s) tag redeclared",                                        /* 46 */
101         "zero sized %s",                                              /* 47 */
102         "overflow in enumeration values: %s",                         /* 48 */
103         "struct or union member must be named",                       /* 49 */
104         "a function is declared as an argument: %s",                  /* 50 */
105         "parameter mismatch: %d declared, %d defined",                /* 51 */
106         "cannot initialize parameter: %s",                            /* 52 */
107         "declared argument %s is missing",                            /* 53 */
108         "trailing ',' prohibited in enum declaration",                /* 54 */
109         "integral constant expression expected",                      /* 55 */
110         "integral constant too large",                                /* 56 */
111         "enumeration constant hides parameter: %s",                   /* 57 */
112         "type does not match prototype: %s",                          /* 58 */
113         "formal parameter lacks name: param #%d",                     /* 59 */
114         "void must be sole parameter",                                /* 60 */
115         "void parameter cannot have name: %s",                        /* 61 */
116         "function prototype parameters must have types",              /* 62 */
117         "prototype does not match old-style definition",              /* 63 */
118         "()-less function definition",                                /* 64 */
119         "%s has no named members",                                    /* 65 */
120         "syntax requires ';' after last struct/union member",         /* 66 */
121         "cannot return incomplete type",                              /* 67 */
122         "typedef already qualified with '%s'",                        /* 68 */
123         "inappropriate qualifiers with 'void'",                       /* 69 */
124         "%soperand of '%s' is unsigned in ANSI C",                    /* 70 */
125         "too many characters in character constant",                  /* 71 */
126         "typedef declares no type name",                              /* 72 */
127         "empty character constant",                                   /* 73 */
128         "no hex digits follow \\x",                                   /* 74 */
129         "overflow in hex escape",                                     /* 75 */
130         "character escape does not fit in character",                 /* 76 */
131         "bad octal digit %c",                                         /* 77 */
132         "nonportable character escape",                               /* 78 */
133         "dubious escape \\%c",                                        /* 79 */
134         "dubious escape \\%o",                                        /* 80 */
135         "\\a undefined in traditional C",                             /* 81 */
136         "\\x undefined in traditional C",                             /* 82 */
137         "storage class after type is obsolescent",                    /* 83 */
138         "ANSI C requires formal parameter before '...'",              /* 84 */
139         "dubious tag declaration: %s %s",                             /* 85 */
140         "automatic hides external declaration: %s",                   /* 86 */
141         "static hides external declaration: %s",                      /* 87 */
142         "typedef hides external declaration: %s",                     /* 88 */
143         "typedef redeclared: %s",                                     /* 89 */
144         "inconsistent redeclaration of extern: %s",                   /* 90 */
145         "declaration hides parameter: %s",                            /* 91 */
146         "inconsistent redeclaration of static: %s",                   /* 92 */
147         "dubious static function at block level: %s",                 /* 93 */
148         "function has illegal storage class: %s",                     /* 94 */
149         "declaration hides earlier one: %s",                          /* 95 */
150         "cannot dereference non-pointer type",                        /* 96 */
151         "suffix U is illegal in traditional C",                       /* 97 */
152         "suffixes F and L are illegal in traditional C",              /* 98 */
153         "%s undefined",                                               /* 99 */
154         "unary + is illegal in traditional C",                        /* 100 */
155         "undefined struct/union member: %s",                          /* 101 */
156         "illegal member use: %s",                                     /* 102 */
157         "left operand of '.' must be struct/union object",            /* 103 */
158         "left operand of '->' must be pointer to struct/union",       /* 104 */
159         "non-unique member requires struct/union %s",                 /* 105 */
160         "left operand of '->' must be pointer",                       /* 106 */
161         "operands of '%s' have incompatible types",                   /* 107 */
162         "operand of '%s' has incompatible type",                      /* 108 */
163         "void type illegal in expression",                            /* 109 */
164         "pointer to function is not allowed here",                    /* 110 */
165         "unacceptable operand of '%s'",                               /* 111 */
166         "cannot take address of bit-field",                           /* 112 */
167         "cannot take address of register %s",                         /* 113 */
168         "%soperand of '%s' must be lvalue",                           /* 114 */
169         "%soperand of '%s' must be modifiable lvalue",                /* 115 */
170         "illegal pointer subtraction",                                /* 116 */
171         "bitwise operation on signed value possibly nonportable",     /* 117 */
172         "semantics of '%s' change in ANSI C; use explicit cast",      /* 118 */
173         "conversion of '%s' to '%s' is out of range",                 /* 119 */
174         "bitwise operation on signed value nonportable",              /* 120 */
175         "negative shift",                                             /* 121 */
176         "shift greater than size of object",                          /* 122 */
177         "illegal combination of pointer and integer, op %s",          /* 123 */
178         "illegal pointer combination, op %s",                         /* 124 */
179         "ANSI C forbids ordered comparisons of pointers to functions",/* 125 */
180         "incompatible types in conditional",                          /* 126 */
181         "'&' before array or function: ignored",                      /* 127 */
182         "operands have incompatible pointer types, op %s",            /* 128 */
183         "expression has null effect",                                 /* 129 */
184         "enum type mismatch, op %s",                                  /* 130 */
185         "conversion to '%s' may sign-extend incorrectly",             /* 131 */
186         "conversion from '%s' may lose accuracy",                     /* 132 */
187         "conversion of pointer to '%s' loses bits",                   /* 133 */
188         "conversion of pointer to '%s' may lose bits",                /* 134 */
189         "possible pointer alignment problem",                         /* 135 */
190         "cannot do pointer arithmetic on operand of unknown size",    /* 136 */
191         "use of incomplete enum type, op %s",                         /* 137 */
192         "unknown operand size, op %s",                                /* 138 */
193         "division by 0",                                              /* 139 */
194         "modulus by 0",                                               /* 140 */
195         "integer overflow detected, op %s",                           /* 141 */
196         "floating point overflow detected, op %s",                    /* 142 */
197         "cannot take size of incomplete type",                        /* 143 */
198         "cannot take size of function",                               /* 144 */
199         "cannot take size of bit-field",                              /* 145 */
200         "cannot take size of void",                                   /* 146 */
201         "invalid cast expression",                                    /* 147 */
202         "improper cast of void expression",                           /* 148 */
203         "illegal function",                                           /* 149 */
204         "argument mismatch: %d arg%s passed, %d expected",            /* 150 */
205         "void expressions may not be arguments, arg #%d",             /* 151 */
206         "argument cannot have unknown size, arg #%d",                 /* 152 */
207         "argument has incompatible pointer type, arg #%d",            /* 153 */
208         "illegal combination of pointer and integer, arg #%d",        /* 154 */
209         "argument is incompatible with prototype, arg #%d",           /* 155 */
210         "enum type mismatch, arg #%d",                                /* 156 */
211         "ANSI C treats constant as unsigned",                         /* 157 */
212         "%s may be used before set",                                  /* 158 */
213         "assignment in conditional context",                          /* 159 */
214         "operator '==' found where '=' was expected",                 /* 160 */
215         "constant in conditional context",                            /* 161 */
216         "comparision of %s with %s, op %s",                           /* 162 */
217         "a cast does not yield an lvalue",                            /* 163 */
218         "assignment of negative constant to unsigned type",           /* 164 */
219         "constant truncated by assignment",                           /* 165 */
220         "precision lost in bit-field assignment",                     /* 166 */
221         "array subscript cannot be negative: %ld",                    /* 167 */
222         "array subscript cannot be > %d: %ld",                        /* 168 */
223         "precedence confusion possible: parenthesize!",               /* 169 */
224         "first operand must have scalar type, op ? :",                /* 170 */
225         "assignment type mismatch",                                   /* 171 */
226         "too many struct/union initializers",                         /* 172 */
227         "too many array initializers",                                /* 173 */
228         "too many initializers",                                      /* 174 */
229         "initialisation of an incomplete type",                       /* 175 */
230         "invalid initializer",                                        /* 176 */
231         "non-constant initializer",                                   /* 177 */
232         "initializer does not fit",                                   /* 178 */
233         "cannot initialize struct/union with no named member",        /* 179 */
234         "bit-field initializer does not fit",                         /* 180 */
235         "{}-enclosed initializer required",                           /* 181 */
236         "incompatible pointer types",                                 /* 182 */
237         "illegal combination of pointer and integer",                 /* 183 */
238         "illegal pointer combination",                                /* 184 */
239         "initialisation type mismatch",                               /* 185 */
240         "bit-field initialisation is illegal in traditional C",       /* 186 */
241         "non-null byte ignored in string initializer",                /* 187 */
242         "no automatic aggregate initialization in traditional C",     /* 188 */
243         "assignment of struct/union illegal in traditional C",        /* 189 */
244         "empty array declaration: %s",                                /* 190 */
245         "%s set but not used in function %s",                         /* 191 */
246         "%s unused in function %s",                                   /* 192 */
247         "statement not reached",                                      /* 193 */
248         "label %s redefined",                                         /* 194 */
249         "case not in switch",                                         /* 195 */
250         "case label affected by conversion",                          /* 196 */
251         "non-constant case expression",                               /* 197 */
252         "non-integral case expression",                               /* 198 */
253         "duplicate case in switch: %ld",                              /* 199 */
254         "duplicate case in switch: %lu",                              /* 200 */
255         "default outside switch",                                     /* 201 */
256         "duplicate default in switch",                                /* 202 */
257         "case label must be of type `int' in traditional C",          /* 203 */
258         "controlling expressions must have scalar type",              /* 204 */
259         "switch expression must have integral type",                  /* 205 */
260         "enumeration value(s) not handled in switch",                 /* 206 */
261         "loop not entered at top",                                    /* 207 */
262         "break outside loop or switch",                               /* 208 */
263         "continue outside loop",                                      /* 209 */
264         "enum type mismatch in initialisation",                       /* 210 */
265         "return value type mismatch",                                 /* 211 */
266         "cannot return incomplete type",                              /* 212 */
267         "void function %s cannot return value",                       /* 213 */
268         "function %s expects to return value",                        /* 214 */
269         "function implicitly declared to return int",                 /* 215 */
270         "function %s has return (e); and return;",                    /* 216 */
271         "function %s falls off bottom without returning value",       /* 217 */
272         "ANSI C treats constant as unsigned, op %s",                  /* 218 */
273         "concatenated strings are illegal in traditional C",          /* 219 */
274         "fallthrough on case statement",                              /* 220 */
275         "initialisation of unsigned with negative constant",          /* 221 */
276         "conversion of negative constant to unsigned type",           /* 222 */
277         "end-of-loop code not reached",                               /* 223 */
278         "cannot recover from previous errors",                        /* 224 */
279         "static function called but not defined: %s()",               /* 225 */
280         "static variable %s unused",                                  /* 226 */
281         "const object %s should have initializer",                    /* 227 */
282         "function cannot return const or volatile object",            /* 228 */
283         "questionable conversion of function pointer",                /* 229 */
284         "nonportable character comparision, op %s",                   /* 230 */
285         "argument %s unused in function %s",                          /* 231 */
286         "label %s unused in function %s",                             /* 232 */
287         "struct %s never defined",                                    /* 233 */
288         "union %s never defined",                                     /* 234 */
289         "enum %s never defined",                                      /* 235 */
290         "static function %s unused",                                  /* 236 */
291         "redeclaration of formal parameter %s",                       /* 237 */
292         "initialisation of union is illegal in traditional C",        /* 238 */
293         "constant argument to NOT",                                   /* 239 */
294         "assignment of different structures",                         /* 240 */
295         "dubious operation on enum, op %s",                           /* 241 */
296         "combination of '%s' and '%s', op %s",                        /* 242 */
297         "dubious comparision of enums, op %s",                        /* 243 */
298         "illegal structure pointer combination",                      /* 244 */
299         "illegal structure pointer combination, op %s",               /* 245 */
300         "dubious conversion of enum to '%s'",                         /* 246 */
301         "pointer casts may be troublesome",                           /* 247 */
302         "floating-point constant out of range",                       /* 248 */
303         "syntax error",                                               /* 249 */
304         "unknown character \\%o",                                     /* 250 */
305         "malformed integer constant",                                 /* 251 */
306         "integer constant out of range",                              /* 252 */
307         "unterminated character constant",                            /* 253 */
308         "newline in string or char constant",                         /* 254 */
309         "undefined or invalid # directive",                           /* 255 */
310         "unterminated comment",                                       /* 256 */
311         "extra characters in lint comment",                           /* 257 */
312         "unterminated string constant",                               /* 258 */
313         "conversion to '%s' due to prototype, arg #%d",               /* 259 */
314         "previous declaration of %s",                                 /* 260 */
315         "previous definition of %s",                                  /* 261 */
316         "\\\" inside character constants undefined in traditional C", /* 262 */
317         "\\? undefined in traditional C",                             /* 263 */
318         "\\v undefined in traditional C",                             /* 264 */
319         "%s C does not support 'long long'",                          /* 265 */
320         "'long double' is illegal in traditional C",                  /* 266 */
321         "shift equal to size of object",                              /* 267 */
322         "variable declared inline: %s",                               /* 268 */
323         "argument declared inline: %s",                               /* 269 */
324         "function prototypes are illegal in traditional C",           /* 270 */
325         "switch expression must be of type `int' in traditional C",   /* 271 */
326         "empty translation unit",                                     /* 272 */
327         "bit-field type '%s' invalid in ANSI C",                      /* 273 */
328         "ANSI C forbids comparision of %s with %s",                   /* 274 */
329         "cast discards 'const' from pointer target type",             /* 275 */
330         "",                                                           /* 276 */
331         "initialisation of '%s' with '%s'",                           /* 277 */
332         "combination of '%s' and '%s', arg #%d",                      /* 278 */
333         "combination of '%s' and '%s' in return",                     /* 279 */
334         "must be outside function: /* %s */",                         /* 280 */
335         "duplicate use of /* %s */",                                  /* 281 */
336         "must precede function definition: /* %s */",                 /* 282 */
337         "argument number mismatch with directive: /* %s */",          /* 283 */
338         "fallthrough on default statement",                           /* 284 */
339         "prototype declaration",                                      /* 285 */
340         "function definition is not a prototype",                     /* 286 */
341         "function declaration is not a prototype",                    /* 287 */
342         "dubious use of /* VARARGS */ with /* %s */",                 /* 288 */
343         "can't be used together: /* PRINTFLIKE */ /* SCANFLIKE */",   /* 289 */
344         "static function %s declared but not defined",                /* 290 */
345         "invalid multibyte character",                                /* 291 */
346         "cannot concatenate wide and regular string literals",        /* 292 */
347         "argument %d must be 'char *' for PRINTFLIKE/SCANFLIKE",      /* 293 */
348         "multi-character character constant",                         /* 294 */
349         "conversion of '%s' to '%s' is out of range, arg #%d",        /* 295 */
350         "conversion of negative constant to unsigned type, arg #%d",  /* 296 */
351         "conversion to '%s' may sign-extend incorrectly, arg #%d",    /* 297 */
352         "conversion from '%s' may lose accuracy, arg #%d",            /* 298 */
353         "prototype does not match old style definition, arg #%d",     /* 299 */
354         "old style definition",                                       /* 300 */
355         "array of incomplete type",                                   /* 301 */
356         "%s returns pointer to automatic object",                     /* 302 */
357         "ANSI C forbids conversion of %s to %s",                      /* 303 */
358         "ANSI C forbids conversion of %s to %s, arg #%d",             /* 304 */
359         "ANSI C forbids conversion of %s to %s, op %s",               /* 305 */
360         "constant truncated by conversion, op %s",                    /* 306 */
361         "static variable %s set but not used",                        /* 307 */
362         "",                                                           /* 308 */
363         "extra bits set to 0 in conversion of '%s' to '%s', op %s",   /* 309 */
364 };
365
366 /*
367  * If Fflag is not set basename() returns a pointer to the last
368  * component of the path, otherwise it returns the argument.
369  */
370 static const char *
371 basename(const char *path)
372 {
373         const   char *cp, *cp1, *cp2;
374
375         if (Fflag)
376                 return (path);
377
378         cp = cp1 = cp2 = path;
379         while (*cp != '\0') {
380                 if (*cp++ == '/') {
381                         cp2 = cp1;
382                         cp1 = cp;
383                 }
384         }
385         return (*cp1 == '\0' ? cp2 : cp1);
386 }
387
388 static void
389 verror(int n, va_list ap)
390 {
391         const   char *fn;
392
393         fn = basename(curr_pos.p_file);
394         (void)printf("%s:%d: ", fn, curr_pos.p_line);
395         (void)vprintf(msgs[n], ap);
396         (void)printf("\n");
397         nerr++;
398 }
399
400 static void
401 vwarning(int n, va_list ap)
402 {
403         const   char *fn;
404
405         if (nowarn)
406                 /* this warning is suppressed by a LINTED comment */
407                 return;
408
409         fn = basename(curr_pos.p_file);
410         (void)printf("%s:%d: warning: ", fn, curr_pos.p_line);
411         (void)vprintf(msgs[n], ap);
412         (void)printf("\n");
413 }
414
415 void
416 error(int n, ...)
417 {
418         va_list ap;
419
420         va_start(ap, n);
421         verror(n, ap);
422         va_end(ap);
423 }
424
425 void
426 lerror(const char *msg, ...)
427 {
428         va_list ap;
429         const   char *fn;
430
431         va_start(ap, msg);
432         fn = basename(curr_pos.p_file);
433         (void)fprintf(stderr, "%s:%d: lint error: ", fn, curr_pos.p_line);
434         (void)vfprintf(stderr, msg, ap);
435         (void)fprintf(stderr, "\n");
436         va_end(ap);
437         exit(1);
438 }
439
440 void
441 warning(int n, ...)
442 {
443         va_list ap;
444
445         va_start(ap, n);
446         vwarning(n, ap);
447         va_end(ap);
448 }
449
450 void
451 message(int n, ...)
452 {
453         va_list ap;
454         const   char *fn;
455
456         va_start(ap, n);
457         fn = basename(curr_pos.p_file);
458         (void)printf("%s:%d: ", fn, curr_pos.p_line);
459         (void)vprintf(msgs[n], ap);
460         (void)printf("\n");
461         va_end(ap);
462 }
463
464 int
465 gnuism(int n, ...)
466 {
467         va_list ap;
468         int     msg;
469
470         va_start(ap, n);
471         if (sflag && !gflag) {
472                 verror(n, ap);
473                 msg = 1;
474         } else if (!sflag && gflag) {
475                 msg = 0;
476         } else {
477                 vwarning(n, ap);
478                 msg = 1;
479         }
480         va_end(ap);
481
482         return (msg);
483 }