Module Name: src Committed By: rillig Date: Wed Mar 17 01:53:21 UTC 2021
Modified Files: src/usr.bin/xlint/lint1: cgram.y decl.c externs1.h Log Message: lint: move to_int_constant from the grammar to decl.c This way, the code is covered by running 'make lint'. The code from the grammar is not covered, therefore it still uses int instead of bool in a few places. Inline the comparison functions for uint64_t. These functions didn't add any clarity to the code. No functional change. To generate a diff of this commit: cvs rdiff -u -r1.174 -r1.175 src/usr.bin/xlint/lint1/cgram.y cvs rdiff -u -r1.145 -r1.146 src/usr.bin/xlint/lint1/decl.c cvs rdiff -u -r1.74 -r1.75 src/usr.bin/xlint/lint1/externs1.h Please note that diffs are not public domain; they are subject to the copyright notices on the relevant files.
Modified files: Index: src/usr.bin/xlint/lint1/cgram.y diff -u src/usr.bin/xlint/lint1/cgram.y:1.174 src/usr.bin/xlint/lint1/cgram.y:1.175 --- src/usr.bin/xlint/lint1/cgram.y:1.174 Wed Mar 17 01:38:31 2021 +++ src/usr.bin/xlint/lint1/cgram.y Wed Mar 17 01:53:21 2021 @@ -1,5 +1,5 @@ %{ -/* $NetBSD: cgram.y,v 1.174 2021/03/17 01:38:31 rillig Exp $ */ +/* $NetBSD: cgram.y,v 1.175 2021/03/17 01:53:21 rillig Exp $ */ /* * Copyright (c) 1996 Christopher G. Demetriou. All Rights Reserved. @@ -35,7 +35,7 @@ #include <sys/cdefs.h> #if defined(__RCSID) && !defined(lint) -__RCSID("$NetBSD: cgram.y,v 1.174 2021/03/17 01:38:31 rillig Exp $"); +__RCSID("$NetBSD: cgram.y,v 1.175 2021/03/17 01:53:21 rillig Exp $"); #endif #include <limits.h> @@ -66,7 +66,6 @@ int mem_block_level; */ static int olwarn = LWARN_BAD; -static int to_int_constant(tnode_t *, int); static void cgram_declare(sym_t *, bool, sbuf_t *); static void ignore_up_to_rparen(void); static sym_t *symbolrename(sym_t *, sbuf_t *); @@ -873,12 +872,12 @@ notype_member_decl: $$ = $1; } | notype_decl T_COLON constant_expr { /* C99 6.7.2.1 */ - $$ = bitfield($1, to_int_constant($3, 1)); + $$ = bitfield($1, to_int_constant($3, true)); } | { symtyp = FVFT; } T_COLON constant_expr { /* C99 6.7.2.1 */ - $$ = bitfield(NULL, to_int_constant($3, 1)); + $$ = bitfield(NULL, to_int_constant($3, true)); } ; @@ -887,12 +886,12 @@ type_member_decl: $$ = $1; } | type_decl T_COLON constant_expr { - $$ = bitfield($1, to_int_constant($3, 1)); + $$ = bitfield($1, to_int_constant($3, true)); } | { symtyp = FVFT; } T_COLON constant_expr { - $$ = bitfield(NULL, to_int_constant($3, 1)); + $$ = bitfield(NULL, to_int_constant($3, true)); } ; @@ -975,7 +974,7 @@ enumerator: $$ = enumeration_constant($1, enumval, 1); } | enumeration_constant T_ASSIGN constant_expr { - $$ = enumeration_constant($1, to_int_constant($3, 1), 0); + $$ = enumeration_constant($1, to_int_constant($3, true), 0); } ; @@ -1043,7 +1042,7 @@ notype_direct_decl: $$ = add_array($1, 0, 0); } | notype_direct_decl T_LBRACK constant_expr T_RBRACK { - $$ = add_array($1, 1, to_int_constant($3, 0)); + $$ = add_array($1, 1, to_int_constant($3, false)); } | notype_direct_decl param_list opt_asm_or_symbolrename { $$ = add_function(symbolrename($1, $3), $2); @@ -1076,7 +1075,7 @@ type_direct_decl: $$ = add_array($1, 0, 0); } | type_direct_decl T_LBRACK constant_expr T_RBRACK { - $$ = add_array($1, 1, to_int_constant($3, 0)); + $$ = add_array($1, 1, to_int_constant($3, false)); } | type_direct_decl param_list opt_asm_or_symbolrename { $$ = add_function(symbolrename($1, $3), $2); @@ -1116,7 +1115,7 @@ direct_param_decl: $$ = add_array($1, 0, 0); } | direct_param_decl T_LBRACK constant_expr T_RBRACK { - $$ = add_array($1, 1, to_int_constant($3, 0)); + $$ = add_array($1, 1, to_int_constant($3, false)); } | direct_param_decl param_list opt_asm_or_symbolrename { $$ = add_function(symbolrename($1, $3), $2); @@ -1145,7 +1144,7 @@ direct_notype_param_decl: $$ = add_array($1, 0, 0); } | direct_notype_param_decl T_LBRACK constant_expr T_RBRACK { - $$ = add_array($1, 1, to_int_constant($3, 0)); + $$ = add_array($1, 1, to_int_constant($3, false)); } | direct_notype_param_decl param_list opt_asm_or_symbolrename { $$ = add_function(symbolrename($1, $3), $2); @@ -1347,12 +1346,12 @@ init_expr_list: range: constant_expr { - $$.lo = to_int_constant($1, 1); + $$.lo = to_int_constant($1, true); $$.hi = $$.lo; } | constant_expr T_ELLIPSIS constant_expr { - $$.lo = to_int_constant($1, 1); - $$.hi = to_int_constant($3, 1); + $$.lo = to_int_constant($1, true); + $$.hi = to_int_constant($3, true); /* initialization with '[a...b]' is a GNU extension */ gnuism(340); } @@ -1445,7 +1444,7 @@ direct_abstract_decl: $$ = add_array(abstract_name(), 0, 0); } | T_LBRACK constant_expr T_RBRACK { - $$ = add_array(abstract_name(), 1, to_int_constant($2, 0)); + $$ = add_array(abstract_name(), 1, to_int_constant($2, false)); } | type_attribute direct_abstract_decl { $$ = $2; @@ -1454,7 +1453,7 @@ direct_abstract_decl: $$ = add_array($1, 0, 0); } | direct_abstract_decl T_LBRACK constant_expr T_RBRACK { - $$ = add_array($1, 1, to_int_constant($3, 0)); + $$ = add_array($1, 1, to_int_constant($3, false)); } | abstract_decl_param_list opt_asm_or_symbolrename { $$ = add_function(symbolrename(abstract_name(), $2), $1); @@ -2055,84 +2054,6 @@ yyerror(const char *msg) return 0; } -static __inline int uq_gt(uint64_t, uint64_t); -static __inline int q_gt(int64_t, int64_t); - -static __inline int -uq_gt(uint64_t a, uint64_t b) -{ - - return a > b; -} - -static __inline int -q_gt(int64_t a, int64_t b) -{ - - return a > b; -} - -#define q_lt(a, b) q_gt(b, a) - -/* - * Gets a node for a constant and returns the value of this constant - * as integer. - * - * If the node is not constant or too large for int or of type float, - * a warning will be printed. - * - * to_int_constant() should be used only inside declarations. If it is used in - * expressions, it frees the memory used for the expression. - */ -static int -to_int_constant(tnode_t *tn, int required) -{ - int i; - tspec_t t; - val_t *v; - - v = constant(tn, required); - - if (tn == NULL) { - i = 1; - goto done; - } - - /* - * Abstract declarations are used inside expression. To free - * the memory would be a fatal error. - * We don't free blocks that are inside casts because these - * will be used later to match types. - */ - if (tn->tn_op != CON && dcs->d_ctx != ABSTRACT) - tfreeblk(); - - if ((t = v->v_tspec) == FLOAT || t == DOUBLE || t == LDOUBLE) { - i = (int)v->v_ldbl; - /* integral constant expression expected */ - error(55); - } else { - i = (int)v->v_quad; - if (is_uinteger(t)) { - if (uq_gt((uint64_t)v->v_quad, - (uint64_t)TARG_INT_MAX)) { - /* integral constant too large */ - warning(56); - } - } else { - if (q_gt(v->v_quad, (int64_t)TARG_INT_MAX) || - q_lt(v->v_quad, (int64_t)TARG_INT_MIN)) { - /* integral constant too large */ - warning(56); - } - } - } - -done: - free(v); - return i; -} - static void cgram_declare(sym_t *decl, bool initflg, sbuf_t *renaming) { Index: src/usr.bin/xlint/lint1/decl.c diff -u src/usr.bin/xlint/lint1/decl.c:1.145 src/usr.bin/xlint/lint1/decl.c:1.146 --- src/usr.bin/xlint/lint1/decl.c:1.145 Wed Mar 17 01:38:31 2021 +++ src/usr.bin/xlint/lint1/decl.c Wed Mar 17 01:53:21 2021 @@ -1,4 +1,4 @@ -/* $NetBSD: decl.c,v 1.145 2021/03/17 01:38:31 rillig Exp $ */ +/* $NetBSD: decl.c,v 1.146 2021/03/17 01:53:21 rillig Exp $ */ /* * Copyright (c) 1996 Christopher G. Demetriou. All Rights Reserved. @@ -38,7 +38,7 @@ #include <sys/cdefs.h> #if defined(__RCSID) && !defined(lint) -__RCSID("$NetBSD: decl.c,v 1.145 2021/03/17 01:38:31 rillig Exp $"); +__RCSID("$NetBSD: decl.c,v 1.146 2021/03/17 01:53:21 rillig Exp $"); #endif #include <sys/param.h> @@ -3301,3 +3301,61 @@ print_previous_declaration(int msg, cons } curr_pos = cpos; } + +/* + * Gets a node for a constant and returns the value of this constant + * as integer. + * + * If the node is not constant or too large for int or of type float, + * a warning will be printed. + * + * to_int_constant() should be used only inside declarations. If it is used in + * expressions, it frees the memory used for the expression. + */ +int +to_int_constant(tnode_t *tn, bool required) +{ + int i; + tspec_t t; + val_t *v; + + v = constant(tn, required); + + if (tn == NULL) { + i = 1; + goto done; + } + + /* + * Abstract declarations are used inside expression. To free + * the memory would be a fatal error. + * We don't free blocks that are inside casts because these + * will be used later to match types. + */ + if (tn->tn_op != CON && dcs->d_ctx != ABSTRACT) + tfreeblk(); + + if ((t = v->v_tspec) == FLOAT || t == DOUBLE || t == LDOUBLE) { + i = (int)v->v_ldbl; + /* integral constant expression expected */ + error(55); + } else { + i = (int)v->v_quad; + if (is_uinteger(t)) { + if ((uint64_t)v->v_quad > (uint64_t)TARG_INT_MAX) { + /* integral constant too large */ + warning(56); + } + } else { + if (v->v_quad > (int64_t)TARG_INT_MAX || + v->v_quad < (int64_t)TARG_INT_MIN) { + /* integral constant too large */ + warning(56); + } + } + } + +done: + free(v); + return i; +} Index: src/usr.bin/xlint/lint1/externs1.h diff -u src/usr.bin/xlint/lint1/externs1.h:1.74 src/usr.bin/xlint/lint1/externs1.h:1.75 --- src/usr.bin/xlint/lint1/externs1.h:1.74 Wed Mar 17 01:38:31 2021 +++ src/usr.bin/xlint/lint1/externs1.h Wed Mar 17 01:53:21 2021 @@ -1,4 +1,4 @@ -/* $NetBSD: externs1.h,v 1.74 2021/03/17 01:38:31 rillig Exp $ */ +/* $NetBSD: externs1.h,v 1.75 2021/03/17 01:53:21 rillig Exp $ */ /* * Copyright (c) 1994, 1995 Jochen Pohl @@ -193,6 +193,7 @@ extern void check_usage(dinfo_t *); extern void check_usage_sym(bool, sym_t *); extern void check_global_symbols(void); extern void print_previous_declaration(int, const sym_t *); +extern int to_int_constant(tnode_t *, bool); /* * tree.c