Home | History | Annotate | Line # | Download | only in lint1
main1.c revision 1.63
      1 /*	$NetBSD: main1.c,v 1.63 2022/05/30 15:13:25 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: main1.c,v 1.63 2022/05/30 15:13:25 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 /* Perform stricter checking of enum types and operations on enum types. */
     72 bool	eflag;
     73 
     74 /* Print complete pathnames, not only the basename. */
     75 bool	Fflag;
     76 
     77 /* Treat warnings as errors */
     78 bool	wflag;
     79 
     80 /*
     81  * Apply a number of heuristic tests to attempt to intuit bugs, improve
     82  * style, and reduce waste.
     83  */
     84 bool	hflag;
     85 
     86 /* Attempt to check portability to other dialects of C. */
     87 bool	pflag;
     88 
     89 /*
     90  * In case of redeclarations/redefinitions print the location of the
     91  * previous declaration/definition.
     92  */
     93 bool	rflag;
     94 
     95 bool	Tflag;
     96 
     97 /* Picky flag */
     98 bool	Pflag;
     99 
    100 /*
    101  * Complain about functions and external variables used and not defined,
    102  * or defined and not used.
    103  */
    104 bool	uflag = true;
    105 
    106 /* Complain about unused function arguments. */
    107 bool	vflag = true;
    108 
    109 /* Complain about structures which are never defined. */
    110 bool	zflag = true;
    111 
    112 /*
    113  * The default language level is the one that checks for compatibility
    114  * between traditional C and C90.  As of 2022, this default is no longer
    115  * useful since most traditional C code has already been migrated.
    116  */
    117 bool	allow_trad = true;
    118 bool	allow_c90 = true;
    119 bool	allow_c99;
    120 bool	allow_c11;
    121 bool	allow_gcc;
    122 
    123 err_set	msgset;
    124 
    125 sig_atomic_t fpe;
    126 
    127 static	void	usage(void);
    128 
    129 static FILE *
    130 gcc_builtins(void)
    131 {
    132 	/* https://gcc.gnu.org/onlinedocs/gcc/Other-Builtins.html */
    133 	static const char builtins[] =
    134 	    "typedef typeof(sizeof(0)) __lint_size_t;\n"
    135 
    136 	    "void *alloca(__lint_size_t);\n"
    137 	    "void *__builtin_alloca(__lint_size_t);\n"
    138 	    "void *__builtin_alloca_with_align"
    139 		"(__lint_size_t, __lint_size_t);\n"
    140 	    "void *__builtin_alloca_with_align_and_max"
    141 		"(__lint_size_t, __lint_size_t, __lint_size_t);\n"
    142 
    143 	    "int __builtin_isinf(long double);\n"
    144 	    "int __builtin_isnan(long double);\n"
    145 	    "int __builtin_copysign(long double, long double);\n";
    146 	size_t builtins_len = sizeof(builtins) - 1;
    147 
    148 #if HAVE_NBTOOL_CONFIG_H
    149 	char template[] = "/tmp/lint.XXXXXX";
    150 	int fd;
    151 	FILE *fp;
    152 	if ((fd = mkstemp(template)) == -1)
    153 		return NULL;
    154 	(void)unlink(template);
    155 	if ((fp = fdopen(fd, "r+")) == NULL) {
    156 		(void)close(fd);
    157 		return NULL;
    158 	}
    159 	if (fwrite(builtins, 1, builtins_len, fp) != builtins_len) {
    160 		(void)fclose(fp);
    161 		return NULL;
    162 	}
    163 	rewind(fp);
    164 	return fp;
    165 #else
    166 	return fmemopen(__UNCONST(builtins), builtins_len, "r");
    167 #endif
    168 }
    169 
    170 /*ARGSUSED*/
    171 static void
    172 sigfpe(int s)
    173 {
    174 	fpe = 1;
    175 }
    176 
    177 static void
    178 suppress_messages(char *ids)
    179 {
    180 	char *ptr, *end;
    181 	long id;
    182 
    183 	for (ptr = strtok(ids, ","); ptr != NULL; ptr = strtok(NULL, ",")) {
    184 		errno = 0;
    185 		id = strtol(ptr, &end, 0);
    186 		if ((id == TARG_LONG_MIN || id == TARG_LONG_MAX) &&
    187 		    errno == ERANGE)
    188 			err(1, "invalid error message id '%s'", ptr);
    189 		if (*end != '\0' || ptr == end || id < 0 || id >= ERR_SETSIZE)
    190 			errx(1, "invalid error message id '%s'", ptr);
    191 		ERR_SET(id, &msgset);
    192 	}
    193 }
    194 
    195 int
    196 main(int argc, char *argv[])
    197 {
    198 	int c;
    199 
    200 	setprogname(argv[0]);
    201 
    202 	ERR_ZERO(&msgset);
    203 	while ((c = getopt(argc, argv, "abceghmprstuvwyzA:FPR:STX:")) != -1) {
    204 		switch (c) {
    205 		case 'a':	aflag++;	break;
    206 		case 'b':	bflag = true;	break;
    207 		case 'c':	cflag = true;	break;
    208 		case 'e':	eflag = true;	break;
    209 		case 'F':	Fflag = true;	break;
    210 		case 'g':	allow_gcc = true;	break;
    211 		case 'h':	hflag = true;	break;
    212 		case 'p':	pflag = true;	break;
    213 		case 'P':	Pflag = true;	break;
    214 		case 'r':	rflag = true;	break;
    215 		case 's':
    216 			allow_trad = false;
    217 			allow_c90 = true;
    218 			allow_c99 = false;
    219 			allow_c11 = false;
    220 			break;
    221 		case 'S':
    222 			allow_trad = false;
    223 			allow_c90 = true;
    224 			allow_c99 = true;
    225 			allow_c11 = false;
    226 			break;
    227 		case 'T':	Tflag = true;	break;
    228 		case 't':
    229 			allow_trad = true;
    230 			allow_c90 = false;
    231 			allow_c99 = false;
    232 			allow_c11 = false;
    233 			break;
    234 		case 'u':	uflag = false;	break;
    235 		case 'w':	wflag = true;	break;
    236 		case 'v':	vflag = false;	break;
    237 		case 'y':	yflag = true;	break;
    238 		case 'z':	zflag = false;	break;
    239 
    240 		case 'A':
    241 			if (strcmp(optarg, "c11") == 0) {
    242 				allow_trad = false;
    243 				allow_c90 = true;
    244 				allow_c99 = true;
    245 				allow_c11 = true;
    246 			} else
    247 				usage();
    248 			break;
    249 
    250 		case 'm':
    251 			msglist();
    252 			return 0;
    253 
    254 		case 'R':
    255 			add_directory_replacement(optarg);
    256 			break;
    257 
    258 		case 'X':
    259 			suppress_messages(optarg);
    260 			break;
    261 		default:
    262 			usage();
    263 		}
    264 	}
    265 	argc -= optind;
    266 	argv += optind;
    267 
    268 	if (argc != 2)
    269 		usage();
    270 
    271 
    272 	/* initialize output */
    273 	outopen(argv[1]);
    274 
    275 #ifdef DEBUG
    276 	setvbuf(stdout, NULL, _IONBF, 0);
    277 #endif
    278 #ifdef YYDEBUG
    279 	if (yflag)
    280 		yydebug = 1;
    281 #endif
    282 
    283 	(void)signal(SIGFPE, sigfpe);
    284 	initmem();
    285 	initdecl();
    286 	initscan();
    287 
    288 	if (allow_gcc && allow_c90) {
    289 		if ((yyin = gcc_builtins()) == NULL)
    290 			err(1, "cannot open builtins");
    291 		curr_pos.p_file = "<gcc-builtins>";
    292 		curr_pos.p_line = 0;
    293 		lex_next_line();
    294 		yyparse();
    295 		(void)fclose(yyin);
    296 	}
    297 
    298 	/* open the input file */
    299 	if ((yyin = fopen(argv[0], "r")) == NULL)
    300 		err(1, "cannot open '%s'", argv[0]);
    301 	curr_pos.p_file = argv[0];
    302 	curr_pos.p_line = 0;
    303 	lex_next_line();
    304 	yyparse();
    305 	(void)fclose(yyin);
    306 
    307 	/* Following warnings cannot be suppressed by LINTED */
    308 	lwarn = LWARN_ALL;
    309 	debug_step("main lwarn = %d", lwarn);
    310 
    311 	check_global_symbols();
    312 
    313 	outclose();
    314 
    315 	return nerr != 0 ? 1 : 0;
    316 }
    317 
    318 static void __attribute__((noreturn))
    319 usage(void)
    320 {
    321 	(void)fprintf(stderr,
    322 	    "usage: %s [-abceghmprstuvwyzFPST] [-Ac11] [-R old=new]\n"
    323 	    "       %*s [-X <id>[,<id>]...] src dest\n",
    324 	    getprogname(), (int)strlen(getprogname()), "");
    325 	exit(1);
    326 }
    327 
    328 void __attribute__((noreturn))
    329 norecover(void)
    330 {
    331 	/* cannot recover from previous errors */
    332 	error(224);
    333 	exit(1);
    334 }
    335