Home | History | Annotate | Line # | Download | only in lint1
main1.c revision 1.41
      1 /*	$NetBSD: main1.c,v 1.41 2021/03/28 15:36:37 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.41 2021/03/28 15:36:37 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 /* Print various debug information. */
     72 bool	dflag;
     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 const char builtins[] =
    134     "int __builtin_isinf(long double);\n"
    135     "int __builtin_isnan(long double);\n"
    136     "int __builtin_copysign(long double, long double);\n"
    137 ;
    138 static const size_t builtinlen = sizeof builtins - 1;
    139 
    140 static FILE *
    141 gcc_builtins(void)
    142 {
    143 #if HAVE_NBTOOL_CONFIG_H
    144 	char template[] = "/tmp/lint.XXXXXX";
    145 	int fd;
    146 	FILE *fp;
    147 	if ((fd = mkstemp(template)) == -1)
    148 		return NULL;
    149 	(void)unlink(template);
    150 	if ((fp = fdopen(fd, "r+")) == NULL) {
    151 		close(fd);
    152 		return NULL;
    153 	}
    154 	if (fwrite(builtins, 1, builtinlen, fp) != builtinlen) {
    155 		fclose(fp);
    156 		return NULL;
    157 	}
    158 	rewind(fp);
    159 	return fp;
    160 #else
    161 	return fmemopen(__UNCONST(builtins), builtinlen, "r");
    162 #endif
    163 }
    164 
    165 /*ARGSUSED*/
    166 static void
    167 sigfpe(int s)
    168 {
    169 	fpe = 1;
    170 }
    171 
    172 int
    173 main(int argc, char *argv[])
    174 {
    175 	int	c;
    176 	char	*ptr;
    177 
    178 	setprogname(argv[0]);
    179 
    180 	ERR_ZERO(&msgset);
    181 	while ((c = getopt(argc, argv, "abcdeghmprstuvwyzFPR:STX:")) != -1) {
    182 		switch (c) {
    183 		case 'a':	aflag++;	break;
    184 		case 'b':	bflag = true;	break;
    185 		case 'c':	cflag = true;	break;
    186 		case 'd':	dflag = true;	break;
    187 		case 'e':	eflag = true;	break;
    188 		case 'F':	Fflag = true;	break;
    189 		case 'g':	gflag = true;	break;
    190 		case 'h':	hflag = true;	break;
    191 		case 'p':	pflag = true;	break;
    192 		case 'P':	Pflag = true;	break;
    193 		case 'r':	rflag = true;	break;
    194 		case 's':	sflag = true;	break;
    195 		case 'S':	Sflag = true;	break;
    196 		case 'T':	Tflag = true;	break;
    197 		case 't':	tflag = true;	break;
    198 		case 'u':	uflag = false;	break;
    199 		case 'w':	wflag = true;	break;
    200 		case 'v':	vflag = false;	break;
    201 		case 'y':	yflag = true;	break;
    202 		case 'z':	zflag = false;	break;
    203 
    204 		case 'm':
    205 			msglist();
    206 			return 0;
    207 
    208 		case 'R':
    209 			add_directory_replacement(optarg);
    210 			break;
    211 
    212 		case 'X':
    213 			for (ptr = strtok(optarg, ","); ptr != NULL;
    214 			    ptr = strtok(NULL, ",")) {
    215 				char *eptr;
    216 				long msg;
    217 
    218 				errno = 0;
    219 				msg = strtol(ptr, &eptr, 0);
    220 				if ((msg == TARG_LONG_MIN || msg == TARG_LONG_MAX) &&
    221 				    errno == ERANGE)
    222 				    err(1, "invalid error message id '%s'",
    223 					ptr);
    224 				if (*eptr != '\0' || ptr == eptr || msg < 0 ||
    225 				    msg >= ERR_SETSIZE)
    226 					errx(1, "invalid error message id '%s'",
    227 					    ptr);
    228 				ERR_SET(msg, &msgset);
    229 			}
    230 			break;
    231 		case '?':
    232 		default:
    233 			usage();
    234 			break;
    235 		}
    236 	}
    237 	argc -= optind;
    238 	argv += optind;
    239 
    240 	if (argc != 2)
    241 		usage();
    242 
    243 
    244 	/* initialize output */
    245 	outopen(argv[1]);
    246 
    247 #ifdef YYDEBUG
    248 	if (yflag)
    249 		yydebug = 1;
    250 #endif
    251 
    252 	(void)signal(SIGFPE, sigfpe);
    253 	initmem();
    254 	initdecl();
    255 	initscan();
    256 
    257 	if (gflag) {
    258 		if ((yyin = gcc_builtins()) == NULL)
    259 			err(1, "cannot open builtins");
    260 		yyparse();
    261 		fclose(yyin);
    262 	}
    263 
    264 	/* open the input file */
    265 	if ((yyin = fopen(argv[0], "r")) == NULL)
    266 		err(1, "cannot open '%s'", argv[0]);
    267 	yyparse();
    268 	fclose(yyin);
    269 
    270 	/* Following warnings cannot be suppressed by LINTED */
    271 	lwarn = LWARN_ALL;
    272 #ifdef DEBUG
    273 	printf("%s, %d: lwarn = %d\n", curr_pos.p_file, curr_pos.p_line, lwarn);
    274 #endif
    275 
    276 	check_global_symbols();
    277 
    278 	outclose();
    279 
    280 	return nerr != 0 ? 1 : 0;
    281 }
    282 
    283 static void
    284 usage(void)
    285 {
    286 	(void)fprintf(stderr,
    287 	    "Usage: %s [-abcdeghmprstuvwyzFST] [-X <id>[,<id>]... src dest\n",
    288 	    getprogname());
    289 	exit(1);
    290 }
    291 
    292 void
    293 norecover(void)
    294 {
    295 	/* cannot recover from previous errors */
    296 	error(224);
    297 	exit(1);
    298 }
    299