db: pass the call expression to call_implies callbacks
[smatch.git] / smatch_scope.c
blob0c51e759d587bf9dbb0da20f3959dfd153329c0c
1 /*
2 * Copyright (C) 2016 Oracle.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, see http://www.gnu.org/copyleft/gpl.txt
18 #include "smatch.h"
20 static struct statement_list *stmt_list;
22 static int end_of_function(struct statement *stmt)
24 struct symbol *fn = get_base_type(cur_func_sym);
26 /* err on the conservative side of things */
27 if (!fn)
28 return 1;
29 if (stmt == fn->stmt || stmt == fn->inline_stmt)
30 return 1;
31 return 0;
35 * We're wasting a lot of time worrying about out of scope variables.
36 * When we come to the end of a scope then just delete them all the out of
37 * scope states.
39 static void match_end_of_block(struct statement *stmt)
41 struct statement *tmp;
42 struct symbol *sym;
44 if (stmt->type != STMT_COMPOUND)
45 return;
47 if (end_of_function(stmt))
48 return;
50 FOR_EACH_PTR(stmt->stmts, tmp) {
51 if (tmp->type != STMT_DECLARATION)
52 return;
54 FOR_EACH_PTR(tmp->declaration, sym) {
55 if (!sym->ident)
56 continue;
57 __delete_all_states_sym(sym);
58 } END_FOR_EACH_PTR(sym);
59 } END_FOR_EACH_PTR(tmp);
62 static int is_outer_stmt(struct statement *stmt)
64 struct symbol *fn = get_base_type(cur_func_sym);
66 if (!fn)
67 return 0;
69 * There are times when ->parent is not set but it's set for
70 * the outer statement so ignoring NULLs works as a work-around.
72 if (!stmt->parent)
73 return 0;
74 if (stmt->parent == fn->stmt ||
75 stmt->parent == fn->inline_stmt)
76 return 1;
77 return 0;
80 static void match_stmt(struct statement *stmt)
82 struct statement *tmp;
84 if (__inline_fn)
85 return;
87 add_ptr_list(&stmt_list, stmt);
89 if (!is_outer_stmt(stmt))
90 return;
92 FOR_EACH_PTR(stmt_list, tmp) {
93 match_end_of_block(tmp);
94 } END_FOR_EACH_PTR(tmp);
95 free_ptr_list(&stmt_list);
98 static void match_end_func(struct symbol *sym)
100 if (__inline_fn)
101 return;
102 free_ptr_list(&stmt_list);
105 void register_scope(int id)
107 add_hook(&match_stmt, STMT_HOOK_AFTER);
108 add_hook(&match_end_func, AFTER_FUNC_HOOK);