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