param_cleared: handle direct assignments
[smatch.git] / check_assigned_expr.c
blob1f3c527d32adc1c3ec43aea3b10b801e56bcddf2
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 struct expression *get_assigned_expr_name_sym(const char *name, struct symbol *sym)
42 struct smatch_state *state;
44 state = get_state(my_id, name, sym);
45 if (!state)
46 return NULL;
47 return (struct expression *)state->data;
50 static struct smatch_state *alloc_my_state(struct expression *expr)
52 struct smatch_state *state;
53 char *name;
55 state = __alloc_smatch_state(0);
56 expr = strip_expr(expr);
57 name = expr_to_str(expr);
58 state->name = alloc_sname(name);
59 free_string(name);
60 state->data = expr;
61 return state;
64 static void match_assignment(struct expression *expr)
66 if (expr->op != '=')
67 return;
68 if (is_fake_call(expr->right))
69 return;
70 set_state_expr(my_id, expr->left, alloc_my_state(expr->right));
73 void check_assigned_expr(int id)
75 my_id = check_assigned_expr_id = id;
76 add_hook(&match_assignment, ASSIGNMENT_HOOK);