Merge branch 'buf_size' into devel
[smatch.git] / smatch_flow.c
blobcedf768e66ec33883fc6052b49fac7d1239feb9d
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 "smatch.h"
15 #include "smatch_expression_stacks.h"
16 #include "smatch_extra.h"
17 #include "smatch_slist.h"
19 int final_pass;
21 static int __smatch_lineno = 0;
23 static char *base_file;
24 static const char *filename;
25 static char *pathname;
26 static char *full_filename;
27 static char *cur_func;
28 static int line_func_start;
29 static int loop_count;
30 int __expr_stmt_count;
31 static struct expression_list *switch_expr_stack = NULL;
33 struct expression_list *big_expression_stack;
34 struct statement_list *big_statement_stack;
35 int __in_pre_condition = 0;
36 int __bail_on_rest_of_function = 0;
37 char *get_function(void) { return cur_func; }
38 int get_lineno(void) { return __smatch_lineno; }
39 int inside_loop(void) { return !!loop_count; }
40 int in_expression_statement(void) { return !!__expr_stmt_count; }
42 static void split_symlist(struct symbol_list *sym_list);
43 static void split_declaration(struct symbol_list *sym_list);
44 static void split_expr_list(struct expression_list *expr_list);
45 static void add_inline_function(struct symbol *sym);
47 int option_assume_loops = 0;
48 int option_known_conditions = 0;
49 int option_two_passes = 0;
50 struct symbol *cur_func_sym = NULL;
52 const char *get_filename(void)
54 if (option_info)
55 return base_file;
56 if (option_full_path)
57 return full_filename;
58 return filename;
61 static void set_position(struct position pos)
63 int len;
64 static int prev_stream = -1;
66 __smatch_lineno = pos.line;
68 if (pos.stream == prev_stream)
69 return;
71 filename = stream_name(pos.stream);
73 free(full_filename);
74 pathname = getcwd(NULL, 0);
75 if (pathname) {
76 len = strlen(pathname) + 1 + strlen(filename) + 1;
77 full_filename = malloc(len);
78 snprintf(full_filename, len, "%s/%s", pathname, filename);
79 } else {
80 full_filename = alloc_string(filename);
82 free(pathname);
85 static int is_inline_func(struct expression *expr)
87 if (expr->type != EXPR_SYMBOL || !expr->symbol)
88 return 0;
89 if (expr->symbol->ctype.modifiers & MOD_INLINE)
90 return 1;
91 return 0;
94 void __split_expr(struct expression *expr)
96 if (!expr)
97 return;
99 // sm_msg(" Debug expr_type %d %s", expr->type, show_special(expr->op));
101 push_expression(&big_expression_stack, expr);
102 set_position(expr->pos);
103 __pass_to_client(expr, EXPR_HOOK);
105 switch (expr->type) {
106 case EXPR_PREOP:
107 if (expr->op == '*')
108 __pass_to_client(expr, DEREF_HOOK);
109 case EXPR_POSTOP:
110 __pass_to_client(expr, OP_HOOK);
111 __split_expr(expr->unop);
112 break;
113 case EXPR_STATEMENT:
114 __expr_stmt_count++;
115 __split_stmt(expr->statement);
116 __expr_stmt_count--;
117 break;
118 case EXPR_LOGICAL:
119 case EXPR_COMPARE:
120 __pass_to_client(expr, LOGIC_HOOK);
121 __handle_logic(expr);
122 break;
123 case EXPR_BINOP:
124 __pass_to_client(expr, BINOP_HOOK);
125 case EXPR_COMMA:
126 __split_expr(expr->left);
127 __split_expr(expr->right);
128 break;
129 case EXPR_ASSIGNMENT: {
130 struct expression *tmp;
132 if (!expr->right)
133 break;
135 __pass_to_client(expr, RAW_ASSIGNMENT_HOOK);
137 /* foo = !bar() */
138 if (__handle_condition_assigns(expr))
139 break;
140 /* foo = (x < 5 ? foo : 5); */
141 if (__handle_select_assigns(expr))
142 break;
143 /* foo = ({frob(); frob(); frob(); 1;}) */
144 if (__handle_expr_statement_assigns(expr))
145 break;
147 __split_expr(expr->right);
148 __pass_to_client(expr, ASSIGNMENT_HOOK);
149 tmp = strip_expr(expr->right);
150 if (tmp->type == EXPR_CALL)
151 __pass_to_client(expr, CALL_ASSIGNMENT_HOOK);
152 if (get_macro_name(tmp->pos))
153 __pass_to_client(expr, MACRO_ASSIGNMENT_HOOK);
154 __split_expr(expr->left);
155 break;
157 case EXPR_DEREF:
158 __pass_to_client(expr, DEREF_HOOK);
159 __split_expr(expr->deref);
160 break;
161 case EXPR_SLICE:
162 __split_expr(expr->base);
163 break;
164 case EXPR_CAST:
165 case EXPR_FORCE_CAST:
166 __pass_to_client(expr, CAST_HOOK);
167 __split_expr(expr->cast_expression);
168 break;
169 case EXPR_SIZEOF:
170 /* there isn't anything to pass a client from inside a sizeof() */
171 break;
172 case EXPR_OFFSETOF:
173 case EXPR_ALIGNOF:
174 evaluate_expression(expr);
175 break;
176 case EXPR_CONDITIONAL:
177 case EXPR_SELECT:
178 if (known_condition_true(expr->conditional)) {
179 __split_expr(expr->cond_true);
180 break;
182 if (known_condition_false(expr->conditional)) {
183 __split_expr(expr->cond_false);
184 break;
186 __pass_to_client(expr, SELECT_HOOK);
187 __split_whole_condition(expr->conditional);
188 __split_expr(expr->cond_true);
189 __push_true_states();
190 __use_false_states();
191 __split_expr(expr->cond_false);
192 __merge_true_states();
193 break;
194 case EXPR_CALL:
195 if (sym_name_is("__builtin_constant_p", expr->fn))
196 break;
197 split_expr_list(expr->args);
198 __split_expr(expr->fn);
199 if (is_inline_func(expr->fn))
200 add_inline_function(expr->fn->symbol);
201 __pass_to_client(expr, FUNCTION_CALL_HOOK);
202 break;
203 case EXPR_INITIALIZER:
204 split_expr_list(expr->expr_list);
205 break;
206 case EXPR_IDENTIFIER:
207 __split_expr(expr->ident_expression);
208 break;
209 case EXPR_INDEX:
210 __split_expr(expr->idx_expression);
211 break;
212 case EXPR_POS:
213 __split_expr(expr->init_expr);
214 break;
215 case EXPR_SYMBOL:
216 __pass_to_client(expr, SYM_HOOK);
217 break;
218 case EXPR_STRING:
219 __pass_to_client(expr, STRING_HOOK);
220 break;
221 default:
222 break;
224 pop_expression(&big_expression_stack);
227 static int is_forever_loop(struct statement *stmt)
229 struct expression *expr;
231 expr = strip_expr(stmt->iterator_pre_condition);
232 if (!expr)
233 expr = stmt->iterator_post_condition;
234 if (!expr) {
235 /* this is a for(;;) loop... */
236 return 1;
239 if (expr->type == EXPR_VALUE && expr->value == 1)
240 return 1;
242 return 0;
245 static int loop_num;
246 static char *get_loop_name(int num)
248 char buf[256];
250 snprintf(buf, 255, "-loop%d", num);
251 buf[255] = '\0';
252 return alloc_sname(buf);
256 * Pre Loops are while and for loops.
258 static void handle_pre_loop(struct statement *stmt)
260 int once_through; /* we go through the loop at least once */
261 struct sm_state *extra_sm = NULL;
262 int unchanged = 0;
263 char *loop_name;
264 struct state_list *slist = NULL;
265 struct sm_state *sm = NULL;
267 loop_name = get_loop_name(loop_num);
268 loop_num++;
270 __split_stmt(stmt->iterator_pre_statement);
272 once_through = implied_condition_true(stmt->iterator_pre_condition);
274 loop_count++;
275 __push_continues();
276 __push_breaks();
278 __merge_gotos(loop_name);
280 extra_sm = __extra_handle_canonical_loops(stmt, &slist);
281 __in_pre_condition++;
282 __pass_to_client(stmt, PRELOOP_HOOK);
283 __split_whole_condition(stmt->iterator_pre_condition);
284 __in_pre_condition--;
285 FOR_EACH_PTR(slist, sm) {
286 set_state(sm->owner, sm->name, sm->sym, sm->state);
287 } END_FOR_EACH_PTR(sm);
288 free_slist(&slist);
289 if (extra_sm)
290 extra_sm = get_sm_state(extra_sm->owner, extra_sm->name, extra_sm->sym);
292 if (option_assume_loops)
293 once_through = 1;
295 __split_stmt(stmt->iterator_statement);
296 __warn_on_silly_pre_loops();
297 if (is_forever_loop(stmt)) {
298 struct state_list *slist;
300 __save_gotos(loop_name);
302 __push_fake_cur_slist();
303 __split_stmt(stmt->iterator_post_statement);
304 slist = __pop_fake_cur_slist();
306 __discard_continues();
307 __discard_false_states();
308 __use_breaks();
310 if (!__path_is_null())
311 __merge_slist_into_cur(slist);
312 free_slist(&slist);
313 } else {
314 __merge_continues();
315 unchanged = __iterator_unchanged(extra_sm);
316 __split_stmt(stmt->iterator_post_statement);
317 __save_gotos(loop_name);
318 __split_whole_condition(stmt->iterator_pre_condition);
319 nullify_path();
320 __merge_false_states();
321 if (once_through)
322 __discard_false_states();
323 else
324 __merge_false_states();
326 if (extra_sm && unchanged)
327 __extra_pre_loop_hook_after(extra_sm,
328 stmt->iterator_post_statement,
329 stmt->iterator_pre_condition);
330 __merge_breaks();
332 loop_count--;
336 * Post loops are do {} while();
338 static void handle_post_loop(struct statement *stmt)
340 char *loop_name;
342 loop_name = get_loop_name(loop_num);
343 loop_num++;
344 loop_count++;
346 __push_continues();
347 __push_breaks();
348 __merge_gotos(loop_name);
349 __split_stmt(stmt->iterator_statement);
350 __merge_continues();
351 if (!is_zero(stmt->iterator_post_condition))
352 __save_gotos(loop_name);
354 if (is_forever_loop(stmt)) {
355 __use_breaks();
356 } else {
357 __split_whole_condition(stmt->iterator_post_condition);
358 __use_false_states();
359 __merge_breaks();
361 loop_count--;
364 static int empty_statement(struct statement *stmt)
366 if (!stmt)
367 return 0;
368 if (stmt->type == STMT_EXPRESSION && !stmt->expression)
369 return 1;
370 return 0;
373 static int last_stmt_on_same_line()
375 struct statement *stmt;
376 int i = 0;
378 FOR_EACH_PTR_REVERSE(big_statement_stack, stmt) {
379 if (!i++)
380 continue;
381 if (stmt->pos.line == get_lineno())
382 return 1;
383 return 0;
384 } END_FOR_EACH_PTR_REVERSE(stmt);
385 return 0;
388 static struct statement *last_stmt;
389 static int is_last_stmt(struct statement *stmt)
391 if (stmt == last_stmt)
392 return 1;
393 return 0;
396 static void print_unreached_initializers(struct symbol_list *sym_list)
398 struct symbol *sym;
400 FOR_EACH_PTR(sym_list, sym) {
401 if (sym->initializer)
402 sm_msg("info: '%s' is not actually initialized (unreached code).",
403 (sym->ident ? sym->ident->name : "this variable"));
404 } END_FOR_EACH_PTR(sym);
407 static void print_unreached(struct statement *stmt)
409 static int print = 1;
411 if (!__path_is_null()) {
412 print = 1;
413 return;
415 if (!print)
416 return;
418 switch (stmt->type) {
419 case STMT_COMPOUND: /* after a switch before a case stmt */
420 case STMT_RANGE:
421 case STMT_CASE:
422 case STMT_LABEL:
423 return;
424 case STMT_DECLARATION: /* switch (x) { int a; case foo: ... */
425 print_unreached_initializers(stmt->declaration);
426 return;
427 case STMT_RETURN: /* gcc complains if you don't have a return statement */
428 if (is_last_stmt(stmt))
429 return;
430 break;
431 case STMT_GOTO:
432 if (!option_spammy)
433 return;
434 break;
435 default:
436 break;
438 if (!option_spammy && empty_statement(stmt))
439 return;
440 sm_msg("info: ignoring unreachable code.");
441 print = 0;
444 static void split_asm_constraints(struct expression_list *expr_list)
446 struct expression *expr;
447 int state = 0;
449 FOR_EACH_PTR(expr_list, expr) {
450 switch (state) {
451 case 0: /* identifier */
452 case 1: /* constraint */
453 state++;
454 continue;
455 case 2: /* expression */
456 state = 0;
457 __split_expr(expr);
458 continue;
460 } END_FOR_EACH_PTR(expr);
463 static int is_case_val(struct statement *stmt, long long val)
465 long long case_val;
467 if (stmt->type != STMT_CASE)
468 return 0;
469 if (!stmt->case_expression) {
470 __set_default();
471 return 1;
473 if (!get_value(stmt->case_expression, &case_val))
474 return 0;
475 if (case_val == val)
476 return 1;
477 return 0;
480 static void split_known_switch(struct statement *stmt, long long val)
482 struct statement *tmp;
484 __split_expr(stmt->switch_expression);
486 push_expression(&switch_expr_stack, stmt->switch_expression);
487 __save_switch_states(top_expression(switch_expr_stack));
488 nullify_path();
489 __push_default();
490 __push_breaks();
492 stmt = stmt->switch_statement;
494 if (!last_stmt)
495 last_stmt = last_ptr_list((struct ptr_list *)stmt->stmts);
497 __push_scope_hooks();
498 FOR_EACH_PTR(stmt->stmts, tmp) {
499 __smatch_lineno = tmp->pos.line;
500 if (is_case_val(tmp, val)) {
501 __merge_switches(top_expression(switch_expr_stack),
502 stmt->case_expression);
503 __pass_case_to_client(top_expression(switch_expr_stack),
504 stmt->case_expression);
506 if (__path_is_null())
507 continue;
508 __split_stmt(tmp);
509 if (__path_is_null()) {
510 __set_default();
511 goto out;
513 } END_FOR_EACH_PTR(tmp);
514 out:
515 __call_scope_hooks();
516 if (!__pop_default())
517 __merge_switches(top_expression(switch_expr_stack),
518 NULL);
519 __discard_switches();
520 __merge_breaks();
521 pop_expression(&switch_expr_stack);
524 void __split_stmt(struct statement *stmt)
526 long long val;
528 if (!stmt)
529 return;
531 if (out_of_memory() || __bail_on_rest_of_function) {
532 static char *printed = NULL;
534 if (printed != cur_func)
535 sm_msg("Function too hairy. Giving up.");
536 final_pass = 0; /* turn off sm_msg() from here */
537 printed = cur_func;
538 return;
541 add_ptr_list(&big_statement_stack, stmt);
542 free_expression_stack(&big_expression_stack);
543 set_position(stmt->pos);
544 print_unreached(stmt);
545 __pass_to_client(stmt, STMT_HOOK);
547 switch (stmt->type) {
548 case STMT_DECLARATION:
549 split_declaration(stmt->declaration);
550 return;
551 case STMT_RETURN:
552 __split_expr(stmt->ret_value);
553 __pass_to_client(stmt->ret_value, RETURN_HOOK);
554 nullify_path();
555 return;
556 case STMT_EXPRESSION:
557 __split_expr(stmt->expression);
558 return;
559 case STMT_COMPOUND: {
560 struct statement *tmp;
562 if (!last_stmt)
563 last_stmt = last_ptr_list((struct ptr_list *)stmt->stmts);
564 __push_scope_hooks();
565 FOR_EACH_PTR(stmt->stmts, tmp) {
566 __split_stmt(tmp);
567 } END_FOR_EACH_PTR(tmp);
568 __call_scope_hooks();
569 return;
571 case STMT_IF:
572 if (known_condition_true(stmt->if_conditional)) {
573 __split_stmt(stmt->if_true);
574 return;
576 if (known_condition_false(stmt->if_conditional)) {
577 __split_stmt(stmt->if_false);
578 return;
580 if (option_known_conditions &&
581 implied_condition_true(stmt->if_conditional)) {
582 sm_info("this condition is true.");
583 __split_stmt(stmt->if_true);
584 return;
586 if (option_known_conditions &&
587 implied_condition_false(stmt->if_conditional)) {
588 sm_info("this condition is false.");
589 __split_stmt(stmt->if_false);
590 return;
592 __split_whole_condition(stmt->if_conditional);
593 __split_stmt(stmt->if_true);
594 if (empty_statement(stmt->if_true) &&
595 last_stmt_on_same_line() &&
596 !get_macro_name(stmt->if_true->pos))
597 sm_msg("warn: if();");
598 __push_true_states();
599 __use_false_states();
600 __split_stmt(stmt->if_false);
601 __merge_true_states();
602 return;
603 case STMT_ITERATOR:
604 if (stmt->iterator_pre_condition)
605 handle_pre_loop(stmt);
606 else if (stmt->iterator_post_condition)
607 handle_post_loop(stmt);
608 else {
609 // these are for(;;) type loops.
610 handle_pre_loop(stmt);
612 return;
613 case STMT_SWITCH:
614 if (get_value(stmt->switch_expression, &val)) {
615 split_known_switch(stmt, val);
616 return;
618 __split_expr(stmt->switch_expression);
619 push_expression(&switch_expr_stack, stmt->switch_expression);
620 __save_switch_states(top_expression(switch_expr_stack));
621 nullify_path();
622 __push_default();
623 __push_breaks();
624 __split_stmt(stmt->switch_statement);
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);
631 return;
632 case STMT_CASE:
633 __merge_switches(top_expression(switch_expr_stack),
634 stmt->case_expression);
635 __pass_case_to_client(top_expression(switch_expr_stack),
636 stmt->case_expression);
637 if (!stmt->case_expression)
638 __set_default();
639 __split_expr(stmt->case_expression);
640 __split_expr(stmt->case_to);
641 __split_stmt(stmt->case_statement);
642 return;
643 case STMT_LABEL:
644 if (stmt->label_identifier &&
645 stmt->label_identifier->type == SYM_LABEL &&
646 stmt->label_identifier->ident) {
647 loop_count = 1000000;
648 __merge_gotos(stmt->label_identifier->ident->name);
650 __split_stmt(stmt->label_statement);
651 return;
652 case STMT_GOTO:
653 __split_expr(stmt->goto_expression);
654 if (stmt->goto_label && stmt->goto_label->type == SYM_NODE) {
655 if (!strcmp(stmt->goto_label->ident->name, "break")) {
656 __process_breaks();
657 } else if (!strcmp(stmt->goto_label->ident->name,
658 "continue")) {
659 __process_continues();
661 } else if (stmt->goto_label &&
662 stmt->goto_label->type == SYM_LABEL &&
663 stmt->goto_label->ident) {
664 __save_gotos(stmt->goto_label->ident->name);
666 nullify_path();
667 return;
668 case STMT_NONE:
669 return;
670 case STMT_ASM:
671 __pass_to_client(stmt, ASM_HOOK);
672 __split_expr(stmt->asm_string);
673 split_asm_constraints(stmt->asm_outputs);
674 split_asm_constraints(stmt->asm_inputs);
675 split_asm_constraints(stmt->asm_clobbers);
676 return;
677 case STMT_CONTEXT:
678 return;
679 case STMT_RANGE:
680 __split_expr(stmt->range_expression);
681 __split_expr(stmt->range_low);
682 __split_expr(stmt->range_high);
683 return;
687 static void split_expr_list(struct expression_list *expr_list)
689 struct expression *expr;
691 FOR_EACH_PTR(expr_list, expr) {
692 __split_expr(expr);
693 } END_FOR_EACH_PTR(expr);
696 static void split_sym(struct symbol *sym)
698 if (!sym)
699 return;
700 if (!(sym->namespace & NS_SYMBOL))
701 return;
703 __split_stmt(sym->stmt);
704 __split_expr(sym->array_size);
705 split_symlist(sym->arguments);
706 split_symlist(sym->symbol_list);
707 __split_stmt(sym->inline_stmt);
708 split_symlist(sym->inline_symbol_list);
711 static void split_symlist(struct symbol_list *sym_list)
713 struct symbol *sym;
715 FOR_EACH_PTR(sym_list, sym) {
716 split_sym(sym);
717 } END_FOR_EACH_PTR(sym);
720 static void fake_member_assigns(struct symbol *sym)
722 struct expression *symbol, *deref, *assign, *tmp;
724 symbol = symbol_expression(sym);
725 FOR_EACH_PTR(sym->initializer->expr_list, tmp) {
726 if (tmp->type != EXPR_IDENTIFIER) /* how to handle arrays?? */
727 continue;
728 deref = deref_expression(symbol, '.', tmp->expr_ident);
729 assign = assign_expression(deref, tmp->ident_expression);
730 __split_expr(assign);
731 } END_FOR_EACH_PTR(tmp);
734 static void fake_assign_expr(struct symbol *sym)
736 struct expression *assign, *symbol;
738 symbol = symbol_expression(sym);
739 assign = assign_expression(symbol, sym->initializer);
740 __split_expr(assign);
743 static void do_initializer_stuff(struct symbol *sym)
745 if (!sym->initializer)
746 return;
747 if (sym->initializer->type == EXPR_INITIALIZER)
748 fake_member_assigns(sym);
749 else
750 fake_assign_expr(sym);
753 static void split_declaration(struct symbol_list *sym_list)
755 struct symbol *sym;
757 FOR_EACH_PTR(sym_list, sym) {
758 __pass_to_client(sym, DECLARATION_HOOK);
759 do_initializer_stuff(sym);
760 split_sym(sym);
761 } END_FOR_EACH_PTR(sym);
764 static void split_function(struct symbol *sym)
766 struct symbol *base_type = get_base_type(sym);
768 cur_func_sym = sym;
769 if (base_type->stmt)
770 line_func_start = base_type->stmt->pos.line;
771 if (sym->ident)
772 cur_func = sym->ident->name;
773 __smatch_lineno = sym->pos.line;
774 last_stmt = NULL;
775 loop_count = 0;
776 sm_debug("new function: %s\n", cur_func);
777 __slist_id = 0;
778 if (option_two_passes) {
779 __unnullify_path();
780 loop_num = 0;
781 final_pass = 0;
782 __pass_to_client(sym, FUNC_DEF_HOOK);
783 __split_stmt(base_type->stmt);
784 __split_stmt(base_type->inline_stmt);
785 nullify_path();
787 __unnullify_path();
788 loop_num = 0;
789 final_pass = 1;
790 __pass_to_client(sym, FUNC_DEF_HOOK);
791 __split_stmt(base_type->stmt);
792 __split_stmt(base_type->inline_stmt);
793 __pass_to_client(sym, END_FUNC_HOOK);
794 cur_func = NULL;
795 line_func_start = 0;
796 clear_all_states();
797 free_data_info_allocs();
798 free_expression_stack(&switch_expr_stack);
799 __free_ptr_list((struct ptr_list **)&big_statement_stack);
800 __bail_on_rest_of_function = 0;
803 static struct symbol_list *inlines_called;
804 static void add_inline_function(struct symbol *sym)
806 static struct symbol_list *already_added;
807 struct symbol *tmp;
809 FOR_EACH_PTR(already_added, tmp) {
810 if (tmp == sym)
811 return;
812 } END_FOR_EACH_PTR(tmp);
814 add_ptr_list(&already_added, sym);
815 add_ptr_list(&inlines_called, sym);
818 static void process_inlines()
820 struct symbol *tmp;
822 FOR_EACH_PTR(inlines_called, tmp) {
823 split_function(tmp);
824 } END_FOR_EACH_PTR(tmp);
825 free_ptr_list(&inlines_called);
828 static void split_functions(struct symbol_list *sym_list)
830 struct symbol *sym;
832 FOR_EACH_PTR(sym_list, sym) {
833 set_position(sym->pos);
834 if (sym->type == SYM_NODE && get_base_type(sym)->type == SYM_FN) {
835 split_function(sym);
836 process_inlines();
837 } else {
838 __pass_to_client(sym, BASE_HOOK);
840 } END_FOR_EACH_PTR(sym);
841 __pass_to_client_no_data(END_FILE_HOOK);
844 void smatch(int argc, char **argv)
847 struct string_list *filelist = NULL;
848 struct symbol_list *sym_list;
850 if (argc < 2) {
851 printf("Usage: smatch [--debug] <filename.c>\n");
852 exit(1);
854 sparse_initialize(argc, argv, &filelist);
855 FOR_EACH_PTR_NOTAG(filelist, base_file) {
856 if (option_file_output) {
857 char buf[256];
859 snprintf(buf, sizeof(buf), "%s.smatch", base_file);
860 sm_outfd = fopen(buf, "w");
861 if (!sm_outfd) {
862 printf("Error: Cannot open %s\n", base_file);
863 exit(1);
866 sym_list = sparse_keep_tokens(base_file);
867 split_functions(sym_list);
868 } END_FOR_EACH_PTR_NOTAG(base_file);