aboutsummaryrefslogtreecommitdiffstatshomepage
diff options
context:
space:
mode:
authorLuc Van Oostenryck <luc.vanoostenryck@gmail.com>2022-06-05 18:29:41 +0200
committerLuc Van Oostenryck <luc.vanoostenryck@gmail.com>2022-06-05 18:30:32 +0200
commitd9c17b4dc21ef531d673d1d6e510347e141e6801 (patch)
tree121a0e8ff0b7fe17992524128a8e9ba428187449
parent56afb504c0b917a2563b44fabe2f1e92e4c14287 (diff)
parent187285b32ea71c25419a09724d9fe5f1b46aab2e (diff)
downloadsparse-d9c17b4dc21ef531d673d1d6e510347e141e6801.tar.gz
Merge branch 'cast-value'
* small improvements to cast_value()
-rw-r--r--expand.c10
-rw-r--r--expression.c3
-rw-r--r--expression.h3
-rw-r--r--parse.c3
4 files changed, 10 insertions, 9 deletions
diff --git a/expand.c b/expand.c
index c4f806de..f14e7181 100644
--- a/expand.c
+++ b/expand.c
@@ -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;
@@ -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 */
@@ -872,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;
diff --git a/expression.c b/expression.c
index efdaa367..727e7056 100644
--- a/expression.c
+++ b/expression.c
@@ -432,8 +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);
- expr->ctype = &int_ctype;
+ cast_value(expr, &int_ctype, expr);
} else {
expr->ctype = wchar_ctype;
}
diff --git a/expression.h b/expression.h
index f733c076..8bf40d32 100644
--- a/expression.h
+++ b/expression.h
@@ -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
diff --git a/parse.c b/parse.c
index 9f2a3cdf..3d6fef7c 100644
--- a/parse.c
+++ b/parse.c
@@ -903,8 +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);
- expr->ctype = base_type;
+ cast_value(expr, base_type, expr);
} END_FOR_EACH_PTR(sym);
}