assigned_expr: add reverse links
[smatch.git] / check_assigned_expr.c
blob5e7e47d7161a0c07c500006a6f0d08b6caf0f487
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;
29 static int link_id;
31 struct expression *get_assigned_expr(struct expression *expr)
33 struct smatch_state *state;
35 state = get_state_expr(my_id, expr);
36 if (!state)
37 return NULL;
38 return (struct expression *)state->data;
41 struct expression *get_assigned_expr_name_sym(const char *name, struct symbol *sym)
43 struct smatch_state *state;
45 state = get_state(my_id, name, sym);
46 if (!state)
47 return NULL;
48 return (struct expression *)state->data;
51 static struct smatch_state *alloc_my_state(struct expression *expr)
53 struct smatch_state *state;
54 char *name;
56 state = __alloc_smatch_state(0);
57 expr = strip_expr(expr);
58 name = expr_to_str(expr);
59 state->name = alloc_sname(name);
60 free_string(name);
61 state->data = expr;
62 return state;
65 static void match_assignment(struct expression *expr)
67 struct symbol *left_sym, *right_sym;
68 char *left_name = NULL;
69 char *right_name = NULL;
71 if (expr->op != '=')
72 return;
73 if (is_fake_call(expr->right))
74 return;
76 left_name = expr_to_var_sym(expr->left, &left_sym);
77 if (!left_name || !left_sym)
78 goto free;
79 set_state(my_id, left_name, left_sym, alloc_my_state(expr->right));
81 right_name = expr_to_var_sym(expr->right, &right_sym);
82 if (!right_name || !right_sym)
83 goto free;
85 store_link(link_id, right_name, right_sym, left_name, left_sym);
87 free:
88 free_string(left_name);
89 free_string(right_name);
92 void check_assigned_expr(int id)
94 my_id = check_assigned_expr_id = id;
95 add_hook(&match_assignment, ASSIGNMENT_HOOK);
98 void check_assigned_expr_links(int id)
100 link_id = id;
101 set_up_link_functions(my_id, link_id);