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
);
131 static int expr_to_val(struct expression
*expr
)
133 struct smatch_state
*state
;
138 val
= get_value(expr
);
139 if (val
!= UNDEFINED
)
142 name
= get_variable_from_expr(expr
, &sym
);
145 state
= get_state(name
, my_id
, sym
);
147 if (!state
|| !state
->data
)
149 return *(int *)state
->data
;
152 static int true_comparison(int left
, int comparison
, int right
)
156 case SPECIAL_UNSIGNED_LT
:
160 case SPECIAL_UNSIGNED_LTE
:
168 case SPECIAL_UNSIGNED_GTE
:
173 case SPECIAL_UNSIGNED_GT
:
177 case SPECIAL_NOTEQUAL
:
182 smatch_msg("unhandled comparison %d\n", comparison
);
188 static int do_comparison(struct expression
*expr
)
190 int left
, right
, ret
;
192 if ((left
= expr_to_val(expr
->left
)) == UNDEFINED
)
195 if ((right
= expr_to_val(expr
->right
)) == UNDEFINED
)
198 ret
= true_comparison(left
, expr
->op
, right
);
200 SM_DEBUG("%d known condition: %d %s %d => true\n",
201 get_lineno(), left
, show_special(expr
->op
), right
);
202 } else if (ret
== 0) {
203 SM_DEBUG("%d known condition: %d %s %d => false\n",
204 get_lineno(), left
, show_special(expr
->op
), right
);
209 int last_stmt_val(struct statement
*stmt
)
211 struct expression
*expr
;
213 stmt
= last_ptr_list((struct ptr_list
*)stmt
->stmts
);
214 if (stmt
->type
!= STMT_EXPRESSION
)
216 expr
= stmt
->expression
;
217 return get_value(expr
);
220 int known_condition_true(struct expression
*expr
)
227 tmp
= get_value(expr
);
228 if (tmp
&& tmp
!= UNDEFINED
)
231 expr
= strip_expr(expr
);
234 if (do_comparison(expr
) == 1)
238 struct statement
*stmt
;
240 stmt
= get_block_thing(expr
);
241 if (stmt
&& (last_stmt_val(stmt
) == 1))
249 int known_condition_false(struct expression
*expr
)
259 if (do_comparison(expr
) == 0)
262 struct statement
*stmt
;
263 struct expression
*tmp
;
265 stmt
= get_block_thing(expr
);
266 if (stmt
&& (last_stmt_val(stmt
) == 0))
268 tmp
= strip_expr(expr
);
270 return known_condition_false(tmp
);