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