db/fixup_kernel.sh: fix clear_user() handling
[smatch.git] / smatch_conditions.c
blobf1d2d242e8c53a57acc0173ff4f6dce173d1433c
1 /*
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
20 * if (a) { ...
22 * The next simplest kind of conditions is
23 * if (a && b) { c;
24 * In that case 'a' is true when we get to 'b' and both are true
25 * when we get to c.
27 * Or's are a little more complicated.
28 * if (a || b) { c;
29 * We know 'a' is not true when we get to 'b' but it may be true
30 * when we get to c.
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
38 * some if conditions.
39 * if (!a) { ...
40 * Smatch has passes the un-negated version to the client and flip
41 * the true and false values internally. This makes it easier
42 * to write checks.
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:
51 * if ((a) != 0){ ...
52 * that's basically the same as testing for just 'a' and we simplify
53 * comparisons with zero before passing it to the script.
57 #include "smatch.h"
58 #include "smatch_slist.h"
59 #include "smatch_extra.h"
60 #include "smatch_expression_stacks.h"
62 extern int __expr_stmt_count;
64 struct expression_list *big_condition_stack;
66 static void split_conditions(struct expression *expr, int *known_tf);
68 static int negate_tf(int known_tf)
70 if (known_tf == -1)
71 return -1;
72 return !known_tf;
75 static int is_logical_and(struct expression *expr)
77 if (expr->op == SPECIAL_LOGICAL_AND)
78 return 1;
79 return 0;
82 static int handle_zero_comparisons(struct expression *expr, int *known_tf)
84 struct expression *tmp = NULL;
85 struct expression *zero;
87 // if left is zero or right is zero
88 if (expr_is_zero(expr->left)) {
89 zero = strip_expr(expr->left);
90 if (zero->type != EXPR_VALUE)
91 __split_expr(expr->left);
92 tmp = expr->right;
93 } else if (expr_is_zero(expr->right)) {
94 zero = strip_expr(expr->left);
95 if (zero->type != EXPR_VALUE)
96 __split_expr(expr->right);
97 tmp = expr->left;
98 } else {
99 return 0;
102 // "if (foo != 0)" is the same as "if (foo)"
103 if (expr->op == SPECIAL_NOTEQUAL) {
104 split_conditions(tmp, known_tf);
105 return 1;
108 // "if (foo == 0)" is the same as "if (!foo)"
109 if (expr->op == SPECIAL_EQUAL) {
110 split_conditions(tmp, known_tf);
111 *known_tf = negate_tf(*known_tf);
112 __negate_cond_stacks();
113 return 1;
116 return 0;
120 * This function is for handling calls to likely/unlikely
123 static int ignore_builtin_expect(struct expression *expr, int *known_tf)
125 // TODO: move this into the caller
126 if (sym_name_is("__builtin_expect", expr->fn)) {
127 split_conditions(first_ptr_list((struct ptr_list *) expr->args), known_tf);
128 return 1;
130 return 0;
134 * handle_compound_stmt() is for: foo = ({blah; blah; blah; 1})
137 static void handle_compound_stmt(struct statement *stmt, int *known_tf)
139 struct expression *expr = NULL;
140 struct statement *last;
141 struct statement *s;
143 last = last_ptr_list((struct ptr_list *)stmt->stmts);
144 if (last->type == STMT_LABEL) {
145 if (last->label_statement &&
146 last->label_statement->type == STMT_EXPRESSION)
147 expr = last->label_statement->expression;
148 else
149 last = NULL;
150 } else if (last->type != STMT_EXPRESSION) {
151 last = NULL;
152 } else {
153 expr = last->expression;
156 FOR_EACH_PTR(stmt->stmts, s) {
157 if (s != last)
158 __split_stmt(s);
159 } END_FOR_EACH_PTR(s);
160 if (last && last->type == STMT_LABEL)
161 __split_label_stmt(last);
162 split_conditions(expr, known_tf);
165 static int handle_preop(struct expression *expr, int *known_tf)
167 struct statement *stmt;
169 if (expr->op == '!') {
170 split_conditions(expr->unop, known_tf);
171 *known_tf = negate_tf(*known_tf);
172 __negate_cond_stacks();
173 return 1;
175 stmt = get_expression_statement(expr);
176 if (stmt) {
177 handle_compound_stmt(stmt, known_tf);
178 return 1;
180 return 0;
183 static void handle_logical(struct expression *expr, int *known_tf)
185 int left_tf = -1, right_tf = -1;
188 * If we come to an "and" expr then:
189 * We split the left side.
190 * We keep all the current states.
191 * We split the right side.
192 * We keep all the states from both true sides.
194 * If it's an "or" expr then:
195 * We save the current slist.
196 * We split the left side.
197 * We use the false states for the right side.
198 * We split the right side.
199 * We save all the states that are the same on both sides.
202 split_conditions(expr->left, &left_tf);
204 if (is_logical_and(expr))
205 __use_cond_true_states();
206 else
207 __use_cond_false_states();
209 __push_cond_stacks();
211 __save_pre_cond_states();
212 split_conditions(expr->right, &right_tf);
213 __discard_pre_cond_states();
215 if (is_logical_and(expr))
216 __and_cond_states();
217 else
218 __or_cond_states();
220 __use_cond_true_states();
222 if (is_logical_and(expr)) {
223 if (left_tf == true && right_tf == true)
224 *known_tf = true;
225 if (left_tf == false || right_tf == false)
226 *known_tf = false;
227 } else {
228 if (left_tf == true || right_tf == true)
229 *known_tf = true;
230 if (left_tf == false && right_tf == false)
231 *known_tf = false;
235 static struct stree *combine_strees(struct stree *orig, struct stree *fake, struct stree *new)
237 struct stree *ret = NULL;
239 overwrite_stree(orig, &ret);
240 overwrite_stree(fake, &ret);
241 overwrite_stree(new, &ret);
242 free_stree(&new);
244 return ret;
248 * handle_select()
249 * if ((aaa()?bbb():ccc())) { ...
251 * This is almost the same as:
252 * if ((aaa() && bbb()) || (!aaa() && ccc())) { ...
254 * It's a bit complicated because we shouldn't pass aaa()
255 * to the clients more than once.
258 static void handle_select(struct expression *expr, int *known_tf)
260 struct stree *a_T = NULL;
261 struct stree *a_F = NULL;
262 struct stree *a_T_b_T = NULL;
263 struct stree *a_T_b_F = NULL;
264 struct stree *a_T_b_fake = NULL;
265 struct stree *a_F_c_T = NULL;
266 struct stree *a_F_c_F = NULL;
267 struct stree *a_F_c_fake = NULL;
268 struct stree *tmp;
269 struct sm_state *sm;
270 int cond_tf = -1, true_tf = -1, false_tf = -1;
273 * Imagine we have this: if (a ? b : c) { ...
275 * The condition is true if "a" is true and "b" is true or
276 * "a" is false and "c" is true. It's false if "a" is true
277 * and "b" is false or "a" is false and "c" is false.
279 * The variable name "a_T_b_T" stands for "a true b true" etc.
281 * But if we know "b" is true then we can simpilify things.
282 * The condition is true if "a" is true or if "a" is false and
283 * "c" is true. The only way the condition can be false is if
284 * "a" is false and "c" is false.
286 * The remaining thing is the "a_T_b_fake". When we simplify
287 * the equations we have to take into consideration that other
288 * states may have changed that don't play into the true false
289 * equation. Take the following example:
290 * if ({
291 * (flags) = __raw_local_irq_save();
292 * _spin_trylock(lock) ? 1 :
293 * ({ raw_local_irq_restore(flags); 0; });
294 * })
295 * Smatch has to record that the irq flags were restored on the
296 * false path.
300 __save_pre_cond_states();
302 split_conditions(expr->conditional, &cond_tf);
304 a_T = __copy_cond_true_states();
305 a_F = __copy_cond_false_states();
307 __use_cond_true_states();
309 __push_cond_stacks();
310 __push_fake_cur_stree();
311 split_conditions(expr->cond_true, &true_tf);
312 __process_post_op_stack();
313 a_T_b_fake = __pop_fake_cur_stree();
314 a_T_b_T = combine_strees(a_T, a_T_b_fake, __pop_cond_true_stack());
315 a_T_b_F = combine_strees(a_T, a_T_b_fake, __pop_cond_false_stack());
317 __use_cond_false_states();
319 __push_cond_stacks();
320 __push_fake_cur_stree();
321 split_conditions(expr->cond_false, &false_tf);
322 a_F_c_fake = __pop_fake_cur_stree();
323 a_F_c_T = combine_strees(a_F, a_F_c_fake, __pop_cond_true_stack());
324 a_F_c_F = combine_strees(a_F, a_F_c_fake, __pop_cond_false_stack());
326 /* We have to restore the pre condition states so that
327 implied_condition_true() will use the right cur_stree */
328 __use_pre_cond_states();
330 if (implied_condition_true(expr->cond_true)) {
331 free_stree(&a_T_b_T);
332 free_stree(&a_T_b_F);
333 a_T_b_T = clone_stree(a_T);
334 overwrite_stree(a_T_b_fake, &a_T_b_T);
336 if (implied_condition_false(expr->cond_true)) {
337 free_stree(&a_T_b_T);
338 free_stree(&a_T_b_F);
339 a_T_b_F = clone_stree(a_T);
340 overwrite_stree(a_T_b_fake, &a_T_b_F);
342 if (implied_condition_true(expr->cond_false)) {
343 free_stree(&a_F_c_T);
344 free_stree(&a_F_c_F);
345 a_F_c_T = clone_stree(a_F);
346 overwrite_stree(a_F_c_fake, &a_F_c_T);
348 if (implied_condition_false(expr->cond_false)) {
349 free_stree(&a_F_c_T);
350 free_stree(&a_F_c_F);
351 a_F_c_F = clone_stree(a_F);
352 overwrite_stree(a_F_c_fake, &a_F_c_F);
355 merge_stree(&a_T_b_T, a_F_c_T);
356 merge_stree(&a_T_b_F, a_F_c_F);
358 tmp = __pop_cond_true_stack();
359 free_stree(&tmp);
360 tmp = __pop_cond_false_stack();
361 free_stree(&tmp);
363 __push_cond_stacks();
364 FOR_EACH_SM(a_T_b_T, sm) {
365 __set_true_false_sm(sm, NULL);
366 } END_FOR_EACH_SM(sm);
367 FOR_EACH_SM(a_T_b_F, sm) {
368 __set_true_false_sm(NULL, sm);
369 } END_FOR_EACH_SM(sm);
370 __free_set_states();
372 free_stree(&a_T_b_fake);
373 free_stree(&a_F_c_fake);
374 free_stree(&a_F_c_T);
375 free_stree(&a_F_c_F);
376 free_stree(&a_T_b_T);
377 free_stree(&a_T_b_F);
378 free_stree(&a_T);
379 free_stree(&a_F);
381 if (cond_tf == true && true_tf == true)
382 *known_tf = true;
383 if (cond_tf == false && false_tf == false)
384 *known_tf = false;
387 static void handle_comma(struct expression *expr, int *known_tf)
389 __split_expr(expr->left);
390 split_conditions(expr->right, known_tf);
393 static int make_op_unsigned(int op)
395 switch (op) {
396 case '<':
397 return SPECIAL_UNSIGNED_LT;
398 case SPECIAL_LTE:
399 return SPECIAL_UNSIGNED_LTE;
400 case '>':
401 return SPECIAL_UNSIGNED_GT;
402 case SPECIAL_GTE:
403 return SPECIAL_UNSIGNED_GTE;
405 return op;
408 static void hackup_unsigned_compares(struct expression *expr)
410 if (expr->type != EXPR_COMPARE)
411 return;
413 if (type_unsigned(get_type(expr)))
414 expr->op = make_op_unsigned(expr->op);
417 static bool handle_expr_statement_conditions(struct expression *expr, int *known_tf)
419 struct statement *last_stmt, *stmt;
420 struct expression *last_expr;
422 if (expr->type == EXPR_PREOP && expr->op == '(')
423 expr = expr->unop;
424 if (expr->type != EXPR_STATEMENT)
425 return false;
427 stmt = expr->statement;
428 if (stmt->type != STMT_COMPOUND)
429 return false;
431 last_stmt = last_ptr_list((struct ptr_list *)stmt->stmts);
432 if (!last_stmt)
433 return false;
436 * I don't think this is right, but I'm not sure how to handle other
437 * types of statements.
439 if (last_stmt->type == STMT_EXPRESSION)
440 last_expr = last_stmt->expression;
441 else if (last_stmt->type == STMT_LABEL &&
442 last_stmt->label_statement->type == STMT_EXPRESSION)
443 last_expr = last_stmt->label_statement->expression;
444 else
445 return false;
447 __expr_stmt_count++;
448 __push_scope_hooks();
449 FOR_EACH_PTR(stmt->stmts, stmt) {
450 if (stmt == last_stmt) {
451 if (stmt->type == STMT_LABEL)
452 __split_label_stmt(stmt);
453 split_conditions(last_expr, known_tf);
454 goto done;
456 __split_stmt(stmt);
457 } END_FOR_EACH_PTR(stmt);
458 exit(1);
459 done:
460 __call_scope_hooks();
461 __expr_stmt_count--;
463 return true;
466 static void do_condition(struct expression *expr)
468 __fold_in_set_states();
469 __push_fake_cur_stree();
470 __pass_to_client(expr, CONDITION_HOOK);
471 __fold_in_set_states();
474 static int confidence_implied;
475 void __set_confidence_implied(void)
477 confidence_implied++;
480 void __unset_confidence(void)
482 confidence_implied--;
485 static void split_conditions(struct expression *expr, int *known_tf)
487 if (option_debug) {
488 char *cond = expr_to_str(expr);
490 sm_msg("%d in split_conditions(%s)", get_lineno(), cond);
491 free_string(cond);
494 expr = strip_expr_set_parent(expr);
495 if (!expr) {
496 __fold_in_set_states();
497 return;
500 if (handle_expr_statement_conditions(expr, known_tf))
501 return;
504 * On fast paths (and also I guess some people think it's cool) people
505 * sometimes use | instead of ||. It works the same basically except
506 * that || implies a memory barrier between conditions. The easiest way
507 * to handle it is by pretending that | also has a barrier and re-using
508 * all the normal condition code. This potentially hides some bugs, but
509 * people who write code like this should just be careful or they
510 * deserve bugs.
512 * We could potentially treat boolean bitwise & this way but that seems
513 * too complicated to deal with.
515 if (expr->type == EXPR_BINOP && expr->op == '|') {
516 expr_set_parent_expr(expr->left, expr);
517 expr_set_parent_expr(expr->right, expr);
518 handle_logical(expr, known_tf);
519 return;
522 switch (expr->type) {
523 case EXPR_LOGICAL:
524 expr_set_parent_expr(expr->left, expr);
525 expr_set_parent_expr(expr->right, expr);
526 __pass_to_client(expr, LOGIC_HOOK);
527 handle_logical(expr, known_tf);
528 return;
529 case EXPR_COMPARE:
530 expr_set_parent_expr(expr->left, expr);
531 expr_set_parent_expr(expr->right, expr);
532 hackup_unsigned_compares(expr);
533 if (handle_zero_comparisons(expr, known_tf))
534 return;
535 break;
536 case EXPR_CALL:
537 if (ignore_builtin_expect(expr, known_tf))
538 return;
539 break;
540 case EXPR_PREOP:
541 expr_set_parent_expr(expr->unop, expr);
542 if (handle_preop(expr, known_tf))
543 return;
544 break;
545 case EXPR_CONDITIONAL:
546 case EXPR_SELECT:
547 expr_set_parent_expr(expr->conditional, expr);
548 expr_set_parent_expr(expr->cond_true, expr);
549 expr_set_parent_expr(expr->cond_false, expr);
550 handle_select(expr, known_tf);
551 return;
552 case EXPR_COMMA:
553 expr_set_parent_expr(expr->left, expr);
554 expr_set_parent_expr(expr->right, expr);
555 handle_comma(expr, known_tf);
556 return;
559 /* fixme: this should be in smatch_flow.c
560 but because of the funny stuff we do with conditions
561 it's awkward to put it there. We would need to
562 call CONDITION_HOOK in smatch_flow as well.
564 push_expression(&big_expression_stack, expr);
565 push_expression(&big_condition_stack, expr);
567 if (expr->type == EXPR_COMPARE) {
568 if (expr->left->type != EXPR_POSTOP)
569 __split_expr(expr->left);
570 if (expr->right->type != EXPR_POSTOP)
571 __split_expr(expr->right);
572 } else if (expr->type != EXPR_POSTOP) {
573 __split_expr(expr);
575 do_condition(expr);
576 if (expr->type == EXPR_COMPARE) {
577 if (expr->left->type == EXPR_POSTOP)
578 __split_expr(expr->left);
579 if (expr->right->type == EXPR_POSTOP)
580 __split_expr(expr->right);
581 } else if (expr->type == EXPR_POSTOP) {
582 __split_expr(expr);
584 if (*known_tf != -1)
585 sm_perror("overwriting known tf. expr='%s' tf=%d",
586 expr_to_str(expr), *known_tf);
588 if (confidence_implied) {
589 if (implied_condition_true(expr))
590 *known_tf = true;
591 if (implied_condition_false(expr))
592 *known_tf = false;
593 } else {
594 if (known_condition_true(expr))
595 *known_tf = true;
596 else if (known_condition_false(expr))
597 *known_tf = false;
600 __push_fake_cur_stree();
601 __process_post_op_stack();
602 __fold_in_set_states();
603 pop_expression(&big_condition_stack);
604 pop_expression(&big_expression_stack);
607 static int inside_condition;
608 void __split_whole_condition_tf(struct expression *expr, int *known_tf)
611 *known_tf = -1;
612 inside_condition++;
614 __save_pre_cond_states();
615 __push_cond_stacks();
616 /* it's a hack, but it's sometimes handy to have this stuff
617 on the big_expression_stack. */
618 push_expression(&big_expression_stack, expr);
619 split_conditions(expr, known_tf);
620 __use_cond_states();
621 __pass_to_client(expr, WHOLE_CONDITION_HOOK);
622 pop_expression(&big_expression_stack);
623 inside_condition--;
626 void __split_whole_condition(struct expression *expr)
628 int known_tf;
630 __split_whole_condition_tf(expr, &known_tf);
633 void __handle_logic(struct expression *expr)
635 int known_tf = -1;
637 inside_condition++;
638 __save_pre_cond_states();
639 __push_cond_stacks();
640 /* it's a hack, but it's sometimes handy to have this stuff
641 on the big_expression_stack. */
642 push_expression(&big_expression_stack, expr);
643 if (expr)
644 split_conditions(expr, &known_tf);
645 __use_cond_states();
646 __pass_to_client(expr, WHOLE_CONDITION_HOOK);
647 pop_expression(&big_expression_stack);
648 __merge_false_states();
649 inside_condition--;
652 int is_condition(struct expression *expr)
655 expr = strip_expr(expr);
656 if (!expr)
657 return 0;
659 switch (expr->type) {
660 case EXPR_LOGICAL:
661 case EXPR_COMPARE:
662 return 1;
663 case EXPR_PREOP:
664 if (expr->op == '!')
665 return 1;
667 return 0;
670 int __handle_condition_assigns(struct expression *expr)
672 struct expression *right;
673 struct stree *true_stree, *false_stree, *fake_stree;
674 struct sm_state *sm;
675 int known_tf = -1;
677 if (expr->op != '=')
678 return 0;
679 right = strip_expr(expr->right);
680 if (!is_condition(expr->right))
681 return 0;
683 inside_condition++;
684 __save_pre_cond_states();
685 __push_cond_stacks();
686 /* it's a hack, but it's sometimes handy to have this stuff
687 on the big_expression_stack. */
688 push_expression(&big_expression_stack, right);
689 split_conditions(right, &known_tf);
690 true_stree = __get_true_states();
691 false_stree = __get_false_states();
692 __use_cond_states();
693 __push_fake_cur_stree();
694 set_extra_expr_mod(expr->left, alloc_estate_sval(sval_type_val(get_type(expr->left), 1)));
695 __pass_to_client(right, WHOLE_CONDITION_HOOK);
697 fake_stree = __pop_fake_cur_stree();
698 FOR_EACH_SM(fake_stree, sm) {
699 overwrite_sm_state_stree(&true_stree, sm);
700 } END_FOR_EACH_SM(sm);
701 free_stree(&fake_stree);
703 pop_expression(&big_expression_stack);
704 inside_condition--;
706 __push_true_states();
708 __use_false_states();
709 __push_fake_cur_stree();
710 set_extra_expr_mod(expr->left, alloc_estate_sval(sval_type_val(get_type(expr->left), 0)));
712 fake_stree = __pop_fake_cur_stree();
713 FOR_EACH_SM(fake_stree, sm) {
714 overwrite_sm_state_stree(&false_stree, sm);
715 } END_FOR_EACH_SM(sm);
716 free_stree(&fake_stree);
718 __merge_true_states();
719 merge_fake_stree(&true_stree, false_stree);
720 free_stree(&false_stree);
721 FOR_EACH_SM(true_stree, sm) {
722 __set_sm(sm);
723 } END_FOR_EACH_SM(sm);
725 __pass_to_client(expr, ASSIGNMENT_HOOK);
727 return 1;
730 static int is_select_assign(struct expression *expr)
732 struct expression *right;
734 if (expr->op != '=')
735 return 0;
736 right = strip_expr(expr->right);
737 if (right->type == EXPR_CONDITIONAL)
738 return 1;
739 if (right->type == EXPR_SELECT)
740 return 1;
741 return 0;
744 int __handle_select_assigns(struct expression *expr)
746 struct expression *right, *condition;
747 struct stree *final_states = NULL;
748 struct sm_state *sm;
749 int is_true;
750 int is_false;
751 char buf[64];
753 if (!is_select_assign(expr))
754 return 0;
755 right = strip_expr(expr->right);
756 __pass_to_client(right, SELECT_HOOK);
758 // FIXME: why is this implied instead of known?
759 is_true = implied_condition_true(right->conditional);
760 is_false = implied_condition_false(right->conditional);
763 * For "x = frob() ?: y;" we only want to parse the frob() call once
764 * so do the assignment and parse the condition in one step.
766 if (right->cond_true) {
767 condition = right->conditional;
768 expr_set_parent_expr(condition, right);
769 __save_pre_cond_states();
770 __split_whole_condition(condition);
771 } else {
772 struct expression *fake_condition, *fake_assign;
774 snprintf(buf, sizeof(buf), "fake_cond_%p", expr);
775 fake_condition = create_fake_assign(buf, get_type(right->conditional), right->conditional);
776 if (!fake_condition) {
777 sm_perror("cannot parse condition");
778 return 0;
780 expr_set_parent_expr(fake_condition, right);
781 __save_pre_cond_states();
782 __split_whole_condition(fake_condition);
783 fake_assign = assign_expression(expr->left, expr->op, fake_condition->left);
784 expr_set_parent_expr(fake_assign, right);
785 __split_expr(fake_assign);
788 if (!is_false && right->cond_true) {
789 struct expression *fake_expr;
791 fake_expr = assign_expression(expr->left, expr->op, right->cond_true);
792 expr_set_parent_expr(fake_expr, expr);
793 __split_expr(fake_expr);
794 final_states = clone_stree(__get_cur_stree());
795 } else if (!is_false) {
796 final_states = clone_stree(__get_cur_stree());
799 if (is_true) {
800 __discard_false_states();
801 merge_stree(&final_states, __get_cur_stree());
802 } else {
803 struct expression *fake_expr;
805 __use_false_states();
806 fake_expr = assign_expression(expr->left, expr->op, right->cond_false);
807 expr_set_parent_expr(fake_expr, expr);
808 __split_expr(fake_expr);
809 merge_stree(&final_states, __get_cur_stree());
812 __use_pre_cond_states();
814 FOR_EACH_SM(final_states, sm) {
815 __set_sm(sm);
816 } END_FOR_EACH_SM(sm);
818 free_stree(&final_states);
820 return 1;
823 static struct statement *split_then_return_last(struct statement *stmt)
825 struct statement *tmp;
826 struct statement *last_stmt;
828 last_stmt = last_ptr_list((struct ptr_list *)stmt->stmts);
829 if (!last_stmt)
830 return NULL;
832 __push_scope_hooks();
833 FOR_EACH_PTR(stmt->stmts, tmp) {
834 if (tmp == last_stmt) {
835 if (tmp->type == STMT_LABEL) {
836 __split_label_stmt(tmp);
837 return tmp->label_statement;
839 return last_stmt;
841 __split_stmt(tmp);
842 } END_FOR_EACH_PTR(tmp);
843 return NULL;
846 static struct expression *add_casts(struct expression *cast, struct expression *expr)
848 if (!cast)
849 return expr;
850 while (cast->cast_expression)
851 cast = cast->cast_expression;
852 cast->cast_expression = expr;
853 return cast;
856 int __handle_expr_statement_assigns(struct expression *expr)
858 struct expression *cast, *right;
859 struct statement *stmt;
861 right = strip_expr_cast(expr->right, &cast);
862 if (right->type == EXPR_PREOP && right->op == '(')
863 right = right->unop;
864 if (right->type != EXPR_STATEMENT)
865 return 0;
867 __expr_stmt_count++;
868 stmt = right->statement;
869 if (stmt->type == STMT_COMPOUND) {
870 struct statement *last_stmt;
871 struct expression *fake_assign;
872 struct expression fake_expr_stmt = { .smatch_flags = Fake, };
874 last_stmt = split_then_return_last(stmt);
875 if (!last_stmt) {
876 __expr_stmt_count--;
877 return 0;
880 fake_expr_stmt.pos = last_stmt->pos;
881 fake_expr_stmt.type = EXPR_STATEMENT;
882 fake_expr_stmt.op = 0;
883 fake_expr_stmt.statement = last_stmt;
885 fake_assign = assign_expression(expr->left, expr->op, &fake_expr_stmt);
886 fake_assign = add_casts(cast, fake_assign);
887 expr_set_parent_expr(fake_assign, expr);
888 __split_expr(fake_assign);
890 __pass_to_client(stmt, STMT_HOOK_AFTER);
891 __call_scope_hooks();
892 } else if (stmt->type == STMT_EXPRESSION) {
893 struct expression *fake_assign;
895 right = strip_no_cast(stmt->expression);
896 fake_assign = assign_expression(expr->left, expr->op, right);
897 fake_assign = add_casts(cast, fake_assign);
898 expr_set_parent_expr(fake_assign, expr);
899 __split_expr(fake_assign);
901 } else {
902 __split_stmt(stmt);
904 __expr_stmt_count--;
905 return 1;
908 int in_condition(void)
910 return inside_condition;