c++: fix old cast warnings

We still have a few old C casts in lalr1.cc, let's get rid of them.
Reported by Frank Heckenbach.

Actually, let's monitor all our casts using easy to grep macros.
Let's use these macros to use the C++ standard casts when we are in
C++.

* data/skeletons/c.m4 (b4_cast_define): New.
* data/skeletons/glr.c, data/skeletons/glr.cc,
* data/skeletons/lalr1.cc, data/skeletons/stack.hh,
* data/skeletons/yacc.c:
Use it and/or its casts.

* tests/actions.at, tests/cxx-type.at,
* tests/glr-regression.at, tests/headers.at, tests/torture.at,
* tests/types.at:
Use YY_CAST instead of C casts.

* configure.ac (warn_cxx): Add -Wold-style-cast.
* doc/bison.texi: Disable it.
This commit is contained in:
Akim Demaille
2019-11-01 16:13:46 +01:00
parent 2bd1d9e20f
commit 3398b0fa90
14 changed files with 186 additions and 137 deletions

View File

@@ -772,10 +772,10 @@ static
static int counter = 0;
int c = ]AT_VAL[]m4_ifval([$6], [.ival])[ = counter++;
assert (c <= YY_CAST (int, strlen (source)));
/* As in BASIC, line numbers go from 10 to 10. */
]AT_LOC_FIRST_LINE[ = ]AT_LOC_FIRST_COLUMN[ = (10 * c);
]AT_LOC_LAST_LINE[ = ]AT_LOC_LAST_COLUMN[ = ]AT_LOC_FIRST_LINE[ + 9;
assert (c <= (int) strlen (source));
if (source[c])
fprintf (stderr, "sending: '%c'", source[c]);
else
@@ -1780,7 +1780,8 @@ float: UNTYPED INT
yy::parser::token::INT,
EOF}]],
[[{UNTYPED, INT, EOF}]]),
[AT_VAL.ival = (int) toknum * 10; AT_VAL.fval = (float) toknum / 10.0f;])[
[AT_VAL.ival = YY_CAST (int, toknum) * 10;
AT_VAL.fval = YY_CAST (float, toknum) / 10.0f;])[
]AT_MAIN_DEFINE[
]])
@@ -1805,7 +1806,6 @@ AT_CLEANUP
])
m4_map_args([AT_TEST], [yacc.c], [glr.c], [lalr1.cc], [glr.cc])
m4_popdef([AT_TEST])
## -------------------------------------------------- ##
@@ -1897,7 +1897,7 @@ exp:
%%
]AT_YYERROR_DEFINE[
]AT_YYLEX_DEFINE(["bcd"], [*lvalp = (int) ((toknum + 1) * 10)])[
]AT_YYLEX_DEFINE(["bcd"], [*lvalp = YY_CAST (int, (toknum + 1) * 10)])[
]AT_MAIN_DEFINE[
]])
AT_BISON_OPTION_POPDEFS

View File

