lint: clean up enum constants for designators

In intializers and offsetof, both struct and union members are handled
in the same way, thus there is no need to distinguish them.
This commit is contained in:
rillig 2024-01-11 23:26:39 +00:00
parent d3fa5ab1cc
commit 0153f4c1c0
4 changed files with 27 additions and 30 deletions

View File

@ -1,5 +1,5 @@
%{ %{
/* $NetBSD: cgram.y,v 1.479 2024/01/11 23:06:19 rillig Exp $ */ /* $NetBSD: cgram.y,v 1.480 2024/01/11 23:26:39 rillig Exp $ */
/* /*
* Copyright (c) 1996 Christopher G. Demetriou. All Rights Reserved. * Copyright (c) 1996 Christopher G. Demetriou. All Rights Reserved.
@ -35,7 +35,7 @@
#include <sys/cdefs.h> #include <sys/cdefs.h>
#if defined(__RCSID) #if defined(__RCSID)
__RCSID("$NetBSD: cgram.y,v 1.479 2024/01/11 23:06:19 rillig Exp $"); __RCSID("$NetBSD: cgram.y,v 1.480 2024/01/11 23:26:39 rillig Exp $");
#endif #endif
#include <limits.h> #include <limits.h>
@ -512,17 +512,17 @@ primary_expression:
member_designator: member_designator:
identifier { identifier {
$$ = (designation) { .dn_len = 0 }; $$ = (designation) { .dn_len = 0 };
designation_push(&$$, DK_STRUCT /* or union */, getsym($1), 0); designation_push(&$$, DK_MEMBER, getsym($1), 0);
} }
| member_designator T_LBRACK range T_RBRACK { | member_designator T_LBRACK range T_RBRACK {
$$ = $1; $$ = $1;
designation_push(&$$, DK_ARRAY, NULL, $3.lo); designation_push(&$$, DK_SUBSCRIPT, NULL, $3.lo);
} }
| member_designator T_POINT { | member_designator T_POINT {
set_symtyp(FMEMBER); set_symtyp(FMEMBER);
} identifier { } identifier {
$$ = $1; $$ = $1;
designation_push(&$$, DK_STRUCT /* or union */, getsym($4), 0); designation_push(&$$, DK_MEMBER, getsym($4), 0);
} }
; ;

View File

