missing_error_code: introduce new check
[smatch.git] / smatch_assigned_expr.c
blob4fbc18073f45341de0f987b0f4475fc8011ebf83
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 expression *get_assigned_expr_recurse(struct expression *expr)
52 struct expression *ret;
53 int cnt = 0;
55 ret = NULL;
56 while ((expr = get_assigned_expr(expr))) {
57 ret = expr;
58 if (cnt++ > 4)
59 break;
62 return ret;
65 struct expression *get_assigned_expr_name_sym(const char *name, struct symbol *sym)
67 struct smatch_state *state;
69 state = __get_state(my_id, name, sym);
70 if (!state)
71 return NULL;
72 return (struct expression *)state->data;
75 struct expression *get_assigned_expr_name_sym_recurse(const char *name, struct symbol *sym)
77 struct expression *expr, *recurse;
78 struct smatch_state *state;
80 expr = get_assigned_expr_name_sym(name, sym);
81 if (!expr)
82 return NULL;
83 recurse = get_assigned_expr_recurse(expr);
84 if (recurse)
85 return recurse;
86 return expr;
89 static void match_assignment(struct expression *expr)
91 static struct expression *ignored_expr, *right;
92 struct symbol *left_sym, *right_sym;
93 char *left_name = NULL;
94 char *right_name = NULL;
96 if (expr->op != '=')
97 return;
98 if (is_fake_call(expr->right))
99 return;
100 if (__in_fake_struct_assign) {
101 struct range_list *rl;
103 if (!get_implied_rl(expr->right, &rl))
104 return;
105 if (is_whole_rl(rl))
106 return;
109 if (expr->left == ignored_expr)
110 return;
111 ignored_expr = NULL;
112 if (__in_fake_parameter_assign)
113 ignored_expr = expr->left;
115 left_name = expr_to_var_sym(expr->left, &left_sym);
116 if (!left_name || !left_sym)
117 goto free;
119 right = expr->right;
120 if (right->type == EXPR_ASSIGNMENT && right->op == '=')
121 right = right->left;
123 set_state(my_id, left_name, left_sym, alloc_state_expr(strip_expr(right)));
125 right_name = expr_to_var_sym(right, &right_sym);
126 if (!right_name || !right_sym)
127 goto free;
129 store_link(link_id, right_name, right_sym, left_name, left_sym);
131 free:
132 free_string(left_name);
133 free_string(right_name);
136 static void record_param_assignment(struct expression *expr, int param, char *key, char *value)
138 struct expression *arg, *right;
139 struct symbol *sym;
140 char *name;
141 char *p;
142 int right_param;
144 while (expr->type == EXPR_ASSIGNMENT)
145 expr = strip_expr(expr->right);
146 if (!expr || expr->type != EXPR_CALL)
147 return;
149 p = strstr(value, "[$");
150 if (!p)
151 return;
153 p += 2;
154 right_param = strtol(p, &p, 10);
155 if (*p != ']')
156 return;
158 arg = get_argument_from_call_expr(expr->args, param);
159 right = get_argument_from_call_expr(expr->args, right_param);
160 if (!right || !arg)
161 return;
162 name = get_variable_from_key(arg, key, &sym);
163 if (!name || !sym)
164 goto free;
166 skip_mod = expr;
167 set_state(my_id, name, sym, alloc_state_expr(right));
168 free:
169 free_string(name);
172 void register_assigned_expr(int id)
174 my_id = check_assigned_expr_id = id;
175 set_dynamic_states(check_assigned_expr_id);
176 add_hook(&match_assignment, ASSIGNMENT_HOOK_AFTER);
177 add_modification_hook(my_id, &undef);
178 select_return_states_hook(PARAM_SET, &record_param_assignment);
181 void register_assigned_expr_links(int id)
183 link_id = id;
184 set_dynamic_states(link_id);
185 db_ignore_states(link_id);
186 set_up_link_functions(my_id, link_id);