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