Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/usr.bin/xlint/lint1 lint: clean up tree.c



details:   https://anonhg.NetBSD.org/src/rev/e8b7fd922107
branches:  trunk
changeset: 377185:e8b7fd922107
user:      rillig <rillig%NetBSD.org@localhost>
date:      Fri Jun 30 12:21:25 2023 +0000

description:
lint: clean up tree.c

No functional change.

diffstat:

 usr.bin/xlint/lint1/mem1.c |    8 +-
 usr.bin/xlint/lint1/tree.c |  536 +++++++++++++++++++-------------------------
 2 files changed, 232 insertions(+), 312 deletions(-)

diffs (truncated from 1216 to 300 lines):

diff -r b61975f9a4da -r e8b7fd922107 usr.bin/xlint/lint1/mem1.c
--- a/usr.bin/xlint/lint1/mem1.c        Fri Jun 30 09:26:03 2023 +0000
+++ b/usr.bin/xlint/lint1/mem1.c        Fri Jun 30 12:21:25 2023 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: mem1.c,v 1.65 2023/04/11 19:02:19 rillig Exp $ */
+/*     $NetBSD: mem1.c,v 1.66 2023/06/30 12:21:25 rillig Exp $ */
 
 /*
  * Copyright (c) 1994, 1995 Jochen Pohl
@@ -37,7 +37,7 @@
 
 #include <sys/cdefs.h>
 #if defined(__RCSID)
-__RCSID("$NetBSD: mem1.c,v 1.65 2023/04/11 19:02:19 rillig Exp $");
+__RCSID("$NetBSD: mem1.c,v 1.66 2023/06/30 12:21:25 rillig Exp $");
 #endif
 
 #include <sys/param.h>
@@ -247,7 +247,7 @@ expr_zero_alloc(size_t s)
 }
 
 static bool
-str_endswith(const char *haystack, const char *needle)
+str_ends_with(const char *haystack, const char *needle)
 {
        size_t hlen = strlen(haystack);
        size_t nlen = strlen(needle);
@@ -274,7 +274,7 @@ expr_alloc_tnode(void)
         */
        tn->tn_sys = in_system_header ||
                     (curr_pos.p_file != csrc_pos.p_file &&
-                     str_endswith(curr_pos.p_file, ".c"));
+                     str_ends_with(curr_pos.p_file, ".c"));
        return tn;
 }
 
diff -r b61975f9a4da -r e8b7fd922107 usr.bin/xlint/lint1/tree.c
--- a/usr.bin/xlint/lint1/tree.c        Fri Jun 30 09:26:03 2023 +0000
+++ b/usr.bin/xlint/lint1/tree.c        Fri Jun 30 12:21:25 2023 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: tree.c,v 1.535 2023/06/30 09:26:03 rillig Exp $        */
+/*     $NetBSD: tree.c,v 1.536 2023/06/30 12:21:25 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.535 2023/06/30 09:26:03 rillig Exp $");
+__RCSID("$NetBSD: tree.c,v 1.536 2023/06/30 12:21:25 rillig Exp $");
 #endif
 
 #include <float.h>
@@ -73,7 +73,7 @@ u64_fill_right(uint64_t x)
 }
 
 static bool
-str_endswith(const char *haystack, const char *needle)
+str_ends_with(const char *haystack, const char *needle)
 {
        size_t hlen = strlen(haystack);
        size_t nlen = strlen(needle);
@@ -370,9 +370,8 @@ new_tnode(op_t op, bool sys, type_t *typ
 tnode_t *
 build_constant(type_t *tp, val_t *v)
 {
-       tnode_t *n;
-
-       n = expr_alloc_tnode();
+
+       tnode_t *n = expr_alloc_tnode();
        n->tn_op = CON;
        n->tn_type = tp;
        n->tn_val.v_tspec = tp->t_tspec;
@@ -386,9 +385,8 @@ build_constant(type_t *tp, val_t *v)
 static tnode_t *
 build_integer_constant(tspec_t t, int64_t q)
 {
-       tnode_t *n;
-
-       n = expr_alloc_tnode();
+
+       tnode_t *n = expr_alloc_tnode();
        n->tn_op = CON;
        n->tn_type = gettyp(t);
        n->tn_val.v_tspec = t;
@@ -459,8 +457,8 @@ static bool
 is_gcc_bool_builtin(const char *name)
 {
        return strncmp(name, "__builtin_", 10) == 0 &&
-              (str_endswith(name, "_overflow") ||
-               str_endswith(name, "_overflow_p"));
+              (str_ends_with(name, "_overflow") ||
+               str_ends_with(name, "_overflow_p"));
 }
 
 static void
@@ -475,7 +473,6 @@ build_name_call(sym_t *sym)
                 */
                if (allow_gcc && is_gcc_bool_builtin(sym->s_name))
                        sym->s_type = gettyp(BOOL);
