Fri Jul 23 16:43:11 2021 UTC ()
lint: make offset and alignment positive numbers

There's no use case for negative alignment or offsets in structures.

No functional change.


(rillig)
diff -r1.207 -r1.208 src/usr.bin/xlint/lint1/decl.c
diff -r1.115 -r1.116 src/usr.bin/xlint/lint1/lint1.h

cvs diff -r1.207 -r1.208 src/usr.bin/xlint/lint1/decl.c (expand / switch to unified diff)

--- src/usr.bin/xlint/lint1/decl.c 2021/07/15 23:54:22 1.207
+++ src/usr.bin/xlint/lint1/decl.c 2021/07/23 16:43:11 1.208
@@ -1,14 +1,14 @@ @@ -1,14 +1,14 @@
1/* $NetBSD: decl.c,v 1.207 2021/07/15 23:54:22 rillig Exp $ */ 1/* $NetBSD: decl.c,v 1.208 2021/07/23 16:43:11 rillig Exp $ */
2 2
3/* 3/*
4 * Copyright (c) 1996 Christopher G. Demetriou. All Rights Reserved. 4 * Copyright (c) 1996 Christopher G. Demetriou. All Rights Reserved.
5 * Copyright (c) 1994, 1995 Jochen Pohl 5 * Copyright (c) 1994, 1995 Jochen Pohl
6 * All Rights Reserved. 6 * All Rights Reserved.
7 * 7 *
8 * Redistribution and use in source and binary forms, with or without 8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions 9 * modification, are permitted provided that the following conditions
10 * are met: 10 * are met:
11 * 1. Redistributions of source code must retain the above copyright 11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer. 12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright 13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the 14 * notice, this list of conditions and the following disclaimer in the
@@ -28,53 +28,53 @@ @@ -28,53 +28,53 @@
28 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 28 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 29 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 30 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 31 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
32 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 32 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 */ 33 */
34 34
35#if HAVE_NBTOOL_CONFIG_H 35#if HAVE_NBTOOL_CONFIG_H
36#include "nbtool_config.h" 36#include "nbtool_config.h"
37#endif 37#endif
38 38
39#include <sys/cdefs.h> 39#include <sys/cdefs.h>
40#if defined(__RCSID) && !defined(lint) 40#if defined(__RCSID) && !defined(lint)
41__RCSID("$NetBSD: decl.c,v 1.207 2021/07/15 23:54:22 rillig Exp $"); 41__RCSID("$NetBSD: decl.c,v 1.208 2021/07/23 16:43:11 rillig Exp $");
42#endif 42#endif
43 43
44#include <sys/param.h> 44#include <sys/param.h>
45#include <limits.h> 45#include <limits.h>
46#include <stdlib.h> 46#include <stdlib.h>
47#include <string.h> 47#include <string.h>
48 48
49#include "lint1.h" 49#include "lint1.h"
50 50
51const char *unnamed = "<unnamed>"; 51const char *unnamed = "<unnamed>";
52 52
53/* shared type structures for arithmetic types and void */ 53/* shared type structures for arithmetic types and void */
54static type_t *typetab; 54static type_t *typetab;
55 55
56/* value of next enumerator during declaration of enum types */ 56/* value of next enumerator during declaration of enum types */
57int enumval; 57int enumval;
58 58
59/* 59/*
60 * pointer to top element of a stack which contains information local 60 * pointer to top element of a stack which contains information local
61 * to nested declarations 61 * to nested declarations
62 */ 62 */
63dinfo_t *dcs; 63dinfo_t *dcs;
64 64
65static type_t *tdeferr(type_t *, tspec_t); 65static type_t *tdeferr(type_t *, tspec_t);
66static void settdsym(type_t *, sym_t *); 66static void settdsym(type_t *, sym_t *);
67static void align(int, int); 67static void align(u_int, u_int);
68static sym_t *newtag(sym_t *, scl_t, bool, bool); 68static sym_t *newtag(sym_t *, scl_t, bool, bool);
69static bool eqargs(const type_t *, const type_t *, bool *); 69static bool eqargs(const type_t *, const type_t *, bool *);
70static bool mnoarg(const type_t *, bool *); 70static bool mnoarg(const type_t *, bool *);
71static bool check_old_style_definition(sym_t *, sym_t *); 71static bool check_old_style_definition(sym_t *, sym_t *);
72static bool check_prototype_declaration(sym_t *, sym_t *); 72static bool check_prototype_declaration(sym_t *, sym_t *);
73static sym_t *new_style_function(sym_t *, sym_t *); 73static sym_t *new_style_function(sym_t *, sym_t *);
74static void old_style_function(sym_t *, sym_t *); 74static void old_style_function(sym_t *, sym_t *);
75static void declare_external_in_block(sym_t *); 75static void declare_external_in_block(sym_t *);
76static bool check_init(sym_t *); 76static bool check_init(sym_t *);
77static void check_argument_usage(bool, sym_t *); 77static void check_argument_usage(bool, sym_t *);
78static void check_variable_usage(bool, sym_t *); 78static void check_variable_usage(bool, sym_t *);
79static void check_label_usage(sym_t *); 79static void check_label_usage(sym_t *);
80static void check_tag_usage(sym_t *); 80static void check_tag_usage(sym_t *);
@@ -1144,27 +1144,27 @@ declare_bit_field(sym_t *dsym, tspec_t * @@ -1144,27 +1144,27 @@ declare_bit_field(sym_t *dsym, tspec_t *
1144 dsym->s_bitfield = false; 1144 dsym->s_bitfield = false;
1145 } 1145 }
1146} 1146}
1147 1147
1148/* 1148/*
1149 * Process the declarator of a struct/union element. 1149 * Process the declarator of a struct/union element.
1150 */ 1150 */
1151sym_t * 1151sym_t *
1152declarator_1_struct_union(sym_t *dsym) 1152declarator_1_struct_union(sym_t *dsym)
1153{ 1153{
1154 type_t *tp; 1154 type_t *tp;
1155 tspec_t t; 1155 tspec_t t;
1156 int sz; 1156 int sz;
1157 int o = 0; /* Appease GCC */ 1157 u_int o = 0; /* Appease GCC */
1158 1158
1159 lint_assert(dsym->s_scl == MOS || dsym->s_scl == MOU); 1159 lint_assert(dsym->s_scl == MOS || dsym->s_scl == MOU);
1160 1160
1161 if (dcs->d_redeclared_symbol != NULL) { 1161 if (dcs->d_redeclared_symbol != NULL) {
1162 /* should be ensured by storesym() */ 1162 /* should be ensured by storesym() */
1163 lint_assert(dcs->d_redeclared_symbol->s_scl == MOS || 1163 lint_assert(dcs->d_redeclared_symbol->s_scl == MOS ||
1164 dcs->d_redeclared_symbol->s_scl == MOU); 1164 dcs->d_redeclared_symbol->s_scl == MOU);
1165 1165
1166 if (dsym->s_styp == dcs->d_redeclared_symbol->s_styp) { 1166 if (dsym->s_styp == dcs->d_redeclared_symbol->s_styp) {
1167 /* duplicate member name: %s */ 1167 /* duplicate member name: %s */
1168 error(33, dsym->s_name); 1168 error(33, dsym->s_name);
1169 rmsym(dcs->d_redeclared_symbol); 1169 rmsym(dcs->d_redeclared_symbol);
1170 } 1170 }
@@ -1221,29 +1221,29 @@ declarator_1_struct_union(sym_t *dsym) @@ -1221,29 +1221,29 @@ declarator_1_struct_union(sym_t *dsym)
1221 * structure element. 1221 * structure element.
1222 */ 1222 */
1223 bitfieldtype_ok = false; 1223 bitfieldtype_ok = false;
1224 1224
1225 return dsym; 1225 return dsym;
1226} 1226}
1227 1227
1228/* 1228/*
1229 * Aligns next structure element as required. 1229 * Aligns next structure element as required.
1230 * 1230 *
1231 * al contains the required alignment, len the length of a bit-field. 1231 * al contains the required alignment, len the length of a bit-field.
1232 */ 1232 */
1233static void 1233static void
1234align(int al, int len) 1234align(u_int al, u_int len)
1235{ 1235{
1236 int no; 1236 u_int no;
1237 1237
1238 /* 1238 /*
1239 * The alignment of the current element becomes the alignment of 1239 * The alignment of the current element becomes the alignment of
1240 * the struct/union if it is larger than the current alignment 1240 * the struct/union if it is larger than the current alignment
1241 * of the struct/union. 1241 * of the struct/union.
1242 */ 1242 */
1243 if (al > dcs->d_sou_align_in_bits) 1243 if (al > dcs->d_sou_align_in_bits)
1244 dcs->d_sou_align_in_bits = al; 1244 dcs->d_sou_align_in_bits = al;
1245 1245
1246 no = (dcs->d_offset + (al - 1)) & ~(al - 1); 1246 no = (dcs->d_offset + (al - 1)) & ~(al - 1);
1247 if (len == 0 || dcs->d_offset + len > no) 1247 if (len == 0 || dcs->d_offset + len > no)
1248 dcs->d_offset = no; 1248 dcs->d_offset = no;
1249} 1249}

cvs diff -r1.115 -r1.116 src/usr.bin/xlint/lint1/lint1.h (expand / switch to unified diff)

--- src/usr.bin/xlint/lint1/lint1.h 2021/07/15 18:13:25 1.115
+++ src/usr.bin/xlint/lint1/lint1.h 2021/07/23 16:43:11 1.116
@@ -1,14 +1,14 @@ @@ -1,14 +1,14 @@
1/* $NetBSD: lint1.h,v 1.115 2021/07/15 18:13:25 rillig Exp $ */ 1/* $NetBSD: lint1.h,v 1.116 2021/07/23 16:43:11 rillig Exp $ */
2 2
3/* 3/*
4 * Copyright (c) 1996 Christopher G. Demetriou. All Rights Reserved. 4 * Copyright (c) 1996 Christopher G. Demetriou. All Rights Reserved.
5 * Copyright (c) 1994, 1995 Jochen Pohl 5 * Copyright (c) 1994, 1995 Jochen Pohl
6 * All Rights Reserved. 6 * All Rights Reserved.
7 * 7 *
8 * Redistribution and use in source and binary forms, with or without 8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions 9 * modification, are permitted provided that the following conditions
10 * are met: 10 * are met:
11 * 1. Redistributions of source code must retain the above copyright 11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer. 12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright 13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the 14 * notice, this list of conditions and the following disclaimer in the
@@ -350,28 +350,28 @@ struct generic_association { @@ -350,28 +350,28 @@ struct generic_association {
350 * AUTO declaration of local symbols 350 * AUTO declaration of local symbols
351 * ABSTRACT abstract declarations (sizeof, casts) 351 * ABSTRACT abstract declarations (sizeof, casts)
352 * 352 *
353 */ 353 */
354typedef struct dinfo { 354typedef struct dinfo {
355 tspec_t d_abstract_type;/* VOID, BOOL, CHAR, INT or COMPLEX */ 355 tspec_t d_abstract_type;/* VOID, BOOL, CHAR, INT or COMPLEX */
356 tspec_t d_complex_mod; /* FLOAT or DOUBLE */ 356 tspec_t d_complex_mod; /* FLOAT or DOUBLE */
357 tspec_t d_sign_mod; /* SIGNED or UNSIGN */ 357 tspec_t d_sign_mod; /* SIGNED or UNSIGN */
358 tspec_t d_rank_mod; /* SHORT, LONG or QUAD */ 358 tspec_t d_rank_mod; /* SHORT, LONG or QUAD */
359 scl_t d_scl; /* storage class */ 359 scl_t d_scl; /* storage class */
360 type_t *d_type; /* after end_type() pointer to the type used 360 type_t *d_type; /* after end_type() pointer to the type used
361 for all declarators */ 361 for all declarators */
362 sym_t *d_redeclared_symbol; 362 sym_t *d_redeclared_symbol;
363 int d_offset; /* offset of next structure member */ 363 u_int d_offset; /* offset of next structure member */
364 int d_sou_align_in_bits; /* alignment required for current 364 u_int d_sou_align_in_bits; /* alignment required for current
365 * structure */ 365 * structure */
366 scl_t d_ctx; /* context of declaration */ 366 scl_t d_ctx; /* context of declaration */
367 bool d_const : 1; /* const in declaration specifiers */ 367 bool d_const : 1; /* const in declaration specifiers */
368 bool d_volatile : 1; /* volatile in declaration specifiers */ 368 bool d_volatile : 1; /* volatile in declaration specifiers */
369 bool d_inline : 1; /* inline in declaration specifiers */ 369 bool d_inline : 1; /* inline in declaration specifiers */
370 bool d_mscl : 1; /* multiple storage classes */ 370 bool d_mscl : 1; /* multiple storage classes */
371 bool d_terr : 1; /* invalid type combination */ 371 bool d_terr : 1; /* invalid type combination */
372 bool d_nonempty_decl : 1; /* if at least one tag is declared 372 bool d_nonempty_decl : 1; /* if at least one tag is declared
373 * ... in the current function decl. */ 373 * ... in the current function decl. */
374 bool d_vararg : 1; 374 bool d_vararg : 1;
375 bool d_proto : 1; /* current function decl. is prototype */ 375 bool d_proto : 1; /* current function decl. is prototype */
376 bool d_notyp : 1; /* set if no type specifier was present */ 376 bool d_notyp : 1; /* set if no type specifier was present */
377 bool d_asm : 1; /* set if d_ctx == AUTO and asm() present */ 377 bool d_asm : 1; /* set if d_ctx == AUTO and asm() present */