improve cast_value()
cover improve cast_value()
From: Luc Van Oostenryck <[email protected]>
This series contains 2 small improvements to the cast_value() function.
Luc Van Oostenryck (2):
cast_value: assign the new type
cast_value: remove error-prone redundant argument
expand.c | 10 +++++++--- expression.c | 3 +-- expression.h | 3 +-- parse.c | 3 +-- 4 files changed, 10 insertions(+), 9 deletions(-)
Signature · 1 line
2.36.1
PATCH 1/2 cast_value: assign the new type
From: Luc Van Oostenryck <[email protected]>
The first two arguments of cast_value() are the new expression and the type wanted for it. This type is then used to calculate the new value.
But the type of the expression must be assigned separately (usually after the cast because the old and the new expression can refer to the same object).
To avoid any possible inconsistencies, assign the new type during the casting itself.
--- expand.c | 4 ++++ expression.c | 1 - parse.c | 1 - 3 files changed, 4 insertions(+), 2 deletions(-)
expand.c +4 -0
@@ -110,11 +110,13 @@ void cast_value(struct expression *expr, struct symbol *newtype, expr->taint = old->taint;if (old_size == new_size) { expr->value = old->value; + expr->ctype = newtype;return ; }// expand it to the full "long long" value value =get_longlong (old); + expr->ctype = newtype; Int:// _Bool requires a zero test rather than truncation.
@@ -153,6 +155,7 @@ Float: value = (long long )old->fvalue; expr->type =EXPR_VALUE ; expr->taint =0 ; + expr->ctype = newtype; goto Int; }
@@ -168,6 +171,7 @@ Float: expr->fvalue = (float )expr->fvalue; } expr->type =EXPR_FVALUE ; + expr->ctype = newtype; }/* Return true if constant shift size is valid */
expression.c +0 -1
@@ -433,7 +433,6 @@ struct token *primary_expression(struct token *token, struct expression **tree)if (token_type (token) <TOKEN_WIDE_CHAR ) { expr->ctype = &char_ctype;cast_value (expr, &int_ctype, expr, expr->ctype); - expr->ctype = &int_ctype; }else { expr->ctype = wchar_ctype; }
parse.c +0 -1
@@ -904,7 +904,6 @@ static void cast_enum_list(struct symbol_list *list, struct symbol *base_type)continue ; }cast_value (expr, base_type, expr, ctype); - expr->ctype = base_type; }END_FOR_EACH_PTR (sym); }
Signature · 1 line
2.36.1
PATCH 2/2 cast_value: remove error-prone redundant argument
In reply to Luc Van OostenryckFrom: Luc Van Oostenryck <[email protected]>
The last two arguments of cast_value() are the old expression and the oldtype which suggest that this oldtype can be distinct from the type of the old expression.
But this is not the case because internally the type used to retrieve the value of the expression is the type of the expression itself (old->ctype) the type which is used and the two types must be the same (or at least be equivalent).
So, remove the error-prone last argument and always us the type of the expression itself.
--- expand.c | 6 +++--- expression.c | 2 +- expression.h | 3 +-- parse.c | 2 +- 4 files changed, 6 insertions(+), 7 deletions(-)
expand.c +3 -3
@@ -94,9 +94,9 @@ static long long get_longlong(struct expression *expr)return (value & andmask) | ormask; } -void cast_value (struct expression *expr,struct symbol *newtype, -struct expression *old,struct symbol *oldtype) +void cast_value (struct expression *expr,struct symbol *newtype,struct expression *old) { +struct symbol *oldtype = old->ctype;int old_size = oldtype->bit_size;int new_size = newtype->bit_size;long long value, mask, signmask;
@@ -876,7 +876,7 @@ static int expand_cast(struct expression *expr)/* Simplify normal integer casts.. */ if (target->type ==EXPR_VALUE || target->type ==EXPR_FVALUE ) { -cast_value (expr, expr->ctype, target, target->ctype); +cast_value (expr, expr->ctype, target);return 0 ; }return cost +1 ;
expression.c +1 -1
@@ -432,7 +432,7 @@ struct token *primary_expression(struct token *token, struct expression **tree)// TODO: handle 'u8', 'u' & 'U' prefixes. if (token_type (token) <TOKEN_WIDE_CHAR ) { expr->ctype = &char_ctype; -cast_value (expr, &int_ctype, expr, expr->ctype); +cast_value (expr, &int_ctype, expr); }else { expr->ctype = wchar_ctype; }
expression.h +1 -2
@@ -337,7 +337,6 @@ struct token *compound_statement(struct token *, struct statement *);#define constant_expression (token,tree) conditional_expression(token, tree)/* Cast folding of constant values.. */ -void cast_value (struct expression *expr,struct symbol *newtype, -struct expression *old,struct symbol *oldtype); +void cast_value (struct expression *expr,struct symbol *newtype,struct expression *old);#endif
parse.c +1 -1
@@ -903,7 +903,7 @@ static void cast_enum_list(struct symbol_list *list, struct symbol *base_type) expr->ctype = &int_ctype;continue ; } -cast_value (expr, base_type, expr, ctype); +cast_value (expr, base_type, expr); }END_FOR_EACH_PTR (sym); }
Signature · 1 line
2.36.1