Home | History | Annotate | Line # | Download | only in lint1
ckgetopt.c revision 1.12
      1 /* $NetBSD: ckgetopt.c,v 1.12 2021/10/09 14:22:42 rillig Exp $ */
      2 
      3 /*-
      4  * Copyright (c) 2021 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Roland Illig <rillig (at) NetBSD.org>.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  * POSSIBILITY OF SUCH DAMAGE.
     30  */
     31 
     32 #if HAVE_NBTOOL_CONFIG_H
     33 #include "nbtool_config.h"
     34 #endif
     35 
     36 #include <sys/cdefs.h>
     37 #if defined(__RCSID) && !defined(lint)
     38 __RCSID("$NetBSD: ckgetopt.c,v 1.12 2021/10/09 14:22:42 rillig Exp $");
     39 #endif
     40 
     41 #include <stdbool.h>
     42 #include <stdlib.h>
     43 #include <string.h>
     44 
     45 #include "lint1.h"
     46 
     47 /*
     48  * In a typical while loop for parsing getopt options, ensure that each
     49  * option from the options string is handled, and that each handled option
     50  * is listed in the options string.
     51  */
     52 
     53 static struct {
     54 	/*
     55 	 * 0	means outside a while loop with a getopt call.
     56 	 * 1	means directly inside a while loop with a getopt call.
     57 	 * > 1	means in a nested while loop; this is used for finishing the
     58 	 *	check at the correct point.
     59 	 */
     60 	int while_level;
     61 
     62 	/*
     63 	 * The options string from the getopt call.  Whenever an option is
     64 	 * handled by a case label, it is set to ' '.  In the end, only ' '
     65 	 * and ':' should remain.
     66 	 */
     67 	pos_t options_pos;
     68 	char *options;
     69 
     70 	/*
     71 	 * The nesting level of switch statements, is only modified if
     72 	 * while_level > 0.  Only the case labels at switch_level == 1 are
     73 	 * relevant, all nested case labels are ignored.
     74 	 */
     75 	int switch_level;
     76 } ck;
     77 
     78 #define NEED(cond) \
     79 	do {				\
     80 		if (!(cond))		\
     81 			return false;	\
     82 	} while (false)
     83 
     84 /* Return whether tn has the form 'getopt(argc, argv, "literal") != -1'. */
     85 static bool
     86 is_getopt_condition(const tnode_t *tn, char **out_options)
     87 {
     88 	NEED(tn != NULL);
     89 	NEED(tn->tn_op == NE);
     90 	NEED(tn->tn_left->tn_op == ASSIGN);
     91 
     92 	const tnode_t *call = tn->tn_left->tn_right;
     93 	NEED(call->tn_op == CALL);
     94 	NEED(call->tn_left->tn_op == ADDR);
     95 	NEED(call->tn_left->tn_left->tn_op == NAME);
     96 	NEED(strcmp(call->tn_left->tn_left->tn_sym->s_name, "getopt") == 0);
     97 
     98 	NEED(call->tn_right->tn_op == PUSH);
     99 
    100 	const tnode_t *last_arg = call->tn_right->tn_left;
    101 	NEED(last_arg->tn_op == CVT);
    102 	NEED(last_arg->tn_left->tn_op == ADDR);
    103 	NEED(last_arg->tn_left->tn_left->tn_op == STRING);
    104 	NEED(last_arg->tn_left->tn_left->tn_string->st_tspec == CHAR);
    105 
    106 	*out_options = xstrdup(
    107 	    (const char *)last_arg->tn_left->tn_left->tn_string->st_cp);
    108 	return true;
    109 }
    110 
    111 static void
    112 check_unlisted_option(char opt)
    113 {
    114 	lint_assert(ck.options != NULL);
    115 
    116 	if (opt == ':' && ck.options[0] != ':')
    117 		goto warn;
    118 
    119 	char *optptr = strchr(ck.options, opt);
    120 	if (optptr != NULL)
    121 		*optptr = ' ';
    122 	else if (opt != '?') {
    123 	warn:
    124 		/* option '%c' should be listed in the options string */
    125 		warning(339, opt);
    126 	}
    127 }
    128 
    129 static void
    130 check_unhandled_option(void)
    131 {
    132 	lint_assert(ck.options != NULL);
    133 
    134 	for (const char *opt = ck.options; *opt != '\0'; opt++) {
    135 		if (*opt == ' ' || *opt == ':')
    136 			continue;
    137 
    138 		/* option '%c' should be handled in the switch */
    139 		warning_at(338, &ck.options_pos, *opt);
    140 	}
    141 }
    142 
    143 
    144 void
    145 check_getopt_begin_while(const tnode_t *tn)
    146 {
    147 	if (ck.while_level == 0) {
    148 		if (!is_getopt_condition(tn, &ck.options))
    149 			return;
    150 		ck.options_pos = curr_pos;
    151 	}
    152 	ck.while_level++;
    153 }
    154 
    155 void
    156 check_getopt_begin_switch(void)
    157 {
    158 	if (ck.while_level > 0)
    159 		ck.switch_level++;
    160 }
    161 
    162 void
    163 check_getopt_case_label(int64_t value)
    164 {
    165 	if (ck.switch_level == 1 && value == (char)value)
    166 		check_unlisted_option((char)value);
    167 }
    168 
    169 void
    170 check_getopt_end_switch(void)
    171 {
    172 	if (ck.switch_level == 0)
    173 		return;
    174 
    175 	ck.switch_level--;
    176 	if (ck.switch_level == 0)
    177 		check_unhandled_option();
    178 }
    179 
    180 void
    181 check_getopt_end_while(void)
    182 {
    183 	if (ck.while_level == 0)
    184 		return;
    185 
    186 	ck.while_level--;
    187 	if (ck.while_level != 0)
    188 		return;
    189 
    190 	free(ck.options);
    191 	ck.options = NULL;
    192 }
    193