Prefer signed to unsigned integers

This patch contains more fixes to prefer signed to unsigned
integer types, as modern tools like 'gcc -fsanitize=undefined'
can check for signed integer overflow but not unsigned overflow.
* NEWS: Document the API change.
* boostrap.conf (gnulib_modules): Add intprops.
* data/skeletons/glr.c: Include stddef.h and stdint.h,
since this skeleton can assume C99 or later.
(YYSIZEMAX): Now signed, and the minimum of SIZE_MAX and PTRDIFF_MAX.
(yybool) [!__cplusplus]: Now signed (which is how bool behaves).
(YYTRANSLATE): Avoid use of unsigned, and make the macro
safe even for values greater than UINT_MAX.
(yytnamerr, struct yyGLRState, struct yyGLRStateSet, struct yyGLRStack)
(yyaddDeferredAction, yyinitStateSet, yyinitGLRStack)
(yyexpandGLRStack, yymarkStackDeleted, yyremoveDeletes)
(yyglrShift, yyglrShiftDefer, yy_reduce_print, yydoAction)
(yyglrReduce, yysplitStack, yyreportTree, yycompressStack)
(yyprocessOneStack, yyreportSyntaxError, yyrecoverSyntaxError)
(yyparse, yy_yypstack, yypstack, yypdumpstack):
* tests/input.at (Torturing the Scanner):
Prefer ptrdiff_t to size_t.
* data/skeletons/c++.m4 (b4_yytranslate_define):
* src/AnnotationList.c (AnnotationList__computePredecessorAnnotations):
* src/AnnotationList.h (AnnotationIndex):
* src/InadequacyList.h (InadequacyListNodeCount):
* src/closure.c (closure_new):
* src/complain.c (error_message, complains, complain_indent)
(complain_args, duplicate_directive, duplicate_rule_directive):
* src/gram.c (nritems, ritem_print, grammar_dump):
* src/ielr.c (ielr_compute_ritem_sees_lookahead_set)
(ielr_item_has_lookahead, ielr_compute_annotation_lists)
(ielr_compute_lookaheads):
* src/location.c (columns, boundary_print, location_print):
* src/muscle-tab.c (muscle_percent_define_insert)
(muscle_percent_define_check_values):
* src/output.c (prepare_rules, prepare_actions):
* src/parse-gram.y (id, handle_require):
* src/reader.c (record_merge_function_type, packgram):
* src/reduce.c (nuseless_productions, nuseless_nonterminals)
(inaccessable_symbols):
* src/relation.c (relation_print):
* src/scan-code.l (variant, variant_table_size, variant_count)
(variant_add, get_at_spec, show_sub_message, show_sub_messages)
(parse_ref):
* src/scan-gram.l (<SC_ESCAPED_STRING,SC_ESCAPED_CHARACTER>)
(scan_integer, convert_ucn_to_byte, handle_syncline):
* src/scan-skel.l (at_complain):
* src/symtab.c (complain_symbol_redeclared)
(complain_semantic_type_redeclared, complain_class_redeclared)
(symbol_class_set, complain_user_token_number_redeclared):
* src/tables.c (conflict_tos, conflrow, conflict_table)
(conflict_list, save_row, pack_vector):
* tests/local.at (AT_YYLEX_DEFINE(c)):
Prefer signed to unsigned integer.
* data/skeletons/lalr1.cc (yy_lac_check_):
* tests/actions.at (_AT_CHECK_PRINTER_AND_DESTRUCTOR):
* tests/local.at (AT_YYLEX_DEFINE(c)):
Omit now-unnecessary casts.
* data/skeletons/location.cc (b4_location_define):
* doc/bison.texi (Mfcalc Lexer, C++ position, C++ location):
Prefer int to unsigned for line and column numbers.
Change example to abort explicitly on memory exhaustion,
and fix an off-by-one bug that led to undefined behavior.
* data/skeletons/stack.hh (stack::operator[]):
Also allow ptrdiff_t indexes.
(stack::pop, slice::slice, slice::operator[]):
Index arg is now ptrdiff_t, not int.
(stack::ssize): New method.
(slice::range_): Now ptrdiff_t, not int.
* data/skeletons/yacc.c (b4_state_num_type): Remove.
All uses replaced by b4_int_type.
(YY_CONVERT_INT_BEGIN, YY_CONVERT_INT_END): New macros.
(yylac, yyparse): Use them around conversions that -Wconversion
would give false alarms about. 	Omit unnecessary casts.
(yy_stack_print): Use int rather than unsigned, and omit
a cast that doesn’t seem to be needed here any more.
* examples/c++/variant.yy (yylex):
* examples/c++/variant-11.yy (yylex):
Omit no-longer-needed conversions to unsigned.
* src/InadequacyList.c (InadequacyList__new_conflict):
Don’t assume *node_count is unsigned.
* src/output.c (muscle_insert_unsigned_table):
Remove; no longer used.
This commit is contained in:
Paul Eggert
2019-10-02 16:56:32 -07:00
parent 4d9ff272cf
commit 133edcd248
41 changed files with 326 additions and 299 deletions

