states: add another read_only check
[smatch.git] / check_assigned_expr.c
blob8e965142d176a19a8e9db19a35fb21230fa4d602
1 /*
2 * Copyright (C) 2009 Dan Carpenter.
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
19 * This is not a check. It just saves an struct expression pointer
20 * whenever something is assigned. This can be used later on by other scripts.
23 #include "smatch.h"
24 #include "smatch_slist.h"
25 #include "smatch_extra.h"
27 int check_assigned_expr_id;
28 static int my_id;
30 struct expression *get_assigned_expr(struct expression *expr)
32 struct smatch_state *state;
34 state = get_state_expr(my_id, expr);
35 if (!state)
36 return NULL;
37 return (struct expression *)state->data;
40 static struct smatch_state *alloc_my_state(struct expression *expr)
42 struct smatch_state *state;
43 char *name;
45 state = __alloc_smatch_state(0);
46 expr = strip_expr(expr);
47 name = expr_to_str(expr);
48 state->name = alloc_sname(name);
49 free_string(name);
50 state->data = expr;
51 return state;
54 static void match_assignment(struct expression *expr)
56 if (expr->op != '=')
57 return;
58 if (is_fake_call(expr->right))
59 return;
60 set_state_expr(my_id, expr->left, alloc_my_state(expr->right));
63 void check_assigned_expr(int id)
65 my_id = check_assigned_expr_id = id;
66 add_hook(&match_assignment, ASSIGNMENT_HOOK);