Tue Jan 5 17:37:57 2021 UTC ()
lint: make check_precedence_confusion simpler

In C, only binary operators have possibly confusing precedence.  All
binary operators have lower precedence than an explicit cast.  When an
expression is parsed, the parentheses are associated with the innermost
possible node.  This means that as soon as a cast operator is
parenthesized, its contained expression can no longer have confusing
precedence.

This allows the code to be written more succinct since the local
variables are no longer necessary.


(rillig)
diff -r1.134 -r1.135 src/usr.bin/xlint/lint1/tree.c

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

--- src/usr.bin/xlint/lint1/tree.c 2021/01/05 00:17:21 1.134
+++ src/usr.bin/xlint/lint1/tree.c 2021/01/05 17:37:57 1.135
@@ -1,14 +1,14 @@ @@ -1,14 +1,14 @@
1/* $NetBSD: tree.c,v 1.134 2021/01/05 00:17:21 rillig Exp $ */ 1/* $NetBSD: tree.c,v 1.135 2021/01/05 17:37:57 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.134 2021/01/05 00:17:21 rillig Exp $"); 40__RCSID("$NetBSD: tree.c,v 1.135 2021/01/05 17:37:57 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);
@@ -4017,43 +4017,33 @@ is_confusing_precedence(op_t op, op_t lo @@ -4017,43 +4017,33 @@ is_confusing_precedence(op_t op, op_t lo
4017 return false; 4017 return false;
4018} 4018}
4019 4019
4020/* 4020/*
4021 * Print a warning if the given node has operands which should be 4021 * Print a warning if the given node has operands which should be
4022 * parenthesized. 4022 * parenthesized.
4023 * 4023 *
4024 * XXX Does not work if an operand is a constant expression. Constant 4024 * XXX Does not work if an operand is a constant expression. Constant
4025 * expressions are already folded. 4025 * expressions are already folded.
4026 */ 4026 */
4027static void 4027static void
4028check_precedence_confusion(tnode_t *tn) 4028check_precedence_confusion(tnode_t *tn)
4029{ 4029{
4030 tnode_t *ln, *rn; 4030 tnode_t *ln, *rn;
4031 op_t lop, rop; 
4032 bool lparn, rparn; 
4033 mod_t *mp; 
4034 4031
4035 if (!hflag) 4032 if (!hflag)
4036 return; 4033 return;
4037 4034
4038 mp = &modtab[tn->tn_op]; 
4039 lint_assert(mp->m_binary); 
4040 
4041 dprint_node(tn); 4035 dprint_node(tn);
4042 4036
4043 lparn = false; 4037 lint_assert(modtab[tn->tn_op].m_binary);
4044 for (ln = tn->tn_left; ln->tn_op == CVT; ln = ln->tn_left) 4038 for (ln = tn->tn_left; ln->tn_op == CVT; ln = ln->tn_left)
4045 lparn |= ln->tn_parenthesized; 4039 continue;
4046 lparn |= ln->tn_parenthesized; 
4047 lop = ln->tn_op; 
4048 
4049 rparn = false; 
4050 for (rn = tn->tn_right; rn->tn_op == CVT; rn = rn->tn_left) 4040 for (rn = tn->tn_right; rn->tn_op == CVT; rn = rn->tn_left)
4051 rparn |= rn->tn_parenthesized; 4041 continue;
4052 rparn |= rn->tn_parenthesized; 
4053 rop = rn->tn_op; 
4054 4042
4055 if (is_confusing_precedence(tn->tn_op, lop, lparn, rop, rparn)) { 4043 if (is_confusing_precedence(tn->tn_op,
 4044 ln->tn_op, ln->tn_parenthesized,
 4045 rn->tn_op, rn->tn_parenthesized)) {
4056 /* precedence confusion possible: parenthesize! */ 4046 /* precedence confusion possible: parenthesize! */
4057 warning(169); 4047 warning(169);
4058 } 4048 }
4059} 4049}