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 get to 'b', 'c' and 'd'.
27 * 'b' is true when we reach 'c' but otherwise we don't know.
29 * The other thing that complicates matters is if we negate
32 * We pass the un-negated version to the client and flip the true
33 * and false values internally.
35 * And negations can be part of a compound.
36 * if (a && !(b || c)) { d;
37 * In that situation we multiply the negative through to simplify
38 * stuff so that we can remove the parens like this:
39 * if (a && !b && !c) { d;
41 * One other thing is that:
43 * that's basically the same as testing for just 'a' so we simplify
44 * it before passing it to the script.
51 static void split_conditions(struct expression
*expr
);
53 static int is_logical_and(struct expression
*expr
)
55 if (expr
->op
== SPECIAL_LOGICAL_AND
)
60 static int handle_zero_comparisons(struct expression
*expr
)
62 struct expression
*tmp
= NULL
;
64 // if left is zero or right is zero
65 if (is_zero(expr
->left
))
67 else if (is_zero(expr
->right
))
72 // "if (foo != 0)" is the same as "if (foo)"
73 if (expr
->op
== SPECIAL_NOTEQUAL
) {
74 split_conditions(tmp
);
78 // "if (foo == 0)" is the same as "if (!foo)"
79 if (expr
->op
== SPECIAL_EQUAL
) {
80 split_conditions(tmp
);
81 __negate_cond_stacks();
89 * This function is for handling calls to likely/unlikely
92 static int ignore_builtin_expect(struct expression
*expr
)
94 if (sym_name_is("__builtin_expect", expr
->fn
)) {
95 split_conditions(first_ptr_list((struct ptr_list
*) expr
->args
));
98 if (sym_name_is("__builtin_constant_p", expr
->fn
)) {
99 split_conditions(first_ptr_list((struct ptr_list
*) expr
->args
));
106 * handle_compound_stmt() is for: foo = ({blah; blah; blah; 1})
109 static void handle_compound_stmt(struct statement
*stmt
)
111 struct expression
*expr
= NULL
;
112 struct statement
*last
;
115 last
= last_ptr_list((struct ptr_list
*)stmt
->stmts
);
116 if (last
->type
!= STMT_EXPRESSION
) {
119 expr
= last
->expression
;
121 FOR_EACH_PTR(stmt
->stmts
, s
) {
123 __split_statements(s
);
124 } END_FOR_EACH_PTR(s
);
125 split_conditions(expr
);
129 static int handle_preop(struct expression
*expr
)
131 struct statement
*stmt
;
133 if (expr
->op
== '!') {
134 split_conditions(expr
->unop
);
135 __negate_cond_stacks();
138 stmt
= get_block_thing(expr
);
140 handle_compound_stmt(stmt
);
146 static void handle_logical(struct expression
*expr
)
149 * If we come to an "and" expr then:
150 * We split the left side.
151 * We keep all the current states.
152 * We split the right side.
153 * We keep all the states from both true sides.
155 * If it's an "or" expr then:
156 * We save the current slist.
157 * We split the left side.
158 * We use the false states for the right side.
159 * We split the right side.
160 * We save all the states that are the same on both sides.
163 split_conditions(expr
->left
);
165 if (!is_logical_and(expr
))
166 __use_cond_false_states();
168 __save_pre_cond_states();
169 __push_cond_stacks();
171 split_conditions(expr
->right
);
173 if (is_logical_and(expr
)) {
179 __pop_pre_cond_states();
180 __use_cond_true_states();
183 static int handle_rostedt_if(struct expression
*expr
)
185 if (expr
->type
!= EXPR_CALL
)
187 if (!sym_name_is("__builtin_constant_p", expr
->fn
))
189 split_conditions(expr
);
193 static void handle_select(struct expression
*expr
)
196 * if ((aaa()?bbb():ccc())) { ...
198 * This is almost the same as:
199 * if ((aaa() && bbb()) || (!aaa() && ccc())) { ...
201 * It's a bit complicated because we shouldn't pass aaa()
202 * to the clients more than once.
205 if (handle_rostedt_if(expr
->conditional
))
208 split_conditions(expr
->conditional
);
210 __save_false_states_for_later();
212 if (implied_condition_true(expr
->cond_true
)) {
213 __split_expr(expr
->cond_true
);
215 __push_cond_stacks();
216 split_conditions(expr
->cond_true
);
220 if (implied_condition_false(expr
->cond_false
)) {
221 __split_expr(expr
->cond_false
);
222 __pop_pre_cond_states();
226 __use_previously_stored_false_states();
228 __save_pre_cond_states();
229 __push_cond_stacks();
230 split_conditions(expr
->cond_false
);
232 __pop_pre_cond_states();
234 __use_cond_true_states();
237 static void split_conditions(struct expression
*expr
)
240 sm_debug("%d in split_conditions type=%d\n", get_lineno(), expr
->type
);
242 expr
= strip_expr(expr
);
248 handle_logical(expr
);
251 if (handle_zero_comparisons(expr
))
255 if (ignore_builtin_expect(expr
))
259 if (handle_preop(expr
))
262 case EXPR_CONDITIONAL
:
269 __pass_to_client(expr
, CONDITION_HOOK
);
272 static int inside_condition
;
273 void __split_whole_condition(struct expression
*expr
)
275 sm_debug("%d in __split_whole_condition\n", get_lineno());
277 __save_pre_cond_states();
278 __push_cond_stacks();
280 split_conditions(expr
);
282 __pass_to_client(expr
, WHOLE_CONDITION_HOOK
);
288 return inside_condition
;