type: include smatch_slist.h to prevent a segfault
[smatch.git] / smatch_flow.c
blob81bdf22cf490983d76d15573a56d72b6bbaa8a07
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);
453 __prev_stmt = stmt->iterator_pre_statement;
455 once_through = implied_condition_true(stmt->iterator_pre_condition);
457 loop_count++;
458 __push_continues();
459 __push_breaks();
461 __merge_gotos(loop_name);
463 extra_sm = __extra_handle_canonical_loops(stmt, &stree);
464 __in_pre_condition++;
465 __pass_to_client(stmt, PRELOOP_HOOK);
466 __split_whole_condition(stmt->iterator_pre_condition);
467 __in_pre_condition--;
468 FOR_EACH_SM(stree, sm) {
469 set_state(sm->owner, sm->name, sm->sym, sm->state);
470 } END_FOR_EACH_SM(sm);
471 free_stree(&stree);
472 if (extra_sm)
473 extra_sm = get_sm_state(extra_sm->owner, extra_sm->name, extra_sm->sym);
475 if (option_assume_loops)
476 once_through = 1;
478 __split_stmt(stmt->iterator_statement);
479 __warn_on_silly_pre_loops();
480 if (is_forever_loop(stmt)) {
481 __merge_continues();
482 __save_gotos(loop_name);
484 __push_fake_cur_stree();
485 __split_stmt(stmt->iterator_post_statement);
486 stree = __pop_fake_cur_stree();
488 __discard_false_states();
489 __use_breaks();
491 if (!__path_is_null())
492 __merge_stree_into_cur(stree);
493 free_stree(&stree);
494 } else {
495 __merge_continues();
496 unchanged = __iterator_unchanged(extra_sm);
497 __split_stmt(stmt->iterator_post_statement);
498 __prev_stmt = stmt->iterator_post_statement;
499 __cur_stmt = stmt;
501 __save_gotos(loop_name);
502 __in_pre_condition++;
503 __split_whole_condition(stmt->iterator_pre_condition);
504 __in_pre_condition--;
505 nullify_path();
506 __merge_false_states();
507 if (once_through)
508 __discard_false_states();
509 else
510 __merge_false_states();
512 if (extra_sm && unchanged)
513 __extra_pre_loop_hook_after(extra_sm,
514 stmt->iterator_post_statement,
515 stmt->iterator_pre_condition);
516 __merge_breaks();
518 loop_count--;
522 * Post loops are do {} while();
524 static void handle_post_loop(struct statement *stmt)
526 char *loop_name;
528 loop_name = get_loop_name(loop_num);
529 loop_num++;
530 loop_count++;
532 __push_continues();
533 __push_breaks();
534 __merge_gotos(loop_name);
535 __split_stmt(stmt->iterator_statement);
536 __merge_continues();
537 if (!is_zero(stmt->iterator_post_condition))
538 __save_gotos(loop_name);
540 if (is_forever_loop(stmt)) {
541 __use_breaks();
542 } else {
543 __split_whole_condition(stmt->iterator_post_condition);
544 __use_false_states();
545 __merge_breaks();
547 loop_count--;
550 static int empty_statement(struct statement *stmt)
552 if (!stmt)
553 return 0;
554 if (stmt->type == STMT_EXPRESSION && !stmt->expression)
555 return 1;
556 return 0;
559 static int last_stmt_on_same_line(void)
561 struct statement *stmt;
562 int i = 0;
564 FOR_EACH_PTR_REVERSE(big_statement_stack, stmt) {
565 if (!i++)
566 continue;
567 if (stmt->pos.line == get_lineno())
568 return 1;
569 return 0;
570 } END_FOR_EACH_PTR_REVERSE(stmt);
571 return 0;
574 static void split_asm_constraints(struct expression_list *expr_list)
576 struct expression *expr;
577 int state = 0;
579 FOR_EACH_PTR(expr_list, expr) {
580 switch (state) {
581 case 0: /* identifier */
582 case 1: /* constraint */
583 state++;
584 continue;
585 case 2: /* expression */
586 state = 0;
587 __split_expr(expr);
588 continue;
590 } END_FOR_EACH_PTR(expr);
593 static int is_case_val(struct statement *stmt, sval_t sval)
595 sval_t case_sval;
597 if (stmt->type != STMT_CASE)
598 return 0;
599 if (!stmt->case_expression) {
600 __set_default();
601 return 1;
603 if (!get_value(stmt->case_expression, &case_sval))
604 return 0;
605 if (case_sval.value == sval.value)
606 return 1;
607 return 0;
610 static void split_known_switch(struct statement *stmt, sval_t sval)
612 struct statement *tmp;
614 __split_expr(stmt->switch_expression);
616 push_expression(&switch_expr_stack, stmt->switch_expression);
617 __save_switch_states(top_expression(switch_expr_stack));
618 nullify_path();
619 __push_default();
620 __push_breaks();
622 stmt = stmt->switch_statement;
624 __push_scope_hooks();
625 FOR_EACH_PTR(stmt->stmts, tmp) {
626 __smatch_lineno = tmp->pos.line;
627 if (is_case_val(tmp, sval)) {
628 __merge_switches(top_expression(switch_expr_stack),
629 stmt->case_expression);
630 __pass_case_to_client(top_expression(switch_expr_stack),
631 stmt->case_expression);
633 if (__path_is_null())
634 continue;
635 __split_stmt(tmp);
636 if (__path_is_null()) {
637 __set_default();
638 goto out;
640 } END_FOR_EACH_PTR(tmp);
641 out:
642 __call_scope_hooks();
643 if (!__pop_default())
644 __merge_switches(top_expression(switch_expr_stack),
645 NULL);
646 __discard_switches();
647 __merge_breaks();
648 pop_expression(&switch_expr_stack);
651 static int taking_too_long(void)
653 int ms;
655 ms = ms_since(&fn_start_time);
656 if (ms > 1000 * 60 * 5) /* five minutes */
657 return 1;
658 return 0;
661 static int is_last_stmt(struct statement *cur_stmt)
663 struct symbol *fn = get_base_type(cur_func_sym);
664 struct statement *stmt;
666 if (!fn)
667 return 0;
668 stmt = fn->stmt;
669 if (!stmt)
670 stmt = fn->inline_stmt;
671 if (!stmt || stmt->type != STMT_COMPOUND)
672 return 0;
673 stmt = last_ptr_list((struct ptr_list *)stmt->stmts);
674 if (stmt && stmt->type == STMT_LABEL)
675 stmt = stmt->label_statement;
676 if (stmt == cur_stmt)
677 return 1;
678 return 0;
681 static void handle_backward_goto(struct statement *goto_stmt)
683 const char *goto_name, *label_name;
684 struct statement *func_stmt;
685 struct symbol *base_type = get_base_type(cur_func_sym);
686 struct statement *tmp;
687 int found = 0;
689 if (!option_info)
690 return;
691 if (last_goto_statement_handled)
692 return;
693 last_goto_statement_handled = 1;
695 if (!goto_stmt->goto_label ||
696 goto_stmt->goto_label->type != SYM_LABEL ||
697 !goto_stmt->goto_label->ident)
698 return;
699 goto_name = goto_stmt->goto_label->ident->name;
701 func_stmt = base_type->stmt;
702 if (!func_stmt)
703 func_stmt = base_type->inline_stmt;
704 if (!func_stmt)
705 return;
706 if (func_stmt->type != STMT_COMPOUND)
707 return;
709 FOR_EACH_PTR(func_stmt->stmts, tmp) {
710 if (!found) {
711 if (tmp->type != STMT_LABEL)
712 continue;
713 if (!tmp->label_identifier ||
714 tmp->label_identifier->type != SYM_LABEL ||
715 !tmp->label_identifier->ident)
716 continue;
717 label_name = tmp->label_identifier->ident->name;
718 if (strcmp(goto_name, label_name) != 0)
719 continue;
720 found = 1;
722 __split_stmt(tmp);
723 } END_FOR_EACH_PTR(tmp);
726 static void fake_a_return(void)
728 struct symbol *return_type;
730 nullify_path();
731 __unnullify_path();
733 return_type = get_real_base_type(cur_func_sym);
734 return_type = get_real_base_type(return_type);
735 if (return_type != &void_ctype) {
736 __pass_to_client(unknown_value_expression(NULL), RETURN_HOOK);
737 nullify_path();
740 __pass_to_client(cur_func_sym, END_FUNC_HOOK);
741 __pass_to_client(cur_func_sym, AFTER_FUNC_HOOK);
744 static void split_compound(struct statement *stmt)
746 struct statement *prev = NULL;
747 struct statement *cur = NULL;
748 struct statement *next;
750 __push_scope_hooks();
752 FOR_EACH_PTR(stmt->stmts, next) {
753 if (cur) {
754 __prev_stmt = prev;
755 __next_stmt = next;
756 __cur_stmt = cur;
757 __split_stmt(cur);
759 prev = cur;
760 cur = next;
761 } END_FOR_EACH_PTR(next);
762 if (cur) {
763 __prev_stmt = prev;
764 __cur_stmt = cur;
765 __next_stmt = NULL;
766 __split_stmt(cur);
769 __call_scope_hooks();
772 void __split_stmt(struct statement *stmt)
774 sval_t sval;
776 if (!stmt)
777 goto out;
779 if (__bail_on_rest_of_function || out_of_memory() || taking_too_long()) {
780 static char *printed = NULL;
782 __bail_on_rest_of_function = 1;
783 if (printed != cur_func)
784 sm_msg("Function too hairy. Giving up.");
785 fake_a_return();
786 final_pass = 0; /* turn off sm_msg() from here */
787 printed = cur_func;
788 return;
791 add_ptr_list(&big_statement_stack, stmt);
792 free_expression_stack(&big_expression_stack);
793 set_position(stmt->pos);
794 __pass_to_client(stmt, STMT_HOOK);
796 switch (stmt->type) {
797 case STMT_DECLARATION:
798 split_declaration(stmt->declaration);
799 break;
800 case STMT_RETURN:
801 __split_expr(stmt->ret_value);
802 __pass_to_client(stmt->ret_value, RETURN_HOOK);
803 __process_post_op_stack();
804 nullify_path();
805 break;
806 case STMT_EXPRESSION:
807 __split_expr(stmt->expression);
808 break;
809 case STMT_COMPOUND:
810 split_compound(stmt);
811 break;
812 case STMT_IF:
813 if (known_condition_true(stmt->if_conditional)) {
814 __split_stmt(stmt->if_true);
815 break;
817 if (known_condition_false(stmt->if_conditional)) {
818 __split_stmt(stmt->if_false);
819 break;
821 if (option_known_conditions &&
822 implied_condition_true(stmt->if_conditional)) {
823 sm_info("this condition is true.");
824 __split_stmt(stmt->if_true);
825 break;
827 if (option_known_conditions &&
828 implied_condition_false(stmt->if_conditional)) {
829 sm_info("this condition is false.");
830 __split_stmt(stmt->if_false);
831 break;
833 __split_whole_condition(stmt->if_conditional);
834 __split_stmt(stmt->if_true);
835 if (empty_statement(stmt->if_true) &&
836 last_stmt_on_same_line() &&
837 !get_macro_name(stmt->if_true->pos))
838 sm_msg("warn: if();");
839 __push_true_states();
840 __use_false_states();
841 __split_stmt(stmt->if_false);
842 __merge_true_states();
843 break;
844 case STMT_ITERATOR:
845 if (stmt->iterator_pre_condition)
846 handle_pre_loop(stmt);
847 else if (stmt->iterator_post_condition)
848 handle_post_loop(stmt);
849 else {
850 // these are for(;;) type loops.
851 handle_pre_loop(stmt);
853 break;
854 case STMT_SWITCH:
855 if (get_value(stmt->switch_expression, &sval)) {
856 split_known_switch(stmt, sval);
857 break;
859 __split_expr(stmt->switch_expression);
860 push_expression(&switch_expr_stack, stmt->switch_expression);
861 __save_switch_states(top_expression(switch_expr_stack));
862 nullify_path();
863 __push_default();
864 __push_breaks();
865 __split_stmt(stmt->switch_statement);
866 if (!__pop_default())
867 __merge_switches(top_expression(switch_expr_stack),
868 NULL);
869 __discard_switches();
870 __merge_breaks();
871 pop_expression(&switch_expr_stack);
872 break;
873 case STMT_CASE:
874 __merge_switches(top_expression(switch_expr_stack),
875 stmt->case_expression);
876 __pass_case_to_client(top_expression(switch_expr_stack),
877 stmt->case_expression);
878 if (!stmt->case_expression)
879 __set_default();
880 __split_expr(stmt->case_expression);
881 __split_expr(stmt->case_to);
882 __split_stmt(stmt->case_statement);
883 break;
884 case STMT_LABEL:
885 if (stmt->label_identifier &&
886 stmt->label_identifier->type == SYM_LABEL &&
887 stmt->label_identifier->ident) {
888 loop_count |= 0x80000000;
889 __merge_gotos(stmt->label_identifier->ident->name);
891 __split_stmt(stmt->label_statement);
892 break;
893 case STMT_GOTO:
894 __split_expr(stmt->goto_expression);
895 if (stmt->goto_label && stmt->goto_label->type == SYM_NODE) {
896 if (!strcmp(stmt->goto_label->ident->name, "break")) {
897 __process_breaks();
898 } else if (!strcmp(stmt->goto_label->ident->name,
899 "continue")) {
900 __process_continues();
902 } else if (stmt->goto_label &&
903 stmt->goto_label->type == SYM_LABEL &&
904 stmt->goto_label->ident) {
905 __save_gotos(stmt->goto_label->ident->name);
907 nullify_path();
908 if (is_last_stmt(stmt))
909 handle_backward_goto(stmt);
910 break;
911 case STMT_NONE:
912 break;
913 case STMT_ASM:
914 __pass_to_client(stmt, ASM_HOOK);
915 __split_expr(stmt->asm_string);
916 split_asm_constraints(stmt->asm_outputs);
917 split_asm_constraints(stmt->asm_inputs);
918 split_asm_constraints(stmt->asm_clobbers);
919 break;
920 case STMT_CONTEXT:
921 break;
922 case STMT_RANGE:
923 __split_expr(stmt->range_expression);
924 __split_expr(stmt->range_low);
925 __split_expr(stmt->range_high);
926 break;
928 __pass_to_client(stmt, STMT_HOOK_AFTER);
929 out:
930 __process_post_op_stack();
933 static void split_expr_list(struct expression_list *expr_list)
935 struct expression *expr;
937 FOR_EACH_PTR(expr_list, expr) {
938 __split_expr(expr);
939 __process_post_op_stack();
940 } END_FOR_EACH_PTR(expr);
943 static void split_sym(struct symbol *sym)
945 if (!sym)
946 return;
947 if (!(sym->namespace & NS_SYMBOL))
948 return;
950 __split_stmt(sym->stmt);
951 __split_expr(sym->array_size);
952 split_symlist(sym->arguments);
953 split_symlist(sym->symbol_list);
954 __split_stmt(sym->inline_stmt);
955 split_symlist(sym->inline_symbol_list);
958 static void split_symlist(struct symbol_list *sym_list)
960 struct symbol *sym;
962 FOR_EACH_PTR(sym_list, sym) {
963 split_sym(sym);
964 } END_FOR_EACH_PTR(sym);
967 typedef void (fake_cb)(struct expression *expr);
969 static int member_to_number(struct expression *expr, struct ident *member)
971 struct symbol *type, *tmp;
972 char *name;
973 int i;
975 if (!member)
976 return -1;
977 name = member->name;
979 type = get_type(expr);
980 if (!type || type->type != SYM_STRUCT)
981 return -1;
983 i = -1;
984 FOR_EACH_PTR(type->symbol_list, tmp) {
985 i++;
986 if (!tmp->ident)
987 continue;
988 if (strcmp(name, tmp->ident->name) == 0)
989 return i;
990 } END_FOR_EACH_PTR(tmp);
991 return -1;
994 static struct ident *number_to_member(struct expression *expr, int num)
996 struct symbol *type, *member;
997 int i = 0;
999 type = get_type(expr);
1000 if (!type || type->type != SYM_STRUCT)
1001 return NULL;
1003 FOR_EACH_PTR(type->symbol_list, member) {
1004 if (i == num)
1005 return member->ident;
1006 i++;
1007 } END_FOR_EACH_PTR(member);
1008 return NULL;
1011 static void fake_element_assigns_helper(struct expression *array, struct expression_list *expr_list, fake_cb *fake_cb);
1013 struct member_set {
1014 struct ident *ident;
1015 int set;
1018 static struct member_set *alloc_member_set(struct symbol *type)
1020 struct member_set *member_set;
1021 struct symbol *member;
1022 int member_count;
1023 int member_idx;
1025 member_count = ptr_list_size((struct ptr_list *)type->symbol_list);
1026 member_set = malloc(member_count * sizeof(*member_set));
1027 member_idx = 0;
1028 FOR_EACH_PTR(type->symbol_list, member) {
1029 member_set[member_idx].ident = member->ident;
1030 member_set[member_idx].set = 0;
1031 member_idx++;
1032 } END_FOR_EACH_PTR(member);
1034 return member_set;
1037 static void mark_member_as_set(struct symbol *type, struct member_set *member_set, struct ident *ident)
1039 int member_count = ptr_list_size((struct ptr_list *)type->symbol_list);
1040 int i;
1042 for (i = 0; i < member_count; i++) {
1043 if (member_set[i].ident == ident) {
1044 member_set[i].set = 1;
1045 return;
1048 // crap. this is buggy.
1049 // sm_msg("internal smatch error in initializer %s.%s", type->ident->name, ident->name);
1052 static void set_inner_struct_members(struct expression *expr, struct symbol *member)
1054 struct expression *edge_member, *assign;
1055 struct symbol *base = get_real_base_type(member);
1056 struct symbol *tmp;
1058 if (member->ident)
1059 expr = member_expression(expr, '.', member->ident);
1061 FOR_EACH_PTR(base->symbol_list, tmp) {
1062 struct symbol *type;
1064 type = get_real_base_type(tmp);
1065 if (!type)
1066 continue;
1068 if (tmp->ident) {
1069 edge_member = member_expression(expr, '.', tmp->ident);
1070 if (get_state_expr(SMATCH_EXTRA, edge_member))
1071 continue;
1074 if (type->type == SYM_UNION || type->type == SYM_STRUCT) {
1075 set_inner_struct_members(expr, tmp);
1076 continue;
1079 if (!tmp->ident)
1080 continue;
1082 assign = assign_expression(edge_member, zero_expr());
1083 __split_expr(assign);
1084 } END_FOR_EACH_PTR(tmp);
1089 static void set_unset_to_zero(struct symbol *type, struct expression *expr)
1091 struct symbol *tmp;
1092 struct expression *member, *assign;
1093 int op = '*';
1095 if (expr->type == EXPR_PREOP && expr->op == '&') {
1096 expr = strip_expr(expr->unop);
1097 op = '.';
1100 FOR_EACH_PTR(type->symbol_list, tmp) {
1101 type = get_real_base_type(tmp);
1102 if (!type)
1103 continue;
1105 if (tmp->ident) {
1106 member = member_expression(expr, op, tmp->ident);
1107 if (get_state_expr(SMATCH_EXTRA, member))
1108 continue;
1111 if (type->type == SYM_UNION || type->type == SYM_STRUCT) {
1112 set_inner_struct_members(expr, tmp);
1113 continue;
1115 if (type->type == SYM_ARRAY)
1116 continue;
1117 if (!tmp->ident)
1118 continue;
1120 assign = assign_expression(member, zero_expr());
1121 __split_expr(assign);
1122 } END_FOR_EACH_PTR(tmp);
1125 static void fake_member_assigns_helper(struct expression *symbol, struct expression_list *members, fake_cb *fake_cb)
1127 struct expression *deref, *assign, *tmp;
1128 struct symbol *struct_type, *type;
1129 struct ident *member;
1130 int member_idx;
1131 struct member_set *member_set;
1133 struct_type = get_type(symbol);
1134 if (!struct_type ||
1135 (struct_type->type != SYM_STRUCT && struct_type->type != SYM_UNION))
1136 return;
1138 member_set = alloc_member_set(struct_type);
1140 member_idx = 0;
1141 FOR_EACH_PTR(members, tmp) {
1142 member = number_to_member(symbol, member_idx);
1143 while (tmp->type == EXPR_IDENTIFIER) {
1144 member = tmp->expr_ident;
1145 member_idx = member_to_number(symbol, member);
1146 tmp = tmp->ident_expression;
1148 mark_member_as_set(struct_type, member_set, member);
1149 member_idx++;
1150 deref = member_expression(symbol, '.', member);
1151 if (tmp->type == EXPR_INITIALIZER) {
1152 type = get_type(deref);
1153 if (type && type->type == SYM_ARRAY)
1154 fake_element_assigns_helper(deref, tmp->expr_list, fake_cb);
1155 else
1156 fake_member_assigns_helper(deref, tmp->expr_list, fake_cb);
1157 } else {
1158 assign = assign_expression(deref, tmp);
1159 fake_cb(assign);
1161 } END_FOR_EACH_PTR(tmp);
1163 set_unset_to_zero(struct_type, symbol);
1166 static void fake_member_assigns(struct symbol *sym, fake_cb *fake_cb)
1168 fake_member_assigns_helper(symbol_expression(sym),
1169 sym->initializer->expr_list, fake_cb);
1172 static void fake_element_assigns_helper(struct expression *array, struct expression_list *expr_list, fake_cb *fake_cb)
1174 struct expression *offset, *binop, *assign, *tmp;
1175 struct symbol *type;
1176 int idx;
1178 idx = 0;
1179 FOR_EACH_PTR(expr_list, tmp) {
1180 if (tmp->type == EXPR_INDEX) {
1181 if (tmp->idx_from != tmp->idx_to)
1182 return;
1183 idx = tmp->idx_from;
1184 if (!tmp->idx_expression)
1185 goto next;
1186 tmp = tmp->idx_expression;
1188 offset = value_expr(idx);
1189 binop = array_element_expression(array, offset);
1190 if (tmp->type == EXPR_INITIALIZER) {
1191 type = get_type(binop);
1192 if (type && type->type == SYM_ARRAY)
1193 fake_element_assigns_helper(binop, tmp->expr_list, fake_cb);
1194 else
1195 fake_member_assigns_helper(binop, tmp->expr_list, fake_cb);
1196 } else {
1197 assign = assign_expression(binop, tmp);
1198 fake_cb(assign);
1200 next:
1201 idx++;
1202 } END_FOR_EACH_PTR(tmp);
1205 static void fake_element_assigns(struct symbol *sym, fake_cb *fake_cb)
1207 fake_element_assigns_helper(symbol_expression(sym), sym->initializer->expr_list, fake_cb);
1210 static void fake_assign_expr(struct symbol *sym)
1212 struct expression *assign, *symbol;
1214 symbol = symbol_expression(sym);
1215 assign = assign_expression(symbol, sym->initializer);
1216 __split_expr(assign);
1219 static void call_split_expr(struct expression *expr)
1221 __split_expr(expr);
1224 static void do_initializer_stuff(struct symbol *sym)
1226 if (!sym->initializer)
1227 return;
1229 if (sym->initializer->type == EXPR_INITIALIZER) {
1230 if (get_real_base_type(sym)->type == SYM_ARRAY)
1231 fake_element_assigns(sym, call_split_expr);
1232 else
1233 fake_member_assigns(sym, call_split_expr);
1234 } else {
1235 fake_assign_expr(sym);
1239 static void split_declaration(struct symbol_list *sym_list)
1241 struct symbol *sym;
1243 FOR_EACH_PTR(sym_list, sym) {
1244 __pass_to_client(sym, DECLARATION_HOOK);
1245 do_initializer_stuff(sym);
1246 split_sym(sym);
1247 } END_FOR_EACH_PTR(sym);
1250 static void call_global_assign_hooks(struct expression *assign)
1252 __pass_to_client(assign, GLOBAL_ASSIGNMENT_HOOK);
1255 static void fake_global_assign(struct symbol *sym)
1257 struct expression *assign, *symbol;
1259 if (get_real_base_type(sym)->type == SYM_ARRAY) {
1260 if (sym->initializer && sym->initializer->type == EXPR_INITIALIZER) {
1261 fake_element_assigns(sym, call_global_assign_hooks);
1262 } else if (sym->initializer) {
1263 symbol = symbol_expression(sym);
1264 assign = assign_expression(symbol, sym->initializer);
1265 __pass_to_client(assign, GLOBAL_ASSIGNMENT_HOOK);
1266 } else {
1267 fake_element_assigns_helper(symbol_expression(sym), NULL, call_global_assign_hooks);
1269 } else if (get_real_base_type(sym)->type == SYM_STRUCT) {
1270 if (sym->initializer && sym->initializer->type == EXPR_INITIALIZER) {
1271 fake_member_assigns(sym, call_global_assign_hooks);
1272 } else if (sym->initializer) {
1273 symbol = symbol_expression(sym);
1274 assign = assign_expression(symbol, sym->initializer);
1275 __pass_to_client(assign, GLOBAL_ASSIGNMENT_HOOK);
1276 } else {
1277 fake_member_assigns_helper(symbol_expression(sym), NULL, call_global_assign_hooks);
1279 } else {
1280 symbol = symbol_expression(sym);
1281 if (sym->initializer)
1282 assign = assign_expression(symbol, sym->initializer);
1283 else
1284 assign = assign_expression(symbol, zero_expr());
1285 __pass_to_client(assign, GLOBAL_ASSIGNMENT_HOOK);
1289 static void start_function_definition(struct symbol *sym)
1291 __in_function_def = 1;
1292 __pass_to_client(sym, FUNC_DEF_HOOK);
1293 __in_function_def = 0;
1294 __pass_to_client(sym, AFTER_DEF_HOOK);
1298 static void split_function(struct symbol *sym)
1300 struct symbol *base_type = get_base_type(sym);
1302 if (!base_type->stmt && !base_type->inline_stmt)
1303 return;
1305 gettimeofday(&fn_start_time, NULL);
1306 cur_func_sym = sym;
1307 if (sym->ident)
1308 cur_func = sym->ident->name;
1309 __smatch_lineno = sym->pos.line;
1310 loop_count = 0;
1311 last_goto_statement_handled = 0;
1312 sm_debug("new function: %s\n", cur_func);
1313 __stree_id = 0;
1314 if (option_two_passes) {
1315 __unnullify_path();
1316 loop_num = 0;
1317 final_pass = 0;
1318 start_function_definition(sym);
1319 __split_stmt(base_type->stmt);
1320 __split_stmt(base_type->inline_stmt);
1321 nullify_path();
1323 __unnullify_path();
1324 loop_num = 0;
1325 final_pass = 1;
1326 start_function_definition(sym);
1327 __split_stmt(base_type->stmt);
1328 __split_stmt(base_type->inline_stmt);
1329 __pass_to_client(sym, END_FUNC_HOOK);
1330 __pass_to_client(sym, AFTER_FUNC_HOOK);
1332 clear_all_states();
1333 cur_func_sym = NULL;
1334 cur_func = NULL;
1335 free_data_info_allocs();
1336 free_expression_stack(&switch_expr_stack);
1337 __free_ptr_list((struct ptr_list **)&big_statement_stack);
1338 __bail_on_rest_of_function = 0;
1341 static void parse_inline(struct expression *call)
1343 struct symbol *base_type;
1344 int loop_num_bak = loop_num;
1345 int final_pass_bak = final_pass;
1346 char *cur_func_bak = cur_func;
1347 struct statement_list *big_statement_stack_bak = big_statement_stack;
1348 struct expression_list *big_expression_stack_bak = big_expression_stack;
1349 struct expression_list *switch_expr_stack_bak = switch_expr_stack;
1350 struct symbol *cur_func_sym_bak = cur_func_sym;
1352 __pass_to_client(call, INLINE_FN_START);
1353 final_pass = 0; /* don't print anything */
1354 __inline_fn = call;
1356 base_type = get_base_type(call->fn->symbol);
1357 cur_func_sym = call->fn->symbol;
1358 if (call->fn->symbol->ident)
1359 cur_func = call->fn->symbol->ident->name;
1360 else
1361 cur_func = NULL;
1362 set_position(call->fn->symbol->pos);
1364 save_all_states();
1365 big_statement_stack = NULL;
1366 big_expression_stack = NULL;
1367 switch_expr_stack = NULL;
1369 sm_debug("inline function: %s\n", cur_func);
1370 __unnullify_path();
1371 loop_num = 0;
1372 start_function_definition(call->fn->symbol);
1373 __split_stmt(base_type->stmt);
1374 __split_stmt(base_type->inline_stmt);
1375 __pass_to_client(call->fn->symbol, END_FUNC_HOOK);
1376 __pass_to_client(call->fn->symbol, AFTER_FUNC_HOOK);
1378 free_expression_stack(&switch_expr_stack);
1379 __free_ptr_list((struct ptr_list **)&big_statement_stack);
1380 nullify_path();
1381 free_goto_stack();
1383 loop_num = loop_num_bak;
1384 final_pass = final_pass_bak;
1385 cur_func_sym = cur_func_sym_bak;
1386 cur_func = cur_func_bak;
1387 big_statement_stack = big_statement_stack_bak;
1388 big_expression_stack = big_expression_stack_bak;
1389 switch_expr_stack = switch_expr_stack_bak;
1391 restore_all_states();
1392 set_position(call->pos);
1393 __inline_fn = NULL;
1394 __pass_to_client(call, INLINE_FN_END);
1397 static struct symbol_list *inlines_called;
1398 static void add_inline_function(struct symbol *sym)
1400 static struct symbol_list *already_added;
1401 struct symbol *tmp;
1403 FOR_EACH_PTR(already_added, tmp) {
1404 if (tmp == sym)
1405 return;
1406 } END_FOR_EACH_PTR(tmp);
1408 add_ptr_list(&already_added, sym);
1409 add_ptr_list(&inlines_called, sym);
1412 static void process_inlines(void)
1414 struct symbol *tmp;
1416 FOR_EACH_PTR(inlines_called, tmp) {
1417 split_function(tmp);
1418 } END_FOR_EACH_PTR(tmp);
1419 free_ptr_list(&inlines_called);
1422 static struct symbol *get_last_scoped_symbol(struct symbol_list *big_list, int use_static)
1424 struct symbol *sym;
1426 FOR_EACH_PTR_REVERSE(big_list, sym) {
1427 if (!sym->scope)
1428 continue;
1429 if (use_static && sym->ctype.modifiers & MOD_STATIC)
1430 return sym;
1431 if (!use_static && !(sym->ctype.modifiers & MOD_STATIC))
1432 return sym;
1433 } END_FOR_EACH_PTR_REVERSE(sym);
1435 return NULL;
1438 static void split_inlines_in_scope(struct symbol *sym)
1440 struct symbol *base;
1441 struct symbol_list *scope_list;
1442 int stream;
1444 scope_list = sym->scope->symbols;
1445 stream = sym->pos.stream;
1447 /* find the last static symbol in the file */
1448 FOR_EACH_PTR_REVERSE(scope_list, sym) {
1449 if (sym->pos.stream != stream)
1450 continue;
1451 if (sym->type != SYM_NODE)
1452 continue;
1453 base = get_base_type(sym);
1454 if (!base)
1455 continue;
1456 if (base->type != SYM_FN)
1457 continue;
1458 if (!base->inline_stmt)
1459 continue;
1460 add_inline_function(sym);
1461 } END_FOR_EACH_PTR_REVERSE(sym);
1463 process_inlines();
1466 static void split_inlines(struct symbol_list *sym_list)
1468 struct symbol *sym;
1470 sym = get_last_scoped_symbol(sym_list, 0);
1471 if (sym)
1472 split_inlines_in_scope(sym);
1473 sym = get_last_scoped_symbol(sym_list, 1);
1474 if (sym)
1475 split_inlines_in_scope(sym);
1478 static struct stree *clone_estates_perm(struct stree *orig)
1480 struct stree *ret = NULL;
1481 struct sm_state *tmp;
1483 FOR_EACH_SM(orig, tmp) {
1484 set_state_stree_perm(&ret, tmp->owner, tmp->name, tmp->sym, clone_estate_perm(tmp->state));
1485 } END_FOR_EACH_SM(tmp);
1487 return ret;
1490 static void split_functions(struct symbol_list *sym_list)
1492 struct symbol *sym;
1494 __unnullify_path();
1495 FOR_EACH_PTR(sym_list, sym) {
1496 set_position(sym->pos);
1497 if (sym->type != SYM_NODE || get_base_type(sym)->type != SYM_FN) {
1498 __pass_to_client(sym, BASE_HOOK);
1499 fake_global_assign(sym);
1501 } END_FOR_EACH_PTR(sym);
1502 global_states = clone_estates_perm(get_all_states_stree(SMATCH_EXTRA));
1503 nullify_path();
1505 FOR_EACH_PTR(sym_list, sym) {
1506 set_position(sym->pos);
1507 if (sym->type == SYM_NODE && get_base_type(sym)->type == SYM_FN) {
1508 split_function(sym);
1509 process_inlines();
1511 } END_FOR_EACH_PTR(sym);
1512 split_inlines(sym_list);
1513 __pass_to_client(sym_list, END_FILE_HOOK);
1516 void smatch(int argc, char **argv)
1519 struct string_list *filelist = NULL;
1520 struct symbol_list *sym_list;
1522 if (argc < 2) {
1523 printf("Usage: smatch [--debug] <filename.c>\n");
1524 exit(1);
1526 sparse_initialize(argc, argv, &filelist);
1527 set_valid_ptr_max();
1528 FOR_EACH_PTR_NOTAG(filelist, base_file) {
1529 if (option_file_output) {
1530 char buf[256];
1532 snprintf(buf, sizeof(buf), "%s.smatch", base_file);
1533 sm_outfd = fopen(buf, "w");
1534 if (!sm_outfd) {
1535 printf("Error: Cannot open %s\n", base_file);
1536 exit(1);
1539 sym_list = sparse_keep_tokens(base_file);
1540 split_functions(sym_list);
1541 } END_FOR_EACH_PTR_NOTAG(base_file);