@ -1,4 +1,4 @@
/* $NetBSD: init.c,v 1.254 2024/01/09 23:46:54 rillig Exp $ */ /* $NetBSD: init.c,v 1.255 2024/01/11 23:26:39 rillig Exp $ */
/* /*
* Copyright (c) 1994, 1995 Jochen Pohl * Copyright (c) 1994, 1995 Jochen Pohl
@ -38,7 +38,7 @@
#include <sys/cdefs.h> #include <sys/cdefs.h>
#if defined(__RCSID) #if defined(__RCSID)
__RCSID("$NetBSD: init.c,v 1.254 2024/01/09 23:46:54 rillig Exp $"); __RCSID("$NetBSD: init.c,v 1.255 2024/01/11 23:26:39 rillig Exp $");
#endif #endif
#include <stdlib.h> #include <stdlib.h>
@ -311,7 +311,7 @@ designator_type(const designator *dr, const type_t *tp)
switch (tp->t_tspec) { switch (tp->t_tspec) {
case STRUCT: case STRUCT:
case UNION: case UNION:
if (dr->dr_kind != DK_STRUCT && dr->dr_kind != DK_UNION) { if (dr->dr_kind != DK_MEMBER) {
const sym_t *fmem = first_named_member(tp); const sym_t *fmem = first_named_member(tp);
/* syntax error '%s' */ /* syntax error '%s' */
error(249, "designator '[...]' is only for arrays"); error(249, "designator '[...]' is only for arrays");
@ -321,7 +321,7 @@ designator_type(const designator *dr, const type_t *tp)
lint_assert(dr->dr_member != NULL); lint_assert(dr->dr_member != NULL);
return dr->dr_member->s_type; return dr->dr_member->s_type;
case ARRAY: case ARRAY:
if (dr->dr_kind != DK_ARRAY) { if (dr->dr_kind != DK_SUBSCRIPT) {
/* syntax error '%s' */ /* syntax error '%s' */
error(249, error(249,
"designator '.member' is only for struct/union"); "designator '.member' is only for struct/union");
@ -344,13 +344,13 @@ static void
designator_debug(const designator *dr) designator_debug(const designator *dr)
{ {
if (dr->dr_kind == DK_STRUCT || dr->dr_kind == DK_UNION) { if (dr->dr_kind == DK_MEMBER) {
lint_assert(dr->dr_subscript == 0); lint_assert(dr->dr_subscript == 0);
debug_printf(".%s", debug_printf(".%s",
dr->dr_member != NULL dr->dr_member != NULL
? dr->dr_member->s_name ? dr->dr_member->s_name
: "<end>"); : "<end>");
} else if (dr->dr_kind == DK_ARRAY) { } else if (dr->dr_kind == DK_SUBSCRIPT) {
lint_assert(dr->dr_member == NULL); lint_assert(dr->dr_member == NULL);
debug_printf("[%zu]", dr->dr_subscript); debug_printf("[%zu]", dr->dr_subscript);
} else { } else {
@ -421,10 +421,9 @@ designation_descend(designation *dn, const type_t *tp)
const sym_t *member = first_named_member(tp); const sym_t *member = first_named_member(tp);
if (member == NULL) if (member == NULL)
return false; return false;
designation_push(dn, designation_push(dn, DK_MEMBER, member, 0);
tp->t_tspec == STRUCT ? DK_STRUCT : DK_UNION, member, 0);
} else if (tp->t_tspec == ARRAY) } else if (tp->t_tspec == ARRAY)
designation_push(dn, DK_ARRAY, NULL, 0); designation_push(dn, DK_SUBSCRIPT, NULL, 0);
else else
designation_push(dn, DK_SCALAR, NULL, 0); designation_push(dn, DK_SCALAR, NULL, 0);
return true; return true;
@ -550,10 +549,10 @@ static void
warn_too_many_initializers(designator_kind kind, const type_t *tp) warn_too_many_initializers(designator_kind kind, const type_t *tp)
{ {
if (kind == DK_STRUCT || kind == DK_UNION) { if (kind == DK_MEMBER) {
/* too many struct/union initializers */ /* too many struct/union initializers */
error(172); error(172);
} else if (kind == DK_ARRAY) { } else if (kind == DK_SUBSCRIPT) {
lint_assert(tp->t_tspec == ARRAY); lint_assert(tp->t_tspec == ARRAY);
lint_assert(!tp->t_incomplete_array); lint_assert(!tp->t_incomplete_array);
/* too many array initializers, expected %d */ /* too many array initializers, expected %d */
@ -816,8 +815,7 @@ proceed:;
return; return;
} }
designation_push(&bl->bl_designation, designation_push(&bl->bl_designation, DK_MEMBER, member, 0);
tp->t_tspec == STRUCT ? DK_STRUCT : DK_UNION, member, 0);
} }
static void static void
@ -847,7 +845,7 @@ initialization_add_designator_subscript(initialization *in, size_t subscript)
if (tp->t_incomplete_array && subscript > in->in_max_subscript) if (tp->t_incomplete_array && subscript > in->in_max_subscript)
in->in_max_subscript = subscript; in->in_max_subscript = subscript;
designation_push(&bl->bl_designation, DK_ARRAY, NULL, subscript); designation_push(&bl->bl_designation, DK_SUBSCRIPT, NULL, subscript);
} }
/* /*

View File

@ -1,4 +1,4 @@
/* $NetBSD: lint1.h,v 1.207 2024/01/09 23:46:54 rillig Exp $ */ /* $NetBSD: lint1.h,v 1.208 2024/01/11 23:26:39 rillig Exp $ */
/* /*
* Copyright (c) 1996 Christopher G. Demetriou. All Rights Reserved. * Copyright (c) 1996 Christopher G. Demetriou. All Rights Reserved.
@ -481,12 +481,11 @@ typedef struct {
} range_t; } range_t;
typedef enum designator_kind { typedef enum designator_kind {
DK_STRUCT, /* .member */ DK_MEMBER, /* .member */
DK_UNION, /* .member */ DK_SUBSCRIPT, /* [subscript] */
DK_ARRAY, /* [subscript] */
/* TODO: actually necessary? */
DK_SCALAR /* no textual representation, not generated by DK_SCALAR /* no textual representation, not generated by
* the parser */ * the parser; used for scalar initializer
* expressions surrounded by braces */
} designator_kind; } designator_kind;
/* /*
@ -497,8 +496,8 @@ typedef enum designator_kind {
*/ */
typedef struct designator { typedef struct designator {
designator_kind dr_kind; designator_kind dr_kind;
const sym_t *dr_member; /* for DK_STRUCT and DK_UNION */ const sym_t *dr_member; /* for DK_MEMBER */
size_t dr_subscript; /* for DK_ARRAY */ size_t dr_subscript; /* for DK_SUBSCRIPT */
bool dr_done; bool dr_done;
} designator; } designator;

View File

@ -1,4 +1,4 @@
/* $NetBSD: tree.c,v 1.594 2024/01/11 23:06:19 rillig Exp $ */ /* $NetBSD: tree.c,v 1.595 2024/01/11 23:26:39 rillig Exp $ */
/* /*
* Copyright (c) 1994, 1995 Jochen Pohl * Copyright (c) 1994, 1995 Jochen Pohl
@ -37,7 +37,7 @@
#include <sys/cdefs.h> #include <sys/cdefs.h>
#if defined(__RCSID) #if defined(__RCSID)
__RCSID("$NetBSD: tree.c,v 1.594 2024/01/11 23:06:19 rillig Exp $"); __RCSID("$NetBSD: tree.c,v 1.595 2024/01/11 23:26:39 rillig Exp $");
#endif #endif
#include <float.h> #include <float.h>
@ -3900,7 +3900,7 @@ build_offsetof(const type_t *tp, designation dn)
} }
for (size_t i = 0; i < dn.dn_len; i++) { for (size_t i = 0; i < dn.dn_len; i++) {
const designator *dr = dn.dn_items + i; const designator *dr = dn.dn_items + i;
if (dr->dr_kind == DK_ARRAY) { if (dr->dr_kind == DK_SUBSCRIPT) {
if (tp->t_tspec != ARRAY) if (tp->t_tspec != ARRAY)
goto proceed; /* silent error */ goto proceed; /* silent error */
tp = tp->t_subt; tp = tp->t_subt;