2 * sparse/smatch_conditions.c
4 * Copyright (C) 2006,2008 Dan Carpenter.
6 * Licensed under the Open Software License version 1.1
11 * The simplest type of condition is
14 * The next simplest kind of conditions is
16 * In that case 'a' is true when we get to 'b' and both are true
19 * Or's are a little more complicated.
21 * We know 'a' is not true when we get to 'b' but it may be true
24 * If we mix and's and or's that's even more complicated.
25 * if (a && b && c || a && d) { d ;
26 * 'a' is true when we evaluate 'b', and 'd'.
27 * 'b' is true when we evaluate 'c' but otherwise we don't.
29 * The other thing that complicates matters is if we negate
32 * Smatch has passes the un-negated version to the client and flip
33 * the true and false values internally. This makes it easier
36 * And negations can be part of a compound.
37 * if (a && !(b || c)) { d;
38 * In that situation we multiply the negative through to simplify
39 * stuff so that we can remove the parens like this:
40 * if (a && !b && !c) { d;
42 * One other thing is that:
44 * that's basically the same as testing for just 'a' and we simplify
45 * comparisons with zero before passing it to the script.
50 #include "smatch_slist.h"
52 static void split_conditions(struct expression
*expr
);
54 static int is_logical_and(struct expression
*expr
)
56 if (expr
->op
== SPECIAL_LOGICAL_AND
)
61 static int handle_zero_comparisons(struct expression
*expr
)
63 struct expression
*tmp
= NULL
;
65 // if left is zero or right is zero
66 if (is_zero(expr
->left
))
68 else if (is_zero(expr
->right
))
73 // "if (foo != 0)" is the same as "if (foo)"
74 if (expr
->op
== SPECIAL_NOTEQUAL
) {
75 split_conditions(tmp
);
79 // "if (foo == 0)" is the same as "if (!foo)"
80 if (expr
->op
== SPECIAL_EQUAL
) {
81 split_conditions(tmp
);
82 __negate_cond_stacks();
90 * This function is for handling calls to likely/unlikely
93 static int ignore_builtin_expect(struct expression
*expr
)
95 if (sym_name_is("__builtin_expect", expr
->fn
)) {
96 split_conditions(first_ptr_list((struct ptr_list
*) expr
->args
));
99 if (sym_name_is("__builtin_constant_p", expr
->fn
)) {
100 split_conditions(first_ptr_list((struct ptr_list
*) expr
->args
));
107 * handle_compound_stmt() is for: foo = ({blah; blah; blah; 1})
110 static void handle_compound_stmt(struct statement
*stmt
)
112 struct expression
*expr
= NULL
;
113 struct statement
*last
;
116 last
= last_ptr_list((struct ptr_list
*)stmt
->stmts
);
117 if (last
->type
!= STMT_EXPRESSION
) {
120 expr
= last
->expression
;
122 FOR_EACH_PTR(stmt
->stmts
, s
) {
124 __split_statements(s
);
125 } END_FOR_EACH_PTR(s
);
126 split_conditions(expr
);
130 static int handle_preop(struct expression
*expr
)
132 struct statement
*stmt
;
134 if (expr
->op
== '!') {
135 split_conditions(expr
->unop
);
136 __negate_cond_stacks();
139 stmt
= get_block_thing(expr
);
141 handle_compound_stmt(stmt
);
147 static void handle_logical(struct expression
*expr
)
150 * If we come to an "and" expr then:
151 * We split the left side.
152 * We keep all the current states.
153 * We split the right side.
154 * We keep all the states from both true sides.
156 * If it's an "or" expr then:
157 * We save the current slist.
158 * We split the left side.
159 * We use the false states for the right side.
160 * We split the right side.
161 * We save all the states that are the same on both sides.
164 split_conditions(expr
->left
);
166 if (!is_logical_and(expr
))
167 __use_cond_false_states();
169 __push_cond_stacks();
171 __save_pre_cond_states();
172 split_conditions(expr
->right
);
173 __pop_pre_cond_states();
175 if (is_logical_and(expr
)) {
180 __use_cond_true_states();
183 static void move_cur_to_tf(int tf
)
187 FOR_EACH_PTR(__fake_cur_slist
, sm
) {
189 __set_true_false_sm(sm
, NULL
);
191 __set_true_false_sm(NULL
, sm
);
192 } END_FOR_EACH_PTR(sm
);
193 free_slist(&__fake_cur_slist
);
199 * if ((aaa()?bbb():ccc())) { ...
201 * This is almost the same as:
202 * if ((aaa() && bbb()) || (!aaa() && ccc())) { ...
204 * It's a bit complicated because we shouldn't pass aaa()
205 * to the clients more than once.
208 static void handle_select(struct expression
*expr
)
210 split_conditions(expr
->conditional
);
212 __save_false_states_for_later();
215 if (implied_condition_true(expr
->cond_true
)) {
216 __split_expr(expr
->cond_true
);
218 __push_cond_stacks();
219 split_conditions(expr
->cond_true
);
222 move_cur_to_tf(TRUE
);
224 if (implied_condition_false(expr
->cond_false
)) {
225 __split_expr(expr
->cond_false
);
226 __pop_pre_cond_states();
227 move_cur_to_tf(FALSE
);
232 __use_previously_stored_false_states();
234 __save_pre_cond_states();
235 __push_cond_stacks();
236 split_conditions(expr
->cond_false
);
237 move_cur_to_tf(FALSE
);
240 __pop_pre_cond_states();
242 __use_cond_true_states();
245 static void split_conditions(struct expression
*expr
)
248 sm_debug("%d in split_conditions type=%d\n", get_lineno(), expr
->type
);
250 expr
= strip_expr(expr
);
254 switch (expr
->type
) {
256 handle_logical(expr
);
259 if (handle_zero_comparisons(expr
))
263 if (ignore_builtin_expect(expr
))
267 if (handle_preop(expr
))
270 case EXPR_CONDITIONAL
:
276 /* fixme: this should be in smatch_flow.c
277 but because of the funny stuff we do with conditions
278 it's awkward to put it there. We would need to
279 call CONDITION_HOOK in smatch_flow as well.
281 if (expr
->type
== EXPR_COMPARE
) {
282 if (expr
->left
->type
!= EXPR_POSTOP
)
283 __split_expr(expr
->left
);
284 if (expr
->right
->type
!= EXPR_POSTOP
)
285 __split_expr(expr
->right
);
289 __pass_to_client(expr
, CONDITION_HOOK
);
290 if (expr
->type
== EXPR_COMPARE
) {
291 if (expr
->left
->type
== EXPR_POSTOP
)
292 __split_expr(expr
->left
);
293 if (expr
->right
->type
== EXPR_POSTOP
)
294 __split_expr(expr
->right
);
298 static int inside_condition
;
299 void __split_whole_condition(struct expression
*expr
)
301 sm_debug("%d in __split_whole_condition\n", get_lineno());
303 __save_pre_cond_states();
304 __push_cond_stacks();
306 split_conditions(expr
);
308 __pass_to_client(expr
, WHOLE_CONDITION_HOOK
);
312 int in_condition(void)
314 return inside_condition
;