Home | History | Annotate | Line # | Download | only in lint1
func.c revision 1.180
      1  1.180    rillig /*	$NetBSD: func.c,v 1.180 2024/02/03 12:57:12 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.170    rillig  *	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.21       jmc #if HAVE_NBTOOL_CONFIG_H
     35   1.21       jmc #include "nbtool_config.h"
     36   1.21       jmc #endif
     37   1.21       jmc 
     38    1.8  christos #include <sys/cdefs.h>
     39  1.136    rillig #if defined(__RCSID)
     40  1.180    rillig __RCSID("$NetBSD: func.c,v 1.180 2024/02/03 12:57:12 rillig Exp $");
     41    1.1       cgd #endif
     42    1.1       cgd 
     43    1.1       cgd #include <stdlib.h>
     44    1.1       cgd #include <string.h>
     45    1.1       cgd 
     46    1.1       cgd #include "lint1.h"
     47   1.10        tv #include "cgram.h"
     48    1.1       cgd 
     49    1.1       cgd /*
     50    1.1       cgd  * Contains a pointer to the symbol table entry of the current function
     51    1.1       cgd  * definition.
     52    1.1       cgd  */
     53  1.178    rillig sym_t *funcsym;
     54    1.1       cgd 
     55    1.1       cgd /* Is set as long as a statement can be reached. Must be set at level 0. */
     56  1.178    rillig bool reached = true;
     57    1.1       cgd 
     58    1.1       cgd /*
     59   1.94    rillig  * Is true by default, can be cleared by NOTREACHED.
     60   1.94    rillig  * Is reset to true whenever 'reached' changes.
     61    1.1       cgd  */
     62  1.178    rillig bool warn_about_unreachable;
     63    1.1       cgd 
     64    1.1       cgd /*
     65   1.63    rillig  * In conjunction with 'reached', controls printing of "fallthrough on ..."
     66    1.1       cgd  * warnings.
     67  1.158    rillig  * Reset by each statement and set by FALLTHROUGH, stmt_switch_expr and
     68  1.158    rillig  * case_label.
     69    1.1       cgd  *
     70  1.168    rillig  * The control statements 'if', 'for', 'while' and 'switch' do not reset
     71  1.168    rillig  * suppress_fallthrough because this must be done by the controlled statement.
     72  1.168    rillig  * At least for 'if', this is important because ** FALLTHROUGH ** after "if
     73  1.168    rillig  * (expr) statement" is evaluated before the following token, which causes
     74  1.168    rillig  * reduction of above. This means that ** FALLTHROUGH ** after "if ..." would
     75  1.168    rillig  * always be ignored.
     76    1.1       cgd  */
     77  1.178    rillig bool suppress_fallthrough;
     78    1.1       cgd 
     79   1.48    rillig /* The innermost control statement */
     80  1.150    rillig static control_statement *cstmt;
     81    1.1       cgd 
     82    1.1       cgd /*
     83  1.173    rillig  * Number of parameters which will be checked for usage in following
     84  1.173    rillig  * function definition. -1 stands for all parameters.
     85    1.1       cgd  *
     86   1.31    rillig  * The position of the last ARGSUSED comment is stored in argsused_pos.
     87    1.1       cgd  */
     88  1.178    rillig int nargusg = -1;
     89  1.178    rillig pos_t argsused_pos;
     90    1.1       cgd 
     91    1.1       cgd /*
     92  1.173    rillig  * Number of parameters of the following function definition whose types
     93  1.173    rillig  * shall be checked by lint2. -1 stands for all parameters.
     94    1.1       cgd  *
     95    1.1       cgd  * The position of the last VARARGS comment is stored in vapos.
     96    1.1       cgd  */
     97  1.178    rillig int nvararg = -1;
     98  1.178    rillig pos_t vapos;
     99    1.1       cgd 
    100    1.1       cgd /*
    101   1.49    rillig  * Both printflike_argnum and scanflike_argnum contain the 1-based number
    102  1.173    rillig  * of the string parameter which shall be used to check the types of remaining
    103   1.49    rillig  * arguments (for PRINTFLIKE and SCANFLIKE).
    104    1.1       cgd  *
    105   1.31    rillig  * printflike_pos and scanflike_pos are the positions of the last PRINTFLIKE
    106   1.31    rillig  * or SCANFLIKE comment.
    107    1.1       cgd  */
    108  1.178    rillig int printflike_argnum = -1;
    109  1.178    rillig int scanflike_argnum = -1;
    110  1.178    rillig pos_t printflike_pos;
    111  1.178    rillig pos_t scanflike_pos;
    112    1.1       cgd 
    113    1.1       cgd /*
    114   1.28    rillig  * If both plibflg and llibflg are set, prototypes are written as function
    115    1.1       cgd  * definitions to the output file.
    116    1.1       cgd  */
    117  1.178    rillig bool plibflg;
    118    1.1       cgd 
    119  1.168    rillig /* Temporarily suppress warnings about constants in conditional context. */
    120  1.178    rillig bool suppress_constcond;
    121    1.1       cgd 
    122    1.1       cgd /*
    123  1.138    rillig  * Whether a lint library shall be created. The effect of this flag is that
    124  1.138    rillig  * all defined symbols are treated as used.
    125    1.1       cgd  * (The LINTLIBRARY comment also resets vflag.)
    126    1.1       cgd  */
    127  1.178    rillig bool llibflg;
    128    1.1       cgd 
    129    1.1       cgd /*
    130  1.138    rillig  * Determines the warnings that are suppressed by a LINTED directive.  For
    131  1.138    rillig  * globally suppressed warnings, see 'msgset'.
    132  1.138    rillig  *
    133  1.138    rillig  * LWARN_ALL:	all warnings are enabled
    134  1.138    rillig  * LWARN_NONE:	all warnings are suppressed
    135  1.138    rillig  * n >= 0:	warning n is ignored, the others are active
    136    1.1       cgd  */
    137  1.178    rillig int lwarn = LWARN_ALL;
    138    1.6       jpo 
    139  1.168    rillig /* Temporarily suppress warnings about wrong types for bit-fields. */
    140  1.178    rillig bool suppress_bitfieldtype;
    141   1.16   thorpej 
    142  1.168    rillig /* Temporarily suppress warnings about use of 'long long'. */
    143  1.178    rillig bool suppress_longlong;
    144    1.1       cgd 
    145    1.1       cgd void
    146   1.96    rillig begin_control_statement(control_statement_kind kind)
    147    1.1       cgd {
    148  1.115    rillig 	control_statement *cs;
    149    1.1       cgd 
    150  1.115    rillig 	cs = xcalloc(1, sizeof(*cs));
    151  1.115    rillig 	cs->c_kind = kind;
    152  1.115    rillig 	cs->c_surrounding = cstmt;
    153  1.115    rillig 	cstmt = cs;
    154    1.1       cgd }
    155    1.1       cgd 
    156    1.1       cgd void
    157   1.96    rillig end_control_statement(control_statement_kind kind)
    158    1.1       cgd {
    159  1.110    rillig 	while (cstmt->c_kind != kind)
    160  1.110    rillig 		cstmt = cstmt->c_surrounding;
    161    1.1       cgd 
    162  1.180    rillig 	control_statement *cs = cstmt;
    163  1.115    rillig 	cstmt = cs->c_surrounding;
    164    1.1       cgd 
    165  1.180    rillig 	case_label_t *cl, *next;
    166  1.115    rillig 	for (cl = cs->c_case_labels; cl != NULL; cl = next) {
    167   1.63    rillig 		next = cl->cl_next;
    168    1.1       cgd 		free(cl);
    169    1.1       cgd 	}
    170    1.1       cgd 
    171  1.115    rillig 	free(cs->c_switch_type);
    172  1.115    rillig 	free(cs);
    173    1.1       cgd }
    174    1.1       cgd 
    175   1.93    rillig static void
    176   1.93    rillig set_reached(bool new_reached)
    177   1.93    rillig {
    178  1.118    rillig 	debug_step("%s -> %s",
    179  1.106    rillig 	    reached ? "reachable" : "unreachable",
    180  1.106    rillig 	    new_reached ? "reachable" : "unreachable");
    181   1.93    rillig 	reached = new_reached;
    182   1.94    rillig 	warn_about_unreachable = true;
    183   1.93    rillig }
    184   1.93    rillig 
    185    1.1       cgd /*
    186    1.1       cgd  * Prints a warning if a statement cannot be reached.
    187    1.1       cgd  */
    188    1.1       cgd void
    189   1.31    rillig check_statement_reachable(void)
    190    1.1       cgd {
    191   1.94    rillig 	if (!reached && warn_about_unreachable) {
    192    1.1       cgd 		/* statement not reached */
    193    1.1       cgd 		warning(193);
    194   1.95    rillig 		warn_about_unreachable = false;
    195    1.1       cgd 	}
    196    1.1       cgd }
    197    1.1       cgd 
    198    1.1       cgd /*
    199    1.1       cgd  * Called after a function declaration which introduces a function definition
    200  1.173    rillig  * and before an (optional) old-style parameter declaration list.
    201    1.1       cgd  *
    202  1.173    rillig  * Puts all symbols declared in the prototype or in an old-style parameter
    203    1.1       cgd  * list back to the symbol table.
    204    1.1       cgd  *
    205    1.1       cgd  * Does the usual checking of storage class, type (return value),
    206   1.28    rillig  * redeclaration, etc.
    207    1.1       cgd  */
    208    1.1       cgd void
    209  1.146    rillig begin_function(sym_t *fsym)
    210    1.1       cgd {
    211    1.1       cgd 	funcsym = fsym;
    212    1.1       cgd 
    213    1.1       cgd 	/*
    214  1.177    rillig 	 * Put all symbols declared in the parameter list back to the symbol
    215  1.177    rillig 	 * table.
    216    1.1       cgd 	 */
    217  1.180    rillig 	for (sym_t *sym = dcs->d_func_proto_syms; sym != NULL;
    218  1.128    rillig 	    sym = sym->s_level_next) {
    219   1.77    rillig 		if (sym->s_block_level != -1) {
    220   1.77    rillig 			lint_assert(sym->s_block_level == 1);
    221    1.1       cgd 			inssym(1, sym);
    222    1.1       cgd 		}
    223    1.1       cgd 	}
    224    1.1       cgd 
    225    1.1       cgd 	/*
    226  1.177    rillig 	 * In old_style_function() we did not know whether it is an old style
    227  1.177    rillig 	 * function definition or only an old-style declaration, if there are
    228  1.177    rillig 	 * no parameters inside the parameter list ("f()").
    229    1.1       cgd 	 */
    230  1.173    rillig 	if (!fsym->s_type->t_proto && fsym->u.s_old_style_params == NULL)
    231   1.59    rillig 		fsym->s_osdef = true;
    232    1.1       cgd 
    233   1.29    rillig 	check_type(fsym);
    234    1.1       cgd 
    235    1.1       cgd 	/*
    236   1.29    rillig 	 * check_type() checks for almost all possible errors, but not for
    237    1.1       cgd 	 * incomplete return values (these are allowed in declarations)
    238    1.1       cgd 	 */
    239    1.1       cgd 	if (fsym->s_type->t_subt->t_tspec != VOID &&
    240   1.66    rillig 	    is_incomplete(fsym->s_type->t_subt)) {
    241    1.1       cgd 		/* cannot return incomplete type */
    242    1.1       cgd 		error(67);
    243    1.1       cgd 	}
    244    1.1       cgd 
    245    1.4       jpo 	fsym->s_def = DEF;
    246    1.1       cgd 
    247    1.1       cgd 	if (fsym->s_scl == TYPEDEF) {
    248    1.1       cgd 		fsym->s_scl = EXTERN;
    249    1.1       cgd 		/* illegal storage class */
    250    1.1       cgd 		error(8);
    251    1.1       cgd 	}
    252    1.1       cgd 
    253    1.4       jpo 	if (dcs->d_inline)
    254   1.59    rillig 		fsym->s_inline = true;
    255    1.4       jpo 
    256    1.1       cgd 	/*
    257  1.177    rillig 	 * Parameters in new-style function declarations need a name. ('void'
    258  1.177    rillig 	 * is already removed from the list of parameters.)
    259    1.1       cgd 	 */
    260  1.180    rillig 	int n = 1;
    261  1.173    rillig 	for (const sym_t *param = fsym->s_type->t_params;
    262  1.176    rillig 	    param != NULL; param = param->s_next) {
    263  1.173    rillig 		if (param->s_scl == ABSTRACT) {
    264  1.173    rillig 			lint_assert(param->s_name == unnamed);
    265  1.141    rillig 			/* formal parameter #%d lacks name */
    266    1.1       cgd 			error(59, n);
    267    1.1       cgd 		} else {
    268  1.173    rillig 			lint_assert(param->s_name != unnamed);
    269    1.1       cgd 		}
    270    1.1       cgd 		n++;
    271    1.1       cgd 	}
    272    1.1       cgd 
    273    1.1       cgd 	/*
    274  1.177    rillig 	 * We must also remember the position. s_def_pos is overwritten if this
    275  1.177    rillig 	 * is an old-style definition, and we had already a prototype.
    276    1.1       cgd 	 */
    277   1.81    rillig 	dcs->d_func_def_pos = fsym->s_def_pos;
    278    1.1       cgd 
    279  1.180    rillig 	sym_t *rdsym = dcs->d_redeclared_symbol;
    280  1.180    rillig 	if (rdsym != NULL) {
    281  1.180    rillig 		bool dowarn = false;
    282  1.180    rillig 		if (!check_redeclaration(fsym, &dowarn)) {
    283    1.1       cgd 
    284    1.1       cgd 			/*
    285  1.177    rillig 			 * Print nothing if the newly defined function is
    286  1.177    rillig 			 * defined in old style. A better warning will be
    287  1.177    rillig 			 * printed in check_func_lint_directives().
    288    1.1       cgd 			 */
    289   1.24  dholland 			if (dowarn && !fsym->s_osdef) {
    290  1.135    rillig 				/* TODO: error in C99 mode as well? */
    291  1.135    rillig 				if (!allow_trad && !allow_c99)
    292  1.139    rillig 					/* redeclaration of '%s' */
    293   1.50    rillig 					error(27, fsym->s_name);
    294   1.50    rillig 				else
    295  1.139    rillig 					/* redeclaration of '%s' */
    296   1.50    rillig 					warning(27, fsym->s_name);
    297  1.144    rillig 				print_previous_declaration(rdsym);
    298    1.1       cgd 			}
    299    1.1       cgd 
    300   1.29    rillig 			copy_usage_info(fsym, rdsym);
    301    1.1       cgd 
    302    1.1       cgd 			/*
    303  1.177    rillig 			 * If the old symbol was a prototype and the new one is
    304  1.177    rillig 			 * none, overtake the position of the declaration of
    305  1.177    rillig 			 * the prototype.
    306    1.1       cgd 			 */
    307    1.1       cgd 			if (fsym->s_osdef && rdsym->s_type->t_proto)
    308   1.36    rillig 				fsym->s_def_pos = rdsym->s_def_pos;
    309    1.1       cgd 
    310   1.29    rillig 			complete_type(fsym, rdsym);
    311    1.1       cgd 
    312    1.4       jpo 			if (rdsym->s_inline)
    313   1.59    rillig 				fsym->s_inline = true;
    314    1.1       cgd 		}
    315    1.1       cgd 
    316    1.1       cgd 		/* remove the old symbol from the symbol table */
    317    1.1       cgd 		rmsym(rdsym);
    318    1.1       cgd 	}
    319    1.1       cgd 
    320    1.1       cgd 	if (fsym->s_osdef && !fsym->s_type->t_proto) {
    321  1.135    rillig 		/* TODO: Make this an error in C99 mode as well. */
    322  1.179    rillig 		if (!allow_trad && !allow_c99 && hflag &&
    323  1.135    rillig 		    strcmp(fsym->s_name, "main") != 0)
    324   1.28    rillig 			/* function definition is not a prototype */
    325    1.1       cgd 			warning(286);
    326    1.1       cgd 	}
    327    1.1       cgd 
    328  1.159    rillig 	if (dcs->d_no_type_specifier)
    329   1.69    rillig 		fsym->s_return_type_implicit_int = true;
    330    1.1       cgd 
    331   1.93    rillig 	set_reached(true);
    332    1.1       cgd }
    333    1.1       cgd 
    334   1.72    rillig static void
    335   1.72    rillig check_missing_return_value(void)
    336   1.72    rillig {
    337   1.72    rillig 	if (funcsym->s_type->t_subt->t_tspec == VOID)
    338   1.72    rillig 		return;
    339   1.72    rillig 	if (funcsym->s_return_type_implicit_int)
    340   1.72    rillig 		return;
    341   1.72    rillig 
    342   1.72    rillig 	/* C99 5.1.2.2.3 "Program termination" p1 */
    343  1.133    rillig 	if (allow_c99 && strcmp(funcsym->s_name, "main") == 0)
    344   1.72    rillig 		return;
    345   1.72    rillig 
    346  1.142    rillig 	/* function '%s' falls off bottom without returning value */
    347   1.72    rillig 	warning(217, funcsym->s_name);
    348   1.72    rillig }
    349   1.72    rillig 
    350    1.1       cgd void
    351  1.146    rillig end_function(void)
    352    1.1       cgd {
    353    1.1       cgd 	if (reached) {
    354   1.63    rillig 		cstmt->c_had_return_noval = true;
    355   1.72    rillig 		check_missing_return_value();
    356    1.1       cgd 	}
    357    1.1       cgd 
    358   1.63    rillig 	if (cstmt->c_had_return_noval && cstmt->c_had_return_value &&
    359   1.69    rillig 	    funcsym->s_return_type_implicit_int)
    360  1.142    rillig 		/* function '%s' has 'return expr' and 'return' */
    361    1.1       cgd 		warning(216, funcsym->s_name);
    362    1.1       cgd 
    363  1.172    rillig 	/* Warn about unused parameters. */
    364  1.172    rillig 	int n = nargusg;
    365  1.172    rillig 	nargusg = -1;
    366  1.173    rillig 	for (const sym_t *param = dcs->d_func_params;
    367  1.176    rillig 	    param != NULL && n != 0; param = param->s_next, n--)
    368  1.173    rillig 		check_usage_sym(dcs->d_asm, param);
    369    1.1       cgd 
    370  1.180    rillig 	if (dcs->d_scl == EXTERN && funcsym->s_inline)
    371    1.4       jpo 		outsym(funcsym, funcsym->s_scl, DECL);
    372  1.180    rillig 	else
    373   1.81    rillig 		outfdef(funcsym, &dcs->d_func_def_pos,
    374   1.81    rillig 		    cstmt->c_had_return_value, funcsym->s_osdef,
    375  1.173    rillig 		    dcs->d_func_params);
    376    1.1       cgd 
    377  1.111    rillig 	/* clean up after syntax errors, see test stmt_for.c. */
    378  1.129    rillig 	while (dcs->d_enclosing != NULL)
    379  1.129    rillig 		dcs = dcs->d_enclosing;
    380  1.111    rillig 
    381  1.129    rillig 	lint_assert(dcs->d_enclosing == NULL);
    382  1.160    rillig 	lint_assert(dcs->d_kind == DLK_EXTERN);
    383  1.162    rillig 	symtab_remove_level(dcs->d_func_proto_syms);
    384    1.1       cgd 
    385    1.1       cgd 	/* must be set on level 0 */
    386   1.93    rillig 	set_reached(true);
    387  1.147    rillig 
    388  1.147    rillig 	funcsym = NULL;
    389    1.1       cgd }
    390    1.1       cgd 
    391    1.1       cgd void
    392   1.42    rillig named_label(sym_t *sym)
    393   1.42    rillig {
    394   1.42    rillig 
    395  1.180    rillig 	if (sym->s_set)
    396  1.142    rillig 		/* label '%s' redefined */
    397   1.42    rillig 		error(194, sym->s_name);
    398  1.180    rillig 	else
    399   1.42    rillig 		mark_as_set(sym);
    400   1.42    rillig 
    401  1.149    rillig 	/* XXX: Assuming that each label is reachable is wrong. */
    402   1.93    rillig 	set_reached(true);
    403   1.42    rillig }
    404   1.42    rillig 
    405   1.43    rillig static void
    406  1.108    rillig check_case_label_bitand(const tnode_t *case_expr, const tnode_t *switch_expr)
    407  1.108    rillig {
    408  1.108    rillig 	if (switch_expr->tn_op != BITAND ||
    409  1.108    rillig 	    switch_expr->tn_right->tn_op != CON)
    410  1.108    rillig 		return;
    411  1.108    rillig 
    412  1.108    rillig 	lint_assert(case_expr->tn_op == CON);
    413  1.167    rillig 	uint64_t case_value = (uint64_t)case_expr->tn_val.u.integer;
    414  1.167    rillig 	uint64_t mask = (uint64_t)switch_expr->tn_right->tn_val.u.integer;
    415  1.108    rillig 
    416  1.180    rillig 	if ((case_value & ~mask) != 0)
    417  1.108    rillig 		/* statement not reached */
    418  1.108    rillig 		warning(193);
    419  1.108    rillig }
    420  1.108    rillig 
    421  1.108    rillig static void
    422  1.115    rillig check_case_label_enum(const tnode_t *tn, const control_statement *cs)
    423   1.75    rillig {
    424   1.75    rillig 	/* similar to typeok_enum in tree.c */
    425   1.75    rillig 
    426  1.115    rillig 	if (!(tn->tn_type->t_is_enum || cs->c_switch_type->t_is_enum))
    427   1.75    rillig 		return;
    428  1.115    rillig 	if (tn->tn_type->t_is_enum && cs->c_switch_type->t_is_enum &&
    429  1.115    rillig 	    tn->tn_type->t_enum == cs->c_switch_type->t_enum)
    430   1.75    rillig 		return;
    431   1.75    rillig 
    432   1.76    rillig #if 0 /* not yet ready, see msg_130.c */
    433   1.75    rillig 	/* enum type mismatch: '%s' '%s' '%s' */
    434  1.115    rillig 	warning(130, type_name(cs->c_switch_type), op_name(EQ),
    435   1.75    rillig 	    type_name(tn->tn_type));
    436   1.76    rillig #endif
    437   1.75    rillig }
    438   1.75    rillig 
    439   1.75    rillig static void
    440  1.180    rillig check_case_label(tnode_t *tn)
    441    1.1       cgd {
    442  1.180    rillig 	control_statement *cs;
    443  1.180    rillig 	for (cs = cstmt; cs != NULL && !cs->c_switch; cs = cs->c_surrounding)
    444  1.180    rillig 		continue;
    445    1.1       cgd 
    446  1.115    rillig 	if (cs == NULL) {
    447   1.42    rillig 		/* case not in switch */
    448   1.42    rillig 		error(195);
    449   1.43    rillig 		return;
    450   1.43    rillig 	}
    451   1.43    rillig 
    452  1.149    rillig 	if (tn == NULL)
    453  1.149    rillig 		return;
    454  1.149    rillig 
    455  1.149    rillig 	if (tn->tn_op != CON) {
    456   1.42    rillig 		/* non-constant case expression */
    457   1.42    rillig 		error(197);
    458   1.43    rillig 		return;
    459   1.43    rillig 	}
    460   1.43    rillig 
    461  1.149    rillig 	if (!is_integer(tn->tn_type->t_tspec)) {
    462   1.42    rillig 		/* non-integral case expression */
    463   1.42    rillig 		error(198);
    464   1.43    rillig 		return;
    465   1.42    rillig 	}
    466    1.1       cgd 
    467  1.115    rillig 	check_case_label_bitand(tn, cs->c_switch_expr);
    468  1.115    rillig 	check_case_label_enum(tn, cs);
    469   1.75    rillig 
    470  1.115    rillig 	lint_assert(cs->c_switch_type != NULL);
    471    1.1       cgd 
    472  1.168    rillig 	if (reached && !suppress_fallthrough) {
    473   1.43    rillig 		if (hflag)
    474   1.43    rillig 			/* fallthrough on case statement */
    475   1.43    rillig 			warning(220);
    476   1.43    rillig 	}
    477    1.1       cgd 
    478  1.180    rillig 	tspec_t t = tn->tn_type->t_tspec;
    479  1.180    rillig 	if ((t == LONG || t == ULONG || t == LLONG || t == ULLONG)
    480  1.180    rillig 	    && !allow_c90)
    481  1.180    rillig 		/* case label must be of type 'int' in traditional C */
    482  1.180    rillig 		warning(203);
    483    1.1       cgd 
    484  1.180    rillig 	val_t *v = integer_constant(tn, true);
    485  1.180    rillig 	val_t nv;
    486  1.101    rillig 	(void)memset(&nv, 0, sizeof(nv));
    487  1.115    rillig 	convert_constant(CASE, 0, cs->c_switch_type, &nv, v);
    488   1.43    rillig 	free(v);
    489   1.43    rillig 
    490   1.43    rillig 	/* look if we had this value already */
    491  1.180    rillig 	case_label_t *cl;
    492  1.115    rillig 	for (cl = cs->c_case_labels; cl != NULL; cl = cl->cl_next) {
    493  1.163    rillig 		if (cl->cl_val.u.integer == nv.u.integer)
    494   1.43    rillig 			break;
    495   1.43    rillig 	}
    496  1.180    rillig 	if (cl != NULL && is_uinteger(nv.v_tspec))
    497  1.166    rillig 		/* duplicate case '%lu' in switch */
    498  1.163    rillig 		error(200, (unsigned long)nv.u.integer);
    499  1.180    rillig 	else if (cl != NULL)
    500  1.166    rillig 		/* duplicate case '%ld' in switch */
    501  1.163    rillig 		error(199, (long)nv.u.integer);
    502  1.180    rillig 	else {
    503  1.163    rillig 		check_getopt_case_label(nv.u.integer);
    504   1.68    rillig 
    505  1.180    rillig 		/* Prepend the value to the list of case values. */
    506  1.101    rillig 		cl = xcalloc(1, sizeof(*cl));
    507   1.43    rillig 		cl->cl_val = nv;
    508  1.115    rillig 		cl->cl_next = cs->c_case_labels;
    509  1.115    rillig 		cs->c_case_labels = cl;
    510   1.42    rillig 	}
    511   1.43    rillig }
    512   1.43    rillig 
    513   1.43    rillig void
    514   1.43    rillig case_label(tnode_t *tn)
    515   1.43    rillig {
    516  1.180    rillig 	check_case_label(tn);
    517   1.99    rillig 	expr_free_all();
    518   1.93    rillig 	set_reached(true);
    519   1.42    rillig }
    520   1.42    rillig 
    521   1.42    rillig void
    522   1.42    rillig default_label(void)
    523   1.42    rillig {
    524  1.180    rillig 	/* find the innermost switch statement */
    525  1.115    rillig 	control_statement *cs;
    526  1.115    rillig 	for (cs = cstmt; cs != NULL && !cs->c_switch; cs = cs->c_surrounding)
    527   1.42    rillig 		continue;
    528    1.1       cgd 
    529  1.180    rillig 	if (cs == NULL)
    530   1.42    rillig 		/* default outside switch */
    531   1.42    rillig 		error(201);
    532  1.180    rillig 	else if (cs->c_default)
    533   1.42    rillig 		/* duplicate default in switch */
    534   1.42    rillig 		error(202);
    535  1.180    rillig 	else {
    536  1.168    rillig 		if (reached && !suppress_fallthrough) {
    537   1.42    rillig 			if (hflag)
    538   1.42    rillig 				/* fallthrough on default statement */
    539   1.42    rillig 				warning(284);
    540    1.1       cgd 		}
    541  1.115    rillig 		cs->c_default = true;
    542   1.42    rillig 	}
    543   1.42    rillig 
    544   1.93    rillig 	set_reached(true);
    545    1.1       cgd }
    546    1.1       cgd 
    547   1.39    rillig static tnode_t *
    548   1.39    rillig check_controlling_expression(tnode_t *tn)
    549   1.39    rillig {
    550  1.124    rillig 	tn = cconv(tn);
    551   1.39    rillig 	if (tn != NULL)
    552   1.60    rillig 		tn = promote(NOOP, false, tn);
    553   1.39    rillig 
    554   1.55    rillig 	if (tn != NULL && !is_scalar(tn->tn_type->t_tspec)) {
    555   1.39    rillig 		/* C99 6.5.15p4 for the ?: operator; see typeok:QUEST */
    556   1.39    rillig 		/* C99 6.8.4.1p1 for if statements */
    557   1.39    rillig 		/* C99 6.8.5p2 for while, do and for loops */
    558   1.39    rillig 		/* controlling expressions must have scalar type */
    559   1.39    rillig 		error(204);
    560   1.39    rillig 		return NULL;
    561   1.39    rillig 	}
    562   1.39    rillig 
    563  1.140    rillig 	if (tn != NULL && Tflag && !is_typeok_bool_compares_with_zero(tn)) {
    564   1.57    rillig 		/* controlling expression must be bool, not '%s' */
    565  1.154    rillig 		error(333, tn->tn_type->t_is_enum ? type_name(tn->tn_type)
    566  1.154    rillig 		    : tspec_name(tn->tn_type->t_tspec));
    567   1.57    rillig 	}
    568   1.57    rillig 
    569   1.39    rillig 	return tn;
    570   1.39    rillig }
    571   1.39    rillig 
    572    1.1       cgd void
    573  1.158    rillig stmt_if_expr(tnode_t *tn)
    574    1.1       cgd {
    575    1.1       cgd 	if (tn != NULL)
    576   1.39    rillig 		tn = check_controlling_expression(tn);
    577    1.1       cgd 	if (tn != NULL)
    578   1.67    rillig 		expr(tn, false, true, false, false);
    579   1.96    rillig 	begin_control_statement(CS_IF);
    580   1.88    rillig 
    581   1.88    rillig 	if (tn != NULL && tn->tn_op == CON && !tn->tn_system_dependent) {
    582   1.93    rillig 		/* XXX: what if inside 'if (0)'? */
    583   1.93    rillig 		set_reached(constant_is_nonzero(tn));
    584   1.93    rillig 		/* XXX: what about always_else? */
    585   1.88    rillig 		cstmt->c_always_then = reached;
    586   1.88    rillig 	}
    587    1.1       cgd }
    588    1.1       cgd 
    589    1.1       cgd void
    590  1.158    rillig stmt_if_then_stmt(void)
    591    1.1       cgd {
    592   1.87    rillig 	cstmt->c_reached_end_of_then = reached;
    593   1.93    rillig 	/* XXX: what if inside 'if (0)'? */
    594   1.93    rillig 	set_reached(!cstmt->c_always_then);
    595    1.1       cgd }
    596    1.1       cgd 
    597    1.1       cgd void
    598  1.158    rillig stmt_if_else_stmt(bool els)
    599    1.1       cgd {
    600   1.90    rillig 	if (cstmt->c_reached_end_of_then)
    601   1.93    rillig 		set_reached(true);
    602   1.90    rillig 	else if (cstmt->c_always_then)
    603   1.93    rillig 		set_reached(false);
    604   1.90    rillig 	else if (!els)
    605   1.93    rillig 		set_reached(true);
    606   1.14     lukem 
    607   1.96    rillig 	end_control_statement(CS_IF);
    608    1.1       cgd }
    609    1.1       cgd 
    610    1.1       cgd void
    611  1.158    rillig stmt_switch_expr(tnode_t *tn)
    612    1.1       cgd {
    613    1.1       cgd 	if (tn != NULL)
    614    1.1       cgd 		tn = cconv(tn);
    615    1.1       cgd 	if (tn != NULL)
    616   1.60    rillig 		tn = promote(NOOP, false, tn);
    617   1.55    rillig 	if (tn != NULL && !is_integer(tn->tn_type->t_tspec)) {
    618    1.1       cgd 		/* switch expression must have integral type */
    619    1.1       cgd 		error(205);
    620    1.1       cgd 		tn = NULL;
    621    1.1       cgd 	}
    622  1.134    rillig 	if (tn != NULL && !allow_c90) {
    623  1.180    rillig 		tspec_t t = tn->tn_type->t_tspec;
    624  1.164    rillig 		if (t == LONG || t == ULONG || t == LLONG || t == ULLONG) {
    625  1.123    rillig 			/* switch expression must be of type 'int' in ... */
    626    1.1       cgd 			warning(271);
    627    1.1       cgd 		}
    628    1.1       cgd 	}
    629    1.1       cgd 
    630    1.1       cgd 	/*
    631  1.177    rillig 	 * Remember the type of the expression. Because it's possible that
    632  1.177    rillig 	 * (*tp) is allocated on tree memory, the type must be duplicated. This
    633  1.177    rillig 	 * is not too complicated because it is only an integer type.
    634    1.1       cgd 	 */
    635  1.180    rillig 	type_t *tp = xcalloc(1, sizeof(*tp));
    636    1.1       cgd 	if (tn != NULL) {
    637    1.1       cgd 		tp->t_tspec = tn->tn_type->t_tspec;
    638   1.71    rillig 		if ((tp->t_is_enum = tn->tn_type->t_is_enum) != false)
    639    1.1       cgd 			tp->t_enum = tn->tn_type->t_enum;
    640    1.1       cgd 	} else {
    641    1.1       cgd 		tp->t_tspec = INT;
    642    1.1       cgd 	}
    643    1.1       cgd 
    644  1.108    rillig 	/* leak the memory, for check_case_label_bitand */
    645  1.122    rillig 	(void)expr_save_memory();
    646  1.108    rillig 
    647   1.68    rillig 	check_getopt_begin_switch();
    648  1.108    rillig 	expr(tn, true, false, false, false);
    649    1.1       cgd 
    650   1.96    rillig 	begin_control_statement(CS_SWITCH);
    651   1.59    rillig 	cstmt->c_switch = true;
    652   1.97    rillig 	cstmt->c_switch_type = tp;
    653  1.108    rillig 	cstmt->c_switch_expr = tn;
    654    1.1       cgd 
    655   1.93    rillig 	set_reached(false);
    656  1.168    rillig 	suppress_fallthrough = true;
    657    1.1       cgd }
    658    1.1       cgd 
    659    1.1       cgd void
    660  1.158    rillig stmt_switch_expr_stmt(void)
    661    1.1       cgd {
    662  1.155    rillig 	int nenum = 0, nclab = 0;
    663  1.155    rillig 	sym_t *esym;
    664   1.79    rillig 	case_label_t *cl;
    665    1.1       cgd 
    666   1.97    rillig 	lint_assert(cstmt->c_switch_type != NULL);
    667    1.1       cgd 
    668   1.97    rillig 	if (cstmt->c_switch_type->t_is_enum) {
    669  1.112    rillig 		/*
    670  1.112    rillig 		 * Warn if the number of case labels is different from the
    671  1.112    rillig 		 * number of enumerators.
    672  1.112    rillig 		 */
    673    1.1       cgd 		nenum = nclab = 0;
    674   1.97    rillig 		lint_assert(cstmt->c_switch_type->t_enum != NULL);
    675   1.97    rillig 		for (esym = cstmt->c_switch_type->t_enum->en_first_enumerator;
    676  1.176    rillig 		    esym != NULL; esym = esym->s_next) {
    677    1.1       cgd 			nenum++;
    678    1.1       cgd 		}
    679   1.79    rillig 		for (cl = cstmt->c_case_labels; cl != NULL; cl = cl->cl_next)
    680    1.1       cgd 			nclab++;
    681  1.137    rillig 		if (hflag && eflag && nclab < nenum && !cstmt->c_default) {
    682    1.1       cgd 			/* enumeration value(s) not handled in switch */
    683    1.1       cgd 			warning(206);
    684    1.1       cgd 		}
    685    1.1       cgd 	}
    686    1.1       cgd 
    687   1.68    rillig 	check_getopt_end_switch();
    688   1.68    rillig 
    689   1.48    rillig 	if (cstmt->c_break) {
    690    1.1       cgd 		/*
    691  1.112    rillig 		 * The end of the switch statement is always reached since
    692  1.177    rillig 		 * c_break is only set if a break statement can actually be
    693  1.177    rillig 		 * reached.
    694    1.1       cgd 		 */
    695   1.93    rillig 		set_reached(true);
    696  1.112    rillig 	} else if (cstmt->c_default ||
    697  1.112    rillig 		   (hflag && cstmt->c_switch_type->t_is_enum &&
    698  1.112    rillig 		    nenum == nclab)) {
    699    1.1       cgd 		/*
    700  1.177    rillig 		 * The end of the switch statement is reached if the end of the
    701  1.177    rillig 		 * last statement inside it is reached.
    702  1.112    rillig 		 */
    703  1.112    rillig 	} else {
    704  1.112    rillig 		/*
    705  1.177    rillig 		 * There are possible values that are not handled in the switch
    706  1.177    rillig 		 * statement.
    707    1.1       cgd 		 */
    708   1.93    rillig 		set_reached(true);
    709  1.112    rillig 	}
    710    1.1       cgd 
    711   1.96    rillig 	end_control_statement(CS_SWITCH);
    712    1.1       cgd }
    713    1.1       cgd 
    714    1.1       cgd void
    715  1.158    rillig stmt_while_expr(tnode_t *tn)
    716    1.1       cgd {
    717    1.1       cgd 	if (!reached) {
    718    1.1       cgd 		/* loop not entered at top */
    719    1.1       cgd 		warning(207);
    720   1.93    rillig 		/* FIXME: that's plain wrong. */
    721   1.93    rillig 		set_reached(true);
    722    1.1       cgd 	}
    723    1.1       cgd 
    724    1.1       cgd 	if (tn != NULL)
    725   1.39    rillig 		tn = check_controlling_expression(tn);
    726    1.1       cgd 
    727   1.96    rillig 	begin_control_statement(CS_WHILE);
    728   1.59    rillig 	cstmt->c_loop = true;
    729   1.92    rillig 	cstmt->c_maybe_endless = is_nonzero(tn);
    730  1.180    rillig 	bool body_reached = !is_zero(tn);
    731    1.1       cgd 
    732   1.68    rillig 	check_getopt_begin_while(tn);
    733   1.67    rillig 	expr(tn, false, true, true, false);
    734   1.92    rillig 
    735   1.93    rillig 	set_reached(body_reached);
    736    1.1       cgd }
    737    1.1       cgd 
    738    1.1       cgd void
    739  1.158    rillig stmt_while_expr_stmt(void)
    740    1.1       cgd {
    741   1.93    rillig 	set_reached(!cstmt->c_maybe_endless || cstmt->c_break);
    742   1.68    rillig 	check_getopt_end_while();
    743   1.96    rillig 	end_control_statement(CS_WHILE);
    744    1.1       cgd }
    745    1.1       cgd 
    746    1.1       cgd void
    747  1.158    rillig stmt_do(void)
    748    1.1       cgd {
    749    1.1       cgd 	if (!reached) {
    750    1.1       cgd 		/* loop not entered at top */
    751    1.1       cgd 		warning(207);
    752   1.93    rillig 		set_reached(true);
    753    1.1       cgd 	}
    754    1.1       cgd 
    755   1.96    rillig 	begin_control_statement(CS_DO_WHILE);
    756   1.59    rillig 	cstmt->c_loop = true;
    757    1.1       cgd }
    758    1.1       cgd 
    759    1.1       cgd void
    760  1.158    rillig stmt_do_while_expr(tnode_t *tn)
    761    1.1       cgd {
    762   1.85    rillig 	if (cstmt->c_continue)
    763   1.93    rillig 		set_reached(true);
    764    1.1       cgd 
    765    1.1       cgd 	if (tn != NULL)
    766   1.39    rillig 		tn = check_controlling_expression(tn);
    767    1.1       cgd 
    768    1.1       cgd 	if (tn != NULL && tn->tn_op == CON) {
    769   1.84    rillig 		cstmt->c_maybe_endless = constant_is_nonzero(tn);
    770   1.85    rillig 		if (!cstmt->c_maybe_endless && cstmt->c_continue)
    771   1.46    rillig 			/* continue in 'do ... while (0)' loop */
    772   1.46    rillig 			error(323);
    773    1.1       cgd 	}
    774    1.1       cgd 
    775   1.67    rillig 	expr(tn, false, true, true, true);
    776    1.1       cgd 
    777   1.84    rillig 	if (cstmt->c_maybe_endless)
    778   1.93    rillig 		set_reached(false);
    779   1.83    rillig 	if (cstmt->c_break)
    780   1.93    rillig 		set_reached(true);
    781    1.1       cgd 
    782   1.96    rillig 	end_control_statement(CS_DO_WHILE);
    783    1.1       cgd }
    784    1.1       cgd 
    785    1.1       cgd void
    786  1.158    rillig stmt_for_exprs(tnode_t *tn1, tnode_t *tn2, tnode_t *tn3)
    787    1.1       cgd {
    788    1.1       cgd 	/*
    789  1.177    rillig 	 * If there is no initialization expression it is possible that it is
    790  1.177    rillig 	 * intended not to enter the loop at top.
    791    1.1       cgd 	 */
    792    1.1       cgd 	if (tn1 != NULL && !reached) {
    793    1.1       cgd 		/* loop not entered at top */
    794    1.1       cgd 		warning(207);
    795   1.93    rillig 		set_reached(true);
    796    1.1       cgd 	}
    797    1.1       cgd 
    798   1.96    rillig 	begin_control_statement(CS_FOR);
    799   1.59    rillig 	cstmt->c_loop = true;
    800    1.1       cgd 
    801    1.1       cgd 	/*
    802  1.177    rillig 	 * Store the tree memory for the reinitialization expression. Also
    803  1.177    rillig 	 * remember this expression itself. We must check it at the end of the
    804  1.177    rillig 	 * loop to get "used but not set" warnings correct.
    805    1.1       cgd 	 */
    806   1.99    rillig 	cstmt->c_for_expr3_mem = expr_save_memory();
    807   1.97    rillig 	cstmt->c_for_expr3 = tn3;
    808   1.97    rillig 	cstmt->c_for_expr3_pos = curr_pos;
    809   1.97    rillig 	cstmt->c_for_expr3_csrc_pos = csrc_pos;
    810    1.1       cgd 
    811    1.1       cgd 	if (tn1 != NULL)
    812   1.67    rillig 		expr(tn1, false, false, true, false);
    813    1.1       cgd 
    814    1.1       cgd 	if (tn2 != NULL)
    815   1.39    rillig 		tn2 = check_controlling_expression(tn2);
    816    1.1       cgd 	if (tn2 != NULL)
    817   1.67    rillig 		expr(tn2, false, true, true, false);
    818    1.1       cgd 
    819   1.91    rillig 	cstmt->c_maybe_endless = tn2 == NULL || is_nonzero(tn2);
    820    1.1       cgd 
    821  1.158    rillig 	/* The tn3 expression is checked in stmt_for_exprs_stmt. */
    822    1.1       cgd 
    823   1.93    rillig 	set_reached(!is_zero(tn2));
    824    1.1       cgd }
    825    1.1       cgd 
    826    1.1       cgd void
    827  1.158    rillig stmt_for_exprs_stmt(void)
    828    1.1       cgd {
    829   1.85    rillig 	if (cstmt->c_continue)
    830   1.93    rillig 		set_reached(true);
    831    1.1       cgd 
    832  1.180    rillig 	expr_restore_memory(cstmt->c_for_expr3_mem);
    833  1.180    rillig 	tnode_t *tn3 = cstmt->c_for_expr3;
    834    1.1       cgd 
    835  1.180    rillig 	pos_t saved_curr_pos = curr_pos;
    836  1.180    rillig 	pos_t saved_csrc_pos = csrc_pos;
    837   1.97    rillig 	curr_pos = cstmt->c_for_expr3_pos;
    838   1.97    rillig 	csrc_pos = cstmt->c_for_expr3_csrc_pos;
    839    1.1       cgd 
    840    1.1       cgd 	/* simply "statement not reached" would be confusing */
    841   1.94    rillig 	if (!reached && warn_about_unreachable) {
    842    1.1       cgd 		/* end-of-loop code not reached */
    843    1.1       cgd 		warning(223);
    844   1.93    rillig 		set_reached(true);
    845    1.1       cgd 	}
    846    1.1       cgd 
    847  1.180    rillig 	if (tn3 != NULL)
    848   1.67    rillig 		expr(tn3, false, false, true, false);
    849  1.180    rillig 	else
    850   1.99    rillig 		expr_free_all();
    851    1.1       cgd 
    852  1.180    rillig 	curr_pos = saved_curr_pos;
    853  1.180    rillig 	csrc_pos = saved_csrc_pos;
    854    1.1       cgd 
    855   1.93    rillig 	set_reached(cstmt->c_break || !cstmt->c_maybe_endless);
    856    1.1       cgd 
    857   1.96    rillig 	end_control_statement(CS_FOR);
    858    1.1       cgd }
    859    1.1       cgd 
    860    1.1       cgd void
    861  1.158    rillig stmt_goto(sym_t *lab)
    862    1.1       cgd {
    863   1.60    rillig 	mark_as_used(lab, false, false);
    864   1.31    rillig 	check_statement_reachable();
    865   1.93    rillig 	set_reached(false);
    866    1.1       cgd }
    867    1.1       cgd 
    868    1.1       cgd void
    869  1.158    rillig stmt_break(void)
    870    1.1       cgd {
    871  1.180    rillig 	control_statement *cs = cstmt;
    872  1.115    rillig 	while (cs != NULL && !cs->c_loop && !cs->c_switch)
    873  1.115    rillig 		cs = cs->c_surrounding;
    874    1.1       cgd 
    875  1.180    rillig 	if (cs == NULL)
    876    1.1       cgd 		/* break outside loop or switch */
    877    1.1       cgd 		error(208);
    878  1.180    rillig 	else if (reached)
    879  1.180    rillig 		cs->c_break = true;
    880    1.1       cgd 
    881    1.1       cgd 	if (bflag)
    882   1.31    rillig 		check_statement_reachable();
    883    1.1       cgd 
    884   1.93    rillig 	set_reached(false);
    885    1.1       cgd }
    886    1.1       cgd 
    887    1.1       cgd void
    888  1.158    rillig stmt_continue(void)
    889    1.1       cgd {
    890  1.115    rillig 	control_statement *cs;
    891    1.1       cgd 
    892  1.115    rillig 	for (cs = cstmt; cs != NULL && !cs->c_loop; cs = cs->c_surrounding)
    893   1.14     lukem 		continue;
    894    1.1       cgd 
    895  1.180    rillig 	if (cs == NULL)
    896    1.1       cgd 		/* continue outside loop */
    897    1.1       cgd 		error(209);
    898  1.180    rillig 	else
    899   1.85    rillig 		/* TODO: only if reachable, for symmetry with c_break */
    900  1.115    rillig 		cs->c_continue = true;
    901    1.1       cgd 
    902   1.31    rillig 	check_statement_reachable();
    903    1.1       cgd 
    904   1.93    rillig 	set_reached(false);
    905    1.1       cgd }
    906    1.1       cgd 
    907  1.153    rillig static bool
    908  1.153    rillig is_parenthesized(const tnode_t *tn)
    909  1.153    rillig {
    910  1.153    rillig 	while (!tn->tn_parenthesized && tn->tn_op == COMMA)
    911  1.153    rillig 		tn = tn->tn_right;
    912  1.153    rillig 	return tn->tn_parenthesized && !tn->tn_sys;
    913  1.153    rillig }
    914  1.153    rillig 
    915  1.152    rillig static void
    916  1.152    rillig check_return_value(bool sys, tnode_t *tn)
    917  1.152    rillig {
    918  1.153    rillig 	if (any_query_enabled && is_parenthesized(tn)) {
    919  1.153    rillig 		/* parenthesized return value */
    920  1.153    rillig 		query_message(9);
    921  1.153    rillig 	}
    922  1.153    rillig 
    923  1.152    rillig 	/* Create a temporary node for the left side */
    924  1.171    rillig 	tnode_t *ln = expr_zero_alloc(sizeof(*ln), "tnode");
    925  1.152    rillig 	ln->tn_op = NAME;
    926  1.152    rillig 	ln->tn_type = expr_unqualified_type(funcsym->s_type->t_subt);
    927  1.152    rillig 	ln->tn_lvalue = true;
    928  1.152    rillig 	ln->tn_sym = funcsym;	/* better than nothing */
    929  1.152    rillig 
    930  1.152    rillig 	tnode_t *retn = build_binary(ln, RETURN, sys, tn);
    931  1.152    rillig 
    932  1.152    rillig 	if (retn != NULL) {
    933  1.152    rillig 		const tnode_t *rn = retn->tn_right;
    934  1.152    rillig 		while (rn->tn_op == CVT || rn->tn_op == PLUS)
    935  1.152    rillig 			rn = rn->tn_left;
    936  1.152    rillig 		if (rn->tn_op == ADDR && rn->tn_left->tn_op == NAME &&
    937  1.152    rillig 		    rn->tn_left->tn_sym->s_scl == AUTO) {
    938  1.152    rillig 			/* '%s' returns pointer to automatic object */
    939  1.152    rillig 			warning(302, funcsym->s_name);
    940  1.152    rillig 		}
    941  1.152    rillig 	}
    942  1.152    rillig 
    943  1.152    rillig 	expr(retn, true, false, true, false);
    944  1.152    rillig }
    945  1.152    rillig 
    946    1.1       cgd void
    947  1.158    rillig stmt_return(bool sys, tnode_t *tn)
    948    1.1       cgd {
    949  1.152    rillig 	control_statement *cs = cstmt;
    950    1.1       cgd 
    951  1.115    rillig 	if (cs == NULL) {
    952  1.109    rillig 		/* syntax error '%s' */
    953  1.109    rillig 		error(249, "return outside function");
    954  1.109    rillig 		return;
    955  1.109    rillig 	}
    956  1.109    rillig 
    957  1.115    rillig 	for (; cs->c_surrounding != NULL; cs = cs->c_surrounding)
    958   1.14     lukem 		continue;
    959    1.1       cgd 
    960   1.82    rillig 	if (tn != NULL)
    961  1.115    rillig 		cs->c_had_return_value = true;
    962   1.82    rillig 	else
    963  1.115    rillig 		cs->c_had_return_noval = true;
    964    1.1       cgd 
    965    1.1       cgd 	if (tn != NULL && funcsym->s_type->t_subt->t_tspec == VOID) {
    966  1.142    rillig 		/* void function '%s' cannot return value */
    967    1.1       cgd 		error(213, funcsym->s_name);
    968   1.99    rillig 		expr_free_all();
    969    1.1       cgd 		tn = NULL;
    970  1.174    rillig 	}
    971  1.174    rillig 	if (tn == NULL && funcsym->s_type->t_subt->t_tspec != VOID
    972  1.174    rillig 	    && !funcsym->s_return_type_implicit_int) {
    973  1.174    rillig 		if (allow_c99)
    974  1.174    rillig 			/* function '%s' expects to return value */
    975  1.174    rillig 			error(214, funcsym->s_name);
    976  1.174    rillig 		else
    977  1.119    rillig 			/* function '%s' expects to return value */
    978    1.1       cgd 			warning(214, funcsym->s_name);
    979    1.1       cgd 	}
    980    1.1       cgd 
    981  1.152    rillig 	if (tn != NULL)
    982  1.152    rillig 		check_return_value(sys, tn);
    983  1.152    rillig 	else
    984   1.31    rillig 		check_statement_reachable();
    985    1.1       cgd 
    986   1.93    rillig 	set_reached(false);
    987    1.1       cgd }
    988    1.1       cgd 
    989    1.1       cgd void
    990   1.59    rillig global_clean_up_decl(bool silent)
    991    1.1       cgd {
    992    1.1       cgd 	if (nargusg != -1) {
    993    1.6       jpo 		if (!silent) {
    994  1.142    rillig 			/* comment ** %s ** must precede function definition */
    995  1.105    rillig 			warning_at(282, &argsused_pos, "ARGSUSED");
    996    1.1       cgd 		}
    997    1.1       cgd 		nargusg = -1;
    998    1.1       cgd 	}
    999    1.1       cgd 	if (nvararg != -1) {
   1000    1.6       jpo 		if (!silent) {
   1001  1.142    rillig 			/* comment ** %s ** must precede function definition */
   1002  1.105    rillig 			warning_at(282, &vapos, "VARARGS");
   1003    1.1       cgd 		}
   1004    1.1       cgd 		nvararg = -1;
   1005    1.1       cgd 	}
   1006   1.49    rillig 	if (printflike_argnum != -1) {
   1007    1.6       jpo 		if (!silent) {
   1008  1.142    rillig 			/* comment ** %s ** must precede function definition */
   1009  1.105    rillig 			warning_at(282, &printflike_pos, "PRINTFLIKE");
   1010    1.1       cgd 		}
   1011   1.49    rillig 		printflike_argnum = -1;
   1012    1.1       cgd 	}
   1013   1.49    rillig 	if (scanflike_argnum != -1) {
   1014    1.6       jpo 		if (!silent) {
   1015  1.143    rillig 			/* comment ** %s ** must precede function definition */
   1016  1.105    rillig 			warning_at(282, &scanflike_pos, "SCANFLIKE");
   1017    1.1       cgd 		}
   1018   1.49    rillig 		scanflike_argnum = -1;
   1019    1.1       cgd 	}
   1020    1.1       cgd 
   1021   1.59    rillig 	dcs->d_asm = false;
   1022  1.107    rillig 
   1023  1.107    rillig 	/*
   1024  1.107    rillig 	 * Needed for BSD yacc in case of parse errors; GNU Bison 3.0.4 is
   1025  1.125    rillig 	 * fine.  See test gcc_attribute.c, function_with_unknown_attribute.
   1026  1.107    rillig 	 */
   1027  1.125    rillig 	in_gcc_attribute = false;
   1028  1.130    rillig 	while (dcs->d_enclosing != NULL)
   1029  1.130    rillig 		end_declaration_level();
   1030    1.1       cgd }
   1031    1.1       cgd 
   1032    1.1       cgd /*
   1033  1.173    rillig  * Only the first n parameters of the following function are checked for usage.
   1034  1.169    rillig  * A missing argument is taken to be 0.
   1035    1.1       cgd  */
   1036  1.169    rillig static void
   1037   1.14     lukem argsused(int n)
   1038    1.1       cgd {
   1039  1.160    rillig 	if (dcs->d_kind != DLK_EXTERN) {
   1040  1.142    rillig 		/* comment ** %s ** must be outside function */
   1041    1.1       cgd 		warning(280, "ARGSUSED");
   1042    1.1       cgd 		return;
   1043    1.1       cgd 	}
   1044    1.1       cgd 	if (nargusg != -1) {
   1045  1.142    rillig 		/* duplicate comment ** %s ** */
   1046    1.1       cgd 		warning(281, "ARGSUSED");
   1047    1.1       cgd 	}
   1048  1.180    rillig 	nargusg = n != -1 ? n : 0;
   1049   1.36    rillig 	argsused_pos = curr_pos;
   1050    1.1       cgd }
   1051    1.1       cgd 
   1052  1.169    rillig static void
   1053   1.14     lukem varargs(int n)
   1054    1.1       cgd {
   1055  1.160    rillig 	if (dcs->d_kind != DLK_EXTERN) {
   1056  1.142    rillig 		/* comment ** %s ** must be outside function */
   1057    1.1       cgd 		warning(280, "VARARGS");
   1058    1.1       cgd 		return;
   1059    1.1       cgd 	}
   1060    1.1       cgd 	if (nvararg != -1) {
   1061  1.142    rillig 		/* duplicate comment ** %s ** */
   1062    1.1       cgd 		warning(281, "VARARGS");
   1063    1.1       cgd 	}
   1064  1.180    rillig 	nvararg = n != -1 ? n : 0;
   1065   1.36    rillig 	vapos = curr_pos;
   1066    1.1       cgd }
   1067    1.1       cgd 
   1068    1.1       cgd /*
   1069  1.173    rillig  * Check all parameters until the (n-1)-th as usual. The n-th argument is
   1070  1.173    rillig  * used to check the types of the remaining arguments.
   1071    1.1       cgd  */
   1072  1.169    rillig static void
   1073   1.14     lukem printflike(int n)
   1074    1.1       cgd {
   1075  1.160    rillig 	if (dcs->d_kind != DLK_EXTERN) {
   1076  1.142    rillig 		/* comment ** %s ** must be outside function */
   1077    1.1       cgd 		warning(280, "PRINTFLIKE");
   1078    1.1       cgd 		return;
   1079    1.1       cgd 	}
   1080   1.49    rillig 	if (printflike_argnum != -1) {
   1081  1.142    rillig 		/* duplicate comment ** %s ** */
   1082    1.1       cgd 		warning(281, "PRINTFLIKE");
   1083    1.1       cgd 	}
   1084  1.180    rillig 	printflike_argnum = n != -1 ? n : 0;
   1085   1.36    rillig 	printflike_pos = curr_pos;
   1086    1.1       cgd }
   1087    1.1       cgd 
   1088    1.1       cgd /*
   1089  1.173    rillig  * Check all parameters until the (n-1)-th as usual. The n-th argument is
   1090    1.1       cgd  * used the check the types of remaining arguments.
   1091    1.1       cgd  */
   1092  1.169    rillig static void
   1093   1.14     lukem scanflike(int n)
   1094    1.1       cgd {
   1095  1.160    rillig 	if (dcs->d_kind != DLK_EXTERN) {
   1096  1.142    rillig 		/* comment ** %s ** must be outside function */
   1097    1.1       cgd 		warning(280, "SCANFLIKE");
   1098    1.1       cgd 		return;
   1099    1.1       cgd 	}
   1100   1.49    rillig 	if (scanflike_argnum != -1) {
   1101  1.142    rillig 		/* duplicate comment ** %s ** */
   1102    1.1       cgd 		warning(281, "SCANFLIKE");
   1103    1.1       cgd 	}
   1104  1.180    rillig 	scanflike_argnum = n != -1 ? n : 0;
   1105   1.36    rillig 	scanflike_pos = curr_pos;
   1106    1.1       cgd }
   1107    1.1       cgd 
   1108  1.169    rillig static void
   1109  1.169    rillig lintlib(void)
   1110    1.1       cgd {
   1111  1.160    rillig 	if (dcs->d_kind != DLK_EXTERN) {
   1112  1.142    rillig 		/* comment ** %s ** must be outside function */
   1113    1.1       cgd 		warning(280, "LINTLIBRARY");
   1114    1.1       cgd 		return;
   1115    1.1       cgd 	}
   1116   1.59    rillig 	llibflg = true;
   1117  1.165    rillig 	vflag = true;
   1118    1.1       cgd }
   1119    1.1       cgd 
   1120    1.1       cgd /*
   1121   1.28    rillig  * PROTOLIB in conjunction with LINTLIBRARY can be used to handle
   1122    1.1       cgd  * prototypes like function definitions. This is done if the argument
   1123  1.156    rillig  * to PROTOLIB is nonzero. Otherwise, prototypes are handled normally.
   1124    1.1       cgd  */
   1125  1.169    rillig static void
   1126   1.14     lukem protolib(int n)
   1127    1.1       cgd {
   1128  1.160    rillig 	if (dcs->d_kind != DLK_EXTERN) {
   1129  1.142    rillig 		/* comment ** %s ** must be outside function */
   1130    1.1       cgd 		warning(280, "PROTOLIB");
   1131    1.1       cgd 		return;
   1132    1.1       cgd 	}
   1133   1.59    rillig 	plibflg = n != 0;
   1134    1.6       jpo }
   1135    1.6       jpo 
   1136    1.6       jpo void
   1137  1.169    rillig handle_lint_comment(lint_comment comment, int arg)
   1138    1.6       jpo {
   1139  1.169    rillig 	switch (comment) {
   1140  1.169    rillig 	case LC_ARGSUSED:	argsused(arg);			break;
   1141  1.169    rillig 	case LC_BITFIELDTYPE:	suppress_bitfieldtype = true;	break;
   1142  1.169    rillig 	case LC_CONSTCOND:	suppress_constcond = true;	break;
   1143  1.169    rillig 	case LC_FALLTHROUGH:	suppress_fallthrough = true;	break;
   1144  1.169    rillig 	case LC_LINTLIBRARY:	lintlib();			break;
   1145  1.169    rillig 	case LC_LINTED:		debug_step("set lwarn %d", arg);
   1146  1.169    rillig 				lwarn = arg;			break;
   1147  1.169    rillig 	case LC_LONGLONG:	suppress_longlong = true;	break;
   1148  1.169    rillig 	case LC_NOTREACHED:	set_reached(false);
   1149  1.169    rillig 				warn_about_unreachable = false;	break;
   1150  1.169    rillig 	case LC_PRINTFLIKE:	printflike(arg);		break;
   1151  1.169    rillig 	case LC_PROTOLIB:	protolib(arg);			break;
   1152  1.169    rillig 	case LC_SCANFLIKE:	scanflike(arg);			break;
   1153  1.169    rillig 	case LC_VARARGS:	varargs(arg);			break;
   1154  1.169    rillig 	}
   1155    1.1       cgd }
   1156