2 * Copyright (C) 2006,2008 Dan Carpenter.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, see http://www.gnu.org/copyleft/gpl.txt
19 * The simplest type of condition is
22 * The next simplest kind of conditions is
24 * In that case 'a' is true when we get to 'b' and both are true
27 * Or's are a little more complicated.
29 * We know 'a' is not true when we get to 'b' but it may be true
32 * If we mix and's and or's that's even more complicated.
33 * if (a && b && c || a && d) { d ;
34 * 'a' is true when we evaluate 'b', and 'd'.
35 * 'b' is true when we evaluate 'c' but otherwise we don't.
37 * The other thing that complicates matters is if we negate
40 * Smatch has passes the un-negated version to the client and flip
41 * the true and false values internally. This makes it easier
44 * And negations can be part of a compound.
45 * if (a && !(b || c)) { d;
46 * In that situation we multiply the negative through to simplify
47 * stuff so that we can remove the parens like this:
48 * if (a && !b && !c) { d;
50 * One other thing is that:
52 * that's basically the same as testing for just 'a' and we simplify
53 * comparisons with zero before passing it to the script.
58 #include "smatch_slist.h"
59 #include "smatch_extra.h"
60 #include "smatch_expression_stacks.h"
62 extern int __expr_stmt_count
;
64 static void split_conditions(struct expression
*expr
);
66 static int is_logical_and(struct expression
*expr
)
68 if (expr
->op
== SPECIAL_LOGICAL_AND
)
73 static int handle_zero_comparisons(struct expression
*expr
)
75 struct expression
*tmp
= NULL
;
77 // if left is zero or right is zero
78 if (is_zero(expr
->left
))
80 else if (is_zero(expr
->right
))
85 // "if (foo != 0)" is the same as "if (foo)"
86 if (expr
->op
== SPECIAL_NOTEQUAL
) {
87 split_conditions(tmp
);
91 // "if (foo == 0)" is the same as "if (!foo)"
92 if (expr
->op
== SPECIAL_EQUAL
) {
93 split_conditions(tmp
);
94 __negate_cond_stacks();
102 * This function is for handling calls to likely/unlikely
105 static int ignore_builtin_expect(struct expression
*expr
)
107 if (sym_name_is("__builtin_expect", expr
->fn
)) {
108 split_conditions(first_ptr_list((struct ptr_list
*) expr
->args
));
115 * handle_compound_stmt() is for: foo = ({blah; blah; blah; 1})
118 static void handle_compound_stmt(struct statement
*stmt
)
120 struct expression
*expr
= NULL
;
121 struct statement
*last
;
124 last
= last_ptr_list((struct ptr_list
*)stmt
->stmts
);
125 if (last
->type
!= STMT_EXPRESSION
)
128 expr
= last
->expression
;
130 FOR_EACH_PTR(stmt
->stmts
, s
) {
133 } END_FOR_EACH_PTR(s
);
134 split_conditions(expr
);
138 static int handle_preop(struct expression
*expr
)
140 struct statement
*stmt
;
142 if (expr
->op
== '!') {
143 split_conditions(expr
->unop
);
144 __negate_cond_stacks();
147 stmt
= get_expression_statement(expr
);
149 handle_compound_stmt(stmt
);
155 static void handle_logical(struct expression
*expr
)
158 * If we come to an "and" expr then:
159 * We split the left side.
160 * We keep all the current states.
161 * We split the right side.
162 * We keep all the states from both true sides.
164 * If it's an "or" expr then:
165 * We save the current slist.
166 * We split the left side.
167 * We use the false states for the right side.
168 * We split the right side.
169 * We save all the states that are the same on both sides.
172 split_conditions(expr
->left
);
173 __process_post_op_stack();
175 if (!is_logical_and(expr
))
176 __use_cond_false_states();
178 __push_cond_stacks();
180 __save_pre_cond_states();
181 split_conditions(expr
->right
);
182 __process_post_op_stack();
183 __discard_pre_cond_states();
185 if (is_logical_and(expr
))
190 __use_cond_true_states();
193 static struct stree
*combine_strees(struct stree
*orig
, struct stree
*fake
, struct stree
*new)
195 struct stree
*ret
= NULL
;
197 overwrite_stree(orig
, &ret
);
198 overwrite_stree(fake
, &ret
);
199 overwrite_stree(new, &ret
);
207 * if ((aaa()?bbb():ccc())) { ...
209 * This is almost the same as:
210 * if ((aaa() && bbb()) || (!aaa() && ccc())) { ...
212 * It's a bit complicated because we shouldn't pass aaa()
213 * to the clients more than once.
216 static void handle_select(struct expression
*expr
)
218 struct stree
*a_T
= NULL
;
219 struct stree
*a_F
= NULL
;
220 struct stree
*a_T_b_T
= NULL
;
221 struct stree
*a_T_b_F
= NULL
;
222 struct stree
*a_T_b_fake
= NULL
;
223 struct stree
*a_F_c_T
= NULL
;
224 struct stree
*a_F_c_F
= NULL
;
225 struct stree
*a_F_c_fake
= NULL
;
230 * Imagine we have this: if (a ? b : c) { ...
232 * The condition is true if "a" is true and "b" is true or
233 * "a" is false and "c" is true. It's false if "a" is true
234 * and "b" is false or "a" is false and "c" is false.
236 * The variable name "a_T_b_T" stands for "a true b true" etc.
238 * But if we know "b" is true then we can simpilify things.
239 * The condition is true if "a" is true or if "a" is false and
240 * "c" is true. The only way the condition can be false is if
241 * "a" is false and "c" is false.
243 * The remaining thing is the "a_T_b_fake". When we simplify
244 * the equations we have to take into consideration that other
245 * states may have changed that don't play into the true false
246 * equation. Take the following example:
248 * (flags) = __raw_local_irq_save();
249 * _spin_trylock(lock) ? 1 :
250 * ({ raw_local_irq_restore(flags); 0; });
252 * Smatch has to record that the irq flags were restored on the
257 __save_pre_cond_states();
259 split_conditions(expr
->conditional
);
261 a_T
= __copy_cond_true_states();
262 a_F
= __copy_cond_false_states();
264 __push_cond_stacks();
265 __push_fake_cur_stree();
266 split_conditions(expr
->cond_true
);
267 __process_post_op_stack();
268 a_T_b_fake
= __pop_fake_cur_stree();
269 a_T_b_T
= combine_strees(a_T
, a_T_b_fake
, __pop_cond_true_stack());
270 a_T_b_F
= combine_strees(a_T
, a_T_b_fake
, __pop_cond_false_stack());
272 __use_cond_false_states();
274 __push_cond_stacks();
275 __push_fake_cur_stree();
276 split_conditions(expr
->cond_false
);
277 a_F_c_fake
= __pop_fake_cur_stree();
278 a_F_c_T
= combine_strees(a_F
, a_F_c_fake
, __pop_cond_true_stack());
279 a_F_c_F
= combine_strees(a_F
, a_F_c_fake
, __pop_cond_false_stack());
281 /* We have to restore the pre condition states so that
282 implied_condition_true() will use the right cur_stree */
283 __use_pre_cond_states();
285 if (implied_condition_true(expr
->cond_true
)) {
286 free_stree(&a_T_b_T
);
287 free_stree(&a_T_b_F
);
288 a_T_b_T
= clone_stree(a_T
);
289 overwrite_stree(a_T_b_fake
, &a_T_b_T
);
291 if (implied_condition_false(expr
->cond_true
)) {
292 free_stree(&a_T_b_T
);
293 free_stree(&a_T_b_F
);
294 a_T_b_F
= clone_stree(a_T
);
295 overwrite_stree(a_T_b_fake
, &a_T_b_F
);
297 if (implied_condition_true(expr
->cond_false
)) {
298 free_stree(&a_F_c_T
);
299 free_stree(&a_F_c_F
);
300 a_F_c_T
= clone_stree(a_F
);
301 overwrite_stree(a_F_c_fake
, &a_F_c_T
);
303 if (implied_condition_false(expr
->cond_false
)) {
304 free_stree(&a_F_c_T
);
305 free_stree(&a_F_c_F
);
306 a_F_c_F
= clone_stree(a_F
);
307 overwrite_stree(a_F_c_fake
, &a_F_c_F
);
310 merge_stree(&a_T_b_T
, a_F_c_T
);
311 merge_stree(&a_T_b_F
, a_F_c_F
);
313 tmp
= __pop_cond_true_stack();
315 tmp
= __pop_cond_false_stack();
318 __push_cond_stacks();
319 FOR_EACH_SM(a_T_b_T
, sm
) {
320 __set_true_false_sm(sm
, NULL
);
321 } END_FOR_EACH_SM(sm
);
322 FOR_EACH_SM(a_T_b_F
, sm
) {
323 __set_true_false_sm(NULL
, sm
);
324 } END_FOR_EACH_SM(sm
);
326 free_stree(&a_T_b_fake
);
327 free_stree(&a_F_c_fake
);
328 free_stree(&a_F_c_T
);
329 free_stree(&a_F_c_F
);
330 free_stree(&a_T_b_T
);
331 free_stree(&a_T_b_F
);
336 static void handle_comma(struct expression
*expr
)
338 __split_expr(expr
->left
);
339 split_conditions(expr
->right
);
342 static int make_op_unsigned(int op
)
346 return SPECIAL_UNSIGNED_LT
;
348 return SPECIAL_UNSIGNED_LTE
;
350 return SPECIAL_UNSIGNED_GT
;
352 return SPECIAL_UNSIGNED_GTE
;
357 static void hackup_unsigned_compares(struct expression
*expr
)
359 if (expr
->type
!= EXPR_COMPARE
)
362 if (type_unsigned(get_type(expr
)))
363 expr
->op
= make_op_unsigned(expr
->op
);
366 static void split_conditions(struct expression
*expr
)
369 char *cond
= expr_to_str(expr
);
371 sm_msg("%d in split_conditions (%s)", get_lineno(), cond
);
375 expr
= strip_expr(expr
);
379 switch (expr
->type
) {
381 __pass_to_client(expr
, LOGIC_HOOK
);
382 handle_logical(expr
);
385 hackup_unsigned_compares(expr
);
386 if (handle_zero_comparisons(expr
))
390 if (ignore_builtin_expect(expr
))
394 if (handle_preop(expr
))
397 case EXPR_CONDITIONAL
:
406 /* fixme: this should be in smatch_flow.c
407 but because of the funny stuff we do with conditions
408 it's awkward to put it there. We would need to
409 call CONDITION_HOOK in smatch_flow as well.
411 push_expression(&big_expression_stack
, expr
);
412 if (expr
->type
== EXPR_COMPARE
) {
413 if (expr
->left
->type
!= EXPR_POSTOP
)
414 __split_expr(expr
->left
);
415 if (expr
->right
->type
!= EXPR_POSTOP
)
416 __split_expr(expr
->right
);
417 } else if (expr
->type
!= EXPR_POSTOP
) {
420 __pass_to_client(expr
, CONDITION_HOOK
);
421 if (expr
->type
== EXPR_COMPARE
) {
422 if (expr
->left
->type
== EXPR_POSTOP
)
423 __split_expr(expr
->left
);
424 if (expr
->right
->type
== EXPR_POSTOP
)
425 __split_expr(expr
->right
);
426 } else if (expr
->type
== EXPR_POSTOP
) {
429 __process_post_op_stack();
430 pop_expression(&big_expression_stack
);
433 static int inside_condition
;
434 void __split_whole_condition(struct expression
*expr
)
436 sm_debug("%d in __split_whole_condition\n", get_lineno());
438 __save_pre_cond_states();
439 __push_cond_stacks();
440 /* it's a hack, but it's sometimes handy to have this stuff
441 on the big_expression_stack. */
442 push_expression(&big_expression_stack
, expr
);
444 split_conditions(expr
);
446 __pass_to_client(expr
, WHOLE_CONDITION_HOOK
);
447 pop_expression(&big_expression_stack
);
449 sm_debug("%d done __split_whole_condition\n", get_lineno());
452 void __handle_logic(struct expression
*expr
)
454 sm_debug("%d in __handle_logic\n", get_lineno());
456 __save_pre_cond_states();
457 __push_cond_stacks();
458 /* it's a hack, but it's sometimes handy to have this stuff
459 on the big_expression_stack. */
460 push_expression(&big_expression_stack
, expr
);
462 split_conditions(expr
);
464 __pass_to_client(expr
, WHOLE_CONDITION_HOOK
);
465 pop_expression(&big_expression_stack
);
466 __merge_false_states();
468 sm_debug("%d done __handle_logic\n", get_lineno());
471 int is_condition(struct expression
*expr
)
474 expr
= strip_expr(expr
);
478 switch (expr
->type
) {
489 int __handle_condition_assigns(struct expression
*expr
)
491 struct expression
*right
;
495 right
= strip_expr(expr
->right
);
496 if (!is_condition(expr
->right
))
499 sm_debug("%d in __handle_condition_assigns\n", get_lineno());
501 __save_pre_cond_states();
502 __push_cond_stacks();
503 /* it's a hack, but it's sometimes handy to have this stuff
504 on the big_expression_stack. */
505 push_expression(&big_expression_stack
, right
);
506 split_conditions(right
);
508 set_extra_expr_mod(expr
->left
, alloc_estate_sval(sval_type_val(get_type(expr
->left
), 1)));
509 __pass_to_client(right
, WHOLE_CONDITION_HOOK
);
510 pop_expression(&big_expression_stack
);
512 sm_debug("%d done __handle_condition_assigns\n", get_lineno());
514 __push_true_states();
515 __use_false_states();
516 set_extra_expr_mod(expr
->left
, alloc_estate_sval(sval_type_val(get_type(expr
->left
), 0)));
517 __merge_true_states();
518 __pass_to_client(expr
, ASSIGNMENT_HOOK
);
522 static int is_select_assign(struct expression
*expr
)
524 struct expression
*right
;
528 right
= strip_expr(expr
->right
);
529 if (right
->type
== EXPR_CONDITIONAL
)
531 if (right
->type
== EXPR_SELECT
)
536 static void set_fake_assign(struct expression
*new,
537 struct expression
*left
, int op
, struct expression
*right
)
540 new->pos
= left
->pos
;
542 new->type
= EXPR_ASSIGNMENT
;
547 int __handle_select_assigns(struct expression
*expr
)
549 struct expression
*right
;
550 struct stree
*final_states
= NULL
;
555 if (!is_select_assign(expr
))
557 sm_debug("%d in __handle_ternary_assigns\n", get_lineno());
558 right
= strip_expr(expr
->right
);
560 is_true
= implied_condition_true(right
->conditional
);
561 is_false
= implied_condition_false(right
->conditional
);
563 /* hah hah. the ultra fake out */
564 __save_pre_cond_states();
565 __split_whole_condition(right
->conditional
);
568 struct expression fake_expr
;
570 if (right
->cond_true
)
571 set_fake_assign(&fake_expr
, expr
->left
, expr
->op
, right
->cond_true
);
573 set_fake_assign(&fake_expr
, expr
->left
, expr
->op
, right
->conditional
);
574 __split_expr(&fake_expr
);
575 final_states
= clone_stree(__get_cur_stree());
578 __use_false_states();
580 struct expression fake_expr
;
582 set_fake_assign(&fake_expr
, expr
->left
, expr
->op
, right
->cond_false
);
583 __split_expr(&fake_expr
);
584 merge_stree(&final_states
, __get_cur_stree());
587 __use_pre_cond_states();
589 FOR_EACH_SM(final_states
, sm
) {
591 } END_FOR_EACH_SM(sm
);
593 free_stree(&final_states
);
595 sm_debug("%d done __handle_ternary_assigns\n", get_lineno());
600 static struct statement
*split_then_return_last(struct statement
*stmt
)
602 struct statement
*tmp
;
603 struct statement
*last_stmt
;
605 last_stmt
= last_ptr_list((struct ptr_list
*)stmt
->stmts
);
609 __push_scope_hooks();
610 FOR_EACH_PTR(stmt
->stmts
, tmp
) {
611 if (tmp
== last_stmt
)
614 } END_FOR_EACH_PTR(tmp
);
618 int __handle_expr_statement_assigns(struct expression
*expr
)
620 struct expression
*right
;
621 struct statement
*stmt
;
624 if (right
->type
== EXPR_PREOP
&& right
->op
== '(')
626 if (right
->type
!= EXPR_STATEMENT
)
630 stmt
= right
->statement
;
631 if (stmt
->type
== STMT_COMPOUND
) {
632 struct statement
*last_stmt
;
633 struct expression fake_assign
;
634 struct expression fake_expr_stmt
;
636 last_stmt
= split_then_return_last(stmt
);
642 fake_expr_stmt
.pos
= last_stmt
->pos
;
643 fake_expr_stmt
.type
= EXPR_STATEMENT
;
644 fake_expr_stmt
.statement
= last_stmt
;
646 fake_assign
.pos
= last_stmt
->pos
;
647 fake_assign
.op
= expr
->op
;
648 fake_assign
.type
= EXPR_ASSIGNMENT
;
649 fake_assign
.left
= expr
->left
;
650 fake_assign
.right
= &fake_expr_stmt
;
652 __split_expr(&fake_assign
);
654 __call_scope_hooks();
655 } else if (stmt
->type
== STMT_EXPRESSION
) {
656 struct expression fake_assign
;
658 fake_assign
.pos
= stmt
->pos
;
659 fake_assign
.op
= expr
->op
;
660 fake_assign
.type
= EXPR_ASSIGNMENT
;
661 fake_assign
.left
= expr
->left
;
662 fake_assign
.right
= stmt
->expression
;
664 __split_expr(&fake_assign
);
673 int in_condition(void)
675 return inside_condition
;