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\n", get_lineno(), expr->type);
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 FOR_EACH_PTR(stmt
->stmts
, s
) {
280 __split_statements(s
);
281 } END_FOR_EACH_PTR(s
);
285 if (known_condition_true(stmt
->if_conditional
)) {
286 __split_statements(stmt
->if_true
);
289 if (known_condition_false(stmt
->if_conditional
)) {
290 __split_statements(stmt
->if_false
);
293 if (option_known_conditions
&&
294 implied_condition_true(stmt
->if_conditional
)) {
295 smatch_msg("info: this condition is true.");
296 __split_statements(stmt
->if_true
);
299 if (option_known_conditions
&&
300 implied_condition_false(stmt
->if_conditional
)) {
301 smatch_msg("info: this condition is false.");
302 __split_statements(stmt
->if_false
);
305 __split_whole_condition(stmt
->if_conditional
);
306 __split_statements(stmt
->if_true
);
307 __push_true_states();
308 __use_false_states();
309 __split_statements(stmt
->if_false
);
310 __merge_true_states();
311 __pop_false_only_stack();
314 if (stmt
->iterator_pre_condition
)
315 handle_pre_loop(stmt
);
316 else if (stmt
->iterator_post_condition
)
317 handle_post_loop(stmt
);
319 // these are for(;;) type loops.
320 handle_pre_loop(stmt
);
324 __split_expr(stmt
->switch_expression
);
325 push_expression(&switch_expr_stack
, stmt
->switch_expression
);
326 __save_switch_states();
330 __split_statements(stmt
->switch_statement
);
331 if (!__pop_default())
332 __merge_switches(top_expression(switch_expr_stack
),
336 pop_expression(&switch_expr_stack
);
339 __merge_switches(top_expression(switch_expr_stack
),
340 stmt
->case_expression
);
341 __pass_case_to_client(top_expression(switch_expr_stack
),
342 stmt
->case_expression
);
343 if (!stmt
->case_expression
)
345 __split_expr(stmt
->case_expression
);
346 __split_expr(stmt
->case_to
);
347 __split_statements(stmt
->case_statement
);
351 stmt
->label
->type
== SYM_LABEL
&&
352 stmt
->label
->ident
) {
353 __merge_gotos(stmt
->label
->ident
->name
);
355 __split_statements(stmt
->label_statement
);
358 __split_expr(stmt
->goto_expression
);
359 if (stmt
->goto_label
&& stmt
->goto_label
->type
== SYM_NODE
) {
360 if (!strcmp(stmt
->goto_label
->ident
->name
, "break")) {
362 } else if (!strcmp(stmt
->goto_label
->ident
->name
,
364 __process_continues();
366 } else if (stmt
->goto_label
&&
367 stmt
->goto_label
->type
== SYM_LABEL
&&
368 stmt
->goto_label
->ident
) {
369 __save_gotos(stmt
->goto_label
->ident
->name
);
376 __split_expr(stmt
->asm_string
);
377 //__split_expr(stmt->asm_outputs);
378 //__split_expr(stmt->asm_inputs);
379 //__split_expr(stmt->asm_clobbers);
384 __split_expr(stmt
->range_expression
);
385 __split_expr(stmt
->range_low
);
386 __split_expr(stmt
->range_high
);
391 static void split_expr_list(struct expression_list
*expr_list
)
393 struct expression
*expr
;
394 FOR_EACH_PTR(expr_list
, expr
) {
396 } END_FOR_EACH_PTR(expr
);
400 static void split_sym(struct symbol
*sym
)
404 if (!(sym
->namespace & NS_SYMBOL
))
407 __pass_to_client(sym
, SYM_HOOK
);
408 __split_statements(sym
->stmt
);
409 __split_expr(sym
->array_size
);
410 split_symlist(sym
->arguments
);
411 split_symlist(sym
->symbol_list
);
412 __split_statements(sym
->inline_stmt
);
413 split_symlist(sym
->inline_symbol_list
);
414 __split_expr(sym
->initializer
);
417 static void split_symlist(struct symbol_list
*sym_list
)
421 FOR_EACH_PTR(sym_list
, sym
) {
423 } END_FOR_EACH_PTR(sym
);
426 static void split_declaration(struct symbol_list
*sym_list
)
430 FOR_EACH_PTR(sym_list
, sym
) {
431 __pass_to_client(sym
, DECLARATION_HOOK
);
432 __split_expr(sym
->initializer
);
433 if(sym
->initializer
&& sym
->initializer
->type
== EXPR_CALL
) {
434 __match_initializer_call(sym
);
437 } END_FOR_EACH_PTR(sym
);
440 static void split_functions(struct symbol_list
*sym_list
)
444 FOR_EACH_PTR(sym_list
, sym
) {
445 struct symbol
*base_type
;
446 base_type
= get_base_type(sym
);
447 if (sym
->type
== SYM_NODE
&& base_type
->type
== SYM_FN
) {
449 line_func_start
= base_type
->stmt
->pos
.line
;
451 cur_func
= sym
->ident
->name
;
452 __smatch_lineno
= sym
->pos
.line
;
453 SM_DEBUG("new function: %s\n", cur_func
);
455 __pass_to_client(sym
, FUNC_DEF_HOOK
);
456 __split_statements(base_type
->stmt
);
457 __pass_to_client(sym
, END_FUNC_HOOK
);
461 free_expression_stack(&switch_expr_stack
);
463 __pass_to_client(sym
, BASE_HOOK
);
465 } END_FOR_EACH_PTR(sym
);
466 __pass_to_client_no_data(END_FILE_HOOK
);
469 void smatch (int argc
, char **argv
)
472 struct string_list
*filelist
= NULL
;
473 struct symbol_list
*sym_list
;
476 printf("Usage: smatch [--debug] <filename.c>\n");
479 sparse_initialize(argc
, argv
, &filelist
);
480 FOR_EACH_PTR_NOTAG(filelist
, filename
) {
481 sym_list
= __sparse(filename
);
482 split_functions(sym_list
);
483 } END_FOR_EACH_PTR_NOTAG(filename
);