2 * sparse/smatch_extra.c
4 * Copyright (C) 2008 Dan Carpenter.
6 * Licensed under the Open Software License version 1.1
16 static struct smatch_state
*alloc_state(int val
)
18 struct smatch_state
*state
;
20 state
= malloc(sizeof(*state
));
21 state
->name
= "value";
22 state
->data
= malloc(sizeof(int));
23 *(int *)state
->data
= val
;
27 static struct smatch_state
*merge_func(const char *name
, struct symbol
*sym
,
28 struct smatch_state
*s1
,
29 struct smatch_state
*s2
)
34 static void match_function_call_after(struct expression
*expr
)
36 struct expression
*tmp
;
41 FOR_EACH_PTR(expr
->args
, tmp
) {
43 name
= get_variable_from_expr_simple(tmp
->unop
, &sym
);
45 name
= alloc_string(name
);
46 set_state(name
, my_id
, sym
, &undefined
);
50 } END_FOR_EACH_PTR(tmp
);
53 static void match_assign(struct expression
*expr
)
58 name
= get_variable_from_expr_simple(expr
->left
, &sym
);
61 name
= alloc_string(name
);
62 set_state(name
, my_id
, sym
, alloc_state(get_value(expr
->right
, NULL
)));
65 static void undef_expr(struct expression
*expr
)
70 name
= get_variable_from_expr_simple(expr
->unop
, &sym
);
73 if (!get_state(name
, my_id
, sym
))
75 name
= alloc_string(name
);
76 set_state(name
, my_id
, sym
, &undefined
);
79 static void match_declarations(struct symbol
*sym
)
84 name
= sym
->ident
->name
;
85 if (sym
->initializer
) {
86 set_state(name
, my_id
, sym
, alloc_state(get_value(sym
->initializer
, NULL
)));
91 void register_smatch_extra(int id
)
94 add_merge_hook(my_id
, &merge_func
);
95 add_hook(&undef_expr
, OP_HOOK
);
96 add_hook(&match_function_call_after
, FUNCTION_CALL_AFTER_HOOK
);
97 add_hook(&match_assign
, ASSIGNMENT_AFTER_HOOK
);
98 add_hook(&match_declarations
, DECLARATION_HOOK
);
101 static int expr_to_val(struct expression
*expr
)
103 struct smatch_state
*state
;
108 val
= get_value(expr
, NULL
);
109 if (val
!= UNDEFINED
)
112 name
= get_variable_from_expr_simple(expr
, &sym
);
115 state
= get_state(name
, my_id
, sym
);
116 if (!state
|| !state
->data
)
118 return *(int *)state
->data
;
121 static int true_comparison(int left
, int comparison
, int right
)
125 case SPECIAL_UNSIGNED_LT
:
129 case SPECIAL_UNSIGNED_LTE
:
137 case SPECIAL_UNSIGNED_GTE
:
142 case SPECIAL_UNSIGNED_GT
:
146 case SPECIAL_NOTEQUAL
:
151 smatch_msg("unhandled comparison %d\n", comparison
);
156 int known_condition_true(struct expression
*expr
)
158 int left
, right
, ret
;
160 if (!expr
|| expr
->type
!= EXPR_COMPARE
)
163 if ((left
= expr_to_val(expr
->left
)) == UNDEFINED
)
166 if ((right
= expr_to_val(expr
->right
)) == UNDEFINED
)
169 ret
= true_comparison(left
, expr
->op
, right
);
170 smatch_msg("known condition: %d & %d => %s", left
, right
, (ret
?"true":"false"));