vla: evaluate operand of sizeof if its type is a vla

The c99 standard in "6.5.3.4 The sizeof operator" states:

 If the type of the operand is a variable length array type, the operand
 is evaluated;[...]

This patch mirrors the following c99 semantic in gdb:

 1| int vla[n][m];
 2| int i = 1;
 3| sizeof(vla[i++][0]); // No sideffect
 4| assert (i == 1);
 5| sizeof(vla[i++]);    // With sideffect
 6| assert (i == 2);

Note: ptype/whatis still do not allow any sideeffects.

This patch was motivated by:

  https://sourceware.org/ml/gdb-patches/2014-01/msg00732.html

gdb/ChangeLog:

	* eval.c (evaluate_subexp_for_sizeof): Add enum noside argument.
	(evaluate_subexp_standard): Pass noside argument.
	(evaluate_subexp_for_sizeof) <BINOP_SUBSCRIPT>: Handle subscript case
	if noside equals EVAL_NORMAL. If the subscript yields a vla type
	re-evaluate subscript operation with EVAL_NORMAL to enable sideffects.
	* gdbtypes.c (resolve_dynamic_bounds): Mark bound as evaluated.
	* gdbtypes.h (enum range_flags): Add RANGE_EVALUATED case.

testsuite/ChangeLog:

	* gdb.base/vla-sideeffect.c: New file.
	* gdb.base/vla-sideeffect.exp: New file.
This commit is contained in:
Sanimir Agovic 2014-02-05 16:22:08 +00:00 committed by Joel Brobecker
parent 1612e0c0f9
commit 5ecaaa66e0
7 changed files with 191 additions and 4 deletions

View file

@ -50,7 +50,8 @@ extern int overload_resolution;
/* Prototypes for local functions. */
static struct value *evaluate_subexp_for_sizeof (struct expression *, int *);
static struct value *evaluate_subexp_for_sizeof (struct expression *, int *,
enum noside);
static struct value *evaluate_subexp_for_address (struct expression *,
int *, enum noside);
@ -2562,7 +2563,7 @@ evaluate_subexp_standard (struct type *expect_type,
evaluate_subexp (NULL_TYPE, exp, pos, EVAL_SKIP);
goto nosideret;
}
return evaluate_subexp_for_sizeof (exp, pos);
return evaluate_subexp_for_sizeof (exp, pos, noside);
case UNOP_CAST:
(*pos) += 2;
@ -2996,10 +2997,13 @@ evaluate_subexp_with_coercion (struct expression *exp,
/* Evaluate a subexpression of EXP, at index *POS,
and return a value for the size of that subexpression.
Advance *POS over the subexpression. */
Advance *POS over the subexpression. If NOSIDE is EVAL_NORMAL
we allow side-effects on the operand if its type is a variable
length array. */
static struct value *
evaluate_subexp_for_sizeof (struct expression *exp, int *pos)
evaluate_subexp_for_sizeof (struct expression *exp, int *pos,
enum noside noside)
{
/* FIXME: This should be size_t. */
struct type *size_type = builtin_type (exp->gdbarch)->builtin_int;
@ -3052,6 +3056,37 @@ evaluate_subexp_for_sizeof (struct expression *exp, int *pos)
(*pos) += 4;
break;
/* Deal with the special case if NOSIDE is EVAL_NORMAL and the resulting
type of the subscript is a variable length array type. In this case we
must re-evaluate the right hand side of the subcription to allow
side-effects. */
case BINOP_SUBSCRIPT:
if (noside == EVAL_NORMAL)
{
int pc = (*pos) + 1;
val = evaluate_subexp (NULL_TYPE, exp, &pc, EVAL_AVOID_SIDE_EFFECTS);
type = check_typedef (value_type (val));
if (TYPE_CODE (type) == TYPE_CODE_ARRAY)
{
type = check_typedef (TYPE_TARGET_TYPE (type));
if (TYPE_CODE (type) == TYPE_CODE_ARRAY)
{
type = TYPE_INDEX_TYPE (type);
/* Only re-evaluate the right hand side if the resulting type
is a variable length type. */
if (TYPE_RANGE_DATA (type)->flag_bound_evaluated)
{
val = evaluate_subexp (NULL_TYPE, exp, pos, EVAL_NORMAL);
return value_from_longest
(size_type, (LONGEST) TYPE_LENGTH (value_type (val)));
}
}
}
}
/* Fall through. */
default:
val = evaluate_subexp (NULL_TYPE, exp, pos, EVAL_AVOID_SIDE_EFFECTS);
type = value_type (val);