err.c revision 1.170 1 /* $NetBSD: err.c,v 1.170 2022/06/15 18:06:51 rillig 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
34 #if HAVE_NBTOOL_CONFIG_H
35 #include "nbtool_config.h"
36 #endif
37
38 #include <sys/cdefs.h>
39 #if defined(__RCSID)
40 __RCSID("$NetBSD: err.c,v 1.170 2022/06/15 18:06:51 rillig Exp $");
41 #endif
42
43 #include <sys/types.h>
44 #include <stdarg.h>
45 #include <stdlib.h>
46
47 #include "lint1.h"
48
49 /* number of errors found */
50 int nerr;
51
52 /* number of syntax errors */
53 int sytxerr;
54
55
56 static const char *const msgs[] = {
57 "empty declaration", /* 0 */
58 "old style declaration; add 'int'", /* 1 */
59 "empty declaration", /* 2 */
60 "'%s' declared in argument declaration list", /* 3 */
61 "illegal type combination", /* 4 */
62 "modifying typedef with '%s'; only qualifiers allowed", /* 5 */
63 "use 'double' instead of 'long float'", /* 6 */
64 "only one storage class allowed", /* 7 */
65 "illegal storage class", /* 8 */
66 "only register valid as formal parameter storage class", /* 9 */
67 "duplicate '%s'", /* 10 */
68 "bit-field initializer out of range", /* 11 */
69 "compiler takes size of function", /* 12 */
70 "incomplete enum type '%s'", /* 13 */
71 "", /* 14 */
72 "function returns illegal type '%s'", /* 15 */
73 "array of function is illegal", /* 16 */
74 "null dimension", /* 17 */
75 "illegal use of 'void'", /* 18 */
76 "void type for '%s'", /* 19 */
77 "negative array dimension (%d)", /* 20 */
78 "redeclaration of formal parameter '%s'", /* 21 */
79 "incomplete or misplaced function definition", /* 22 */
80 "undefined label '%s'", /* 23 */
81 "cannot initialize function: %s", /* 24 */
82 "cannot initialize typedef: %s", /* 25 */
83 "cannot initialize extern declaration: %s", /* 26 */
84 "redeclaration of '%s'", /* 27 */
85 "redefinition of %s", /* 28 */
86 "previously declared extern, becomes static: %s", /* 29 */
87 "redeclaration of '%s'; ANSI C requires static", /* 30 */
88 "'%s' has incomplete type '%s'", /* 31 */
89 "argument type defaults to 'int': %s", /* 32 */
90 "duplicate member name: %s", /* 33 */
91 "nonportable bit-field type '%s'", /* 34 */
92 "illegal bit-field type '%s'", /* 35 */
93 "illegal bit-field size: %d", /* 36 */
94 "zero size bit-field", /* 37 */
95 "function illegal in structure or union", /* 38 */
96 "zero sized array in struct is a C99 extension: %s", /* 39 */
97 "", /* never used */ /* 40 */
98 "bit-field in union is very unusual", /* 41 */
99 "forward reference to enum type", /* 42 */
100 "redefinition hides earlier one: %s", /* 43 */
101 "declaration introduces new type in ANSI C: %s %s", /* 44 */
102 "base type is really '%s %s'", /* 45 */
103 "%s tag '%s' redeclared as %s", /* 46 */
104 "zero sized %s is a C99 feature", /* 47 */
105 "overflow in enumeration values: %s", /* 48 */
106 "anonymous struct/union members is a C11 feature", /* 49 */
107 "a function is declared as an argument: %s", /* 50 */
108 "parameter mismatch: %d declared, %d defined", /* 51 */
109 "cannot initialize parameter: %s", /* 52 */
110 "declared argument %s is missing", /* 53 */
111 "trailing ',' prohibited in enum declaration", /* 54 */
112 "integral constant expression expected", /* 55 */
113 "integral constant too large", /* 56 */
114 "enumeration constant hides parameter: %s", /* 57 */
115 "type does not match prototype: %s", /* 58 */
116 "formal parameter lacks name: param #%d", /* 59 */
117 "void must be sole parameter", /* 60 */
118 "void parameter cannot have name: %s", /* 61 */
119 "function prototype parameters must have types", /* 62 */
120 "prototype does not match old-style definition", /* 63 */
121 "()-less function definition", /* 64 */
122 "%s has no named members", /* 65 */
123 "", /* 66 */
124 "cannot return incomplete type", /* 67 */
125 "typedef already qualified with '%s'", /* 68 */
126 "inappropriate qualifiers with 'void'", /* 69 */
127 "", /* unused */ /* 70 */
128 "too many characters in character constant", /* 71 */
129 "typedef declares no type name", /* 72 */
130 "empty character constant", /* 73 */
131 "no hex digits follow \\x", /* 74 */
132 "overflow in hex escape", /* 75 */
133 "character escape does not fit in character", /* 76 */
134 "bad octal digit %c", /* 77 */
135 "", /* unused */ /* 78 */
136 "dubious escape \\%c", /* 79 */
137 "dubious escape \\%o", /* 80 */
138 "\\a undefined in traditional C", /* 81 */
139 "\\x undefined in traditional C", /* 82 */
140 "storage class after type is obsolescent", /* 83 */
141 "ANSI C requires formal parameter before '...'", /* 84 */
142 "dubious tag declaration: %s %s", /* 85 */
143 "automatic hides external declaration: %s", /* 86 */
144 "static hides external declaration: %s", /* 87 */
145 "typedef hides external declaration: %s", /* 88 */
146 "typedef redeclared: %s", /* 89 */
147 "inconsistent redeclaration of extern: %s", /* 90 */
148 "declaration hides parameter: %s", /* 91 */
149 "inconsistent redeclaration of static: %s", /* 92 */
150 "dubious static function at block level: %s", /* 93 */
151 "function has illegal storage class: %s", /* 94 */
152 "declaration hides earlier one: %s", /* 95 */
153 "cannot dereference non-pointer type", /* 96 */
154 "suffix U is illegal in traditional C", /* 97 */
155 "suffixes F and L are illegal in traditional C", /* 98 */
156 "'%s' undefined", /* 99 */
157 "unary + is illegal in traditional C", /* 100 */
158 "type '%s' does not have member '%s'", /* 101 */
159 "illegal member use: %s", /* 102 */
160 "left operand of '.' must be struct or union, not '%s'", /* 103 */
161 "left operand of '->' must be pointer to struct or union, not '%s'", /* 104 */
162 "non-unique member requires struct/union %s", /* 105 */
163 "left operand of '->' must be pointer", /* 106 */
164 "operands of '%s' have incompatible types (%s != %s)", /* 107 */
165 "operand of '%s' has invalid type (%s)", /* 108 */
166 "void type illegal in expression", /* 109 */
167 "pointer to function is not allowed here", /* 110 */
168 "unacceptable operand of '%s'", /* 111 */
169 "cannot take address of bit-field", /* 112 */
170 "cannot take address of register %s", /* 113 */
171 "%soperand of '%s' must be lvalue", /* 114 */
172 "%soperand of '%s' must be modifiable lvalue", /* 115 */
173 "illegal pointer subtraction", /* 116 */
174 "bitwise '%s' on signed value possibly nonportable", /* 117 */
175 "semantics of '%s' change in ANSI C; use explicit cast", /* 118 */
176 "conversion of '%s' to '%s' is out of range", /* 119 */
177 "bitwise '%s' on signed value nonportable", /* 120 */
178 "negative shift", /* 121 */
179 "shift amount %llu is greater than bit-size %llu of '%s'", /* 122 */
180 "illegal combination of %s '%s' and %s '%s', op '%s'", /* 123 */
181 "illegal combination of '%s' and '%s', op '%s'", /* 124 */
182 "ANSI C forbids ordered comparisons of pointers to functions",/* 125 */
183 "incompatible types '%s' and '%s' in conditional", /* 126 */
184 "'&' before array or function: ignored", /* 127 */
185 "operands have incompatible pointer types, op %s (%s != %s)", /* 128 */
186 "expression has null effect", /* 129 */
187 "enum type mismatch: '%s' '%s' '%s'", /* 130 */
188 "conversion to '%s' may sign-extend incorrectly", /* 131 */
189 "conversion from '%s' to '%s' may lose accuracy", /* 132 */
190 "conversion of pointer to '%s' loses bits", /* 133 */
191 "conversion of pointer to '%s' may lose bits", /* 134 */
192 "converting '%s' to '%s' may cause alignment problem", /* 135 */
193 "cannot do pointer arithmetic on operand of unknown size", /* 136 */
194 "use of incomplete enum type, op %s", /* 137 */
195 "unknown operand size, op %s", /* 138 */
196 "division by 0", /* 139 */
197 "modulus by 0", /* 140 */
198 "integer overflow detected, op '%s'", /* 141 */
199 "floating point overflow detected, op %s", /* 142 */
200 "cannot take size/alignment of incomplete type", /* 143 */
201 "cannot take size/alignment of function type '%s'", /* 144 */
202 "cannot take size/alignment of bit-field", /* 145 */
203 "cannot take size/alignment of void", /* 146 */
204 "invalid cast from '%s' to '%s'", /* 147 */
205 "improper cast of void expression", /* 148 */
206 "illegal function (type %s)", /* 149 */
207 "argument mismatch: %d arg%s passed, %d expected", /* 150 */
208 "void expressions may not be arguments, arg #%d", /* 151 */
209 "argument cannot have unknown size, arg #%d", /* 152 */
210 "converting '%s' to incompatible '%s' for argument %d", /* 153 */
211 "illegal combination of %s (%s) and %s (%s), arg #%d", /* 154 */
212 "passing '%s' to incompatible '%s', arg #%d", /* 155 */
213 "enum type mismatch, arg #%d (%s != %s)", /* 156 */
214 "ANSI C treats constant as unsigned", /* 157 */
215 "%s may be used before set", /* 158 */
216 "assignment in conditional context", /* 159 */
217 "operator '==' found where '=' was expected", /* 160 */
218 "constant in conditional context", /* 161 */
219 "comparison of %s with %s, op %s", /* 162 */
220 "a cast does not yield an lvalue", /* 163 */
221 "assignment of negative constant to unsigned type", /* 164 */
222 "constant truncated by assignment", /* 165 */
223 "precision lost in bit-field assignment", /* 166 */
224 "array subscript cannot be negative: %ld", /* 167 */
225 "array subscript cannot be > %d: %ld", /* 168 */
226 "precedence confusion possible: parenthesize!", /* 169 */
227 "first operand must have scalar type, op ? :", /* 170 */
228 "cannot assign to '%s' from '%s'", /* 171 */
229 "too many struct/union initializers", /* 172 */
230 "too many array initializers, expected %d", /* 173 */
231 "too many initializers", /* 174 */
232 "initialization of incomplete type '%s'", /* 175 */
233 "", /* no longer used */ /* 176 */
234 "non-constant initializer", /* 177 */
235 "initializer does not fit", /* 178 */
236 "cannot initialize struct/union with no named member", /* 179 */
237 "bit-field initializer does not fit", /* 180 */
238 "{}-enclosed initializer required", /* 181 */
239 "incompatible pointer types (%s != %s)", /* 182 */
240 "illegal combination of %s (%s) and %s (%s)", /* 183 */
241 "illegal combination of '%s' and '%s'", /* 184 */
242 "cannot initialize '%s' from '%s'", /* 185 */
243 "bit-field initialization is illegal in traditional C", /* 186 */
244 "string literal too long (%lu) for target array (%lu)", /* 187 */
245 "no automatic aggregate initialization in traditional C", /* 188 */
246 "", /* no longer used */ /* 189 */
247 "empty array declaration: %s", /* 190 */
248 "'%s' set but not used in function '%s'", /* 191 */
249 "'%s' unused in function '%s'", /* 192 */
250 "statement not reached", /* 193 */
251 "label %s redefined", /* 194 */
252 "case not in switch", /* 195 */
253 "case label affected by conversion", /* 196 */
254 "non-constant case expression", /* 197 */
255 "non-integral case expression", /* 198 */
256 "duplicate case in switch: %ld", /* 199 */
257 "duplicate case in switch: %lu", /* 200 */
258 "default outside switch", /* 201 */
259 "duplicate default in switch", /* 202 */
260 "case label must be of type 'int' in traditional C", /* 203 */
261 "controlling expressions must have scalar type", /* 204 */
262 "switch expression must have integral type", /* 205 */
263 "enumeration value(s) not handled in switch", /* 206 */
264 "loop not entered at top", /* 207 */
265 "break outside loop or switch", /* 208 */
266 "continue outside loop", /* 209 */
267 "enum type mismatch between '%s' and '%s' in initialization", /* 210 */
268 "return value type mismatch (%s) and (%s)", /* 211 */
269 "cannot return incomplete type", /* 212 */
270 "void function %s cannot return value", /* 213 */
271 "function '%s' expects to return value", /* 214 */
272 "function '%s' implicitly declared to return int", /* 215 */
273 "function %s has return (e); and return;", /* 216 */
274 "function %s falls off bottom without returning value", /* 217 */
275 "ANSI C treats constant as unsigned, op %s", /* 218 */
276 "concatenated strings are illegal in traditional C", /* 219 */
277 "fallthrough on case statement", /* 220 */
278 "initialization of unsigned with negative constant", /* 221 */
279 "conversion of negative constant to unsigned type", /* 222 */
280 "end-of-loop code not reached", /* 223 */
281 "cannot recover from previous errors", /* 224 */
282 "static function called but not defined: %s()", /* 225 */
283 "static variable '%s' unused", /* 226 */
284 "const object %s should have initializer", /* 227 */
285 "function cannot return const or volatile object", /* 228 */
286 "converting '%s' to '%s' is questionable", /* 229 */
287 "nonportable character comparison '%s %d'", /* 230 */
288 "argument '%s' unused in function '%s'", /* 231 */
289 "label '%s' unused in function '%s'", /* 232 */
290 "struct '%s' never defined", /* 233 */
291 "union '%s' never defined", /* 234 */
292 "enum '%s' never defined", /* 235 */
293 "static function '%s' unused", /* 236 */
294 "redeclaration of formal parameter '%s'", /* 237 */
295 "initialization of union is illegal in traditional C", /* 238 */
296 "constant argument to '!'", /* 239 */
297 "assignment of different structures (%s != %s)", /* 240 */
298 "dubious operation on enum, op %s", /* 241 */
299 "combination of '%s' and '%s', op %s", /* 242 */
300 "dubious comparison of enums, op %s", /* 243 */
301 "illegal structure pointer combination", /* 244 */
302 "incompatible structure pointers: '%s' '%s' '%s'", /* 245 */
303 "dubious conversion of enum to '%s'", /* 246 */
304 "pointer cast from '%s' to '%s' may be troublesome", /* 247 */
305 "floating-point constant out of range", /* 248 */
306 "syntax error '%s'", /* 249 */
307 "unknown character \\%o", /* 250 */
308 "malformed integer constant", /* 251 */
309 "integer constant out of range", /* 252 */
310 "unterminated character constant", /* 253 */
311 "newline in string or char constant", /* 254 */
312 "undefined or invalid # directive", /* 255 */
313 "unterminated comment", /* 256 */
314 "extra characters in lint comment", /* 257 */
315 "unterminated string constant", /* 258 */
316 "argument #%d is converted from '%s' to '%s' due to prototype", /* 259 */
317 "previous declaration of '%s'", /* 260 */
318 "previous definition of '%s'", /* 261 */
319 "\\\" inside character constants undefined in traditional C", /* 262 */
320 "\\? undefined in traditional C", /* 263 */
321 "\\v undefined in traditional C", /* 264 */
322 "%s does not support 'long long'", /* 265 */
323 "'long double' is illegal in traditional C", /* 266 */
324 "shift equal to size of object", /* 267 */
325 "variable '%s' declared inline", /* 268 */
326 "argument '%s' declared inline", /* 269 */
327 "function prototypes are illegal in traditional C", /* 270 */
328 "switch expression must be of type 'int' in traditional C", /* 271 */
329 "empty translation unit", /* 272 */
330 "bit-field type '%s' invalid in ANSI C", /* 273 */
331 "ANSI C forbids comparison of %s with %s", /* 274 */
332 "cast discards 'const' from type '%s'", /* 275 */
333 "__%s__ is illegal for type %s", /* 276 */
334 "initialization of '%s' with '%s'", /* 277 */
335 "combination of '%s' and '%s', arg #%d", /* 278 */
336 "combination of '%s' and '%s' in return", /* 279 */
337 "must be outside function: /* %s */", /* 280 */
338 "duplicate use of /* %s */", /* 281 */
339 "must precede function definition: /* %s */", /* 282 */
340 "argument number mismatch with directive: /* %s */", /* 283 */
341 "fallthrough on default statement", /* 284 */
342 "prototype declaration", /* 285 */
343 "function definition is not a prototype", /* 286 */
344 "function declaration is not a prototype", /* 287 */
345 "dubious use of /* VARARGS */ with /* %s */", /* 288 */
346 "can't be used together: /* PRINTFLIKE */ /* SCANFLIKE */", /* 289 */
347 "static function %s declared but not defined", /* 290 */
348 "invalid multibyte character", /* 291 */
349 "cannot concatenate wide and regular string literals", /* 292 */
350 "argument %d must be 'char *' for PRINTFLIKE/SCANFLIKE", /* 293 */
351 "multi-character character constant", /* 294 */
352 "conversion of '%s' to '%s' is out of range, arg #%d", /* 295 */
353 "conversion of negative constant to unsigned type, arg #%d", /* 296 */
354 "conversion to '%s' may sign-extend incorrectly, arg #%d", /* 297 */
355 "conversion from '%s' to '%s' may lose accuracy, arg #%d", /* 298 */
356 "prototype does not match old style definition, arg #%d", /* 299 */
357 "old style definition", /* 300 */
358 "array of incomplete type", /* 301 */
359 "%s returns pointer to automatic object", /* 302 */
360 "ANSI C forbids conversion of %s to %s", /* 303 */
361 "ANSI C forbids conversion of %s to %s, arg #%d", /* 304 */
362 "ANSI C forbids conversion of %s to %s, op %s", /* 305 */
363 "constant truncated by conversion, op %s", /* 306 */
364 "static variable '%s' set but not used", /* 307 */
365 "invalid type for _Complex", /* 308 */
366 "extra bits set to 0 in conversion of '%s' to '%s', op '%s'", /* 309 */
367 "symbol renaming can't be used on function arguments", /* 310 */
368 "symbol renaming can't be used on automatic variables", /* 311 */
369 "%s does not support // comments", /* 312 */
370 "struct or union member name in initializer is a C99 feature",/* 313 */
371 "", /* never used */ /* 314 */
372 "GCC style struct or union member name in initializer", /* 315 */
373 "__FUNCTION__/__PRETTY_FUNCTION__ is a GCC extension", /* 316 */
374 "__func__ is a C99 feature", /* 317 */
375 "variable array dimension is a C99/GCC extension", /* 318 */
376 "compound literals are a C99/GCC extension", /* 319 */
377 "({ }) is a GCC extension", /* 320 */
378 "array initializer with designators is a C99 feature", /* 321 */
379 "zero sized array is a C99 extension", /* 322 */
380 "continue in 'do ... while (0)' loop", /* 323 */
381 "suggest cast from '%s' to '%s' on op %s to avoid overflow", /* 324 */
382 "variable declaration in for loop", /* 325 */
383 "%s attribute ignored for %s", /* 326 */
384 "declarations after statements is a C99 feature", /* 327 */
385 "union cast is a GCC extension", /* 328 */
386 "type '%s' is not a member of '%s'", /* 329 */
387 "operand of '%s' must be bool, not '%s'", /* 330 */
388 "left operand of '%s' must be bool, not '%s'", /* 331 */
389 "right operand of '%s' must be bool, not '%s'", /* 332 */
390 "controlling expression must be bool, not '%s'", /* 333 */
391 "argument #%d expects '%s', gets passed '%s'", /* 334 */
392 "operand of '%s' must not be bool", /* 335 */
393 "left operand of '%s' must not be bool", /* 336 */
394 "right operand of '%s' must not be bool", /* 337 */
395 "option '%c' should be handled in the switch", /* 338 */
396 "option '%c' should be listed in the options string", /* 339 */
397 "initialization with '[a...b]' is a GCC extension", /* 340 */
398 "argument to '%s' must be 'unsigned char' or EOF, not '%s'", /* 341 */
399 "argument to '%s' must be cast to 'unsigned char', not to '%s'", /* 342 */
400 "static array size is a C11 extension", /* 343 */
401 "bit-field of type plain 'int' has implementation-defined signedness", /* 344 */
402 "generic selection requires C11 or later", /* 345 */
403 "call to '%s' effectively discards 'const' from argument", /* 346 */
404 "redeclaration of '%s' with type '%s', expected '%s'", /* 347 */
405 "maximum value %d of '%s' does not match maximum array index %d", /* 348 */
406 "non type argument to alignof is a GCC extension", /* 349 */
407 };
408
409 static struct include_level {
410 const char *filename;
411 int lineno;
412 struct include_level *by;
413 } *includes;
414
415
416 void
417 update_location(const char *filename, int lineno, bool is_begin, bool is_end)
418 {
419 struct include_level *top;
420
421 top = includes;
422 if (is_begin && top != NULL)
423 top->lineno = curr_pos.p_line;
424
425 if (top == NULL || is_begin) {
426 top = xmalloc(sizeof(*top));
427 top->filename = filename;
428 top->lineno = lineno;
429 top->by = includes;
430 includes = top;
431 } else {
432 if (is_end) {
433 includes = top->by;
434 free(top);
435 top = includes;
436 }
437 if (top != NULL) {
438 top->filename = filename;
439 top->lineno = lineno;
440 }
441 }
442 }
443
444 static void
445 print_stack_trace(void)
446 {
447 const struct include_level *top;
448
449 if ((top = includes) == NULL)
450 return;
451 /*
452 * Skip the innermost include level since it is already listed in the
453 * diagnostic itself. Furthermore, its lineno is the line number of
454 * the last '#' line, not the current line.
455 */
456 for (top = top->by; top != NULL; top = top->by)
457 printf("\tincluded from %s(%d)\n", top->filename, top->lineno);
458 }
459
460 /*
461 * print a list of the messages with their ids
462 */
463 void
464 msglist(void)
465 {
466 size_t i;
467
468 for (i = 0; i < sizeof(msgs) / sizeof(msgs[0]); i++) {
469 if (msgs[i][0] != '\0')
470 printf("%zu\t%s\n", i, msgs[i]);
471 else
472 printf("---\t(no longer used)\n");
473 }
474 }
475
476 /*
477 * If Fflag is not set, lbasename() returns a pointer to the last
478 * component of the path, otherwise it returns the argument.
479 */
480 static const char *
481 lbasename(const char *path)
482 {
483 const char *p, *base, *dir;
484
485 if (Fflag)
486 return path;
487
488 p = base = dir = path;
489 while (*p != '\0') {
490 if (*p++ == '/') {
491 dir = base;
492 base = p;
493 }
494 }
495 return *base != '\0' ? base : dir;
496 }
497
498 static void
499 verror_at(int msgid, const pos_t *pos, va_list ap)
500 {
501 const char *fn;
502
503 if (ERR_ISSET(msgid, &msgset))
504 return;
505
506 fn = lbasename(pos->p_file);
507 (void)printf("%s(%d): error: ", fn, pos->p_line);
508 (void)vprintf(msgs[msgid], ap);
509 (void)printf(" [%d]\n", msgid);
510 nerr++;
511 print_stack_trace();
512 }
513
514 static void
515 vwarning_at(int msgid, const pos_t *pos, va_list ap)
516 {
517 const char *fn;
518
519 if (ERR_ISSET(msgid, &msgset))
520 return;
521
522 debug_step("%s: lwarn=%d msgid=%d", __func__, lwarn, msgid);
523 if (lwarn == LWARN_NONE || lwarn == msgid)
524 /* this warning is suppressed by a LINTED comment */
525 return;
526
527 fn = lbasename(pos->p_file);
528 (void)printf("%s(%d): warning: ", fn, pos->p_line);
529 (void)vprintf(msgs[msgid], ap);
530 (void)printf(" [%d]\n", msgid);
531 if (wflag)
532 nerr++;
533 print_stack_trace();
534 }
535
536 static void
537 vmessage_at(int msgid, const pos_t *pos, va_list ap)
538 {
539 const char *fn;
540
541 if (ERR_ISSET(msgid, &msgset))
542 return;
543
544 fn = lbasename(pos->p_file);
545 (void)printf("%s(%d): ", fn, pos->p_line);
546 (void)vprintf(msgs[msgid], ap);
547 (void)printf(" [%d]\n", msgid);
548 print_stack_trace();
549 }
550
551 void
552 (error_at)(int msgid, const pos_t *pos, ...)
553 {
554 va_list ap;
555
556 va_start(ap, pos);
557 verror_at(msgid, pos, ap);
558 va_end(ap);
559 }
560
561 void
562 (error)(int msgid, ...)
563 {
564 va_list ap;
565
566 va_start(ap, msgid);
567 verror_at(msgid, &curr_pos, ap);
568 va_end(ap);
569 }
570
571 void
572 internal_error(const char *file, int line, const char *msg, ...)
573 {
574 va_list ap;
575 const char *fn;
576
577 fn = lbasename(curr_pos.p_file);
578 (void)fflush(stdout);
579 (void)fprintf(stderr, "lint: internal error in %s:%d near %s:%d: ",
580 file, line, fn, curr_pos.p_line);
581 va_start(ap, msg);
582 (void)vfprintf(stderr, msg, ap);
583 va_end(ap);
584 (void)fprintf(stderr, "\n");
585 print_stack_trace();
586 abort();
587 }
588
589 void
590 assert_failed(const char *file, int line, const char *func, const char *cond)
591 {
592 const char *fn;
593
594 /*
595 * After encountering a parse error in the grammar, lint often does
596 * not properly clean up its data structures, especially in 'dcs',
597 * the stack of declaration levels. This often leads to assertion
598 * failures. These cases are not interesting though, as the purpose
599 * of lint is to check syntactically valid code. In such a case,
600 * exit gracefully. This allows a fuzzer like afl to focus on more
601 * interesting cases instead of reporting nonsense translation units
602 * like 'f=({e:;}' or 'v(const(char););e(v){'.
603 */
604 if (sytxerr > 0)
605 norecover();
606
607 fn = lbasename(curr_pos.p_file);
608 (void)fflush(stdout);
609 (void)fprintf(stderr,
610 "lint: assertion \"%s\" failed in %s at %s:%d near %s:%d\n",
611 cond, func, file, line, fn, curr_pos.p_line);
612 print_stack_trace();
613 (void)fflush(stdout);
614 abort();
615 }
616
617 void
618 (warning_at)(int msgid, const pos_t *pos, ...)
619 {
620 va_list ap;
621
622 va_start(ap, pos);
623 vwarning_at(msgid, pos, ap);
624 va_end(ap);
625 }
626
627 void
628 (warning)(int msgid, ...)
629 {
630 va_list ap;
631
632 va_start(ap, msgid);
633 vwarning_at(msgid, &curr_pos, ap);
634 va_end(ap);
635 }
636
637 void
638 (message_at)(int msgid, const pos_t *pos, ...)
639 {
640 va_list ap;
641
642 va_start(ap, pos);
643 vmessage_at(msgid, pos, ap);
644 va_end(ap);
645 }
646
647 void
648 (c99ism)(int msgid, ...)
649 {
650 va_list ap;
651
652 if (allow_c99)
653 return;
654
655 va_start(ap, msgid);
656 int severity = (!allow_gcc ? 1 : 0) + (!allow_trad ? 1 : 0);
657 if (severity == 2)
658 verror_at(msgid, &curr_pos, ap);
659 if (severity == 1)
660 vwarning_at(msgid, &curr_pos, ap);
661 va_end(ap);
662 }
663
664 void
665 (c11ism)(int msgid, ...)
666 {
667 va_list ap;
668
669 /* FIXME: C11 mode has nothing to do with GCC mode. */
670 if (allow_c11 || allow_gcc)
671 return;
672 va_start(ap, msgid);
673 verror_at(msgid, &curr_pos, ap);
674 va_end(ap);
675 }
676
677 bool
678 (gnuism)(int msgid, ...)
679 {
680 va_list ap;
681 int severity = (!allow_gcc ? 1 : 0) +
682 (!allow_trad && !allow_c99 ? 1 : 0);
683
684 va_start(ap, msgid);
685 if (severity == 2)
686 verror_at(msgid, &curr_pos, ap);
687 if (severity == 1)
688 vwarning_at(msgid, &curr_pos, ap);
689 va_end(ap);
690 return severity > 0;
691 }
692