Home | History | Annotate | Line # | Download | only in lint1
ckgetopt.c revision 1.2
      1 /* $NetBSD: ckgetopt.c,v 1.2 2021/02/19 14:44:29 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.2 2021/02/19 14:44:29 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 struct {
     54 	pos_t options_pos;
     55 	char *options;
     56 	char *unhandled_options;
     57 	int while_level;
     58 	int switch_level;
     59 } ck;
     60 
     61 
     62 static bool
     63 is_getopt_call(const tnode_t *tn, char **out_options)
     64 {
     65 	if (tn == NULL)
     66 		return false;
     67 	if (tn->tn_op != NE)
     68 		return false;
     69 	if (tn->tn_left->tn_op != ASSIGN)
     70 		return false;
     71 
     72 	const tnode_t *call = tn->tn_left->tn_right;
     73 	if (call->tn_op != CALL)
     74 		return false;
     75 	if (call->tn_left->tn_op != ADDR)
     76 		return false;
     77 	if (call->tn_left->tn_left->tn_op != NAME)
     78 		return false;
     79 	if (strcmp(call->tn_left->tn_left->tn_sym->s_name, "getopt") != 0)
     80 		return false;
     81 
     82 	if (call->tn_right->tn_op != PUSH)
     83 		return false;
     84 
     85 	const tnode_t *last_arg = call->tn_right->tn_left;
     86 	if (last_arg->tn_op != CVT)
     87 		return false;
     88 	if (last_arg->tn_left->tn_op != ADDR)
     89 		return false;
     90 	if (last_arg->tn_left->tn_left->tn_op != STRING)
     91 		return false;
     92 	if (last_arg->tn_left->tn_left->tn_string->st_tspec != CHAR)
     93 		return false;
     94 
     95 	*out_options = xstrdup(
     96 	    (const char *)last_arg->tn_left->tn_left->tn_string->st_cp);
     97 	return true;
     98 }
     99 
    100 static void
    101 check_unlisted_option(char opt)
    102 {
    103 	if (opt == '?')
    104 		return;
    105 
    106 	const char *optptr = strchr(ck.options, opt);
    107 	if (optptr != NULL)
    108 		ck.unhandled_options[optptr - ck.options] = ' ';
    109 	else {
    110 		/* option '%c' should be listed in the options string */
    111 		warning(339, opt);
    112 		return;
    113 	}
    114 }
    115 
    116 static void
    117 check_unhandled_option(void)
    118 {
    119 	for (const char *opt = ck.unhandled_options; *opt != '\0'; opt++) {
    120 		if (*opt == ' ' || *opt == ':')
    121 			continue;
    122 
    123 		pos_t prev_pos = curr_pos;
    124 		curr_pos = ck.options_pos;
    125 		/* option '%c' should be handled in the switch */
    126 		warning(338, *opt);
    127 		curr_pos = prev_pos;
    128 	}
    129 }
    130 
    131 
    132 void
    133 check_getopt_begin_while(const tnode_t *tn)
    134 {
    135 	if (ck.while_level == 0 && is_getopt_call(tn, &ck.options)) {
    136 		ck.unhandled_options = xstrdup(ck.options);
    137 		ck.options_pos = curr_pos;
    138 	}
    139 	ck.while_level++;
    140 }
    141 
    142 void
    143 check_getopt_begin_switch(void)
    144 {
    145 	if (ck.while_level > 0)
    146 		ck.switch_level++;
    147 }
    148 
    149 
    150 void
    151 check_getopt_case_label(int64_t value)
    152 {
    153 	if (ck.switch_level == 1 && value == (char)value)
    154 		check_unlisted_option((char)value);
    155 }
    156 
    157 void
    158 check_getopt_end_switch(void)
    159 {
    160 	if (ck.switch_level == 0)
    161 		return;
    162 
    163 	ck.switch_level--;
    164 	if (ck.switch_level == 0)
    165 		check_unhandled_option();
    166 }
    167 
    168 void
    169 check_getopt_end_while(void)
    170 {
    171 	if (ck.while_level == 0)
    172 		return;
    173 
    174 	ck.while_level--;
    175 	if (ck.while_level != 0)
    176 		return;
    177 
    178 	free(ck.options);
    179 	free(ck.unhandled_options);
    180 	ck.options = NULL;
    181 	ck.unhandled_options = NULL;
    182 }
    183