View File

@@ -223,7 +223,7 @@ contains_dot_or_dash (const char* p)
typedef struct
{
/* Index in symbol list. */
unsigned symbol_index;
int symbol_index;
/* Matched symbol id and loc. */
uniqstr id;
@@ -250,8 +250,8 @@ typedef struct
#define VARIANT_NOT_VISIBLE_FROM_MIDRULE (1 << 2)
static variant *variant_table = NULL;
static unsigned variant_table_size = 0;
static unsigned variant_count = 0;
static int variant_table_size = 0;
static int variant_count = 0;
static variant *
variant_table_grow (void)
@@ -286,7 +286,7 @@ find_prefix_end (char const *prefix, char const *cp, char const *end)
}
static variant *
variant_add (uniqstr id, location id_loc, unsigned symbol_index,
variant_add (uniqstr id, location id_loc, int symbol_index,
char const *cp, char const *cp_end, bool explicit_bracketing)
{
char const *prefix_end = find_prefix_end (id, cp, cp_end);
@@ -307,7 +307,7 @@ variant_add (uniqstr id, location id_loc, unsigned symbol_index,
}
static const char *
get_at_spec(unsigned symbol_index)
get_at_spec(int symbol_index)
{
static char at_buf[20];
if (symbol_index == 0)
@@ -321,7 +321,7 @@ static void
show_sub_message (warnings warning,
const char* cp, bool explicit_bracketing,
int midrule_rhs_index, char dollar_or_at,
unsigned indent, const variant *var)
int indent, const variant *var)
{
const char *at_spec = get_at_spec (var->symbol_index);
@@ -385,9 +385,9 @@ static void
show_sub_messages (warnings warning,
const char* cp, bool explicit_bracketing,
int midrule_rhs_index, char dollar_or_at,
unsigned indent)
int indent)
{
for (unsigned i = 0; i < variant_count; ++i)
for (int i = 0; i < variant_count; ++i)
show_sub_message (warning | silent,
cp, explicit_bracketing,
midrule_rhs_index, dollar_or_at,
@@ -437,7 +437,7 @@ parse_ref (char *cp, symbol_list *rule, int rule_length,
/* Add all relevant variants. */
{
unsigned symbol_index;
int symbol_index;
symbol_list *l;
variant_count = 0;
for (symbol_index = 0, l = rule; !symbol_list_null (l);
@@ -459,12 +459,12 @@ parse_ref (char *cp, symbol_list *rule, int rule_length,
}
/* Check errors. */
unsigned valid_variants = 0;
unsigned valid_variant_index = 0;
for (unsigned i = 0; i < variant_count; ++i)
int valid_variants = 0;
int valid_variant_index = 0;
for (int i = 0; i < variant_count; ++i)
{
variant *var = &variant_table[i];
unsigned symbol_index = var->symbol_index;
int symbol_index = var->symbol_index;
/* Check visibility from midrule actions. */
if (midrule_rhs_index != 0
@@ -490,9 +490,9 @@ parse_ref (char *cp, symbol_list *rule, int rule_length,
{
case 0:
{
unsigned len = (explicit_bracketing || !ref_tail_fields) ?
int len = (explicit_bracketing || !ref_tail_fields) ?
cp_end - cp : ref_tail_fields - cp;
unsigned indent = 0;
int indent = 0;
complain_indent (text_loc, complaint, &indent,
_("invalid reference: %s"), quote (text));
@@ -525,7 +525,7 @@ parse_ref (char *cp, symbol_list *rule, int rule_length,
}
case 1:
{
unsigned indent = 0;
int indent = 0;
if (variant_count > 1)
{
complain_indent (text_loc, Wother, &indent,
@@ -535,7 +535,7 @@ parse_ref (char *cp, symbol_list *rule, int rule_length,
dollar_or_at, indent + SUB_INDENT);
}
{
unsigned symbol_index =
int symbol_index =
variant_table[valid_variant_index].symbol_index;
return (symbol_index == midrule_rhs_index) ? LHS_REF : symbol_index;
}
@@ -543,7 +543,7 @@ parse_ref (char *cp, symbol_list *rule, int rule_length,
case 2:
default:
{
unsigned indent = 0;
int indent = 0;
complain_indent (text_loc, complaint, &indent,
_("ambiguous reference: %s"), quote (text));
show_sub_messages (complaint,