4 * Copyright (C) 2006,2008 Dan Carpenter.
6 * Licensed under the Open Software License version 1.1
14 int __smatch_lineno
= 0;
16 static char *filename
;
17 static char *cur_func
;
18 static int line_func_start
;
20 char *get_filename() { return filename
; }
21 char *get_function() { return cur_func
; }
22 int get_lineno() { return __smatch_lineno
; }
23 int get_func_pos() { return __smatch_lineno
- line_func_start
; }
25 static void split_symlist(struct symbol_list
*sym_list
);
26 static void split_expr_list(struct expression_list
*expr_list
);
28 unsigned int __get_allocations();
30 int option_assume_loops
= 0;
32 void __split_expr(struct expression
*expr
)
37 // printf("%d Debug expr_type %d\n", get_lineno(), expr->type);
39 __smatch_lineno
= expr
->pos
.line
;
40 __pass_to_client(expr
, EXPR_HOOK
);
45 __pass_to_client(expr
, OP_HOOK
);
46 __split_expr(expr
->unop
);
49 __split_statements(expr
->statement
);
52 __split_whole_condition(expr
);
55 __merge_true_states();
56 __pop_false_only_stack();
64 if (expr
->type
== EXPR_ASSIGNMENT
)
65 __pass_to_client(expr
, ASSIGNMENT_HOOK
);
66 __split_expr(expr
->left
);
67 __split_expr(expr
->right
);
68 if (expr
->type
== EXPR_ASSIGNMENT
)
69 __pass_to_client(expr
, ASSIGNMENT_AFTER_HOOK
);
72 __pass_to_client(expr
, DEREF_HOOK
);
73 __split_expr(expr
->deref
);
76 __split_expr(expr
->base
);
79 __split_expr(expr
->cast_expression
);
82 /* there isn't anything to pass a client from inside a sizeof() */
84 case EXPR_CONDITIONAL
:
86 __split_whole_condition(expr
->conditional
);
87 __split_expr(expr
->cond_true
);
90 __split_expr(expr
->cond_false
);
91 __merge_true_states();
92 __pop_false_only_stack();
95 __pass_to_client(expr
, FUNCTION_CALL_HOOK
);
96 __split_expr(expr
->fn
);
97 split_expr_list(expr
->args
);
98 __pass_to_client(expr
, FUNCTION_CALL_AFTER_HOOK
);
100 if (expr
->fn
->type
== EXPR_SYMBOL
&&
101 !strcmp(expr
->fn
->symbol_name
->name
, "panic"))
105 case EXPR_INITIALIZER
:
106 split_expr_list(expr
->expr_list
);
108 case EXPR_IDENTIFIER
:
109 __split_expr(expr
->ident_expression
);
112 __split_expr(expr
->idx_expression
);
115 __split_expr(expr
->init_expr
);
122 static int is_forever_loop(struct statement
*stmt
)
125 struct expression
*expr
;
127 expr
= strip_expr(stmt
->iterator_pre_condition
);
129 expr
= stmt
->iterator_post_condition
;
131 /* this is a for(;;) loop... */
135 if (expr
->type
== EXPR_VALUE
&& expr
->value
== 1) {
143 * Pre Loops are while and for loops.
146 static void handle_pre_loop(struct statement
*stmt
)
148 int once_through
; /* we go through the loop at least once */
150 __split_statements(stmt
->iterator_pre_statement
);
152 once_through
= known_condition_true(stmt
->iterator_pre_condition
);
153 if (option_assume_loops
)
159 __split_whole_condition(stmt
->iterator_pre_condition
);
161 __split_statements(stmt
->iterator_statement
);
162 if (is_forever_loop(stmt
)) {
163 __pop_false_only_stack();
164 /* forever loops don't have an iterator_post_statement */
166 __pop_false_states();
168 } else if (once_through
) {
170 __split_statements(stmt
->iterator_post_statement
);
171 __pop_false_states();
172 __use_false_only_stack();
176 __split_statements(stmt
->iterator_post_statement
);
177 __merge_false_states();
178 __use_false_only_stack();
184 * Post loops are do {} while();
186 static void handle_post_loop(struct statement
*stmt
)
190 __split_statements(stmt
->iterator_statement
);
191 if (is_forever_loop(stmt
)) {
197 __split_whole_condition(stmt
->iterator_post_condition
);
198 __use_false_states();
201 __pop_false_only_stack();
204 static void print_unreached(struct statement
*stmt
)
208 * GCC insists on a return statement even where it is never
209 * reached. Also BUG() sometimes is a forever loop and
210 * sometimes not so people put code after a BUG(). There
211 * are way to many false positives.
216 if (__path_is_null()) {
218 case STMT_COMPOUND
: /* after a switch before a case stmt */
221 case STMT_DECLARATION
: /* switch(x) { int a; case foo: ... */
224 smatch_msg("unreachable code. %d", stmt
->type
);
229 void __split_statements(struct statement
*stmt
)
234 if (__get_allocations() > MAXSMSTATES
) {
235 static char *printed
= NULL
;
237 if (printed
!= cur_func
)
238 smatch_msg("Function too big. Giving up.");
243 __smatch_lineno
= stmt
->pos
.line
;
244 print_unreached(stmt
);
245 __pass_to_client(stmt
, STMT_HOOK
);
247 switch (stmt
->type
) {
248 case STMT_DECLARATION
:
249 __pass_declarations_to_client(stmt
->declaration
);
250 split_symlist(stmt
->declaration
);
253 __pass_to_client(stmt
, RETURN_HOOK
);
254 __split_expr(stmt
->ret_value
);
257 case STMT_EXPRESSION
:
258 __split_expr(stmt
->expression
);
260 case STMT_COMPOUND
: {
262 FOR_EACH_PTR(stmt
->stmts
, s
) {
263 __split_statements(s
);
264 } END_FOR_EACH_PTR(s
);
268 __split_whole_condition(stmt
->if_conditional
);
269 __split_statements(stmt
->if_true
);
270 __push_true_states();
271 __use_false_states();
272 __split_statements(stmt
->if_false
);
273 __merge_true_states();
274 __pop_false_only_stack();
277 if (stmt
->iterator_pre_condition
)
278 handle_pre_loop(stmt
);
279 else if (stmt
->iterator_post_condition
)
280 handle_post_loop(stmt
);
282 // these are for(;;) type loops.
283 handle_pre_loop(stmt
);
287 __split_expr(stmt
->switch_expression
);
288 __save_switch_states();
291 __split_statements(stmt
->switch_statement
);
292 if (!__pop_default())
299 if (!stmt
->case_expression
)
301 __split_expr(stmt
->case_expression
);
302 __split_expr(stmt
->case_to
);
303 __split_statements(stmt
->case_statement
);
307 stmt
->label
->type
== SYM_LABEL
&&
308 stmt
->label
->ident
) {
309 __merge_gotos(stmt
->label
->ident
->name
);
311 __split_statements(stmt
->label_statement
);
314 __split_expr(stmt
->goto_expression
);
315 if (stmt
->goto_label
&& stmt
->goto_label
->type
== SYM_NODE
) {
316 if (!strcmp(stmt
->goto_label
->ident
->name
, "break")) {
318 } else if (!strcmp(stmt
->goto_label
->ident
->name
,
320 __process_continues();
322 } else if (stmt
->goto_label
&&
323 stmt
->goto_label
->type
== SYM_LABEL
&&
324 stmt
->goto_label
->ident
) {
325 __save_gotos(stmt
->goto_label
->ident
->name
);
332 __split_expr(stmt
->asm_string
);
333 //__split_expr(stmt->asm_outputs);
334 //__split_expr(stmt->asm_inputs);
335 //__split_expr(stmt->asm_clobbers);
340 __split_expr(stmt
->range_expression
);
341 __split_expr(stmt
->range_low
);
342 __split_expr(stmt
->range_high
);
347 static void split_expr_list(struct expression_list
*expr_list
)
349 struct expression
*expr
;
350 FOR_EACH_PTR(expr_list
, expr
) {
352 } END_FOR_EACH_PTR(expr
);
356 static void split_sym(struct symbol
*sym
)
360 if (!(sym
->namespace & NS_SYMBOL
))
363 __pass_to_client(sym
, SYM_HOOK
);
364 __split_statements(sym
->stmt
);
365 __split_expr(sym
->array_size
);
366 split_symlist(sym
->arguments
);
367 split_symlist(sym
->symbol_list
);
368 __split_statements(sym
->inline_stmt
);
369 split_symlist(sym
->inline_symbol_list
);
370 __split_expr(sym
->initializer
);
373 static void split_symlist(struct symbol_list
*sym_list
)
377 FOR_EACH_PTR(sym_list
, sym
) {
379 } END_FOR_EACH_PTR(sym
);
382 static void split_functions(struct symbol_list
*sym_list
)
386 FOR_EACH_PTR(sym_list
, sym
) {
387 struct symbol
*base_type
;
388 base_type
= get_base_type(sym
);
389 if (sym
->type
== SYM_NODE
&& base_type
->type
== SYM_FN
) {
391 line_func_start
= base_type
->stmt
->pos
.line
;
393 cur_func
= sym
->ident
->name
;
394 __smatch_lineno
= sym
->pos
.line
;
395 SM_DEBUG("new function: %s\n", cur_func
);
397 __pass_to_client(sym
, FUNC_DEF_HOOK
);
398 __split_statements(base_type
->stmt
);
399 __pass_to_client(sym
, END_FUNC_HOOK
);
405 __pass_to_client(sym
, BASE_HOOK
);
407 } END_FOR_EACH_PTR(sym
);
408 __pass_to_client_no_data(END_FILE_HOOK
);
411 void smatch (int argc
, char **argv
)
414 struct string_list
*filelist
= NULL
;
415 struct symbol_list
*sym_list
;
418 printf("Usage: smatch [--debug] <filename.c>\n");
421 sparse_initialize(argc
, argv
, &filelist
);
422 FOR_EACH_PTR_NOTAG(filelist
, filename
) {
423 sym_list
= __sparse(filename
);
424 split_functions(sym_list
);
425 } END_FOR_EACH_PTR_NOTAG(filename
);