modification_hooks: going out of scope is not a modification
[smatch.git] / smatch_auto_copy.c
blob127589f20ed35cc84d68797acc0ff0c16fed984d
1 /*
2 * Copyright (C) 2014 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"
19 #include "smatch_slist.h"
21 static int my_id;
23 static int *auto_copy;
25 void set_auto_copy(int owner)
27 if (owner <= 1 || owner > num_checks) {
28 printf("bogus set_auto_copy()\n");
29 return;
31 auto_copy[owner] = 1;
34 static void match_assign(struct expression *expr)
36 char *left_name = NULL;
37 char *right_name = NULL;
38 struct symbol *left_sym, *right_sym;
39 struct state_list *slist = NULL;
40 struct sm_state *sm;
42 left_name = expr_to_var_sym(expr->left, &left_sym);
43 if (!left_name || !left_sym)
44 goto free;
45 right_name = expr_to_var_sym(expr->right, &right_sym);
46 if (!right_name || !right_sym)
47 goto free;
49 FOR_EACH_SM(__get_cur_stree(), sm) {
50 if (sm->owner <= 1 || sm->owner > num_checks)
51 continue;
52 if (!auto_copy[sm->owner])
53 continue;
54 if (right_sym != sm->sym)
55 continue;
56 if (strcmp(right_name, sm->name) != 0)
57 continue;
58 add_ptr_list(&slist, sm);
59 } END_FOR_EACH_SM(sm);
62 FOR_EACH_PTR(slist, sm) {
63 set_state(sm->owner, left_name, left_sym, sm->state);
64 } END_FOR_EACH_PTR(sm);
66 free:
67 free_slist(&slist);
68 free_string(left_name);
69 free_string(right_name);
72 void register_auto_copy(int id)
74 my_id = id;
75 auto_copy = malloc((num_checks + 1) * sizeof(*auto_copy));
76 memset(auto_copy, 0, (num_checks + 1) * sizeof(*auto_copy));
78 add_hook(&match_assign, ASSIGNMENT_HOOK);