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 static 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_string(name
);
52 static struct smatch_state
*merge_func(const char *name
, struct symbol
*sym
,
53 struct smatch_state
*s1
,
54 struct smatch_state
*s2
)
56 struct data_info
*info1
= (struct data_info
*)s1
->data
;
57 struct data_info
*info2
= (struct data_info
*)s2
->data
;
58 struct smatch_state
*tmp
;
60 tmp
= alloc_extra_state_no_name(UNDEFINED
);
61 tmp
->name
= "extra_merged";
62 ((struct data_info
*)tmp
->data
)->merged
= 1;
63 ((struct data_info
*)tmp
->data
)->values
=
64 num_list_union(info1
->values
, info2
->values
);
68 struct sm_state
*__extra_merge(struct sm_state
*one
, struct state_list
*slist1
,
69 struct sm_state
*two
, struct state_list
*slist2
)
71 struct data_info
*info1
;
72 struct data_info
*info2
;
74 if (!one
->state
->data
|| !two
->state
->data
) {
75 smatch_msg("internal error in smatch extra '%s = %s or %s'",
76 one
->name
, show_state(one
->state
),
77 show_state(two
->state
));
78 return alloc_state(one
->name
, one
->owner
, one
->sym
,
82 info1
= (struct data_info
*)one
->state
->data
;
83 info2
= (struct data_info
*)two
->state
->data
;
86 free_stack(&one
->my_pools
);
88 free_stack(&two
->my_pools
);
90 if (one
== two
&& !one
->my_pools
) {
91 add_pool(&one
->my_pools
, slist1
);
92 add_pool(&one
->my_pools
, slist2
);
95 add_pool(&one
->my_pools
, slist1
);
97 add_pool(&two
->my_pools
, slist2
);
100 add_pool(&one
->all_pools
, slist1
);
101 add_pool(&two
->all_pools
, slist2
);
102 return merge_sm_states(one
, two
);
105 static struct smatch_state
*unmatched_state(struct sm_state
*sm
)
107 return &extra_undefined
;
110 static void match_function_call(struct expression
*expr
)
112 struct expression
*tmp
;
117 FOR_EACH_PTR(expr
->args
, tmp
) {
118 if (tmp
->op
== '&') {
119 name
= get_variable_from_expr(tmp
->unop
, &sym
);
121 set_state(name
, my_id
, sym
, &extra_undefined
);
126 } END_FOR_EACH_PTR(tmp
);
129 static void match_assign(struct expression
*expr
)
131 struct expression
*left
;
135 left
= strip_expr(expr
->left
);
136 name
= get_variable_from_expr(left
, &sym
);
139 set_state(name
, my_id
, sym
, alloc_extra_state(get_value(expr
->right
)));
143 static void undef_expr(struct expression
*expr
)
148 name
= get_variable_from_expr(expr
->unop
, &sym
);
151 if (!get_state(name
, my_id
, sym
)) {
155 set_state(name
, my_id
, sym
, &extra_undefined
);
159 static void match_declarations(struct symbol
*sym
)
164 name
= sym
->ident
->name
;
165 if (sym
->initializer
) {
166 set_state(name
, my_id
, sym
, alloc_extra_state(get_value(sym
->initializer
)));
168 set_state(name
, my_id
, sym
, &extra_undefined
);
173 static void match_function_def(struct symbol
*sym
)
177 FOR_EACH_PTR(sym
->ctype
.base_type
->arguments
, arg
) {
181 set_state(arg
->ident
->name
, my_id
, arg
, &extra_undefined
);
182 } END_FOR_EACH_PTR(arg
);
185 static void match_unop(struct expression
*expr
)
192 name
= get_variable_from_expr(expr
->unop
, &sym
);
196 tmp
= show_special(expr
->op
);
197 if ((!strcmp(tmp
, "--")) || (!strcmp(tmp
, "++")))
198 set_state(name
, my_id
, sym
, &extra_undefined
);
202 static int expr_to_val(struct expression
*expr
)
204 struct smatch_state
*state
;
209 val
= get_value(expr
);
210 if (val
!= UNDEFINED
)
213 name
= get_variable_from_expr(expr
, &sym
);
216 state
= get_state(name
, my_id
, sym
);
218 if (!state
|| !state
->data
)
220 return get_single_value((struct data_info
*)state
->data
);
223 int true_comparison(int left
, int comparison
, int right
)
227 case SPECIAL_UNSIGNED_LT
:
231 case SPECIAL_UNSIGNED_LTE
:
239 case SPECIAL_UNSIGNED_GTE
:
244 case SPECIAL_UNSIGNED_GT
:
248 case SPECIAL_NOTEQUAL
:
253 smatch_msg("unhandled comparison %d\n", comparison
);
259 static int do_comparison(struct expression
*expr
)
261 int left
, right
, ret
;
263 if ((left
= expr_to_val(expr
->left
)) == UNDEFINED
)
266 if ((right
= expr_to_val(expr
->right
)) == UNDEFINED
)
269 ret
= true_comparison(left
, expr
->op
, right
);
271 SM_DEBUG("%d known condition: %d %s %d => true\n",
272 get_lineno(), left
, show_special(expr
->op
), right
);
273 } else if (ret
== 0) {
274 SM_DEBUG("%d known condition: %d %s %d => false\n",
275 get_lineno(), left
, show_special(expr
->op
), right
);
280 int last_stmt_val(struct statement
*stmt
)
282 struct expression
*expr
;
284 stmt
= last_ptr_list((struct ptr_list
*)stmt
->stmts
);
285 if (stmt
->type
!= STMT_EXPRESSION
)
287 expr
= stmt
->expression
;
288 return get_value(expr
);
291 static int variable_non_zero(struct expression
*expr
)
295 struct smatch_state
*state
;
298 name
= get_variable_from_expr(expr
, &sym
);
301 state
= get_state(name
, my_id
, sym
);
302 if (!state
|| !state
->data
)
304 ret
= true_comparison(get_single_value((struct data_info
*)state
->data
),
305 SPECIAL_NOTEQUAL
, 0);
311 int known_condition_true(struct expression
*expr
)
313 struct statement
*stmt
;
319 tmp
= get_value(expr
);
320 if (tmp
&& tmp
!= UNDEFINED
)
323 expr
= strip_expr(expr
);
326 if (do_comparison(expr
) == 1)
330 if (expr
->op
== '!') {
331 if (known_condition_false(expr
->unop
))
335 stmt
= get_block_thing(expr
);
336 if (stmt
&& (last_stmt_val(stmt
) == 1))
340 if (variable_non_zero(expr
) == 1)
347 int known_condition_false(struct expression
*expr
)
349 struct statement
*stmt
;
350 struct expression
*tmp
;
360 if (do_comparison(expr
) == 0)
363 if (expr
->op
== '!') {
364 if (known_condition_true(expr
->unop
))
368 stmt
= get_block_thing(expr
);
369 if (stmt
&& (last_stmt_val(stmt
) == 0))
371 tmp
= strip_expr(expr
);
373 return known_condition_false(tmp
);
376 if (variable_non_zero(expr
) == 0)
383 void register_smatch_extra(int id
)
386 add_merge_hook(my_id
, &merge_func
);
387 add_unmatched_state_hook(my_id
, &unmatched_state
);
388 add_hook(&undef_expr
, OP_HOOK
);
389 add_hook(&match_function_def
, FUNC_DEF_HOOK
);
390 add_hook(&match_function_call
, FUNCTION_CALL_HOOK
);
391 add_hook(&match_assign
, ASSIGNMENT_HOOK
);
392 add_hook(&match_declarations
, DECLARATION_HOOK
);
393 add_hook(&match_unop
, OP_HOOK
);
395 /* I don't know how to test for the ATTRIB_NORET attribute. :( */
396 add_function_hook("panic", &__match_nullify_path_hook
, NULL
);
397 add_function_hook("do_exit", &__match_nullify_path_hook
, NULL
);
398 add_function_hook("complete_and_exit", &__match_nullify_path_hook
, NULL
);
399 add_function_hook("do_group_exit", &__match_nullify_path_hook
, NULL
);