Module Name:    src
Committed By:   rillig
Date:           Sat Jan  6 15:05:24 UTC 2024

Modified Files:
        src/tests/usr.bin/xlint/lint1: expr_fold.c
        src/usr.bin/xlint/lint1: decl.c func.c lint1.h tree.c

Log Message:
lint: remove redundant parentheses, braces and comments

Rename the functions for folding constant expressions, to make the
comments redundant.


To generate a diff of this commit:
cvs rdiff -u -r1.10 -r1.11 src/tests/usr.bin/xlint/lint1/expr_fold.c
cvs rdiff -u -r1.385 -r1.386 src/usr.bin/xlint/lint1/decl.c
cvs rdiff -u -r1.178 -r1.179 src/usr.bin/xlint/lint1/func.c
cvs rdiff -u -r1.205 -r1.206 src/usr.bin/xlint/lint1/lint1.h
cvs rdiff -u -r1.588 -r1.589 src/usr.bin/xlint/lint1/tree.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/tests/usr.bin/xlint/lint1/expr_fold.c
diff -u src/tests/usr.bin/xlint/lint1/expr_fold.c:1.10 src/tests/usr.bin/xlint/lint1/expr_fold.c:1.11
--- src/tests/usr.bin/xlint/lint1/expr_fold.c:1.10	Sun Jul  9 11:01:27 2023
+++ src/tests/usr.bin/xlint/lint1/expr_fold.c	Sat Jan  6 15:05:24 2024
@@ -1,4 +1,4 @@
-/*	$NetBSD: expr_fold.c,v 1.10 2023/07/09 11:01:27 rillig Exp $	*/
+/*	$NetBSD: expr_fold.c,v 1.11 2024/01/06 15:05:24 rillig Exp $	*/
 # 3 "expr_fold.c"
 
 /*
@@ -185,7 +185,6 @@ fold_shl(void)
 	/* expect+1: warning: operator '<<' produces integer overflow [141] */
 	take_uint(1U << 24 << 24);
 
-	/* FIXME: undefined behavior in 'fold' at 'uint64_t << 104'. */
 	/* expect+1: warning: shift amount 104 is greater than bit-size 32 of 'unsigned int' [122] */
 	take_uint(1U << 24 << 104);
 }
@@ -197,7 +196,6 @@ fold_shr(void)
 
 	take_int(16777216 >> 25);
 
-	/* FIXME: undefined behavior in 'fold' at 'uint64_t >> 104'. */
 	/* expect+1: warning: shift amount 104 is greater than bit-size 32 of 'int' [122] */
 	take_int(16777216 >> 104);
 }

Index: src/usr.bin/xlint/lint1/decl.c
diff -u src/usr.bin/xlint/lint1/decl.c:1.385 src/usr.bin/xlint/lint1/decl.c:1.386
--- src/usr.bin/xlint/lint1/decl.c:1.385	Sun Dec 10 15:29:38 2023
+++ src/usr.bin/xlint/lint1/decl.c	Sat Jan  6 15:05:24 2024
@@ -1,4 +1,4 @@
-/* $NetBSD: decl.c,v 1.385 2023/12/10 15:29:38 rillig Exp $ */
+/* $NetBSD: decl.c,v 1.386 2024/01/06 15:05:24 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.385 2023/12/10 15:29:38 rillig Exp $");
+__RCSID("$NetBSD: decl.c,v 1.386 2024/01/06 15:05:24 rillig Exp $");
 #endif
 
 #include <sys/param.h>
@@ -852,7 +852,7 @@ check_type(sym_t *sym)
 		if (t == FUNC && !tp->t_proto &&
 		    !(to == NO_TSPEC && sym->s_osdef)) {
 			/* TODO: Make this an error in C99 mode as well. */
-			if ((!allow_trad && !allow_c99) && hflag)
+			if (!allow_trad && !allow_c99 && hflag)
 				/* function declaration is not a prototype */
 				warning(287);
 		}

