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