db/fixup_kernel.sh: fix clear_user() handling
[smatch.git] / smatch_assigned_expr.c
blob6e7bb602e931cd18fd41909fb1cca80459c27836
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 static struct expression *skip_mod;
33 static void undef(struct sm_state *sm, struct expression *mod_expr)
35 if (mod_expr == skip_mod)
36 return;
37 set_state(my_id, sm->name, sm->sym, &undefined);
40 struct expression *get_assigned_expr(struct expression *expr)
42 struct smatch_state *state;
44 state = get_state_expr(my_id, expr);
45 if (!state)
46 return NULL;
47 return (struct expression *)state->data;
50 struct sm_state *get_assigned_sm(struct expression *expr)
52 return get_sm_state_expr(my_id, expr);
55 struct expression *get_assigned_expr_recurse(struct expression *expr)
57 struct expression *ret;
58 int cnt = 0;
60 ret = NULL;
61 while ((expr = get_assigned_expr(expr))) {
62 ret = expr;
63 if (cnt++ > 4)
64 break;
67 return ret;
70 struct expression *get_assigned_expr_name_sym(const char *name, struct symbol *sym)
72 struct smatch_state *state;
74 state = __get_state(my_id, name, sym);
75 if (!state)
76 return NULL;
77 return (struct expression *)state->data;
80 struct expression *get_assigned_expr_name_sym_recurse(const char *name, struct symbol *sym)
82 struct expression *expr, *recurse;
84 expr = get_assigned_expr_name_sym(name, sym);
85 if (!expr)
86 return NULL;
87 recurse = get_assigned_expr_recurse(expr);
88 if (recurse)
89 return recurse;
90 return expr;
93 static void match_assignment(struct expression *expr)
95 static struct expression *ignored_expr, *right;
96 struct symbol *left_sym, *right_sym;
97 struct smatch_state *state;
98 char *left_name = NULL;
99 char *right_name = NULL;
101 if (!cur_func_sym)
102 return;
104 if (__in_buf_clear)
105 return;
107 if (expr->op != '=')
108 return;
109 if (is_fake_call(expr->right))
110 return;
111 if (is_fake_var_assign(expr))
112 return;
113 if (__in_fake_struct_assign) {
114 struct range_list *rl;
116 if (!get_implied_rl(expr->right, &rl))
117 return;
118 if (is_whole_rl(rl))
119 return;
122 if (expr->left == ignored_expr)
123 return;
124 ignored_expr = NULL;
125 if (__in_fake_parameter_assign)
126 ignored_expr = expr->left;
128 left_name = expr_to_var_sym(expr->left, &left_sym);
129 if (!left_name || !left_sym)
130 goto free;
132 right = expr->right;
133 if (right->type == EXPR_ASSIGNMENT && right->op == '=')
134 right = right->left;
136 right = strip__builtin_choose_expr(right);
137 right = strip_Generic(right);
139 state = alloc_state_expr(strip_expr(right));
140 if (!state)
141 goto free;
143 skip_mod = expr;
144 if (get_unfaked_call())
145 skip_mod = get_unfaked_call();
146 set_state(my_id, left_name, left_sym, state);
148 right_name = expr_to_var_sym(right, &right_sym);
149 if (!right_name || !right_sym)
150 goto free;
152 store_link(link_id, right_name, right_sym, left_name, left_sym);
154 free:
155 free_string(left_name);
156 free_string(right_name);
159 static void record_param_assignment(struct expression *expr, int param, char *key, char *value)
161 struct expression *arg, *right;
162 struct symbol *sym;
163 char *name;
164 char *p;
165 int right_param;
167 while (expr->type == EXPR_ASSIGNMENT)
168 expr = strip_expr(expr->right);
169 if (!expr || expr->type != EXPR_CALL)
170 return;
172 p = strstr(value, "[$");
173 if (!p)
174 return;
176 p += 2;
177 right_param = strtol(p, &p, 10);
178 if (*p != ']')
179 return;
181 arg = get_argument_from_call_expr(expr->args, param);
182 right = get_argument_from_call_expr(expr->args, right_param);
183 if (!right || !arg)
184 return;
185 name = get_variable_from_key(arg, key, &sym);
186 if (!name || !sym)
187 goto free;
189 skip_mod = expr;
190 set_state(my_id, name, sym, alloc_state_expr(right));
191 free:
192 free_string(name);
195 void register_assigned_expr(int id)
197 my_id = check_assigned_expr_id = id;
198 add_function_data((unsigned long *)&skip_mod);
199 set_dynamic_states(check_assigned_expr_id);
200 add_hook(&match_assignment, ASSIGNMENT_HOOK_AFTER);
201 add_modification_hook_late(my_id, &undef);
202 select_return_states_hook(PARAM_SET, &record_param_assignment);
205 void register_assigned_expr_links(int id)
207 link_id = id;
208 set_dynamic_states(link_id);
209 db_ignore_states(link_id);
210 set_up_link_functions(my_id, link_id);