Source-Changes-HG archive

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

[src/trunk]: src/usr.bin/xlint lint: use standard terminology for bit-field w...



details:   https://anonhg.NetBSD.org/src/rev/ed7bd1ebee17
branches:  trunk
changeset: 376699:ed7bd1ebee17
user:      rillig <rillig%NetBSD.org@localhost>
date:      Thu Jun 29 12:52:06 2023 +0000

description:
lint: use standard terminology for bit-field width

No functional change.

diffstat:

 usr.bin/xlint/common/tyname.c |   6 +-
 usr.bin/xlint/lint1/decl.c    |  85 +++++++++++++++++++-----------------------
 usr.bin/xlint/lint1/lint1.h   |  16 +++-----
 usr.bin/xlint/lint1/tree.c    |  28 ++++++++------
 4 files changed, 63 insertions(+), 72 deletions(-)

diffs (truncated from 316 to 300 lines):

diff -r 66143d559fb4 -r ed7bd1ebee17 usr.bin/xlint/common/tyname.c
--- a/usr.bin/xlint/common/tyname.c     Thu Jun 29 10:31:32 2023 +0000
+++ b/usr.bin/xlint/common/tyname.c     Thu Jun 29 12:52:06 2023 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: tyname.c,v 1.55 2023/04/22 17:49:14 rillig Exp $       */
+/*     $NetBSD: tyname.c,v 1.56 2023/06/29 12:52:06 rillig Exp $       */
 
 /*-
  * Copyright (c) 2005 The NetBSD Foundation, Inc.
@@ -35,7 +35,7 @@
 
 #include <sys/cdefs.h>
 #if defined(__RCSID)
-__RCSID("$NetBSD: tyname.c,v 1.55 2023/04/22 17:49:14 rillig Exp $");
+__RCSID("$NetBSD: tyname.c,v 1.56 2023/06/29 12:52:06 rillig Exp $");
 #endif
 
 #include <assert.h>
@@ -263,7 +263,7 @@ type_name(const type_t *tp)
 #ifdef IS_LINT1
        if (tp->t_bitfield) {
                buf_add(&buf, ":");
-               buf_add_int(&buf, (int)tp->t_flen);
+               buf_add_int(&buf, (int)tp->t_bit_field_width);
        }
 #endif
 
diff -r 66143d559fb4 -r ed7bd1ebee17 usr.bin/xlint/lint1/decl.c
--- a/usr.bin/xlint/lint1/decl.c        Thu Jun 29 10:31:32 2023 +0000
+++ b/usr.bin/xlint/lint1/decl.c        Thu Jun 29 12:52:06 2023 +0000
@@ -1,4 +1,4 @@
-/* $NetBSD: decl.c,v 1.321 2023/06/29 10:31:33 rillig Exp $ */
+/* $NetBSD: decl.c,v 1.322 2023/06/29 12:52:06 rillig Exp $ */
 
 /*
  * Copyright (c) 1996 Christopher G. Demetriou.  All Rights Reserved.
@@ -38,7 +38,7 @@
 
 #include <sys/cdefs.h>
 #if defined(__RCSID)
-__RCSID("$NetBSD: decl.c,v 1.321 2023/06/29 10:31:33 rillig Exp $");
+__RCSID("$NetBSD: decl.c,v 1.322 2023/06/29 12:52:06 rillig Exp $");
 #endif
 
 #include <sys/param.h>
@@ -456,14 +456,16 @@ set_first_typedef(type_t *tp, sym_t *sym
 }
 
 static unsigned int
-bit_field_size(sym_t **mem)
+bit_field_width(sym_t **mem)
 {
-       unsigned int len = (*mem)->s_type->t_flen;
+       unsigned int width = (*mem)->s_type->t_bit_field_width;
        while (*mem != NULL && (*mem)->s_type->t_bitfield) {
-               len += (*mem)->s_type->t_flen;
+               width += (*mem)->s_type->t_bit_field_width;
                *mem = (*mem)->s_next;
        }
-       return len - len % INT_SIZE;
+       // XXX: Why INT_SIZE? C99 6.7.2.1p4 allows bit-fields to have type
+       // XXX: _Bool or another implementation-defined type.
+       return width - width % INT_SIZE;
 }
 
 static void
@@ -482,7 +484,7 @@ set_packed_size(type_t *tp)
                        unsigned int x;
 
                        if (mem->s_type->t_bitfield) {
-                               sp->sou_size_in_bits += bit_field_size(&mem);
+                               sp->sou_size_in_bits += bit_field_width(&mem);
                                if (mem == NULL)
                                        break;
                        }
@@ -1047,10 +1049,11 @@ check_bit_field_type(sym_t *dsym, type_t
                /* illegal bit-field type '%s' */
                warning(35, type_name(btp));
 