Index: src/usr.bin/xlint/lint1/func.c
diff -u src/usr.bin/xlint/lint1/func.c:1.178 src/usr.bin/xlint/lint1/func.c:1.179
--- src/usr.bin/xlint/lint1/func.c:1.178	Sun Dec  3 18:17:41 2023
+++ src/usr.bin/xlint/lint1/func.c	Sat Jan  6 15:05:24 2024
@@ -1,4 +1,4 @@
-/*	$NetBSD: func.c,v 1.178 2023/12/03 18:17:41 rillig Exp $	*/
+/*	$NetBSD: func.c,v 1.179 2024/01/06 15:05:24 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.178 2023/12/03 18:17:41 rillig Exp $");
+__RCSID("$NetBSD: func.c,v 1.179 2024/01/06 15:05:24 rillig Exp $");
 #endif
 
 #include <stdlib.h>
@@ -326,7 +326,7 @@ begin_function(sym_t *fsym)
 
 	if (fsym->s_osdef && !fsym->s_type->t_proto) {
 		/* TODO: Make this an error in C99 mode as well. */
-		if ((!allow_trad && !allow_c99) && hflag &&
+		if (!allow_trad && !allow_c99 && hflag &&
 		    strcmp(fsym->s_name, "main") != 0)
 			/* function definition is not a prototype */
 			warning(286);

Index: src/usr.bin/xlint/lint1/lint1.h
diff -u src/usr.bin/xlint/lint1/lint1.h:1.205 src/usr.bin/xlint/lint1/lint1.h:1.206
--- src/usr.bin/xlint/lint1/lint1.h:1.205	Sun Dec  3 18:17:41 2023
+++ src/usr.bin/xlint/lint1/lint1.h	Sat Jan  6 15:05:24 2024
@@ -1,4 +1,4 @@
-/* $NetBSD: lint1.h,v 1.205 2023/12/03 18:17:41 rillig Exp $ */
+/* $NetBSD: lint1.h,v 1.206 2024/01/06 15:05:24 rillig Exp $ */
 
 /*
  * Copyright (c) 1996 Christopher G. Demetriou.  All Rights Reserved.
@@ -664,7 +664,7 @@ value_bits(unsigned bitsize)
 	 * see d_c99_complex_split.c to trigger this case.
 	 */
 	if (bitsize >= 64)
-		return ~((uint64_t)0);
+		return ~(uint64_t)0;
 
 	return ~(~(uint64_t)0 << bitsize);
 }

Index: src/usr.bin/xlint/lint1/tree.c
diff -u src/usr.bin/xlint/lint1/tree.c:1.588 src/usr.bin/xlint/lint1/tree.c:1.589
--- src/usr.bin/xlint/lint1/tree.c:1.588	Sat Jan  6 14:21:26 2024
+++ src/usr.bin/xlint/lint1/tree.c	Sat Jan  6 15:05:24 2024
@@ -1,4 +1,4 @@
-/*	$NetBSD: tree.c,v 1.588 2024/01/06 14:21:26 rillig Exp $	*/
+/*	$NetBSD: tree.c,v 1.589 2024/01/06 15:05:24 rillig Exp $	*/
 
 /*
  * Copyright (c) 1994, 1995 Jochen Pohl
@@ -37,7 +37,7 @@
 
 #include <sys/cdefs.h>
 #if defined(__RCSID)
-__RCSID("$NetBSD: tree.c,v 1.588 2024/01/06 14:21:26 rillig Exp $");
+__RCSID("$NetBSD: tree.c,v 1.589 2024/01/06 15:05:24 rillig Exp $");
 #endif
 
 #include <float.h>
@@ -787,12 +787,8 @@ build_address(bool sys, tnode_t *tn, boo
  * this function. An audit and a set of proper regression tests are needed.
  *     --Perry Metzger, Nov. 16, 2001
  */
-/*
- * Do only as much as necessary to compute constant expressions.
- * Called only if the operator allows folding and all operands are constants.
- */
 static tnode_t *