-
        } else if (allow_c99) {
                /* function '%s' implicitly declared to return int */
                error(215, sym->s_name);
@@ -492,7 +489,6 @@ build_name_call(sym_t *sym)
 tnode_t *
 build_name(sym_t *sym, bool is_funcname)
 {
-       tnode_t *n;
 
        if (sym->s_scl == NOSCL && !in_gcc_attribute) {
                sym->s_scl = EXTERN;
@@ -505,7 +501,7 @@ build_name(sym_t *sym, bool is_funcname)
 
        lint_assert(sym->s_kind == FVFT || sym->s_kind == FMEMBER);
 
-       n = expr_alloc_tnode();
+       tnode_t *n = expr_alloc_tnode();
        n->tn_type = sym->s_type;
        if (sym->s_scl == BOOL_CONST) {
                n->tn_op = CON;
@@ -532,19 +528,14 @@ build_name(sym_t *sym, bool is_funcname)
 tnode_t *
 build_string(strg_t *strg)
 {
-       size_t len;
-       tnode_t *n;
-       type_t *tp;
-
-       len = strg->st_len;
-
-       n = expr_alloc_tnode();
-
-       tp = expr_zero_alloc(sizeof(*tp));
+       size_t len = strg->st_len;
+
+       type_t *tp = expr_zero_alloc(sizeof(*tp));
        tp->t_tspec = ARRAY;
        tp->t_subt = gettyp(strg->st_char ? CHAR : WCHAR);
        tp->t_dim = (int)(len + 1);
 
+       tnode_t *n = expr_alloc_tnode();
        n->tn_op = STRING;
        n->tn_type = tp;
        n->tn_lvalue = true;
@@ -592,10 +583,9 @@ is_out_of_char_range(const tnode_t *tn)
 static void
 check_integer_comparison(op_t op, tnode_t *ln, tnode_t *rn)
 {
-       tspec_t lt, rt;
-
-       lt = ln->tn_type->t_tspec;
-       rt = rn->tn_type->t_tspec;
+
+       tspec_t lt = ln->tn_type->t_tspec;
+       tspec_t rt = rn->tn_type->t_tspec;
 
        if (ln->tn_op != CON && rn->tn_op != CON)
                return;
@@ -804,25 +794,21 @@ build_address(bool sys, tnode_t *tn, boo
 static tnode_t *
 fold(tnode_t *tn)
 {
-       val_t *v;
-       tspec_t t;
-       bool utyp, ovfl;
-       int64_t sl, sr = 0, q = 0, mask;
-       uint64_t ul, ur = 0;
-       tnode_t *cn;
-
-       v = xcalloc(1, sizeof(*v));
+
+       val_t *v = xcalloc(1, sizeof(*v));
        v->v_tspec = tn->tn_type->t_tspec;
 
-       t = tn->tn_left->tn_type->t_tspec;
-       utyp = !is_integer(t) || is_uinteger(t);
-       ul = sl = tn->tn_left->tn_val.v_quad;
+       tspec_t t = tn->tn_left->tn_type->t_tspec;
+       bool utyp = !is_integer(t) || is_uinteger(t);
+       int64_t sl = tn->tn_left->tn_val.v_quad, sr = 0;
+       uint64_t ul = sl, ur = 0;
        if (is_binary(tn))
                ur = sr = tn->tn_right->tn_val.v_quad;
 
-       mask = (int64_t)value_bits(size_in_bits(t));
-       ovfl = false;
-
+       int64_t mask = (int64_t)value_bits(size_in_bits(t));
+       bool ovfl = false;
+
+       int64_t q;
        switch (tn->tn_op) {
        case UPLUS:
                q = sl;
@@ -925,7 +911,7 @@ fold(tnode_t *tn)
                lint_assert(/*CONSTCOND*/false);
        }
 
-       /* XXX does not work for quads. */
+       /* XXX: The overflow check does not work for 64-bit integers. */
        if (ovfl ||
            ((uint64_t)(q | mask) != ~(uint64_t)0 && (q & ~mask) != 0)) {
                if (hflag)
@@ -935,7 +921,7 @@ fold(tnode_t *tn)
 
        v->v_quad = convert_integer(q, t, 0);
 
-       cn = build_constant(tn->tn_type, v);
+       tnode_t *cn = build_constant(tn->tn_type, v);
        if (tn->tn_left->tn_system_dependent)
                cn->tn_system_dependent = true;
        if (is_binary(tn) && tn->tn_right->tn_system_dependent)
@@ -950,8 +936,6 @@ fold(tnode_t *tn)
 static tnode_t *
 build_struct_access(op_t op, bool sys, tnode_t *ln, tnode_t *rn)
 {
-       tnode_t *ntn, *ctn;
-       bool nolval;
 
        lint_assert(rn->tn_op == NAME);
        lint_assert(is_member(rn->tn_sym));
@@ -960,7 +944,7 @@ build_struct_access(op_t op, bool sys, t
         * Remember if the left operand is an lvalue (structure members
         * are lvalues if and only if the structure itself is an lvalue).
         */
-       nolval = op == POINT && !ln->tn_lvalue;
+       bool nolval = op == POINT && !ln->tn_lvalue;
 
        if (op == POINT) {
                ln = build_address(sys, ln, true);
@@ -970,11 +954,11 @@ build_struct_access(op_t op, bool sys, t
                ln = convert(NOOP, 0, expr_derive_type(gettyp(VOID), PTR), ln);
        }
 
-       ctn = build_integer_constant(PTRDIFF_TSPEC,
+       tnode_t *ctn = build_integer_constant(PTRDIFF_TSPEC,
            rn->tn_sym->u.s_member.sm_offset_in_bits / CHAR_SIZE);
 
-       ntn = new_tnode(PLUS, sys, expr_derive_type(rn->tn_type, PTR),
-           ln, ctn);
+       type_t *ptr_tp = expr_derive_type(rn->tn_type, PTR);
+       tnode_t *ntn = new_tnode(PLUS, sys, ptr_tp, ln, ctn);
        if (ln->tn_op == CON)
                ntn = fold(ntn);
 
@@ -997,19 +981,17 @@ build_struct_access(op_t op, bool sys, t
 static tnode_t *
 subt_size_in_bytes(type_t *tp)
 {
-       int elem, elsz_in_bits;
 
        lint_assert(tp->t_tspec == PTR);
        tp = tp->t_subt;
 
-       elem = 1;
-       elsz_in_bits = 0;
-
+       int elem = 1;
        while (tp->t_tspec == ARRAY) {
                elem *= tp->t_dim;
                tp = tp->t_subt;
        }
 
+       int elsz_in_bits = 0;
        switch (tp->t_tspec) {
        case FUNC:
                /* pointer to function is not allowed here */
@@ -1059,33 +1041,23 @@ subt_size_in_bytes(type_t *tp)
 static tnode_t *
 build_prepost_incdec(op_t op, bool sys, tnode_t *ln)
 {
-       tnode_t *cn, *ntn;
 
        lint_assert(ln != NULL);
-
-       if (ln->tn_type->t_tspec == PTR) {
-               cn = subt_size_in_bytes(ln->tn_type);
-       } else {
-               cn = build_integer_constant(INT, (int64_t)1);
-       }
-       ntn = new_tnode(op, sys, ln->tn_type, ln, cn);
-
-       return ntn;
+       tnode_t *cn = ln->tn_type->t_tspec == PTR
+           ? subt_size_in_bytes(ln->tn_type)
+           : build_integer_constant(INT, 1);
+       return new_tnode(op, sys, ln->tn_type, ln, cn);
 }
 
 static void
 check_enum_array_index(const tnode_t *ln, const tnode_t *rn)
 {
-       int max_array_index;
-       int64_t max_enum_value;
-       const struct sym *ec, *max_ec;
-       const type_t *lt, *rt;
 



Home | Main Index | Thread Index | Old Index