main1.c revision 1.62 1 1.62 rillig /* $NetBSD: main1.c,v 1.62 2022/05/20 21:18:55 rillig Exp $ */
2 1.2 cgd
3 1.1 cgd /*
4 1.1 cgd * Copyright (c) 1994, 1995 Jochen Pohl
5 1.1 cgd * All Rights Reserved.
6 1.1 cgd *
7 1.1 cgd * Redistribution and use in source and binary forms, with or without
8 1.1 cgd * modification, are permitted provided that the following conditions
9 1.1 cgd * are met:
10 1.1 cgd * 1. Redistributions of source code must retain the above copyright
11 1.1 cgd * notice, this list of conditions and the following disclaimer.
12 1.1 cgd * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 cgd * notice, this list of conditions and the following disclaimer in the
14 1.1 cgd * documentation and/or other materials provided with the distribution.
15 1.1 cgd * 3. All advertising materials mentioning features or use of this software
16 1.1 cgd * must display the following acknowledgement:
17 1.1 cgd * This product includes software developed by Jochen Pohl for
18 1.1 cgd * The NetBSD Project.
19 1.1 cgd * 4. The name of the author may not be used to endorse or promote products
20 1.1 cgd * derived from this software without specific prior written permission.
21 1.1 cgd *
22 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
23 1.1 cgd * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
24 1.1 cgd * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
25 1.1 cgd * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
26 1.1 cgd * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
27 1.1 cgd * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 1.1 cgd * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 1.1 cgd * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 1.1 cgd * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
31 1.1 cgd * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 1.1 cgd */
33 1.1 cgd
34 1.15 jmc #if HAVE_NBTOOL_CONFIG_H
35 1.15 jmc #include "nbtool_config.h"
36 1.15 jmc #endif
37 1.15 jmc
38 1.4 christos #include <sys/cdefs.h>
39 1.62 rillig #if defined(__RCSID)
40 1.62 rillig __RCSID("$NetBSD: main1.c,v 1.62 2022/05/20 21:18:55 rillig Exp $");
41 1.1 cgd #endif
42 1.1 cgd
43 1.5 christos #include <sys/types.h>
44 1.29 rillig #include <errno.h>
45 1.29 rillig #include <limits.h>
46 1.29 rillig #include <signal.h>
47 1.1 cgd #include <stdio.h>
48 1.29 rillig #include <stdlib.h>
49 1.5 christos #include <string.h>
50 1.1 cgd #include <unistd.h>
51 1.1 cgd
52 1.1 cgd #include "lint1.h"
53 1.1 cgd
54 1.1 cgd /* set yydebug to 1*/
55 1.36 rillig bool yflag;
56 1.1 cgd
57 1.1 cgd /*
58 1.32 rillig * Print warnings if an assignment of an integer type to another integer type
59 1.16 perry * causes an implicit narrowing conversion. If aflag is 1, these warnings
60 1.1 cgd * are printed only if the source type is at least as wide as long. If aflag
61 1.9 wiz * is greater than 1, they are always printed.
62 1.1 cgd */
63 1.1 cgd int aflag;
64 1.1 cgd
65 1.1 cgd /* Print a warning if a break statement cannot be reached. */
66 1.36 rillig bool bflag;
67 1.1 cgd
68 1.1 cgd /* Print warnings for pointer casts. */
69 1.36 rillig bool cflag;
70 1.1 cgd
71 1.1 cgd /* Perform stricter checking of enum types and operations on enum types. */
72 1.36 rillig bool eflag;
73 1.1 cgd
74 1.1 cgd /* Print complete pathnames, not only the basename. */
75 1.36 rillig bool Fflag;
76 1.1 cgd
77 1.5 christos /* Treat warnings as errors */
78 1.36 rillig bool wflag;
79 1.5 christos
80 1.1 cgd /*
81 1.1 cgd * Apply a number of heuristic tests to attempt to intuit bugs, improve
82 1.1 cgd * style, and reduce waste.
83 1.1 cgd */
84 1.36 rillig bool hflag;
85 1.1 cgd
86 1.1 cgd /* Attempt to check portability to other dialects of C. */
87 1.36 rillig bool pflag;
88 1.1 cgd
89 1.1 cgd /*
90 1.1 cgd * In case of redeclarations/redefinitions print the location of the
91 1.1 cgd * previous declaration/definition.
92 1.1 cgd */
93 1.36 rillig bool rflag;
94 1.1 cgd
95 1.34 rillig bool Tflag;
96 1.34 rillig
97 1.19 christos /* Picky flag */
98 1.36 rillig bool Pflag;
99 1.19 christos
100 1.1 cgd /*
101 1.1 cgd * Complain about functions and external variables used and not defined,
102 1.1 cgd * or defined and not used.
103 1.1 cgd */
104 1.36 rillig bool uflag = true;
105 1.1 cgd
106 1.1 cgd /* Complain about unused function arguments. */
107 1.36 rillig bool vflag = true;
108 1.1 cgd
109 1.1 cgd /* Complain about structures which are never defined. */
110 1.36 rillig bool zflag = true;
111 1.1 cgd
112 1.60 rillig /*
113 1.60 rillig * The default language level is the one that checks for compatibility
114 1.60 rillig * between traditional C and C90. As of 2022, this default is no longer
115 1.60 rillig * useful since most traditional C code has already been migrated.
116 1.60 rillig */
117 1.60 rillig bool allow_trad = true;
118 1.60 rillig bool allow_c90 = true;
119 1.60 rillig bool allow_c99;
120 1.60 rillig bool allow_c11;
121 1.60 rillig bool allow_gcc;
122 1.60 rillig
123 1.10 augustss err_set msgset;
124 1.5 christos
125 1.18 christos sig_atomic_t fpe;
126 1.18 christos
127 1.8 lukem static void usage(void);
128 1.4 christos
129 1.25 christos static FILE *
130 1.41 rillig gcc_builtins(void)
131 1.25 christos {
132 1.58 rillig /* https://gcc.gnu.org/onlinedocs/gcc/Other-Builtins.html */
133 1.54 rillig static const char builtins[] =
134 1.58 rillig "typedef typeof(sizeof(0)) __lint_size_t;\n"
135 1.58 rillig
136 1.58 rillig "void *alloca(__lint_size_t);\n"
137 1.58 rillig "void *__builtin_alloca(__lint_size_t);\n"
138 1.58 rillig "void *__builtin_alloca_with_align"
139 1.58 rillig "(__lint_size_t, __lint_size_t);\n"
140 1.58 rillig "void *__builtin_alloca_with_align_and_max"
141 1.58 rillig "(__lint_size_t, __lint_size_t, __lint_size_t);\n"
142 1.58 rillig
143 1.54 rillig "int __builtin_isinf(long double);\n"
144 1.54 rillig "int __builtin_isnan(long double);\n"
145 1.54 rillig "int __builtin_copysign(long double, long double);\n";
146 1.54 rillig size_t builtins_len = sizeof(builtins) - 1;
147 1.54 rillig
148 1.25 christos #if HAVE_NBTOOL_CONFIG_H
149 1.25 christos char template[] = "/tmp/lint.XXXXXX";
150 1.25 christos int fd;
151 1.25 christos FILE *fp;
152 1.25 christos if ((fd = mkstemp(template)) == -1)
153 1.25 christos return NULL;
154 1.25 christos (void)unlink(template);
155 1.25 christos if ((fp = fdopen(fd, "r+")) == NULL) {
156 1.57 rillig (void)close(fd);
157 1.25 christos return NULL;
158 1.25 christos }
159 1.54 rillig if (fwrite(builtins, 1, builtins_len, fp) != builtins_len) {
160 1.57 rillig (void)fclose(fp);
161 1.25 christos return NULL;
162 1.25 christos }
163 1.25 christos rewind(fp);
164 1.25 christos return fp;
165 1.25 christos #else
166 1.54 rillig return fmemopen(__UNCONST(builtins), builtins_len, "r");
167 1.23 christos #endif
168 1.25 christos }
169 1.1 cgd
170 1.18 christos /*ARGSUSED*/
171 1.18 christos static void
172 1.18 christos sigfpe(int s)
173 1.18 christos {
174 1.37 rillig fpe = 1;
175 1.18 christos }
176 1.18 christos
177 1.56 rillig static void
178 1.56 rillig suppress_messages(char *ids)
179 1.56 rillig {
180 1.56 rillig char *ptr, *end;
181 1.56 rillig long id;
182 1.56 rillig
183 1.56 rillig for (ptr = strtok(ids, ","); ptr != NULL; ptr = strtok(NULL, ",")) {
184 1.56 rillig errno = 0;
185 1.56 rillig id = strtol(ptr, &end, 0);
186 1.56 rillig if ((id == TARG_LONG_MIN || id == TARG_LONG_MAX) &&
187 1.56 rillig errno == ERANGE)
188 1.56 rillig err(1, "invalid error message id '%s'", ptr);
189 1.56 rillig if (*end != '\0' || ptr == end || id < 0 || id >= ERR_SETSIZE)
190 1.56 rillig errx(1, "invalid error message id '%s'", ptr);
191 1.56 rillig ERR_SET(id, &msgset);
192 1.56 rillig }
193 1.56 rillig }
194 1.56 rillig
195 1.1 cgd int
196 1.8 lukem main(int argc, char *argv[])
197 1.1 cgd {
198 1.56 rillig int c;
199 1.1 cgd
200 1.12 tv setprogname(argv[0]);
201 1.12 tv
202 1.10 augustss ERR_ZERO(&msgset);
203 1.53 rillig while ((c = getopt(argc, argv, "abceghmprstuvwyzA:FPR:STX:")) != -1) {
204 1.1 cgd switch (c) {
205 1.1 cgd case 'a': aflag++; break;
206 1.36 rillig case 'b': bflag = true; break;
207 1.36 rillig case 'c': cflag = true; break;
208 1.36 rillig case 'e': eflag = true; break;
209 1.36 rillig case 'F': Fflag = true; break;
210 1.60 rillig case 'g': allow_gcc = true; break;
211 1.36 rillig case 'h': hflag = true; break;
212 1.36 rillig case 'p': pflag = true; break;
213 1.36 rillig case 'P': Pflag = true; break;
214 1.36 rillig case 'r': rflag = true; break;
215 1.60 rillig case 's':
216 1.60 rillig allow_trad = false;
217 1.60 rillig allow_c90 = true;
218 1.60 rillig allow_c99 = false;
219 1.60 rillig allow_c11 = false;
220 1.60 rillig break;
221 1.60 rillig case 'S':
222 1.60 rillig allow_trad = false;
223 1.60 rillig allow_c90 = true;
224 1.60 rillig allow_c99 = true;
225 1.60 rillig allow_c11 = false;
226 1.60 rillig break;
227 1.34 rillig case 'T': Tflag = true; break;
228 1.60 rillig case 't':
229 1.60 rillig allow_trad = true;
230 1.60 rillig allow_c90 = false;
231 1.60 rillig allow_c99 = false;
232 1.60 rillig allow_c11 = false;
233 1.60 rillig break;
234 1.36 rillig case 'u': uflag = false; break;
235 1.36 rillig case 'w': wflag = true; break;
236 1.36 rillig case 'v': vflag = false; break;
237 1.36 rillig case 'y': yflag = true; break;
238 1.36 rillig case 'z': zflag = false; break;
239 1.5 christos
240 1.43 rillig case 'A':
241 1.43 rillig if (strcmp(optarg, "c11") == 0) {
242 1.60 rillig allow_trad = false;
243 1.60 rillig allow_c90 = true;
244 1.60 rillig allow_c99 = true;
245 1.60 rillig allow_c11 = true;
246 1.43 rillig } else
247 1.43 rillig usage();
248 1.43 rillig break;
249 1.43 rillig
250 1.5 christos case 'm':
251 1.5 christos msglist();
252 1.33 rillig return 0;
253 1.5 christos
254 1.28 rillig case 'R':
255 1.40 rillig add_directory_replacement(optarg);
256 1.26 christos break;
257 1.26 christos
258 1.5 christos case 'X':
259 1.56 rillig suppress_messages(optarg);
260 1.5 christos break;
261 1.5 christos default:
262 1.5 christos usage();
263 1.1 cgd }
264 1.1 cgd }
265 1.1 cgd argc -= optind;
266 1.1 cgd argv += optind;
267 1.1 cgd
268 1.1 cgd if (argc != 2)
269 1.1 cgd usage();
270 1.1 cgd
271 1.1 cgd
272 1.1 cgd /* initialize output */
273 1.1 cgd outopen(argv[1]);
274 1.1 cgd
275 1.49 rillig #ifdef DEBUG
276 1.49 rillig setvbuf(stdout, NULL, _IONBF, 0);
277 1.49 rillig #endif
278 1.27 christos #ifdef YYDEBUG
279 1.1 cgd if (yflag)
280 1.1 cgd yydebug = 1;
281 1.27 christos #endif
282 1.1 cgd
283 1.18 christos (void)signal(SIGFPE, sigfpe);
284 1.1 cgd initmem();
285 1.1 cgd initdecl();
286 1.1 cgd initscan();
287 1.1 cgd
288 1.61 rillig if (allow_gcc && allow_c90) {
289 1.41 rillig if ((yyin = gcc_builtins()) == NULL)
290 1.41 rillig err(1, "cannot open builtins");
291 1.41 rillig yyparse();
292 1.57 rillig (void)fclose(yyin);
293 1.41 rillig }
294 1.25 christos
295 1.22 christos /* open the input file */
296 1.22 christos if ((yyin = fopen(argv[0], "r")) == NULL)
297 1.22 christos err(1, "cannot open '%s'", argv[0]);
298 1.1 cgd yyparse();
299 1.57 rillig (void)fclose(yyin);
300 1.1 cgd
301 1.1 cgd /* Following warnings cannot be suppressed by LINTED */
302 1.21 christos lwarn = LWARN_ALL;
303 1.51 rillig debug_step("main lwarn = %d", lwarn);
304 1.1 cgd
305 1.30 rillig check_global_symbols();
306 1.1 cgd
307 1.1 cgd outclose();
308 1.1 cgd
309 1.36 rillig return nerr != 0 ? 1 : 0;
310 1.1 cgd }
311 1.1 cgd
312 1.45 rillig static void __attribute__((noreturn))
313 1.8 lukem usage(void)
314 1.1 cgd {
315 1.5 christos (void)fprintf(stderr,
316 1.55 rillig "usage: %s [-abceghmprstuvwyzFPST] [-Ac11] [-R old=new]\n"
317 1.55 rillig " %*s [-X <id>[,<id>]...] src dest\n",
318 1.55 rillig getprogname(), (int)strlen(getprogname()), "");
319 1.1 cgd exit(1);
320 1.1 cgd }
321 1.8 lukem
322 1.45 rillig void __attribute__((noreturn))
323 1.8 lukem norecover(void)
324 1.1 cgd {
325 1.1 cgd /* cannot recover from previous errors */
326 1.1 cgd error(224);
327 1.1 cgd exit(1);
328 1.1 cgd }
329