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 __warn_on_silly_pre_loops();
163 if (is_forever_loop(stmt
)) {
164 __pop_false_only_stack();
165 /* forever loops don't have an iterator_post_statement */
167 __pop_false_states();
169 } else if (once_through
) {
171 __split_statements(stmt
->iterator_post_statement
);
172 __pop_false_states();
173 __use_false_only_stack();
177 __split_statements(stmt
->iterator_post_statement
);
178 __merge_false_states();
179 __use_false_only_stack();
185 * Post loops are do {} while();
187 static void handle_post_loop(struct statement
*stmt
)
191 __split_statements(stmt
->iterator_statement
);
192 if (is_forever_loop(stmt
)) {
198 __split_whole_condition(stmt
->iterator_post_condition
);
199 __use_false_states();
202 __pop_false_only_stack();
205 static void print_unreached(struct statement
*stmt
)
209 * GCC insists on a return statement even where it is never
210 * reached. Also BUG() sometimes is a forever loop and
211 * sometimes not so people put code after a BUG(). There
212 * are way to many false positives.
217 if (__path_is_null()) {
219 case STMT_COMPOUND
: /* after a switch before a case stmt */
222 case STMT_DECLARATION
: /* switch(x) { int a; case foo: ... */
225 smatch_msg("unreachable code. %d", stmt
->type
);
230 void __split_statements(struct statement
*stmt
)
235 if (__get_allocations() > MAXSMSTATES
) {
236 static char *printed
= NULL
;
238 if (printed
!= cur_func
)
239 smatch_msg("Function too big. Giving up.");
244 __smatch_lineno
= stmt
->pos
.line
;
245 print_unreached(stmt
);
246 __pass_to_client(stmt
, STMT_HOOK
);
248 switch (stmt
->type
) {
249 case STMT_DECLARATION
:
250 __pass_declarations_to_client(stmt
->declaration
);
251 split_symlist(stmt
->declaration
);
254 __split_expr(stmt
->ret_value
);
255 __pass_to_client(stmt
, RETURN_HOOK
);
258 case STMT_EXPRESSION
:
259 __split_expr(stmt
->expression
);
261 case STMT_COMPOUND
: {
263 FOR_EACH_PTR(stmt
->stmts
, s
) {
264 __split_statements(s
);
265 } END_FOR_EACH_PTR(s
);
269 if (known_condition_true(stmt
->if_conditional
)) {
270 smatch_msg("info: this condition is true.");
271 __split_statements(stmt
->if_true
);
274 if (known_condition_false(stmt
->if_conditional
)) {
275 smatch_msg("info: this condition is false.");
276 __split_statements(stmt
->if_false
);
279 __split_whole_condition(stmt
->if_conditional
);
280 __split_statements(stmt
->if_true
);
281 __push_true_states();
282 __use_false_states();
283 __split_statements(stmt
->if_false
);
284 __merge_true_states();
285 __pop_false_only_stack();
288 if (stmt
->iterator_pre_condition
)
289 handle_pre_loop(stmt
);
290 else if (stmt
->iterator_post_condition
)
291 handle_post_loop(stmt
);
293 // these are for(;;) type loops.
294 handle_pre_loop(stmt
);
298 __split_expr(stmt
->switch_expression
);
299 __save_switch_states();
302 __split_statements(stmt
->switch_statement
);
303 if (!__pop_default())
310 if (!stmt
->case_expression
)
312 __split_expr(stmt
->case_expression
);
313 __split_expr(stmt
->case_to
);
314 __split_statements(stmt
->case_statement
);
318 stmt
->label
->type
== SYM_LABEL
&&
319 stmt
->label
->ident
) {
320 __merge_gotos(stmt
->label
->ident
->name
);
322 __split_statements(stmt
->label_statement
);
325 __split_expr(stmt
->goto_expression
);
326 if (stmt
->goto_label
&& stmt
->goto_label
->type
== SYM_NODE
) {
327 if (!strcmp(stmt
->goto_label
->ident
->name
, "break")) {
329 } else if (!strcmp(stmt
->goto_label
->ident
->name
,
331 __process_continues();
333 } else if (stmt
->goto_label
&&
334 stmt
->goto_label
->type
== SYM_LABEL
&&
335 stmt
->goto_label
->ident
) {
336 __save_gotos(stmt
->goto_label
->ident
->name
);
343 __split_expr(stmt
->asm_string
);
344 //__split_expr(stmt->asm_outputs);
345 //__split_expr(stmt->asm_inputs);
346 //__split_expr(stmt->asm_clobbers);
351 __split_expr(stmt
->range_expression
);
352 __split_expr(stmt
->range_low
);
353 __split_expr(stmt
->range_high
);
358 static void split_expr_list(struct expression_list
*expr_list
)
360 struct expression
*expr
;
361 FOR_EACH_PTR(expr_list
, expr
) {
363 } END_FOR_EACH_PTR(expr
);
367 static void split_sym(struct symbol
*sym
)
371 if (!(sym
->namespace & NS_SYMBOL
))
374 __pass_to_client(sym
, SYM_HOOK
);
375 __split_statements(sym
->stmt
);
376 __split_expr(sym
->array_size
);
377 split_symlist(sym
->arguments
);
378 split_symlist(sym
->symbol_list
);
379 __split_statements(sym
->inline_stmt
);
380 split_symlist(sym
->inline_symbol_list
);
381 __split_expr(sym
->initializer
);
384 static void split_symlist(struct symbol_list
*sym_list
)
388 FOR_EACH_PTR(sym_list
, sym
) {
390 } END_FOR_EACH_PTR(sym
);
393 static void split_functions(struct symbol_list
*sym_list
)
397 FOR_EACH_PTR(sym_list
, sym
) {
398 struct symbol
*base_type
;
399 base_type
= get_base_type(sym
);
400 if (sym
->type
== SYM_NODE
&& base_type
->type
== SYM_FN
) {
402 line_func_start
= base_type
->stmt
->pos
.line
;
404 cur_func
= sym
->ident
->name
;
405 __smatch_lineno
= sym
->pos
.line
;
406 SM_DEBUG("new function: %s\n", cur_func
);
408 __pass_to_client(sym
, FUNC_DEF_HOOK
);
409 __split_statements(base_type
->stmt
);
410 __pass_to_client(sym
, END_FUNC_HOOK
);
416 __pass_to_client(sym
, BASE_HOOK
);
418 } END_FOR_EACH_PTR(sym
);
419 __pass_to_client_no_data(END_FILE_HOOK
);
422 void smatch (int argc
, char **argv
)
425 struct string_list
*filelist
= NULL
;
426 struct symbol_list
*sym_list
;
429 printf("Usage: smatch [--debug] <filename.c>\n");
432 sparse_initialize(argc
, argv
, &filelist
);
433 FOR_EACH_PTR_NOTAG(filelist
, filename
) {
434 sym_list
= __sparse(filename
);
435 split_functions(sym_list
);
436 } END_FOR_EACH_PTR_NOTAG(filename
);