states, stree: we can save stree directly in backup without converting
[smatch.git] / smatch_flow.c
blob8349671f04b3d2f41f539060197fa3f7bd1eb1c5
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 int __expr_stmt_count;
42 int __in_function_def;
43 static struct expression_list *switch_expr_stack = NULL;
44 static struct expression_list *post_op_stack = NULL;
46 struct expression_list *big_expression_stack;
47 struct statement_list *big_statement_stack;
48 int __in_pre_condition = 0;
49 int __bail_on_rest_of_function = 0;
50 char *get_function(void) { return cur_func; }
51 int get_lineno(void) { return __smatch_lineno; }
52 int inside_loop(void) { return !!loop_count; }
53 int definitely_inside_loop(void) { return !!(loop_count & ~0x80000000); }
54 struct expression *get_switch_expr(void) { return top_expression(switch_expr_stack); }
55 int in_expression_statement(void) { return !!__expr_stmt_count; }
57 static void split_symlist(struct symbol_list *sym_list);
58 static void split_declaration(struct symbol_list *sym_list);
59 static void split_expr_list(struct expression_list *expr_list);
60 static void add_inline_function(struct symbol *sym);
61 static void parse_inline(struct expression *expr);
63 int option_assume_loops = 0;
64 int option_known_conditions = 0;
65 int option_two_passes = 0;
66 struct symbol *cur_func_sym = NULL;
68 int outside_of_function(void)
70 return cur_func_sym == NULL;
73 const char *get_filename(void)
75 if (option_info)
76 return base_file;
77 if (option_full_path)
78 return full_filename;
79 return filename;
82 const char *get_base_file(void)
84 return base_file;
87 static void set_position(struct position pos)
89 int len;
90 static int prev_stream = -1;
92 if (pos.stream == 0 && pos.line == 0)
93 return;
95 __smatch_lineno = pos.line;
97 if (pos.stream == prev_stream)
98 return;
100 filename = stream_name(pos.stream);
102 free(full_filename);
103 pathname = getcwd(NULL, 0);
104 if (pathname) {
105 len = strlen(pathname) + 1 + strlen(filename) + 1;
106 full_filename = malloc(len);
107 snprintf(full_filename, len, "%s/%s", pathname, filename);
108 } else {
109 full_filename = alloc_string(filename);
111 free(pathname);
114 static int is_inline_func(struct expression *expr)
116 if (expr->type != EXPR_SYMBOL || !expr->symbol)
117 return 0;
118 if (expr->symbol->ctype.modifiers & MOD_INLINE)
119 return 1;
120 return 0;
123 static int is_noreturn_func(struct expression *expr)
125 if (expr->type != EXPR_SYMBOL || !expr->symbol)
126 return 0;
127 if (expr->symbol->ctype.modifiers & MOD_NORETURN)
128 return 1;
129 return 0;
132 int inlinable(struct expression *expr)
134 struct symbol *sym;
136 if (__inline_fn) /* don't nest */
137 return 0;
139 if (expr->type != EXPR_SYMBOL || !expr->symbol)
140 return 0;
141 if (is_no_inline_function(expr->symbol->ident->name))
142 return 0;
143 sym = get_base_type(expr->symbol);
144 if (sym->stmt && sym->stmt->type == STMT_COMPOUND) {
145 if (ptr_list_size((struct ptr_list *)sym->stmt->stmts) <= 10)
146 return 1;
147 return 0;
149 if (sym->inline_stmt && sym->inline_stmt->type == STMT_COMPOUND) {
150 if (ptr_list_size((struct ptr_list *)sym->inline_stmt->stmts) <= 10)
151 return 1;
152 return 0;
154 return 0;
157 void __process_post_op_stack(void)
159 struct expression *expr;
161 FOR_EACH_PTR(post_op_stack, expr) {
162 __pass_to_client(expr, OP_HOOK);
163 } END_FOR_EACH_PTR(expr);
165 __free_ptr_list((struct ptr_list **)&post_op_stack);
168 void __split_expr(struct expression *expr)
170 if (!expr)
171 return;
173 // sm_msg(" Debug expr_type %d %s", expr->type, show_special(expr->op));
175 if (__in_fake_assign && expr->type != EXPR_ASSIGNMENT)
176 return;
177 if (__in_fake_assign >= 4) /* don't allow too much nesting */
178 return;
180 push_expression(&big_expression_stack, expr);
181 set_position(expr->pos);
182 __pass_to_client(expr, EXPR_HOOK);
184 switch (expr->type) {
185 case EXPR_PREOP:
186 if (expr->op == '*')
187 __pass_to_client(expr, DEREF_HOOK);
188 __split_expr(expr->unop);
189 __pass_to_client(expr, OP_HOOK);
190 break;
191 case EXPR_POSTOP:
192 __split_expr(expr->unop);
193 push_expression(&post_op_stack, expr);
194 break;
195 case EXPR_STATEMENT:
196 __expr_stmt_count++;
197 __split_stmt(expr->statement);
198 __expr_stmt_count--;
199 break;
200 case EXPR_LOGICAL:
201 case EXPR_COMPARE:
202 __pass_to_client(expr, LOGIC_HOOK);
203 __handle_logic(expr);
204 break;
205 case EXPR_BINOP:
206 __pass_to_client(expr, BINOP_HOOK);
207 case EXPR_COMMA:
208 __split_expr(expr->left);
209 __process_post_op_stack();
210 __split_expr(expr->right);
211 break;
212 case EXPR_ASSIGNMENT: {
213 struct expression *tmp;
215 if (!expr->right)
216 break;
218 __pass_to_client(expr, RAW_ASSIGNMENT_HOOK);
220 /* foo = !bar() */
221 if (__handle_condition_assigns(expr))
222 break;
223 /* foo = (x < 5 ? foo : 5); */
224 if (__handle_select_assigns(expr))
225 break;
226 /* foo = ({frob(); frob(); frob(); 1;}) */
227 if (__handle_expr_statement_assigns(expr))
228 break;
230 __split_expr(expr->right);
231 if (outside_of_function())
232 __pass_to_client(expr, GLOBAL_ASSIGNMENT_HOOK);
233 else
234 __pass_to_client(expr, ASSIGNMENT_HOOK);
236 __fake_struct_member_assignments(expr);
238 tmp = strip_expr(expr->right);
239 if (tmp->type == EXPR_CALL)
240 __pass_to_client(expr, CALL_ASSIGNMENT_HOOK);
241 if (get_macro_name(tmp->pos) &&
242 get_macro_name(expr->pos) != get_macro_name(tmp->pos))
243 __pass_to_client(expr, MACRO_ASSIGNMENT_HOOK);
244 __split_expr(expr->left);
245 break;
247 case EXPR_DEREF:
248 __pass_to_client(expr, DEREF_HOOK);
249 __split_expr(expr->deref);
250 break;
251 case EXPR_SLICE:
252 __split_expr(expr->base);
253 break;
254 case EXPR_CAST:
255 case EXPR_FORCE_CAST:
256 __pass_to_client(expr, CAST_HOOK);
257 __split_expr(expr->cast_expression);
258 break;
259 case EXPR_SIZEOF:
260 if (expr->cast_expression)
261 __pass_to_client(strip_parens(expr->cast_expression),
262 SIZEOF_HOOK);
263 break;
264 case EXPR_OFFSETOF:
265 case EXPR_ALIGNOF:
266 evaluate_expression(expr);
267 break;
268 case EXPR_CONDITIONAL:
269 case EXPR_SELECT:
270 if (known_condition_true(expr->conditional)) {
271 __split_expr(expr->cond_true);
272 break;
274 if (known_condition_false(expr->conditional)) {
275 __split_expr(expr->cond_false);
276 break;
278 __pass_to_client(expr, SELECT_HOOK);
279 __split_whole_condition(expr->conditional);
280 __split_expr(expr->cond_true);
281 __push_true_states();
282 __use_false_states();
283 __split_expr(expr->cond_false);
284 __merge_true_states();
285 break;
286 case EXPR_CALL:
287 if (sym_name_is("__builtin_constant_p", expr->fn))
288 break;
289 split_expr_list(expr->args);
290 __split_expr(expr->fn);
291 if (is_inline_func(expr->fn))
292 add_inline_function(expr->fn->symbol);
293 if (inlinable(expr->fn))
294 __inline_call = 1;
295 __process_post_op_stack();
296 __pass_to_client(expr, FUNCTION_CALL_HOOK);
297 __inline_call = 0;
298 if (inlinable(expr->fn)) {
299 parse_inline(expr);
301 __pass_to_client(expr, CALL_HOOK_AFTER_INLINE);
302 if (is_noreturn_func(expr->fn))
303 nullify_path();
304 break;
305 case EXPR_INITIALIZER:
306 split_expr_list(expr->expr_list);
307 break;
308 case EXPR_IDENTIFIER:
309 __split_expr(expr->ident_expression);
310 break;
311 case EXPR_INDEX:
312 __split_expr(expr->idx_expression);
313 break;
314 case EXPR_POS:
315 __split_expr(expr->init_expr);
316 break;
317 case EXPR_SYMBOL:
318 __pass_to_client(expr, SYM_HOOK);
319 break;
320 case EXPR_STRING:
321 __pass_to_client(expr, STRING_HOOK);
322 break;
323 default:
324 break;
326 pop_expression(&big_expression_stack);
329 static int is_forever_loop(struct statement *stmt)
331 struct expression *expr;
333 expr = strip_expr(stmt->iterator_pre_condition);
334 if (!expr)
335 expr = stmt->iterator_post_condition;
336 if (!expr) {
337 /* this is a for(;;) loop... */
338 return 1;
341 if (expr->type == EXPR_VALUE && expr->value == 1)
342 return 1;
344 return 0;
347 static int loop_num;
348 static char *get_loop_name(int num)
350 char buf[256];
352 snprintf(buf, 255, "-loop%d", num);
353 buf[255] = '\0';
354 return alloc_sname(buf);
358 * Pre Loops are while and for loops.
360 static void handle_pre_loop(struct statement *stmt)
362 int once_through; /* we go through the loop at least once */
363 struct sm_state *extra_sm = NULL;
364 int unchanged = 0;
365 char *loop_name;
366 struct state_list *slist = NULL;
367 struct sm_state *sm = NULL;
369 loop_name = get_loop_name(loop_num);
370 loop_num++;
372 __split_stmt(stmt->iterator_pre_statement);
374 once_through = implied_condition_true(stmt->iterator_pre_condition);
376 loop_count++;
377 __push_continues();
378 __push_breaks();
380 __merge_gotos(loop_name);
382 extra_sm = __extra_handle_canonical_loops(stmt, &slist);
383 __in_pre_condition++;
384 __pass_to_client(stmt, PRELOOP_HOOK);
385 __split_whole_condition(stmt->iterator_pre_condition);
386 __in_pre_condition--;
387 FOR_EACH_PTR(slist, sm) {
388 set_state(sm->owner, sm->name, sm->sym, sm->state);
389 } END_FOR_EACH_PTR(sm);
390 free_slist(&slist);
391 if (extra_sm)
392 extra_sm = get_sm_state(extra_sm->owner, extra_sm->name, extra_sm->sym);
394 if (option_assume_loops)
395 once_through = 1;
397 __split_stmt(stmt->iterator_statement);
398 __warn_on_silly_pre_loops();
399 if (is_forever_loop(stmt)) {
400 struct state_list *slist;
402 __save_gotos(loop_name);
404 __push_fake_cur_slist();
405 __split_stmt(stmt->iterator_post_statement);
406 slist = stree_to_slist(__pop_fake_cur_slist());
408 __discard_continues();
409 __discard_false_states();
410 __use_breaks();
412 if (!__path_is_null())
413 __merge_slist_into_cur(slist);
414 free_slist(&slist);
415 } else {
416 __merge_continues();
417 unchanged = __iterator_unchanged(extra_sm);
418 __split_stmt(stmt->iterator_post_statement);
419 __save_gotos(loop_name);
420 __split_whole_condition(stmt->iterator_pre_condition);
421 nullify_path();
422 __merge_false_states();
423 if (once_through)
424 __discard_false_states();
425 else
426 __merge_false_states();
428 if (extra_sm && unchanged)
429 __extra_pre_loop_hook_after(extra_sm,
430 stmt->iterator_post_statement,
431 stmt->iterator_pre_condition);
432 __merge_breaks();
434 loop_count--;
438 * Post loops are do {} while();
440 static void handle_post_loop(struct statement *stmt)
442 char *loop_name;
444 loop_name = get_loop_name(loop_num);
445 loop_num++;
446 loop_count++;
448 __push_continues();
449 __push_breaks();
450 __merge_gotos(loop_name);
451 __split_stmt(stmt->iterator_statement);
452 __merge_continues();
453 if (!is_zero(stmt->iterator_post_condition))
454 __save_gotos(loop_name);
456 if (is_forever_loop(stmt)) {
457 __use_breaks();
458 } else {
459 __split_whole_condition(stmt->iterator_post_condition);
460 __use_false_states();
461 __merge_breaks();
463 loop_count--;
466 static int empty_statement(struct statement *stmt)
468 if (!stmt)
469 return 0;
470 if (stmt->type == STMT_EXPRESSION && !stmt->expression)
471 return 1;
472 return 0;
475 static int last_stmt_on_same_line()
477 struct statement *stmt;
478 int i = 0;
480 FOR_EACH_PTR_REVERSE(big_statement_stack, stmt) {
481 if (!i++)
482 continue;
483 if (stmt->pos.line == get_lineno())
484 return 1;
485 return 0;
486 } END_FOR_EACH_PTR_REVERSE(stmt);
487 return 0;
490 static struct statement *last_stmt;
491 static int is_last_stmt(struct statement *stmt)
493 if (stmt == last_stmt)
494 return 1;
495 return 0;
498 static void print_unreached_initializers(struct symbol_list *sym_list)
500 struct symbol *sym;
502 FOR_EACH_PTR(sym_list, sym) {
503 if (sym->initializer)
504 sm_msg("info: '%s' is not actually initialized (unreached code).",
505 (sym->ident ? sym->ident->name : "this variable"));
506 } END_FOR_EACH_PTR(sym);
509 static void print_unreached(struct statement *stmt)
511 static int print = 1;
513 if (__inline_fn)
514 return;
516 if (!__path_is_null()) {
517 print = 1;
518 return;
520 if (!print)
521 return;
523 switch (stmt->type) {
524 case STMT_COMPOUND: /* after a switch before a case stmt */
525 case STMT_RANGE:
526 case STMT_CASE:
527 case STMT_LABEL:
528 return;
529 case STMT_DECLARATION: /* switch (x) { int a; case foo: ... */
530 print_unreached_initializers(stmt->declaration);
531 return;
532 case STMT_RETURN: /* gcc complains if you don't have a return statement */
533 if (is_last_stmt(stmt))
534 return;
535 break;
536 case STMT_GOTO:
537 /* people put extra breaks inside switch statements */
538 if (stmt->goto_label && stmt->goto_label->type == SYM_NODE &&
539 strcmp(stmt->goto_label->ident->name, "break") == 0)
540 return;
541 break;
542 default:
543 break;
545 if (empty_statement(stmt))
546 return;
547 if (!option_spammy)
548 return;
549 sm_msg("info: ignoring unreachable code.");
550 print = 0;
553 static void split_asm_constraints(struct expression_list *expr_list)
555 struct expression *expr;
556 int state = 0;
558 FOR_EACH_PTR(expr_list, expr) {
559 switch (state) {
560 case 0: /* identifier */
561 case 1: /* constraint */
562 state++;
563 continue;
564 case 2: /* expression */
565 state = 0;
566 __split_expr(expr);
567 continue;
569 } END_FOR_EACH_PTR(expr);
572 static int is_case_val(struct statement *stmt, sval_t sval)
574 sval_t case_sval;
576 if (stmt->type != STMT_CASE)
577 return 0;
578 if (!stmt->case_expression) {
579 __set_default();
580 return 1;
582 if (!get_value(stmt->case_expression, &case_sval))
583 return 0;
584 if (case_sval.value == sval.value)
585 return 1;
586 return 0;
589 static void split_known_switch(struct statement *stmt, sval_t sval)
591 struct statement *tmp;
593 __split_expr(stmt->switch_expression);
595 push_expression(&switch_expr_stack, stmt->switch_expression);
596 __save_switch_states(top_expression(switch_expr_stack));
597 nullify_path();
598 __push_default();
599 __push_breaks();
601 stmt = stmt->switch_statement;
603 if (!last_stmt)
604 last_stmt = last_ptr_list((struct ptr_list *)stmt->stmts);
606 __push_scope_hooks();
607 FOR_EACH_PTR(stmt->stmts, tmp) {
608 __smatch_lineno = tmp->pos.line;
609 if (is_case_val(tmp, sval)) {
610 __merge_switches(top_expression(switch_expr_stack),
611 stmt->case_expression);
612 __pass_case_to_client(top_expression(switch_expr_stack),
613 stmt->case_expression);
615 if (__path_is_null())
616 continue;
617 __split_stmt(tmp);
618 if (__path_is_null()) {
619 __set_default();
620 goto out;
622 } END_FOR_EACH_PTR(tmp);
623 out:
624 __call_scope_hooks();
625 if (!__pop_default())
626 __merge_switches(top_expression(switch_expr_stack),
627 NULL);
628 __discard_switches();
629 __merge_breaks();
630 pop_expression(&switch_expr_stack);
633 void __split_stmt(struct statement *stmt)
635 sval_t sval;
637 if (!stmt)
638 goto out;
640 if (out_of_memory() || __bail_on_rest_of_function) {
641 static char *printed = NULL;
643 if (printed != cur_func)
644 sm_msg("Function too hairy. Giving up.");
645 final_pass = 0; /* turn off sm_msg() from here */
646 printed = cur_func;
647 return;
650 add_ptr_list(&big_statement_stack, stmt);
651 free_expression_stack(&big_expression_stack);
652 set_position(stmt->pos);
653 print_unreached(stmt);
654 __pass_to_client(stmt, STMT_HOOK);
656 switch (stmt->type) {
657 case STMT_DECLARATION:
658 split_declaration(stmt->declaration);
659 break;
660 case STMT_RETURN:
661 __split_expr(stmt->ret_value);
662 __pass_to_client(stmt->ret_value, RETURN_HOOK);
663 nullify_path();
664 break;
665 case STMT_EXPRESSION:
666 __split_expr(stmt->expression);
667 break;
668 case STMT_COMPOUND: {
669 struct statement *tmp;
671 if (!last_stmt)
672 last_stmt = last_ptr_list((struct ptr_list *)stmt->stmts);
673 __push_scope_hooks();
674 FOR_EACH_PTR(stmt->stmts, tmp) {
675 __split_stmt(tmp);
676 } END_FOR_EACH_PTR(tmp);
677 __call_scope_hooks();
678 break;
680 case STMT_IF:
681 if (known_condition_true(stmt->if_conditional)) {
682 __split_stmt(stmt->if_true);
683 break;
685 if (known_condition_false(stmt->if_conditional)) {
686 __split_stmt(stmt->if_false);
687 break;
689 if (option_known_conditions &&
690 implied_condition_true(stmt->if_conditional)) {
691 sm_info("this condition is true.");
692 __split_stmt(stmt->if_true);
693 break;
695 if (option_known_conditions &&
696 implied_condition_false(stmt->if_conditional)) {
697 sm_info("this condition is false.");
698 __split_stmt(stmt->if_false);
699 break;
701 __split_whole_condition(stmt->if_conditional);
702 __split_stmt(stmt->if_true);
703 if (empty_statement(stmt->if_true) &&
704 last_stmt_on_same_line() &&
705 !get_macro_name(stmt->if_true->pos))
706 sm_msg("warn: if();");
707 __push_true_states();
708 __use_false_states();
709 __split_stmt(stmt->if_false);
710 __merge_true_states();
711 break;
712 case STMT_ITERATOR:
713 if (stmt->iterator_pre_condition)
714 handle_pre_loop(stmt);
715 else if (stmt->iterator_post_condition)
716 handle_post_loop(stmt);
717 else {
718 // these are for(;;) type loops.
719 handle_pre_loop(stmt);
721 break;
722 case STMT_SWITCH:
723 if (get_value(stmt->switch_expression, &sval)) {
724 split_known_switch(stmt, sval);
725 break;
727 __split_expr(stmt->switch_expression);
728 push_expression(&switch_expr_stack, stmt->switch_expression);
729 __save_switch_states(top_expression(switch_expr_stack));
730 nullify_path();
731 __push_default();
732 __push_breaks();
733 __split_stmt(stmt->switch_statement);
734 if (!__pop_default())
735 __merge_switches(top_expression(switch_expr_stack),
736 NULL);
737 __discard_switches();
738 __merge_breaks();
739 pop_expression(&switch_expr_stack);
740 break;
741 case STMT_CASE:
742 __merge_switches(top_expression(switch_expr_stack),
743 stmt->case_expression);
744 __pass_case_to_client(top_expression(switch_expr_stack),
745 stmt->case_expression);
746 if (!stmt->case_expression)
747 __set_default();
748 __split_expr(stmt->case_expression);
749 __split_expr(stmt->case_to);
750 __split_stmt(stmt->case_statement);
751 break;
752 case STMT_LABEL:
753 if (stmt->label_identifier &&
754 stmt->label_identifier->type == SYM_LABEL &&
755 stmt->label_identifier->ident) {
756 loop_count |= 0x80000000;
757 __merge_gotos(stmt->label_identifier->ident->name);
759 __split_stmt(stmt->label_statement);
760 break;
761 case STMT_GOTO:
762 __split_expr(stmt->goto_expression);
763 if (stmt->goto_label && stmt->goto_label->type == SYM_NODE) {
764 if (!strcmp(stmt->goto_label->ident->name, "break")) {
765 __process_breaks();
766 } else if (!strcmp(stmt->goto_label->ident->name,
767 "continue")) {
768 __process_continues();
770 } else if (stmt->goto_label &&
771 stmt->goto_label->type == SYM_LABEL &&
772 stmt->goto_label->ident) {
773 __save_gotos(stmt->goto_label->ident->name);
775 nullify_path();
776 break;
777 case STMT_NONE:
778 break;
779 case STMT_ASM:
780 __pass_to_client(stmt, ASM_HOOK);
781 __split_expr(stmt->asm_string);
782 split_asm_constraints(stmt->asm_outputs);
783 split_asm_constraints(stmt->asm_inputs);
784 split_asm_constraints(stmt->asm_clobbers);
785 break;
786 case STMT_CONTEXT:
787 break;
788 case STMT_RANGE:
789 __split_expr(stmt->range_expression);
790 __split_expr(stmt->range_low);
791 __split_expr(stmt->range_high);
792 break;
794 __pass_to_client(stmt, STMT_HOOK_AFTER);
795 out:
796 __process_post_op_stack();
799 static void split_expr_list(struct expression_list *expr_list)
801 struct expression *expr;
803 FOR_EACH_PTR(expr_list, expr) {
804 __split_expr(expr);
805 __process_post_op_stack();
806 } END_FOR_EACH_PTR(expr);
809 static void split_sym(struct symbol *sym)
811 if (!sym)
812 return;
813 if (!(sym->namespace & NS_SYMBOL))
814 return;
816 __split_stmt(sym->stmt);
817 __split_expr(sym->array_size);
818 split_symlist(sym->arguments);
819 split_symlist(sym->symbol_list);
820 __split_stmt(sym->inline_stmt);
821 split_symlist(sym->inline_symbol_list);
824 static void split_symlist(struct symbol_list *sym_list)
826 struct symbol *sym;
828 FOR_EACH_PTR(sym_list, sym) {
829 split_sym(sym);
830 } END_FOR_EACH_PTR(sym);
833 typedef void (fake_cb)(struct expression *expr);
835 static int member_to_number(struct expression *expr, struct ident *member)
837 struct symbol *type, *tmp;
838 char *name;
839 int i;
841 if (!member)
842 return -1;
843 name = member->name;
845 type = get_type(expr);
846 if (!type || type->type != SYM_STRUCT)
847 return -1;
849 i = -1;
850 FOR_EACH_PTR(type->symbol_list, tmp) {
851 i++;
852 if (!tmp->ident)
853 continue;
854 if (strcmp(name, tmp->ident->name) == 0)
855 return i;
856 } END_FOR_EACH_PTR(tmp);
857 return -1;
860 static struct ident *number_to_member(struct expression *expr, int num)
862 struct symbol *type, *member;
863 int i = 0;
865 type = get_type(expr);
866 if (!type || type->type != SYM_STRUCT)
867 return NULL;
869 FOR_EACH_PTR(type->symbol_list, member) {
870 if (i == num)
871 return member->ident;
872 i++;
873 } END_FOR_EACH_PTR(member);
874 return NULL;
877 static void fake_element_assigns_helper(struct expression *array, struct expression_list *expr_list, fake_cb *fake_cb);
879 struct member_set {
880 struct ident *ident;
881 int set;
884 static struct member_set *alloc_member_set(struct symbol *type)
886 struct member_set *member_set;
887 struct symbol *member;
888 int member_count;
889 int member_idx;
891 member_count = ptr_list_size((struct ptr_list *)type->symbol_list);
892 member_set = malloc(member_count * sizeof(*member_set));
893 member_idx = 0;
894 FOR_EACH_PTR(type->symbol_list, member) {
895 member_set[member_idx].ident = member->ident;
896 member_set[member_idx].set = 0;
897 member_idx++;
898 } END_FOR_EACH_PTR(member);
900 return member_set;
903 static void mark_member_as_set(struct symbol *type, struct member_set *member_set, struct ident *ident)
905 int member_count = ptr_list_size((struct ptr_list *)type->symbol_list);
906 int i;
908 for (i = 0; i < member_count; i++) {
909 if (member_set[i].ident == ident) {
910 member_set[i].set = 1;
911 return;
914 // crap. this is buggy.
915 // sm_msg("internal smatch error in initializer %s.%s", type->ident->name, ident->name);
918 static void set_unset_to_zero(struct expression *symbol, struct symbol *type, struct member_set *member_set)
920 struct expression *deref, *assign;
921 struct symbol *member, *member_type;
922 int member_idx;
924 member_idx = 0;
925 FOR_EACH_PTR(type->symbol_list, member) {
926 if (!member->ident || member_set[member_idx].set) {
927 member_idx++;
928 continue;
930 member_type = get_real_base_type(member);
931 if (!member_type || member_type->type == SYM_ARRAY) {
932 member_idx++;
933 continue;
935 /* TODO: this should be handled recursively and not ignored */
936 if (member_type->type == SYM_STRUCT || member_type->type == SYM_UNION) {
937 member_idx++;
938 continue;
940 deref = member_expression(symbol, '.', member->ident);
941 assign = assign_expression(deref, zero_expr());
942 __split_expr(assign);
943 member_idx++;
944 } END_FOR_EACH_PTR(member);
948 static void fake_member_assigns_helper(struct expression *symbol, struct expression_list *members, fake_cb *fake_cb)
950 struct expression *deref, *assign, *tmp;
951 struct symbol *struct_type, *type;
952 struct ident *member;
953 int member_idx;
954 struct member_set *member_set;
956 struct_type = get_type(symbol);
957 if (!struct_type ||
958 (struct_type->type != SYM_STRUCT && struct_type->type != SYM_UNION))
959 return;
961 member_set = alloc_member_set(struct_type);
963 member_idx = 0;
964 FOR_EACH_PTR(members, tmp) {
965 member = number_to_member(symbol, member_idx);
966 while (tmp->type == EXPR_IDENTIFIER) {
967 member = tmp->expr_ident;
968 member_idx = member_to_number(symbol, member);
969 tmp = tmp->ident_expression;
971 mark_member_as_set(struct_type, member_set, member);
972 member_idx++;
973 deref = member_expression(symbol, '.', member);
974 if (tmp->type == EXPR_INITIALIZER) {
975 type = get_type(deref);
976 if (type && type->type == SYM_ARRAY)
977 fake_element_assigns_helper(deref, tmp->expr_list, fake_cb);
978 else
979 fake_member_assigns_helper(deref, tmp->expr_list, fake_cb);
980 } else {
981 assign = assign_expression(deref, tmp);
982 fake_cb(assign);
984 } END_FOR_EACH_PTR(tmp);
986 set_unset_to_zero(symbol, struct_type, member_set);
989 static void fake_member_assigns(struct symbol *sym, fake_cb *fake_cb)
991 fake_member_assigns_helper(symbol_expression(sym),
992 sym->initializer->expr_list, fake_cb);
995 static void fake_element_assigns_helper(struct expression *array, struct expression_list *expr_list, fake_cb *fake_cb)
997 struct expression *offset, *binop, *assign, *tmp;
998 struct symbol *type;
999 int idx;
1001 idx = 0;
1002 FOR_EACH_PTR(expr_list, tmp) {
1003 if (tmp->type == EXPR_INDEX) {
1004 if (tmp->idx_from != tmp->idx_to)
1005 return;
1006 idx = tmp->idx_from;
1007 if (!tmp->idx_expression)
1008 goto next;
1009 tmp = tmp->idx_expression;
1011 offset = value_expr(idx);
1012 binop = array_element_expression(array, offset);
1013 if (tmp->type == EXPR_INITIALIZER) {
1014 type = get_type(binop);
1015 if (type && type->type == SYM_ARRAY)
1016 fake_element_assigns_helper(binop, tmp->expr_list, fake_cb);
1017 else
1018 fake_member_assigns_helper(binop, tmp->expr_list, fake_cb);
1019 } else {
1020 assign = assign_expression(binop, tmp);
1021 fake_cb(assign);
1023 next:
1024 idx++;
1025 } END_FOR_EACH_PTR(tmp);
1028 static void fake_element_assigns(struct symbol *sym, fake_cb *fake_cb)
1030 fake_element_assigns_helper(symbol_expression(sym), sym->initializer->expr_list, fake_cb);
1033 static void fake_assign_expr(struct symbol *sym)
1035 struct expression *assign, *symbol;
1037 symbol = symbol_expression(sym);
1038 assign = assign_expression(symbol, sym->initializer);
1039 __split_expr(assign);
1042 static void call_split_expr(struct expression *expr)
1044 __split_expr(expr);
1047 static void do_initializer_stuff(struct symbol *sym)
1049 if (!sym->initializer)
1050 return;
1052 if (sym->initializer->type == EXPR_INITIALIZER) {
1053 if (get_real_base_type(sym)->type == SYM_ARRAY)
1054 fake_element_assigns(sym, call_split_expr);
1055 else
1056 fake_member_assigns(sym, call_split_expr);
1057 } else {
1058 fake_assign_expr(sym);
1062 static void split_declaration(struct symbol_list *sym_list)
1064 struct symbol *sym;
1066 FOR_EACH_PTR(sym_list, sym) {
1067 __pass_to_client(sym, DECLARATION_HOOK);
1068 do_initializer_stuff(sym);
1069 split_sym(sym);
1070 } END_FOR_EACH_PTR(sym);
1073 static void call_global_assign_hooks(struct expression *assign)
1075 __pass_to_client(assign, GLOBAL_ASSIGNMENT_HOOK);
1078 static void fake_global_assign(struct symbol *sym)
1080 struct expression *assign, *symbol;
1082 if (get_real_base_type(sym)->type == SYM_ARRAY) {
1083 if (sym->initializer && sym->initializer->type == EXPR_INITIALIZER) {
1084 fake_element_assigns(sym, call_global_assign_hooks);
1085 } else if (sym->initializer) {
1086 symbol = symbol_expression(sym);
1087 assign = assign_expression(symbol, sym->initializer);
1088 __pass_to_client(assign, GLOBAL_ASSIGNMENT_HOOK);
1089 } else {
1090 fake_element_assigns_helper(symbol_expression(sym), NULL, call_global_assign_hooks);
1092 } else if (get_real_base_type(sym)->type == SYM_STRUCT) {
1093 if (sym->initializer && sym->initializer->type == EXPR_INITIALIZER) {
1094 fake_member_assigns(sym, call_global_assign_hooks);
1095 } else if (sym->initializer) {
1096 symbol = symbol_expression(sym);
1097 assign = assign_expression(symbol, sym->initializer);
1098 __pass_to_client(assign, GLOBAL_ASSIGNMENT_HOOK);
1099 } else {
1100 fake_member_assigns_helper(symbol_expression(sym), NULL, call_global_assign_hooks);
1102 } else {
1103 symbol = symbol_expression(sym);
1104 if (sym->initializer)
1105 assign = assign_expression(symbol, sym->initializer);
1106 else
1107 assign = assign_expression(symbol, zero_expr());
1108 __pass_to_client(assign, GLOBAL_ASSIGNMENT_HOOK);
1112 static void start_function_definition(struct symbol *sym)
1114 __in_function_def = 1;
1115 __pass_to_client(sym, FUNC_DEF_HOOK);
1116 __in_function_def = 0;
1117 __pass_to_client(sym, AFTER_DEF_HOOK);
1121 static void split_function(struct symbol *sym)
1123 struct symbol *base_type = get_base_type(sym);
1125 cur_func_sym = sym;
1126 if (sym->ident)
1127 cur_func = sym->ident->name;
1128 __smatch_lineno = sym->pos.line;
1129 last_stmt = NULL;
1130 loop_count = 0;
1131 sm_debug("new function: %s\n", cur_func);
1132 __slist_id = 0;
1133 if (option_two_passes) {
1134 __unnullify_path();
1135 loop_num = 0;
1136 final_pass = 0;
1137 start_function_definition(sym);
1138 __split_stmt(base_type->stmt);
1139 __split_stmt(base_type->inline_stmt);
1140 nullify_path();
1142 __unnullify_path();
1143 loop_num = 0;
1144 start_function_definition(sym);
1145 __split_stmt(base_type->stmt);
1146 __split_stmt(base_type->inline_stmt);
1147 __pass_to_client(sym, END_FUNC_HOOK);
1148 __pass_to_client(sym, AFTER_FUNC_HOOK);
1149 cur_func_sym = NULL;
1150 cur_func = NULL;
1151 clear_all_states();
1152 free_data_info_allocs();
1153 free_expression_stack(&switch_expr_stack);
1154 __free_ptr_list((struct ptr_list **)&big_statement_stack);
1155 __bail_on_rest_of_function = 0;
1158 static void parse_inline(struct expression *call)
1160 struct symbol *base_type;
1161 int loop_num_bak = loop_num;
1162 int final_pass_bak = final_pass;
1163 char *cur_func_bak = cur_func;
1164 struct statement_list *big_statement_stack_bak = big_statement_stack;
1165 struct expression_list *big_expression_stack_bak = big_expression_stack;
1166 struct expression_list *switch_expr_stack_bak = switch_expr_stack;
1167 struct symbol *cur_func_sym_bak = cur_func_sym;
1169 __pass_to_client(call, INLINE_FN_START);
1170 final_pass = 0; /* don't print anything */
1171 __inline_fn = call;
1173 base_type = get_base_type(call->fn->symbol);
1174 cur_func_sym = call->fn->symbol;
1175 if (call->fn->symbol->ident)
1176 cur_func = call->fn->symbol->ident->name;
1177 else
1178 cur_func = NULL;
1179 set_position(call->fn->symbol->pos);
1181 save_all_states();
1182 nullify_all_states();
1183 big_statement_stack = NULL;
1184 big_expression_stack = NULL;
1185 switch_expr_stack = NULL;
1187 sm_debug("inline function: %s\n", cur_func);
1188 __unnullify_path();
1189 loop_num = 0;
1190 start_function_definition(call->fn->symbol);
1191 __split_stmt(base_type->stmt);
1192 __split_stmt(base_type->inline_stmt);
1193 __pass_to_client(call->fn->symbol, END_FUNC_HOOK);
1194 __pass_to_client(call->fn->symbol, AFTER_FUNC_HOOK);
1196 free_expression_stack(&switch_expr_stack);
1197 __free_ptr_list((struct ptr_list **)&big_statement_stack);
1198 nullify_path();
1200 loop_num = loop_num_bak;
1201 final_pass = final_pass_bak;
1202 cur_func_sym = cur_func_sym_bak;
1203 cur_func = cur_func_bak;
1204 big_statement_stack = big_statement_stack_bak;
1205 big_expression_stack = big_expression_stack_bak;
1206 switch_expr_stack = switch_expr_stack_bak;
1208 restore_all_states();
1209 set_position(call->pos);
1210 __inline_fn = NULL;
1211 __pass_to_client(call, INLINE_FN_END);
1214 static struct symbol_list *inlines_called;
1215 static void add_inline_function(struct symbol *sym)
1217 static struct symbol_list *already_added;
1218 struct symbol *tmp;
1220 FOR_EACH_PTR(already_added, tmp) {
1221 if (tmp == sym)
1222 return;
1223 } END_FOR_EACH_PTR(tmp);
1225 add_ptr_list(&already_added, sym);
1226 add_ptr_list(&inlines_called, sym);
1229 static void process_inlines()
1231 struct symbol *tmp;
1233 FOR_EACH_PTR(inlines_called, tmp) {
1234 split_function(tmp);
1235 } END_FOR_EACH_PTR(tmp);
1236 free_ptr_list(&inlines_called);
1239 static struct symbol *get_last_scoped_symbol(struct symbol_list *big_list, int use_static)
1241 struct symbol *sym;
1243 FOR_EACH_PTR_REVERSE(big_list, sym) {
1244 if (!sym->scope)
1245 continue;
1246 if (use_static && sym->ctype.modifiers & MOD_STATIC)
1247 return sym;
1248 if (!use_static && !(sym->ctype.modifiers & MOD_STATIC))
1249 return sym;
1250 } END_FOR_EACH_PTR_REVERSE(sym);
1252 return NULL;
1255 static void split_inlines_in_scope(struct symbol *sym)
1257 struct symbol *base;
1258 struct symbol_list *scope_list;
1259 int stream;
1261 scope_list = sym->scope->symbols;
1262 stream = sym->pos.stream;
1264 /* find the last static symbol in the file */
1265 FOR_EACH_PTR_REVERSE(scope_list, sym) {
1266 if (sym->pos.stream != stream)
1267 continue;
1268 if (sym->type != SYM_NODE)
1269 continue;
1270 base = get_base_type(sym);
1271 if (!base)
1272 continue;
1273 if (base->type != SYM_FN)
1274 continue;
1275 if (!base->inline_stmt)
1276 continue;
1277 add_inline_function(sym);
1278 } END_FOR_EACH_PTR_REVERSE(sym);
1280 process_inlines();
1283 static void split_inlines(struct symbol_list *sym_list)
1285 struct symbol *sym;
1287 sym = get_last_scoped_symbol(sym_list, 0);
1288 if (sym)
1289 split_inlines_in_scope(sym);
1290 sym = get_last_scoped_symbol(sym_list, 1);
1291 if (sym)
1292 split_inlines_in_scope(sym);
1295 static void split_functions(struct symbol_list *sym_list)
1297 struct symbol *sym;
1299 FOR_EACH_PTR(sym_list, sym) {
1300 set_position(sym->pos);
1301 if (sym->type == SYM_NODE && get_base_type(sym)->type == SYM_FN) {
1302 split_function(sym);
1303 process_inlines();
1304 } else {
1305 __pass_to_client(sym, BASE_HOOK);
1306 fake_global_assign(sym);
1308 } END_FOR_EACH_PTR(sym);
1309 split_inlines(sym_list);
1310 __pass_to_client(sym_list, END_FILE_HOOK);
1313 void smatch(int argc, char **argv)
1316 struct string_list *filelist = NULL;
1317 struct symbol_list *sym_list;
1319 if (argc < 2) {
1320 printf("Usage: smatch [--debug] <filename.c>\n");
1321 exit(1);
1323 sparse_initialize(argc, argv, &filelist);
1324 FOR_EACH_PTR_NOTAG(filelist, base_file) {
1325 if (option_file_output) {
1326 char buf[256];
1328 snprintf(buf, sizeof(buf), "%s.smatch", base_file);
1329 sm_outfd = fopen(buf, "w");
1330 if (!sm_outfd) {
1331 printf("Error: Cannot open %s\n", base_file);
1332 exit(1);
1335 sym_list = sparse_keep_tokens(base_file);
1336 split_functions(sym_list);
1337 } END_FOR_EACH_PTR_NOTAG(base_file);