2 * sparse/smatch_extra.c
4 * Copyright (C) 2008 Dan Carpenter.
6 * Licensed under the Open Software License version 1.1
19 static struct smatch_state zero
= {
24 static struct smatch_state one
= {
29 static struct smatch_state
*alloc_state(int val
)
31 struct smatch_state
*state
;
40 state
= malloc(sizeof(*state
));
41 state
->name
= "value";
42 state
->data
= malloc(sizeof(int));
43 *(int *)state
->data
= val
;
47 static struct smatch_state
*merge_func(const char *name
, struct symbol
*sym
,
48 struct smatch_state
*s1
,
49 struct smatch_state
*s2
)
54 static void match_function_call_after(struct expression
*expr
)
56 struct expression
*tmp
;
61 FOR_EACH_PTR(expr
->args
, tmp
) {
63 name
= get_variable_from_expr(tmp
->unop
, &sym
);
65 set_state(name
, my_id
, sym
, &undefined
);
69 } END_FOR_EACH_PTR(tmp
);
72 static void match_assign(struct expression
*expr
)
74 struct expression
*left
;
78 left
= strip_expr(expr
->left
);
79 name
= get_variable_from_expr(left
, &sym
);
82 set_state(name
, my_id
, sym
, alloc_state(get_value(expr
->right
)));
85 static void undef_expr(struct expression
*expr
)
90 name
= get_variable_from_expr(expr
->unop
, &sym
);
93 if (!get_state(name
, my_id
, sym
)) {
97 set_state(name
, my_id
, sym
, &undefined
);
100 static void match_declarations(struct symbol
*sym
)
105 name
= sym
->ident
->name
;
106 if (sym
->initializer
) {
107 set_state(name
, my_id
, sym
, alloc_state(get_value(sym
->initializer
)));
112 static void match_unop(struct expression
*expr
)
119 name
= get_variable_from_expr(expr
->unop
, &sym
);
123 tmp
= show_special(expr
->op
);
124 if ((!strcmp(tmp
, "--")) || (!strcmp(tmp
, "++")))
125 set_state(name
, my_id
, sym
, &undefined
);
128 void register_smatch_extra(int id
)
131 add_merge_hook(my_id
, &merge_func
);
132 add_hook(&undef_expr
, OP_HOOK
);
133 add_hook(&match_function_call_after
, FUNCTION_CALL_AFTER_HOOK
);
134 add_hook(&match_assign
, ASSIGNMENT_AFTER_HOOK
);
135 add_hook(&match_declarations
, DECLARATION_HOOK
);
136 add_hook(&match_unop
, OP_HOOK
);
137 add_hook(&__implied_states_hook
, WHOLE_CONDITION_HOOK
);
140 static int expr_to_val(struct expression
*expr
)
142 struct smatch_state
*state
;
147 val
= get_value(expr
);
148 if (val
!= UNDEFINED
)
151 name
= get_variable_from_expr(expr
, &sym
);
154 state
= get_state(name
, my_id
, sym
);
156 if (!state
|| !state
->data
)
158 return *(int *)state
->data
;
161 static int true_comparison(int left
, int comparison
, int right
)
165 case SPECIAL_UNSIGNED_LT
:
169 case SPECIAL_UNSIGNED_LTE
:
177 case SPECIAL_UNSIGNED_GTE
:
182 case SPECIAL_UNSIGNED_GT
:
186 case SPECIAL_NOTEQUAL
:
191 smatch_msg("unhandled comparison %d\n", comparison
);
196 int known_condition_true(struct expression
*expr
)
198 int left
, right
, ret
;
200 if (!expr
|| expr
->type
!= EXPR_COMPARE
)
203 if ((left
= expr_to_val(expr
->left
)) == UNDEFINED
)
206 if ((right
= expr_to_val(expr
->right
)) == UNDEFINED
)
209 ret
= true_comparison(left
, expr
->op
, right
);
211 SM_DEBUG("%d known condition: %d %s %d => true", get_lineno(),
212 left
, show_special(expr
->op
), right
);
214 smatch_msg("known condition: %d %s %d => false", left
,
215 show_special(expr
->op
), right
);