constraints: remove debugging and use the stripped() expression
[smatch.git] / smatch_assigned_expr.c
blob5a5a122927a24689f35db5a62a84f2251add2d77
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_name_sym(const char *name, struct symbol *sym)
52 struct smatch_state *state;
54 state = get_state(my_id, name, sym);
55 if (!state)
56 return NULL;
57 return (struct expression *)state->data;
60 static void match_assignment(struct expression *expr)
62 struct symbol *left_sym, *right_sym;
63 char *left_name = NULL;
64 char *right_name = NULL;
66 if (expr->op != '=')
67 return;
68 if (is_fake_call(expr->right))
69 return;
70 if (__in_fake_struct_assign) {
71 struct range_list *rl;
73 if (!get_implied_rl(expr->right, &rl))
74 return;
75 if (is_whole_rl(rl))
76 return;
79 left_name = expr_to_var_sym(expr->left, &left_sym);
80 if (!left_name || !left_sym)
81 goto free;
82 set_state(my_id, left_name, left_sym, alloc_state_expr(strip_expr(expr->right)));
84 right_name = expr_to_var_sym(expr->right, &right_sym);
85 if (!right_name || !right_sym)
86 goto free;
88 store_link(link_id, right_name, right_sym, left_name, left_sym);
90 free:
91 free_string(left_name);
92 free_string(right_name);
95 static void record_param_assignment(struct expression *expr, int param, char *key, char *value)
97 struct expression *arg, *right;
98 struct symbol *sym;
99 char *name;
100 char *p;
101 int right_param;
103 while (expr->type == EXPR_ASSIGNMENT)
104 expr = strip_expr(expr->right);
105 if (!expr || expr->type != EXPR_CALL)
106 return;
108 p = strstr(value, "[$");
109 if (!p)
110 return;
112 p += 2;
113 right_param = strtol(p, &p, 10);
114 if (*p != ']')
115 return;
117 arg = get_argument_from_call_expr(expr->args, param);
118 right = get_argument_from_call_expr(expr->args, right_param);
119 if (!right || !arg)
120 return;
121 name = get_variable_from_key(arg, key, &sym);
122 if (!name || !sym)
123 goto free;
125 skip_mod = expr;
126 set_state(my_id, name, sym, alloc_state_expr(right));
127 free:
128 free_string(name);
131 void register_assigned_expr(int id)
133 my_id = check_assigned_expr_id = id;
134 add_hook(&match_assignment, ASSIGNMENT_HOOK_AFTER);
135 add_modification_hook(my_id, &undef);
136 select_return_states_hook(PARAM_SET, &record_param_assignment);
139 void register_assigned_expr_links(int id)
141 link_id = id;
142 db_ignore_states(link_id);
143 set_up_link_functions(my_id, link_id);