Module Name: src
Committed By: rillig
Date: Sat Jan 9 23:02:51 UTC 2021
Modified Files:
src/usr.bin/xlint/lint1: tree.c
Log Message:
lint: move pointer subtypes from typeok to the sub-functions
The code in typeok is already complicated enough. Only few of the
checks actually examine the subtype of the pointer, the others don't
need to look at it. Therefore don't initialize the variables lstp
(left-hand side subtype) and rstp unless they are actually needed.
This reduces the number of variables in typeok and the number of
parameters to the sub-functions.
To generate a diff of this commit:
cvs rdiff -u -r1.141 -r1.142 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/usr.bin/xlint/lint1/tree.c
diff -u src/usr.bin/xlint/lint1/tree.c:1.141 src/usr.bin/xlint/lint1/tree.c:1.142
--- src/usr.bin/xlint/lint1/tree.c:1.141 Sat Jan 9 19:13:17 2021
+++ src/usr.bin/xlint/lint1/tree.c Sat Jan 9 23:02:51 2021
@@ -1,4 +1,4 @@
-/* $NetBSD: tree.c,v 1.141 2021/01/09 19:13:17 rillig Exp $ */
+/* $NetBSD: tree.c,v 1.142 2021/01/09 23:02:51 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.141 2021/01/09 19:13:17 rillig Exp $");
+__RCSID("$NetBSD: tree.c,v 1.142 2021/01/09 23:02:51 rillig Exp $");
#endif
#include <float.h>
@@ -770,7 +770,7 @@ typeok_plus(op_t op, tspec_t lt, tspec_t
}
static bool
-typeok_minus(op_t op, tspec_t lt, type_t *lstp, tspec_t rt, type_t *rstp)
+typeok_minus(op_t op, type_t *ltp, tspec_t lt, type_t *rtp, tspec_t rt)
{
/* operands have scalar types (checked above) */
if (lt == PTR && (!tspec_is_int(rt) && rt != PTR)) {
@@ -781,7 +781,7 @@ typeok_minus(op_t op, tspec_t lt, type_t
return false;
}
if (lt == PTR && rt == PTR) {
- if (!eqtype(lstp, rstp, 1, 0, NULL)) {
+ if (!eqtype(ltp->t_subt, rtp->t_subt, 1, 0, NULL)) {
/* illegal pointer subtraction */
error(116);
}
@@ -873,15 +873,14 @@ typeok_shift(tspec_t lt, tnode_t *rn, ts
}
static bool
-typeok_eq(tnode_t *ln, tspec_t lt, tspec_t lst,
- tnode_t *rn, tspec_t rt, tspec_t rst)
+typeok_eq(tnode_t *ln, tspec_t lt, tnode_t *rn, tspec_t rt)
{
- if (lt == PTR && ((rt == PTR && rst == VOID) ||
+ if (lt == PTR && ((rt == PTR && rn->tn_type->t_tspec == VOID) ||
tspec_is_int(rt))) {
if (rn->tn_op == CON && rn->tn_val->v_quad == 0)
return true;
}
- if (rt == PTR && ((lt == PTR && lst == VOID) ||
+ if (rt == PTR && ((lt == PTR && ln->tn_type->t_tspec == VOID) ||
tspec_is_int(lt))) {
if (ln->tn_op == CON && ln->tn_val->v_quad == 0)
return true;
@@ -929,9 +928,12 @@ typeok_quest(tspec_t lt, tnode_t **rn)
static bool
typeok_colon(mod_t *mp,
- tnode_t *ln, type_t *ltp, tspec_t lt, type_t *lstp, tspec_t lst,
- tnode_t *rn, type_t *rtp, tspec_t rt, type_t *rstp, tspec_t rst)
+ tnode_t *ln, type_t *ltp, tspec_t lt,
+ tnode_t *rn, type_t *rtp, tspec_t rt)
{
+ type_t *lstp, *rstp;
+ tspec_t lst, rst;
+
if (tspec_is_arith(lt) && tspec_is_arith(rt))
return true;
@@ -940,6 +942,11 @@ typeok_colon(mod_t *mp,
if (lt == UNION && rt == UNION && ltp->t_str == rtp->t_str)
return true;
+ lstp = lt == PTR ? ltp->t_subt : NULL;
+ rstp = rt == PTR ? rtp->t_subt : NULL;
+ lst = lstp != NULL ? lstp->t_tspec : NOTSPEC;
+ rst = rstp != NULL ? rstp->t_tspec : NOTSPEC;
+
/* combination of any pointer and 0, 0L or (void *)0 is ok */
if (lt == PTR && ((rt == PTR && rst == VOID) || tspec_is_int(rt))) {
if (rn->tn_op == CON && rn->tn_val->v_quad == 0)
@@ -1024,21 +1031,21 @@ bool
typeok(op_t op, int arg, tnode_t *ln, tnode_t *rn)
{
mod_t *mp;
- tspec_t lt, rt = NOTSPEC, lst = NOTSPEC, rst = NOTSPEC, olt = NOTSPEC,
- ort = NOTSPEC;
- type_t *ltp, *rtp = NULL, *lstp = NULL, *rstp = NULL;
+ tspec_t lt, rt, olt = NOTSPEC, ort = NOTSPEC;
+ type_t *ltp, *rtp;
tnode_t *tn;
mp = &modtab[op];
lint_assert((ltp = ln->tn_type) != NULL);
+ lt = ltp->t_tspec;
- if ((lt = ltp->t_tspec) == PTR)
- lst = (lstp = ltp->t_subt)->t_tspec;
if (mp->m_binary) {
lint_assert((rtp = rn->tn_type) != NULL);
- if ((rt = rtp->t_tspec) == PTR)
- rst = (rstp = rtp->t_subt)->t_tspec;
+ rt = rtp->t_tspec;
+ } else {
+ rtp = NULL;
+ rt = NOTSPEC;
}
if (mp->m_requires_integer) {
@@ -1126,7 +1133,7 @@ typeok(op_t op, int arg, tnode_t *ln, tn
return false;
break;
case MINUS:
- if (!typeok_minus(op, lt, lstp, rt, rstp))
+ if (!typeok_minus(op, ltp, lt, rtp, rt))
return false;
break;
case SHR:
@@ -1143,7 +1150,7 @@ typeok(op_t op, int arg, tnode_t *ln, tn
* Accept some things which are allowed with EQ and NE,
* but not with ordered comparisons.
*/
- if (typeok_eq(ln, lt, lst, rn, rt, rst))
+ if (typeok_eq(ln, lt, rn, rt))
break;
/* FALLTHROUGH */
case LT:
@@ -1159,9 +1166,7 @@ typeok(op_t op, int arg, tnode_t *ln, tn
return false;
break;
case COLON:
- if (!typeok_colon(mp,
- ln, ltp, lt, lstp, lst,
- rn, rtp, rt, rstp, rst))
+ if (!typeok_colon(mp, ln, ltp, lt, rn, rtp, rt))
return false;
break;
case ASSIGN: