indenting: allow "break; case XXX:"
[smatch.git] / smatch_flow.c
blob4120ef70b655564f4fec2fa4e29fb7ef80e247ce
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
18 #define _GNU_SOURCE 1
19 #include <unistd.h>
20 #include <stdio.h>
21 #include "token.h"
22 #include "scope.h"
23 #include "smatch.h"
24 #include "smatch_expression_stacks.h"
25 #include "smatch_extra.h"
26 #include "smatch_slist.h"
28 int __in_fake_assign;
29 int final_pass;
30 int __inline_call;
31 struct expression *__inline_fn;
33 static int __smatch_lineno = 0;
35 static char *base_file;
36 static const char *filename;
37 static char *pathname;
38 static char *full_filename;
39 static char *cur_func;
40 static unsigned int loop_count;
41 static int last_goto_statement_handled;
42 int __expr_stmt_count;
43 int __in_function_def;
44 static struct expression_list *switch_expr_stack = NULL;
45 static struct expression_list *post_op_stack = NULL;
47 struct expression_list *big_expression_stack;
48 struct statement_list *big_statement_stack;
49 struct statement *__prev_stmt;
50 struct statement *__cur_stmt;
51 struct statement *__next_stmt;
52 int __in_pre_condition = 0;
53 int __bail_on_rest_of_function = 0;
54 static struct timeval fn_start_time;
55 char *get_function(void) { return cur_func; }
56 int get_lineno(void) { return __smatch_lineno; }
57 int inside_loop(void) { return !!loop_count; }
58 int definitely_inside_loop(void) { return !!(loop_count & ~0x80000000); }
59 struct expression *get_switch_expr(void) { return top_expression(switch_expr_stack); }
60 int in_expression_statement(void) { return !!__expr_stmt_count; }
62 static void split_symlist(struct symbol_list *sym_list);
63 static void split_declaration(struct symbol_list *sym_list);
64 static void split_expr_list(struct expression_list *expr_list);
65 static void add_inline_function(struct symbol *sym);
66 static void parse_inline(struct expression *expr);
68 int option_assume_loops = 0;
69 int option_known_conditions = 0;
70 int option_two_passes = 0;
71 struct symbol *cur_func_sym = NULL;
72 struct stree *global_states;
74 long long valid_ptr_min = 4096;
75 long long valid_ptr_max = 2117777777;
76 sval_t valid_ptr_min_sval = {
77 .type = &ptr_ctype,
78 {.value = 4096},
80 sval_t valid_ptr_max_sval = {
81 .type = &ptr_ctype,
82 {.value = LONG_MAX - 100000},
85 static void set_valid_ptr_max(void)
87 if (type_bits(&ptr_ctype) == 32)
88 valid_ptr_max = 2117777777;
89 else if (type_bits(&ptr_ctype) == 64)
90 valid_ptr_max = 2117777777777777777LL;
92 valid_ptr_max_sval.value = valid_ptr_max;
95 int outside_of_function(void)
97 return cur_func_sym == NULL;
100 const char *get_filename(void)
102 if (option_info)
103 return base_file;
104 if (option_full_path)
105 return full_filename;
106 return filename;
109 const char *get_base_file(void)
111 return base_file;
114 static void set_position(struct position pos)
116 int len;
117 static int prev_stream = -1;
119 if (pos.stream == 0 && pos.line == 0)
120 return;
122 __smatch_lineno = pos.line;
124 if (pos.stream == prev_stream)
125 return;
127 filename = stream_name(pos.stream);
129 free(full_filename);
130 pathname = getcwd(NULL, 0);
131 if (pathname) {
132 len = strlen(pathname) + 1 + strlen(filename) + 1;
133 full_filename = malloc(len);
134 snprintf(full_filename, len, "%s/%s", pathname, filename);
135 } else {
136 full_filename = alloc_string(filename);
138 free(pathname);
141 int is_assigned_call(struct expression *expr)
143 struct expression *tmp;
145 FOR_EACH_PTR_REVERSE(big_expression_stack, tmp) {
146 if (tmp->type == EXPR_ASSIGNMENT && tmp->op == '=' &&
147 strip_expr(tmp->right) == expr)
148 return 1;
149 if (tmp->pos.line < expr->pos.line)
150 return 0;
151 } END_FOR_EACH_PTR_REVERSE(tmp);
152 return 0;
155 static int is_inline_func(struct expression *expr)
157 if (expr->type != EXPR_SYMBOL || !expr->symbol)
158 return 0;
159 if (expr->symbol->ctype.modifiers & MOD_INLINE)
160 return 1;
161 return 0;
164 static int is_noreturn_func(struct expression *expr)
166 if (expr->type != EXPR_SYMBOL || !expr->symbol)
167 return 0;
168 if (expr->symbol->ctype.modifiers & MOD_NORETURN)
169 return 1;
170 return 0;
173 int inlinable(struct expression *expr)
175 struct symbol *sym;
176 struct statement *last_stmt = NULL;
178 if (__inline_fn) /* don't nest */
179 return 0;
181 if (expr->type != EXPR_SYMBOL || !expr->symbol)
182 return 0;
183 if (is_no_inline_function(expr->symbol->ident->name))
184 return 0;
185 sym = get_base_type(expr->symbol);
186 if (sym->stmt && sym->stmt->type == STMT_COMPOUND) {
187 if (ptr_list_size((struct ptr_list *)sym->stmt->stmts) > 10)
188 return 0;
189 if (sym->stmt->type != STMT_COMPOUND)
190 return 0;
191 last_stmt = last_ptr_list((struct ptr_list *)sym->stmt->stmts);
193 if (sym->inline_stmt && sym->inline_stmt->type == STMT_COMPOUND) {
194 if (ptr_list_size((struct ptr_list *)sym->inline_stmt->stmts) > 10)
195 return 0;
196 if (sym->inline_stmt->type != STMT_COMPOUND)
197 return 0;
198 last_stmt = last_ptr_list((struct ptr_list *)sym->inline_stmt->stmts);
201 if (!last_stmt)
202 return 0;
204 /* the magic numbers in this function are pulled out of my bum. */
205 if (last_stmt->pos.line > sym->pos.line + 20)
206 return 0;
208 return 1;
211 void __process_post_op_stack(void)
213 struct expression *expr;
215 FOR_EACH_PTR(post_op_stack, expr) {
216 __pass_to_client(expr, OP_HOOK);
217 } END_FOR_EACH_PTR(expr);
219 __free_ptr_list((struct ptr_list **)&post_op_stack);
222 static int handle_comma_assigns(struct expression *expr)
224 struct expression *right;
225 struct expression *assign;
227 right = strip_expr(expr->right);
228 if (right->type != EXPR_COMMA)
229 return 0;
231 __split_expr(right->left);
232 __process_post_op_stack();
234 assign = assign_expression(expr->left, right->right);
235 __split_expr(assign);
237 return 1;
240 void __split_expr(struct expression *expr)
242 if (!expr)
243 return;
245 // sm_msg(" Debug expr_type %d %s", expr->type, show_special(expr->op));
247 if (__in_fake_assign && expr->type != EXPR_ASSIGNMENT)
248 return;
249 if (__in_fake_assign >= 4) /* don't allow too much nesting */
250 return;
252 push_expression(&big_expression_stack, expr);
253 set_position(expr->pos);
254 __pass_to_client(expr, EXPR_HOOK);
256 switch (expr->type) {
257 case EXPR_PREOP:
258 if (expr->op == '*')
259 __pass_to_client(expr, DEREF_HOOK);
260 __split_expr(expr->unop);
261 __pass_to_client(expr, OP_HOOK);
262 break;
263 case EXPR_POSTOP:
264 __split_expr(expr->unop);
265 push_expression(&post_op_stack, expr);
266 break;
267 case EXPR_STATEMENT:
268 __expr_stmt_count++;
269 __split_stmt(expr->statement);
270 __expr_stmt_count--;
271 break;
272 case EXPR_LOGICAL:
273 case EXPR_COMPARE:
274 __pass_to_client(expr, LOGIC_HOOK);
275 __handle_logic(expr);
276 break;
277 case EXPR_BINOP:
278 __pass_to_client(expr, BINOP_HOOK);
279 case EXPR_COMMA:
280 __split_expr(expr->left);
281 __process_post_op_stack();
282 __split_expr(expr->right);
283 break;
284 case EXPR_ASSIGNMENT: {
285 struct expression *tmp;
287 if (!expr->right)
288 break;
290 __pass_to_client(expr, RAW_ASSIGNMENT_HOOK);
292 /* foo = !bar() */
293 if (__handle_condition_assigns(expr))
294 break;
295 /* foo = (x < 5 ? foo : 5); */
296 if (__handle_select_assigns(expr))
297 break;
298 /* foo = ({frob(); frob(); frob(); 1;}) */
299 if (__handle_expr_statement_assigns(expr))
300 break;
301 /* foo = (3, 4); */
302 if (handle_comma_assigns(expr))
303 break;
305 __split_expr(expr->right);
306 if (outside_of_function())
307 __pass_to_client(expr, GLOBAL_ASSIGNMENT_HOOK);
308 else
309 __pass_to_client(expr, ASSIGNMENT_HOOK);
311 __fake_struct_member_assignments(expr);
313 tmp = strip_expr(expr->right);
314 if (expr->op == '=' && tmp->type == EXPR_CALL) {
315 __pass_to_client(expr, CALL_ASSIGNMENT_HOOK);
316 if (!is_fake_call(tmp))
317 __pass_to_client(tmp, FUNCTION_CALL_HOOK_AFTER);
319 if (get_macro_name(tmp->pos) &&
320 get_macro_name(expr->pos) != get_macro_name(tmp->pos))
321 __pass_to_client(expr, MACRO_ASSIGNMENT_HOOK);
322 __split_expr(expr->left);
323 break;
325 case EXPR_DEREF:
326 __pass_to_client(expr, DEREF_HOOK);
327 __split_expr(expr->deref);
328 break;
329 case EXPR_SLICE:
330 __split_expr(expr->base);
331 break;
332 case EXPR_CAST:
333 case EXPR_FORCE_CAST:
334 __pass_to_client(expr, CAST_HOOK);
335 __split_expr(expr->cast_expression);
336 break;
337 case EXPR_SIZEOF:
338 if (expr->cast_expression)
339 __pass_to_client(strip_parens(expr->cast_expression),
340 SIZEOF_HOOK);
341 break;
342 case EXPR_OFFSETOF:
343 case EXPR_ALIGNOF:
344 evaluate_expression(expr);
345 break;
346 case EXPR_CONDITIONAL:
347 case EXPR_SELECT:
348 if (known_condition_true(expr->conditional)) {
349 __split_expr(expr->cond_true);
350 break;
352 if (known_condition_false(expr->conditional)) {
353 __split_expr(expr->cond_false);
354 break;
356 __pass_to_client(expr, SELECT_HOOK);
357 __split_whole_condition(expr->conditional);
358 __split_expr(expr->cond_true);
359 __push_true_states();
360 __use_false_states();
361 __split_expr(expr->cond_false);
362 __merge_true_states();
363 break;
364 case EXPR_CALL:
365 if (sym_name_is("__builtin_constant_p", expr->fn))
366 break;
367 split_expr_list(expr->args);
368 __split_expr(expr->fn);
369 if (is_inline_func(expr->fn))
370 add_inline_function(expr->fn->symbol);
371 if (inlinable(expr->fn))
372 __inline_call = 1;
373 __process_post_op_stack();
374 __pass_to_client(expr, FUNCTION_CALL_HOOK);
375 __inline_call = 0;
376 if (inlinable(expr->fn)) {
377 parse_inline(expr);
379 __pass_to_client(expr, CALL_HOOK_AFTER_INLINE);
380 if (!is_assigned_call(expr))
381 __pass_to_client(expr, FUNCTION_CALL_HOOK_AFTER);
382 if (is_noreturn_func(expr->fn))
383 nullify_path();
384 break;
385 case EXPR_INITIALIZER:
386 split_expr_list(expr->expr_list);
387 break;
388 case EXPR_IDENTIFIER:
389 __split_expr(expr->ident_expression);
390 break;
391 case EXPR_INDEX:
392 __split_expr(expr->idx_expression);
393 break;
394 case EXPR_POS:
395 __split_expr(expr->init_expr);
396 break;
397 case EXPR_SYMBOL:
398 __pass_to_client(expr, SYM_HOOK);
399 break;
400 case EXPR_STRING:
401 __pass_to_client(expr, STRING_HOOK);
402 break;
403 default:
404 break;
406 pop_expression(&big_expression_stack);
409 static int is_forever_loop(struct statement *stmt)
411 struct expression *expr;
413 expr = strip_expr(stmt->iterator_pre_condition);
414 if (!expr)
415 expr = stmt->iterator_post_condition;
416 if (!expr) {
417 /* this is a for(;;) loop... */
418 return 1;
421 if (expr->type == EXPR_VALUE && expr->value == 1)
422 return 1;
424 return 0;
427 static int loop_num;
428 static char *get_loop_name(int num)
430 char buf[256];
432 snprintf(buf, 255, "-loop%d", num);
433 buf[255] = '\0';
434 return alloc_sname(buf);
438 * Pre Loops are while and for loops.
440 static void handle_pre_loop(struct statement *stmt)
442 int once_through; /* we go through the loop at least once */
443 struct sm_state *extra_sm = NULL;
444 int unchanged = 0;
445 char *loop_name;
446 struct stree *stree = NULL;
447 struct sm_state *sm = NULL;
449 loop_name = get_loop_name(loop_num);
450 loop_num++;
452 __split_stmt(stmt->iterator_pre_statement);
454 once_through = implied_condition_true(stmt->iterator_pre_condition);
456 loop_count++;
457 __push_continues();
458 __push_breaks();
460 __merge_gotos(loop_name);
462 extra_sm = __extra_handle_canonical_loops(stmt, &stree);
463 __in_pre_condition++;
464 __pass_to_client(stmt, PRELOOP_HOOK);
465 __split_whole_condition(stmt->iterator_pre_condition);
466 __in_pre_condition--;
467 FOR_EACH_SM(stree, sm) {
468 set_state(sm->owner, sm->name, sm->sym, sm->state);
469 } END_FOR_EACH_SM(sm);
470 free_stree(&stree);
471 if (extra_sm)
472 extra_sm = get_sm_state(extra_sm->owner, extra_sm->name, extra_sm->sym);
474 if (option_assume_loops)
475 once_through = 1;
477 __split_stmt(stmt->iterator_statement);
478 __warn_on_silly_pre_loops();
479 if (is_forever_loop(stmt)) {
480 __merge_continues();
481 __save_gotos(loop_name);
483 __push_fake_cur_stree();
484 __split_stmt(stmt->iterator_post_statement);
485 stree = __pop_fake_cur_stree();
487 __discard_false_states();
488 __use_breaks();
490 if (!__path_is_null())
491 __merge_stree_into_cur(stree);
492 free_stree(&stree);
493 } else {
494 __merge_continues();
495 unchanged = __iterator_unchanged(extra_sm);
496 __split_stmt(stmt->iterator_post_statement);
497 __save_gotos(loop_name);
498 __in_pre_condition++;
499 __split_whole_condition(stmt->iterator_pre_condition);
500 __in_pre_condition--;
501 nullify_path();
502 __merge_false_states();
503 if (once_through)
504 __discard_false_states();
505 else
506 __merge_false_states();
508 if (extra_sm && unchanged)
509 __extra_pre_loop_hook_after(extra_sm,
510 stmt->iterator_post_statement,
511 stmt->iterator_pre_condition);
512 __merge_breaks();
514 loop_count--;
518 * Post loops are do {} while();
520 static void handle_post_loop(struct statement *stmt)
522 char *loop_name;
524 loop_name = get_loop_name(loop_num);
525 loop_num++;
526 loop_count++;
528 __push_continues();
529 __push_breaks();
530 __merge_gotos(loop_name);
531 __split_stmt(stmt->iterator_statement);
532 __merge_continues();
533 if (!is_zero(stmt->iterator_post_condition))
534 __save_gotos(loop_name);
536 if (is_forever_loop(stmt)) {
537 __use_breaks();
538 } else {
539 __split_whole_condition(stmt->iterator_post_condition);
540 __use_false_states();
541 __merge_breaks();
543 loop_count--;
546 static int empty_statement(struct statement *stmt)
548 if (!stmt)
549 return 0;
550 if (stmt->type == STMT_EXPRESSION && !stmt->expression)
551 return 1;
552 return 0;
555 static int last_stmt_on_same_line(void)
557 struct statement *stmt;
558 int i = 0;
560 FOR_EACH_PTR_REVERSE(big_statement_stack, stmt) {
561 if (!i++)
562 continue;
563 if (stmt->pos.line == get_lineno())
564 return 1;
565 return 0;
566 } END_FOR_EACH_PTR_REVERSE(stmt);
567 return 0;
570 static void split_asm_constraints(struct expression_list *expr_list)
572 struct expression *expr;
573 int state = 0;
575 FOR_EACH_PTR(expr_list, expr) {
576 switch (state) {
577 case 0: /* identifier */
578 case 1: /* constraint */
579 state++;
580 continue;
581 case 2: /* expression */
582 state = 0;
583 __split_expr(expr);
584 continue;
586 } END_FOR_EACH_PTR(expr);
589 static int is_case_val(struct statement *stmt, sval_t sval)
591 sval_t case_sval;
593 if (stmt->type != STMT_CASE)
594 return 0;
595 if (!stmt->case_expression) {
596 __set_default();
597 return 1;
599 if (!get_value(stmt->case_expression, &case_sval))
600 return 0;
601 if (case_sval.value == sval.value)
602 return 1;
603 return 0;
606 static void split_known_switch(struct statement *stmt, sval_t sval)
608 struct statement *tmp;
610 __split_expr(stmt->switch_expression);
612 push_expression(&switch_expr_stack, stmt->switch_expression);
613 __save_switch_states(top_expression(switch_expr_stack));
614 nullify_path();
615 __push_default();
616 __push_breaks();
618 stmt = stmt->switch_statement;
620 __push_scope_hooks();
621 FOR_EACH_PTR(stmt->stmts, tmp) {
622 __smatch_lineno = tmp->pos.line;
623 if (is_case_val(tmp, sval)) {
624 __merge_switches(top_expression(switch_expr_stack),
625 stmt->case_expression);
626 __pass_case_to_client(top_expression(switch_expr_stack),
627 stmt->case_expression);
629 if (__path_is_null())
630 continue;
631 __split_stmt(tmp);
632 if (__path_is_null()) {
633 __set_default();
634 goto out;
636 } END_FOR_EACH_PTR(tmp);
637 out:
638 __call_scope_hooks();
639 if (!__pop_default())
640 __merge_switches(top_expression(switch_expr_stack),
641 NULL);
642 __discard_switches();
643 __merge_breaks();
644 pop_expression(&switch_expr_stack);
647 static int taking_too_long(void)
649 int ms;
651 ms = ms_since(&fn_start_time);
652 if (ms > 1000 * 60 * 5) /* five minutes */
653 return 1;
654 return 0;
657 static int is_last_stmt(struct statement *cur_stmt)
659 struct symbol *fn = get_base_type(cur_func_sym);
660 struct statement *stmt;
662 if (!fn)
663 return 0;
664 stmt = fn->stmt;
665 if (!stmt)
666 stmt = fn->inline_stmt;
667 if (!stmt || stmt->type != STMT_COMPOUND)
668 return 0;
669 stmt = last_ptr_list((struct ptr_list *)stmt->stmts);
670 if (stmt && stmt->type == STMT_LABEL)
671 stmt = stmt->label_statement;
672 if (stmt == cur_stmt)
673 return 1;
674 return 0;
677 static void handle_backward_goto(struct statement *goto_stmt)
679 const char *goto_name, *label_name;
680 struct statement *func_stmt;
681 struct symbol *base_type = get_base_type(cur_func_sym);
682 struct statement *tmp;
683 int found = 0;
685 if (!option_info)
686 return;
687 if (last_goto_statement_handled)
688 return;
689 last_goto_statement_handled = 1;
691 if (!goto_stmt->goto_label ||
692 goto_stmt->goto_label->type != SYM_LABEL ||
693 !goto_stmt->goto_label->ident)
694 return;
695 goto_name = goto_stmt->goto_label->ident->name;
697 func_stmt = base_type->stmt;
698 if (!func_stmt)
699 func_stmt = base_type->inline_stmt;
700 if (!func_stmt)
701 return;
702 if (func_stmt->type != STMT_COMPOUND)
703 return;
705 FOR_EACH_PTR(func_stmt->stmts, tmp) {
706 if (!found) {
707 if (tmp->type != STMT_LABEL)
708 continue;
709 if (!tmp->label_identifier ||
710 tmp->label_identifier->type != SYM_LABEL ||
711 !tmp->label_identifier->ident)
712 continue;
713 label_name = tmp->label_identifier->ident->name;
714 if (strcmp(goto_name, label_name) != 0)
715 continue;
716 found = 1;
718 __split_stmt(tmp);
719 } END_FOR_EACH_PTR(tmp);
722 static void fake_a_return(void)
724 struct symbol *return_type;
726 nullify_path();
727 __unnullify_path();
729 return_type = get_real_base_type(cur_func_sym);
730 return_type = get_real_base_type(return_type);
731 if (return_type != &void_ctype) {
732 __pass_to_client(unknown_value_expression(NULL), RETURN_HOOK);
733 nullify_path();
736 __pass_to_client(cur_func_sym, END_FUNC_HOOK);
737 __pass_to_client(cur_func_sym, AFTER_FUNC_HOOK);
740 static void split_compound(struct statement *stmt)
742 struct statement *prev = NULL;
743 struct statement *cur = NULL;
744 struct statement *next;
746 __push_scope_hooks();
748 FOR_EACH_PTR(stmt->stmts, next) {
749 if (cur) {
750 __prev_stmt = prev;
751 __next_stmt = next;
752 __cur_stmt = cur;
753 __split_stmt(cur);
755 prev = cur;
756 cur = next;
757 } END_FOR_EACH_PTR(next);
758 if (cur) {
759 __prev_stmt = prev;
760 __cur_stmt = cur;
761 __next_stmt = NULL;
762 __split_stmt(cur);
765 __call_scope_hooks();
768 void __split_stmt(struct statement *stmt)
770 sval_t sval;
772 if (!stmt)
773 goto out;
775 if (__bail_on_rest_of_function || out_of_memory() || taking_too_long()) {
776 static char *printed = NULL;
778 __bail_on_rest_of_function = 1;
779 if (printed != cur_func)
780 sm_msg("Function too hairy. Giving up.");
781 fake_a_return();
782 final_pass = 0; /* turn off sm_msg() from here */
783 printed = cur_func;
784 return;
787 add_ptr_list(&big_statement_stack, stmt);
788 free_expression_stack(&big_expression_stack);
789 set_position(stmt->pos);
790 __pass_to_client(stmt, STMT_HOOK);
792 switch (stmt->type) {
793 case STMT_DECLARATION:
794 split_declaration(stmt->declaration);
795 break;
796 case STMT_RETURN:
797 __split_expr(stmt->ret_value);
798 __pass_to_client(stmt->ret_value, RETURN_HOOK);
799 __process_post_op_stack();
800 nullify_path();
801 break;
802 case STMT_EXPRESSION:
803 __split_expr(stmt->expression);
804 break;
805 case STMT_COMPOUND:
806 split_compound(stmt);
807 break;
808 case STMT_IF:
809 if (known_condition_true(stmt->if_conditional)) {
810 __split_stmt(stmt->if_true);
811 break;
813 if (known_condition_false(stmt->if_conditional)) {
814 __split_stmt(stmt->if_false);
815 break;
817 if (option_known_conditions &&
818 implied_condition_true(stmt->if_conditional)) {
819 sm_info("this condition is true.");
820 __split_stmt(stmt->if_true);
821 break;
823 if (option_known_conditions &&
824 implied_condition_false(stmt->if_conditional)) {
825 sm_info("this condition is false.");
826 __split_stmt(stmt->if_false);
827 break;
829 __split_whole_condition(stmt->if_conditional);
830 __split_stmt(stmt->if_true);
831 if (empty_statement(stmt->if_true) &&
832 last_stmt_on_same_line() &&
833 !get_macro_name(stmt->if_true->pos))
834 sm_msg("warn: if();");
835 __push_true_states();
836 __use_false_states();
837 __split_stmt(stmt->if_false);
838 __merge_true_states();
839 break;
840 case STMT_ITERATOR:
841 if (stmt->iterator_pre_condition)
842 handle_pre_loop(stmt);
843 else if (stmt->iterator_post_condition)
844 handle_post_loop(stmt);
845 else {
846 // these are for(;;) type loops.
847 handle_pre_loop(stmt);
849 break;
850 case STMT_SWITCH:
851 if (get_value(stmt->switch_expression, &sval)) {
852 split_known_switch(stmt, sval);
853 break;
855 __split_expr(stmt->switch_expression);
856 push_expression(&switch_expr_stack, stmt->switch_expression);
857 __save_switch_states(top_expression(switch_expr_stack));
858 nullify_path();
859 __push_default();
860 __push_breaks();
861 __split_stmt(stmt->switch_statement);
862 if (!__pop_default())
863 __merge_switches(top_expression(switch_expr_stack),
864 NULL);
865 __discard_switches();
866 __merge_breaks();
867 pop_expression(&switch_expr_stack);
868 break;
869 case STMT_CASE:
870 __merge_switches(top_expression(switch_expr_stack),
871 stmt->case_expression);
872 __pass_case_to_client(top_expression(switch_expr_stack),
873 stmt->case_expression);
874 if (!stmt->case_expression)
875 __set_default();
876 __split_expr(stmt->case_expression);
877 __split_expr(stmt->case_to);
878 __split_stmt(stmt->case_statement);
879 break;
880 case STMT_LABEL:
881 if (stmt->label_identifier &&
882 stmt->label_identifier->type == SYM_LABEL &&
883 stmt->label_identifier->ident) {
884 loop_count |= 0x80000000;
885 __merge_gotos(stmt->label_identifier->ident->name);
887 __split_stmt(stmt->label_statement);
888 break;
889 case STMT_GOTO:
890 __split_expr(stmt->goto_expression);
891 if (stmt->goto_label && stmt->goto_label->type == SYM_NODE) {
892 if (!strcmp(stmt->goto_label->ident->name, "break")) {
893 __process_breaks();
894 } else if (!strcmp(stmt->goto_label->ident->name,
895 "continue")) {
896 __process_continues();
898 } else if (stmt->goto_label &&
899 stmt->goto_label->type == SYM_LABEL &&
900 stmt->goto_label->ident) {
901 __save_gotos(stmt->goto_label->ident->name);
903 nullify_path();
904 if (is_last_stmt(stmt))
905 handle_backward_goto(stmt);
906 break;
907 case STMT_NONE:
908 break;
909 case STMT_ASM:
910 __pass_to_client(stmt, ASM_HOOK);
911 __split_expr(stmt->asm_string);
912 split_asm_constraints(stmt->asm_outputs);
913 split_asm_constraints(stmt->asm_inputs);
914 split_asm_constraints(stmt->asm_clobbers);
915 break;
916 case STMT_CONTEXT:
917 break;
918 case STMT_RANGE:
919 __split_expr(stmt->range_expression);
920 __split_expr(stmt->range_low);
921 __split_expr(stmt->range_high);
922 break;
924 __pass_to_client(stmt, STMT_HOOK_AFTER);
925 out:
926 __process_post_op_stack();
929 static void split_expr_list(struct expression_list *expr_list)
931 struct expression *expr;
933 FOR_EACH_PTR(expr_list, expr) {
934 __split_expr(expr);
935 __process_post_op_stack();
936 } END_FOR_EACH_PTR(expr);
939 static void split_sym(struct symbol *sym)
941 if (!sym)
942 return;
943 if (!(sym->namespace & NS_SYMBOL))
944 return;
946 __split_stmt(sym->stmt);
947 __split_expr(sym->array_size);
948 split_symlist(sym->arguments);
949 split_symlist(sym->symbol_list);
950 __split_stmt(sym->inline_stmt);
951 split_symlist(sym->inline_symbol_list);
954 static void split_symlist(struct symbol_list *sym_list)
956 struct symbol *sym;
958 FOR_EACH_PTR(sym_list, sym) {
959 split_sym(sym);
960 } END_FOR_EACH_PTR(sym);
963 typedef void (fake_cb)(struct expression *expr);
965 static int member_to_number(struct expression *expr, struct ident *member)
967 struct symbol *type, *tmp;
968 char *name;
969 int i;
971 if (!member)
972 return -1;
973 name = member->name;
975 type = get_type(expr);
976 if (!type || type->type != SYM_STRUCT)
977 return -1;
979 i = -1;
980 FOR_EACH_PTR(type->symbol_list, tmp) {
981 i++;
982 if (!tmp->ident)
983 continue;
984 if (strcmp(name, tmp->ident->name) == 0)
985 return i;
986 } END_FOR_EACH_PTR(tmp);
987 return -1;
990 static struct ident *number_to_member(struct expression *expr, int num)
992 struct symbol *type, *member;
993 int i = 0;
995 type = get_type(expr);
996 if (!type || type->type != SYM_STRUCT)
997 return NULL;
999 FOR_EACH_PTR(type->symbol_list, member) {
1000 if (i == num)
1001 return member->ident;
1002 i++;
1003 } END_FOR_EACH_PTR(member);
1004 return NULL;
1007 static void fake_element_assigns_helper(struct expression *array, struct expression_list *expr_list, fake_cb *fake_cb);
1009 struct member_set {
1010 struct ident *ident;
1011 int set;
1014 static struct member_set *alloc_member_set(struct symbol *type)
1016 struct member_set *member_set;
1017 struct symbol *member;
1018 int member_count;
1019 int member_idx;
1021 member_count = ptr_list_size((struct ptr_list *)type->symbol_list);
1022 member_set = malloc(member_count * sizeof(*member_set));
1023 member_idx = 0;
1024 FOR_EACH_PTR(type->symbol_list, member) {
1025 member_set[member_idx].ident = member->ident;
1026 member_set[member_idx].set = 0;
1027 member_idx++;
1028 } END_FOR_EACH_PTR(member);
1030 return member_set;
1033 static void mark_member_as_set(struct symbol *type, struct member_set *member_set, struct ident *ident)
1035 int member_count = ptr_list_size((struct ptr_list *)type->symbol_list);
1036 int i;
1038 for (i = 0; i < member_count; i++) {
1039 if (member_set[i].ident == ident) {
1040 member_set[i].set = 1;
1041 return;
1044 // crap. this is buggy.
1045 // sm_msg("internal smatch error in initializer %s.%s", type->ident->name, ident->name);
1048 static void set_inner_struct_members(struct expression *expr, struct symbol *member)
1050 struct expression *edge_member, *assign;
1051 struct symbol *base = get_real_base_type(member);
1052 struct symbol *tmp;
1054 if (member->ident)
1055 expr = member_expression(expr, '.', member->ident);
1057 FOR_EACH_PTR(base->symbol_list, tmp) {
1058 struct symbol *type;
1060 type = get_real_base_type(tmp);
1061 if (!type)
1062 continue;
1064 if (tmp->ident) {
1065 edge_member = member_expression(expr, '.', tmp->ident);
1066 if (get_state_expr(SMATCH_EXTRA, edge_member))
1067 continue;
1070 if (type->type == SYM_UNION || type->type == SYM_STRUCT) {
1071 set_inner_struct_members(expr, tmp);
1072 continue;
1075 if (!tmp->ident)
1076 continue;
1078 assign = assign_expression(edge_member, zero_expr());
1079 __split_expr(assign);
1080 } END_FOR_EACH_PTR(tmp);
1085 static void set_unset_to_zero(struct symbol *type, struct expression *expr)
1087 struct symbol *tmp;
1088 struct expression *member, *assign;
1089 int op = '*';
1091 if (expr->type == EXPR_PREOP && expr->op == '&') {
1092 expr = strip_expr(expr->unop);
1093 op = '.';
1096 FOR_EACH_PTR(type->symbol_list, tmp) {
1097 type = get_real_base_type(tmp);
1098 if (!type)
1099 continue;
1101 if (tmp->ident) {
1102 member = member_expression(expr, op, tmp->ident);
1103 if (get_state_expr(SMATCH_EXTRA, member))
1104 continue;
1107 if (type->type == SYM_UNION || type->type == SYM_STRUCT) {
1108 set_inner_struct_members(expr, tmp);
1109 continue;
1111 if (type->type == SYM_ARRAY)
1112 continue;
1113 if (!tmp->ident)
1114 continue;
1116 assign = assign_expression(member, zero_expr());
1117 __split_expr(assign);
1118 } END_FOR_EACH_PTR(tmp);
1121 static void fake_member_assigns_helper(struct expression *symbol, struct expression_list *members, fake_cb *fake_cb)
1123 struct expression *deref, *assign, *tmp;
1124 struct symbol *struct_type, *type;
1125 struct ident *member;
1126 int member_idx;
1127 struct member_set *member_set;
1129 struct_type = get_type(symbol);
1130 if (!struct_type ||
1131 (struct_type->type != SYM_STRUCT && struct_type->type != SYM_UNION))
1132 return;
1134 member_set = alloc_member_set(struct_type);
1136 member_idx = 0;
1137 FOR_EACH_PTR(members, tmp) {
1138 member = number_to_member(symbol, member_idx);
1139 while (tmp->type == EXPR_IDENTIFIER) {
1140 member = tmp->expr_ident;
1141 member_idx = member_to_number(symbol, member);
1142 tmp = tmp->ident_expression;
1144 mark_member_as_set(struct_type, member_set, member);
1145 member_idx++;
1146 deref = member_expression(symbol, '.', member);
1147 if (tmp->type == EXPR_INITIALIZER) {
1148 type = get_type(deref);
1149 if (type && type->type == SYM_ARRAY)
1150 fake_element_assigns_helper(deref, tmp->expr_list, fake_cb);
1151 else
1152 fake_member_assigns_helper(deref, tmp->expr_list, fake_cb);
1153 } else {
1154 assign = assign_expression(deref, tmp);
1155 fake_cb(assign);
1157 } END_FOR_EACH_PTR(tmp);
1159 set_unset_to_zero(struct_type, symbol);
1162 static void fake_member_assigns(struct symbol *sym, fake_cb *fake_cb)
1164 fake_member_assigns_helper(symbol_expression(sym),
1165 sym->initializer->expr_list, fake_cb);
1168 static void fake_element_assigns_helper(struct expression *array, struct expression_list *expr_list, fake_cb *fake_cb)
1170 struct expression *offset, *binop, *assign, *tmp;
1171 struct symbol *type;
1172 int idx;
1174 idx = 0;
1175 FOR_EACH_PTR(expr_list, tmp) {
1176 if (tmp->type == EXPR_INDEX) {
1177 if (tmp->idx_from != tmp->idx_to)
1178 return;
1179 idx = tmp->idx_from;
1180 if (!tmp->idx_expression)
1181 goto next;
1182 tmp = tmp->idx_expression;
1184 offset = value_expr(idx);
1185 binop = array_element_expression(array, offset);
1186 if (tmp->type == EXPR_INITIALIZER) {
1187 type = get_type(binop);
1188 if (type && type->type == SYM_ARRAY)
1189 fake_element_assigns_helper(binop, tmp->expr_list, fake_cb);
1190 else
1191 fake_member_assigns_helper(binop, tmp->expr_list, fake_cb);
1192 } else {
1193 assign = assign_expression(binop, tmp);
1194 fake_cb(assign);
1196 next:
1197 idx++;
1198 } END_FOR_EACH_PTR(tmp);
1201 static void fake_element_assigns(struct symbol *sym, fake_cb *fake_cb)
1203 fake_element_assigns_helper(symbol_expression(sym), sym->initializer->expr_list, fake_cb);
1206 static void fake_assign_expr(struct symbol *sym)
1208 struct expression *assign, *symbol;
1210 symbol = symbol_expression(sym);
1211 assign = assign_expression(symbol, sym->initializer);
1212 __split_expr(assign);
1215 static void call_split_expr(struct expression *expr)
1217 __split_expr(expr);
1220 static void do_initializer_stuff(struct symbol *sym)
1222 if (!sym->initializer)
1223 return;
1225 if (sym->initializer->type == EXPR_INITIALIZER) {
1226 if (get_real_base_type(sym)->type == SYM_ARRAY)
1227 fake_element_assigns(sym, call_split_expr);
1228 else
1229 fake_member_assigns(sym, call_split_expr);
1230 } else {
1231 fake_assign_expr(sym);
1235 static void split_declaration(struct symbol_list *sym_list)
1237 struct symbol *sym;
1239 FOR_EACH_PTR(sym_list, sym) {
1240 __pass_to_client(sym, DECLARATION_HOOK);
1241 do_initializer_stuff(sym);
1242 split_sym(sym);
1243 } END_FOR_EACH_PTR(sym);
1246 static void call_global_assign_hooks(struct expression *assign)
1248 __pass_to_client(assign, GLOBAL_ASSIGNMENT_HOOK);
1251 static void fake_global_assign(struct symbol *sym)
1253 struct expression *assign, *symbol;
1255 if (get_real_base_type(sym)->type == SYM_ARRAY) {
1256 if (sym->initializer && sym->initializer->type == EXPR_INITIALIZER) {
1257 fake_element_assigns(sym, call_global_assign_hooks);
1258 } else if (sym->initializer) {
1259 symbol = symbol_expression(sym);
1260 assign = assign_expression(symbol, sym->initializer);
1261 __pass_to_client(assign, GLOBAL_ASSIGNMENT_HOOK);
1262 } else {
1263 fake_element_assigns_helper(symbol_expression(sym), NULL, call_global_assign_hooks);
1265 } else if (get_real_base_type(sym)->type == SYM_STRUCT) {
1266 if (sym->initializer && sym->initializer->type == EXPR_INITIALIZER) {
1267 fake_member_assigns(sym, call_global_assign_hooks);
1268 } else if (sym->initializer) {
1269 symbol = symbol_expression(sym);
1270 assign = assign_expression(symbol, sym->initializer);
1271 __pass_to_client(assign, GLOBAL_ASSIGNMENT_HOOK);
1272 } else {
1273 fake_member_assigns_helper(symbol_expression(sym), NULL, call_global_assign_hooks);
1275 } else {
1276 symbol = symbol_expression(sym);
1277 if (sym->initializer)
1278 assign = assign_expression(symbol, sym->initializer);
1279 else
1280 assign = assign_expression(symbol, zero_expr());
1281 __pass_to_client(assign, GLOBAL_ASSIGNMENT_HOOK);
1285 static void start_function_definition(struct symbol *sym)
1287 __in_function_def = 1;
1288 __pass_to_client(sym, FUNC_DEF_HOOK);
1289 __in_function_def = 0;
1290 __pass_to_client(sym, AFTER_DEF_HOOK);
1294 static void split_function(struct symbol *sym)
1296 struct symbol *base_type = get_base_type(sym);
1298 if (!base_type->stmt && !base_type->inline_stmt)
1299 return;
1301 gettimeofday(&fn_start_time, NULL);
1302 cur_func_sym = sym;
1303 if (sym->ident)
1304 cur_func = sym->ident->name;
1305 __smatch_lineno = sym->pos.line;
1306 loop_count = 0;
1307 last_goto_statement_handled = 0;
1308 sm_debug("new function: %s\n", cur_func);
1309 __stree_id = 0;
1310 if (option_two_passes) {
1311 __unnullify_path();
1312 loop_num = 0;
1313 final_pass = 0;
1314 start_function_definition(sym);
1315 __split_stmt(base_type->stmt);
1316 __split_stmt(base_type->inline_stmt);
1317 nullify_path();
1319 __unnullify_path();
1320 loop_num = 0;
1321 final_pass = 1;
1322 start_function_definition(sym);
1323 __split_stmt(base_type->stmt);
1324 __split_stmt(base_type->inline_stmt);
1325 __pass_to_client(sym, END_FUNC_HOOK);
1326 __pass_to_client(sym, AFTER_FUNC_HOOK);
1328 clear_all_states();
1329 cur_func_sym = NULL;
1330 cur_func = NULL;
1331 free_data_info_allocs();
1332 free_expression_stack(&switch_expr_stack);
1333 __free_ptr_list((struct ptr_list **)&big_statement_stack);
1334 __bail_on_rest_of_function = 0;
1337 static void parse_inline(struct expression *call)
1339 struct symbol *base_type;
1340 int loop_num_bak = loop_num;
1341 int final_pass_bak = final_pass;
1342 char *cur_func_bak = cur_func;
1343 struct statement_list *big_statement_stack_bak = big_statement_stack;
1344 struct expression_list *big_expression_stack_bak = big_expression_stack;
1345 struct expression_list *switch_expr_stack_bak = switch_expr_stack;
1346 struct symbol *cur_func_sym_bak = cur_func_sym;
1348 __pass_to_client(call, INLINE_FN_START);
1349 final_pass = 0; /* don't print anything */
1350 __inline_fn = call;
1352 base_type = get_base_type(call->fn->symbol);
1353 cur_func_sym = call->fn->symbol;
1354 if (call->fn->symbol->ident)
1355 cur_func = call->fn->symbol->ident->name;
1356 else
1357 cur_func = NULL;
1358 set_position(call->fn->symbol->pos);
1360 save_all_states();
1361 big_statement_stack = NULL;
1362 big_expression_stack = NULL;
1363 switch_expr_stack = NULL;
1365 sm_debug("inline function: %s\n", cur_func);
1366 __unnullify_path();
1367 loop_num = 0;
1368 start_function_definition(call->fn->symbol);
1369 __split_stmt(base_type->stmt);
1370 __split_stmt(base_type->inline_stmt);
1371 __pass_to_client(call->fn->symbol, END_FUNC_HOOK);
1372 __pass_to_client(call->fn->symbol, AFTER_FUNC_HOOK);
1374 free_expression_stack(&switch_expr_stack);
1375 __free_ptr_list((struct ptr_list **)&big_statement_stack);
1376 nullify_path();
1377 free_goto_stack();
1379 loop_num = loop_num_bak;
1380 final_pass = final_pass_bak;
1381 cur_func_sym = cur_func_sym_bak;
1382 cur_func = cur_func_bak;
1383 big_statement_stack = big_statement_stack_bak;
1384 big_expression_stack = big_expression_stack_bak;
1385 switch_expr_stack = switch_expr_stack_bak;
1387 restore_all_states();
1388 set_position(call->pos);
1389 __inline_fn = NULL;
1390 __pass_to_client(call, INLINE_FN_END);
1393 static struct symbol_list *inlines_called;
1394 static void add_inline_function(struct symbol *sym)
1396 static struct symbol_list *already_added;
1397 struct symbol *tmp;
1399 FOR_EACH_PTR(already_added, tmp) {
1400 if (tmp == sym)
1401 return;
1402 } END_FOR_EACH_PTR(tmp);
1404 add_ptr_list(&already_added, sym);
1405 add_ptr_list(&inlines_called, sym);
1408 static void process_inlines(void)
1410 struct symbol *tmp;
1412 FOR_EACH_PTR(inlines_called, tmp) {
1413 split_function(tmp);
1414 } END_FOR_EACH_PTR(tmp);
1415 free_ptr_list(&inlines_called);
1418 static struct symbol *get_last_scoped_symbol(struct symbol_list *big_list, int use_static)
1420 struct symbol *sym;
1422 FOR_EACH_PTR_REVERSE(big_list, sym) {
1423 if (!sym->scope)
1424 continue;
1425 if (use_static && sym->ctype.modifiers & MOD_STATIC)
1426 return sym;
1427 if (!use_static && !(sym->ctype.modifiers & MOD_STATIC))
1428 return sym;
1429 } END_FOR_EACH_PTR_REVERSE(sym);
1431 return NULL;
1434 static void split_inlines_in_scope(struct symbol *sym)
1436 struct symbol *base;
1437 struct symbol_list *scope_list;
1438 int stream;
1440 scope_list = sym->scope->symbols;
1441 stream = sym->pos.stream;
1443 /* find the last static symbol in the file */
1444 FOR_EACH_PTR_REVERSE(scope_list, sym) {
1445 if (sym->pos.stream != stream)
1446 continue;
1447 if (sym->type != SYM_NODE)
1448 continue;
1449 base = get_base_type(sym);
1450 if (!base)
1451 continue;
1452 if (base->type != SYM_FN)
1453 continue;
1454 if (!base->inline_stmt)
1455 continue;
1456 add_inline_function(sym);
1457 } END_FOR_EACH_PTR_REVERSE(sym);
1459 process_inlines();
1462 static void split_inlines(struct symbol_list *sym_list)
1464 struct symbol *sym;
1466 sym = get_last_scoped_symbol(sym_list, 0);
1467 if (sym)
1468 split_inlines_in_scope(sym);
1469 sym = get_last_scoped_symbol(sym_list, 1);
1470 if (sym)
1471 split_inlines_in_scope(sym);
1474 static struct stree *clone_estates_perm(struct stree *orig)
1476 struct stree *ret = NULL;
1477 struct sm_state *tmp;
1479 FOR_EACH_SM(orig, tmp) {
1480 set_state_stree_perm(&ret, tmp->owner, tmp->name, tmp->sym, clone_estate_perm(tmp->state));
1481 } END_FOR_EACH_SM(tmp);
1483 return ret;
1486 static void split_functions(struct symbol_list *sym_list)
1488 struct symbol *sym;
1490 __unnullify_path();
1491 FOR_EACH_PTR(sym_list, sym) {
1492 set_position(sym->pos);
1493 if (sym->type != SYM_NODE || get_base_type(sym)->type != SYM_FN) {
1494 __pass_to_client(sym, BASE_HOOK);
1495 fake_global_assign(sym);
1497 } END_FOR_EACH_PTR(sym);
1498 global_states = clone_estates_perm(get_all_states_stree(SMATCH_EXTRA));
1499 nullify_path();
1501 FOR_EACH_PTR(sym_list, sym) {
1502 set_position(sym->pos);
1503 if (sym->type == SYM_NODE && get_base_type(sym)->type == SYM_FN) {
1504 split_function(sym);
1505 process_inlines();
1507 } END_FOR_EACH_PTR(sym);
1508 split_inlines(sym_list);
1509 __pass_to_client(sym_list, END_FILE_HOOK);
1512 void smatch(int argc, char **argv)
1515 struct string_list *filelist = NULL;
1516 struct symbol_list *sym_list;
1518 if (argc < 2) {
1519 printf("Usage: smatch [--debug] <filename.c>\n");
1520 exit(1);
1522 sparse_initialize(argc, argv, &filelist);
1523 set_valid_ptr_max();
1524 FOR_EACH_PTR_NOTAG(filelist, base_file) {
1525 if (option_file_output) {
1526 char buf[256];
1528 snprintf(buf, sizeof(buf), "%s.smatch", base_file);
1529 sm_outfd = fopen(buf, "w");
1530 if (!sm_outfd) {
1531 printf("Error: Cannot open %s\n", base_file);
1532 exit(1);
1535 sym_list = sparse_keep_tokens(base_file);
1536 split_functions(sym_list);
1537 } END_FOR_EACH_PTR_NOTAG(base_file);