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_statements(struct statement
*stmt
);
26 static void split_symlist(struct symbol_list
*sym_list
);
27 static void split_expr_list(struct expression_list
*expr_list
);
29 void __split_expr(struct expression
*expr
)
34 // printf("%d Debug expr_type %d\n", get_lineno(), expr->type);
36 __smatch_lineno
= expr
->pos
.line
;
37 __pass_to_client(expr
, EXPR_HOOK
);
42 __pass_to_client(expr
, OP_HOOK
);
43 __split_expr(expr
->unop
);
46 split_statements(expr
->statement
);
49 __split_whole_condition(expr
);
52 __merge_true_states();
53 __pop_false_only_stack();
61 if (expr
->type
== EXPR_ASSIGNMENT
)
62 __pass_to_client(expr
, ASSIGNMENT_HOOK
);
63 __split_expr(expr
->left
);
64 __split_expr(expr
->right
);
65 if (expr
->type
== EXPR_ASSIGNMENT
)
66 __pass_to_client(expr
, ASSIGNMENT_AFTER_HOOK
);
69 __pass_to_client(expr
, DEREF_HOOK
);
70 __split_expr(expr
->deref
);
73 __split_expr(expr
->base
);
76 __split_expr(expr
->cast_expression
);
79 /* there isn't anything to pass a client from inside a sizeof() */
81 case EXPR_CONDITIONAL
:
83 __split_whole_condition(expr
->conditional
);
84 __split_expr(expr
->cond_true
);
87 __split_expr(expr
->cond_false
);
88 __merge_true_states();
89 __pop_false_only_stack();
92 __pass_to_client(expr
, FUNCTION_CALL_HOOK
);
93 __split_expr(expr
->fn
);
94 split_expr_list(expr
->args
);
95 __pass_to_client(expr
, FUNCTION_CALL_AFTER_HOOK
);
97 if (expr
->fn
->type
== EXPR_SYMBOL
&&
98 !strcmp(expr
->fn
->symbol_name
->name
, "panic"))
102 case EXPR_INITIALIZER
:
103 split_expr_list(expr
->expr_list
);
105 case EXPR_IDENTIFIER
:
106 __split_expr(expr
->ident_expression
);
109 __split_expr(expr
->idx_expression
);
112 __split_expr(expr
->init_expr
);
119 static int is_forever_loop(struct statement
*stmt
)
122 struct expression
*expr
;
124 expr
= strip_expr(stmt
->iterator_pre_condition
);
126 expr
= stmt
->iterator_post_condition
;
128 /* this is a for(;;) loop... */
132 if (expr
->type
== EXPR_VALUE
&& expr
->value
== 1) {
140 * Pre Loops are while and for loops.
143 static void handle_pre_loop(struct statement
*stmt
)
145 int once_through
; /* we go through the loop at least once */
147 split_statements(stmt
->iterator_pre_statement
);
149 once_through
= known_condition_true(stmt
->iterator_pre_condition
);
154 __split_whole_condition(stmt
->iterator_pre_condition
);
156 split_statements(stmt
->iterator_statement
);
157 split_statements(stmt
->iterator_post_statement
);
158 if (is_forever_loop(stmt
)) {
159 __pop_false_only_stack();
161 __pop_false_states();
163 } else if (once_through
) {
165 __pop_false_states();
166 __use_false_only_stack();
170 __merge_false_states();
171 __use_false_only_stack();
177 * Post loops are do {} while();
179 static void handle_post_loop(struct statement
*stmt
)
183 split_statements(stmt
->iterator_statement
);
185 __split_whole_condition(stmt
->iterator_post_condition
);
186 __use_false_states();
188 if (is_forever_loop(stmt
)) {
196 __pop_false_only_stack();
199 static void print_unreached(struct statement
*stmt
)
203 * GCC insists on a return statement even where it is never
204 * reached. Also BUG() sometimes is a forever loop and
205 * sometimes not so people put code after a BUG(). There
206 * are way to many false positives.
211 if (__path_is_null()) {
213 case STMT_COMPOUND
: /* after a switch before a case stmt */
216 case STMT_DECLARATION
: /* switch(x) { int a; case foo: ... */
219 smatch_msg("unreachable code. %d", stmt
->type
);
224 static void split_statements(struct statement
*stmt
)
229 __smatch_lineno
= stmt
->pos
.line
;
230 print_unreached(stmt
);
231 __pass_to_client(stmt
, STMT_HOOK
);
233 switch (stmt
->type
) {
234 case STMT_DECLARATION
:
235 __pass_declarations_to_client(stmt
->declaration
);
236 split_symlist(stmt
->declaration
);
239 __pass_to_client(stmt
, RETURN_HOOK
);
240 __split_expr(stmt
->ret_value
);
243 case STMT_EXPRESSION
:
244 __split_expr(stmt
->expression
);
246 case STMT_COMPOUND
: {
248 FOR_EACH_PTR(stmt
->stmts
, s
) {
250 } END_FOR_EACH_PTR(s
);
254 __split_whole_condition(stmt
->if_conditional
);
255 split_statements(stmt
->if_true
);
256 __push_true_states();
257 __use_false_states();
258 split_statements(stmt
->if_false
);
259 __merge_true_states();
260 __pop_false_only_stack();
263 if (stmt
->iterator_pre_condition
)
264 handle_pre_loop(stmt
);
265 else if (stmt
->iterator_post_condition
)
266 handle_post_loop(stmt
);
268 // these are for(;;) type loops.
269 handle_pre_loop(stmt
);
273 __split_expr(stmt
->switch_expression
);
274 __save_switch_states();
278 split_statements(stmt
->switch_statement
);
279 if (!__pop_default())
286 if (!stmt
->case_expression
)
288 __split_expr(stmt
->case_expression
);
289 __split_expr(stmt
->case_to
);
290 split_statements(stmt
->case_statement
);
294 stmt
->label
->type
== SYM_LABEL
&&
295 stmt
->label
->ident
) {
296 __merge_gotos(stmt
->label
->ident
->name
);
298 split_statements(stmt
->label_statement
);
301 __split_expr(stmt
->goto_expression
);
302 if (stmt
->goto_label
&& stmt
->goto_label
->type
== SYM_NODE
) {
303 if (!strcmp(stmt
->goto_label
->ident
->name
, "break")) {
305 } else if (!strcmp(stmt
->goto_label
->ident
->name
,
307 __process_continues();
309 } else if (stmt
->goto_label
&&
310 stmt
->goto_label
->type
== SYM_LABEL
&&
311 stmt
->goto_label
->ident
) {
312 __save_gotos(stmt
->goto_label
->ident
->name
);
319 __split_expr(stmt
->asm_string
);
320 //__split_expr(stmt->asm_outputs);
321 //__split_expr(stmt->asm_inputs);
322 //__split_expr(stmt->asm_clobbers);
327 __split_expr(stmt
->range_expression
);
328 __split_expr(stmt
->range_low
);
329 __split_expr(stmt
->range_high
);
334 static void split_expr_list(struct expression_list
*expr_list
)
336 struct expression
*expr
;
337 FOR_EACH_PTR(expr_list
, expr
) {
339 } END_FOR_EACH_PTR(expr
);
343 static void split_sym(struct symbol
*sym
)
347 if (!(sym
->namespace & NS_SYMBOL
))
350 __pass_to_client(sym
, SYM_HOOK
);
351 split_statements(sym
->stmt
);
352 __split_expr(sym
->array_size
);
353 split_symlist(sym
->arguments
);
354 split_symlist(sym
->symbol_list
);
355 split_statements(sym
->inline_stmt
);
356 split_symlist(sym
->inline_symbol_list
);
357 __split_expr(sym
->initializer
);
360 static void split_symlist(struct symbol_list
*sym_list
)
364 FOR_EACH_PTR(sym_list
, sym
) {
366 } END_FOR_EACH_PTR(sym
);
369 static void split_functions(struct symbol_list
*sym_list
)
373 FOR_EACH_PTR(sym_list
, sym
) {
374 struct symbol
*base_type
;
375 base_type
= get_base_type(sym
);
376 if (sym
->type
== SYM_NODE
&& base_type
->type
== SYM_FN
) {
378 line_func_start
= base_type
->stmt
->pos
.line
;
380 cur_func
= sym
->ident
->name
;
381 __smatch_lineno
= sym
->pos
.line
;
382 SM_DEBUG("new function: %s\n", cur_func
);
383 __pass_to_client(sym
, FUNC_DEF_HOOK
);
385 split_statements(base_type
->stmt
);
386 __pass_to_client(sym
, END_FUNC_HOOK
);
392 __pass_to_client(sym
, BASE_HOOK
);
394 } END_FOR_EACH_PTR(sym
);
395 __pass_to_client_no_data(END_FILE_HOOK
);
398 void smatch (int argc
, char **argv
)
401 struct string_list
*filelist
= NULL
;
402 struct symbol_list
*sym_list
;
405 printf("Usage: smatch [--debug] <filename.c>\n");
408 sparse_initialize(argc
, argv
, &filelist
);
409 FOR_EACH_PTR_NOTAG(filelist
, filename
) {
410 sym_list
= __sparse(filename
);
411 split_functions(sym_list
);
412 } END_FOR_EACH_PTR_NOTAG(filename
);