Mon Jan 4 22:41:56 2021 UTC ()
lint: finish the comments in check_precedence_confusion


(rillig)
diff -r1.2 -r1.3 src/tests/usr.bin/xlint/lint1/msg_169.c
diff -r1.128 -r1.129 src/usr.bin/xlint/lint1/tree.c

cvs diff -r1.2 -r1.3 src/tests/usr.bin/xlint/lint1/msg_169.c (expand / switch to unified diff)

--- src/tests/usr.bin/xlint/lint1/msg_169.c 2021/01/04 15:52:51 1.2
+++ src/tests/usr.bin/xlint/lint1/msg_169.c 2021/01/04 22:41:56 1.3
@@ -1,14 +1,14 @@ @@ -1,14 +1,14 @@
1/* $NetBSD: msg_169.c,v 1.2 2021/01/04 15:52:51 rillig Exp $ */ 1/* $NetBSD: msg_169.c,v 1.3 2021/01/04 22:41:56 rillig Exp $ */
2# 3 "msg_169.c" 2# 3 "msg_169.c"
3 3
4// Test for message: precedence confusion possible: parenthesize! [169] 4// Test for message: precedence confusion possible: parenthesize! [169]
5 5
6/* lint1-flags: -g -h -S -w */ 6/* lint1-flags: -g -h -S -w */
7 7
8typedef _Bool bool; 8typedef _Bool bool;
9 9
10void 10void
11confusing_shift_arith(unsigned a, unsigned b, unsigned c, unsigned char ch) 11confusing_shift_arith(unsigned a, unsigned b, unsigned c, unsigned char ch)
12{ 12{
13 unsigned con, okl, okr; 13 unsigned con, okl, okr;
14 14
@@ -136,15 +136,16 @@ cast_expressions(char a, char b, char c) @@ -136,15 +136,16 @@ cast_expressions(char a, char b, char c)
136 con = a | (unsigned)(b & c); 136 con = a | (unsigned)(b & c);
137 con = (unsigned)(a | b) & (unsigned)c; 137 con = (unsigned)(a | b) & (unsigned)c;
138 con = (unsigned)(a | b) & c; 138 con = (unsigned)(a | b) & c;
139} 139}
140 140
141void 141void
142expected_precedence(int a, int b, int c) 142expected_precedence(int a, int b, int c)
143{ 143{
144 int ok; 144 int ok;
145 145
146 ok = a + b * c; 146 ok = a + b * c;
147} 147}
148 148
149// TODO: add a test with unsigned long instead of unsigned, to demonstrate 149// TODO: add a test with unsigned long instead of unsigned, trying to
150// that the typo in 150// demonstrate that the typo in check_precedence_confusion actually has an
 151// effect.

cvs diff -r1.128 -r1.129 src/usr.bin/xlint/lint1/tree.c (expand / switch to unified diff)

