*new* add smatch_data/kernel.silenced_functions to silence common noise
[smatch.git] / smatch_flow.c
blob0738e99d59fec191a1fe60671024903ad6cb6d4a
1 /*
2 * sparse/smatch_flow.c
4 * Copyright (C) 2006,2008 Dan Carpenter.
6 * Licensed under the Open Software License version 1.1
8 */
10 #define _GNU_SOURCE 1
11 #include <unistd.h>
12 #include <stdio.h>
13 #include "token.h"
14 #include "scope.h"
15 #include "smatch.h"
16 #include "smatch_expression_stacks.h"
17 #include "smatch_extra.h"
18 #include "smatch_slist.h"
20 int final_pass;
21 int __inline_call;
22 struct expression *__inline_fn;
24 static int __smatch_lineno = 0;
26 static char *base_file;
27 static const char *filename;
28 static char *pathname;
29 static char *full_filename;
30 static char *cur_func;
31 static int loop_count;
32 int __expr_stmt_count;
33 int __in_function_def;
34 static struct expression_list *switch_expr_stack = NULL;
35 static struct expression_list *post_op_stack = NULL;
37 struct expression_list *big_expression_stack;
38 struct statement_list *big_statement_stack;
39 int __in_pre_condition = 0;
40 int __bail_on_rest_of_function = 0;
41 char *get_function(void) { return cur_func; }
42 int get_lineno(void) { return __smatch_lineno; }
43 int inside_loop(void) { return !!loop_count; }
44 struct expression *get_switch_expr(void) { return top_expression(switch_expr_stack); }
45 int in_expression_statement(void) { return !!__expr_stmt_count; }
47 static void split_symlist(struct symbol_list *sym_list);
48 static void split_declaration(struct symbol_list *sym_list);
49 static void split_expr_list(struct expression_list *expr_list);
50 static void add_inline_function(struct symbol *sym);
51 static void parse_inline(struct expression *expr);
53 int option_assume_loops = 0;
54 int option_known_conditions = 0;
55 int option_two_passes = 0;
56 struct symbol *cur_func_sym = NULL;
58 int outside_of_function(void)
60 return cur_func_sym == NULL;
63 const char *get_filename(void)
65 if (option_info)
66 return base_file;
67 if (option_full_path)
68 return full_filename;
69 return filename;
72 const char *get_base_file(void)
74 return base_file;
77 static void set_position(struct position pos)
79 int len;
80 static int prev_stream = -1;
82 if (pos.stream == 0 && pos.line == 0)
83 return;
85 __smatch_lineno = pos.line;
87 if (pos.stream == prev_stream)
88 return;
90 filename = stream_name(pos.stream);
92 free(full_filename);
93 pathname = getcwd(NULL, 0);
94 if (pathname) {
95 len = strlen(pathname) + 1 + strlen(filename) + 1;
96 full_filename = malloc(len);
97 snprintf(full_filename, len, "%s/%s", pathname, filename);
98 } else {
99 full_filename = alloc_string(filename);
101 free(pathname);
104 static int is_inline_func(struct expression *expr)
106 if (expr->type != EXPR_SYMBOL || !expr->symbol)
107 return 0;
108 if (expr->symbol->ctype.modifiers & MOD_INLINE)
109 return 1;
110 return 0;
113 static int is_noreturn_func(struct expression *expr)
115 if (expr->type != EXPR_SYMBOL || !expr->symbol)
116 return 0;
117 if (expr->symbol->ctype.modifiers & MOD_NORETURN)
118 return 1;
119 return 0;
122 int inlinable(struct expression *expr)
124 struct symbol *sym;
126 if (__inline_fn) /* don't nest */
127 return 0;
129 if (expr->type != EXPR_SYMBOL || !expr->symbol)
130 return 0;
131 sym = get_base_type(expr->symbol);
132 if (sym->stmt && sym->stmt->type == STMT_COMPOUND) {
133 if (ptr_list_size((struct ptr_list *)sym->stmt->stmts) <= 10)
134 return 1;
135 return 0;
137 if (sym->inline_stmt && sym->inline_stmt->type == STMT_COMPOUND) {
138 if (ptr_list_size((struct ptr_list *)sym->inline_stmt->stmts) <= 10)
139 return 1;
140 return 0;
142 return 0;
145 void __process_post_op_stack(void)
147 struct expression *expr;
149 FOR_EACH_PTR(post_op_stack, expr) {
150 __pass_to_client(expr, OP_HOOK);
151 } END_FOR_EACH_PTR(expr);
153 __free_ptr_list((struct ptr_list **)&post_op_stack);
156 void __split_expr(struct expression *expr)
158 if (!expr)
159 return;
161 // sm_msg(" Debug expr_type %d %s", expr->type, show_special(expr->op));
163 push_expression(&big_expression_stack, expr);
164 set_position(expr->pos);
165 __pass_to_client(expr, EXPR_HOOK);
167 switch (expr->type) {
168 case EXPR_PREOP:
169 if (expr->op == '*')
170 __pass_to_client(expr, DEREF_HOOK);
171 __split_expr(expr->unop);
172 __pass_to_client(expr, OP_HOOK);
173 break;
174 case EXPR_POSTOP:
175 __split_expr(expr->unop);
176 push_expression(&post_op_stack, expr);
177 break;
178 case EXPR_STATEMENT:
179 __expr_stmt_count++;
180 __split_stmt(expr->statement);
181 __expr_stmt_count--;
182 break;
183 case EXPR_LOGICAL:
184 case EXPR_COMPARE:
185 __pass_to_client(expr, LOGIC_HOOK);
186 __handle_logic(expr);
187 break;
188 case EXPR_BINOP:
189 __pass_to_client(expr, BINOP_HOOK);
190 case EXPR_COMMA:
191 __split_expr(expr->left);
192 __process_post_op_stack();
193 __split_expr(expr->right);
194 break;
195 case EXPR_ASSIGNMENT: {
196 struct expression *tmp;
198 if (!expr->right)
199 break;
201 __pass_to_client(expr, RAW_ASSIGNMENT_HOOK);
203 /* foo = !bar() */
204 if (__handle_condition_assigns(expr))
205 break;
206 /* foo = (x < 5 ? foo : 5); */
207 if (__handle_select_assigns(expr))
208 break;
209 /* foo = ({frob(); frob(); frob(); 1;}) */
210 if (__handle_expr_statement_assigns(expr))
211 break;
213 __split_expr(expr->right);
214 if (outside_of_function())
215 __pass_to_client(expr, GLOBAL_ASSIGNMENT_HOOK);
216 else
217 __pass_to_client(expr, ASSIGNMENT_HOOK);
218 tmp = strip_expr(expr->right);
219 if (tmp->type == EXPR_CALL)
220 __pass_to_client(expr, CALL_ASSIGNMENT_HOOK);
221 if (get_macro_name(tmp->pos) &&
222 get_macro_name(expr->pos) != get_macro_name(tmp->pos))
223 __pass_to_client(expr, MACRO_ASSIGNMENT_HOOK);
224 __split_expr(expr->left);
225 break;
227 case EXPR_DEREF:
228 __pass_to_client(expr, DEREF_HOOK);
229 __split_expr(expr->deref);
230 break;
231 case EXPR_SLICE:
232 __split_expr(expr->base);
233 break;
234 case EXPR_CAST:
235 case EXPR_FORCE_CAST:
236 __pass_to_client(expr, CAST_HOOK);
237 __split_expr(expr->cast_expression);
238 break;
239 case EXPR_SIZEOF:
240 if (expr->cast_expression)
241 __pass_to_client(strip_parens(expr->cast_expression),
242 SIZEOF_HOOK);
243 break;
244 case EXPR_OFFSETOF:
245 case EXPR_ALIGNOF:
246 evaluate_expression(expr);
247 break;
248 case EXPR_CONDITIONAL:
249 case EXPR_SELECT:
250 if (known_condition_true(expr->conditional)) {
251 __split_expr(expr->cond_true);
252 break;
254 if (known_condition_false(expr->conditional)) {
255 __split_expr(expr->cond_false);
256 break;
258 __pass_to_client(expr, SELECT_HOOK);
259 __split_whole_condition(expr->conditional);
260 __split_expr(expr->cond_true);
261 __push_true_states();
262 __use_false_states();
263 __split_expr(expr->cond_false);
264 __merge_true_states();
265 break;
266 case EXPR_CALL:
267 if (sym_name_is("__builtin_constant_p", expr->fn))
268 break;
269 split_expr_list(expr->args);
270 __split_expr(expr->fn);
271 if (is_inline_func(expr->fn))
272 add_inline_function(expr->fn->symbol);
273 if (inlinable(expr->fn))
274 __inline_call = 1;
275 __process_post_op_stack();
276 __pass_to_client(expr, FUNCTION_CALL_HOOK);
277 __inline_call = 0;
278 if (inlinable(expr->fn)) {
279 parse_inline(expr);
281 __pass_to_client(expr, CALL_HOOK_AFTER_INLINE);
282 if (is_noreturn_func(expr->fn))
283 nullify_path();
284 break;
285 case EXPR_INITIALIZER:
286 split_expr_list(expr->expr_list);
287 break;
288 case EXPR_IDENTIFIER:
289 __split_expr(expr->ident_expression);
290 break;
291 case EXPR_INDEX:
292 __split_expr(expr->idx_expression);
293 break;
294 case EXPR_POS:
295 __split_expr(expr->init_expr);
296 break;
297 case EXPR_SYMBOL:
298 __pass_to_client(expr, SYM_HOOK);
299 break;
300 case EXPR_STRING:
301 __pass_to_client(expr, STRING_HOOK);
302 break;
303 default:
304 break;
306 pop_expression(&big_expression_stack);
309 static int is_forever_loop(struct statement *stmt)
311 struct expression *expr;
313 expr = strip_expr(stmt->iterator_pre_condition);
314 if (!expr)
315 expr = stmt->iterator_post_condition;
316 if (!expr) {
317 /* this is a for(;;) loop... */
318 return 1;
321 if (expr->type == EXPR_VALUE && expr->value == 1)
322 return 1;
324 return 0;
327 static int loop_num;
328 static char *get_loop_name(int num)
330 char buf[256];
332 snprintf(buf, 255, "-loop%d", num);
333 buf[255] = '\0';
334 return alloc_sname(buf);
338 * Pre Loops are while and for loops.
340 static void handle_pre_loop(struct statement *stmt)
342 int once_through; /* we go through the loop at least once */
343 struct sm_state *extra_sm = NULL;
344 int unchanged = 0;
345 char *loop_name;
346 struct state_list *slist = NULL;
347 struct sm_state *sm = NULL;
349 loop_name = get_loop_name(loop_num);
350 loop_num++;
352 __split_stmt(stmt->iterator_pre_statement);
354 once_through = implied_condition_true(stmt->iterator_pre_condition);
356 loop_count++;
357 __push_continues();
358 __push_breaks();
360 __merge_gotos(loop_name);
362 extra_sm = __extra_handle_canonical_loops(stmt, &slist);
363 __in_pre_condition++;
364 __pass_to_client(stmt, PRELOOP_HOOK);
365 __split_whole_condition(stmt->iterator_pre_condition);
366 __in_pre_condition--;
367 FOR_EACH_PTR(slist, sm) {
368 set_state(sm->owner, sm->name, sm->sym, sm->state);
369 } END_FOR_EACH_PTR(sm);
370 free_slist(&slist);
371 if (extra_sm)
372 extra_sm = get_sm_state(extra_sm->owner, extra_sm->name, extra_sm->sym);
374 if (option_assume_loops)
375 once_through = 1;
377 __split_stmt(stmt->iterator_statement);
378 __warn_on_silly_pre_loops();
379 if (is_forever_loop(stmt)) {
380 struct state_list *slist;
382 __save_gotos(loop_name);
384 __push_fake_cur_slist();
385 __split_stmt(stmt->iterator_post_statement);
386 slist = __pop_fake_cur_slist();
388 __discard_continues();
389 __discard_false_states();
390 __use_breaks();
392 if (!__path_is_null())
393 __merge_slist_into_cur(slist);
394 free_slist(&slist);
395 } else {
396 __merge_continues();
397 unchanged = __iterator_unchanged(extra_sm);
398 __split_stmt(stmt->iterator_post_statement);
399 __save_gotos(loop_name);
400 __split_whole_condition(stmt->iterator_pre_condition);
401 nullify_path();
402 __merge_false_states();
403 if (once_through)
404 __discard_false_states();
405 else
406 __merge_false_states();
408 if (extra_sm && unchanged)
409 __extra_pre_loop_hook_after(extra_sm,
410 stmt->iterator_post_statement,
411 stmt->iterator_pre_condition);
412 __merge_breaks();
414 loop_count--;
418 * Post loops are do {} while();
420 static void handle_post_loop(struct statement *stmt)
422 char *loop_name;
424 loop_name = get_loop_name(loop_num);
425 loop_num++;
426 loop_count++;
428 __push_continues();
429 __push_breaks();
430 __merge_gotos(loop_name);
431 __split_stmt(stmt->iterator_statement);
432 __merge_continues();
433 if (!is_zero(stmt->iterator_post_condition))
434 __save_gotos(loop_name);
436 if (is_forever_loop(stmt)) {
437 __use_breaks();
438 } else {
439 __split_whole_condition(stmt->iterator_post_condition);
440 __use_false_states();
441 __merge_breaks();
443 loop_count--;
446 static int empty_statement(struct statement *stmt)
448 if (!stmt)
449 return 0;
450 if (stmt->type == STMT_EXPRESSION && !stmt->expression)
451 return 1;
452 return 0;
455 static int last_stmt_on_same_line()
457 struct statement *stmt;
458 int i = 0;
460 FOR_EACH_PTR_REVERSE(big_statement_stack, stmt) {
461 if (!i++)
462 continue;
463 if (stmt->pos.line == get_lineno())
464 return 1;
465 return 0;
466 } END_FOR_EACH_PTR_REVERSE(stmt);
467 return 0;
470 static struct statement *last_stmt;
471 static int is_last_stmt(struct statement *stmt)
473 if (stmt == last_stmt)
474 return 1;
475 return 0;
478 static void print_unreached_initializers(struct symbol_list *sym_list)
480 struct symbol *sym;
482 FOR_EACH_PTR(sym_list, sym) {
483 if (sym->initializer)
484 sm_msg("info: '%s' is not actually initialized (unreached code).",
485 (sym->ident ? sym->ident->name : "this variable"));
486 } END_FOR_EACH_PTR(sym);
489 static void print_unreached(struct statement *stmt)
491 static int print = 1;
493 if (__inline_fn)
494 return;
496 if (!__path_is_null()) {
497 print = 1;
498 return;
500 if (!print)
501 return;
503 switch (stmt->type) {
504 case STMT_COMPOUND: /* after a switch before a case stmt */
505 case STMT_RANGE:
506 case STMT_CASE:
507 case STMT_LABEL:
508 return;
509 case STMT_DECLARATION: /* switch (x) { int a; case foo: ... */
510 print_unreached_initializers(stmt->declaration);
511 return;
512 case STMT_RETURN: /* gcc complains if you don't have a return statement */
513 if (is_last_stmt(stmt))
514 return;
515 break;
516 case STMT_GOTO:
517 if (!option_spammy)
518 return;
519 break;
520 default:
521 break;
523 if (!option_spammy && empty_statement(stmt))
524 return;
525 sm_msg("info: ignoring unreachable code.");
526 print = 0;
529 static void split_asm_constraints(struct expression_list *expr_list)
531 struct expression *expr;
532 int state = 0;
534 FOR_EACH_PTR(expr_list, expr) {
535 switch (state) {
536 case 0: /* identifier */
537 case 1: /* constraint */
538 state++;
539 continue;
540 case 2: /* expression */
541 state = 0;
542 __split_expr(expr);
543 continue;
545 } END_FOR_EACH_PTR(expr);
548 static int is_case_val(struct statement *stmt, sval_t sval)
550 sval_t case_sval;
552 if (stmt->type != STMT_CASE)
553 return 0;
554 if (!stmt->case_expression) {
555 __set_default();
556 return 1;
558 if (!get_value(stmt->case_expression, &case_sval))
559 return 0;
560 if (case_sval.value == sval.value)
561 return 1;
562 return 0;
565 static void split_known_switch(struct statement *stmt, sval_t sval)
567 struct statement *tmp;
569 __split_expr(stmt->switch_expression);
571 push_expression(&switch_expr_stack, stmt->switch_expression);
572 __save_switch_states(top_expression(switch_expr_stack));
573 nullify_path();
574 __push_default();
575 __push_breaks();
577 stmt = stmt->switch_statement;
579 if (!last_stmt)
580 last_stmt = last_ptr_list((struct ptr_list *)stmt->stmts);
582 __push_scope_hooks();
583 FOR_EACH_PTR(stmt->stmts, tmp) {
584 __smatch_lineno = tmp->pos.line;
585 if (is_case_val(tmp, sval)) {
586 __merge_switches(top_expression(switch_expr_stack),
587 stmt->case_expression);
588 __pass_case_to_client(top_expression(switch_expr_stack),
589 stmt->case_expression);
591 if (__path_is_null())
592 continue;
593 __split_stmt(tmp);
594 if (__path_is_null()) {
595 __set_default();
596 goto out;
598 } END_FOR_EACH_PTR(tmp);
599 out:
600 __call_scope_hooks();
601 if (!__pop_default())
602 __merge_switches(top_expression(switch_expr_stack),
603 NULL);
604 __discard_switches();
605 __merge_breaks();
606 pop_expression(&switch_expr_stack);
609 void __split_stmt(struct statement *stmt)
611 sval_t sval;
613 if (!stmt)
614 goto out;
616 if (out_of_memory() || __bail_on_rest_of_function) {
617 static char *printed = NULL;
619 if (printed != cur_func)
620 sm_msg("Function too hairy. Giving up.");
621 final_pass = 0; /* turn off sm_msg() from here */
622 printed = cur_func;
623 return;
626 add_ptr_list(&big_statement_stack, stmt);
627 free_expression_stack(&big_expression_stack);
628 set_position(stmt->pos);
629 print_unreached(stmt);
630 __pass_to_client(stmt, STMT_HOOK);
632 switch (stmt->type) {
633 case STMT_DECLARATION:
634 split_declaration(stmt->declaration);
635 break;
636 case STMT_RETURN:
637 __split_expr(stmt->ret_value);
638 __pass_to_client(stmt->ret_value, RETURN_HOOK);
639 nullify_path();
640 break;
641 case STMT_EXPRESSION:
642 __split_expr(stmt->expression);
643 break;
644 case STMT_COMPOUND: {
645 struct statement *tmp;
647 if (!last_stmt)
648 last_stmt = last_ptr_list((struct ptr_list *)stmt->stmts);
649 __push_scope_hooks();
650 FOR_EACH_PTR(stmt->stmts, tmp) {
651 __split_stmt(tmp);
652 } END_FOR_EACH_PTR(tmp);
653 __call_scope_hooks();
654 break;
656 case STMT_IF:
657 if (known_condition_true(stmt->if_conditional)) {
658 __split_stmt(stmt->if_true);
659 break;
661 if (known_condition_false(stmt->if_conditional)) {
662 __split_stmt(stmt->if_false);
663 break;
665 if (option_known_conditions &&
666 implied_condition_true(stmt->if_conditional)) {
667 sm_info("this condition is true.");
668 __split_stmt(stmt->if_true);
669 break;
671 if (option_known_conditions &&
672 implied_condition_false(stmt->if_conditional)) {
673 sm_info("this condition is false.");
674 __split_stmt(stmt->if_false);
675 break;
677 __split_whole_condition(stmt->if_conditional);
678 __split_stmt(stmt->if_true);
679 if (empty_statement(stmt->if_true) &&
680 last_stmt_on_same_line() &&
681 !get_macro_name(stmt->if_true->pos))
682 sm_msg("warn: if();");
683 __push_true_states();
684 __use_false_states();
685 __split_stmt(stmt->if_false);
686 __merge_true_states();
687 break;
688 case STMT_ITERATOR:
689 if (stmt->iterator_pre_condition)
690 handle_pre_loop(stmt);
691 else if (stmt->iterator_post_condition)
692 handle_post_loop(stmt);
693 else {
694 // these are for(;;) type loops.
695 handle_pre_loop(stmt);
697 break;
698 case STMT_SWITCH:
699 if (get_value(stmt->switch_expression, &sval)) {
700 split_known_switch(stmt, sval);
701 break;
703 __split_expr(stmt->switch_expression);
704 push_expression(&switch_expr_stack, stmt->switch_expression);
705 __save_switch_states(top_expression(switch_expr_stack));
706 nullify_path();
707 __push_default();
708 __push_breaks();
709 __split_stmt(stmt->switch_statement);
710 if (!__pop_default())
711 __merge_switches(top_expression(switch_expr_stack),
712 NULL);
713 __discard_switches();
714 __merge_breaks();
715 pop_expression(&switch_expr_stack);
716 break;
717 case STMT_CASE:
718 __merge_switches(top_expression(switch_expr_stack),
719 stmt->case_expression);
720 __pass_case_to_client(top_expression(switch_expr_stack),
721 stmt->case_expression);
722 if (!stmt->case_expression)
723 __set_default();
724 __split_expr(stmt->case_expression);
725 __split_expr(stmt->case_to);
726 __split_stmt(stmt->case_statement);
727 break;
728 case STMT_LABEL:
729 if (stmt->label_identifier &&
730 stmt->label_identifier->type == SYM_LABEL &&
731 stmt->label_identifier->ident) {
732 loop_count = 1000000;
733 __merge_gotos(stmt->label_identifier->ident->name);
735 __split_stmt(stmt->label_statement);
736 break;
737 case STMT_GOTO:
738 __split_expr(stmt->goto_expression);
739 if (stmt->goto_label && stmt->goto_label->type == SYM_NODE) {
740 if (!strcmp(stmt->goto_label->ident->name, "break")) {
741 __process_breaks();
742 } else if (!strcmp(stmt->goto_label->ident->name,
743 "continue")) {
744 __process_continues();
746 } else if (stmt->goto_label &&
747 stmt->goto_label->type == SYM_LABEL &&
748 stmt->goto_label->ident) {
749 __save_gotos(stmt->goto_label->ident->name);
751 nullify_path();
752 break;
753 case STMT_NONE:
754 break;
755 case STMT_ASM:
756 __pass_to_client(stmt, ASM_HOOK);
757 __split_expr(stmt->asm_string);
758 split_asm_constraints(stmt->asm_outputs);
759 split_asm_constraints(stmt->asm_inputs);
760 split_asm_constraints(stmt->asm_clobbers);
761 break;
762 case STMT_CONTEXT:
763 break;
764 case STMT_RANGE:
765 __split_expr(stmt->range_expression);
766 __split_expr(stmt->range_low);
767 __split_expr(stmt->range_high);
768 break;
770 out:
771 __process_post_op_stack();
774 static void split_expr_list(struct expression_list *expr_list)
776 struct expression *expr;
778 FOR_EACH_PTR(expr_list, expr) {
779 __split_expr(expr);
780 __process_post_op_stack();
781 } END_FOR_EACH_PTR(expr);
784 static void split_sym(struct symbol *sym)
786 if (!sym)
787 return;
788 if (!(sym->namespace & NS_SYMBOL))
789 return;
791 __split_stmt(sym->stmt);
792 __split_expr(sym->array_size);
793 split_symlist(sym->arguments);
794 split_symlist(sym->symbol_list);
795 __split_stmt(sym->inline_stmt);
796 split_symlist(sym->inline_symbol_list);
799 static void split_symlist(struct symbol_list *sym_list)
801 struct symbol *sym;
803 FOR_EACH_PTR(sym_list, sym) {
804 split_sym(sym);
805 } END_FOR_EACH_PTR(sym);
808 typedef void (fake_cb)(struct expression *expr);
810 static int member_to_number(struct expression *expr, struct ident *member)
812 struct symbol *type, *tmp;
813 char *name;
814 int i;
816 if (!member)
817 return -1;
818 name = member->name;
820 type = get_type(expr);
821 if (!type || type->type != SYM_STRUCT)
822 return -1;
824 i = -1;
825 FOR_EACH_PTR(type->symbol_list, tmp) {
826 i++;
827 if (!tmp->ident)
828 continue;
829 if (strcmp(name, tmp->ident->name) == 0)
830 return i;
831 } END_FOR_EACH_PTR(tmp);
832 return -1;
835 static struct ident *number_to_member(struct expression *expr, int num)
837 struct symbol *type, *member;
838 int i = 0;
840 type = get_type(expr);
841 if (!type || type->type != SYM_STRUCT)
842 return NULL;
844 FOR_EACH_PTR(type->symbol_list, member) {
845 if (i == num)
846 return member->ident;
847 i++;
848 } END_FOR_EACH_PTR(member);
849 return NULL;
852 static void fake_element_assigns_helper(struct expression *array, struct expression_list *expr_list, fake_cb *fake_cb);
854 struct member_set {
855 struct ident *ident;
856 int set;
859 static struct member_set *alloc_member_set(struct symbol *type)
861 struct member_set *member_set;
862 struct symbol *member;
863 int member_count;
864 int member_idx;
866 member_count = ptr_list_size((struct ptr_list *)type->symbol_list);
867 member_set = malloc(member_count * sizeof(*member_set));
868 member_idx = 0;
869 FOR_EACH_PTR(type->symbol_list, member) {
870 member_set[member_idx].ident = member->ident;
871 member_set[member_idx].set = 0;
872 member_idx++;
873 } END_FOR_EACH_PTR(member);
875 return member_set;
878 static void mark_member_as_set(struct symbol *type, struct member_set *member_set, struct ident *ident)
880 int member_count = ptr_list_size((struct ptr_list *)type->symbol_list);
881 int i;
883 for (i = 0; i < member_count; i++) {
884 if (member_set[i].ident == ident) {
885 member_set[i].set = 1;
886 return;
889 // crap. this is buggy.
890 // sm_msg("internal smatch error in initializer %s.%s", type->ident->name, ident->name);
893 static void set_unset_to_zero(struct expression *symbol, struct symbol *type, struct member_set *member_set)
895 struct expression *deref, *assign;
896 struct symbol *member, *member_type;
897 int member_idx;
899 member_idx = 0;
900 FOR_EACH_PTR(type->symbol_list, member) {
901 if (!member->ident || member_set[member_idx].set) {
902 member_idx++;
903 continue;
905 member_type = get_real_base_type(member);
906 if (!member_type || member_type->type == SYM_ARRAY)
907 continue;
908 /* TODO: this should be handled recursively and not ignored */
909 if (member_type->type == SYM_STRUCT || member_type->type == SYM_UNION)
910 continue;
911 deref = member_expression(symbol, '.', member->ident);
912 assign = assign_expression(deref, zero_expr());
913 __split_expr(assign);
914 member_idx++;
915 } END_FOR_EACH_PTR(member);
919 static void fake_member_assigns_helper(struct expression *symbol, struct expression_list *members, fake_cb *fake_cb)
921 struct expression *deref, *assign, *tmp;
922 struct symbol *struct_type, *type;
923 struct ident *member;
924 int member_idx;
925 struct member_set *member_set;
927 struct_type = get_type(symbol);
928 if (!struct_type ||
929 (struct_type->type != SYM_STRUCT && struct_type->type != SYM_UNION))
930 return;
932 member_set = alloc_member_set(struct_type);
934 member_idx = 0;
935 FOR_EACH_PTR(members, tmp) {
936 member = number_to_member(symbol, member_idx);
937 while (tmp->type == EXPR_IDENTIFIER) {
938 member = tmp->expr_ident;
939 member_idx = member_to_number(symbol, member);
940 tmp = tmp->ident_expression;
942 mark_member_as_set(struct_type, member_set, member);
943 member_idx++;
944 deref = member_expression(symbol, '.', member);
945 if (tmp->type == EXPR_INITIALIZER) {
946 type = get_type(deref);
947 if (type && type->type == SYM_ARRAY)
948 fake_element_assigns_helper(deref, tmp->expr_list, fake_cb);
949 else
950 fake_member_assigns_helper(deref, tmp->expr_list, fake_cb);
951 } else {
952 assign = assign_expression(deref, tmp);
953 fake_cb(assign);
955 } END_FOR_EACH_PTR(tmp);
957 set_unset_to_zero(symbol, struct_type, member_set);
960 static void fake_member_assigns(struct symbol *sym, fake_cb *fake_cb)
962 fake_member_assigns_helper(symbol_expression(sym),
963 sym->initializer->expr_list, fake_cb);
966 static void fake_element_assigns_helper(struct expression *array, struct expression_list *expr_list, fake_cb *fake_cb)
968 struct expression *offset, *binop, *assign, *tmp;
969 struct symbol *type;
970 int idx;
972 idx = 0;
973 FOR_EACH_PTR(expr_list, tmp) {
974 if (tmp->type == EXPR_INDEX) {
975 if (tmp->idx_from != tmp->idx_to)
976 return;
977 idx = tmp->idx_from;
978 if (!tmp->idx_expression)
979 goto next;
980 tmp = tmp->idx_expression;
982 offset = value_expr(idx);
983 binop = array_element_expression(array, offset);
984 if (tmp->type == EXPR_INITIALIZER) {
985 type = get_type(binop);
986 if (type && type->type == SYM_ARRAY)
987 fake_element_assigns_helper(binop, tmp->expr_list, fake_cb);
988 else
989 fake_member_assigns_helper(binop, tmp->expr_list, fake_cb);
990 } else {
991 assign = assign_expression(binop, tmp);
992 fake_cb(assign);
994 next:
995 idx++;
996 } END_FOR_EACH_PTR(tmp);
999 static void fake_element_assigns(struct symbol *sym, fake_cb *fake_cb)
1001 fake_element_assigns_helper(symbol_expression(sym), sym->initializer->expr_list, fake_cb);
1004 static void fake_assign_expr(struct symbol *sym)
1006 struct expression *assign, *symbol;
1008 symbol = symbol_expression(sym);
1009 assign = assign_expression(symbol, sym->initializer);
1010 __split_expr(assign);
1013 static void call_split_expr(struct expression *expr)
1015 __split_expr(expr);
1018 static void do_initializer_stuff(struct symbol *sym)
1020 if (!sym->initializer)
1021 return;
1023 if (sym->initializer->type == EXPR_INITIALIZER) {
1024 if (get_real_base_type(sym)->type == SYM_ARRAY)
1025 fake_element_assigns(sym, call_split_expr);
1026 else
1027 fake_member_assigns(sym, call_split_expr);
1028 } else {
1029 fake_assign_expr(sym);
1033 static void split_declaration(struct symbol_list *sym_list)
1035 struct symbol *sym;
1037 FOR_EACH_PTR(sym_list, sym) {
1038 __pass_to_client(sym, DECLARATION_HOOK);
1039 do_initializer_stuff(sym);
1040 split_sym(sym);
1041 } END_FOR_EACH_PTR(sym);
1044 static void call_global_assign_hooks(struct expression *assign)
1046 __pass_to_client(assign, GLOBAL_ASSIGNMENT_HOOK);
1049 static void fake_global_assign(struct symbol *sym)
1051 struct expression *assign, *symbol;
1053 if (!sym->initializer)
1054 return;
1055 if (sym->initializer->type == EXPR_INITIALIZER) {
1056 if (get_real_base_type(sym)->type == SYM_ARRAY)
1057 fake_element_assigns(sym, call_global_assign_hooks);
1058 else
1059 fake_member_assigns(sym, call_global_assign_hooks);
1060 } else {
1061 symbol = symbol_expression(sym);
1062 assign = assign_expression(symbol, sym->initializer);
1063 __pass_to_client(assign, GLOBAL_ASSIGNMENT_HOOK);
1067 static void start_function_definition(struct symbol *sym)
1069 __in_function_def = 1;
1070 __pass_to_client(sym, FUNC_DEF_HOOK);
1071 __in_function_def = 0;
1072 __pass_to_client(sym, AFTER_DEF_HOOK);
1076 static void split_function(struct symbol *sym)
1078 struct symbol *base_type = get_base_type(sym);
1080 cur_func_sym = sym;
1081 if (sym->ident)
1082 cur_func = sym->ident->name;
1083 __smatch_lineno = sym->pos.line;
1084 last_stmt = NULL;
1085 loop_count = 0;
1086 sm_debug("new function: %s\n", cur_func);
1087 __slist_id = 0;
1088 if (option_two_passes) {
1089 __unnullify_path();
1090 loop_num = 0;
1091 final_pass = 0;
1092 start_function_definition(sym);
1093 __split_stmt(base_type->stmt);
1094 __split_stmt(base_type->inline_stmt);
1095 nullify_path();
1097 __unnullify_path();
1098 loop_num = 0;
1099 start_function_definition(sym);
1100 __split_stmt(base_type->stmt);
1101 __split_stmt(base_type->inline_stmt);
1102 __pass_to_client(sym, END_FUNC_HOOK);
1103 __pass_to_client(sym, AFTER_FUNC_HOOK);
1104 cur_func_sym = NULL;
1105 cur_func = NULL;
1106 clear_all_states();
1107 free_data_info_allocs();
1108 free_expression_stack(&switch_expr_stack);
1109 __free_ptr_list((struct ptr_list **)&big_statement_stack);
1110 __bail_on_rest_of_function = 0;
1113 static void parse_inline(struct expression *call)
1115 struct symbol *base_type;
1116 int loop_num_bak = loop_num;
1117 int final_pass_bak = final_pass;
1118 char *cur_func_bak = cur_func;
1119 struct statement_list *big_statement_stack_bak = big_statement_stack;
1120 struct expression_list *big_expression_stack_bak = big_expression_stack;
1121 struct expression_list *switch_expr_stack_bak = switch_expr_stack;
1122 struct symbol *cur_func_sym_bak = cur_func_sym;
1124 __pass_to_client(call, INLINE_FN_START);
1125 final_pass = 0; /* don't print anything */
1126 __inline_fn = call;
1128 base_type = get_base_type(call->fn->symbol);
1129 cur_func_sym = call->fn->symbol;
1130 if (call->fn->symbol->ident)
1131 cur_func = call->fn->symbol->ident->name;
1132 else
1133 cur_func = NULL;
1134 set_position(call->fn->symbol->pos);
1136 save_all_states();
1137 nullify_all_states();
1138 big_statement_stack = NULL;
1139 big_expression_stack = NULL;
1140 switch_expr_stack = NULL;
1142 sm_debug("inline function: %s\n", cur_func);
1143 __unnullify_path();
1144 loop_num = 0;
1145 start_function_definition(call->fn->symbol);
1146 __split_stmt(base_type->stmt);
1147 __split_stmt(base_type->inline_stmt);
1148 __pass_to_client(call->fn->symbol, END_FUNC_HOOK);
1149 __pass_to_client(call->fn->symbol, AFTER_FUNC_HOOK);
1151 free_expression_stack(&switch_expr_stack);
1152 __free_ptr_list((struct ptr_list **)&big_statement_stack);
1153 nullify_path();
1155 loop_num = loop_num_bak;
1156 final_pass = final_pass_bak;
1157 cur_func_sym = cur_func_sym_bak;
1158 cur_func = cur_func_bak;
1159 big_statement_stack = big_statement_stack_bak;
1160 big_expression_stack = big_expression_stack_bak;
1161 switch_expr_stack = switch_expr_stack_bak;
1163 restore_all_states();
1164 set_position(call->pos);
1165 __inline_fn = NULL;
1166 __pass_to_client(call, INLINE_FN_END);
1169 static struct symbol_list *inlines_called;
1170 static void add_inline_function(struct symbol *sym)
1172 static struct symbol_list *already_added;
1173 struct symbol *tmp;
1175 FOR_EACH_PTR(already_added, tmp) {
1176 if (tmp == sym)
1177 return;
1178 } END_FOR_EACH_PTR(tmp);
1180 add_ptr_list(&already_added, sym);
1181 add_ptr_list(&inlines_called, sym);
1184 static void process_inlines()
1186 struct symbol *tmp;
1188 FOR_EACH_PTR(inlines_called, tmp) {
1189 split_function(tmp);
1190 } END_FOR_EACH_PTR(tmp);
1191 free_ptr_list(&inlines_called);
1194 static struct symbol *get_last_scoped_symbol(struct symbol_list *big_list, int use_static)
1196 struct symbol *sym;
1198 FOR_EACH_PTR_REVERSE(big_list, sym) {
1199 if (!sym->scope)
1200 continue;
1201 if (use_static && sym->ctype.modifiers & MOD_STATIC)
1202 return sym;
1203 if (!use_static && !(sym->ctype.modifiers & MOD_STATIC))
1204 return sym;
1205 } END_FOR_EACH_PTR_REVERSE(sym);
1207 return NULL;
1210 static void split_inlines_in_scope(struct symbol *sym)
1212 struct symbol *base;
1213 struct symbol_list *scope_list;
1214 int stream;
1216 scope_list = sym->scope->symbols;
1217 stream = sym->pos.stream;
1219 /* find the last static symbol in the file */
1220 FOR_EACH_PTR_REVERSE(scope_list, sym) {
1221 if (sym->pos.stream != stream)
1222 continue;
1223 if (sym->type != SYM_NODE)
1224 continue;
1225 base = get_base_type(sym);
1226 if (!base)
1227 continue;
1228 if (base->type != SYM_FN)
1229 continue;
1230 if (!base->inline_stmt)
1231 continue;
1232 add_inline_function(sym);
1233 } END_FOR_EACH_PTR_REVERSE(sym);
1235 process_inlines();
1238 static void split_inlines(struct symbol_list *sym_list)
1240 struct symbol *sym;
1242 sym = get_last_scoped_symbol(sym_list, 0);
1243 if (sym)
1244 split_inlines_in_scope(sym);
1245 sym = get_last_scoped_symbol(sym_list, 1);
1246 if (sym)
1247 split_inlines_in_scope(sym);
1250 static void split_functions(struct symbol_list *sym_list)
1252 struct symbol *sym;
1254 FOR_EACH_PTR(sym_list, sym) {
1255 set_position(sym->pos);
1256 if (sym->type == SYM_NODE && get_base_type(sym)->type == SYM_FN) {
1257 split_function(sym);
1258 process_inlines();
1259 } else {
1260 __pass_to_client(sym, BASE_HOOK);
1261 fake_global_assign(sym);
1263 } END_FOR_EACH_PTR(sym);
1264 split_inlines(sym_list);
1265 __pass_to_client(sym_list, END_FILE_HOOK);
1268 void smatch(int argc, char **argv)
1271 struct string_list *filelist = NULL;
1272 struct symbol_list *sym_list;
1274 if (argc < 2) {
1275 printf("Usage: smatch [--debug] <filename.c>\n");
1276 exit(1);
1278 sparse_initialize(argc, argv, &filelist);
1279 FOR_EACH_PTR_NOTAG(filelist, base_file) {
1280 if (option_file_output) {
1281 char buf[256];
1283 snprintf(buf, sizeof(buf), "%s.smatch", base_file);
1284 sm_outfd = fopen(buf, "w");
1285 if (!sm_outfd) {
1286 printf("Error: Cannot open %s\n", base_file);
1287 exit(1);
1290 sym_list = sparse_keep_tokens(base_file);
1291 split_functions(sym_list);
1292 } END_FOR_EACH_PTR_NOTAG(base_file);