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"
51 #include "smatch_extra.h"
52 #include "smatch_expression_stacks.h"
54 static void split_conditions(struct expression
*expr
);
56 static int is_logical_and(struct expression
*expr
)
58 if (expr
->op
== SPECIAL_LOGICAL_AND
)
63 static int handle_zero_comparisons(struct expression
*expr
)
65 struct expression
*tmp
= NULL
;
67 // if left is zero or right is zero
68 if (is_zero(expr
->left
))
70 else if (is_zero(expr
->right
))
75 // "if (foo != 0)" is the same as "if (foo)"
76 if (expr
->op
== SPECIAL_NOTEQUAL
) {
77 split_conditions(tmp
);
81 // "if (foo == 0)" is the same as "if (!foo)"
82 if (expr
->op
== SPECIAL_EQUAL
) {
83 split_conditions(tmp
);
84 __negate_cond_stacks();
92 * This function is for handling calls to likely/unlikely
95 static int ignore_builtin_expect(struct expression
*expr
)
97 if (sym_name_is("__builtin_expect", expr
->fn
)) {
98 split_conditions(first_ptr_list((struct ptr_list
*) expr
->args
));
101 if (sym_name_is("__builtin_constant_p", expr
->fn
)) {
102 split_conditions(first_ptr_list((struct ptr_list
*) expr
->args
));
109 * handle_compound_stmt() is for: foo = ({blah; blah; blah; 1})
112 static void handle_compound_stmt(struct statement
*stmt
)
114 struct expression
*expr
= NULL
;
115 struct statement
*last
;
118 last
= last_ptr_list((struct ptr_list
*)stmt
->stmts
);
119 if (last
->type
!= STMT_EXPRESSION
) {
122 expr
= last
->expression
;
124 FOR_EACH_PTR(stmt
->stmts
, s
) {
126 __split_statements(s
);
127 } END_FOR_EACH_PTR(s
);
128 split_conditions(expr
);
132 static int handle_preop(struct expression
*expr
)
134 struct statement
*stmt
;
136 if (expr
->op
== '!') {
137 split_conditions(expr
->unop
);
138 __negate_cond_stacks();
141 stmt
= get_block_thing(expr
);
143 handle_compound_stmt(stmt
);
149 static void handle_logical(struct expression
*expr
)
152 * If we come to an "and" expr then:
153 * We split the left side.
154 * We keep all the current states.
155 * We split the right side.
156 * We keep all the states from both true sides.
158 * If it's an "or" expr then:
159 * We save the current slist.
160 * We split the left side.
161 * We use the false states for the right side.
162 * We split the right side.
163 * We save all the states that are the same on both sides.
166 split_conditions(expr
->left
);
168 if (!is_logical_and(expr
))
169 __use_cond_false_states();
171 __push_cond_stacks();
173 __save_pre_cond_states();
174 split_conditions(expr
->right
);
175 __pop_pre_cond_states();
177 if (is_logical_and(expr
)) {
182 __use_cond_true_states();
185 static void move_cur_to_tf(int tf
)
189 FOR_EACH_PTR(__fake_cur_slist
, sm
) {
191 __set_true_false_sm(sm
, NULL
);
193 __set_true_false_sm(NULL
, sm
);
194 } END_FOR_EACH_PTR(sm
);
195 free_slist(&__fake_cur_slist
);
201 * if ((aaa()?bbb():ccc())) { ...
203 * This is almost the same as:
204 * if ((aaa() && bbb()) || (!aaa() && ccc())) { ...
206 * It's a bit complicated because we shouldn't pass aaa()
207 * to the clients more than once.
210 static void handle_select(struct expression
*expr
)
212 split_conditions(expr
->conditional
);
214 __save_false_states_for_later();
217 if (implied_condition_true(expr
->cond_true
)) {
218 __split_expr(expr
->cond_true
);
220 __push_cond_stacks();
221 split_conditions(expr
->cond_true
);
224 move_cur_to_tf(TRUE
);
226 if (implied_condition_false(expr
->cond_false
)) {
227 __split_expr(expr
->cond_false
);
228 __pop_pre_cond_states();
229 move_cur_to_tf(FALSE
);
234 __use_previously_stored_false_states();
236 __save_pre_cond_states();
237 __push_cond_stacks();
238 split_conditions(expr
->cond_false
);
239 move_cur_to_tf(FALSE
);
242 __pop_pre_cond_states();
244 __use_cond_true_states();
247 static void split_conditions(struct expression
*expr
)
250 sm_debug("%d in split_conditions type=%d\n", get_lineno(), expr
->type
);
252 expr
= strip_expr(expr
);
256 switch (expr
->type
) {
258 handle_logical(expr
);
261 if (handle_zero_comparisons(expr
))
265 if (ignore_builtin_expect(expr
))
269 if (handle_preop(expr
))
272 case EXPR_CONDITIONAL
:
278 /* fixme: this should be in smatch_flow.c
279 but because of the funny stuff we do with conditions
280 it's awkward to put it there. We would need to
281 call CONDITION_HOOK in smatch_flow as well.
283 push_expression(&big_expression_stack
, expr
);
284 if (expr
->type
== EXPR_COMPARE
) {
285 if (expr
->left
->type
!= EXPR_POSTOP
)
286 __split_expr(expr
->left
);
287 if (expr
->right
->type
!= EXPR_POSTOP
)
288 __split_expr(expr
->right
);
292 __pass_to_client(expr
, CONDITION_HOOK
);
293 if (expr
->type
== EXPR_COMPARE
) {
294 if (expr
->left
->type
== EXPR_POSTOP
)
295 __split_expr(expr
->left
);
296 if (expr
->right
->type
== EXPR_POSTOP
)
297 __split_expr(expr
->right
);
299 pop_expression(&big_expression_stack
);
302 static int inside_condition
;
303 void __split_whole_condition(struct expression
*expr
)
305 sm_debug("%d in __split_whole_condition\n", get_lineno());
307 __save_pre_cond_states();
308 __push_cond_stacks();
309 /* it's a hack, but it's sometimes handy to have this stuff
310 on the big_expression_stack. */
311 push_expression(&big_expression_stack
, expr
);
313 split_conditions(expr
);
315 __pass_to_client(expr
, WHOLE_CONDITION_HOOK
);
316 pop_expression(&big_expression_stack
);
320 int __handle_condition_assigns(struct expression
*expr
)
322 struct expression
*right
;
324 right
= strip_expr(expr
->right
);
325 if (right
->type
!= EXPR_PREOP
)
327 if (right
->op
!= '!')
329 sm_debug("%d in __handle_condition_assigns\n", get_lineno());
331 __save_pre_cond_states();
332 __push_cond_stacks();
333 /* it's a hack, but it's sometimes handy to have this stuff
334 on the big_expression_stack. */
335 push_expression(&big_expression_stack
, right
);
336 split_conditions(right
);
337 set_true_false_states_expr(SMATCH_EXTRA
, expr
->left
, alloc_extra_state(1), alloc_extra_state(0));
339 __pass_to_client(right
, WHOLE_CONDITION_HOOK
);
340 pop_expression(&big_expression_stack
);
343 __push_true_states();
344 __use_false_states();
345 __merge_true_states();
349 int in_condition(void)
351 return inside_condition
;