Module Name:    src
Committed By:   rillig
Date:           Sat Apr 16 20:57:10 UTC 2022

Modified Files:
        src/tests/usr.bin/xlint/lint1: msg_161.c msg_161.exp
        src/usr.bin/xlint/lint1: lint1.h tree.c

Log Message:
lint: investigate why lint only warns about some constant conditions

Noticed in lex.c, in the macro kwdef, where lint complains about the
condition containing '||' but not about the conditions containing only
'>' or '=='.


To generate a diff of this commit:
cvs rdiff -u -r1.7 -r1.8 src/tests/usr.bin/xlint/lint1/msg_161.c
cvs rdiff -u -r1.6 -r1.7 src/tests/usr.bin/xlint/lint1/msg_161.exp
cvs rdiff -u -r1.152 -r1.153 src/usr.bin/xlint/lint1/lint1.h
cvs rdiff -u -r1.430 -r1.431 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/msg_161.c
diff -u src/tests/usr.bin/xlint/lint1/msg_161.c:1.7 src/tests/usr.bin/xlint/lint1/msg_161.c:1.8
--- src/tests/usr.bin/xlint/lint1/msg_161.c:1.7	Sun Mar 21 15:44:57 2021
+++ src/tests/usr.bin/xlint/lint1/msg_161.c	Sat Apr 16 20:57:10 2022
@@ -1,4 +1,4 @@
-/*	$NetBSD: msg_161.c,v 1.7 2021/03/21 15:44:57 rillig Exp $	*/
+/*	$NetBSD: msg_161.c,v 1.8 2022/04/16 20:57:10 rillig Exp $	*/
 # 3 "msg_161.c"
 
 // Test for message: constant in conditional context [161]
@@ -55,3 +55,22 @@ test_sizeof(void)
 	if (sizeof(int) < sizeof(char))
 		println("impossible");
 }
+
+const _Bool conditions[] = {
+	/* XXX: Why no warning here? */
+	13 < 13,
+	/* XXX: Why no warning here? */
+	0 < 0,
+	/* XXX: Why no warning here? */
+	0 != 0,
+	/* expect+1: warning: constant in conditional context [161] */
+	0 == 0 && 1 == 0,
+	/* expect+1: warning: constant in conditional context [161] */
+	1 == 0 || 2 == 1,
+	/* expect+2: warning: constant in conditional context [161] */
+	/* expect+1: error: non-constant initializer [177] */
+	0 == 0 && ""[0] == '\0',
+	/* expect+2: warning: constant in conditional context [161] */
+	/* expect+1: error: non-constant initializer [177] */
+	""[0] == '\0' && 0 == 0,
+};

Index: src/tests/usr.bin/xlint/lint1/msg_161.exp
diff -u src/tests/usr.bin/xlint/lint1/msg_161.exp:1.6 src/tests/usr.bin/xlint/lint1/msg_161.exp:1.7
--- src/tests/usr.bin/xlint/lint1/msg_161.exp:1.6	Sun Mar 21 15:44:57 2021
+++ src/tests/usr.bin/xlint/lint1/msg_161.exp	Sat Apr 16 20:57:10 2022
@@ -2,3 +2,9 @@ msg_161.c(11): warning: constant in cond
 msg_161.c(18): warning: constant in conditional context [161]
 msg_161.c(19): warning: statement not reached [193]
 msg_161.c(41): warning: constant in conditional context [161]
+msg_161.c(67): warning: constant in conditional context [161]
+msg_161.c(69): warning: constant in conditional context [161]
+msg_161.c(72): warning: constant in conditional context [161]
+msg_161.c(72): error: non-constant initializer [177]
+msg_161.c(75): warning: constant in conditional context [161]
+msg_161.c(75): error: non-constant initializer [177]

