Home | History | Annotate | Line # | Download | only in lint1
main1.c revision 1.35
      1  1.35    rillig /*	$NetBSD: main1.c,v 1.35 2021/01/12 21:48:10 rillig Exp $	*/
      2   1.2       cgd 
      3   1.1       cgd /*
      4   1.1       cgd  * Copyright (c) 1994, 1995 Jochen Pohl
      5   1.1       cgd  * All Rights Reserved.
      6   1.1       cgd  *
      7   1.1       cgd  * Redistribution and use in source and binary forms, with or without
      8   1.1       cgd  * modification, are permitted provided that the following conditions
      9   1.1       cgd  * are met:
     10   1.1       cgd  * 1. Redistributions of source code must retain the above copyright
     11   1.1       cgd  *    notice, this list of conditions and the following disclaimer.
     12   1.1       cgd  * 2. Redistributions in binary form must reproduce the above copyright
     13   1.1       cgd  *    notice, this list of conditions and the following disclaimer in the
     14   1.1       cgd  *    documentation and/or other materials provided with the distribution.
     15   1.1       cgd  * 3. All advertising materials mentioning features or use of this software
     16   1.1       cgd  *    must display the following acknowledgement:
     17   1.1       cgd  *      This product includes software developed by Jochen Pohl for
     18   1.1       cgd  *	The NetBSD Project.
     19   1.1       cgd  * 4. The name of the author may not be used to endorse or promote products
     20   1.1       cgd  *    derived from this software without specific prior written permission.
     21   1.1       cgd  *
     22   1.1       cgd  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     23   1.1       cgd  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     24   1.1       cgd  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     25   1.1       cgd  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     26   1.1       cgd  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     27   1.1       cgd  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     28   1.1       cgd  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     29   1.1       cgd  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     30   1.1       cgd  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     31   1.1       cgd  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     32   1.1       cgd  */
     33   1.1       cgd 
     34  1.15       jmc #if HAVE_NBTOOL_CONFIG_H
     35  1.15       jmc #include "nbtool_config.h"
     36  1.15       jmc #endif
     37  1.15       jmc 
     38   1.4  christos #include <sys/cdefs.h>
     39  1.12        tv #if defined(__RCSID) && !defined(lint)
     40  1.35    rillig __RCSID("$NetBSD: main1.c,v 1.35 2021/01/12 21:48:10 rillig Exp $");
     41   1.1       cgd #endif
     42   1.1       cgd 
     43   1.5  christos #include <sys/types.h>
     44  1.29    rillig #include <errno.h>
     45  1.29    rillig #include <limits.h>
     46  1.29    rillig #include <signal.h>
     47   1.1       cgd #include <stdio.h>
     48  1.29    rillig #include <stdlib.h>
     49   1.5  christos #include <string.h>
     50   1.1       cgd #include <unistd.h>
     51   1.1       cgd 
     52   1.1       cgd #include "lint1.h"
     53   1.1       cgd 
     54   1.1       cgd /* set yydebug to 1*/
     55   1.1       cgd int	yflag;
     56   1.1       cgd 
     57   1.1       cgd /*
     58  1.32    rillig  * Print warnings if an assignment of an integer type to another integer type
     59  1.16     perry  * causes an implicit narrowing conversion. If aflag is 1, these warnings
     60   1.1       cgd  * are printed only if the source type is at least as wide as long. If aflag
     61   1.9       wiz  * is greater than 1, they are always printed.
     62   1.1       cgd  */
     63   1.1       cgd int	aflag;
     64   1.1       cgd 
     65   1.1       cgd /* Print a warning if a break statement cannot be reached. */
     66   1.1       cgd int	bflag;
     67   1.1       cgd 
     68   1.1       cgd /* Print warnings for pointer casts. */
     69   1.1       cgd int	cflag;
     70   1.1       cgd 
     71   1.1       cgd /* Print various debug information. */
     72   1.1       cgd int	dflag;
     73   1.1       cgd 
     74   1.1       cgd /* Perform stricter checking of enum types and operations on enum types. */
     75   1.1       cgd int	eflag;
     76   1.1       cgd 
     77   1.1       cgd /* Print complete pathnames, not only the basename. */
     78   1.1       cgd int	Fflag;
     79   1.1       cgd 
     80   1.1       cgd /* Enable some extensions of gcc */
     81   1.1       cgd int	gflag;
     82   1.1       cgd 
     83   1.5  christos /* Treat warnings as errors */
     84   1.5  christos int	wflag;
     85   1.5  christos 
     86   1.1       cgd /*
     87   1.1       cgd  * Apply a number of heuristic tests to attempt to intuit bugs, improve
     88   1.1       cgd  * style, and reduce waste.
     89   1.1       cgd  */
     90   1.1       cgd int	hflag;
     91   1.1       cgd 
     92   1.1       cgd /* Attempt to check portability to other dialects of C. */
     93   1.1       cgd int	pflag;
     94   1.1       cgd 
     95   1.1       cgd /*
     96   1.1       cgd  * In case of redeclarations/redefinitions print the location of the
     97   1.1       cgd  * previous declaration/definition.
     98   1.1       cgd  */
     99   1.1       cgd int	rflag;
    100   1.1       cgd 
    101   1.1       cgd /* Strict ANSI C mode. */
    102   1.1       cgd int	sflag;
    103   1.1       cgd 
    104  1.34    rillig bool	Tflag;
    105  1.34    rillig 
    106   1.1       cgd /* Traditional C mode. */
    107   1.1       cgd int	tflag;
    108   1.1       cgd 
    109  1.13  christos /* Enable C9X extensions */
    110  1.13  christos int	Sflag;
    111  1.19  christos 
    112  1.19  christos /* Picky flag */
    113  1.19  christos int	Pflag;
    114  1.19  christos 
    115   1.1       cgd /*
    116   1.1       cgd  * Complain about functions and external variables used and not defined,
    117   1.1       cgd  * or defined and not used.
    118   1.1       cgd  */
    119   1.1       cgd int	uflag = 1;
    120   1.1       cgd 
    121   1.1       cgd /* Complain about unused function arguments. */
    122   1.1       cgd int	vflag = 1;
    123   1.1       cgd 
    124   1.1       cgd /* Complain about structures which are never defined. */
    125   1.1       cgd int	zflag = 1;
    126   1.1       cgd 
    127  1.10  augustss err_set	msgset;
    128   1.5  christos 
    129  1.18  christos sig_atomic_t fpe;
    130  1.18  christos 
    131   1.8     lukem static	void	usage(void);
    132   1.4  christos 
    133  1.22  christos static const char builtins[] =
    134  1.22  christos     "int __builtin_isinf(long double);\n"
    135  1.22  christos     "int __builtin_isnan(long double);\n"
    136  1.22  christos     "int __builtin_copysign(long double, long double);\n"
    137  1.22  christos ;
    138  1.25  christos static size_t builtinlen = sizeof(builtins) - 1;
    139  1.25  christos 
    140  1.25  christos static FILE *
    141  1.25  christos bltin(void)
    142  1.25  christos {
    143  1.25  christos #if HAVE_NBTOOL_CONFIG_H
    144  1.25  christos 	char template[] = "/tmp/lint.XXXXXX";
    145  1.25  christos 	int fd;
    146  1.25  christos 	FILE *fp;
    147  1.25  christos 	if ((fd = mkstemp(template)) == -1)
    148  1.25  christos 		return NULL;
    149  1.25  christos 	(void)unlink(template);
    150  1.25  christos 	if ((fp = fdopen(fd, "r+")) == NULL) {
    151  1.25  christos 		close(fd);
    152  1.25  christos 		return NULL;
    153  1.25  christos 	}
    154  1.25  christos 	if (fwrite(builtins, 1, builtinlen, fp) != builtinlen) {
    155  1.25  christos 		fclose(fp);
    156  1.25  christos 		return NULL;
    157  1.25  christos 	}
    158  1.25  christos 	rewind(fp);
    159  1.25  christos 	return fp;
    160  1.25  christos #else
    161  1.25  christos 	return fmemopen(__UNCONST(builtins), builtinlen, "r");
    162  1.23  christos #endif
    163  1.25  christos }
    164   1.1       cgd 
    165  1.18  christos /*ARGSUSED*/
    166  1.18  christos static void
    167  1.18  christos sigfpe(int s)
    168  1.18  christos {
    169  1.18  christos 	fpe = 1;
    170  1.18  christos }
    171  1.18  christos 
    172   1.1       cgd int
    173   1.8     lukem main(int argc, char *argv[])
    174   1.1       cgd {
    175   1.1       cgd 	int	c;
    176   1.5  christos 	char	*ptr;
    177   1.1       cgd 
    178  1.12        tv 	setprogname(argv[0]);
    179  1.12        tv 
    180  1.10  augustss 	ERR_ZERO(&msgset);
    181  1.34    rillig 	while ((c = getopt(argc, argv, "abcdeghmprstuvwyzFPR:STX:")) != -1) {
    182   1.1       cgd 		switch (c) {
    183   1.1       cgd 		case 'a':	aflag++;	break;
    184   1.1       cgd 		case 'b':	bflag = 1;	break;
    185   1.1       cgd 		case 'c':	cflag = 1;	break;
    186   1.1       cgd 		case 'd':	dflag = 1;	break;
    187   1.1       cgd 		case 'e':	eflag = 1;	break;
    188   1.1       cgd 		case 'F':	Fflag = 1;	break;
    189   1.1       cgd 		case 'g':	gflag = 1;	break;
    190   1.1       cgd 		case 'h':	hflag = 1;	break;
    191   1.1       cgd 		case 'p':	pflag = 1;	break;
    192  1.19  christos 		case 'P':	Pflag = 1;	break;
    193   1.1       cgd 		case 'r':	rflag = 1;	break;
    194   1.1       cgd 		case 's':	sflag = 1;	break;
    195  1.13  christos 		case 'S':	Sflag = 1;	break;
    196  1.34    rillig 		case 'T':	Tflag = true;	break;
    197   1.1       cgd 		case 't':	tflag = 1;	break;
    198   1.1       cgd 		case 'u':	uflag = 0;	break;
    199   1.5  christos 		case 'w':	wflag = 1;	break;
    200   1.1       cgd 		case 'v':	vflag = 0;	break;
    201   1.1       cgd 		case 'y':	yflag = 1;	break;
    202   1.1       cgd 		case 'z':	zflag = 0;	break;
    203   1.5  christos 
    204   1.5  christos 		case 'm':
    205   1.5  christos 			msglist();
    206  1.33    rillig 			return 0;
    207   1.5  christos 
    208  1.28    rillig 		case 'R':
    209  1.26  christos 			fnaddreplsrcdir(optarg);
    210  1.26  christos 			break;
    211  1.26  christos 
    212   1.5  christos 		case 'X':
    213   1.5  christos 			for (ptr = strtok(optarg, ","); ptr;
    214   1.5  christos 			    ptr = strtok(NULL, ",")) {
    215   1.5  christos 				char *eptr;
    216  1.14     lukem 				long msg;
    217  1.14     lukem 
    218  1.14     lukem 				errno = 0;
    219  1.14     lukem 				msg = strtol(ptr, &eptr, 0);
    220  1.20  christos 				if ((msg == TARG_LONG_MIN || msg == TARG_LONG_MAX) &&
    221   1.5  christos 				    errno == ERANGE)
    222   1.5  christos 				    err(1, "invalid error message id '%s'",
    223   1.5  christos 					ptr);
    224   1.5  christos 				if (*eptr || ptr == eptr || msg < 0 ||
    225  1.10  augustss 				    msg >= ERR_SETSIZE)
    226   1.5  christos 					errx(1, "invalid error message id '%s'",
    227   1.5  christos 					    ptr);
    228  1.10  augustss 				ERR_SET(msg, &msgset);
    229   1.5  christos 			}
    230   1.5  christos 			break;
    231   1.5  christos 		case '?':
    232   1.5  christos 		default:
    233   1.5  christos 			usage();
    234   1.5  christos 			break;
    235   1.1       cgd 		}
    236   1.1       cgd 	}
    237   1.1       cgd 	argc -= optind;
    238   1.1       cgd 	argv += optind;
    239   1.1       cgd 
    240   1.1       cgd 	if (argc != 2)
    241   1.1       cgd 		usage();
    242   1.1       cgd 
    243   1.1       cgd 
    244   1.1       cgd 	/* initialize output */
    245   1.1       cgd 	outopen(argv[1]);
    246   1.1       cgd 
    247  1.27  christos #ifdef YYDEBUG
    248   1.1       cgd 	if (yflag)
    249   1.1       cgd 		yydebug = 1;
    250  1.27  christos #endif
    251   1.1       cgd 
    252  1.18  christos 	(void)signal(SIGFPE, sigfpe);
    253   1.1       cgd 	initmem();
    254   1.1       cgd 	initdecl();
    255   1.1       cgd 	initscan();
    256   1.1       cgd 	initmtab();
    257   1.1       cgd 
    258  1.25  christos 	if ((yyin = bltin()) == NULL)
    259  1.22  christos 		err(1, "cannot open builtins");
    260  1.22  christos 	yyparse();
    261  1.25  christos 	fclose(yyin);
    262  1.25  christos 
    263  1.22  christos 	/* open the input file */
    264  1.22  christos 	if ((yyin = fopen(argv[0], "r")) == NULL)
    265  1.22  christos 		err(1, "cannot open '%s'", argv[0]);
    266   1.1       cgd 	yyparse();
    267  1.25  christos 	fclose(yyin);
    268   1.1       cgd 
    269   1.1       cgd 	/* Following warnings cannot be suppressed by LINTED */
    270  1.21  christos 	lwarn = LWARN_ALL;
    271  1.17  christos #ifdef DEBUG
    272  1.21  christos 	printf("%s, %d: lwarn = %d\n", curr_pos.p_file, curr_pos.p_line, lwarn);
    273  1.17  christos #endif
    274   1.1       cgd 
    275  1.30    rillig 	check_global_symbols();
    276   1.1       cgd 
    277   1.1       cgd 	outclose();
    278   1.1       cgd 
    279  1.31    rillig 	return nerr != 0;
    280   1.1       cgd }
    281   1.1       cgd 
    282   1.1       cgd static void
    283   1.8     lukem usage(void)
    284   1.1       cgd {
    285   1.5  christos 	(void)fprintf(stderr,
    286  1.35    rillig 	    "Usage: %s [-abcdeghmprstuvwyzFST] [-X <id>[,<id>]... src dest\n",
    287   1.6       cgd 	    getprogname());
    288   1.1       cgd 	exit(1);
    289   1.1       cgd }
    290   1.8     lukem 
    291   1.1       cgd void
    292   1.8     lukem norecover(void)
    293   1.1       cgd {
    294   1.1       cgd 	/* cannot recover from previous errors */
    295   1.1       cgd 	error(224);
    296   1.1       cgd 	exit(1);
    297   1.1       cgd }
    298