Module Name: src Committed By: rillig Date: Sat Feb 3 12:57:12 UTC 2024
Modified Files: src/usr.bin/xlint/lint1: ckbool.c ckgetopt.c decl.c emit1.c externs1.h func.c lex.c main1.c Log Message: lint: clean up comments, reduce scope of variables To generate a diff of this commit: cvs rdiff -u -r1.28 -r1.29 src/usr.bin/xlint/lint1/ckbool.c cvs rdiff -u -r1.20 -r1.21 src/usr.bin/xlint/lint1/ckgetopt.c cvs rdiff -u -r1.390 -r1.391 src/usr.bin/xlint/lint1/decl.c cvs rdiff -u -r1.83 -r1.84 src/usr.bin/xlint/lint1/emit1.c cvs rdiff -u -r1.213 -r1.214 src/usr.bin/xlint/lint1/externs1.h cvs rdiff -u -r1.179 -r1.180 src/usr.bin/xlint/lint1/func.c cvs rdiff -u -r1.209 -r1.210 src/usr.bin/xlint/lint1/lex.c cvs rdiff -u -r1.80 -r1.81 src/usr.bin/xlint/lint1/main1.c Please note that diffs are not public domain; they are subject to the copyright notices on the relevant files.
Modified files: Index: src/usr.bin/xlint/lint1/ckbool.c diff -u src/usr.bin/xlint/lint1/ckbool.c:1.28 src/usr.bin/xlint/lint1/ckbool.c:1.29 --- src/usr.bin/xlint/lint1/ckbool.c:1.28 Sat Dec 30 15:37:27 2023 +++ src/usr.bin/xlint/lint1/ckbool.c Sat Feb 3 12:57:12 2024 @@ -1,4 +1,4 @@ -/* $NetBSD: ckbool.c,v 1.28 2023/12/30 15:37:27 rillig Exp $ */ +/* $NetBSD: ckbool.c,v 1.29 2024/02/03 12:57:12 rillig Exp $ */ /*- * Copyright (c) 2021 The NetBSD Foundation, Inc. @@ -36,7 +36,7 @@ #include <sys/cdefs.h> #if defined(__RCSID) -__RCSID("$NetBSD: ckbool.c,v 1.28 2023/12/30 15:37:27 rillig Exp $"); +__RCSID("$NetBSD: ckbool.c,v 1.29 2024/02/03 12:57:12 rillig Exp $"); #endif #include <string.h> @@ -46,7 +46,7 @@ __RCSID("$NetBSD: ckbool.c,v 1.28 2023/1 /* * The option -T treats _Bool as incompatible with all other scalar types. - * See d_c99_bool_strict.c for the exact rules and for examples. + * See d_c99_bool_strict.c for the detailed rules and for examples. */ @@ -113,16 +113,15 @@ typeok_strict_bool_binary_compatible(op_ if (is_typeok_strict_bool_binary(op, ln, lt, rn, rt)) return true; - if (op == FARG) { + if (op == FARG) /* parameter %d expects '%s', gets passed '%s' */ error(334, arg, tspec_name(lt), tspec_name(rt)); - } else if (op == RETURN) { + else if (op == RETURN) /* function has return type '%s' but returns '%s' */ error(211, tspec_name(lt), tspec_name(rt)); - } else { + else /* operands of '%s' have incompatible types '%s' and '%s' */ error(107, op_name(op), tspec_name(lt), tspec_name(rt)); - } return false; } @@ -136,16 +135,12 @@ typeok_scalar_strict_bool(op_t op, const const tnode_t *ln, const tnode_t *rn) { - tspec_t lt, rt; - ln = before_conversion(ln); - lt = ln->tn_type->t_tspec; - + tspec_t lt = ln->tn_type->t_tspec; + tspec_t rt = NO_TSPEC; if (rn != NULL) { rn = before_conversion(rn); rt = rn->tn_type->t_tspec; - } else { - rt = NO_TSPEC; } if (rn != NULL && @@ -202,26 +197,16 @@ typeok_scalar_strict_bool(op_t op, const return true; } -/* - * See if the node is valid as operand of an operator that compares its - * operand with 0. - */ bool is_typeok_bool_compares_with_zero(const tnode_t *tn) { - tspec_t t; - while (tn->tn_op == COMMA) tn = tn->tn_right; tn = before_conversion(tn); - t = tn->tn_type->t_tspec; - - if (t == BOOL) - return true; - if (tn->tn_sys && is_scalar(t)) - return true; - return tn->tn_op == BITAND; + return tn->tn_type->t_tspec == BOOL + || tn->tn_op == BITAND + || (tn->tn_sys && is_scalar(tn->tn_type->t_tspec)); } bool Index: src/usr.bin/xlint/lint1/ckgetopt.c diff -u src/usr.bin/xlint/lint1/ckgetopt.c:1.20 src/usr.bin/xlint/lint1/ckgetopt.c:1.21 --- src/usr.bin/xlint/lint1/ckgetopt.c:1.20 Thu Feb 1 18:37:06 2024 +++ src/usr.bin/xlint/lint1/ckgetopt.c Sat Feb 3 12:57:12 2024 @@ -1,4 +1,4 @@ -/* $NetBSD: ckgetopt.c,v 1.20 2024/02/01 18:37:06 rillig Exp $ */ +/* $NetBSD: ckgetopt.c,v 1.21 2024/02/03 12:57:12 rillig Exp $ */ /*- * Copyright (c) 2021 The NetBSD Foundation, Inc. @@ -35,7 +35,7 @@ #include <sys/cdefs.h> #if defined(__RCSID) -__RCSID("$NetBSD: ckgetopt.c,v 1.20 2024/02/01 18:37:06 rillig Exp $"); +__RCSID("$NetBSD: ckgetopt.c,v 1.21 2024/02/03 12:57:12 rillig Exp $"); #endif #include <stdbool.h> @@ -109,31 +109,22 @@ is_getopt_condition(const tnode_t *tn, c static void check_unlisted_option(char opt) { - char *optptr; - - lint_assert(ck.options != NULL); - if (opt == ':' && ck.options[0] != ':') goto warn; - optptr = strchr(ck.options, opt); + char *optptr = strchr(ck.options, opt); if (optptr != NULL) *optptr = ' '; - else if (opt != '?') { + else if (opt != '?') warn: /* option '%c' should be listed in the options string */ warning(339, opt); - } } static void check_unhandled_option(void) { - const char *opt; - - lint_assert(ck.options != NULL); - - for (opt = ck.options; *opt != '\0'; opt++) { + for (const char *opt = ck.options; *opt != '\0'; opt++) { if (*opt == ' ' || *opt == ':') continue; Index: src/usr.bin/xlint/lint1/decl.c diff -u src/usr.bin/xlint/lint1/decl.c:1.390 src/usr.bin/xlint/lint1/decl.c:1.391 --- src/usr.bin/xlint/lint1/decl.c:1.390 Tue Jan 23 20:03:42 2024 +++ src/usr.bin/xlint/lint1/decl.c Sat Feb 3 12:57:12 2024 @@ -1,4 +1,4 @@ -/* $NetBSD: decl.c,v 1.390 2024/01/23 20:03:42 rillig Exp $ */ +/* $NetBSD: decl.c,v 1.391 2024/02/03 12:57:12 rillig Exp $ */ /* * Copyright (c) 1996 Christopher G. Demetriou. All Rights Reserved. @@ -38,7 +38,7 @@ #include <sys/cdefs.h> #if defined(__RCSID) -__RCSID("$NetBSD: decl.c,v 1.390 2024/01/23 20:03:42 rillig Exp $"); +__RCSID("$NetBSD: decl.c,v 1.391 2024/02/03 12:57:12 rillig Exp $"); #endif #include <sys/param.h> @@ -64,11 +64,8 @@ int enumval; decl_level *dcs; -/* - * initializes all global vars used in declarations - */ void -initdecl(void) +init_decl(void) { /* declaration stack */ Index: src/usr.bin/xlint/lint1/emit1.c diff -u src/usr.bin/xlint/lint1/emit1.c:1.83 src/usr.bin/xlint/lint1/emit1.c:1.84 --- src/usr.bin/xlint/lint1/emit1.c:1.83 Thu Feb 1 18:37:06 2024 +++ src/usr.bin/xlint/lint1/emit1.c Sat Feb 3 12:57:12 2024 @@ -1,4 +1,4 @@ -/* $NetBSD: emit1.c,v 1.83 2024/02/01 18:37:06 rillig Exp $ */ +/* $NetBSD: emit1.c,v 1.84 2024/02/03 12:57:12 rillig Exp $ */ /* * Copyright (c) 1996 Christopher G. Demetriou. All Rights Reserved. @@ -38,7 +38,7 @@ #include <sys/cdefs.h> #if defined(__RCSID) -__RCSID("$NetBSD: emit1.c,v 1.83 2024/02/01 18:37:06 rillig Exp $"); +__RCSID("$NetBSD: emit1.c,v 1.84 2024/02/03 12:57:12 rillig Exp $"); #endif #include "lint1.h" @@ -234,7 +234,6 @@ outfdef(const sym_t *fsym, const pos_t * const sym_t *args) { int narg; - const sym_t *arg; if (posp->p_file == csrc_pos.p_file) { outint(posp->p_line); @@ -298,11 +297,11 @@ outfdef(const sym_t *fsym, const pos_t * /* parameter types and return value */ if (osdef) { narg = 0; - for (arg = args; arg != NULL; arg = arg->s_next) + for (const sym_t *arg = args; arg != NULL; arg = arg->s_next) narg++; outchar('f'); outint(narg); - for (arg = args; arg != NULL; arg = arg->s_next) + for (const sym_t *arg = args; arg != NULL; arg = arg->s_next) outtype(arg->s_type); outtype(fsym->s_type->t_subt); } else { @@ -323,8 +322,7 @@ void outcall(const tnode_t *tn, bool retval_used, bool retval_discarded) { tnode_t *args, *arg; - int narg, n, i; - tspec_t t; + int narg, i; outint(csrc_pos.p_line); outchar('c'); /* function call */ @@ -341,13 +339,14 @@ outcall(const tnode_t *tn, bool retval_u for (arg = args; arg != NULL; arg = tn_ck_right(arg)) narg++; /* information about arguments */ - for (n = 1; n <= narg; n++) { + for (int n = 1; n <= narg; n++) { /* the last argument is the top one in the tree */ for (i = narg, arg = args; i > n; i--, arg = arg->tn_right) continue; arg = arg->tn_left; if (arg->tn_op == CON) { - if (is_integer(t = arg->tn_type->t_tspec)) { + tspec_t t = arg->tn_type->t_tspec; + if (is_integer(t)) { /* * XXX it would probably be better to * explicitly test the sign @@ -382,7 +381,7 @@ outcall(const tnode_t *tn, bool retval_u /* types of arguments */ outchar('f'); outint(narg); - for (n = 1; n <= narg; n++) { + for (int n = 1; n <= narg; n++) { /* the last argument is the top one in the tree */ for (i = narg, arg = args; i > n; i--, arg = arg->tn_right) continue; Index: src/usr.bin/xlint/lint1/externs1.h diff -u src/usr.bin/xlint/lint1/externs1.h:1.213 src/usr.bin/xlint/lint1/externs1.h:1.214 --- src/usr.bin/xlint/lint1/externs1.h:1.213 Thu Feb 1 18:37:06 2024 +++ src/usr.bin/xlint/lint1/externs1.h Sat Feb 3 12:57:12 2024 @@ -1,4 +1,4 @@ -/* $NetBSD: externs1.h,v 1.213 2024/02/01 18:37:06 rillig Exp $ */ +/* $NetBSD: externs1.h,v 1.214 2024/02/03 12:57:12 rillig Exp $ */ /* * Copyright (c) 1994, 1995 Jochen Pohl @@ -83,7 +83,7 @@ extern bool in_system_header; extern symbol_kind sym_kind; extern FILE *yyin; -void initscan(void); +void init_lex(void); int64_t convert_integer(int64_t, tspec_t, unsigned int); void clear_warn_flags(void); sym_t *getsym(sbuf_t *); @@ -205,7 +205,7 @@ extern decl_level *dcs; extern const char unnamed[]; extern int enumval; -void initdecl(void); +void init_decl(void); type_t *gettyp(tspec_t); type_t *block_dup_type(const type_t *); type_t *expr_dup_type(const type_t *); Index: src/usr.bin/xlint/lint1/func.c diff -u src/usr.bin/xlint/lint1/func.c:1.179 src/usr.bin/xlint/lint1/func.c:1.180 --- src/usr.bin/xlint/lint1/func.c:1.179 Sat Jan 6 15:05:24 2024 +++ src/usr.bin/xlint/lint1/func.c Sat Feb 3 12:57:12 2024 @@ -1,4 +1,4 @@ -/* $NetBSD: func.c,v 1.179 2024/01/06 15:05:24 rillig Exp $ */ +/* $NetBSD: func.c,v 1.180 2024/02/03 12:57:12 rillig Exp $ */ /* * Copyright (c) 1994, 1995 Jochen Pohl @@ -37,7 +37,7 @@ #include <sys/cdefs.h> #if defined(__RCSID) -__RCSID("$NetBSD: func.c,v 1.179 2024/01/06 15:05:24 rillig Exp $"); +__RCSID("$NetBSD: func.c,v 1.180 2024/02/03 12:57:12 rillig Exp $"); #endif #include <stdlib.h> @@ -156,17 +156,13 @@ begin_control_statement(control_statemen void end_control_statement(control_statement_kind kind) { - control_statement *cs; - case_label_t *cl, *next; - - lint_assert(cstmt != NULL); - while (cstmt->c_kind != kind) cstmt = cstmt->c_surrounding; - cs = cstmt; + control_statement *cs = cstmt; cstmt = cs->c_surrounding; + case_label_t *cl, *next; for (cl = cs->c_case_labels; cl != NULL; cl = next) { next = cl->cl_next; free(cl); @@ -212,17 +208,13 @@ check_statement_reachable(void) void begin_function(sym_t *fsym) { - int n; - bool dowarn; - sym_t *sym, *rdsym; - funcsym = fsym; /* * Put all symbols declared in the parameter list back to the symbol * table. */ - for (sym = dcs->d_func_proto_syms; sym != NULL; + for (sym_t *sym = dcs->d_func_proto_syms; sym != NULL; sym = sym->s_level_next) { if (sym->s_block_level != -1) { lint_assert(sym->s_block_level == 1); @@ -265,7 +257,7 @@ begin_function(sym_t *fsym) * Parameters in new-style function declarations need a name. ('void' * is already removed from the list of parameters.) */ - n = 1; + int n = 1; for (const sym_t *param = fsym->s_type->t_params; param != NULL; param = param->s_next) { if (param->s_scl == ABSTRACT) { @@ -284,9 +276,10 @@ begin_function(sym_t *fsym) */ dcs->d_func_def_pos = fsym->s_def_pos; - if ((rdsym = dcs->d_redeclared_symbol) != NULL) { - - if (!check_redeclaration(fsym, (dowarn = false, &dowarn))) { + sym_t *rdsym = dcs->d_redeclared_symbol; + if (rdsym != NULL) { + bool dowarn = false; + if (!check_redeclaration(fsym, &dowarn)) { /* * Print nothing if the newly defined function is @@ -354,23 +347,14 @@ check_missing_return_value(void) warning(217, funcsym->s_name); } -/* - * Called at the end of a function definition. - */ void end_function(void) { - if (reached) { cstmt->c_had_return_noval = true; check_missing_return_value(); } - /* - * This warning is printed only if the return value was implicitly - * declared to be int. Otherwise, the wrong return statement has - * already printed a warning. - */ if (cstmt->c_had_return_noval && cstmt->c_had_return_value && funcsym->s_return_type_implicit_int) /* function '%s' has 'return expr' and 'return' */ @@ -383,27 +367,17 @@ end_function(void) param != NULL && n != 0; param = param->s_next, n--) check_usage_sym(dcs->d_asm, param); - /* - * Write the information about the function definition to the output - * file. Inline functions explicitly declared extern are written as - * declarations only. - */ - if (dcs->d_scl == EXTERN && funcsym->s_inline) { + if (dcs->d_scl == EXTERN && funcsym->s_inline) outsym(funcsym, funcsym->s_scl, DECL); - } else { + else outfdef(funcsym, &dcs->d_func_def_pos, cstmt->c_had_return_value, funcsym->s_osdef, dcs->d_func_params); - } /* clean up after syntax errors, see test stmt_for.c. */ while (dcs->d_enclosing != NULL) dcs = dcs->d_enclosing; - /* - * Remove all symbols declared during the parameter declaration from - * the symbol table. - */ lint_assert(dcs->d_enclosing == NULL); lint_assert(dcs->d_kind == DLK_EXTERN); symtab_remove_level(dcs->d_func_proto_syms); @@ -418,12 +392,11 @@ void named_label(sym_t *sym) { - if (sym->s_set) { + if (sym->s_set) /* label '%s' redefined */ error(194, sym->s_name); - } else { + else mark_as_set(sym); - } /* XXX: Assuming that each label is reachable is wrong. */ set_reached(true); @@ -432,7 +405,6 @@ named_label(sym_t *sym) static void check_case_label_bitand(const tnode_t *case_expr, const tnode_t *switch_expr) { - if (switch_expr->tn_op != BITAND || switch_expr->tn_right->tn_op != CON) return; @@ -441,10 +413,9 @@ check_case_label_bitand(const tnode_t *c uint64_t case_value = (uint64_t)case_expr->tn_val.u.integer; uint64_t mask = (uint64_t)switch_expr->tn_right->tn_val.u.integer; - if ((case_value & ~mask) != 0) { + if ((case_value & ~mask) != 0) /* statement not reached */ warning(193); - } } static void @@ -466,12 +437,11 @@ check_case_label_enum(const tnode_t *tn, } static void -check_case_label(tnode_t *tn, control_statement *cs) +check_case_label(tnode_t *tn) { - case_label_t *cl; - val_t *v; - val_t nv; - tspec_t t; + control_statement *cs; + for (cs = cstmt; cs != NULL && !cs->c_switch; cs = cs->c_surrounding) + continue; if (cs == NULL) { /* case not in switch */ @@ -505,38 +475,34 @@ check_case_label(tnode_t *tn, control_st warning(220); } - t = tn->tn_type->t_tspec; - if (t == LONG || t == ULONG || - t == LLONG || t == ULLONG) { - if (!allow_c90) - /* case label must be of type 'int' in traditional C */ - warning(203); - } + tspec_t t = tn->tn_type->t_tspec; + if ((t == LONG || t == ULONG || t == LLONG || t == ULLONG) + && !allow_c90) + /* case label must be of type 'int' in traditional C */ + warning(203); - /* - * get the value of the expression and convert it to the type of the - * switch expression - */ - v = integer_constant(tn, true); + val_t *v = integer_constant(tn, true); + val_t nv; (void)memset(&nv, 0, sizeof(nv)); convert_constant(CASE, 0, cs->c_switch_type, &nv, v); free(v); /* look if we had this value already */ + case_label_t *cl; for (cl = cs->c_case_labels; cl != NULL; cl = cl->cl_next) { if (cl->cl_val.u.integer == nv.u.integer) break; } - if (cl != NULL && is_uinteger(nv.v_tspec)) { + if (cl != NULL && is_uinteger(nv.v_tspec)) /* duplicate case '%lu' in switch */ error(200, (unsigned long)nv.u.integer); - } else if (cl != NULL) { + else if (cl != NULL) /* duplicate case '%ld' in switch */ error(199, (long)nv.u.integer); - } else { + else { check_getopt_case_label(nv.u.integer); - /* append the value to the list of case values */ + /* Prepend the value to the list of case values. */ cl = xcalloc(1, sizeof(*cl)); cl->cl_val = nv; cl->cl_next = cs->c_case_labels; @@ -547,35 +513,26 @@ check_case_label(tnode_t *tn, control_st void case_label(tnode_t *tn) { - control_statement *cs; - - /* find the innermost switch statement */ - for (cs = cstmt; cs != NULL && !cs->c_switch; cs = cs->c_surrounding) - continue; - - check_case_label(tn, cs); - + check_case_label(tn); expr_free_all(); - set_reached(true); } void default_label(void) { - control_statement *cs; - /* find the innermost switch statement */ + control_statement *cs; for (cs = cstmt; cs != NULL && !cs->c_switch; cs = cs->c_surrounding) continue; - if (cs == NULL) { + if (cs == NULL) /* default outside switch */ error(201); - } else if (cs->c_default) { + else if (cs->c_default) /* duplicate default in switch */ error(202); - } else { + else { if (reached && !suppress_fallthrough) { if (hflag) /* fallthrough on default statement */ @@ -590,7 +547,6 @@ default_label(void) static tnode_t * check_controlling_expression(tnode_t *tn) { - tn = cconv(tn); if (tn != NULL) tn = promote(NOOP, false, tn); @@ -616,7 +572,6 @@ check_controlling_expression(tnode_t *tn void stmt_if_expr(tnode_t *tn) { - if (tn != NULL) tn = check_controlling_expression(tn); if (tn != NULL) @@ -634,7 +589,6 @@ stmt_if_expr(tnode_t *tn) void stmt_if_then_stmt(void) { - cstmt->c_reached_end_of_then = reached; /* XXX: what if inside 'if (0)'? */ set_reached(!cstmt->c_always_then); @@ -656,9 +610,6 @@ stmt_if_else_stmt(bool els) void stmt_switch_expr(tnode_t *tn) { - tspec_t t; - type_t *tp; - if (tn != NULL) tn = cconv(tn); if (tn != NULL) @@ -669,7 +620,7 @@ stmt_switch_expr(tnode_t *tn) tn = NULL; } if (tn != NULL && !allow_c90) { - t = tn->tn_type->t_tspec; + tspec_t t = tn->tn_type->t_tspec; if (t == LONG || t == ULONG || t == LLONG || t == ULLONG) { /* switch expression must be of type 'int' in ... */ warning(271); @@ -681,7 +632,7 @@ stmt_switch_expr(tnode_t *tn) * (*tp) is allocated on tree memory, the type must be duplicated. This * is not too complicated because it is only an integer type. */ - tp = xcalloc(1, sizeof(*tp)); + type_t *tp = xcalloc(1, sizeof(*tp)); if (tn != NULL) { tp->t_tspec = tn->tn_type->t_tspec; if ((tp->t_is_enum = tn->tn_type->t_is_enum) != false) @@ -763,8 +714,6 @@ stmt_switch_expr_stmt(void) void stmt_while_expr(tnode_t *tn) { - bool body_reached; - if (!reached) { /* loop not entered at top */ warning(207); @@ -778,7 +727,7 @@ stmt_while_expr(tnode_t *tn) begin_control_statement(CS_WHILE); cstmt->c_loop = true; cstmt->c_maybe_endless = is_nonzero(tn); - body_reached = !is_zero(tn); + bool body_reached = !is_zero(tn); check_getopt_begin_while(tn); expr(tn, false, true, true, false); @@ -789,13 +738,7 @@ stmt_while_expr(tnode_t *tn) void stmt_while_expr_stmt(void) { - - /* - * The end of the loop can be reached if it is no endless loop or there - * was a break statement which was reached. - */ set_reached(!cstmt->c_maybe_endless || cstmt->c_break); - check_getopt_end_while(); end_control_statement(CS_WHILE); } @@ -803,7 +746,6 @@ stmt_while_expr_stmt(void) void stmt_do(void) { - if (!reached) { /* loop not entered at top */ warning(207); @@ -817,11 +759,6 @@ stmt_do(void) void stmt_do_while_expr(tnode_t *tn) { - - /* - * If there was a continue statement, the expression controlling the - * loop is reached. - */ if (cstmt->c_continue) set_reached(true); @@ -848,7 +785,6 @@ stmt_do_while_expr(tnode_t *tn) void stmt_for_exprs(tnode_t *tn1, tnode_t *tn2, tnode_t *tn3) { - /* * If there is no initialization expression it is possible that it is * intended not to enter the loop at top. @@ -890,18 +826,14 @@ stmt_for_exprs(tnode_t *tn1, tnode_t *tn void stmt_for_exprs_stmt(void) { - pos_t cpos, cspos; - tnode_t *tn3; - if (cstmt->c_continue) set_reached(true); - cpos = curr_pos; - cspos = csrc_pos; - - /* Restore the tree memory for the reinitialization expression */ expr_restore_memory(cstmt->c_for_expr3_mem); - tn3 = cstmt->c_for_expr3; + tnode_t *tn3 = cstmt->c_for_expr3; + + pos_t saved_curr_pos = curr_pos; + pos_t saved_csrc_pos = csrc_pos; curr_pos = cstmt->c_for_expr3_pos; csrc_pos = cstmt->c_for_expr3_csrc_pos; @@ -912,17 +844,14 @@ stmt_for_exprs_stmt(void) set_reached(true); } - if (tn3 != NULL) { + if (tn3 != NULL) expr(tn3, false, false, true, false); - } else { + else expr_free_all(); - } - curr_pos = cpos; - csrc_pos = cspos; + curr_pos = saved_curr_pos; + csrc_pos = saved_csrc_pos; - /* An endless loop without break will never terminate */ - /* TODO: What if the loop contains a 'return'? */ set_reached(cstmt->c_break || !cstmt->c_maybe_endless); end_control_statement(CS_FOR); @@ -931,30 +860,23 @@ stmt_for_exprs_stmt(void) void stmt_goto(sym_t *lab) { - mark_as_used(lab, false, false); - check_statement_reachable(); - set_reached(false); } void stmt_break(void) { - control_statement *cs; - - cs = cstmt; + control_statement *cs = cstmt; while (cs != NULL && !cs->c_loop && !cs->c_switch) cs = cs->c_surrounding; - if (cs == NULL) { + if (cs == NULL) /* break outside loop or switch */ error(208); - } else { - if (reached) - cs->c_break = true; - } + else if (reached) + cs->c_break = true; if (bflag) check_statement_reachable(); @@ -970,13 +892,12 @@ stmt_continue(void) for (cs = cstmt; cs != NULL && !cs->c_loop; cs = cs->c_surrounding) continue; - if (cs == NULL) { + if (cs == NULL) /* continue outside loop */ error(209); - } else { + else /* TODO: only if reachable, for symmetry with c_break */ cs->c_continue = true; - } check_statement_reachable(); @@ -986,7 +907,6 @@ stmt_continue(void) static bool is_parenthesized(const tnode_t *tn) { - while (!tn->tn_parenthesized && tn->tn_op == COMMA) tn = tn->tn_right; return tn->tn_parenthesized && !tn->tn_sys; @@ -995,7 +915,6 @@ is_parenthesized(const tnode_t *tn) static void check_return_value(bool sys, tnode_t *tn) { - if (any_query_enabled && is_parenthesized(tn)) { /* parenthesized return value */ query_message(9); @@ -1067,14 +986,9 @@ stmt_return(bool sys, tnode_t *tn) set_reached(false); } -/* - * Do some cleanup after a global declaration or definition. - * Especially remove information about unused lint comments. - */ void global_clean_up_decl(bool silent) { - if (nargusg != -1) { if (!silent) { /* comment ** %s ** must precede function definition */ @@ -1122,10 +1036,6 @@ global_clean_up_decl(bool silent) static void argsused(int n) { - - if (n == -1) - n = 0; - if (dcs->d_kind != DLK_EXTERN) { /* comment ** %s ** must be outside function */ warning(280, "ARGSUSED"); @@ -1135,17 +1045,13 @@ argsused(int n) /* duplicate comment ** %s ** */ warning(281, "ARGSUSED"); } - nargusg = n; + nargusg = n != -1 ? n : 0; argsused_pos = curr_pos; } static void varargs(int n) { - - if (n == -1) - n = 0; - if (dcs->d_kind != DLK_EXTERN) { /* comment ** %s ** must be outside function */ warning(280, "VARARGS"); @@ -1155,7 +1061,7 @@ varargs(int n) /* duplicate comment ** %s ** */ warning(281, "VARARGS"); } - nvararg = n; + nvararg = n != -1 ? n : 0; vapos = curr_pos; } @@ -1166,10 +1072,6 @@ varargs(int n) static void printflike(int n) { - - if (n == -1) - n = 0; - if (dcs->d_kind != DLK_EXTERN) { /* comment ** %s ** must be outside function */ warning(280, "PRINTFLIKE"); @@ -1179,7 +1081,7 @@ printflike(int n) /* duplicate comment ** %s ** */ warning(281, "PRINTFLIKE"); } - printflike_argnum = n; + printflike_argnum = n != -1 ? n : 0; printflike_pos = curr_pos; } @@ -1190,10 +1092,6 @@ printflike(int n) static void scanflike(int n) { - - if (n == -1) - n = 0; - if (dcs->d_kind != DLK_EXTERN) { /* comment ** %s ** must be outside function */ warning(280, "SCANFLIKE"); @@ -1203,14 +1101,13 @@ scanflike(int n) /* duplicate comment ** %s ** */ warning(281, "SCANFLIKE"); } - scanflike_argnum = n; + scanflike_argnum = n != -1 ? n : 0; scanflike_pos = curr_pos; } static void lintlib(void) { - if (dcs->d_kind != DLK_EXTERN) { /* comment ** %s ** must be outside function */ warning(280, "LINTLIBRARY"); @@ -1228,7 +1125,6 @@ lintlib(void) static void protolib(int n) { - if (dcs->d_kind != DLK_EXTERN) { /* comment ** %s ** must be outside function */ warning(280, "PROTOLIB"); Index: src/usr.bin/xlint/lint1/lex.c diff -u src/usr.bin/xlint/lint1/lex.c:1.209 src/usr.bin/xlint/lint1/lex.c:1.210 --- src/usr.bin/xlint/lint1/lex.c:1.209 Sat Feb 3 10:56:18 2024 +++ src/usr.bin/xlint/lint1/lex.c Sat Feb 3 12:57:12 2024 @@ -1,4 +1,4 @@ -/* $NetBSD: lex.c,v 1.209 2024/02/03 10:56:18 rillig Exp $ */ +/* $NetBSD: lex.c,v 1.210 2024/02/03 12:57:12 rillig Exp $ */ /* * Copyright (c) 1996 Christopher G. Demetriou. All Rights Reserved. @@ -38,7 +38,7 @@ #include <sys/cdefs.h> #if defined(__RCSID) -__RCSID("$NetBSD: lex.c,v 1.209 2024/02/03 10:56:18 rillig Exp $"); +__RCSID("$NetBSD: lex.c,v 1.210 2024/02/03 12:57:12 rillig Exp $"); #endif #include <ctype.h> @@ -211,11 +211,8 @@ symbol_kind sym_kind; static unsigned int hash(const char *s) { - unsigned int v; - const char *p; - - v = 0; - for (p = s; *p != '\0'; p++) { + unsigned int v = 0; + for (const char *p = s; *p != '\0'; p++) { v = (v << 4) + (unsigned char)*p; v ^= v >> 28; } @@ -225,9 +222,7 @@ hash(const char *s) static void symtab_add(sym_t *sym) { - unsigned int h; - - h = hash(sym->s_name); + unsigned int h = hash(sym->s_name); if ((sym->s_symtab_next = symtab[h]) != NULL) symtab[h]->s_symtab_ref = &sym->s_symtab_next; sym->s_symtab_ref = &symtab[h]; @@ -345,7 +340,7 @@ debug_symtab(void) #endif static void -add_keyword(const struct keyword *kw, bool leading, bool trailing) +register_keyword(const struct keyword *kw, bool leading, bool trailing) { const char *name; @@ -402,7 +397,7 @@ is_keyword_known(const struct keyword *k /* Write all keywords to the symbol table. */ void -initscan(void) +init_lex(void) { size_t n = sizeof(keywords) / sizeof(keywords[0]); @@ -411,11 +406,11 @@ initscan(void) if (!is_keyword_known(kw)) continue; if (kw->kw_plain) - add_keyword(kw, false, false); + register_keyword(kw, false, false); if (kw->kw_leading) - add_keyword(kw, true, false); + register_keyword(kw, true, false); if (kw->kw_both) - add_keyword(kw, true, true); + register_keyword(kw, true, true); } } Index: src/usr.bin/xlint/lint1/main1.c diff -u src/usr.bin/xlint/lint1/main1.c:1.80 src/usr.bin/xlint/lint1/main1.c:1.81 --- src/usr.bin/xlint/lint1/main1.c:1.80 Fri Feb 2 23:36:01 2024 +++ src/usr.bin/xlint/lint1/main1.c Sat Feb 3 12:57:12 2024 @@ -1,4 +1,4 @@ -/* $NetBSD: main1.c,v 1.80 2024/02/02 23:36:01 rillig Exp $ */ +/* $NetBSD: main1.c,v 1.81 2024/02/03 12:57:12 rillig Exp $ */ /* * Copyright (c) 1994, 1995 Jochen Pohl @@ -37,7 +37,7 @@ #include <sys/cdefs.h> #if defined(__RCSID) -__RCSID("$NetBSD: main1.c,v 1.80 2024/02/02 23:36:01 rillig Exp $"); +__RCSID("$NetBSD: main1.c,v 1.81 2024/02/03 12:57:12 rillig Exp $"); #endif #include <sys/types.h> @@ -226,8 +226,8 @@ main(int argc, char *argv[]) #endif (void)signal(SIGFPE, sigfpe); - initdecl(); - initscan(); + init_decl(); + init_lex(); if (allow_gcc && allow_c90) { if ((yyin = gcc_builtins()) == NULL)