Module Name:    src
Committed By:   rillig
Date:           Fri Jan 15 23:43:51 UTC 2021

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

Log Message:
lint: merge duplicate code for non-zero detection


To generate a diff of this commit:
cvs rdiff -u -r1.57 -r1.58 src/usr.bin/xlint/lint1/func.c
cvs rdiff -u -r1.53 -r1.54 src/usr.bin/xlint/lint1/lint1.h
cvs rdiff -u -r1.152 -r1.153 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/usr.bin/xlint/lint1/func.c
diff -u src/usr.bin/xlint/lint1/func.c:1.57 src/usr.bin/xlint/lint1/func.c:1.58
--- src/usr.bin/xlint/lint1/func.c:1.57	Tue Jan 12 20:42:01 2021
+++ src/usr.bin/xlint/lint1/func.c	Fri Jan 15 23:43:51 2021
@@ -1,4 +1,4 @@
-/*	$NetBSD: func.c,v 1.57 2021/01/12 20:42:01 rillig Exp $	*/
+/*	$NetBSD: func.c,v 1.58 2021/01/15 23:43:51 rillig Exp $	*/
 
 /*
  * Copyright (c) 1994, 1995 Jochen Pohl
@@ -37,7 +37,7 @@
 
 #include <sys/cdefs.h>
 #if defined(__RCSID) && !defined(lint)
-__RCSID("$NetBSD: func.c,v 1.57 2021/01/12 20:42:01 rillig Exp $");
+__RCSID("$NetBSD: func.c,v 1.58 2021/01/15 23:43:51 rillig Exp $");
 #endif
 
 #include <stdlib.h>
@@ -725,13 +725,8 @@ while1(tnode_t *tn)
 
 	pushctrl(T_WHILE);
 	cstmt->c_loop = 1;
-	if (tn != NULL && tn->tn_op == CON) {
-		if (is_integer(tn->tn_type->t_tspec)) {
-			cstmt->c_infinite = tn->tn_val->v_quad != 0;
-		} else {
-			cstmt->c_infinite = tn->tn_val->v_ldbl != 0.0;
-		}
-	}
+	if (tn != NULL && tn->tn_op == CON)
+		cstmt->c_infinite = is_nonzero(tn);
 
 	expr(tn, 0, 1, 1);
 }
@@ -790,11 +785,7 @@ do2(tnode_t *tn)
 		tn = check_controlling_expression(tn);
 
 	if (tn != NULL && tn->tn_op == CON) {
-		if (is_integer(tn->tn_type->t_tspec)) {
-			cstmt->c_infinite = tn->tn_val->v_quad != 0;
-		} else {
-			cstmt->c_infinite = tn->tn_val->v_ldbl != 0.0;
-		}
+		cstmt->c_infinite = is_nonzero(tn);
 		if (!cstmt->c_infinite && cstmt->c_cont)
 			/* continue in 'do ... while (0)' loop */
 			error(323);
@@ -850,15 +841,8 @@ for1(tnode_t *tn1, tnode_t *tn2, tnode_t
 	if (tn2 != NULL)
 		expr(tn2, 0, 1, 1);
 
-	if (tn2 == NULL) {
-		cstmt->c_infinite = 1;
-	} else if (tn2->tn_op == CON) {
-		if (is_integer(tn2->tn_type->t_tspec)) {
-			cstmt->c_infinite = tn2->tn_val->v_quad != 0;
-		} else {
-			cstmt->c_infinite = tn2->tn_val->v_ldbl != 0.0;
-		}
-	}
+	cstmt->c_infinite =
+	    tn2 == NULL || (tn2->tn_op == CON && is_nonzero(tn2));
 
 	/* Checking the reinitialisation expression is done in for2() */
 

Index: src/usr.bin/xlint/lint1/lint1.h
diff -u src/usr.bin/xlint/lint1/lint1.h:1.53 src/usr.bin/xlint/lint1/lint1.h:1.54
--- src/usr.bin/xlint/lint1/lint1.h:1.53	Mon Jan  4 22:26:50 2021
+++ src/usr.bin/xlint/lint1/lint1.h	Fri Jan 15 23:43:51 2021
@@ -1,4 +1,4 @@
-/* $NetBSD: lint1.h,v 1.53 2021/01/04 22:26:50 rillig Exp $ */
+/* $NetBSD: lint1.h,v 1.54 2021/01/15 23:43:51 rillig Exp $ */
 
 /*
  * Copyright (c) 1996 Christopher G. Demetriou.  All Rights Reserved.
@@ -459,3 +459,20 @@ check_printf(const char *fmt, ...)
 #  define gnuism(id, args...) wrap_check_printf(gnuism, id, ##args)
 #  define c99ism(id, args...) wrap_check_printf(c99ism, id, ##args)
 #endif
+
+static inline bool
+is_nonzero_val(tspec_t t, const val_t *val)
+{
+	return is_floating(t) ? val->v_ldbl != 0.0 : val->v_quad != 0;
+}
+
+static inline bool
+is_nonzero(const tnode_t *tn)
+{
+	/*
+	 * XXX: It's strange that val_t doesn't know itself whether it
+	 * holds a floating-point or an integer value.
+	 */
+	lint_assert(tn->tn_op == CON);
+	return is_nonzero_val(tn->tn_type->t_tspec, tn->tn_val);
+}

