overflow: silence glibc strcmp false positives with string literal
[smatch.git] / smatch_flow.c
blob85492aec39b7f4c203df9a139a4efa4551ec0265
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, struct expression *parent);
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 static void set_parent(struct expression *expr, struct expression *parent)
143 if (!expr)
144 return;
145 expr->parent = parent;
148 static void set_parent_stmt(struct statement *stmt, struct statement *parent)
150 if (!stmt)
151 return;
152 stmt->parent = parent;
155 int is_assigned_call(struct expression *expr)
157 struct expression *tmp;
159 FOR_EACH_PTR_REVERSE(big_expression_stack, tmp) {
160 if (tmp->type == EXPR_ASSIGNMENT && tmp->op == '=' &&
161 strip_expr(tmp->right) == expr)
162 return 1;
163 if (tmp->pos.line < expr->pos.line)
164 return 0;
165 } END_FOR_EACH_PTR_REVERSE(tmp);
166 return 0;
169 static int is_inline_func(struct expression *expr)
171 if (expr->type != EXPR_SYMBOL || !expr->symbol)
172 return 0;
173 if (expr->symbol->ctype.modifiers & MOD_INLINE)
174 return 1;
175 return 0;
178 static int is_noreturn_func(struct expression *expr)
180 if (expr->type != EXPR_SYMBOL || !expr->symbol)
181 return 0;
182 if (expr->symbol->ctype.modifiers & MOD_NORETURN)
183 return 1;
184 return 0;
187 int inlinable(struct expression *expr)
189 struct symbol *sym;
190 struct statement *last_stmt = NULL;
192 if (__inline_fn) /* don't nest */
193 return 0;
195 if (expr->type != EXPR_SYMBOL || !expr->symbol)
196 return 0;
197 if (is_no_inline_function(expr->symbol->ident->name))
198 return 0;
199 sym = get_base_type(expr->symbol);
200 if (sym->stmt && sym->stmt->type == STMT_COMPOUND) {
201 if (ptr_list_size((struct ptr_list *)sym->stmt->stmts) > 10)
202 return 0;
203 if (sym->stmt->type != STMT_COMPOUND)
204 return 0;
205 last_stmt = last_ptr_list((struct ptr_list *)sym->stmt->stmts);
207 if (sym->inline_stmt && sym->inline_stmt->type == STMT_COMPOUND) {
208 if (ptr_list_size((struct ptr_list *)sym->inline_stmt->stmts) > 10)
209 return 0;
210 if (sym->inline_stmt->type != STMT_COMPOUND)
211 return 0;
212 last_stmt = last_ptr_list((struct ptr_list *)sym->inline_stmt->stmts);
215 if (!last_stmt)
216 return 0;
218 /* the magic numbers in this function are pulled out of my bum. */
219 if (last_stmt->pos.line > sym->pos.line + 20)
220 return 0;
222 return 1;
225 void __process_post_op_stack(void)
227 struct expression *expr;
229 FOR_EACH_PTR(post_op_stack, expr) {
230 __pass_to_client(expr, OP_HOOK);
231 } END_FOR_EACH_PTR(expr);
233 __free_ptr_list((struct ptr_list **)&post_op_stack);
236 static int handle_comma_assigns(struct expression *expr)
238 struct expression *right;
239 struct expression *assign;
241 right = strip_expr(expr->right);
242 if (right->type != EXPR_COMMA)
243 return 0;
245 __split_expr(right->left);
246 __process_post_op_stack();
248 assign = assign_expression(expr->left, right->right);
249 __split_expr(assign);
251 return 1;
254 static int prev_expression_is_getting_address(struct expression *expr)
256 struct expression *parent;
258 do {
259 parent = expr->parent;
261 if (!parent)
262 return 0;
263 if (parent->type == EXPR_PREOP && parent->op == '&')
264 return 1;
265 if (parent->type == EXPR_PREOP && parent->op == '(')
266 goto next;
267 if (parent->type == EXPR_DEREF && parent->op == '.')
268 goto next;
270 return 0;
271 next:
272 expr = parent;
273 } while (1);
276 void __split_expr(struct expression *expr)
278 if (!expr)
279 return;
281 // sm_msg(" Debug expr_type %d %s", expr->type, show_special(expr->op));
283 if (__in_fake_assign && expr->type != EXPR_ASSIGNMENT)
284 return;
285 if (__in_fake_assign >= 4) /* don't allow too much nesting */
286 return;
288 push_expression(&big_expression_stack, expr);
289 set_position(expr->pos);
290 __pass_to_client(expr, EXPR_HOOK);
292 switch (expr->type) {
293 case EXPR_PREOP:
294 set_parent(expr->unop, expr);
296 if (expr->op == '*' &&
297 !prev_expression_is_getting_address(expr))
298 __pass_to_client(expr, DEREF_HOOK);
299 __split_expr(expr->unop);
300 __pass_to_client(expr, OP_HOOK);
301 break;
302 case EXPR_POSTOP:
303 set_parent(expr->unop, expr);
305 __split_expr(expr->unop);
306 push_expression(&post_op_stack, expr);
307 break;
308 case EXPR_STATEMENT:
309 __expr_stmt_count++;
310 __split_stmt(expr->statement);
311 __expr_stmt_count--;
312 break;
313 case EXPR_LOGICAL:
314 case EXPR_COMPARE:
315 set_parent(expr->left, expr);
316 set_parent(expr->right, expr);
318 __pass_to_client(expr, LOGIC_HOOK);
319 __handle_logic(expr);
320 break;
321 case EXPR_BINOP:
322 set_parent(expr->left, expr);
323 set_parent(expr->right, expr);
325 __pass_to_client(expr, BINOP_HOOK);
326 case EXPR_COMMA:
327 set_parent(expr->left, expr);
328 set_parent(expr->right, expr);
330 __split_expr(expr->left);
331 __process_post_op_stack();
332 __split_expr(expr->right);
333 break;
334 case EXPR_ASSIGNMENT: {
335 struct expression *tmp;
337 set_parent(expr->left, expr);
338 set_parent(expr->right, expr);
340 if (!expr->right)
341 break;
343 __pass_to_client(expr, RAW_ASSIGNMENT_HOOK);
345 /* foo = !bar() */
346 if (__handle_condition_assigns(expr))
347 break;
348 /* foo = (x < 5 ? foo : 5); */
349 if (__handle_select_assigns(expr))
350 break;
351 /* foo = ({frob(); frob(); frob(); 1;}) */
352 if (__handle_expr_statement_assigns(expr))
353 break;
354 /* foo = (3, 4); */
355 if (handle_comma_assigns(expr))
356 break;
358 __split_expr(expr->right);
359 if (outside_of_function())
360 __pass_to_client(expr, GLOBAL_ASSIGNMENT_HOOK);
361 else
362 __pass_to_client(expr, ASSIGNMENT_HOOK);
364 __fake_struct_member_assignments(expr);
366 tmp = strip_expr(expr->right);
367 if (expr->op == '=' && tmp->type == EXPR_CALL) {
368 __pass_to_client(expr, CALL_ASSIGNMENT_HOOK);
369 if (!is_fake_call(tmp))
370 __pass_to_client(tmp, FUNCTION_CALL_HOOK_AFTER);
372 if (get_macro_name(tmp->pos) &&
373 get_macro_name(expr->pos) != get_macro_name(tmp->pos))
374 __pass_to_client(expr, MACRO_ASSIGNMENT_HOOK);
375 __split_expr(expr->left);
376 break;
378 case EXPR_DEREF:
379 set_parent(expr->deref, expr);
381 __pass_to_client(expr, DEREF_HOOK);
382 __split_expr(expr->deref);
383 break;
384 case EXPR_SLICE:
385 set_parent(expr->base, expr);
387 __split_expr(expr->base);
388 break;
389 case EXPR_CAST:
390 case EXPR_FORCE_CAST:
391 set_parent(expr->cast_expression, expr);
393 __pass_to_client(expr, CAST_HOOK);
394 __split_expr(expr->cast_expression);
395 break;
396 case EXPR_SIZEOF:
397 if (expr->cast_expression)
398 __pass_to_client(strip_parens(expr->cast_expression),
399 SIZEOF_HOOK);
400 break;
401 case EXPR_OFFSETOF:
402 case EXPR_ALIGNOF:
403 evaluate_expression(expr);
404 break;
405 case EXPR_CONDITIONAL:
406 case EXPR_SELECT:
407 set_parent(expr->conditional, expr);
408 set_parent(expr->cond_true, expr);
409 set_parent(expr->cond_false, expr);
411 if (known_condition_true(expr->conditional)) {
412 __split_expr(expr->cond_true);
413 break;
415 if (known_condition_false(expr->conditional)) {
416 __split_expr(expr->cond_false);
417 break;
419 __pass_to_client(expr, SELECT_HOOK);
420 __split_whole_condition(expr->conditional);
421 __split_expr(expr->cond_true);
422 __push_true_states();
423 __use_false_states();
424 __split_expr(expr->cond_false);
425 __merge_true_states();
426 break;
427 case EXPR_CALL:
428 set_parent(expr->fn, expr);
430 if (sym_name_is("__builtin_constant_p", expr->fn))
431 break;
432 split_expr_list(expr->args, expr);
433 __split_expr(expr->fn);
434 if (is_inline_func(expr->fn))
435 add_inline_function(expr->fn->symbol);
436 if (inlinable(expr->fn))
437 __inline_call = 1;
438 __process_post_op_stack();
439 __pass_to_client(expr, FUNCTION_CALL_HOOK);
440 __inline_call = 0;
441 if (inlinable(expr->fn)) {
442 parse_inline(expr);
444 __pass_to_client(expr, CALL_HOOK_AFTER_INLINE);
445 if (!is_assigned_call(expr))
446 __pass_to_client(expr, FUNCTION_CALL_HOOK_AFTER);
447 if (is_noreturn_func(expr->fn))
448 nullify_path();
449 break;
450 case EXPR_INITIALIZER:
451 split_expr_list(expr->expr_list, expr);
452 break;
453 case EXPR_IDENTIFIER:
454 set_parent(expr->ident_expression, expr);
455 __split_expr(expr->ident_expression);
456 break;
457 case EXPR_INDEX:
458 set_parent(expr->idx_expression, expr);
459 __split_expr(expr->idx_expression);
460 break;
461 case EXPR_POS:
462 set_parent(expr->init_expr, expr);
463 __split_expr(expr->init_expr);
464 break;
465 case EXPR_SYMBOL:
466 __pass_to_client(expr, SYM_HOOK);
467 break;
468 case EXPR_STRING:
469 __pass_to_client(expr, STRING_HOOK);
470 break;
471 default:
472 break;
474 pop_expression(&big_expression_stack);
477 static int is_forever_loop(struct statement *stmt)
479 struct expression *expr;
481 expr = strip_expr(stmt->iterator_pre_condition);
482 if (!expr)
483 expr = stmt->iterator_post_condition;
484 if (!expr) {
485 /* this is a for(;;) loop... */
486 return 1;
489 if (expr->type == EXPR_VALUE && expr->value == 1)
490 return 1;
492 return 0;
495 static int loop_num;
496 static char *get_loop_name(int num)
498 char buf[256];
500 snprintf(buf, 255, "-loop%d", num);
501 buf[255] = '\0';
502 return alloc_sname(buf);
506 * Pre Loops are while and for loops.
508 static void handle_pre_loop(struct statement *stmt)
510 int once_through; /* we go through the loop at least once */
511 struct sm_state *extra_sm = NULL;
512 int unchanged = 0;
513 char *loop_name;
514 struct stree *stree = NULL;
515 struct sm_state *sm = NULL;
517 loop_name = get_loop_name(loop_num);
518 loop_num++;
520 __split_stmt(stmt->iterator_pre_statement);
521 __prev_stmt = stmt->iterator_pre_statement;
523 once_through = implied_condition_true(stmt->iterator_pre_condition);
525 loop_count++;
526 __push_continues();
527 __push_breaks();
529 __merge_gotos(loop_name);
531 extra_sm = __extra_handle_canonical_loops(stmt, &stree);
532 __in_pre_condition++;
533 __pass_to_client(stmt, PRELOOP_HOOK);
534 __split_whole_condition(stmt->iterator_pre_condition);
535 __in_pre_condition--;
536 FOR_EACH_SM(stree, sm) {
537 set_state(sm->owner, sm->name, sm->sym, sm->state);
538 } END_FOR_EACH_SM(sm);
539 free_stree(&stree);
540 if (extra_sm)
541 extra_sm = get_sm_state(extra_sm->owner, extra_sm->name, extra_sm->sym);
543 if (option_assume_loops)
544 once_through = 1;
546 __split_stmt(stmt->iterator_statement);
547 __warn_on_silly_pre_loops();
548 if (is_forever_loop(stmt)) {
549 __merge_continues();
550 __save_gotos(loop_name);
552 __push_fake_cur_stree();
553 __split_stmt(stmt->iterator_post_statement);
554 stree = __pop_fake_cur_stree();
556 __discard_false_states();
557 __use_breaks();
559 if (!__path_is_null())
560 __merge_stree_into_cur(stree);
561 free_stree(&stree);
562 } else {
563 __merge_continues();
564 unchanged = __iterator_unchanged(extra_sm);
565 __split_stmt(stmt->iterator_post_statement);
566 __prev_stmt = stmt->iterator_post_statement;
567 __cur_stmt = stmt;
569 __save_gotos(loop_name);
570 __in_pre_condition++;
571 __split_whole_condition(stmt->iterator_pre_condition);
572 __in_pre_condition--;
573 nullify_path();
574 __merge_false_states();
575 if (once_through)
576 __discard_false_states();
577 else
578 __merge_false_states();
580 if (extra_sm && unchanged)
581 __extra_pre_loop_hook_after(extra_sm,
582 stmt->iterator_post_statement,
583 stmt->iterator_pre_condition);
584 __merge_breaks();
586 loop_count--;
590 * Post loops are do {} while();
592 static void handle_post_loop(struct statement *stmt)
594 char *loop_name;
596 loop_name = get_loop_name(loop_num);
597 loop_num++;
598 loop_count++;
600 __push_continues();
601 __push_breaks();
602 __merge_gotos(loop_name);
603 __split_stmt(stmt->iterator_statement);
604 __merge_continues();
605 if (!is_zero(stmt->iterator_post_condition))
606 __save_gotos(loop_name);
608 if (is_forever_loop(stmt)) {
609 __use_breaks();
610 } else {
611 __split_whole_condition(stmt->iterator_post_condition);
612 __use_false_states();
613 __merge_breaks();
615 loop_count--;
618 static int empty_statement(struct statement *stmt)
620 if (!stmt)
621 return 0;
622 if (stmt->type == STMT_EXPRESSION && !stmt->expression)
623 return 1;
624 return 0;
627 static int last_stmt_on_same_line(void)
629 struct statement *stmt;
630 int i = 0;
632 FOR_EACH_PTR_REVERSE(big_statement_stack, stmt) {
633 if (!i++)
634 continue;
635 if (stmt->pos.line == get_lineno())
636 return 1;
637 return 0;
638 } END_FOR_EACH_PTR_REVERSE(stmt);
639 return 0;
642 static void split_asm_constraints(struct expression_list *expr_list)
644 struct expression *expr;
645 int state = 0;
647 FOR_EACH_PTR(expr_list, expr) {
648 switch (state) {
649 case 0: /* identifier */
650 case 1: /* constraint */
651 state++;
652 continue;
653 case 2: /* expression */
654 state = 0;
655 __split_expr(expr);
656 continue;
658 } END_FOR_EACH_PTR(expr);
661 static int is_case_val(struct statement *stmt, sval_t sval)
663 sval_t case_sval;
665 if (stmt->type != STMT_CASE)
666 return 0;
667 if (!stmt->case_expression) {
668 __set_default();
669 return 1;
671 if (!get_value(stmt->case_expression, &case_sval))
672 return 0;
673 if (case_sval.value == sval.value)
674 return 1;
675 return 0;
678 static void split_known_switch(struct statement *stmt, sval_t sval)
680 struct statement *tmp;
682 __split_expr(stmt->switch_expression);
684 push_expression(&switch_expr_stack, stmt->switch_expression);
685 __save_switch_states(top_expression(switch_expr_stack));
686 nullify_path();
687 __push_default();
688 __push_breaks();
690 stmt = stmt->switch_statement;
692 __push_scope_hooks();
693 FOR_EACH_PTR(stmt->stmts, tmp) {
694 __smatch_lineno = tmp->pos.line;
695 if (is_case_val(tmp, sval)) {
696 __merge_switches(top_expression(switch_expr_stack),
697 stmt->case_expression);
698 __pass_case_to_client(top_expression(switch_expr_stack),
699 stmt->case_expression);
701 if (__path_is_null())
702 continue;
703 __split_stmt(tmp);
704 if (__path_is_null()) {
705 __set_default();
706 goto out;
708 } END_FOR_EACH_PTR(tmp);
709 out:
710 __call_scope_hooks();
711 if (!__pop_default())
712 __merge_switches(top_expression(switch_expr_stack),
713 NULL);
714 __discard_switches();
715 __merge_breaks();
716 pop_expression(&switch_expr_stack);
719 static int taking_too_long(void)
721 int ms;
723 ms = ms_since(&fn_start_time);
724 if (ms > 1000 * 60 * 5) /* five minutes */
725 return 1;
726 return 0;
729 static int is_last_stmt(struct statement *cur_stmt)
731 struct symbol *fn = get_base_type(cur_func_sym);
732 struct statement *stmt;
734 if (!fn)
735 return 0;
736 stmt = fn->stmt;
737 if (!stmt)
738 stmt = fn->inline_stmt;
739 if (!stmt || stmt->type != STMT_COMPOUND)
740 return 0;
741 stmt = last_ptr_list((struct ptr_list *)stmt->stmts);
742 if (stmt && stmt->type == STMT_LABEL)
743 stmt = stmt->label_statement;
744 if (stmt == cur_stmt)
745 return 1;
746 return 0;
749 static void handle_backward_goto(struct statement *goto_stmt)
751 const char *goto_name, *label_name;
752 struct statement *func_stmt;
753 struct symbol *base_type = get_base_type(cur_func_sym);
754 struct statement *tmp;
755 int found = 0;
757 if (!option_info)
758 return;
759 if (last_goto_statement_handled)
760 return;
761 last_goto_statement_handled = 1;
763 if (!goto_stmt->goto_label ||
764 goto_stmt->goto_label->type != SYM_LABEL ||
765 !goto_stmt->goto_label->ident)
766 return;
767 goto_name = goto_stmt->goto_label->ident->name;
769 func_stmt = base_type->stmt;
770 if (!func_stmt)
771 func_stmt = base_type->inline_stmt;
772 if (!func_stmt)
773 return;
774 if (func_stmt->type != STMT_COMPOUND)
775 return;
777 FOR_EACH_PTR(func_stmt->stmts, tmp) {
778 if (!found) {
779 if (tmp->type != STMT_LABEL)
780 continue;
781 if (!tmp->label_identifier ||
782 tmp->label_identifier->type != SYM_LABEL ||
783 !tmp->label_identifier->ident)
784 continue;
785 label_name = tmp->label_identifier->ident->name;
786 if (strcmp(goto_name, label_name) != 0)
787 continue;
788 found = 1;
790 __split_stmt(tmp);
791 } END_FOR_EACH_PTR(tmp);
794 static void fake_a_return(void)
796 struct symbol *return_type;
798 nullify_path();
799 __unnullify_path();
801 return_type = get_real_base_type(cur_func_sym);
802 return_type = get_real_base_type(return_type);
803 if (return_type != &void_ctype) {
804 __pass_to_client(unknown_value_expression(NULL), RETURN_HOOK);
805 nullify_path();
808 __pass_to_client(cur_func_sym, END_FUNC_HOOK);
809 __pass_to_client(cur_func_sym, AFTER_FUNC_HOOK);
812 static void split_compound(struct statement *stmt)
814 struct statement *prev = NULL;
815 struct statement *cur = NULL;
816 struct statement *next;
818 __push_scope_hooks();
820 FOR_EACH_PTR(stmt->stmts, next) {
821 /* just set them all ahead of time */
822 set_parent_stmt(next, stmt);
824 if (cur) {
825 __prev_stmt = prev;
826 __next_stmt = next;
827 __cur_stmt = cur;
828 __split_stmt(cur);
830 prev = cur;
831 cur = next;
832 } END_FOR_EACH_PTR(next);
833 if (cur) {
834 __prev_stmt = prev;
835 __cur_stmt = cur;
836 __next_stmt = NULL;
837 __split_stmt(cur);
840 __call_scope_hooks();
843 void __split_stmt(struct statement *stmt)
845 sval_t sval;
847 if (!stmt)
848 goto out;
850 if (__bail_on_rest_of_function || out_of_memory() || taking_too_long()) {
851 static char *printed = NULL;
853 __bail_on_rest_of_function = 1;
854 if (printed != cur_func)
855 sm_msg("Function too hairy. Giving up.");
856 fake_a_return();
857 final_pass = 0; /* turn off sm_msg() from here */
858 printed = cur_func;
859 return;
862 add_ptr_list(&big_statement_stack, stmt);
863 free_expression_stack(&big_expression_stack);
864 set_position(stmt->pos);
865 __pass_to_client(stmt, STMT_HOOK);
867 switch (stmt->type) {
868 case STMT_DECLARATION:
869 split_declaration(stmt->declaration);
870 break;
871 case STMT_RETURN:
872 __split_expr(stmt->ret_value);
873 __pass_to_client(stmt->ret_value, RETURN_HOOK);
874 __process_post_op_stack();
875 nullify_path();
876 break;
877 case STMT_EXPRESSION:
878 __split_expr(stmt->expression);
879 break;
880 case STMT_COMPOUND:
881 split_compound(stmt);
882 break;
883 case STMT_IF:
884 set_parent_stmt(stmt->if_true, stmt);
885 set_parent_stmt(stmt->if_false, stmt);
887 if (known_condition_true(stmt->if_conditional)) {
888 __split_stmt(stmt->if_true);
889 break;
891 if (known_condition_false(stmt->if_conditional)) {
892 __split_stmt(stmt->if_false);
893 break;
895 if (option_known_conditions &&
896 implied_condition_true(stmt->if_conditional)) {
897 sm_info("this condition is true.");
898 __split_stmt(stmt->if_true);
899 break;
901 if (option_known_conditions &&
902 implied_condition_false(stmt->if_conditional)) {
903 sm_info("this condition is false.");
904 __split_stmt(stmt->if_false);
905 break;
907 __split_whole_condition(stmt->if_conditional);
908 __split_stmt(stmt->if_true);
909 if (empty_statement(stmt->if_true) &&
910 last_stmt_on_same_line() &&
911 !get_macro_name(stmt->if_true->pos))
912 sm_msg("warn: if();");
913 __push_true_states();
914 __use_false_states();
915 __split_stmt(stmt->if_false);
916 __merge_true_states();
917 break;
918 case STMT_ITERATOR:
919 set_parent_stmt(stmt->iterator_pre_statement, stmt);
920 set_parent_stmt(stmt->iterator_statement, stmt);
921 set_parent_stmt(stmt->iterator_post_statement, stmt);
923 if (stmt->iterator_pre_condition)
924 handle_pre_loop(stmt);
925 else if (stmt->iterator_post_condition)
926 handle_post_loop(stmt);
927 else {
928 // these are for(;;) type loops.
929 handle_pre_loop(stmt);
931 break;
932 case STMT_SWITCH:
933 set_parent_stmt(stmt->switch_statement, stmt);
935 if (get_value(stmt->switch_expression, &sval)) {
936 split_known_switch(stmt, sval);
937 break;
939 __split_expr(stmt->switch_expression);
940 push_expression(&switch_expr_stack, stmt->switch_expression);
941 __save_switch_states(top_expression(switch_expr_stack));
942 nullify_path();
943 __push_default();
944 __push_breaks();
945 __split_stmt(stmt->switch_statement);
946 if (!__pop_default())
947 __merge_switches(top_expression(switch_expr_stack),
948 NULL);
949 __discard_switches();
950 __merge_breaks();
951 pop_expression(&switch_expr_stack);
952 break;
953 case STMT_CASE:
954 __merge_switches(top_expression(switch_expr_stack),
955 stmt->case_expression);
956 __pass_case_to_client(top_expression(switch_expr_stack),
957 stmt->case_expression);
958 if (!stmt->case_expression)
959 __set_default();
960 __split_expr(stmt->case_expression);
961 __split_expr(stmt->case_to);
962 __split_stmt(stmt->case_statement);
963 break;
964 case STMT_LABEL:
965 if (stmt->label_identifier &&
966 stmt->label_identifier->type == SYM_LABEL &&
967 stmt->label_identifier->ident) {
968 loop_count |= 0x80000000;
969 __merge_gotos(stmt->label_identifier->ident->name);
971 __split_stmt(stmt->label_statement);
972 break;
973 case STMT_GOTO:
974 __split_expr(stmt->goto_expression);
975 if (stmt->goto_label && stmt->goto_label->type == SYM_NODE) {
976 if (!strcmp(stmt->goto_label->ident->name, "break")) {
977 __process_breaks();
978 } else if (!strcmp(stmt->goto_label->ident->name,
979 "continue")) {
980 __process_continues();
982 } else if (stmt->goto_label &&
983 stmt->goto_label->type == SYM_LABEL &&
984 stmt->goto_label->ident) {
985 __save_gotos(stmt->goto_label->ident->name);
987 nullify_path();
988 if (is_last_stmt(stmt))
989 handle_backward_goto(stmt);
990 break;
991 case STMT_NONE:
992 break;
993 case STMT_ASM:
994 __pass_to_client(stmt, ASM_HOOK);
995 __split_expr(stmt->asm_string);
996 split_asm_constraints(stmt->asm_outputs);
997 split_asm_constraints(stmt->asm_inputs);
998 split_asm_constraints(stmt->asm_clobbers);
999 break;
1000 case STMT_CONTEXT:
1001 break;
1002 case STMT_RANGE:
1003 __split_expr(stmt->range_expression);
1004 __split_expr(stmt->range_low);
1005 __split_expr(stmt->range_high);
1006 break;
1008 __pass_to_client(stmt, STMT_HOOK_AFTER);
1009 out:
1010 __process_post_op_stack();
1013 static void split_expr_list(struct expression_list *expr_list, struct expression *parent)
1015 struct expression *expr;
1017 FOR_EACH_PTR(expr_list, expr) {
1018 set_parent(expr, parent);
1019 __split_expr(expr);
1020 __process_post_op_stack();
1021 } END_FOR_EACH_PTR(expr);
1024 static void split_sym(struct symbol *sym)
1026 if (!sym)
1027 return;
1028 if (!(sym->namespace & NS_SYMBOL))
1029 return;
1031 __split_stmt(sym->stmt);
1032 __split_expr(sym->array_size);
1033 split_symlist(sym->arguments);
1034 split_symlist(sym->symbol_list);
1035 __split_stmt(sym->inline_stmt);
1036 split_symlist(sym->inline_symbol_list);
1039 static void split_symlist(struct symbol_list *sym_list)
1041 struct symbol *sym;
1043 FOR_EACH_PTR(sym_list, sym) {
1044 split_sym(sym);
1045 } END_FOR_EACH_PTR(sym);
1048 typedef void (fake_cb)(struct expression *expr);
1050 static int member_to_number(struct expression *expr, struct ident *member)
1052 struct symbol *type, *tmp;
1053 char *name;
1054 int i;
1056 if (!member)
1057 return -1;
1058 name = member->name;
1060 type = get_type(expr);
1061 if (!type || type->type != SYM_STRUCT)
1062 return -1;
1064 i = -1;
1065 FOR_EACH_PTR(type->symbol_list, tmp) {
1066 i++;
1067 if (!tmp->ident)
1068 continue;
1069 if (strcmp(name, tmp->ident->name) == 0)
1070 return i;
1071 } END_FOR_EACH_PTR(tmp);
1072 return -1;
1075 static struct ident *number_to_member(struct expression *expr, int num)
1077 struct symbol *type, *member;
1078 int i = 0;
1080 type = get_type(expr);
1081 if (!type || type->type != SYM_STRUCT)
1082 return NULL;
1084 FOR_EACH_PTR(type->symbol_list, member) {
1085 if (i == num)
1086 return member->ident;
1087 i++;
1088 } END_FOR_EACH_PTR(member);
1089 return NULL;
1092 static void fake_element_assigns_helper(struct expression *array, struct expression_list *expr_list, fake_cb *fake_cb);
1094 struct member_set {
1095 struct ident *ident;
1096 int set;
1099 static struct member_set *alloc_member_set(struct symbol *type)
1101 struct member_set *member_set;
1102 struct symbol *member;
1103 int member_count;
1104 int member_idx;
1106 member_count = ptr_list_size((struct ptr_list *)type->symbol_list);
1107 member_set = malloc(member_count * sizeof(*member_set));
1108 member_idx = 0;
1109 FOR_EACH_PTR(type->symbol_list, member) {
1110 member_set[member_idx].ident = member->ident;
1111 member_set[member_idx].set = 0;
1112 member_idx++;
1113 } END_FOR_EACH_PTR(member);
1115 return member_set;
1118 static void mark_member_as_set(struct symbol *type, struct member_set *member_set, struct ident *ident)
1120 int member_count = ptr_list_size((struct ptr_list *)type->symbol_list);
1121 int i;
1123 for (i = 0; i < member_count; i++) {
1124 if (member_set[i].ident == ident) {
1125 member_set[i].set = 1;
1126 return;
1129 // crap. this is buggy.
1130 // sm_msg("internal smatch error in initializer %s.%s", type->ident->name, ident->name);
1133 static void set_inner_struct_members(struct expression *expr, struct symbol *member)
1135 struct expression *edge_member, *assign;
1136 struct symbol *base = get_real_base_type(member);
1137 struct symbol *tmp;
1139 if (member->ident)
1140 expr = member_expression(expr, '.', member->ident);
1142 FOR_EACH_PTR(base->symbol_list, tmp) {
1143 struct symbol *type;
1145 type = get_real_base_type(tmp);
1146 if (!type)
1147 continue;
1149 if (tmp->ident) {
1150 edge_member = member_expression(expr, '.', tmp->ident);
1151 if (get_state_expr(SMATCH_EXTRA, edge_member))
1152 continue;
1155 if (type->type == SYM_UNION || type->type == SYM_STRUCT) {
1156 set_inner_struct_members(expr, tmp);
1157 continue;
1160 if (!tmp->ident)
1161 continue;
1163 assign = assign_expression(edge_member, zero_expr());
1164 __split_expr(assign);
1165 } END_FOR_EACH_PTR(tmp);
1170 static void set_unset_to_zero(struct symbol *type, struct expression *expr)
1172 struct symbol *tmp;
1173 struct expression *member, *assign;
1174 int op = '*';
1176 if (expr->type == EXPR_PREOP && expr->op == '&') {
1177 expr = strip_expr(expr->unop);
1178 op = '.';
1181 FOR_EACH_PTR(type->symbol_list, tmp) {
1182 type = get_real_base_type(tmp);
1183 if (!type)
1184 continue;
1186 if (tmp->ident) {
1187 member = member_expression(expr, op, tmp->ident);
1188 if (get_state_expr(SMATCH_EXTRA, member))
1189 continue;
1192 if (type->type == SYM_UNION || type->type == SYM_STRUCT) {
1193 set_inner_struct_members(expr, tmp);
1194 continue;
1196 if (type->type == SYM_ARRAY)
1197 continue;
1198 if (!tmp->ident)
1199 continue;
1201 assign = assign_expression(member, zero_expr());
1202 __split_expr(assign);
1203 } END_FOR_EACH_PTR(tmp);
1206 static void fake_member_assigns_helper(struct expression *symbol, struct expression_list *members, fake_cb *fake_cb)
1208 struct expression *deref, *assign, *tmp;
1209 struct symbol *struct_type, *type;
1210 struct ident *member;
1211 int member_idx;
1212 struct member_set *member_set;
1214 struct_type = get_type(symbol);
1215 if (!struct_type ||
1216 (struct_type->type != SYM_STRUCT && struct_type->type != SYM_UNION))
1217 return;
1219 member_set = alloc_member_set(struct_type);
1221 member_idx = 0;
1222 FOR_EACH_PTR(members, tmp) {
1223 member = number_to_member(symbol, member_idx);
1224 while (tmp->type == EXPR_IDENTIFIER) {
1225 member = tmp->expr_ident;
1226 member_idx = member_to_number(symbol, member);
1227 tmp = tmp->ident_expression;
1229 mark_member_as_set(struct_type, member_set, member);
1230 member_idx++;
1231 deref = member_expression(symbol, '.', member);
1232 if (tmp->type == EXPR_INITIALIZER) {
1233 type = get_type(deref);
1234 if (type && type->type == SYM_ARRAY)
1235 fake_element_assigns_helper(deref, tmp->expr_list, fake_cb);
1236 else
1237 fake_member_assigns_helper(deref, tmp->expr_list, fake_cb);
1238 } else {
1239 assign = assign_expression(deref, tmp);
1240 fake_cb(assign);
1242 } END_FOR_EACH_PTR(tmp);
1244 set_unset_to_zero(struct_type, symbol);
1247 static void fake_member_assigns(struct symbol *sym, fake_cb *fake_cb)
1249 fake_member_assigns_helper(symbol_expression(sym),
1250 sym->initializer->expr_list, fake_cb);
1253 static void fake_element_assigns_helper(struct expression *array, struct expression_list *expr_list, fake_cb *fake_cb)
1255 struct expression *offset, *binop, *assign, *tmp;
1256 struct symbol *type;
1257 int idx;
1259 if (ptr_list_size((struct ptr_list *)expr_list) > 1000)
1260 return;
1262 idx = 0;
1263 FOR_EACH_PTR(expr_list, tmp) {
1264 if (tmp->type == EXPR_INDEX) {
1265 if (tmp->idx_from != tmp->idx_to)
1266 return;
1267 idx = tmp->idx_from;
1268 if (!tmp->idx_expression)
1269 goto next;
1270 tmp = tmp->idx_expression;
1272 offset = value_expr(idx);
1273 binop = array_element_expression(array, offset);
1274 if (tmp->type == EXPR_INITIALIZER) {
1275 type = get_type(binop);
1276 if (type && type->type == SYM_ARRAY)
1277 fake_element_assigns_helper(binop, tmp->expr_list, fake_cb);
1278 else
1279 fake_member_assigns_helper(binop, tmp->expr_list, fake_cb);
1280 } else {
1281 assign = assign_expression(binop, tmp);
1282 fake_cb(assign);
1284 next:
1285 idx++;
1286 } END_FOR_EACH_PTR(tmp);
1289 static void fake_element_assigns(struct symbol *sym, fake_cb *fake_cb)
1291 fake_element_assigns_helper(symbol_expression(sym), sym->initializer->expr_list, fake_cb);
1294 static void fake_assign_expr(struct symbol *sym)
1296 struct expression *assign, *symbol;
1298 symbol = symbol_expression(sym);
1299 assign = assign_expression(symbol, sym->initializer);
1300 __split_expr(assign);
1303 static void call_split_expr(struct expression *expr)
1305 __split_expr(expr);
1308 static void do_initializer_stuff(struct symbol *sym)
1310 if (!sym->initializer)
1311 return;
1313 if (sym->initializer->type == EXPR_INITIALIZER) {
1314 if (get_real_base_type(sym)->type == SYM_ARRAY)
1315 fake_element_assigns(sym, call_split_expr);
1316 else
1317 fake_member_assigns(sym, call_split_expr);
1318 } else {
1319 fake_assign_expr(sym);
1323 static void split_declaration(struct symbol_list *sym_list)
1325 struct symbol *sym;
1327 FOR_EACH_PTR(sym_list, sym) {
1328 __pass_to_client(sym, DECLARATION_HOOK);
1329 do_initializer_stuff(sym);
1330 split_sym(sym);
1331 } END_FOR_EACH_PTR(sym);
1334 static void call_global_assign_hooks(struct expression *assign)
1336 __pass_to_client(assign, GLOBAL_ASSIGNMENT_HOOK);
1339 static void fake_global_assign(struct symbol *sym)
1341 struct expression *assign, *symbol;
1343 if (get_real_base_type(sym)->type == SYM_ARRAY) {
1344 if (sym->initializer && sym->initializer->type == EXPR_INITIALIZER) {
1345 fake_element_assigns(sym, call_global_assign_hooks);
1346 } else if (sym->initializer) {
1347 symbol = symbol_expression(sym);
1348 assign = assign_expression(symbol, sym->initializer);
1349 __pass_to_client(assign, GLOBAL_ASSIGNMENT_HOOK);
1350 } else {
1351 fake_element_assigns_helper(symbol_expression(sym), NULL, call_global_assign_hooks);
1353 } else if (get_real_base_type(sym)->type == SYM_STRUCT) {
1354 if (sym->initializer && sym->initializer->type == EXPR_INITIALIZER) {
1355 fake_member_assigns(sym, call_global_assign_hooks);
1356 } else if (sym->initializer) {
1357 symbol = symbol_expression(sym);
1358 assign = assign_expression(symbol, sym->initializer);
1359 __pass_to_client(assign, GLOBAL_ASSIGNMENT_HOOK);
1360 } else {
1361 fake_member_assigns_helper(symbol_expression(sym), NULL, call_global_assign_hooks);
1363 } else {
1364 symbol = symbol_expression(sym);
1365 if (sym->initializer)
1366 assign = assign_expression(symbol, sym->initializer);
1367 else
1368 assign = assign_expression(symbol, zero_expr());
1369 __pass_to_client(assign, GLOBAL_ASSIGNMENT_HOOK);
1373 static void start_function_definition(struct symbol *sym)
1375 __in_function_def = 1;
1376 __pass_to_client(sym, FUNC_DEF_HOOK);
1377 __in_function_def = 0;
1378 __pass_to_client(sym, AFTER_DEF_HOOK);
1382 static void split_function(struct symbol *sym)
1384 struct symbol *base_type = get_base_type(sym);
1386 if (!base_type->stmt && !base_type->inline_stmt)
1387 return;
1389 gettimeofday(&fn_start_time, NULL);
1390 cur_func_sym = sym;
1391 if (sym->ident)
1392 cur_func = sym->ident->name;
1393 __smatch_lineno = sym->pos.line;
1394 loop_count = 0;
1395 last_goto_statement_handled = 0;
1396 sm_debug("new function: %s\n", cur_func);
1397 __stree_id = 0;
1398 if (option_two_passes) {
1399 __unnullify_path();
1400 loop_num = 0;
1401 final_pass = 0;
1402 start_function_definition(sym);
1403 __split_stmt(base_type->stmt);
1404 __split_stmt(base_type->inline_stmt);
1405 nullify_path();
1407 __unnullify_path();
1408 loop_num = 0;
1409 final_pass = 1;
1410 start_function_definition(sym);
1411 __split_stmt(base_type->stmt);
1412 __split_stmt(base_type->inline_stmt);
1413 __pass_to_client(sym, END_FUNC_HOOK);
1414 __pass_to_client(sym, AFTER_FUNC_HOOK);
1416 clear_all_states();
1417 cur_func_sym = NULL;
1418 cur_func = NULL;
1419 free_data_info_allocs();
1420 free_expression_stack(&switch_expr_stack);
1421 __free_ptr_list((struct ptr_list **)&big_statement_stack);
1422 __bail_on_rest_of_function = 0;
1425 static void parse_inline(struct expression *call)
1427 struct symbol *base_type;
1428 int loop_num_bak = loop_num;
1429 int final_pass_bak = final_pass;
1430 char *cur_func_bak = cur_func;
1431 struct statement_list *big_statement_stack_bak = big_statement_stack;
1432 struct expression_list *big_expression_stack_bak = big_expression_stack;
1433 struct expression_list *switch_expr_stack_bak = switch_expr_stack;
1434 struct symbol *cur_func_sym_bak = cur_func_sym;
1436 __pass_to_client(call, INLINE_FN_START);
1437 final_pass = 0; /* don't print anything */
1438 __inline_fn = call;
1440 base_type = get_base_type(call->fn->symbol);
1441 cur_func_sym = call->fn->symbol;
1442 if (call->fn->symbol->ident)
1443 cur_func = call->fn->symbol->ident->name;
1444 else
1445 cur_func = NULL;
1446 set_position(call->fn->symbol->pos);
1448 save_all_states();
1449 big_statement_stack = NULL;
1450 big_expression_stack = NULL;
1451 switch_expr_stack = NULL;
1453 sm_debug("inline function: %s\n", cur_func);
1454 __unnullify_path();
1455 loop_num = 0;
1456 start_function_definition(call->fn->symbol);
1457 __split_stmt(base_type->stmt);
1458 __split_stmt(base_type->inline_stmt);
1459 __pass_to_client(call->fn->symbol, END_FUNC_HOOK);
1460 __pass_to_client(call->fn->symbol, AFTER_FUNC_HOOK);
1462 free_expression_stack(&switch_expr_stack);
1463 __free_ptr_list((struct ptr_list **)&big_statement_stack);
1464 nullify_path();
1465 free_goto_stack();
1467 loop_num = loop_num_bak;
1468 final_pass = final_pass_bak;
1469 cur_func_sym = cur_func_sym_bak;
1470 cur_func = cur_func_bak;
1471 big_statement_stack = big_statement_stack_bak;
1472 big_expression_stack = big_expression_stack_bak;
1473 switch_expr_stack = switch_expr_stack_bak;
1475 restore_all_states();
1476 set_position(call->pos);
1477 __inline_fn = NULL;
1478 __pass_to_client(call, INLINE_FN_END);
1481 static struct symbol_list *inlines_called;
1482 static void add_inline_function(struct symbol *sym)
1484 static struct symbol_list *already_added;
1485 struct symbol *tmp;
1487 FOR_EACH_PTR(already_added, tmp) {
1488 if (tmp == sym)
1489 return;
1490 } END_FOR_EACH_PTR(tmp);
1492 add_ptr_list(&already_added, sym);
1493 add_ptr_list(&inlines_called, sym);
1496 static void process_inlines(void)
1498 struct symbol *tmp;
1500 FOR_EACH_PTR(inlines_called, tmp) {
1501 split_function(tmp);
1502 } END_FOR_EACH_PTR(tmp);
1503 free_ptr_list(&inlines_called);
1506 static struct symbol *get_last_scoped_symbol(struct symbol_list *big_list, int use_static)
1508 struct symbol *sym;
1510 FOR_EACH_PTR_REVERSE(big_list, sym) {
1511 if (!sym->scope)
1512 continue;
1513 if (use_static && sym->ctype.modifiers & MOD_STATIC)
1514 return sym;
1515 if (!use_static && !(sym->ctype.modifiers & MOD_STATIC))
1516 return sym;
1517 } END_FOR_EACH_PTR_REVERSE(sym);
1519 return NULL;
1522 static void split_inlines_in_scope(struct symbol *sym)
1524 struct symbol *base;
1525 struct symbol_list *scope_list;
1526 int stream;
1528 scope_list = sym->scope->symbols;
1529 stream = sym->pos.stream;
1531 /* find the last static symbol in the file */
1532 FOR_EACH_PTR_REVERSE(scope_list, sym) {
1533 if (sym->pos.stream != stream)
1534 continue;
1535 if (sym->type != SYM_NODE)
1536 continue;
1537 base = get_base_type(sym);
1538 if (!base)
1539 continue;
1540 if (base->type != SYM_FN)
1541 continue;
1542 if (!base->inline_stmt)
1543 continue;
1544 add_inline_function(sym);
1545 } END_FOR_EACH_PTR_REVERSE(sym);
1547 process_inlines();
1550 static void split_inlines(struct symbol_list *sym_list)
1552 struct symbol *sym;
1554 sym = get_last_scoped_symbol(sym_list, 0);
1555 if (sym)
1556 split_inlines_in_scope(sym);
1557 sym = get_last_scoped_symbol(sym_list, 1);
1558 if (sym)
1559 split_inlines_in_scope(sym);
1562 static struct stree *clone_estates_perm(struct stree *orig)
1564 struct stree *ret = NULL;
1565 struct sm_state *tmp;
1567 FOR_EACH_SM(orig, tmp) {
1568 set_state_stree_perm(&ret, tmp->owner, tmp->name, tmp->sym, clone_estate_perm(tmp->state));
1569 } END_FOR_EACH_SM(tmp);
1571 return ret;
1574 static void split_functions(struct symbol_list *sym_list)
1576 struct symbol *sym;
1578 __unnullify_path();
1579 FOR_EACH_PTR(sym_list, sym) {
1580 set_position(sym->pos);
1581 if (sym->type != SYM_NODE || get_base_type(sym)->type != SYM_FN) {
1582 __pass_to_client(sym, BASE_HOOK);
1583 fake_global_assign(sym);
1585 } END_FOR_EACH_PTR(sym);
1586 global_states = clone_estates_perm(get_all_states_stree(SMATCH_EXTRA));
1587 nullify_path();
1589 FOR_EACH_PTR(sym_list, sym) {
1590 set_position(sym->pos);
1591 if (sym->type == SYM_NODE && get_base_type(sym)->type == SYM_FN) {
1592 split_function(sym);
1593 process_inlines();
1595 } END_FOR_EACH_PTR(sym);
1596 split_inlines(sym_list);
1597 __pass_to_client(sym_list, END_FILE_HOOK);
1600 void smatch(int argc, char **argv)
1603 struct string_list *filelist = NULL;
1604 struct symbol_list *sym_list;
1606 if (argc < 2) {
1607 printf("Usage: smatch [--debug] <filename.c>\n");
1608 exit(1);
1610 sparse_initialize(argc, argv, &filelist);
1611 set_valid_ptr_max();
1612 FOR_EACH_PTR_NOTAG(filelist, base_file) {
1613 if (option_file_output) {
1614 char buf[256];
1616 snprintf(buf, sizeof(buf), "%s.smatch", base_file);
1617 sm_outfd = fopen(buf, "w");
1618 if (!sm_outfd) {
1619 printf("Error: Cannot open %s\n", base_file);
1620 exit(1);
1623 sym_list = sparse_keep_tokens(base_file);
1624 split_functions(sym_list);
1625 } END_FOR_EACH_PTR_NOTAG(base_file);