silence some sparse warnings: () => (void)
[smatch.git] / check_signed.c
blob680b4497540b6f40b89704df382d0667595c8e30
1 /*
2 * sparse/check_signed.c
4 * Copyright (C) 2009 Dan Carpenter.
6 * Licensed under the Open Software License version 1.1
8 */
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.
18 #include "smatch.h"
20 static int my_id;
22 #define RIGHT 0
23 #define LEFT 1
25 static int is_unsigned(struct symbol *base_type)
27 if (base_type->ctype.modifiers & MOD_UNSIGNED)
28 return 1;
29 return 0;
32 static void match_assign(struct expression *expr)
34 struct symbol *sym;
35 long long val;
36 long long max;
37 char *name;
39 sym = get_type(expr->left);
40 if (!sym) {
41 //sm_msg("could not get type");
42 return;
44 if (sym->bit_size >= 32) /* max_val limits this */
45 return;
46 if (!get_implied_value(expr->right, &val))
47 return;
48 max = type_max(sym);
49 if (max && max < val) {
50 name = get_variable_from_expr_complex(expr->left, NULL);
51 sm_msg("warn: value %lld can't fit into %lld '%s'", val, max, name);
52 free_string(name);
56 static void match_condition(struct expression *expr)
58 long long known;
59 struct expression *var = NULL;
60 struct symbol *type = NULL;
61 long long max;
62 int lr;
63 char *name;
65 if (expr->type != EXPR_COMPARE)
66 return;
68 if (get_value(expr->left, &known)) {
69 lr = RIGHT;
70 var = expr->right;
71 } else if (get_value(expr->right, &known)) {
72 lr = LEFT;
73 var = expr->left;
74 } else {
75 return;
78 type = get_type(var);
79 if (!type || type->bit_size >= 32)
80 return;
82 max = type_max(type);
83 if (!max)
84 return;
86 name = get_variable_from_expr_complex(var, NULL);
88 if (known < 0) {
89 if (is_unsigned(type))
90 sm_msg("error: comparing unsigned '%s' to negative", name);
91 goto free;
94 if (known == 0) {
95 if (!is_unsigned(type))
96 goto free;
97 if (lr == LEFT) {
98 if (expr->op == '<')
99 sm_msg("error: unsigned '%s' cannot be less than 0", name);
100 if (expr->op == SPECIAL_LTE)
101 sm_msg("warn: unsigned '%s' cannot be less than 0", name);
103 if (lr == RIGHT) {
104 if (expr->op == '>')
105 sm_msg("error: unsigned '%s' cannot be less than 0", name);
106 if (expr->op == SPECIAL_GTE)
107 sm_msg("warn: unsigned '%s' cannot be less than 0", name);
109 goto free;
112 if (max < known) {
113 const char *tf = "the same";
115 if (expr->op == SPECIAL_EQUAL)
116 tf = "false";
117 if (expr->op == SPECIAL_NOTEQUAL)
118 tf = "true";
119 if (lr == LEFT && (expr->op == '<' || expr->op == SPECIAL_LTE))
120 tf = "true";
121 if (lr == RIGHT && (expr->op == '>' || expr->op == SPECIAL_GTE))
122 tf = "false";
123 sm_msg("warn: %lld is higher than %lld (max '%s' can be) so this is always %s.",
124 known, max, name, tf);
126 free:
127 free_string(name);
130 void check_signed(int id)
132 my_id = id;
134 add_hook(&match_assign, ASSIGNMENT_HOOK);
135 add_hook(&match_condition, CONDITION_HOOK);