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: fix handling of unnamed struct/uni...
details: https://anonhg.NetBSD.org/src/rev/4ba740b2e8ac
branches: trunk
changeset: 377198:4ba740b2e8ac
user: rillig <rillig%NetBSD.org@localhost>
date: Fri Jun 30 21:06:18 2023 +0000
description:
lint: fix handling of unnamed struct/union members
The support for unnamed struct/union members that was added in decl.c
1.60 from 2015-10-13 was simple but wrong. It didn't cover initializers
of these structures and computed wrong sizes for structures containing
anonymous unions. At that time, the handling of initializers was broken
as well, it was fixed 6 years later in init.c 1.229 from 2021-12-22.
Real-life examples for code that lint couldn't handle are:
* external/bsd/jemalloc/dist/src/jemalloc.c
* external/mit/xorg/lib/dri.old/Makefile
diffstat:
tests/usr.bin/xlint/lint1/expr_sizeof.c | 5 +-
tests/usr.bin/xlint/lint1/init_braces.c | 4 +-
tests/usr.bin/xlint/lint1/msg_102.c | 10 +--
usr.bin/xlint/lint1/cgram.y | 20 +----
usr.bin/xlint/lint1/debug.c | 7 +-
usr.bin/xlint/lint1/decl.c | 103 ++++++++++++++++++-------------
usr.bin/xlint/lint1/externs1.h | 3 +-
usr.bin/xlint/lint1/init.c | 15 +++-
usr.bin/xlint/lint1/tree.c | 39 ++++++++++-
9 files changed, 121 insertions(+), 85 deletions(-)
diffs (truncated from 419 to 300 lines):
diff -r 2a3a6a9c9ebe -r 4ba740b2e8ac tests/usr.bin/xlint/lint1/expr_sizeof.c
--- a/tests/usr.bin/xlint/lint1/expr_sizeof.c Fri Jun 30 19:43:00 2023 +0000
+++ b/tests/usr.bin/xlint/lint1/expr_sizeof.c Fri Jun 30 21:06:18 2023 +0000
@@ -1,4 +1,4 @@
-/* $NetBSD: expr_sizeof.c,v 1.11 2023/06/30 16:39:17 rillig Exp $ */
+/* $NetBSD: expr_sizeof.c,v 1.12 2023/06/30 21:06:18 rillig Exp $ */
# 3 "expr_sizeof.c"
/*
@@ -152,8 +152,7 @@ anonymous_struct_and_union(void)
unsigned char uc32[32];
};
} su_16_32;
- /* FIXME: Must be 32, not 48. */
- /* expect+1: error: negative array dimension (-48) [20] */
+ /* expect+1: error: negative array dimension (-32) [20] */
typedef int sizeof_su_16_32[-(int)sizeof(su_16_32)];
union {
diff -r 2a3a6a9c9ebe -r 4ba740b2e8ac tests/usr.bin/xlint/lint1/init_braces.c
--- a/tests/usr.bin/xlint/lint1/init_braces.c Fri Jun 30 19:43:00 2023 +0000
+++ b/tests/usr.bin/xlint/lint1/init_braces.c Fri Jun 30 21:06:18 2023 +0000
@@ -1,4 +1,4 @@
-/* $NetBSD: init_braces.c,v 1.4 2023/06/30 09:21:52 rillig Exp $ */
+/* $NetBSD: init_braces.c,v 1.5 2023/06/30 21:06:18 rillig Exp $ */
# 3 "init_braces.c"
/*
@@ -86,8 +86,6 @@ init_anonymous_struct_and_union(void)
struct outer var = { /* struct outer */
{ /* anonymous union */
{ /* anonymous struct */
-/* FIXME: GCC and Clang both compile this initializer. */
-/* expect+1: error: type 'struct time' does not have member 'times' [101] */
.times = {
.t0 = { .ns = 0, },
.t1 = { .ns = 0, },
diff -r 2a3a6a9c9ebe -r 4ba740b2e8ac tests/usr.bin/xlint/lint1/msg_102.c
--- a/tests/usr.bin/xlint/lint1/msg_102.c Fri Jun 30 19:43:00 2023 +0000
+++ b/tests/usr.bin/xlint/lint1/msg_102.c Fri Jun 30 21:06:18 2023 +0000
@@ -1,4 +1,4 @@
-/* $NetBSD: msg_102.c,v 1.5 2023/06/30 09:21:52 rillig Exp $ */
+/* $NetBSD: msg_102.c,v 1.6 2023/06/30 21:06:18 rillig Exp $ */
# 3 "msg_102.c"
// Test for message: illegal use of member '%s' [102]
@@ -31,14 +31,8 @@ static struct bit_fields_and_bits *b1, *
static inline _Bool
eq(int x)
{
- /*
- * TODO: Once this is fixed, enable lint in
- * external/mit/xorg/lib/dri.old/Makefile again.
- */
-
if (x == 0)
- /* expect+2: error: illegal use of member 'bits' [102] */
- /* expect+1: error: illegal use of member 'bits' [102] */
+ /* Accessing a member from an unnamed struct member. */
return u1->bits == u2->bits;
/*
diff -r 2a3a6a9c9ebe -r 4ba740b2e8ac usr.bin/xlint/lint1/cgram.y
--- a/usr.bin/xlint/lint1/cgram.y Fri Jun 30 19:43:00 2023 +0000
+++ b/usr.bin/xlint/lint1/cgram.y Fri Jun 30 21:06:18 2023 +0000
@@ -1,5 +1,5 @@
%{
-/* $NetBSD: cgram.y,v 1.441 2023/06/30 19:10:49 rillig Exp $ */
+/* $NetBSD: cgram.y,v 1.442 2023/06/30 21:06:18 rillig Exp $ */
/*
* Copyright (c) 1996 Christopher G. Demetriou. All Rights Reserved.
@@ -35,7 +35,7 @@
#include <sys/cdefs.h>
#if defined(__RCSID)
-__RCSID("$NetBSD: cgram.y,v 1.441 2023/06/30 19:10:49 rillig Exp $");
+__RCSID("$NetBSD: cgram.y,v 1.442 2023/06/30 21:06:18 rillig Exp $");
#endif
#include <limits.h>
@@ -120,14 +120,6 @@ restore_warning_flags_loc(const char *fi
#define save_warning_flags() save_warning_flags_loc(__FILE__, __LINE__)
#define restore_warning_flags() restore_warning_flags_loc(__FILE__, __LINE__)
-/* unbind the anonymous struct members from the struct */
-static void
-anonymize(sym_t *s)
-{
- for ( ; s != NULL; s = s->s_next)
- s->u.s_member.sm_sou_type = NULL;
-}
-
static bool
is_either(const char *s, const char *a, const char *b)
{
@@ -968,11 +960,9 @@ struct_declaration: /* C99 6.7.2.1 */
if (!allow_c11 && !allow_gcc)
/* anonymous struct/union members is a C11 feature */
warning(49);
- if (is_struct_or_union(dcs->d_type->t_tspec)) {
- $$ = dcs->d_type->t_sou->sou_first_member;
- /* add all the members of the anonymous struct/union */
- anonymize($$);
- } else {
+ if (is_struct_or_union(dcs->d_type->t_tspec))
+ $$ = declare_unnamed_member();
+ else {
/* syntax error '%s' */
error(249, "unnamed member");
$$ = NULL;
diff -r 2a3a6a9c9ebe -r 4ba740b2e8ac usr.bin/xlint/lint1/debug.c
--- a/usr.bin/xlint/lint1/debug.c Fri Jun 30 19:43:00 2023 +0000
+++ b/usr.bin/xlint/lint1/debug.c Fri Jun 30 21:06:18 2023 +0000
@@ -1,4 +1,4 @@
-/* $NetBSD: debug.c,v 1.38 2023/06/30 14:39:23 rillig Exp $ */
+/* $NetBSD: debug.c,v 1.39 2023/06/30 21:06:18 rillig Exp $ */
/*-
* Copyright (c) 2021 The NetBSD Foundation, Inc.
@@ -35,7 +35,7 @@
#include <sys/cdefs.h>
#if defined(__RCSID)
-__RCSID("$NetBSD: debug.c,v 1.38 2023/06/30 14:39:23 rillig Exp $");
+__RCSID("$NetBSD: debug.c,v 1.39 2023/06/30 21:06:18 rillig Exp $");
#endif
#include <stdlib.h>
@@ -367,8 +367,9 @@ debug_sym(const char *prefix, const sym_
debug_printf(" value=%s",
sym->u.s_bool_constant ? "true" : "false");
- if (is_member(sym) && sym->u.s_member.sm_sou_type != NULL) {
+ if (is_member(sym)) {
struct_or_union *sou_type = sym->u.s_member.sm_sou_type;
+ lint_assert(sou_type != NULL);
const char *tag = sou_type->sou_tag->s_name;
const sym_t *def = sou_type->sou_first_typedef;
if (tag == unnamed && def != NULL)
diff -r 2a3a6a9c9ebe -r 4ba740b2e8ac usr.bin/xlint/lint1/decl.c
--- a/usr.bin/xlint/lint1/decl.c Fri Jun 30 19:43:00 2023 +0000
+++ b/usr.bin/xlint/lint1/decl.c Fri Jun 30 21:06:18 2023 +0000
@@ -1,4 +1,4 @@
-/* $NetBSD: decl.c,v 1.328 2023/06/30 19:43:00 rillig Exp $ */
+/* $NetBSD: decl.c,v 1.329 2023/06/30 21:06:18 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.328 2023/06/30 19:43:00 rillig Exp $");
+__RCSID("$NetBSD: decl.c,v 1.329 2023/06/30 21:06:18 rillig Exp $");
#endif
#include <sys/param.h>
@@ -1016,6 +1016,49 @@ check_bit_field(sym_t *dsym, tspec_t *in
}
}
+/* Add a member to the struct or union type that is being built in 'dcs'. */
+static void
+dcs_add_member(sym_t *mem)
+{
+ type_t *tp = mem->s_type;
+
+ unsigned int union_offset = 0;
+ if (dcs->d_kind == DK_UNION_MEMBER) {
+ union_offset = dcs->d_offset_in_bits;
+ dcs->d_offset_in_bits = 0;
+ }
+
+ if (mem->s_bitfield) {
+ dcs_align(alignment_in_bits(tp), tp->t_bit_field_width);
+ // XXX: Why round down?
+ mem->u.s_member.sm_offset_in_bits = dcs->d_offset_in_bits
+ - dcs->d_offset_in_bits % size_in_bits(tp->t_tspec);
+ tp->t_bit_field_offset = dcs->d_offset_in_bits
+ - mem->u.s_member.sm_offset_in_bits;
+ dcs->d_offset_in_bits += tp->t_bit_field_width;
+ } else {
+ dcs_align(alignment_in_bits(tp), 0);
+ mem->u.s_member.sm_offset_in_bits = dcs->d_offset_in_bits;
+ dcs->d_offset_in_bits += type_size_in_bits(tp);
+ }
+
+ if (union_offset > dcs->d_offset_in_bits)
+ dcs->d_offset_in_bits = union_offset;
+}
+
+sym_t *
+declare_unnamed_member(void)
+{
+
+ sym_t *mem = block_zero_alloc(sizeof(*mem));
+ mem->s_name = unnamed;
+ mem->s_type = dcs->d_type;
+
+ dcs_add_member(mem);
+ bitfieldtype_ok = false;
+ return mem;
+}
+
sym_t *
declare_member(sym_t *dsym)
{
@@ -1056,25 +1099,7 @@ declare_member(sym_t *dsym)
c99ism(39, dsym->s_name);
}
- unsigned int union_offset = 0;
- if (dcs->d_kind == DK_UNION_MEMBER) {
- union_offset = dcs->d_offset_in_bits;
- dcs->d_offset_in_bits = 0;
- }
- if (dsym->s_bitfield) {
- 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_bit_field_offset = dcs->d_offset_in_bits -
- dsym->u.s_member.sm_offset_in_bits;
- 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;
- dcs->d_offset_in_bits += sz;
- }
- if (union_offset > dcs->d_offset_in_bits)
- dcs->d_offset_in_bits = union_offset;
+ dcs_add_member(dsym);
check_function_definition(dsym, false);
@@ -1683,6 +1708,20 @@ storage_class_name(scl_t sc)
/* NOTREACHED */
}
+static bool
+has_named_member(const type_t *tp)
+{
+ for (const sym_t *mem = tp->t_sou->sou_first_member;
+ mem != NULL; mem = mem->s_next) {
+ if (mem->s_name != unnamed)
+ return true;
+ if (is_struct_or_union(mem->s_type->t_tspec)
+ && has_named_member(mem->s_type))
+ return true;
+ }
+ return false;
+}
+
type_t *
complete_struct_or_union(sym_t *first_member)
{
@@ -1705,27 +1744,7 @@ complete_struct_or_union(sym_t *first_me
if (sp->sou_size_in_bits == 0) {
/* zero sized %s is a C99 feature */
c99ism(47, tspec_name(tp->t_tspec));
- }
-
- bool has_named_member = false;
- for (sym_t *mem = first_member; mem != NULL; mem = mem->s_next) {
- if (mem->s_name != unnamed)
- has_named_member = true;
- /* bind anonymous members to the structure */
- 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_fields_width(&mem, &has_named_member);
- if (mem == NULL)
- break;
- }
- sp->sou_size_in_bits +=
- type_size_in_bits(mem->s_type);
- }
- }
-
- if (!has_named_member && sp->sou_size_in_bits != 0) {
+ } else if (!has_named_member(tp)) {
/* '%s' has no named members */
warning(65, type_name(tp));
}
diff -r 2a3a6a9c9ebe -r 4ba740b2e8ac usr.bin/xlint/lint1/externs1.h
--- a/usr.bin/xlint/lint1/externs1.h Fri Jun 30 19:43:00 2023 +0000
+++ b/usr.bin/xlint/lint1/externs1.h Fri Jun 30 21:06:18 2023 +0000
@@ -1,4 +1,4 @@
-/* $NetBSD: externs1.h,v 1.181 2023/06/30 19:10:49 rillig Exp $ */
+/* $NetBSD: externs1.h,v 1.182 2023/06/30 21:06:18 rillig Exp $ */
/*
* Copyright (c) 1994, 1995 Jochen Pohl
@@ -203,6 +203,7 @@ int length_in_bits(const type_t *, const
unsigned int alignment_in_bits(const type_t *);
sym_t *concat_symbols(sym_t *, sym_t *);
void check_type(sym_t *);
+sym_t *declare_unnamed_member(void);
sym_t *declare_member(sym_t *);
Home |
Main Index |
Thread Index |
Old Index