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 void match_function_call_after(struct expression
*expr
)
49 struct expression
*tmp
;
54 FOR_EACH_PTR(expr
->args
, tmp
) {
56 name
= get_variable_from_expr(tmp
->unop
, &sym
);
58 set_state(name
, my_id
, sym
, &undefined
);
62 } END_FOR_EACH_PTR(tmp
);
65 static void match_assign(struct expression
*expr
)
67 struct expression
*left
;
71 left
= strip_expr(expr
->left
);
72 name
= get_variable_from_expr(left
, &sym
);
75 set_state(name
, my_id
, sym
, alloc_state(get_value(expr
->right
)));
78 static void undef_expr(struct expression
*expr
)
83 name
= get_variable_from_expr(expr
->unop
, &sym
);
86 if (!get_state(name
, my_id
, sym
)) {
90 set_state(name
, my_id
, sym
, &undefined
);
93 static void match_declarations(struct symbol
*sym
)
98 name
= sym
->ident
->name
;
99 if (sym
->initializer
) {
100 set_state(name
, my_id
, sym
, alloc_state(get_value(sym
->initializer
)));
105 static void match_unop(struct expression
*expr
)
112 name
= get_variable_from_expr(expr
->unop
, &sym
);
116 tmp
= show_special(expr
->op
);
117 if ((!strcmp(tmp
, "--")) || (!strcmp(tmp
, "++")))
118 set_state(name
, my_id
, sym
, &undefined
);
121 void register_smatch_extra(int id
)
124 add_hook(&undef_expr
, OP_HOOK
);
125 add_hook(&match_function_call_after
, FUNCTION_CALL_AFTER_HOOK
);
126 add_hook(&match_assign
, ASSIGNMENT_AFTER_HOOK
);
127 add_hook(&match_declarations
, DECLARATION_HOOK
);
128 add_hook(&match_unop
, OP_HOOK
);
129 add_hook(&__implied_states_hook
, CONDITION_HOOK
);
132 static int expr_to_val(struct expression
*expr
)
134 struct smatch_state
*state
;
139 val
= get_value(expr
);
140 if (val
!= UNDEFINED
)
143 name
= get_variable_from_expr(expr
, &sym
);
146 state
= get_state(name
, my_id
, sym
);
148 if (!state
|| !state
->data
)
150 return *(int *)state
->data
;
153 static int true_comparison(int left
, int comparison
, int right
)
157 case SPECIAL_UNSIGNED_LT
:
161 case SPECIAL_UNSIGNED_LTE
:
169 case SPECIAL_UNSIGNED_GTE
:
174 case SPECIAL_UNSIGNED_GT
:
178 case SPECIAL_NOTEQUAL
:
183 smatch_msg("unhandled comparison %d\n", comparison
);
188 int known_condition_true(struct expression
*expr
)
190 int left
, right
, ret
;
192 if (!expr
|| expr
->type
!= EXPR_COMPARE
)
195 if ((left
= expr_to_val(expr
->left
)) == UNDEFINED
)
198 if ((right
= expr_to_val(expr
->right
)) == UNDEFINED
)
201 ret
= true_comparison(left
, expr
->op
, right
);
203 SM_DEBUG("%d known condition: %d %s %d => true", get_lineno(),
204 left
, show_special(expr
->op
), right
);
206 smatch_msg("known condition: %d %s %d => false", left
,
207 show_special(expr
->op
), right
);