Mon Jan 4 23:17:04 2021 UTC ()
lint: add more rationale for removing effect-less code


(rillig)
diff -r1.129 -r1.130 src/usr.bin/xlint/lint1/tree.c

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

--- src/usr.bin/xlint/lint1/tree.c 2021/01/04 22:41:56 1.129
+++ src/usr.bin/xlint/lint1/tree.c 2021/01/04 23:17:03 1.130
@@ -1,14 +1,14 @@ @@ -1,14 +1,14 @@
1/* $NetBSD: tree.c,v 1.129 2021/01/04 22:41:56 rillig Exp $ */ 1/* $NetBSD: tree.c,v 1.130 2021/01/04 23:17:03 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.129 2021/01/04 22:41:56 rillig Exp $"); 40__RCSID("$NetBSD: tree.c,v 1.130 2021/01/04 23:17:03 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);
@@ -4007,38 +4007,42 @@ check_precedence_confusion(tnode_t *tn) @@ -4007,38 +4007,42 @@ check_precedence_confusion(tnode_t *tn)
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 * 4019 *
4020 * Right now, the condition is always false. To make it true 4020 * rn must be a chain of casts and conversions, and at least
4021 * after fixing the typo, the right-hand operand must be an 4021 * one of these must be a parenthesized cast.
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 * 4022 *
4026 * To make a difference, the right-hand operand must not be 4023 * The argument of the innermost cast or conversion must not
4027 * parenthesized, but its indirect cast or conversion must be. 4024 * be parenthesized.
4028 * 4025 *
4029 * An implicit conversion is never parenthesized. Therefore 4026 * The argument of the innermost cast or conversion must be
4030 * this must be a cast that is later converted, to build a 4027 * an expression with confusing precedence. Since all these
4031 * chain. 4028 * expressions have lower precedence than a cast, these can
 4029 * only appear as a parenthesized expression. This in turn
 4030 * makes the whole loop superfluous.
 4031 *
 4032 * An edge case might be due to constant folding, if the
 4033 * nodes created from constant folding did not preserve
 4034 * tn_parenthesized properly. But that would be another bug,
 4035 * so it doesn't count as an argument.
4032 */ 4036 */
4033 for (rn = tn->tn_right; tn->tn_op == CVT; rn = rn->tn_left) 4037 for (rn = tn->tn_right; tn->tn_op == CVT; rn = rn->tn_left)
4034 rparn |= rn->tn_parenthesized; 4038 rparn |= rn->tn_parenthesized;
4035 rparn |= rn->tn_parenthesized; 4039 rparn |= rn->tn_parenthesized;
4036 rop = rn->tn_op; 4040 rop = rn->tn_op;
4037 } 4041 }
4038 4042
4039 dowarn = 0; 4043 dowarn = 0;
4040 4044
4041 switch (tn->tn_op) { 4045 switch (tn->tn_op) {
4042 case SHL: 4046 case SHL:
4043 case SHR: 4047 case SHR:
4044 if (!lparn && (lop == PLUS || lop == MINUS)) { 4048 if (!lparn && (lop == PLUS || lop == MINUS)) {