Home | History | Annotate | Line # | Download | only in lint1
main1.c revision 1.55
      1 /*	$NetBSD: main1.c,v 1.55 2021/08/17 21:19:02 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.55 2021/08/17 21:19:02 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 		close(fd);
    151 		return NULL;
    152 	}
    153 	if (fwrite(builtins, 1, builtins_len, fp) != builtins_len) {
    154 		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 int
    172 main(int argc, char *argv[])
    173 {
    174 	int	c;
    175 	char	*ptr;
    176 
    177 	setprogname(argv[0]);
    178 
    179 	ERR_ZERO(&msgset);
    180 	while ((c = getopt(argc, argv, "abceghmprstuvwyzA:FPR:STX:")) != -1) {
    181 		switch (c) {
    182 		case 'a':	aflag++;	break;
    183 		case 'b':	bflag = true;	break;
    184 		case 'c':	cflag = true;	break;
    185 		case 'e':	eflag = true;	break;
    186 		case 'F':	Fflag = true;	break;
    187 		case 'g':	gflag = true;	break;
    188 		case 'h':	hflag = true;	break;
    189 		case 'p':	pflag = true;	break;
    190 		case 'P':	Pflag = true;	break;
    191 		case 'r':	rflag = true;	break;
    192 		case 's':	sflag = true;	break;
    193 		case 'S':	Sflag = true;	break;
    194 		case 'T':	Tflag = true;	break;
    195 		case 't':	tflag = true;	break;
    196 		case 'u':	uflag = false;	break;
    197 		case 'w':	wflag = true;	break;
    198 		case 'v':	vflag = false;	break;
    199 		case 'y':	yflag = true;	break;
    200 		case 'z':	zflag = false;	break;
    201 
    202 		case 'A':
    203 			if (strcmp(optarg, "c11") == 0) {
    204 				c11flag = true;
    205 				Sflag = true;
    206 				sflag = false;
    207 			} else
    208 				usage();
    209 			break;
    210 
    211 		case 'm':
    212 			msglist();
    213 			return 0;
    214 
    215 		case 'R':
    216 			add_directory_replacement(optarg);
    217 			break;
    218 
    219 		case 'X':
    220 			for (ptr = strtok(optarg, ","); ptr != NULL;
    221 			    ptr = strtok(NULL, ",")) {
    222 				char *eptr;
    223 				long msg;
    224 
    225 				errno = 0;
    226 				msg = strtol(ptr, &eptr, 0);
    227 				if ((msg == TARG_LONG_MIN || msg == TARG_LONG_MAX) &&
    228 				    errno == ERANGE)
    229 				    err(1, "invalid error message id '%s'",
    230 					ptr);
    231 				if (*eptr != '\0' || ptr == eptr || msg < 0 ||
    232 				    msg >= ERR_SETSIZE)
    233 					errx(1, "invalid error message id '%s'",
    234 					    ptr);
    235 				ERR_SET(msg, &msgset);
    236 			}
    237 			break;
    238 		default:
    239 			usage();
    240 		}
    241 	}
    242 	argc -= optind;
    243 	argv += optind;
    244 
    245 	if (argc != 2)
    246 		usage();
    247 
    248 
    249 	/* initialize output */
    250 	outopen(argv[1]);
    251 
    252 #ifdef DEBUG
    253 	setvbuf(stdout, NULL, _IONBF, 0);
    254 #endif
    255 #ifdef YYDEBUG
    256 	if (yflag)
    257 		yydebug = 1;
    258 #endif
    259 
    260 	(void)signal(SIGFPE, sigfpe);
    261 	initmem();
    262 	initdecl();
    263 	initscan();
    264 
    265 	if (gflag && !tflag) {
    266 		if ((yyin = gcc_builtins()) == NULL)
    267 			err(1, "cannot open builtins");
    268 		yyparse();
    269 		fclose(yyin);
    270 	}
    271 
    272 	/* open the input file */
    273 	if ((yyin = fopen(argv[0], "r")) == NULL)
    274 		err(1, "cannot open '%s'", argv[0]);
    275 	yyparse();
    276 	fclose(yyin);
    277 
    278 	/* Following warnings cannot be suppressed by LINTED */
    279 	lwarn = LWARN_ALL;
    280 	debug_step("main lwarn = %d", lwarn);
    281 
    282 	check_global_symbols();
    283 
    284 	outclose();
    285 
    286 	return nerr != 0 ? 1 : 0;
    287 }
    288 
    289 static void __attribute__((noreturn))
    290 usage(void)
    291 {
    292 	(void)fprintf(stderr,
    293 	    "usage: %s [-abceghmprstuvwyzFPST] [-Ac11] [-R old=new]\n"
    294 	    "       %*s [-X <id>[,<id>]...] src dest\n",
    295 	    getprogname(), (int)strlen(getprogname()), "");
    296 	exit(1);
    297 }
    298 
    299 void __attribute__((noreturn))
    300 norecover(void)
    301 {
    302 	/* cannot recover from previous errors */
    303 	error(224);
    304 	exit(1);
    305 }
    306