flow, conditions: handle label statements correctly
[smatch.git] / smatch_conditions.c
blob100357011a50a99594e3403553ae2e82bbf121b1
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);
68 static int is_logical_and(struct expression *expr)
70 if (expr->op == SPECIAL_LOGICAL_AND)
71 return 1;
72 return 0;
75 static int handle_zero_comparisons(struct expression *expr)
77 struct expression *tmp = NULL;
79 // if left is zero or right is zero
80 if (is_zero(expr->left))
81 tmp = expr->right;
82 else if (is_zero(expr->right))
83 tmp = expr->left;
84 else
85 return 0;
87 // "if (foo != 0)" is the same as "if (foo)"
88 if (expr->op == SPECIAL_NOTEQUAL) {
89 split_conditions(tmp);
90 return 1;
93 // "if (foo == 0)" is the same as "if (!foo)"
94 if (expr->op == SPECIAL_EQUAL) {
95 split_conditions(tmp);
96 __negate_cond_stacks();
97 return 1;
100 return 0;
104 * This function is for handling calls to likely/unlikely
107 static int ignore_builtin_expect(struct expression *expr)
109 if (sym_name_is("__builtin_expect", expr->fn)) {
110 split_conditions(first_ptr_list((struct ptr_list *) expr->args));
111 return 1;
113 return 0;
117 * handle_compound_stmt() is for: foo = ({blah; blah; blah; 1})
120 static void handle_compound_stmt(struct statement *stmt)
122 struct expression *expr = NULL;
123 struct statement *last;
124 struct statement *s;
126 last = last_ptr_list((struct ptr_list *)stmt->stmts);
127 if (last->type == STMT_LABEL) {
128 if (last->label_statement &&
129 last->label_statement->type == STMT_EXPRESSION)
130 expr = last->label_statement->expression;
131 else
132 last = NULL;
133 } else if (last->type != STMT_EXPRESSION) {
134 last = NULL;
135 } else {
136 expr = last->expression;
139 FOR_EACH_PTR(stmt->stmts, s) {
140 if (s != last)
141 __split_stmt(s);
142 } END_FOR_EACH_PTR(s);
143 if (last->type == STMT_LABEL)
144 __split_label_stmt(last);
145 split_conditions(expr);
146 return;
149 static int handle_preop(struct expression *expr)
151 struct statement *stmt;
153 if (expr->op == '!') {
154 split_conditions(expr->unop);
155 __negate_cond_stacks();
156 return 1;
158 stmt = get_expression_statement(expr);
159 if (stmt) {
160 handle_compound_stmt(stmt);
161 return 1;
163 return 0;
166 static void handle_logical(struct expression *expr)
169 * If we come to an "and" expr then:
170 * We split the left side.
171 * We keep all the current states.
172 * We split the right side.
173 * We keep all the states from both true sides.
175 * If it's an "or" expr then:
176 * We save the current slist.
177 * We split the left side.
178 * We use the false states for the right side.
179 * We split the right side.
180 * We save all the states that are the same on both sides.
183 split_conditions(expr->left);
184 __process_post_op_stack();
186 if (is_logical_and(expr))
187 __use_cond_true_states();
188 else
189 __use_cond_false_states();
191 __push_cond_stacks();
193 __save_pre_cond_states();
194 split_conditions(expr->right);
195 __process_post_op_stack();
196 __discard_pre_cond_states();
198 if (is_logical_and(expr))
199 __and_cond_states();
200 else
201 __or_cond_states();
203 __use_cond_true_states();
206 static struct stree *combine_strees(struct stree *orig, struct stree *fake, struct stree *new)
208 struct stree *ret = NULL;
210 overwrite_stree(orig, &ret);
211 overwrite_stree(fake, &ret);
212 overwrite_stree(new, &ret);
213 free_stree(&new);
215 return ret;
219 * handle_select()
220 * if ((aaa()?bbb():ccc())) { ...
222 * This is almost the same as:
223 * if ((aaa() && bbb()) || (!aaa() && ccc())) { ...
225 * It's a bit complicated because we shouldn't pass aaa()
226 * to the clients more than once.
229 static void handle_select(struct expression *expr)
231 struct stree *a_T = NULL;
232 struct stree *a_F = NULL;
233 struct stree *a_T_b_T = NULL;
234 struct stree *a_T_b_F = NULL;
235 struct stree *a_T_b_fake = NULL;
236 struct stree *a_F_c_T = NULL;
237 struct stree *a_F_c_F = NULL;
238 struct stree *a_F_c_fake = NULL;
239 struct stree *tmp;
240 struct sm_state *sm;
243 * Imagine we have this: if (a ? b : c) { ...
245 * The condition is true if "a" is true and "b" is true or
246 * "a" is false and "c" is true. It's false if "a" is true
247 * and "b" is false or "a" is false and "c" is false.
249 * The variable name "a_T_b_T" stands for "a true b true" etc.
251 * But if we know "b" is true then we can simpilify things.
252 * The condition is true if "a" is true or if "a" is false and
253 * "c" is true. The only way the condition can be false is if
254 * "a" is false and "c" is false.
256 * The remaining thing is the "a_T_b_fake". When we simplify
257 * the equations we have to take into consideration that other
258 * states may have changed that don't play into the true false
259 * equation. Take the following example:
260 * if ({
261 * (flags) = __raw_local_irq_save();
262 * _spin_trylock(lock) ? 1 :
263 * ({ raw_local_irq_restore(flags); 0; });
264 * })
265 * Smatch has to record that the irq flags were restored on the
266 * false path.
270 __save_pre_cond_states();
272 split_conditions(expr->conditional);
274 a_T = __copy_cond_true_states();
275 a_F = __copy_cond_false_states();
277 __use_cond_true_states();
279 __push_cond_stacks();
280 __push_fake_cur_stree();
281 split_conditions(expr->cond_true);
282 __process_post_op_stack();
283 a_T_b_fake = __pop_fake_cur_stree();
284 a_T_b_T = combine_strees(a_T, a_T_b_fake, __pop_cond_true_stack());
285 a_T_b_F = combine_strees(a_T, a_T_b_fake, __pop_cond_false_stack());
287 __use_cond_false_states();
289 __push_cond_stacks();
290 __push_fake_cur_stree();
291 split_conditions(expr->cond_false);
292 a_F_c_fake = __pop_fake_cur_stree();
293 a_F_c_T = combine_strees(a_F, a_F_c_fake, __pop_cond_true_stack());
294 a_F_c_F = combine_strees(a_F, a_F_c_fake, __pop_cond_false_stack());
296 /* We have to restore the pre condition states so that
297 implied_condition_true() will use the right cur_stree */
298 __use_pre_cond_states();
300 if (implied_condition_true(expr->cond_true)) {
301 free_stree(&a_T_b_T);
302 free_stree(&a_T_b_F);
303 a_T_b_T = clone_stree(a_T);
304 overwrite_stree(a_T_b_fake, &a_T_b_T);
306 if (implied_condition_false(expr->cond_true)) {
307 free_stree(&a_T_b_T);
308 free_stree(&a_T_b_F);
309 a_T_b_F = clone_stree(a_T);
310 overwrite_stree(a_T_b_fake, &a_T_b_F);
312 if (implied_condition_true(expr->cond_false)) {
313 free_stree(&a_F_c_T);
314 free_stree(&a_F_c_F);
315 a_F_c_T = clone_stree(a_F);
316 overwrite_stree(a_F_c_fake, &a_F_c_T);
318 if (implied_condition_false(expr->cond_false)) {
319 free_stree(&a_F_c_T);
320 free_stree(&a_F_c_F);
321 a_F_c_F = clone_stree(a_F);
322 overwrite_stree(a_F_c_fake, &a_F_c_F);
325 merge_stree(&a_T_b_T, a_F_c_T);
326 merge_stree(&a_T_b_F, a_F_c_F);
328 tmp = __pop_cond_true_stack();
329 free_stree(&tmp);
330 tmp = __pop_cond_false_stack();
331 free_stree(&tmp);
333 __push_cond_stacks();
334 FOR_EACH_SM(a_T_b_T, sm) {
335 __set_true_false_sm(sm, NULL);
336 } END_FOR_EACH_SM(sm);
337 FOR_EACH_SM(a_T_b_F, sm) {
338 __set_true_false_sm(NULL, sm);
339 } END_FOR_EACH_SM(sm);
341 free_stree(&a_T_b_fake);
342 free_stree(&a_F_c_fake);
343 free_stree(&a_F_c_T);
344 free_stree(&a_F_c_F);
345 free_stree(&a_T_b_T);
346 free_stree(&a_T_b_F);
347 free_stree(&a_T);
348 free_stree(&a_F);
351 static void handle_comma(struct expression *expr)
353 set_parent_expr(expr->left, expr);
354 set_parent_expr(expr->right, expr);
355 __split_expr(expr->left);
356 split_conditions(expr->right);
359 static int make_op_unsigned(int op)
361 switch (op) {
362 case '<':
363 return SPECIAL_UNSIGNED_LT;
364 case SPECIAL_LTE:
365 return SPECIAL_UNSIGNED_LTE;
366 case '>':
367 return SPECIAL_UNSIGNED_GT;
368 case SPECIAL_GTE:
369 return SPECIAL_UNSIGNED_GTE;
371 return op;
374 static void hackup_unsigned_compares(struct expression *expr)
376 if (expr->type != EXPR_COMPARE)
377 return;
379 if (type_unsigned(get_type(expr)))
380 expr->op = make_op_unsigned(expr->op);
383 static void split_conditions(struct expression *expr)
385 if (option_debug) {
386 char *cond = expr_to_str(expr);
388 sm_msg("%d in split_conditions (%s)", get_lineno(), cond);
389 free_string(cond);
392 expr = strip_expr(expr);
393 if (!expr)
394 return;
396 switch (expr->type) {
397 case EXPR_LOGICAL:
398 __pass_to_client(expr, LOGIC_HOOK);
399 handle_logical(expr);
400 return;
401 case EXPR_COMPARE:
402 hackup_unsigned_compares(expr);
403 if (handle_zero_comparisons(expr))
404 return;
405 break;
406 case EXPR_CALL:
407 if (ignore_builtin_expect(expr))
408 return;
409 break;
410 case EXPR_PREOP:
411 if (handle_preop(expr))
412 return;
413 break;
414 case EXPR_CONDITIONAL:
415 case EXPR_SELECT:
416 handle_select(expr);
417 return;
418 case EXPR_COMMA:
419 handle_comma(expr);
420 return;
423 /* fixme: this should be in smatch_flow.c
424 but because of the funny stuff we do with conditions
425 it's awkward to put it there. We would need to
426 call CONDITION_HOOK in smatch_flow as well.
428 push_expression(&big_expression_stack, expr);
429 push_expression(&big_condition_stack, expr);
431 if (expr->type == EXPR_COMPARE) {
432 set_parent_expr(expr->left, expr);
433 set_parent_expr(expr->right, expr);
435 if (expr->left->type != EXPR_POSTOP)
436 __split_expr(expr->left);
437 if (expr->right->type != EXPR_POSTOP)
438 __split_expr(expr->right);
439 } else if (expr->type != EXPR_POSTOP) {
440 __split_expr(expr);
442 __pass_to_client(expr, CONDITION_HOOK);
443 if (expr->type == EXPR_COMPARE) {
444 if (expr->left->type == EXPR_POSTOP)
445 __split_expr(expr->left);
446 if (expr->right->type == EXPR_POSTOP)
447 __split_expr(expr->right);
448 } else if (expr->type == EXPR_POSTOP) {
449 __split_expr(expr);
451 __process_post_op_stack();
452 pop_expression(&big_condition_stack);
453 pop_expression(&big_expression_stack);
456 static int inside_condition;
457 void __split_whole_condition(struct expression *expr)
459 sm_debug("%d in __split_whole_condition\n", get_lineno());
460 inside_condition++;
461 __save_pre_cond_states();
462 __push_cond_stacks();
463 /* it's a hack, but it's sometimes handy to have this stuff
464 on the big_expression_stack. */
465 push_expression(&big_expression_stack, expr);
466 if (expr)
467 split_conditions(expr);
468 __use_cond_states();
469 __pass_to_client(expr, WHOLE_CONDITION_HOOK);
470 pop_expression(&big_expression_stack);
471 inside_condition--;
472 sm_debug("%d done __split_whole_condition\n", get_lineno());
475 void __handle_logic(struct expression *expr)
477 sm_debug("%d in __handle_logic\n", get_lineno());
478 inside_condition++;
479 __save_pre_cond_states();
480 __push_cond_stacks();
481 /* it's a hack, but it's sometimes handy to have this stuff
482 on the big_expression_stack. */
483 push_expression(&big_expression_stack, expr);
484 if (expr)
485 split_conditions(expr);
486 __use_cond_states();
487 __pass_to_client(expr, WHOLE_CONDITION_HOOK);
488 pop_expression(&big_expression_stack);
489 __merge_false_states();
490 inside_condition--;
491 sm_debug("%d done __handle_logic\n", get_lineno());
494 int is_condition(struct expression *expr)
497 expr = strip_expr(expr);
498 if (!expr)
499 return 0;
501 switch (expr->type) {
502 case EXPR_LOGICAL:
503 case EXPR_COMPARE:
504 return 1;
505 case EXPR_PREOP:
506 if (expr->op == '!')
507 return 1;
509 return 0;
512 int __handle_condition_assigns(struct expression *expr)
514 struct expression *right;
516 if (expr->op != '=')
517 return 0;
518 right = strip_expr(expr->right);
519 if (!is_condition(expr->right))
520 return 0;
522 sm_debug("%d in __handle_condition_assigns\n", get_lineno());
523 inside_condition++;
524 __save_pre_cond_states();
525 __push_cond_stacks();
526 /* it's a hack, but it's sometimes handy to have this stuff
527 on the big_expression_stack. */
528 push_expression(&big_expression_stack, right);
529 split_conditions(right);
530 __use_cond_states();
531 set_extra_expr_mod(expr->left, alloc_estate_sval(sval_type_val(get_type(expr->left), 1)));
532 __pass_to_client(right, WHOLE_CONDITION_HOOK);
533 pop_expression(&big_expression_stack);
534 inside_condition--;
535 sm_debug("%d done __handle_condition_assigns\n", get_lineno());
537 __push_true_states();
538 __use_false_states();
539 set_extra_expr_mod(expr->left, alloc_estate_sval(sval_type_val(get_type(expr->left), 0)));
540 __merge_true_states();
541 __pass_to_client(expr, ASSIGNMENT_HOOK);
542 return 1;
545 static int is_select_assign(struct expression *expr)
547 struct expression *right;
549 if (expr->op != '=')
550 return 0;
551 right = strip_expr(expr->right);
552 if (right->type == EXPR_CONDITIONAL)
553 return 1;
554 if (right->type == EXPR_SELECT)
555 return 1;
556 return 0;
559 static void set_fake_assign(struct expression *new,
560 struct expression *left, int op, struct expression *right)
563 new->pos = left->pos;
564 new->op = op;
565 new->type = EXPR_ASSIGNMENT;
566 new->left = left;
567 new->right = right;
570 int __handle_select_assigns(struct expression *expr)
572 struct expression *right;
573 struct stree *final_states = NULL;
574 struct sm_state *sm;
575 int is_true;
576 int is_false;
578 if (!is_select_assign(expr))
579 return 0;
580 sm_debug("%d in __handle_ternary_assigns\n", get_lineno());
581 right = strip_expr(expr->right);
583 is_true = implied_condition_true(right->conditional);
584 is_false = implied_condition_false(right->conditional);
586 /* hah hah. the ultra fake out */
587 __save_pre_cond_states();
588 __split_whole_condition(right->conditional);
590 if (!is_false) {
591 struct expression fake_expr;
593 if (right->cond_true)
594 set_fake_assign(&fake_expr, expr->left, expr->op, right->cond_true);
595 else
596 set_fake_assign(&fake_expr, expr->left, expr->op, right->conditional);
597 __split_expr(&fake_expr);
598 final_states = clone_stree(__get_cur_stree());
601 __use_false_states();
602 if (!is_true) {
603 struct expression fake_expr;
605 set_fake_assign(&fake_expr, expr->left, expr->op, right->cond_false);
606 __split_expr(&fake_expr);
607 merge_stree(&final_states, __get_cur_stree());
610 __use_pre_cond_states();
612 FOR_EACH_SM(final_states, sm) {
613 __set_sm(sm);
614 } END_FOR_EACH_SM(sm);
616 free_stree(&final_states);
618 sm_debug("%d done __handle_ternary_assigns\n", get_lineno());
620 return 1;
623 static struct statement *split_then_return_last(struct statement *stmt)
625 struct statement *tmp;
626 struct statement *last_stmt;
628 last_stmt = last_ptr_list((struct ptr_list *)stmt->stmts);
629 if (!last_stmt)
630 return NULL;
632 __push_scope_hooks();
633 FOR_EACH_PTR(stmt->stmts, tmp) {
634 if (tmp == last_stmt) {
635 if (tmp->type == STMT_LABEL) {
636 __split_label_stmt(tmp);
637 return stmt->label_statement;
639 return last_stmt;
641 __split_stmt(tmp);
642 } END_FOR_EACH_PTR(tmp);
643 return NULL;
646 int __handle_expr_statement_assigns(struct expression *expr)
648 struct expression *right;
649 struct statement *stmt;
651 right = expr->right;
652 if (right->type == EXPR_PREOP && right->op == '(')
653 right = right->unop;
654 if (right->type != EXPR_STATEMENT)
655 return 0;
657 __expr_stmt_count++;
658 stmt = right->statement;
659 if (stmt->type == STMT_COMPOUND) {
660 struct statement *last_stmt;
661 struct expression fake_assign;
662 struct expression fake_expr_stmt;
664 last_stmt = split_then_return_last(stmt);
665 if (!last_stmt) {
666 __expr_stmt_count--;
667 return 0;
670 fake_expr_stmt.pos = last_stmt->pos;
671 fake_expr_stmt.type = EXPR_STATEMENT;
672 fake_expr_stmt.statement = last_stmt;
674 fake_assign.pos = last_stmt->pos;
675 fake_assign.op = expr->op;
676 fake_assign.type = EXPR_ASSIGNMENT;
677 fake_assign.left = expr->left;
678 fake_assign.right = &fake_expr_stmt;
680 __split_expr(&fake_assign);
682 __call_scope_hooks();
683 } else if (stmt->type == STMT_EXPRESSION) {
684 struct expression fake_assign;
686 fake_assign.pos = stmt->pos;
687 fake_assign.op = expr->op;
688 fake_assign.type = EXPR_ASSIGNMENT;
689 fake_assign.left = expr->left;
690 fake_assign.right = stmt->expression;
692 __split_expr(&fake_assign);
694 } else {
695 __split_stmt(stmt);
697 __expr_stmt_count--;
698 return 1;
701 int in_condition(void)
703 return inside_condition;