@@ -178,7 +178,7 @@ main (int argc, char **argv)
do
{
buffer[i++] = (char) c;
buffer[i++] = YY_CAST (char, c);
colNum += 1;
assert (i != sizeof buffer - 1);
c = getchar ();
@@ -187,8 +187,8 @@ main (int argc, char **argv)
ungetc (c, stdin);
buffer[i++] = 0;
tok = isupper ((unsigned char) buffer[0]) ? TYPENAME : ID;
yylval = new_term (strcpy ((char *) malloc (i), buffer));
tok = isupper (YY_CAST (unsigned char, buffer[0])) ? TYPENAME : ID;
yylval = new_term (strcpy (YY_CAST (char *, malloc (i)), buffer));
}
else
{
@@ -206,7 +206,7 @@ main (int argc, char **argv)
static Node *
new_nterm (char const *form, Node *child0, Node *child1, Node *child2)
{
Node *node = (Node *) malloc (sizeof (Node));
Node *node = YY_CAST (Node *, malloc (sizeof (Node)));
node->nterm.isNterm = 1;
node->nterm.parents = 0;
node->nterm.form = form;
@@ -225,7 +225,7 @@ new_nterm (char const *form, Node *child0, Node *child1, Node *child2)
static Node *
new_term (char *text)
{
Node *node = (Node *) malloc (sizeof (Node));
Node *node = YY_CAST (Node *, malloc (sizeof (Node)));
node->term.isNterm = 0;
node->term.parents = 0;
node->term.text = text;
@@ -255,30 +255,27 @@ free_node (Node *node)
static char *
node_to_string (Node *node)
{
char *child0;
char *child1;
char *child2;
char *buffer;
char *res;
if (!node)
{
buffer = (char *) malloc (1);
buffer[0] = 0;
res = YY_CAST (char *, malloc (1));
res[0] = 0;
}
else if (node->nodeInfo.isNterm == 1)
{
child0 = node_to_string (node->nterm.children[0]);
child1 = node_to_string (node->nterm.children[1]);
child2 = node_to_string (node->nterm.children[2]);
buffer = (char *) malloc (strlen (node->nterm.form) + strlen (child0)
+ strlen (child1) + strlen (child2) + 1);
sprintf (buffer, node->nterm.form, child0, child1, child2);
free (child0);
free (child1);
char *child0 = node_to_string (node->nterm.children[0]);
char *child1 = node_to_string (node->nterm.children[1]);
char *child2 = node_to_string (node->nterm.children[2]);
res = YY_CAST (char *, malloc (strlen (node->nterm.form) + strlen (child0)
+ strlen (child1) + strlen (child2) + 1));
sprintf (res, node->nterm.form, child0, child1, child2);
free (child2);
free (child1);
free (child0);
}
else
buffer = strdup (node->term.text);
return buffer;
res = strdup (node->term.text);
return res;
}
]]

View File

@@ -153,7 +153,7 @@ var_list:
{ $$ = $1; }
| var ',' var_list
{
char *s = (char *) realloc ($1, strlen ($1) + 1 + strlen ($3) + 1);
char *s = YY_CAST (char *, realloc ($1, strlen ($1) + 1 + strlen ($3) + 1));
strcat (s, ",");
strcat (s, $3);
free ($3);
@@ -172,7 +172,6 @@ int
yylex (void)
{
char buf[50];
char *s;
assert (!feof (stdin));
switch (fscanf (input, " %1[a-z,]", buf))
{
@@ -181,15 +180,19 @@ yylex (void)
case EOF:
return 0;
default:
if (fscanf (input, "%49s", buf) != 1)
return 0;
else
{
char *s;
assert (strlen (buf) < sizeof buf - 1);
s = YY_CAST (char *, malloc (strlen (buf) + 1));
strcpy (s, buf);
yylval = s;
return 'V';
}
break;
}
if (fscanf (input, "%49s", buf) != 1)
return 0;
assert (strlen (buf) < sizeof buf - 1);
s = (char *) malloc (strlen (buf) + 1);
strcpy (s, buf);
yylval = s;
return 'V';
}
int
@@ -419,7 +422,7 @@ make_value (char const *parent, char const *child)
{
char const format[] = "%s <- %s";
char *value = *ptrs_next++ =
(char *) malloc (strlen (parent) + strlen (child) + sizeof format);
YY_CAST (char *, malloc (strlen (parent) + strlen (child) + sizeof format));
sprintf (value, format, parent, child);
return value;
}
@@ -429,7 +432,7 @@ merge (YYSTYPE s1, YYSTYPE s2)
{
char const format[] = "merge{ %s and %s }";
char *value = *ptrs_next++ =
(char *) malloc (strlen (s1.ptr) + strlen (s2.ptr) + sizeof format);
YY_CAST (char *, malloc (strlen (s1.ptr) + strlen (s2.ptr) + sizeof format));
sprintf (value, format, s1.ptr, s2.ptr);
return value;
}
@@ -601,7 +604,7 @@ stack2: 'a' ;
static int
yylex (void)
{
yylval.node = (count_node*) malloc (sizeof *yylval.node);
yylval.node = YY_CAST (count_node*, malloc (sizeof *yylval.node));
if (!yylval.node)
{
fprintf (stderr, "Test inconclusive.\n");
@@ -1118,7 +1121,7 @@ change_lookahead:
]AT_YYERROR_DEFINE[
]AT_YYLEX_DEFINE(["ab"],
[yylval.value = (char) (res + 'A' - 'a')])[
[yylval.value = YY_CAST (char, res + 'A' - 'a')])[
static void
print_lookahead (char const *reduction)
@@ -1319,10 +1322,10 @@ yylex (void)
{
static char const input[] = "abcdddd";
static int toknum = 0;
assert (toknum < (int) sizeof input);
assert (toknum < YY_CAST (int, sizeof input));
yylloc.first_line = yylloc.last_line = 1;
yylloc.first_column = yylloc.last_column = toknum + 1;
yylval.value = (char) (input[toknum] + 'A' - 'a');
yylval.value = YY_CAST (char, input[toknum] + 'A' - 'a');
return input[toknum++];
}
@@ -1349,7 +1352,7 @@ print_lookahead (char const *reduction)
static char
merge (union YYSTYPE s1, union YYSTYPE s2)
{
return (char) (s1.value + s2.value);
return YY_CAST (char, s1.value + s2.value);
}
int
@@ -1577,7 +1580,7 @@ yylex (YYSTYPE *lvalp, YYLTYPE *llocp)
{
static char const input[] = "ab";
static int toknum = 0;
assert (toknum < (int) sizeof input);
assert (toknum < YY_CAST (int, sizeof input));
lvalp->dummy = 0;
llocp->first_line = llocp->last_line = 2;
llocp->first_column = toknum + 1;

View File

@@ -322,6 +322,7 @@ AT_PERL_CHECK([[-n -0777 -e '
|YYPUSH_MORE(?:_DEFINED)?
|YYUSE
|YY_ATTRIBUTE(?:_PURE|_UNUSED)
|YY(?:_REINTERPRET)?_CAST
|YY_CONSTEXPR
|YY_COPY
|YY_CPLUSPLUS

View File

@@ -446,7 +446,7 @@ get_args (int argc, const char **argv)
assert (0 <= res);
assert (res <= INT_MAX);
assert (errno != ERANGE);
return (int) res;
return YY_CAST (int, res);
}
int

View File

@@ -166,7 +166,7 @@ m4_foreach([b4_skel], [[yacc.c], [glr.c], [lalr1.cc], [glr.cc]],
[if (res)
{
AT_VAL.ival = (res - '0') * 10;
AT_VAL.fval = (float) (res - '0') / 10.f;
AT_VAL.fval = YY_CAST (float, res - '0') / 10.f;
}],
[30 0.3])
@@ -197,7 +197,7 @@ m4_foreach([b4_skel], [[yacc.c], [glr.c], [lalr1.cc], [glr.cc]],
["12"],
[if (res)
{
AT_VAL.up = (struct u *) malloc (sizeof *AT_VAL.up);
AT_VAL.up = YY_CAST (struct u *, malloc (sizeof *AT_VAL.up));
assert (AT_VAL.up);
AT_VAL.up->ival = res - '0';
}