main1.c revision 1.49 1 1.49 rillig /* $NetBSD: main1.c,v 1.49 2021/07/04 05:49:20 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.12 tv #if defined(__RCSID) && !defined(lint)
40 1.49 rillig __RCSID("$NetBSD: main1.c,v 1.49 2021/07/04 05:49:20 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.43 rillig /* Allow features from C11, C99 and C90. */
72 1.43 rillig bool c11flag;
73 1.43 rillig
74 1.1 cgd /* Print various debug information. */
75 1.36 rillig bool dflag;
76 1.1 cgd
77 1.1 cgd /* Perform stricter checking of enum types and operations on enum types. */
78 1.36 rillig bool eflag;
79 1.1 cgd
80 1.1 cgd /* Print complete pathnames, not only the basename. */
81 1.36 rillig bool Fflag;
82 1.1 cgd
83 1.1 cgd /* Enable some extensions of gcc */
84 1.36 rillig bool gflag;
85 1.1 cgd
86 1.5 christos /* Treat warnings as errors */
87 1.36 rillig bool wflag;
88 1.5 christos
89 1.1 cgd /*
90 1.1 cgd * Apply a number of heuristic tests to attempt to intuit bugs, improve
91 1.1 cgd * style, and reduce waste.
92 1.1 cgd */
93 1.36 rillig bool hflag;
94 1.1 cgd
95 1.1 cgd /* Attempt to check portability to other dialects of C. */
96 1.36 rillig bool pflag;
97 1.1 cgd
98 1.1 cgd /*
99 1.1 cgd * In case of redeclarations/redefinitions print the location of the
100 1.1 cgd * previous declaration/definition.
101 1.1 cgd */
102 1.36 rillig bool rflag;
103 1.1 cgd
104 1.1 cgd /* Strict ANSI C mode. */
105 1.36 rillig bool sflag;
106 1.1 cgd
107 1.34 rillig bool Tflag;
108 1.34 rillig
109 1.1 cgd /* Traditional C mode. */
110 1.36 rillig bool tflag;
111 1.1 cgd
112 1.13 christos /* Enable C9X extensions */
113 1.36 rillig bool Sflag;
114 1.19 christos
115 1.19 christos /* Picky flag */
116 1.36 rillig bool Pflag;
117 1.19 christos
118 1.1 cgd /*
119 1.1 cgd * Complain about functions and external variables used and not defined,
120 1.1 cgd * or defined and not used.
121 1.1 cgd */
122 1.36 rillig bool uflag = true;
123 1.1 cgd
124 1.1 cgd /* Complain about unused function arguments. */
125 1.36 rillig bool vflag = true;
126 1.1 cgd
127 1.1 cgd /* Complain about structures which are never defined. */
128 1.36 rillig bool zflag = true;
129 1.1 cgd
130 1.10 augustss err_set msgset;
131 1.5 christos
132 1.18 christos sig_atomic_t fpe;
133 1.18 christos
134 1.8 lukem static void usage(void);
135 1.4 christos
136 1.22 christos static const char builtins[] =
137 1.22 christos "int __builtin_isinf(long double);\n"
138 1.22 christos "int __builtin_isnan(long double);\n"
139 1.22 christos "int __builtin_copysign(long double, long double);\n"
140 1.22 christos ;
141 1.42 rillig static const size_t builtinlen = sizeof(builtins) - 1;
142 1.25 christos
143 1.25 christos static FILE *
144 1.41 rillig gcc_builtins(void)
145 1.25 christos {
146 1.25 christos #if HAVE_NBTOOL_CONFIG_H
147 1.25 christos char template[] = "/tmp/lint.XXXXXX";
148 1.25 christos int fd;
149 1.25 christos FILE *fp;
150 1.25 christos if ((fd = mkstemp(template)) == -1)
151 1.25 christos return NULL;
152 1.25 christos (void)unlink(template);
153 1.25 christos if ((fp = fdopen(fd, "r+")) == NULL) {
154 1.25 christos close(fd);
155 1.25 christos return NULL;
156 1.25 christos }
157 1.25 christos if (fwrite(builtins, 1, builtinlen, fp) != builtinlen) {
158 1.25 christos fclose(fp);
159 1.25 christos return NULL;
160 1.25 christos }
161 1.25 christos rewind(fp);
162 1.25 christos return fp;
163 1.25 christos #else
164 1.25 christos return fmemopen(__UNCONST(builtins), builtinlen, "r");
165 1.23 christos #endif
166 1.25 christos }
167 1.1 cgd
168 1.18 christos /*ARGSUSED*/
169 1.18 christos static void
170 1.18 christos sigfpe(int s)
171 1.18 christos {
172 1.37 rillig fpe = 1;
173 1.18 christos }
174 1.18 christos
175 1.1 cgd int
176 1.8 lukem main(int argc, char *argv[])
177 1.1 cgd {
178 1.1 cgd int c;
179 1.5 christos char *ptr;
180 1.1 cgd
181 1.12 tv setprogname(argv[0]);
182 1.12 tv
183 1.10 augustss ERR_ZERO(&msgset);
184 1.43 rillig while ((c = getopt(argc, argv, "abcdeghmprstuvwyzA:FPR:STX:")) != -1) {
185 1.1 cgd switch (c) {
186 1.1 cgd case 'a': aflag++; break;
187 1.36 rillig case 'b': bflag = true; break;
188 1.36 rillig case 'c': cflag = true; break;
189 1.36 rillig case 'd': dflag = true; break;
190 1.36 rillig case 'e': eflag = true; break;
191 1.36 rillig case 'F': Fflag = true; break;
192 1.36 rillig case 'g': gflag = true; break;
193 1.36 rillig case 'h': hflag = true; break;
194 1.36 rillig case 'p': pflag = true; break;
195 1.36 rillig case 'P': Pflag = true; break;
196 1.36 rillig case 'r': rflag = true; break;
197 1.36 rillig case 's': sflag = true; break;
198 1.36 rillig case 'S': Sflag = true; break;
199 1.34 rillig case 'T': Tflag = true; break;
200 1.36 rillig case 't': tflag = true; break;
201 1.36 rillig case 'u': uflag = false; break;
202 1.36 rillig case 'w': wflag = true; break;
203 1.36 rillig case 'v': vflag = false; break;
204 1.36 rillig case 'y': yflag = true; break;
205 1.36 rillig case 'z': zflag = false; break;
206 1.5 christos
207 1.43 rillig case 'A':
208 1.43 rillig if (strcmp(optarg, "c11") == 0) {
209 1.43 rillig c11flag = true;
210 1.43 rillig Sflag = true;
211 1.46 rillig sflag = false;
212 1.43 rillig } else
213 1.43 rillig usage();
214 1.43 rillig break;
215 1.43 rillig
216 1.5 christos case 'm':
217 1.5 christos msglist();
218 1.33 rillig return 0;
219 1.5 christos
220 1.28 rillig case 'R':
221 1.40 rillig add_directory_replacement(optarg);
222 1.26 christos break;
223 1.26 christos
224 1.5 christos case 'X':
225 1.36 rillig for (ptr = strtok(optarg, ","); ptr != NULL;
226 1.5 christos ptr = strtok(NULL, ",")) {
227 1.5 christos char *eptr;
228 1.14 lukem long msg;
229 1.14 lukem
230 1.14 lukem errno = 0;
231 1.14 lukem msg = strtol(ptr, &eptr, 0);
232 1.20 christos if ((msg == TARG_LONG_MIN || msg == TARG_LONG_MAX) &&
233 1.5 christos errno == ERANGE)
234 1.5 christos err(1, "invalid error message id '%s'",
235 1.5 christos ptr);
236 1.36 rillig if (*eptr != '\0' || ptr == eptr || msg < 0 ||
237 1.10 augustss msg >= ERR_SETSIZE)
238 1.5 christos errx(1, "invalid error message id '%s'",
239 1.5 christos ptr);
240 1.10 augustss ERR_SET(msg, &msgset);
241 1.5 christos }
242 1.5 christos break;
243 1.5 christos default:
244 1.5 christos usage();
245 1.1 cgd }
246 1.1 cgd }
247 1.1 cgd argc -= optind;
248 1.1 cgd argv += optind;
249 1.1 cgd
250 1.1 cgd if (argc != 2)
251 1.1 cgd usage();
252 1.1 cgd
253 1.1 cgd
254 1.1 cgd /* initialize output */
255 1.1 cgd outopen(argv[1]);
256 1.1 cgd
257 1.49 rillig #ifdef DEBUG
258 1.49 rillig setvbuf(stdout, NULL, _IONBF, 0);
259 1.49 rillig #endif
260 1.47 rillig if (dflag)
261 1.47 rillig setvbuf(stdout, NULL, _IONBF, 0);
262 1.27 christos #ifdef YYDEBUG
263 1.1 cgd if (yflag)
264 1.1 cgd yydebug = 1;
265 1.27 christos #endif
266 1.1 cgd
267 1.18 christos (void)signal(SIGFPE, sigfpe);
268 1.1 cgd initmem();
269 1.1 cgd initdecl();
270 1.1 cgd initscan();
271 1.1 cgd
272 1.48 rillig if (gflag && !tflag) {
273 1.41 rillig if ((yyin = gcc_builtins()) == NULL)
274 1.41 rillig err(1, "cannot open builtins");
275 1.41 rillig yyparse();
276 1.41 rillig fclose(yyin);
277 1.41 rillig }
278 1.25 christos
279 1.22 christos /* open the input file */
280 1.22 christos if ((yyin = fopen(argv[0], "r")) == NULL)
281 1.22 christos err(1, "cannot open '%s'", argv[0]);
282 1.1 cgd yyparse();
283 1.25 christos fclose(yyin);
284 1.1 cgd
285 1.1 cgd /* Following warnings cannot be suppressed by LINTED */
286 1.21 christos lwarn = LWARN_ALL;
287 1.17 christos #ifdef DEBUG
288 1.21 christos printf("%s, %d: lwarn = %d\n", curr_pos.p_file, curr_pos.p_line, lwarn);
289 1.17 christos #endif
290 1.1 cgd
291 1.30 rillig check_global_symbols();
292 1.1 cgd
293 1.1 cgd outclose();
294 1.1 cgd
295 1.36 rillig return nerr != 0 ? 1 : 0;
296 1.1 cgd }
297 1.1 cgd
298 1.45 rillig static void __attribute__((noreturn))
299 1.8 lukem usage(void)
300 1.1 cgd {
301 1.5 christos (void)fprintf(stderr,
302 1.43 rillig "usage: %s [-abcdeghmprstuvwyzFST] [-Ac11] [-X <id>[,<id>]... "
303 1.43 rillig "src dest\n",
304 1.6 cgd getprogname());
305 1.1 cgd exit(1);
306 1.1 cgd }
307 1.8 lukem
308 1.45 rillig void __attribute__((noreturn))
309 1.8 lukem norecover(void)
310 1.1 cgd {
311 1.1 cgd /* cannot recover from previous errors */
312 1.1 cgd error(224);
313 1.1 cgd exit(1);
314 1.1 cgd }
315