Index: src/usr.bin/xlint/lint1/tree.c
diff -u src/usr.bin/xlint/lint1/tree.c:1.152 src/usr.bin/xlint/lint1/tree.c:1.153
--- src/usr.bin/xlint/lint1/tree.c:1.152	Thu Jan 14 07:42:31 2021
+++ src/usr.bin/xlint/lint1/tree.c	Fri Jan 15 23:43:51 2021
@@ -1,4 +1,4 @@
-/*	$NetBSD: tree.c,v 1.152 2021/01/14 07:42:31 rillig Exp $	*/
+/*	$NetBSD: tree.c,v 1.153 2021/01/15 23:43:51 rillig Exp $	*/
 
 /*
  * Copyright (c) 1994, 1995 Jochen Pohl
@@ -37,7 +37,7 @@
 
 #include <sys/cdefs.h>
 #if defined(__RCSID) && !defined(lint)
-__RCSID("$NetBSD: tree.c,v 1.152 2021/01/14 07:42:31 rillig Exp $");
+__RCSID("$NetBSD: tree.c,v 1.153 2021/01/15 23:43:51 rillig Exp $");
 #endif
 
 #include <float.h>
@@ -2234,9 +2234,7 @@ convert_constant(op_t op, int arg, type_
 
 	if (nt == BOOL) {	/* C99 6.3.1.2 */
 		nv->v_ansiu = 0;
-		nv->v_quad = ot == FLOAT || ot == DOUBLE || ot == LDOUBLE
-		    ? v->v_ldbl != 0.0
-		    : v->v_quad != 0;
+		nv->v_quad = is_nonzero_val(ot, v) ? 1 : 0;
 		return;
 	}
 
@@ -3142,26 +3140,15 @@ fold(tnode_t *tn)
 static tnode_t *
 fold_test(tnode_t *tn)
 {
-	int	l, r = 0;
+	bool	l, r;
 	val_t	*v;
 
 	v = xcalloc(1, sizeof (val_t));
 	v->v_tspec = tn->tn_type->t_tspec;
 	lint_assert(v->v_tspec == INT || (Tflag && v->v_tspec == BOOL));
 
-	if (is_floating(tn->tn_left->tn_type->t_tspec)) {
-		l = tn->tn_left->tn_val->v_ldbl != 0.0;
-	} else {
-		l = tn->tn_left->tn_val->v_quad != 0;
-	}
-
-	if (modtab[tn->tn_op].m_binary) {
-		if (is_floating(tn->tn_right->tn_type->t_tspec)) {
-			r = tn->tn_right->tn_val->v_ldbl != 0.0;
-		} else {
-			r = tn->tn_right->tn_val->v_quad != 0;
-		}
-	}
+	l = is_nonzero(tn->tn_left);
+	r = modtab[tn->tn_op].m_binary && is_nonzero(tn->tn_right);
 
 	switch (tn->tn_op) {
 	case NOT:

Reply via email to