4 * Copyright (C) 2006,2008 Dan Carpenter.
6 * Licensed under the Open Software License version 1.1
13 #include "smatch_expression_stacks.h"
14 #include "smatch_extra.h"
16 static int __smatch_lineno
= 0;
18 static char *filename
;
19 static char *cur_func
;
20 static int line_func_start
;
21 static struct expression_stack
*switch_expr_stack
= NULL
;
23 char *get_filename(void) { return filename
; }
24 char *get_function(void) { return cur_func
; }
25 int get_lineno(void) { return __smatch_lineno
; }
26 int get_func_pos(void) { return __smatch_lineno
- line_func_start
; }
28 static void split_symlist(struct symbol_list
*sym_list
);
29 static void split_declaration(struct symbol_list
*sym_list
);
30 static void split_expr_list(struct expression_list
*expr_list
);
32 int option_assume_loops
= 0;
33 int option_known_conditions
= 0;
35 void __split_expr(struct expression
*expr
)
40 // printf("%d Debug expr_type %d %s\n", get_lineno(), expr->type, show_special(expr->op));
42 __smatch_lineno
= expr
->pos
.line
;
43 __pass_to_client(expr
, EXPR_HOOK
);
48 __pass_to_client(expr
, OP_HOOK
);
49 __split_expr(expr
->unop
);
52 __split_statements(expr
->statement
);
55 __split_whole_condition(expr
);
58 __merge_true_states();
59 __pop_false_only_stack();
66 __split_expr(expr
->left
);
67 __split_expr(expr
->right
);
69 case EXPR_ASSIGNMENT
: {
70 struct expression
*tmp
;
72 __split_expr(expr
->right
);
73 __pass_to_client(expr
, ASSIGNMENT_HOOK
);
74 tmp
= strip_expr(expr
->right
);
75 if (tmp
->type
== EXPR_CALL
)
76 __pass_to_client(expr
, CALL_ASSIGNMENT_HOOK
);
77 __split_expr(expr
->left
);
81 __pass_to_client(expr
, DEREF_HOOK
);
82 __split_expr(expr
->deref
);
85 __split_expr(expr
->base
);
88 __split_expr(expr
->cast_expression
);
91 /* there isn't anything to pass a client from inside a sizeof() */
93 case EXPR_CONDITIONAL
:
95 __split_whole_condition(expr
->conditional
);
96 __split_expr(expr
->cond_true
);
99 __split_expr(expr
->cond_false
);
100 __merge_true_states();
101 __pop_false_only_stack();
104 split_expr_list(expr
->args
);
105 __split_expr(expr
->fn
);
106 __pass_to_client(expr
, FUNCTION_CALL_HOOK
);
108 case EXPR_INITIALIZER
:
109 split_expr_list(expr
->expr_list
);
111 case EXPR_IDENTIFIER
:
112 __split_expr(expr
->ident_expression
);
115 __split_expr(expr
->idx_expression
);
118 __split_expr(expr
->init_expr
);
125 static int is_forever_loop(struct statement
*stmt
)
128 struct expression
*expr
;
130 expr
= strip_expr(stmt
->iterator_pre_condition
);
132 expr
= stmt
->iterator_post_condition
;
134 /* this is a for(;;) loop... */
138 if (expr
->type
== EXPR_VALUE
&& expr
->value
== 1) {
146 * Pre Loops are while and for loops.
149 static void handle_pre_loop(struct statement
*stmt
)
151 int once_through
; /* we go through the loop at least once */
152 struct sm_state
*extra_state
= NULL
;
156 __split_statements(stmt
->iterator_pre_statement
);
158 once_through
= implied_condition_true(stmt
->iterator_pre_condition
);
163 __split_whole_condition(stmt
->iterator_pre_condition
);
165 extra_state
= __extra_pre_loop_hook_before(stmt
->iterator_pre_statement
);
166 if (option_assume_loops
)
168 __split_statements(stmt
->iterator_statement
);
170 __warn_on_silly_pre_loops();
171 if (is_forever_loop(stmt
)) {
172 __pop_false_only_stack();
173 /* forever loops don't have an iterator_post_statement */
175 __pop_false_states();
177 } else if (once_through
) {
180 unchanged
= __iterator_unchanged(extra_state
, stmt
->iterator_post_statement
);
181 __split_statements(stmt
->iterator_post_statement
);
182 __split_whole_condition(stmt
->iterator_pre_condition
);
184 __merge_false_states();
185 if (extra_state
&& unchanged
)
186 __extra_pre_loop_hook_after(extra_state
,
187 stmt
->iterator_post_statement
, stmt
->iterator_pre_condition
);
188 __pop_false_states();
189 __pop_false_only_stack();
190 __pop_false_only_stack();
194 __split_statements(stmt
->iterator_post_statement
);
195 __merge_false_states();
196 __use_false_only_stack();
202 * Post loops are do {} while();
204 static void handle_post_loop(struct statement
*stmt
)
208 __split_statements(stmt
->iterator_statement
);
209 if (is_forever_loop(stmt
)) {
215 __split_whole_condition(stmt
->iterator_post_condition
);
216 __use_false_states();
219 __pop_false_only_stack();
222 static void print_unreached(struct statement
*stmt
)
226 * GCC insists on a return statement even where it is never
227 * reached. Also BUG() sometimes is a forever loop and
228 * sometimes not so people put code after a BUG(). There
229 * are way to many false positives.
234 if (__path_is_null()) {
236 case STMT_COMPOUND
: /* after a switch before a case stmt */
239 case STMT_DECLARATION
: /* switch(x) { int a; case foo: ... */
242 smatch_msg("unreachable code. %d", stmt
->type
);
247 void __split_statements(struct statement
*stmt
)
252 if (out_of_memory()) {
253 static char *printed
= NULL
;
255 if (printed
!= cur_func
)
256 smatch_msg("Function too big. Giving up.");
261 __smatch_lineno
= stmt
->pos
.line
;
262 print_unreached(stmt
);
263 __pass_to_client(stmt
, STMT_HOOK
);
265 switch (stmt
->type
) {
266 case STMT_DECLARATION
:
267 split_declaration(stmt
->declaration
);
270 __split_expr(stmt
->ret_value
);
271 __pass_to_client(stmt
, RETURN_HOOK
);
274 case STMT_EXPRESSION
:
275 __split_expr(stmt
->expression
);
277 case STMT_COMPOUND
: {
279 __push_scope_hooks();
280 FOR_EACH_PTR(stmt
->stmts
, s
) {
281 __split_statements(s
);
282 } END_FOR_EACH_PTR(s
);
283 __call_scope_hooks();
287 if (known_condition_true(stmt
->if_conditional
)) {
288 __split_statements(stmt
->if_true
);
291 if (known_condition_false(stmt
->if_conditional
)) {
292 __split_statements(stmt
->if_false
);
295 if (option_known_conditions
&&
296 implied_condition_true(stmt
->if_conditional
)) {
297 smatch_msg("info: this condition is true.");
298 __split_statements(stmt
->if_true
);
301 if (option_known_conditions
&&
302 implied_condition_false(stmt
->if_conditional
)) {
303 smatch_msg("info: this condition is false.");
304 __split_statements(stmt
->if_false
);
307 __split_whole_condition(stmt
->if_conditional
);
308 __split_statements(stmt
->if_true
);
309 __push_true_states();
310 __use_false_states();
311 __split_statements(stmt
->if_false
);
312 __merge_true_states();
313 __pop_false_only_stack();
316 if (stmt
->iterator_pre_condition
)
317 handle_pre_loop(stmt
);
318 else if (stmt
->iterator_post_condition
)
319 handle_post_loop(stmt
);
321 // these are for(;;) type loops.
322 handle_pre_loop(stmt
);
326 __split_expr(stmt
->switch_expression
);
327 push_expression(&switch_expr_stack
, stmt
->switch_expression
);
328 __save_switch_states(top_expression(switch_expr_stack
));
332 __split_statements(stmt
->switch_statement
);
333 if (!__pop_default())
334 __merge_switches(top_expression(switch_expr_stack
),
338 pop_expression(&switch_expr_stack
);
341 __merge_switches(top_expression(switch_expr_stack
),
342 stmt
->case_expression
);
343 __pass_case_to_client(top_expression(switch_expr_stack
),
344 stmt
->case_expression
);
345 if (!stmt
->case_expression
)
347 __split_expr(stmt
->case_expression
);
348 __split_expr(stmt
->case_to
);
349 __split_statements(stmt
->case_statement
);
353 stmt
->label
->type
== SYM_LABEL
&&
354 stmt
->label
->ident
) {
355 __merge_gotos(stmt
->label
->ident
->name
);
357 __split_statements(stmt
->label_statement
);
360 __split_expr(stmt
->goto_expression
);
361 if (stmt
->goto_label
&& stmt
->goto_label
->type
== SYM_NODE
) {
362 if (!strcmp(stmt
->goto_label
->ident
->name
, "break")) {
364 } else if (!strcmp(stmt
->goto_label
->ident
->name
,
366 __process_continues();
368 } else if (stmt
->goto_label
&&
369 stmt
->goto_label
->type
== SYM_LABEL
&&
370 stmt
->goto_label
->ident
) {
371 __save_gotos(stmt
->goto_label
->ident
->name
);
378 __split_expr(stmt
->asm_string
);
379 //__split_expr(stmt->asm_outputs);
380 //__split_expr(stmt->asm_inputs);
381 //__split_expr(stmt->asm_clobbers);
386 __split_expr(stmt
->range_expression
);
387 __split_expr(stmt
->range_low
);
388 __split_expr(stmt
->range_high
);
393 static void split_expr_list(struct expression_list
*expr_list
)
395 struct expression
*expr
;
396 FOR_EACH_PTR(expr_list
, expr
) {
398 } END_FOR_EACH_PTR(expr
);
402 static void split_sym(struct symbol
*sym
)
406 if (!(sym
->namespace & NS_SYMBOL
))
409 __pass_to_client(sym
, SYM_HOOK
);
410 __split_statements(sym
->stmt
);
411 __split_expr(sym
->array_size
);
412 split_symlist(sym
->arguments
);
413 split_symlist(sym
->symbol_list
);
414 __split_statements(sym
->inline_stmt
);
415 split_symlist(sym
->inline_symbol_list
);
416 __split_expr(sym
->initializer
);
419 static void split_symlist(struct symbol_list
*sym_list
)
423 FOR_EACH_PTR(sym_list
, sym
) {
425 } END_FOR_EACH_PTR(sym
);
428 static void split_declaration(struct symbol_list
*sym_list
)
432 FOR_EACH_PTR(sym_list
, sym
) {
433 __pass_to_client(sym
, DECLARATION_HOOK
);
434 __split_expr(sym
->initializer
);
435 if(sym
->initializer
&& sym
->initializer
->type
== EXPR_CALL
) {
436 __match_initializer_call(sym
);
439 } END_FOR_EACH_PTR(sym
);
442 static void split_functions(struct symbol_list
*sym_list
)
446 FOR_EACH_PTR(sym_list
, sym
) {
447 struct symbol
*base_type
;
448 base_type
= get_base_type(sym
);
449 if (sym
->type
== SYM_NODE
&& base_type
->type
== SYM_FN
) {
451 line_func_start
= base_type
->stmt
->pos
.line
;
453 cur_func
= sym
->ident
->name
;
454 __smatch_lineno
= sym
->pos
.line
;
455 SM_DEBUG("new function: %s\n", cur_func
);
457 __pass_to_client(sym
, FUNC_DEF_HOOK
);
458 __split_statements(base_type
->stmt
);
459 __pass_to_client(sym
, END_FUNC_HOOK
);
463 free_expression_stack(&switch_expr_stack
);
465 __pass_to_client(sym
, BASE_HOOK
);
467 } END_FOR_EACH_PTR(sym
);
468 __pass_to_client_no_data(END_FILE_HOOK
);
471 void smatch (int argc
, char **argv
)
474 struct string_list
*filelist
= NULL
;
475 struct symbol_list
*sym_list
;
478 printf("Usage: smatch [--debug] <filename.c>\n");
481 sparse_initialize(argc
, argv
, &filelist
);
482 FOR_EACH_PTR_NOTAG(filelist
, filename
) {
483 sym_list
= __sparse(filename
);
484 split_functions(sym_list
);
485 } END_FOR_EACH_PTR_NOTAG(filename
);