Module Name:    src
Committed By:   rillig
Date:           Sun Dec 15 06:04:17 UTC 2024

Modified Files:
        src/tests/usr.bin/xlint/lint1: msg_169.c
        src/usr.bin/xlint/lint1: tree.c

Log Message:
lint: merge duplicate code for possibly confusing precedence


To generate a diff of this commit:
cvs rdiff -u -r1.9 -r1.10 src/tests/usr.bin/xlint/lint1/msg_169.c
cvs rdiff -u -r1.663 -r1.664 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_169.c
diff -u src/tests/usr.bin/xlint/lint1/msg_169.c:1.9 src/tests/usr.bin/xlint/lint1/msg_169.c:1.10
--- src/tests/usr.bin/xlint/lint1/msg_169.c:1.9	Sun Dec 15 05:08:42 2024
+++ src/tests/usr.bin/xlint/lint1/msg_169.c	Sun Dec 15 06:04:17 2024
@@ -1,4 +1,4 @@
-/*	$NetBSD: msg_169.c,v 1.9 2024/12/15 05:08:42 rillig Exp $	*/
+/*	$NetBSD: msg_169.c,v 1.10 2024/12/15 06:04:17 rillig Exp $	*/
 # 3 "msg_169.c"
 
 // Test for message: possible precedence confusion between '%s' and '%s' [169]
@@ -50,9 +50,9 @@ confusing_shift_arith(unsigned a, unsign
 }
 
 void
-confusing_logical(bool a, bool b, bool c)
+confusing_logical(bool a, bool b, bool c, bool d)
 {
-	bool con, okl, okr, eql;
+	bool con, okl, okr, okb, eql;
 
 	eql = a && b && c;
 	eql = a || b || c;
@@ -66,6 +66,16 @@ confusing_logical(bool a, bool b, bool c
 	con = a || b && c;
 	okl = (a || b) && c;
 	okr = a || (b && c);
+
+	// When both nested operands have confusing precedence, there's only
+	// a single warning, as that is enough to point to the issue.
+	/* expect+1: warning: possible precedence confusion between '||' and '&&' [169] */
+	con = a && b || c && d;
+	/* expect+1: warning: possible precedence confusion between '||' and '&&' [169] */
+	okl = (a && b) || c && d;
+	/* expect+1: warning: possible precedence confusion between '||' and '&&' [169] */
+	okr = a && b || (c && d);
+	okb = (a && b) || (c && d);
 }
 
 void

Index: src/usr.bin/xlint/lint1/tree.c
diff -u src/usr.bin/xlint/lint1/tree.c:1.663 src/usr.bin/xlint/lint1/tree.c:1.664
--- src/usr.bin/xlint/lint1/tree.c:1.663	Sun Dec 15 05:08:42 2024
+++ src/usr.bin/xlint/lint1/tree.c	Sun Dec 15 06:04:17 2024
@@ -1,4 +1,4 @@
-/*	$NetBSD: tree.c,v 1.663 2024/12/15 05:08:42 rillig Exp $	*/
+/*	$NetBSD: tree.c,v 1.664 2024/12/15 06:04:17 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.663 2024/12/15 05:08:42 rillig Exp $");
+__RCSID("$NetBSD: tree.c,v 1.664 2024/12/15 06:04:17 rillig Exp $");
 #endif
 
 #include <float.h>
@@ -1581,32 +1581,28 @@ build_real_imag(op_t op, bool sys, tnode
 }
 
 static bool
-is_confusing_precedence(op_t op, op_t lop, op_t rop, op_t *cop)
+is_confusing_precedence(op_t op, const tnode_t *operand, op_t *cop)
 {
+	if (operand->tn_parenthesized)
+		return false;
+	op_t oop = operand->tn_op;
 
 	if (op == SHL || op == SHR) {
-		if (lop == PLUS || lop == MINUS)
-			return *cop = lop, true;
-		if (rop == PLUS || rop == MINUS)
-			return *cop = rop, true;
+		if (oop == PLUS || oop == MINUS)
+			return *cop = oop, true;
 		return false;
 	}
 
 	if (op == LOGOR) {
-		if (lop == LOGAND)
-			return *cop = lop, true;
-		if (rop == LOGAND)
-			return *cop = rop, true;
+		if (oop == LOGAND)
+			return *cop = oop, true;
 		return false;
 	}
 
 	lint_assert(op == BITAND || op == BITXOR || op == BITOR);
-	if (lop != op
-	    && (lop == PLUS || lop == MINUS || lop == BITAND || lop == BITXOR))
-		return *cop = lop, true;
-	if (rop != op
-	    && (rop == PLUS || rop == MINUS || rop == BITAND || rop == BITXOR))
-		return *cop = rop, true;
+	if (oop != op
+	    && (oop == PLUS || oop == MINUS || oop == BITAND || oop == BITXOR))
+		return *cop = oop, true;
 	return false;
 }
 
@@ -1634,9 +1630,8 @@ check_precedence_confusion(tnode_t *tn)
 		continue;
 
 	op_t cop;
-	if (is_confusing_precedence(tn->tn_op,
-	    ln->tn_parenthesized ? NOOP : ln->tn_op,
-	    rn->tn_parenthesized ? NOOP : rn->tn_op, &cop)) {
+	if (is_confusing_precedence(tn->tn_op, ln, &cop) ||
+	    is_confusing_precedence(tn->tn_op, rn, &cop)) {
 		/* possible precedence confusion between '%s' and '%s' */
 		warning(169, op_name(tn->tn_op), op_name(cop));
 	}

Reply via email to