comparison: rename var1 to left_var and var2 to right_var
[smatch.git] / smatch_scope.c
blobef5ec8bc4711e88de7495611a0f8bd308cf9416b
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 (end_of_function(stmt))
45 return;
47 FOR_EACH_PTR(stmt->stmts, tmp) {
48 if (tmp->type != STMT_DECLARATION)
49 return;
51 FOR_EACH_PTR(tmp->declaration, sym) {
52 if (!sym->ident)
53 continue;
54 __delete_all_states_sym(sym);
55 } END_FOR_EACH_PTR(sym);
56 } END_FOR_EACH_PTR(tmp);
59 static int is_outer_stmt(struct statement *stmt)
61 struct symbol *fn = get_base_type(cur_func_sym);
63 if (!fn)
64 return 0;
66 * There are times when ->parent is not set but it's set for
67 * the outer statement so ignoring NULLs works as a work-around.
69 if (!stmt->parent)
70 return 0;
71 if (stmt->parent == fn->stmt ||
72 stmt->parent == fn->inline_stmt)
73 return 1;
74 return 0;
77 static void match_stmt(struct statement *stmt)
79 struct statement *tmp;
81 if (__inline_fn)
82 return;
84 if (stmt->type == STMT_COMPOUND)
85 add_ptr_list(&stmt_list, stmt);
87 if (!is_outer_stmt(stmt))
88 return;
90 FOR_EACH_PTR(stmt_list, tmp) {
91 match_end_of_block(tmp);
92 } END_FOR_EACH_PTR(tmp);
93 free_ptr_list(&stmt_list);
96 static void match_end_func(struct symbol *sym)
98 if (__inline_fn)
99 return;
100 free_ptr_list(&stmt_list);
103 void register_scope(int id)
105 add_hook(&match_stmt, STMT_HOOK_AFTER);
106 add_hook(&match_end_func, AFTER_FUNC_HOOK);