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