-fold(tnode_t *tn)
+fold_constant_integer(tnode_t *tn)
 {
 
 	val_t *v = xcalloc(1, sizeof(*v));
@@ -827,7 +823,7 @@ fold(tnode_t *tn)
 			si = (int64_t)(ul * ur);
 			if (si != (si & mask))
 				ovfl = true;
-			else if ((ul != 0) && ((si / ul) != ur))
+			else if (ul != 0 && si / ul != ur)
 				ovfl = true;
 		} else {
 			si = sl * sr;
@@ -954,7 +950,7 @@ build_struct_access(op_t op, bool sys, t
 	type_t *ptr_tp = expr_derive_type(rn->tn_type, PTR);
 	tnode_t *ntn = build_op(PLUS, sys, ptr_tp, ln, ctn);
 	if (ln->tn_op == CON)
-		ntn = fold(ntn);
+		ntn = fold_constant_integer(ntn);
 
 	op_t nop = rn->tn_type->t_bitfield ? FSEL : INDIR;
 	ntn = build_op(nop, sys, ntn->tn_type->t_subt, ntn, NULL);
@@ -1104,7 +1100,7 @@ build_plus_minus(op_t op, bool sys, tnod
 
 		tnode_t *prod = build_op(MULT, sys, rn->tn_type, rn, elsz);
 		if (rn->tn_op == CON)
-			prod = fold(prod);
+			prod = fold_constant_integer(prod);
 
 		return build_op(op, sys, ln->tn_type, ln, prod);
 	}
@@ -1117,7 +1113,7 @@ build_plus_minus(op_t op, bool sys, tnod
 		type_t *ptrdiff = gettyp(PTRDIFF_TSPEC);
 		tnode_t *raw_diff = build_op(op, sys, ptrdiff, ln, rn);
 		if (ln->tn_op == CON && rn->tn_op == CON)
-			raw_diff = fold(raw_diff);
+			raw_diff = fold_constant_integer(raw_diff);
 
 		tnode_t *elsz = subt_size_in_bytes(ln->tn_type);
 		balance(NOOP, &raw_diff, &elsz);
@@ -1308,7 +1304,7 @@ build_assignment(op_t op, bool sys, tnod
 			rn = convert(NOOP, 0, ctn->tn_type, rn);
 		rn = build_op(MULT, sys, rn->tn_type, rn, ctn);
 		if (rn->tn_left->tn_op == CON)
-			rn = fold(rn);
+			rn = fold_constant_integer(rn);
 	}
 
 	if ((op == ASSIGN || op == RETURN || op == INIT) &&
@@ -1454,11 +1450,8 @@ check_precedence_confusion(tnode_t *tn)
 	}
 }
 
-/*
- * Fold constant nodes, as much as is needed for comparing the value with 0.
- */
 static tnode_t *
-fold_bool(tnode_t *tn)
+fold_constant_compare_zero(tnode_t *tn)
 {
 
 	val_t *v = xcalloc(1, sizeof(*v));
@@ -1530,11 +1523,8 @@ is_floating_overflow(tspec_t t, long dou
 	return false;
 }
 
-/*
- * Fold constant nodes having operands with floating point type.
- */
 static tnode_t *
-fold_float(tnode_t *tn)
+fold_constant_floating(tnode_t *tn)
 {
 
 	fpe = 0;
@@ -1657,22 +1647,14 @@ build_binary(tnode_t *ln, op_t op, bool 
 	if (mp->m_binary && op != ARROW && op != POINT)
 		rn = cconv(rn);
 
-	/*
-	 * Print some warnings for comparisons of unsigned values with
-	 * constants lower than or equal to null. This must be done before
-	 * promote() because otherwise unsigned char and unsigned short would
-	 * be promoted to int. Types are also tested to be CHAR, which would
-	 * also become int.
-	 */
 	if (mp->m_comparison)
 		check_integer_comparison(op, ln, rn);
 
 	if (mp->m_value_context || mp->m_compares_with_zero)
 		ln = promote(op, false, ln);
 	if (mp->m_binary && op != ARROW && op != POINT &&
-	    op != ASSIGN && op != RETURN && op != INIT) {
+	    op != ASSIGN && op != RETURN && op != INIT)
 		rn = promote(op, false, rn);
-	}
 
 	if (mp->m_warn_if_left_unsigned_in_c90 &&
 	    ln->tn_op == CON && ln->tn_val.v_unsigned_since_c90) {
@@ -1768,23 +1750,22 @@ build_binary(tnode_t *ln, op_t op, bool 
 	if (hflag && !suppress_constcond &&
 	    mp->m_compares_with_zero &&
 	    (ln->tn_op == CON ||
-	     ((mp->m_binary && op != QUEST) && rn->tn_op == CON)) &&
+	     (mp->m_binary && op != QUEST && rn->tn_op == CON)) &&
 	    /* XXX: rn->tn_system_dependent should be checked as well */
 	    !ln->tn_system_dependent) {
 		/* constant in conditional context */
 		warning(161);
 	}
 
-	if (mp->m_fold_constant_operands) {
-		if (ln->tn_op == CON && (!mp->m_binary || rn->tn_op == CON)) {
-			if (mp->m_compares_with_zero) {
-				ntn = fold_bool(ntn);
-			} else if (is_floating(ntn->tn_type->t_tspec)) {
-				ntn = fold_float(ntn);
-			} else {
-				ntn = fold(ntn);
-			}
-		} else if (op == QUEST && ln->tn_op == CON) {
+	if (mp->m_fold_constant_operands && ln->tn_op == CON) {
+		if (!mp->m_binary || rn->tn_op == CON) {
+			if (mp->m_compares_with_zero)
+				ntn = fold_constant_compare_zero(ntn);
+			else if (is_floating(ntn->tn_type->t_tspec))
+				ntn = fold_constant_floating(ntn);
+			else
+				ntn = fold_constant_integer(ntn);
+		} else if (op == QUEST) {
 			lint_assert(has_operands(rn));
 			use(ln->tn_val.u.integer != 0
 			    ? rn->tn_right : rn->tn_left);
@@ -2337,7 +2318,7 @@ check_pointer_comparison(op_t op, const 
 
 	if (lst == VOID || rst == VOID) {
 		/* TODO: C99 behaves like C90 here. */
-		if ((!allow_trad && !allow_c99) &&
+		if (!allow_trad && !allow_c99 &&
 		    (lst == FUNC || rst == FUNC)) {
 			/* (void *)0 is already handled in typeok() */
 			const char *lsts, *rsts;
@@ -2356,7 +2337,7 @@ check_pointer_comparison(op_t op, const 
 
 	if (lst == FUNC && rst == FUNC) {
 		/* TODO: C99 behaves like C90 here, see C99 6.5.8p2. */
-		if ((!allow_trad && !allow_c99) && op != EQ && op != NE)
+		if (!allow_trad && !allow_c99 && op != EQ && op != NE)
 			/* pointers to functions can only be compared ... */
 			warning(125);
 	}
@@ -2554,7 +2535,7 @@ check_assign_void_pointer(op_t op, int a
 	/* two pointers, at least one pointer to void */
 
 	/* TODO: C99 behaves like C90 here. */
-	if (!((!allow_trad && !allow_c99) && (lst == FUNC || rst == FUNC)))
+	if (!(!allow_trad && !allow_c99 && (lst == FUNC || rst == FUNC)))
 		return;
 	/* comb. of ptr to func and ptr to void */
 
@@ -3515,7 +3496,7 @@ convert_pointer_from_pointer(type_t *ntp
 
 	if (nst == VOID || ost == VOID) {
 		/* TODO: C99 behaves like C90 here. */
-		if ((!allow_trad && !allow_c99) && (nst == FUNC || ost == FUNC)) {
+		if (!allow_trad && !allow_c99 && (nst == FUNC || ost == FUNC)) {
 			const char *nts, *ots;
 			/* null pointers are already handled in convert() */
 			*(nst == FUNC ? &nts : &ots) = "function pointer";
@@ -3829,8 +3810,8 @@ convert_constant_check_range(tspec_t ot,
 	} else if (op == ANDASS || op == BITAND) {
 		convert_constant_check_range_bitand(
 		    nbitsz, obitsz, xmask, nv, ot, v, tp, op);
-	} else if ((nt != PTR && is_uinteger(nt)) &&
-	    (ot != PTR && !is_uinteger(ot)) &&
+	} else if (nt != PTR && is_uinteger(nt) &&
+	    ot != PTR && !is_uinteger(ot) &&
 	    v->u.integer < 0)
 		convert_constant_check_range_signed(op, arg);
 	else if (nv->u.integer != v->u.integer && nbitsz <= obitsz &&

Reply via email to