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