Home | History | Annotate | Line # | Download | only in lint2
main2.c revision 1.30
      1 /*	$NetBSD: main2.c,v 1.30 2023/07/03 10:23:12 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)
     40 __RCSID("$NetBSD: main2.c,v 1.30 2023/07/03 10:23:12 rillig Exp $");
     41 #endif
     42 
     43 #include <stdio.h>
     44 #include <stdlib.h>
     45 #include <string.h>
     46 #include <unistd.h>
     47 
     48 #include "lint2.h"
     49 
     50 /* warnings for symbols which are declared but not defined or used */
     51 bool	xflag;
     52 
     53 bool	uflag;
     54 
     55 /* Create a lint library in the current directory with name libname. */
     56 bool	Cflag;
     57 const char *libname;
     58 
     59 /*
     60  * warnings for (tentative) definitions of the same name in more than
     61  * one translation unit
     62  */
     63 bool	sflag;
     64 
     65 bool	tflag;
     66 
     67 /*
     68  * If a complaint stems from a included file, print the name of the included
     69  * file instead of the name specified at the command line followed by '?'
     70  */
     71 bool	Hflag;
     72 
     73 bool	hflag;
     74 
     75 /* Print full path names, not only the last component */
     76 bool	Fflag;
     77 
     78 /*
     79  * List of libraries (from -l flag). These libraries are read after all
     80  * other input files has been read and, for Cflag, after the new lint library
     81  * has been written.
     82  */
     83 static const char **libs;
     84 
     85 static	void	usage(void) __attribute__((noreturn));
     86 
     87 static void
     88 check_name_non_const(hte_t *hte)
     89 {
     90 	check_name(hte);
     91 }
     92 
     93 int
     94 main(int argc, char *argv[])
     95 {
     96 	int c, i;
     97 	size_t len;
     98 	char *lname;
     99 
    100 	libs = xcalloc(1, sizeof(*libs));
    101 
    102 	opterr = 0;
    103 	while ((c = getopt(argc, argv, "hstxuC:HFl:")) != -1) {
    104 		switch (c) {
    105 		case 's':
    106 			sflag = true;
    107 			break;
    108 		case 't':
    109 			tflag = true;
    110 			break;
    111 		case 'u':
    112 			uflag = true;
    113 			break;
    114 		case 'x':
    115 			xflag = true;
    116 			break;
    117 		case 'C':
    118 			len = strlen(optarg);
    119 			lname = xmalloc(len + 10);
    120 			(void)sprintf(lname, "llib-l%s.ln", optarg);
    121 			libname = lname;
    122 			Cflag = true;
    123 			uflag = true;
    124 			break;
    125 		case 'H':
    126 			Hflag = true;
    127 			break;
    128 		case 'h':
    129 			hflag = true;
    130 			break;
    131 		case 'F':
    132 			Fflag = true;
    133 			break;
    134 		case 'l':
    135 			for (i = 0; libs[i] != NULL; i++)
    136 				continue;
    137 			libs = xrealloc(libs, (i + 2) * sizeof(*libs));
    138 			libs[i] = xstrdup(optarg);
    139 			libs[i + 1] = NULL;
    140 			break;
    141 		default:
    142 			usage();
    143 		}
    144 	}
    145 
    146 	argc -= optind;
    147 	argv += optind;
    148 
    149 	if (argc == 0)
    150 		usage();
    151 
    152 	symtab_init();
    153 
    154 	for (i = 0; i < argc; i++)
    155 		readfile(argv[i]);
    156 
    157 	/* write the lint library */
    158 	if (Cflag) {
    159 		symtab_forall(mkstatic);
    160 		outlib(libname);
    161 	}
    162 
    163 	/* read additional libraries */
    164 	for (i = 0; libs[i] != NULL; i++)
    165 		readfile(libs[i]);
    166 
    167 	symtab_forall(mkstatic);
    168 
    169 	mark_main_as_used();
    170 
    171 	/* perform all tests */
    172 	symtab_forall_sorted(check_name_non_const);
    173 
    174 	return 0;
    175 }
    176 
    177 static void
    178 usage(void)
    179 {
    180 	(void)fprintf(stderr,
    181 		      "usage: lint2 -hpstxuHFT -Clib -l lib ... src1 ...\n");
    182 	exit(1);
    183 }
    184