2 * sparse/check_signed.c
4 * Copyright (C) 2009 Dan Carpenter.
6 * Licensed under the Open Software License version 1.1
11 * Check for things which are signed but probably should be unsigned.
13 * Hm... It seems like at this point in the processing, sparse makes all
14 * bitfields unsigned. Which is logical but not what GCC does.
22 static unsigned long long max_size(struct symbol
*base_type
)
27 unsigned long long ret = 0xffffffffffffffff;
29 But gcc complained that was too large. What am I doing wrong?
30 Oh well. I expect most of the problems are with smaller
34 unsigned long long ret
= 0xffffffff;
37 bits
= base_type
->bit_size
;
38 if (base_type
->ctype
.modifiers
& MOD_SIGNED
)
44 static void match_assign(struct expression
*expr
)
51 sym
= get_type(expr
->left
);
53 //sm_msg("could not get type");
56 if (sym
->bit_size
>= 32) /* max_val limits this */
58 if (!get_implied_value(expr
->right
, &val
))
62 name
= get_variable_from_expr_complex(expr
->left
, NULL
);
63 sm_msg("error: value %lld can't fit into %lld %s", val
, max
, name
);
68 static void match_condition(struct expression
*expr
)
71 struct expression
*var
= NULL
;
72 struct symbol
*sym
= NULL
;
76 if (expr
->type
!= EXPR_COMPARE
)
78 if (get_implied_value(expr
->left
, &known
))
80 else if (get_implied_value(expr
->right
, &known
))
86 if (!sym
|| sym
->bit_size
>= 32)
90 name
= get_variable_from_expr_complex(var
, NULL
);
91 sm_msg("error: value %lld is higher than %lld so this is always false.",
97 void check_signed(int id
)
103 add_hook(&match_assign
, ASSIGNMENT_HOOK
);
104 add_hook(&match_condition
, CONDITION_HOOK
);