Index: src/usr.bin/xlint/lint1/lint1.h
diff -u src/usr.bin/xlint/lint1/lint1.h:1.152 src/usr.bin/xlint/lint1/lint1.h:1.153
--- src/usr.bin/xlint/lint1/lint1.h:1.152	Sat Apr 16 19:18:17 2022
+++ src/usr.bin/xlint/lint1/lint1.h	Sat Apr 16 20:57:10 2022
@@ -1,4 +1,4 @@
-/* $NetBSD: lint1.h,v 1.152 2022/04/16 19:18:17 rillig Exp $ */
+/* $NetBSD: lint1.h,v 1.153 2022/04/16 20:57:10 rillig Exp $ */
 
 /*
  * Copyright (c) 1996 Christopher G. Demetriou.  All Rights Reserved.
@@ -493,6 +493,7 @@ check_printf(const char *fmt, ...)
 
 #  define wrap_check_printf(func, msgid, args...)			\
 	({								\
+		debug_step("%s:%d: %s", __FILE__, __LINE__, __func__);	\
 		check_printf(__CONCAT(MSG_, msgid), ##args);		\
 		(func)(msgid, ##args);					\
 		/* LINTED 129 */					\

Index: src/usr.bin/xlint/lint1/tree.c
diff -u src/usr.bin/xlint/lint1/tree.c:1.430 src/usr.bin/xlint/lint1/tree.c:1.431
--- src/usr.bin/xlint/lint1/tree.c:1.430	Sat Apr 16 20:02:55 2022
+++ src/usr.bin/xlint/lint1/tree.c	Sat Apr 16 20:57:10 2022
@@ -1,4 +1,4 @@
-/*	$NetBSD: tree.c,v 1.430 2022/04/16 20:02:55 rillig Exp $	*/
+/*	$NetBSD: tree.c,v 1.431 2022/04/16 20:57:10 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.430 2022/04/16 20:02:55 rillig Exp $");
+__RCSID("$NetBSD: tree.c,v 1.431 2022/04/16 20:57:10 rillig Exp $");
 #endif
 
 #include <float.h>
@@ -534,7 +534,7 @@ build_binary(tnode_t *ln, op_t op, bool 
 
 	/*
 	 * Apply class conversions to the left operand, but only if its
-	 * value is needed or it is compared with null.
+	 * value is needed or it is compared with zero.
 	 */
 	if (mp->m_left_value_context || mp->m_left_test_context)
 		ln = cconv(ln);
@@ -549,21 +549,14 @@ build_binary(tnode_t *ln, op_t op, bool 
 	 * 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. Also types are tested to be
+	 * 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);
 
-	/*
-	 * Promote the left operand if it is in a test or value context
-	 */
 	if (mp->m_left_value_context || mp->m_left_test_context)
 		ln = promote(op, false, ln);
-	/*
-	 * Promote the right operand, but only if it is no struct or
-	 * union member, or if it is not to be assigned to the left operand
-	 */
 	if (mp->m_binary && op != ARROW && op != POINT &&
 	    op != ASSIGN && op != RETURN && op != INIT) {
 		rn = promote(op, false, rn);
@@ -653,7 +646,7 @@ build_binary(tnode_t *ln, op_t op, bool 
 	default:
 		rettp = mp->m_returns_bool
 		    ? gettyp(Tflag ? BOOL : INT) : ln->tn_type;
-		lint_assert(mp->m_binary || rn == NULL);
+		lint_assert(mp->m_binary == (rn != NULL));
 		ntn = new_tnode(op, sys, rettp, ln, rn);
 		break;
 	}
@@ -670,14 +663,14 @@ build_binary(tnode_t *ln, op_t op, bool 
 	 * Print a warning if one of the operands is in a context where
 	 * it is compared with zero and if this operand is a constant.
 	 */
-	if (mp->m_left_test_context) {
-		if (ln->tn_op == CON ||
-		    ((mp->m_binary && op != QUEST) && rn->tn_op == CON)) {
-			if (hflag && !constcond_flag &&
-			    !ln->tn_system_dependent)
-				/* constant in conditional context */
-				warning(161);
-		}
+	if (hflag && !constcond_flag &&
+	    mp->m_left_test_context &&
+	    (ln->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);
 	}
 
 	/* Fold if the operator requires it */

Reply via email to