-               unsigned int sz = tp->t_flen;
+               // XXX: What about _Bool bit-fields since C99 6.7.2.1?
+               unsigned int width = tp->t_bit_field_width;
                dsym->s_type = tp = block_dup_type(gettyp(t = INT));
-               if ((tp->t_flen = sz) > size_in_bits(t))
-                       tp->t_flen = size_in_bits(t);
+               if ((tp->t_bit_field_width = width) > size_in_bits(t))
+                       tp->t_bit_field_width = size_in_bits(t);
                *inout_t = t;
                *inout_tp = tp;
        }
@@ -1059,21 +1062,20 @@ check_bit_field_type(sym_t *dsym, type_t
 static void
 declare_bit_field(sym_t *dsym, tspec_t *inout_t, type_t **const inout_tp)
 {
-       type_t *tp;
-       tspec_t t;
 
        check_bit_field_type(dsym, inout_tp, inout_t);
 
-       tp = *inout_tp;
-       t = *inout_t;
-       if (tp->t_flen > size_in_bits(t)) {
+       type_t *tp = *inout_tp;
+       tspec_t t = *inout_t;
+       unsigned int t_width = size_in_bits(t);
+       if (tp->t_bit_field_width > t_width) {
                /* illegal bit-field size: %d */
-               error(36, tp->t_flen);
-               tp->t_flen = size_in_bits(t);
-       } else if (tp->t_flen == 0 && dsym->s_name != unnamed) {
+               error(36, (int)tp->t_bit_field_width);
+               tp->t_bit_field_width = t_width;
+       } else if (tp->t_bit_field_width == 0 && dsym->s_name != unnamed) {
                /* zero size bit-field */
                error(37);
-               tp->t_flen = size_in_bits(t);
+               tp->t_bit_field_width = t_width;
        }
        if (dsym->s_scl == UNION_MEMBER) {
                /* bit-field in union is very unusual */
@@ -1133,12 +1135,12 @@ declare_member(sym_t *dsym)
                dcs->d_offset_in_bits = 0;
        }
        if (dsym->s_bitfield) {
-               dcs_align(alignment_in_bits(tp), tp->t_flen);
+               dcs_align(alignment_in_bits(tp), tp->t_bit_field_width);
                dsym->u.s_member.sm_offset_in_bits = dcs->d_offset_in_bits -
                    dcs->d_offset_in_bits % size_in_bits(t);
-               tp->t_foffs = dcs->d_offset_in_bits -
+               tp->t_bit_field_offset = dcs->d_offset_in_bits -
                    dsym->u.s_member.sm_offset_in_bits;
-               dcs->d_offset_in_bits += tp->t_flen;
+               dcs->d_offset_in_bits += tp->t_bit_field_width;
        } else {
                dcs_align(alignment_in_bits(tp), 0);
                dsym->u.s_member.sm_offset_in_bits = dcs->d_offset_in_bits;
@@ -1158,34 +1160,23 @@ declare_member(sym_t *dsym)
        return dsym;
 }
 
-/*
- * Aligns the next structure element as required.
- *
- * al contains the required alignment, len the length of a bit-field.
- */
+/* Aligns the next structure element as required. */
 static void
-dcs_align(unsigned int al, unsigned int len)
+dcs_align(unsigned int member_alignment, unsigned int bit_field_width)
 {
-       unsigned int no;
-
-       /*
-        * The alignment of the current element becomes the alignment of
-        * the struct/union if it is larger than the current alignment
-        * of the struct/union.
-        */
-       if (al > dcs->d_sou_align_in_bits)
-               dcs->d_sou_align_in_bits = al;
-
-       no = (dcs->d_offset_in_bits + (al - 1)) & ~(al - 1);
-       if (len == 0 || dcs->d_offset_in_bits + len > no)
-               dcs->d_offset_in_bits = no;
+
+       if (member_alignment > dcs->d_sou_align_in_bits)
+               dcs->d_sou_align_in_bits = member_alignment;
+
+       unsigned int offset = (dcs->d_offset_in_bits + member_alignment - 1)
+           & ~(member_alignment - 1);
+       if (bit_field_width == 0
+           || dcs->d_offset_in_bits + bit_field_width > offset)
+               dcs->d_offset_in_bits = offset;
 }
 
-/*
- * Remember the width of the field in its type structure.
- */
 sym_t *
-set_bit_field_width(sym_t *dsym, int len)
+set_bit_field_width(sym_t *dsym, int bit_field_width)
 {
 
        if (dsym == NULL) {
@@ -1198,7 +1189,7 @@ set_bit_field_width(sym_t *dsym, int len
        }
        dsym->s_type = block_dup_type(dsym->s_type);
        dsym->s_type->t_bitfield = true;
-       dsym->s_type->t_flen = len;
+       dsym->s_type->t_bit_field_width = bit_field_width;
        dsym->s_bitfield = true;
        return dsym;
 }
@@ -1814,7 +1805,7 @@ complete_struct_or_union(sym_t *first_me
                if (mem->u.s_member.sm_sou_type == NULL) {
                        mem->u.s_member.sm_sou_type = sp;
                        if (mem->s_type->t_bitfield) {
-                               sp->sou_size_in_bits += bit_field_size(&mem);
+                               sp->sou_size_in_bits += bit_field_width(&mem);
                                if (mem == NULL)
                                        break;
                        }
diff -r 66143d559fb4 -r ed7bd1ebee17 usr.bin/xlint/lint1/lint1.h
--- a/usr.bin/xlint/lint1/lint1.h       Thu Jun 29 10:31:32 2023 +0000
+++ b/usr.bin/xlint/lint1/lint1.h       Thu Jun 29 12:52:06 2023 +0000
@@ -1,4 +1,4 @@
-/* $NetBSD: lint1.h,v 1.168 2023/06/29 05:47:41 rillig Exp $ */
+/* $NetBSD: lint1.h,v 1.169 2023/06/29 12:52:06 rillig Exp $ */
 
 /*
  * Copyright (c) 1996 Christopher G. Demetriou.  All Rights Reserved.
@@ -179,21 +179,17 @@ struct lint1_type {
                enumeration     *_t_enum;
                struct  sym *_t_args;   /* arguments (if t_proto) */
        } t_u;
-       struct {
-               unsigned int    _t_flen:8;      /* length of bit-field */
-               unsigned int    _t_foffs:24;    /* offset of bit-field */
-       } t_b;
-       struct  lint1_type *t_subt; /* element type (if ARRAY),
-                                * return value (if FUNC),
-                                * target type (if PTR) */
+       unsigned int    t_bit_field_width:8;
+       unsigned int    t_bit_field_offset:24;
+       struct  lint1_type *t_subt;     /* element type (if ARRAY),
+                                        * return value (if FUNC),
+                                        * target type (if PTR) */
 };
 
 #define        t_dim   t_u._t_dim
 #define        t_sou   t_u._t_sou
 #define        t_enum  t_u._t_enum
 #define        t_args  t_u._t_args
-#define        t_flen  t_b._t_flen
-#define        t_foffs t_b._t_foffs
 
 /*
  * types of symbols
diff -r 66143d559fb4 -r ed7bd1ebee17 usr.bin/xlint/lint1/tree.c
--- a/usr.bin/xlint/lint1/tree.c        Thu Jun 29 10:31:32 2023 +0000
+++ b/usr.bin/xlint/lint1/tree.c        Thu Jun 29 12:52:06 2023 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: tree.c,v 1.531 2023/06/24 20:50:54 rillig Exp $        */
+/*     $NetBSD: tree.c,v 1.532 2023/06/29 12:52:06 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.531 2023/06/24 20:50:54 rillig Exp $");
+__RCSID("$NetBSD: tree.c,v 1.532 2023/06/29 12:52:06 rillig Exp $");
 #endif
 
 #include <float.h>
@@ -93,7 +93,9 @@ width_in_bits(const type_t *tp)
 {
 
        lint_assert(is_integer(tp->t_tspec));
-       return tp->t_bitfield ? tp->t_flen : size_in_bits(tp->t_tspec);
+       return tp->t_bitfield
+           ? tp->t_bit_field_width
+           : size_in_bits(tp->t_tspec);
 }
 
 static bool
@@ -1899,11 +1901,11 @@ all_members_compatible(const sym_t *msym
                        if (csym->s_bitfield != sym->s_bitfield)
                                return false;
                        if (csym->s_bitfield) {
-                               type_t *tp1 = csym->s_type;
-                               type_t *tp2 = sym->s_type;
-                               if (tp1->t_flen != tp2->t_flen)
+                               if (csym->s_type->t_bit_field_width
+                                   != sym->s_type->t_bit_field_width)
                                        return false;
-                               if (tp1->t_foffs != tp2->t_foffs)
+                               if (csym->s_type->t_bit_field_offset
+                                   != sym->s_type->t_bit_field_offset)
                                        return false;
                        }
                }
@@ -3264,10 +3266,12 @@ static tspec_t
 promote_c90(const tnode_t *tn, tspec_t t, bool farg)
 {
        if (tn->tn_type->t_bitfield) {
-               unsigned int len = tn->tn_type->t_flen;
-               if (len < size_in_bits(INT))
+               unsigned int width = tn->tn_type->t_bit_field_width;
+               unsigned int int_width = size_in_bits(INT);
+               // XXX: What about _Bool bit-fields, since C99?
+               if (width < int_width)
                        return INT;
-               if (len == size_in_bits(INT))
+               if (width == int_width)
                        return is_uinteger(t) ? UINT : INT;
                return t;
        }
@@ -3912,7 +3916,7 @@ convert_constant_check_range(tspec_t ot,
        uint64_t xmask, xmsk1;



Home | Main Index | Thread Index | Old Index