extra: fix unknown += assignments
[smatch.git] / smatch_conditions.c
blob05534661aa087d84b882a0ccb53678647683718a
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 static void split_conditions(struct expression *expr);
66 static int is_logical_and(struct expression *expr)
68 if (expr->op == SPECIAL_LOGICAL_AND)
69 return 1;
70 return 0;
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))
79 tmp = expr->right;
80 else if (is_zero(expr->right))
81 tmp = expr->left;
82 else
83 return 0;
85 // "if (foo != 0)" is the same as "if (foo)"
86 if (expr->op == SPECIAL_NOTEQUAL) {
87 split_conditions(tmp);
88 return 1;
91 // "if (foo == 0)" is the same as "if (!foo)"
92 if (expr->op == SPECIAL_EQUAL) {
93 split_conditions(tmp);
94 __negate_cond_stacks();
95 return 1;
98 return 0;
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));
109 return 1;
111 return 0;
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;
122 struct statement *s;
124 last = last_ptr_list((struct ptr_list *)stmt->stmts);
125 if (last->type != STMT_EXPRESSION)
126 last = NULL;
127 else
128 expr = last->expression;
130 FOR_EACH_PTR(stmt->stmts, s) {
131 if (s != last)
132 __split_stmt(s);
133 } END_FOR_EACH_PTR(s);
134 split_conditions(expr);
135 return;
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();
145 return 1;
147 stmt = get_expression_statement(expr);
148 if (stmt) {
149 handle_compound_stmt(stmt);
150 return 1;
152 return 0;
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))
186 __and_cond_states();
187 else
188 __or_cond_states();
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);
200 free_stree(&new);
202 return ret;
206 * handle_select()
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;
226 struct sm_state *sm;
229 * Imagine we have this: if (a ? b : c) { ...
231 * The condition is true if "a" is true and "b" is true or
232 * "a" is false and "c" is true. It's false if "a" is true
233 * and "b" is false or "a" is false and "c" is false.
235 * The variable name "a_T_b_T" stands for "a true b true" etc.
237 * But if we know "b" is true then we can simpilify things.
238 * The condition is true if "a" is true or if "a" is false and
239 * "c" is true. The only way the condition can be false is if
240 * "a" is false and "c" is false.
242 * The remaining thing is the "a_T_b_fake". When we simplify
243 * the equations we have to take into consideration that other
244 * states may have changed that don't play into the true false
245 * equation. Take the following example:
246 * if ({
247 * (flags) = __raw_local_irq_save();
248 * _spin_trylock(lock) ? 1 :
249 * ({ raw_local_irq_restore(flags); 0; });
250 * })
251 * Smatch has to record that the irq flags were restored on the
252 * false path.
256 __save_pre_cond_states();
258 split_conditions(expr->conditional);
260 a_T = __copy_cond_true_states();
261 a_F = __copy_cond_false_states();
263 __push_cond_stacks();
264 __push_fake_cur_stree();
265 split_conditions(expr->cond_true);
266 __process_post_op_stack();
267 a_T_b_fake = __pop_fake_cur_stree();
268 a_T_b_T = combine_strees(a_T, a_T_b_fake, __pop_cond_true_stack());
269 a_T_b_F = combine_strees(a_T, a_T_b_fake, __pop_cond_false_stack());
271 __use_cond_false_states();
273 __push_cond_stacks();
274 __push_fake_cur_stree();
275 split_conditions(expr->cond_false);
276 a_F_c_fake = __pop_fake_cur_stree();
277 a_F_c_T = combine_strees(a_F, a_F_c_fake, __pop_cond_true_stack());
278 a_F_c_F = combine_strees(a_F, a_F_c_fake, __pop_cond_false_stack());
280 /* We have to restore the pre condition states so that
281 implied_condition_true() will use the right cur_stree */
282 __use_pre_cond_states();
284 if (implied_condition_true(expr->cond_true)) {
285 free_stree(&a_T_b_T);
286 free_stree(&a_T_b_F);
287 a_T_b_T = clone_stree(a_T);
288 overwrite_stree(a_T_b_fake, &a_T_b_T);
290 if (implied_condition_false(expr->cond_true)) {
291 free_stree(&a_T_b_T);
292 free_stree(&a_T_b_F);
293 a_T_b_F = clone_stree(a_T);
294 overwrite_stree(a_T_b_fake, &a_T_b_F);
296 if (implied_condition_true(expr->cond_false)) {
297 free_stree(&a_F_c_T);
298 free_stree(&a_F_c_F);
299 a_F_c_T = clone_stree(a_F);
300 overwrite_stree(a_F_c_fake, &a_F_c_T);
302 if (implied_condition_false(expr->cond_false)) {
303 free_stree(&a_F_c_T);
304 free_stree(&a_F_c_F);
305 a_F_c_F = clone_stree(a_F);
306 overwrite_stree(a_F_c_fake, &a_F_c_F);
309 merge_stree(&a_T_b_T, a_F_c_T);
310 merge_stree(&a_T_b_F, a_F_c_F);
312 __pop_cond_true_stack();
313 __pop_cond_false_stack();
314 __push_cond_stacks();
315 FOR_EACH_SM(a_T_b_T, sm) {
316 __set_true_false_sm(sm, NULL);
317 } END_FOR_EACH_SM(sm);
318 FOR_EACH_SM(a_T_b_F, sm) {
319 __set_true_false_sm(NULL, sm);
320 } END_FOR_EACH_SM(sm);
322 free_stree(&a_T_b_fake);
323 free_stree(&a_F_c_fake);
324 free_stree(&a_F_c_T);
325 free_stree(&a_F_c_F);
326 free_stree(&a_T_b_T);
327 free_stree(&a_T_b_F);
328 free_stree(&a_T);
329 free_stree(&a_F);
332 static int make_op_unsigned(int op)
334 switch (op) {
335 case '<':
336 return SPECIAL_UNSIGNED_LT;
337 case SPECIAL_LTE:
338 return SPECIAL_UNSIGNED_LTE;
339 case '>':
340 return SPECIAL_UNSIGNED_GT;
341 case SPECIAL_GTE:
342 return SPECIAL_UNSIGNED_GTE;
344 return op;
347 static void hackup_unsigned_compares(struct expression *expr)
349 if (expr->type != EXPR_COMPARE)
350 return;
352 if (type_unsigned(get_type(expr)))
353 expr->op = make_op_unsigned(expr->op);
356 static void split_conditions(struct expression *expr)
358 if (option_debug) {
359 char *cond = expr_to_str(expr);
361 sm_debug("%d in split_conditions (%s)\n", get_lineno(), cond);
362 free_string(cond);
365 expr = strip_expr(expr);
366 if (!expr)
367 return;
369 switch (expr->type) {
370 case EXPR_LOGICAL:
371 __pass_to_client(expr, LOGIC_HOOK);
372 handle_logical(expr);
373 return;
374 case EXPR_COMPARE:
375 hackup_unsigned_compares(expr);
376 if (handle_zero_comparisons(expr))
377 return;
378 break;
379 case EXPR_CALL:
380 if (ignore_builtin_expect(expr))
381 return;
382 break;
383 case EXPR_PREOP:
384 if (handle_preop(expr))
385 return;
386 break;
387 case EXPR_CONDITIONAL:
388 case EXPR_SELECT:
389 handle_select(expr);
390 return;
393 /* fixme: this should be in smatch_flow.c
394 but because of the funny stuff we do with conditions
395 it's awkward to put it there. We would need to
396 call CONDITION_HOOK in smatch_flow as well.
398 push_expression(&big_expression_stack, expr);
399 if (expr->type == EXPR_COMPARE) {
400 if (expr->left->type != EXPR_POSTOP)
401 __split_expr(expr->left);
402 if (expr->right->type != EXPR_POSTOP)
403 __split_expr(expr->right);
404 } else if (expr->type != EXPR_POSTOP) {
405 __split_expr(expr);
407 __pass_to_client(expr, CONDITION_HOOK);
408 if (expr->type == EXPR_COMPARE) {
409 if (expr->left->type == EXPR_POSTOP)
410 __split_expr(expr->left);
411 if (expr->right->type == EXPR_POSTOP)
412 __split_expr(expr->right);
413 } else if (expr->type == EXPR_POSTOP) {
414 __split_expr(expr);
416 __process_post_op_stack();
417 pop_expression(&big_expression_stack);
420 static int inside_condition;
421 void __split_whole_condition(struct expression *expr)
423 sm_debug("%d in __split_whole_condition\n", get_lineno());
424 inside_condition++;
425 __save_pre_cond_states();
426 __push_cond_stacks();
427 /* it's a hack, but it's sometimes handy to have this stuff
428 on the big_expression_stack. */
429 push_expression(&big_expression_stack, expr);
430 if (expr)
431 split_conditions(expr);
432 __use_cond_states();
433 __pass_to_client(expr, WHOLE_CONDITION_HOOK);
434 pop_expression(&big_expression_stack);
435 inside_condition--;
436 sm_debug("%d done __split_whole_condition\n", get_lineno());
439 void __handle_logic(struct expression *expr)
441 sm_debug("%d in __handle_logic\n", get_lineno());
442 inside_condition++;
443 __save_pre_cond_states();
444 __push_cond_stacks();
445 /* it's a hack, but it's sometimes handy to have this stuff
446 on the big_expression_stack. */
447 push_expression(&big_expression_stack, expr);
448 if (expr)
449 split_conditions(expr);
450 __use_cond_states();
451 __pass_to_client(expr, WHOLE_CONDITION_HOOK);
452 pop_expression(&big_expression_stack);
453 __merge_false_states();
454 inside_condition--;
455 sm_debug("%d done __handle_logic\n", get_lineno());
458 int is_condition(struct expression *expr)
461 expr = strip_expr(expr);
462 if (!expr)
463 return 0;
465 switch (expr->type) {
466 case EXPR_LOGICAL:
467 case EXPR_COMPARE:
468 return 1;
469 case EXPR_PREOP:
470 if (expr->op == '!')
471 return 1;
473 return 0;
476 int __handle_condition_assigns(struct expression *expr)
478 struct expression *right;
480 right = strip_expr(expr->right);
481 if (!is_condition(expr->right))
482 return 0;
484 sm_debug("%d in __handle_condition_assigns\n", get_lineno());
485 inside_condition++;
486 __save_pre_cond_states();
487 __push_cond_stacks();
488 /* it's a hack, but it's sometimes handy to have this stuff
489 on the big_expression_stack. */
490 push_expression(&big_expression_stack, right);
491 split_conditions(right);
492 __use_cond_states();
493 set_extra_expr_mod(expr->left, alloc_estate_sval(sval_type_val(get_type(expr->left), 1)));
494 __pass_to_client(right, WHOLE_CONDITION_HOOK);
495 pop_expression(&big_expression_stack);
496 inside_condition--;
497 sm_debug("%d done __handle_condition_assigns\n", get_lineno());
499 __push_true_states();
500 __use_false_states();
501 set_extra_expr_mod(expr->left, alloc_estate_sval(sval_type_val(get_type(expr->left), 0)));
502 __merge_true_states();
503 __pass_to_client(expr, ASSIGNMENT_HOOK);
504 return 1;
507 static int is_select_assign(struct expression *expr)
509 struct expression *right;
511 if (expr->op != '=')
512 return 0;
513 right = strip_expr(expr->right);
514 if (right->type == EXPR_CONDITIONAL)
515 return 1;
516 if (right->type == EXPR_SELECT)
517 return 1;
518 return 0;
521 static void set_fake_assign(struct expression *new,
522 struct expression *left, int op, struct expression *right)
525 new->pos = left->pos;
526 new->op = op;
527 new->type = EXPR_ASSIGNMENT;
528 new->left = left;
529 new->right = right;
532 int __handle_select_assigns(struct expression *expr)
534 struct expression *right;
535 struct stree *final_states = NULL;
536 struct sm_state *sm;
537 int is_true;
538 int is_false;
540 if (!is_select_assign(expr))
541 return 0;
542 sm_debug("%d in __handle_ternary_assigns\n", get_lineno());
543 right = strip_expr(expr->right);
545 is_true = implied_condition_true(right->conditional);
546 is_false = implied_condition_false(right->conditional);
548 /* hah hah. the ultra fake out */
549 __save_pre_cond_states();
550 __split_whole_condition(right->conditional);
552 if (!is_false) {
553 struct expression fake_expr;
555 if (right->cond_true)
556 set_fake_assign(&fake_expr, expr->left, expr->op, right->cond_true);
557 else
558 set_fake_assign(&fake_expr, expr->left, expr->op, right->conditional);
559 __split_expr(&fake_expr);
560 final_states = clone_stree(__get_cur_stree());
563 __use_false_states();
564 if (!is_true) {
565 struct expression fake_expr;
567 set_fake_assign(&fake_expr, expr->left, expr->op, right->cond_false);
568 __split_expr(&fake_expr);
569 merge_stree(&final_states, __get_cur_stree());
572 __use_pre_cond_states();
574 FOR_EACH_SM(final_states, sm) {
575 __set_sm(sm);
576 } END_FOR_EACH_SM(sm);
578 free_stree(&final_states);
580 sm_debug("%d done __handle_ternary_assigns\n", get_lineno());
582 return 1;
585 static struct statement *split_then_return_last(struct statement *stmt)
587 struct statement *tmp;
588 struct statement *last_stmt;
590 last_stmt = last_ptr_list((struct ptr_list *)stmt->stmts);
591 if (!last_stmt)
592 return NULL;
594 __push_scope_hooks();
595 FOR_EACH_PTR(stmt->stmts, tmp) {
596 if (tmp == last_stmt)
597 return last_stmt;
598 __split_stmt(tmp);
599 } END_FOR_EACH_PTR(tmp);
600 return NULL;
603 int __handle_expr_statement_assigns(struct expression *expr)
605 struct expression *right;
606 struct statement *stmt;
608 right = expr->right;
609 if (right->type == EXPR_PREOP && right->op == '(')
610 right = right->unop;
611 if (right->type != EXPR_STATEMENT)
612 return 0;
614 __expr_stmt_count++;
615 stmt = right->statement;
616 if (stmt->type == STMT_COMPOUND) {
617 struct statement *last_stmt;
618 struct expression fake_assign;
619 struct expression fake_expr_stmt;
621 last_stmt = split_then_return_last(stmt);
622 if (!last_stmt) {
623 __expr_stmt_count--;
624 return 0;
627 fake_expr_stmt.pos = last_stmt->pos;
628 fake_expr_stmt.type = EXPR_STATEMENT;
629 fake_expr_stmt.statement = last_stmt;
631 fake_assign.pos = last_stmt->pos;
632 fake_assign.op = expr->op;
633 fake_assign.type = EXPR_ASSIGNMENT;
634 fake_assign.left = expr->left;
635 fake_assign.right = &fake_expr_stmt;
637 __split_expr(&fake_assign);
639 __call_scope_hooks();
640 } else if (stmt->type == STMT_EXPRESSION) {
641 struct expression fake_assign;
643 fake_assign.pos = stmt->pos;
644 fake_assign.op = expr->op;
645 fake_assign.type = EXPR_ASSIGNMENT;
646 fake_assign.left = expr->left;
647 fake_assign.right = stmt->expression;
649 __split_expr(&fake_assign);
651 } else {
652 __split_stmt(stmt);
654 __expr_stmt_count--;
655 return 1;
658 int in_condition(void)
660 return inside_condition;