--- src/usr.bin/xlint/lint1/tree.c 2021/01/04 22:33:47 1.128
+++ src/usr.bin/xlint/lint1/tree.c 2021/01/04 22:41:56 1.129
@@ -1,14 +1,14 @@ @@ -1,14 +1,14 @@
1/* $NetBSD: tree.c,v 1.128 2021/01/04 22:33:47 rillig Exp $ */ 1/* $NetBSD: tree.c,v 1.129 2021/01/04 22:41:56 rillig Exp $ */
2 2
3/* 3/*
4 * Copyright (c) 1994, 1995 Jochen Pohl 4 * Copyright (c) 1994, 1995 Jochen Pohl
5 * All Rights Reserved. 5 * All Rights Reserved.
6 * 6 *
7 * Redistribution and use in source and binary forms, with or without 7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions 8 * modification, are permitted provided that the following conditions
9 * are met: 9 * are met:
10 * 1. Redistributions of source code must retain the above copyright 10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer. 11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright 12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the 13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution. 14 * documentation and/or other materials provided with the distribution.
@@ -27,27 +27,27 @@ @@ -27,27 +27,27 @@
27 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 27 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
31 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 31 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 */ 32 */
33 33
34#if HAVE_NBTOOL_CONFIG_H 34#if HAVE_NBTOOL_CONFIG_H
35#include "nbtool_config.h" 35#include "nbtool_config.h"
36#endif 36#endif
37 37
38#include <sys/cdefs.h> 38#include <sys/cdefs.h>
39#if defined(__RCSID) && !defined(lint) 39#if defined(__RCSID) && !defined(lint)
40__RCSID("$NetBSD: tree.c,v 1.128 2021/01/04 22:33:47 rillig Exp $"); 40__RCSID("$NetBSD: tree.c,v 1.129 2021/01/04 22:41:56 rillig Exp $");
41#endif 41#endif
42 42
43#include <float.h> 43#include <float.h>
44#include <limits.h> 44#include <limits.h>
45#include <math.h> 45#include <math.h>
46#include <signal.h> 46#include <signal.h>
47#include <stdlib.h> 47#include <stdlib.h>
48#include <string.h> 48#include <string.h>
49 49
50#include "lint1.h" 50#include "lint1.h"
51#include "cgram.h" 51#include "cgram.h"
52 52
53static tnode_t *new_integer_constant_node(tspec_t, int64_t); 53static tnode_t *new_integer_constant_node(tspec_t, int64_t);
@@ -4006,26 +4006,39 @@ check_precedence_confusion(tnode_t *tn) @@ -4006,26 +4006,39 @@ check_precedence_confusion(tnode_t *tn)
4006 4006
4007 dprint_node(tn); 4007 dprint_node(tn);
4008 4008
4009 if (mp->m_binary) { 4009 if (mp->m_binary) {
4010 rparn = 0; 4010 rparn = 0;
4011 /* 4011 /*
4012 * FIXME: There is a typo "tn->tn_op == CVT", which should 4012 * FIXME: There is a typo "tn->tn_op == CVT", which should
4013 * rather be "rn->tn_op". Since tn must be a binary operator, 4013 * rather be "rn->tn_op". Since tn must be a binary operator,
4014 * it can never be CVT. 4014 * it can never be CVT.
4015 * 4015 *
4016 * Before fixing this though, there should be a unit test 4016 * Before fixing this though, there should be a unit test
4017 * that demonstrates an actual change in behavior when this 4017 * that demonstrates an actual change in behavior when this
4018 * bug gets fixed. 4018 * bug gets fixed.
 4019 *
 4020 * Right now, the condition is always false. To make it true
 4021 * after fixing the typo, the right-hand operand must be an
 4022 * explicit cast or an implicit conversion that is
 4023 * parenthesized. For the right-hand operand itself, this
 4024 * would already be done using the line below the loop.
 4025 *
 4026 * To make a difference, the right-hand operand must not be
 4027 * parenthesized, but its indirect cast or conversion must be.
 4028 *
 4029 * An implicit conversion is never parenthesized. Therefore
 4030 * this must be a cast that is later converted, to build a
 4031 * chain.
4019 */ 4032 */
4020 for (rn = tn->tn_right; tn->tn_op == CVT; rn = rn->tn_left) 4033 for (rn = tn->tn_right; tn->tn_op == CVT; rn = rn->tn_left)
4021 rparn |= rn->tn_parenthesized; 4034 rparn |= rn->tn_parenthesized;
4022 rparn |= rn->tn_parenthesized; 4035 rparn |= rn->tn_parenthesized;
4023 rop = rn->tn_op; 4036 rop = rn->tn_op;
4024 } 4037 }
4025 4038
4026 dowarn = 0; 4039 dowarn = 0;
4027 4040
4028 switch (tn->tn_op) { 4041 switch (tn->tn_op) {
4029 case SHL: 4042 case SHL:
4030 case SHR: 4043 case SHR:
4031 if (!lparn && (lop == PLUS || lop == MINUS)) { 4044 if (!lparn && (lop == PLUS || lop == MINUS)) {