2 * sparse/smatch_extra.c
4 * Copyright (C) 2008 Dan Carpenter.
6 * Licensed under the Open Software License version 1.1
13 #include "smatch_slist.h"
14 #include "smatch_extra.h"
18 struct data_info unknown_num
= {
24 static struct smatch_state extra_undefined
= {
29 static struct smatch_state
*alloc_extra_state_no_name(int val
)
31 struct smatch_state
*state
;
33 state
= __alloc_smatch_state(0);
34 state
->data
= (void *)alloc_data_info(val
);
38 struct smatch_state
*alloc_extra_state(int val
)
40 struct smatch_state
*state
;
44 return &extra_undefined
;
46 state
= alloc_extra_state_no_name(val
);
47 snprintf(name
, 20, "%d", val
);
48 state
->name
= alloc_sname(name
);
52 struct smatch_state
*add_filter(struct smatch_state
*orig
, long long num
)
55 struct smatch_state
*ret
;
56 struct data_info
*orig_info
= NULL
;
57 struct data_info
*ret_info
;
61 orig_info
= (struct data_info
*)orig
->data
;
62 ret
= alloc_extra_state_no_name(UNDEFINED
);
63 snprintf(buf
, 254, "%s f%lld", orig
?orig
->name
:"any", num
);
65 ret
->name
= alloc_sname(buf
);
66 ret_info
= (struct data_info
*)ret
->data
;
67 ret_info
->values
= NULL
;
69 ret_info
->values
= clone_num_list(orig_info
->values
);
70 ret_info
->filter
= NULL
;
72 ret_info
->filter
= clone_num_list(orig_info
->filter
);
73 add_num(&ret_info
->filter
, num
);
77 static struct smatch_state
*merge_func(const char *name
, struct symbol
*sym
,
78 struct smatch_state
*s1
,
79 struct smatch_state
*s2
)
81 struct data_info
*info1
= (struct data_info
*)s1
->data
;
82 struct data_info
*info2
= (struct data_info
*)s2
->data
;
83 struct data_info
*ret_info
;
84 struct smatch_state
*tmp
;
86 tmp
= alloc_extra_state_no_name(UNDEFINED
);
87 tmp
->name
= "extra_merged";
88 ret_info
= (struct data_info
*)tmp
->data
;
90 ret_info
->values
= num_list_union(info1
->values
, info2
->values
);
91 ret_info
->filter
= num_list_intersection(info1
->values
, info2
->values
);
95 struct sm_state
*__extra_merge(struct sm_state
*one
, struct state_list
*slist1
,
96 struct sm_state
*two
, struct state_list
*slist2
)
98 struct data_info
*info1
;
99 struct data_info
*info2
;
101 if (!one
->state
->data
|| !two
->state
->data
) {
102 smatch_msg("internal error in smatch extra '%s = %s or %s'",
103 one
->name
, show_state(one
->state
),
104 show_state(two
->state
));
105 return alloc_state(one
->name
, one
->owner
, one
->sym
,
109 info1
= (struct data_info
*)one
->state
->data
;
110 info2
= (struct data_info
*)two
->state
->data
;
113 free_stack(&one
->my_pools
);
115 free_stack(&two
->my_pools
);
117 if (one
== two
&& !one
->my_pools
) {
118 add_pool(&one
->my_pools
, slist1
);
119 add_pool(&one
->my_pools
, slist2
);
122 add_pool(&one
->my_pools
, slist1
);
124 add_pool(&two
->my_pools
, slist2
);
127 add_pool(&one
->all_pools
, slist1
);
128 add_pool(&two
->all_pools
, slist2
);
129 return merge_sm_states(one
, two
);
132 struct sm_state
*__extra_and_merge(struct sm_state
*sm
,
133 struct state_list_stack
*stack
)
135 struct state_list
*slist
;
136 struct sm_state
*ret
= NULL
;
137 struct sm_state
*tmp
;
140 FOR_EACH_PTR(stack
, slist
) {
142 ret
= get_sm_state_slist(slist
, sm
->name
, sm
->owner
,
145 tmp
= get_sm_state_slist(slist
, sm
->name
, sm
->owner
,
147 ret
= merge_sm_states(ret
, tmp
);
149 } END_FOR_EACH_PTR(slist
);
151 smatch_msg("Internal error in __extra_and_merge");
154 ret
->my_pools
= stack
;
155 ret
->all_pools
= clone_stack(stack
);
159 static struct smatch_state
*unmatched_state(struct sm_state
*sm
)
161 return &extra_undefined
;
164 static void match_function_call(struct expression
*expr
)
166 struct expression
*tmp
;
171 FOR_EACH_PTR(expr
->args
, tmp
) {
172 if (tmp
->op
== '&') {
173 name
= get_variable_from_expr(tmp
->unop
, &sym
);
175 set_state(name
, my_id
, sym
, &extra_undefined
);
180 } END_FOR_EACH_PTR(tmp
);
183 static void match_assign(struct expression
*expr
)
185 struct expression
*left
;
189 left
= strip_expr(expr
->left
);
190 name
= get_variable_from_expr(left
, &sym
);
193 set_state(name
, my_id
, sym
, alloc_extra_state(get_value(expr
->right
)));
197 static void undef_expr(struct expression
*expr
)
202 name
= get_variable_from_expr(expr
->unop
, &sym
);
205 if (!get_state(name
, my_id
, sym
)) {
209 set_state(name
, my_id
, sym
, &extra_undefined
);
213 static void match_declarations(struct symbol
*sym
)
218 name
= sym
->ident
->name
;
219 if (sym
->initializer
) {
220 set_state(name
, my_id
, sym
, alloc_extra_state(get_value(sym
->initializer
)));
222 set_state(name
, my_id
, sym
, &extra_undefined
);
227 static void match_function_def(struct symbol
*sym
)
231 FOR_EACH_PTR(sym
->ctype
.base_type
->arguments
, arg
) {
235 set_state(arg
->ident
->name
, my_id
, arg
, &extra_undefined
);
236 } END_FOR_EACH_PTR(arg
);
239 static void match_unop(struct expression
*expr
)
246 name
= get_variable_from_expr(expr
->unop
, &sym
);
250 tmp
= show_special(expr
->op
);
251 if ((!strcmp(tmp
, "--")) || (!strcmp(tmp
, "++")))
252 set_state(name
, my_id
, sym
, &extra_undefined
);
256 int expr_to_val(struct expression
*expr
)
258 struct smatch_state
*state
;
263 val
= get_value(expr
);
264 if (val
!= UNDEFINED
)
267 name
= get_variable_from_expr(expr
, &sym
);
270 state
= get_state(name
, my_id
, sym
);
272 if (!state
|| !state
->data
)
274 return get_single_value((struct data_info
*)state
->data
);
277 int true_comparison(int left
, int comparison
, int right
)
281 case SPECIAL_UNSIGNED_LT
:
285 case SPECIAL_UNSIGNED_LTE
:
293 case SPECIAL_UNSIGNED_GTE
:
298 case SPECIAL_UNSIGNED_GT
:
302 case SPECIAL_NOTEQUAL
:
307 smatch_msg("unhandled comparison %d\n", comparison
);
313 static int do_comparison(struct expression
*expr
)
315 int left
, right
, ret
;
317 if ((left
= expr_to_val(expr
->left
)) == UNDEFINED
)
320 if ((right
= expr_to_val(expr
->right
)) == UNDEFINED
)
323 ret
= true_comparison(left
, expr
->op
, right
);
325 SM_DEBUG("%d known condition: %d %s %d => true\n",
326 get_lineno(), left
, show_special(expr
->op
), right
);
327 } else if (ret
== 0) {
328 SM_DEBUG("%d known condition: %d %s %d => false\n",
329 get_lineno(), left
, show_special(expr
->op
), right
);
334 int last_stmt_val(struct statement
*stmt
)
336 struct expression
*expr
;
338 stmt
= last_ptr_list((struct ptr_list
*)stmt
->stmts
);
339 if (stmt
->type
!= STMT_EXPRESSION
)
341 expr
= stmt
->expression
;
342 return get_value(expr
);
345 /* this is actually hooked from smatch_implied.c... it's hacky, yes */
346 void __extra_match_condition(struct expression
*expr
)
350 struct smatch_state
*pre_state
;
351 struct smatch_state
*true_state
;
352 struct smatch_state
*false_state
;
354 expr
= strip_expr(expr
);
359 name
= get_variable_from_expr(expr
, &sym
);
362 pre_state
= get_state(name
, my_id
, sym
);
363 true_state
= add_filter(pre_state
, 0);
364 false_state
= alloc_extra_state(0);
365 set_true_false_states(name
, my_id
, sym
, true_state
, false_state
);
368 // case EXPR_COMPARE: // later
372 static int variable_non_zero(struct expression
*expr
)
376 struct smatch_state
*state
;
379 name
= get_variable_from_expr(expr
, &sym
);
382 state
= get_state(name
, my_id
, sym
);
383 if (!state
|| !state
->data
)
385 ret
= true_comparison(get_single_value((struct data_info
*)state
->data
),
386 SPECIAL_NOTEQUAL
, 0);
392 int known_condition_true(struct expression
*expr
)
399 tmp
= get_value(expr
);
400 if (tmp
&& tmp
!= UNDEFINED
)
403 expr
= strip_expr(expr
);
406 if (expr
->op
== '!') {
407 if (known_condition_false(expr
->unop
))
418 int known_condition_false(struct expression
*expr
)
428 if (expr
->op
== '!') {
429 if (known_condition_true(expr
->unop
))
440 int implied_condition_true(struct expression
*expr
)
442 struct statement
*stmt
;
448 tmp
= get_value(expr
);
449 if (tmp
&& tmp
!= UNDEFINED
)
452 expr
= strip_expr(expr
);
455 if (do_comparison(expr
) == 1)
459 if (expr
->op
== '!') {
460 if (implied_condition_false(expr
->unop
))
464 stmt
= get_block_thing(expr
);
465 if (stmt
&& (last_stmt_val(stmt
) == 1))
469 if (variable_non_zero(expr
) == 1)
476 int implied_condition_false(struct expression
*expr
)
478 struct statement
*stmt
;
479 struct expression
*tmp
;
489 if (do_comparison(expr
) == 0)
492 if (expr
->op
== '!') {
493 if (implied_condition_true(expr
->unop
))
497 stmt
= get_block_thing(expr
);
498 if (stmt
&& (last_stmt_val(stmt
) == 0))
500 tmp
= strip_expr(expr
);
502 return implied_condition_false(tmp
);
505 if (variable_non_zero(expr
) == 0)
512 void register_smatch_extra(int id
)
515 add_merge_hook(my_id
, &merge_func
);
516 add_unmatched_state_hook(my_id
, &unmatched_state
);
517 add_hook(&undef_expr
, OP_HOOK
);
518 add_hook(&match_function_def
, FUNC_DEF_HOOK
);
519 add_hook(&match_function_call
, FUNCTION_CALL_HOOK
);
520 add_hook(&match_assign
, ASSIGNMENT_HOOK
);
521 add_hook(&match_declarations
, DECLARATION_HOOK
);
522 add_hook(&match_unop
, OP_HOOK
);
523 add_hook(&free_data_info_allocs
, END_FUNC_HOOK
);
526 /* I don't know how to test for the ATTRIB_NORET attribute. :( */
527 add_function_hook("panic", &__match_nullify_path_hook
, NULL
);
528 add_function_hook("do_exit", &__match_nullify_path_hook
, NULL
);
529 add_function_hook("complete_and_exit", &__match_nullify_path_hook
, NULL
);
530 add_function_hook("__module_put_and_exit", &__match_nullify_path_hook
, NULL
);
531 add_function_hook("do_group_exit", &__match_nullify_path_hook
, NULL
);