Home | History | Annotate | Line # | Download | only in lint1
ckgetopt.c revision 1.3
      1 /* $NetBSD: ckgetopt.c,v 1.3 2021/02/20 01:18:02 christos 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.3 2021/02/20 01:18:02 christos 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 == '?' || ck.options == NULL)
    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 	if (ck.unhandled_options == NULL)
    120 		return;
    121 
    122 	for (const char *opt = ck.unhandled_options; *opt != '\0'; opt++) {
    123 		if (*opt == ' ' || *opt == ':')
    124 			continue;
    125 
    126 		pos_t prev_pos = curr_pos;
    127 		curr_pos = ck.options_pos;
    128 		/* option '%c' should be handled in the switch */
    129 		warning(338, *opt);
    130 		curr_pos = prev_pos;
    131 	}
    132 }
    133 
    134 
    135 void
    136 check_getopt_begin_while(const tnode_t *tn)
    137 {
    138 	if (ck.while_level == 0 && is_getopt_call(tn, &ck.options)) {
    139 		ck.unhandled_options = xstrdup(ck.options);
    140 		ck.options_pos = curr_pos;
    141 	}
    142 	ck.while_level++;
    143 }
    144 
    145 void
    146 check_getopt_begin_switch(void)
    147 {
    148 	if (ck.while_level > 0)
    149 		ck.switch_level++;
    150 }
    151 
    152 
    153 void
    154 check_getopt_case_label(int64_t value)
    155 {
    156 	if (ck.switch_level == 1 && value == (char)value)
    157 		check_unlisted_option((char)value);
    158 }
    159 
    160 void
    161 check_getopt_end_switch(void)
    162 {
    163 	if (ck.switch_level == 0)
    164 		return;
    165 
    166 	ck.switch_level--;
    167 	if (ck.switch_level == 0)
    168 		check_unhandled_option();
    169 }
    170 
    171 void
    172 check_getopt_end_while(void)
    173 {
    174 	if (ck.while_level == 0)
    175 		return;
    176 
    177 	ck.while_level--;
    178 	if (ck.while_level != 0)
    179 		return;
    180 
    181 	free(ck.options);
    182 	free(ck.unhandled_options);
    183 	ck.options = NULL;
    184 	ck.unhandled_options = NULL;
    185 }
    186