main1.c revision 1.57 1 /* $NetBSD: main1.c,v 1.57 2021/08/28 13:29:26 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) && !defined(lint)
40 __RCSID("$NetBSD: main1.c,v 1.57 2021/08/28 13:29:26 rillig Exp $");
41 #endif
42
43 #include <sys/types.h>
44 #include <errno.h>
45 #include <limits.h>
46 #include <signal.h>
47 #include <stdio.h>
48 #include <stdlib.h>
49 #include <string.h>
50 #include <unistd.h>
51
52 #include "lint1.h"
53
54 /* set yydebug to 1*/
55 bool yflag;
56
57 /*
58 * Print warnings if an assignment of an integer type to another integer type
59 * causes an implicit narrowing conversion. If aflag is 1, these warnings
60 * are printed only if the source type is at least as wide as long. If aflag
61 * is greater than 1, they are always printed.
62 */
63 int aflag;
64
65 /* Print a warning if a break statement cannot be reached. */
66 bool bflag;
67
68 /* Print warnings for pointer casts. */
69 bool cflag;
70
71 /* Allow features from C11, C99 and C90. */
72 bool c11flag;
73
74 /* Perform stricter checking of enum types and operations on enum types. */
75 bool eflag;
76
77 /* Print complete pathnames, not only the basename. */
78 bool Fflag;
79
80 /* Enable some extensions of gcc */
81 bool gflag;
82
83 /* Treat warnings as errors */
84 bool wflag;
85
86 /*
87 * Apply a number of heuristic tests to attempt to intuit bugs, improve
88 * style, and reduce waste.
89 */
90 bool hflag;
91
92 /* Attempt to check portability to other dialects of C. */
93 bool pflag;
94
95 /*
96 * In case of redeclarations/redefinitions print the location of the
97 * previous declaration/definition.
98 */
99 bool rflag;
100
101 /* Strict ANSI C mode. */
102 bool sflag;
103
104 bool Tflag;
105
106 /* Traditional C mode. */
107 bool tflag;
108
109 /* Enable C9X extensions */
110 bool Sflag;
111
112 /* Picky flag */
113 bool Pflag;
114
115 /*
116 * Complain about functions and external variables used and not defined,
117 * or defined and not used.
118 */
119 bool uflag = true;
120
121 /* Complain about unused function arguments. */
122 bool vflag = true;
123
124 /* Complain about structures which are never defined. */
125 bool zflag = true;
126
127 err_set msgset;
128
129 sig_atomic_t fpe;
130
131 static void usage(void);
132
133 static FILE *
134 gcc_builtins(void)
135 {
136 static const char builtins[] =
137 "int __builtin_isinf(long double);\n"
138 "int __builtin_isnan(long double);\n"
139 "int __builtin_copysign(long double, long double);\n";
140 size_t builtins_len = sizeof(builtins) - 1;
141
142 #if HAVE_NBTOOL_CONFIG_H
143 char template[] = "/tmp/lint.XXXXXX";
144 int fd;
145 FILE *fp;
146 if ((fd = mkstemp(template)) == -1)
147 return NULL;
148 (void)unlink(template);
149 if ((fp = fdopen(fd, "r+")) == NULL) {
150 (void)close(fd);
151 return NULL;
152 }
153 if (fwrite(builtins, 1, builtins_len, fp) != builtins_len) {
154 (void)fclose(fp);
155 return NULL;
156 }
157 rewind(fp);
158 return fp;
159 #else
160 return fmemopen(__UNCONST(builtins), builtins_len, "r");
161 #endif
162 }
163
164 /*ARGSUSED*/
165 static void
166 sigfpe(int s)
167 {
168 fpe = 1;
169 }
170
171 static void
172 suppress_messages(char *ids)
173 {
174 char *ptr, *end;
175 long id;
176
177 for (ptr = strtok(ids, ","); ptr != NULL; ptr = strtok(NULL, ",")) {
178 errno = 0;
179 id = strtol(ptr, &end, 0);
180 if ((id == TARG_LONG_MIN || id == TARG_LONG_MAX) &&
181 errno == ERANGE)
182 err(1, "invalid error message id '%s'", ptr);
183 if (*end != '\0' || ptr == end || id < 0 || id >= ERR_SETSIZE)
184 errx(1, "invalid error message id '%s'", ptr);
185 ERR_SET(id, &msgset);
186 }
187 }
188
189 int
190 main(int argc, char *argv[])
191 {
192 int c;
193
194 setprogname(argv[0]);
195
196 ERR_ZERO(&msgset);
197 while ((c = getopt(argc, argv, "abceghmprstuvwyzA:FPR:STX:")) != -1) {
198 switch (c) {
199 case 'a': aflag++; break;
200 case 'b': bflag = true; break;
201 case 'c': cflag = true; break;
202 case 'e': eflag = true; break;
203 case 'F': Fflag = true; break;
204 case 'g': gflag = true; break;
205 case 'h': hflag = true; break;
206 case 'p': pflag = true; break;
207 case 'P': Pflag = true; break;
208 case 'r': rflag = true; break;
209 case 's': sflag = true; break;
210 case 'S': Sflag = true; break;
211 case 'T': Tflag = true; break;
212 case 't': tflag = true; break;
213 case 'u': uflag = false; break;
214 case 'w': wflag = true; break;
215 case 'v': vflag = false; break;
216 case 'y': yflag = true; break;
217 case 'z': zflag = false; break;
218
219 case 'A':
220 if (strcmp(optarg, "c11") == 0) {
221 c11flag = true;
222 Sflag = true;
223 sflag = false;
224 } else
225 usage();
226 break;
227
228 case 'm':
229 msglist();
230 return 0;
231
232 case 'R':
233 add_directory_replacement(optarg);
234 break;
235
236 case 'X':
237 suppress_messages(optarg);
238 break;
239 default:
240 usage();
241 }
242 }
243 argc -= optind;
244 argv += optind;
245
246 if (argc != 2)
247 usage();
248
249
250 /* initialize output */
251 outopen(argv[1]);
252
253 #ifdef DEBUG
254 setvbuf(stdout, NULL, _IONBF, 0);
255 #endif
256 #ifdef YYDEBUG
257 if (yflag)
258 yydebug = 1;
259 #endif
260
261 (void)signal(SIGFPE, sigfpe);
262 initmem();
263 initdecl();
264 initscan();
265
266 if (gflag && !tflag) {
267 if ((yyin = gcc_builtins()) == NULL)
268 err(1, "cannot open builtins");
269 yyparse();
270 (void)fclose(yyin);
271 }
272
273 /* open the input file */
274 if ((yyin = fopen(argv[0], "r")) == NULL)
275 err(1, "cannot open '%s'", argv[0]);
276 yyparse();
277 (void)fclose(yyin);
278
279 /* Following warnings cannot be suppressed by LINTED */
280 lwarn = LWARN_ALL;
281 debug_step("main lwarn = %d", lwarn);
282
283 check_global_symbols();
284
285 outclose();
286
287 return nerr != 0 ? 1 : 0;
288 }
289
290 static void __attribute__((noreturn))
291 usage(void)
292 {
293 (void)fprintf(stderr,
294 "usage: %s [-abceghmprstuvwyzFPST] [-Ac11] [-R old=new]\n"
295 " %*s [-X <id>[,<id>]...] src dest\n",
296 getprogname(), (int)strlen(getprogname()), "");
297 exit(1);
298 }
299
300 void __attribute__((noreturn))
301 norecover(void)
302 {
303 /* cannot recover from previous errors */
304 error(224);
305 exit(1);
306 }
307