Home | History | Annotate | Line # | Download | only in lint1
main1.c revision 1.26
      1 /*	$NetBSD: main1.c,v 1.26 2016/12/24 17:43:45 christos 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.26 2016/12/24 17:43:45 christos Exp $");
     41 #endif
     42 
     43 #include <sys/types.h>
     44 #include <stdio.h>
     45 #include <string.h>
     46 #include <stdlib.h>
     47 #include <unistd.h>
     48 #include <errno.h>
     49 #include <limits.h>
     50 #include <signal.h>
     51 
     52 #include "lint1.h"
     53 
     54 /* set yydebug to 1*/
     55 int	yflag;
     56 
     57 /*
     58  * Print warnings if an assignment of an integertype to another integertype
     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 int	bflag;
     67 
     68 /* Print warnings for pointer casts. */
     69 int	cflag;
     70 
     71 /* Print various debug information. */
     72 int	dflag;
     73 
     74 /* Perform stricter checking of enum types and operations on enum types. */
     75 int	eflag;
     76 
     77 /* Print complete pathnames, not only the basename. */
     78 int	Fflag;
     79 
     80 /* Enable some extensions of gcc */
     81 int	gflag;
     82 
     83 /* Treat warnings as errors */
     84 int	wflag;
     85 
     86 /*
     87  * Apply a number of heuristic tests to attempt to intuit bugs, improve
     88  * style, and reduce waste.
     89  */
     90 int	hflag;
     91 
     92 /* Attempt to check portability to other dialects of C. */
     93 int	pflag;
     94 
     95 /*
     96  * In case of redeclarations/redefinitions print the location of the
     97  * previous declaration/definition.
     98  */
     99 int	rflag;
    100 
    101 /* Strict ANSI C mode. */
    102 int	sflag;
    103 
    104 /* Traditional C mode. */
    105 int	tflag;
    106 
    107 /* Enable C9X extensions */
    108 int	Sflag;
    109 
    110 /* Picky flag */
    111 int	Pflag;
    112 
    113 /*
    114  * Complain about functions and external variables used and not defined,
    115  * or defined and not used.
    116  */
    117 int	uflag = 1;
    118 
    119 /* Complain about unused function arguments. */
    120 int	vflag = 1;
    121 
    122 /* Complain about structures which are never defined. */
    123 int	zflag = 1;
    124 
    125 err_set	msgset;
    126 
    127 sig_atomic_t fpe;
    128 
    129 static	void	usage(void);
    130 
    131 static const char builtins[] =
    132     "int __builtin_isinf(long double);\n"
    133     "int __builtin_isnan(long double);\n"
    134     "int __builtin_copysign(long double, long double);\n"
    135 ;
    136 static size_t builtinlen = sizeof(builtins) - 1;
    137 
    138 static FILE *
    139 bltin(void)
    140 {
    141 #if HAVE_NBTOOL_CONFIG_H
    142 	char template[] = "/tmp/lint.XXXXXX";
    143 	int fd;
    144 	FILE *fp;
    145 	if ((fd = mkstemp(template)) == -1)
    146 		return NULL;
    147 	(void)unlink(template);
    148 	if ((fp = fdopen(fd, "r+")) == NULL) {
    149 		close(fd);
    150 		return NULL;
    151 	}
    152 	if (fwrite(builtins, 1, builtinlen, fp) != builtinlen) {
    153 		fclose(fp);
    154 		return NULL;
    155 	}
    156 	rewind(fp);
    157 	return fp;
    158 #else
    159 	return fmemopen(__UNCONST(builtins), builtinlen, "r");
    160 #endif
    161 }
    162 
    163 /*ARGSUSED*/
    164 static void
    165 sigfpe(int s)
    166 {
    167 	fpe = 1;
    168 }
    169 
    170 int
    171 main(int argc, char *argv[])
    172 {
    173 	int	c;
    174 	char	*ptr;
    175 
    176 	setprogname(argv[0]);
    177 
    178 	ERR_ZERO(&msgset);
    179 	while ((c = getopt(argc, argv, "abcdeghmprstuvwyzFPR:SX:")) != -1) {
    180 		switch (c) {
    181 		case 'a':	aflag++;	break;
    182 		case 'b':	bflag = 1;	break;
    183 		case 'c':	cflag = 1;	break;
    184 		case 'd':	dflag = 1;	break;
    185 		case 'e':	eflag = 1;	break;
    186 		case 'F':	Fflag = 1;	break;
    187 		case 'g':	gflag = 1;	break;
    188 		case 'h':	hflag = 1;	break;
    189 		case 'p':	pflag = 1;	break;
    190 		case 'P':	Pflag = 1;	break;
    191 		case 'r':	rflag = 1;	break;
    192 		case 's':	sflag = 1;	break;
    193 		case 'S':	Sflag = 1;	break;
    194 		case 't':	tflag = 1;	break;
    195 		case 'u':	uflag = 0;	break;
    196 		case 'w':	wflag = 1;	break;
    197 		case 'v':	vflag = 0;	break;
    198 		case 'y':	yflag = 1;	break;
    199 		case 'z':	zflag = 0;	break;
    200 
    201 		case 'm':
    202 			msglist();
    203 			return(0);
    204 
    205 		case 'R':
    206 			fnaddreplsrcdir(optarg);
    207 			break;
    208 
    209 		case 'X':
    210 			for (ptr = strtok(optarg, ","); ptr;
    211 			    ptr = strtok(NULL, ",")) {
    212 				char *eptr;
    213 				long msg;
    214 
    215 				errno = 0;
    216 				msg = strtol(ptr, &eptr, 0);
    217 				if ((msg == TARG_LONG_MIN || msg == TARG_LONG_MAX) &&
    218 				    errno == ERANGE)
    219 				    err(1, "invalid error message id '%s'",
    220 					ptr);
    221 				if (*eptr || ptr == eptr || msg < 0 ||
    222 				    msg >= ERR_SETSIZE)
    223 					errx(1, "invalid error message id '%s'",
    224 					    ptr);
    225 				ERR_SET(msg, &msgset);
    226 			}
    227 			break;
    228 		case '?':
    229 		default:
    230 			usage();
    231 			break;
    232 		}
    233 	}
    234 	argc -= optind;
    235 	argv += optind;
    236 
    237 	if (argc != 2)
    238 		usage();
    239 
    240 
    241 	/* initialize output */
    242 	outopen(argv[1]);
    243 
    244 	if (yflag)
    245 		yydebug = 1;
    246 
    247 	(void)signal(SIGFPE, sigfpe);
    248 	initmem();
    249 	initdecl();
    250 	initscan();
    251 	initmtab();
    252 
    253 	if ((yyin = bltin()) == NULL)
    254 		err(1, "cannot open builtins");
    255 	yyparse();
    256 	fclose(yyin);
    257 
    258 	/* open the input file */
    259 	if ((yyin = fopen(argv[0], "r")) == NULL)
    260 		err(1, "cannot open '%s'", argv[0]);
    261 	yyparse();
    262 	fclose(yyin);
    263 
    264 	/* Following warnings cannot be suppressed by LINTED */
    265 	lwarn = LWARN_ALL;
    266 #ifdef DEBUG
    267 	printf("%s, %d: lwarn = %d\n", curr_pos.p_file, curr_pos.p_line, lwarn);
    268 #endif
    269 
    270 	chkglsyms();
    271 
    272 	outclose();
    273 
    274 	return (nerr != 0);
    275 }
    276 
    277 static void
    278 usage(void)
    279 {
    280 	(void)fprintf(stderr,
    281 	    "Usage: %s [-abcdeghmprstuvwyzFS] [-X <id>[,<id>]... src dest\n",
    282 	    getprogname());
    283 	exit(1);
    284 }
    285 
    286 void
    287 norecover(void)
    288 {
    289 	/* cannot recover from previous errors */
    290 	error(224);
    291 	exit(1);
    292 }
    293