11f5207b7SJohn Levon /*
21f5207b7SJohn Levon * Copyright (C) 2009 Dan Carpenter.
31f5207b7SJohn Levon *
41f5207b7SJohn Levon * This program is free software; you can redistribute it and/or
51f5207b7SJohn Levon * modify it under the terms of the GNU General Public License
61f5207b7SJohn Levon * as published by the Free Software Foundation; either version 2
71f5207b7SJohn Levon * of the License, or (at your option) any later version.
81f5207b7SJohn Levon *
91f5207b7SJohn Levon * This program is distributed in the hope that it will be useful,
101f5207b7SJohn Levon * but WITHOUT ANY WARRANTY; without even the implied warranty of
111f5207b7SJohn Levon * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
121f5207b7SJohn Levon * GNU General Public License for more details.
131f5207b7SJohn Levon *
141f5207b7SJohn Levon * You should have received a copy of the GNU General Public License
151f5207b7SJohn Levon * along with this program; if not, see http://www.gnu.org/copyleft/gpl.txt
161f5207b7SJohn Levon */
171f5207b7SJohn Levon
181f5207b7SJohn Levon /*
191f5207b7SJohn Levon * This is not a check. It just saves an struct expression pointer
201f5207b7SJohn Levon * whenever something is assigned. This can be used later on by other scripts.
211f5207b7SJohn Levon */
221f5207b7SJohn Levon
231f5207b7SJohn Levon #include "smatch.h"
241f5207b7SJohn Levon #include "smatch_slist.h"
251f5207b7SJohn Levon #include "smatch_extra.h"
261f5207b7SJohn Levon
271f5207b7SJohn Levon int check_assigned_expr_id;
281f5207b7SJohn Levon static int my_id;
291f5207b7SJohn Levon static int link_id;
301f5207b7SJohn Levon
31efe51d0cSJohn Levon static struct expression *skip_mod;
32efe51d0cSJohn Levon
undef(struct sm_state * sm,struct expression * mod_expr)331f5207b7SJohn Levon static void undef(struct sm_state *sm, struct expression *mod_expr)
341f5207b7SJohn Levon {
35efe51d0cSJohn Levon if (mod_expr == skip_mod)
36efe51d0cSJohn Levon return;
371f5207b7SJohn Levon set_state(my_id, sm->name, sm->sym, &undefined);
381f5207b7SJohn Levon }
391f5207b7SJohn Levon
get_assigned_expr(struct expression * expr)401f5207b7SJohn Levon struct expression *get_assigned_expr(struct expression *expr)
411f5207b7SJohn Levon {
421f5207b7SJohn Levon struct smatch_state *state;
431f5207b7SJohn Levon
441f5207b7SJohn Levon state = get_state_expr(my_id, expr);
451f5207b7SJohn Levon if (!state)
461f5207b7SJohn Levon return NULL;
471f5207b7SJohn Levon return (struct expression *)state->data;
481f5207b7SJohn Levon }
491f5207b7SJohn Levon
get_assigned_expr_name_sym(const char * name,struct symbol * sym)501f5207b7SJohn Levon struct expression *get_assigned_expr_name_sym(const char *name, struct symbol *sym)
511f5207b7SJohn Levon {
521f5207b7SJohn Levon struct smatch_state *state;
531f5207b7SJohn Levon
54*c85f09ccSJohn Levon state = __get_state(my_id, name, sym);
551f5207b7SJohn Levon if (!state)
561f5207b7SJohn Levon return NULL;
571f5207b7SJohn Levon return (struct expression *)state->data;
581f5207b7SJohn Levon }
591f5207b7SJohn Levon
match_assignment(struct expression * expr)601f5207b7SJohn Levon static void match_assignment(struct expression *expr)
611f5207b7SJohn Levon {
62efe51d0cSJohn Levon static struct expression *ignored_expr;
631f5207b7SJohn Levon struct symbol *left_sym, *right_sym;
641f5207b7SJohn Levon char *left_name = NULL;
651f5207b7SJohn Levon char *right_name = NULL;
661f5207b7SJohn Levon
671f5207b7SJohn Levon if (expr->op != '=')
681f5207b7SJohn Levon return;
691f5207b7SJohn Levon if (is_fake_call(expr->right))
701f5207b7SJohn Levon return;
711f5207b7SJohn Levon if (__in_fake_struct_assign) {
721f5207b7SJohn Levon struct range_list *rl;
731f5207b7SJohn Levon
741f5207b7SJohn Levon if (!get_implied_rl(expr->right, &rl))
751f5207b7SJohn Levon return;
761f5207b7SJohn Levon if (is_whole_rl(rl))
771f5207b7SJohn Levon return;
781f5207b7SJohn Levon }
791f5207b7SJohn Levon
80efe51d0cSJohn Levon if (expr->left == ignored_expr)
81efe51d0cSJohn Levon return;
82efe51d0cSJohn Levon ignored_expr = NULL;
83efe51d0cSJohn Levon if (__in_fake_parameter_assign)
84efe51d0cSJohn Levon ignored_expr = expr->left;
85efe51d0cSJohn Levon
861f5207b7SJohn Levon left_name = expr_to_var_sym(expr->left, &left_sym);
871f5207b7SJohn Levon if (!left_name || !left_sym)
881f5207b7SJohn Levon goto free;
891f5207b7SJohn Levon set_state(my_id, left_name, left_sym, alloc_state_expr(strip_expr(expr->right)));
901f5207b7SJohn Levon
911f5207b7SJohn Levon right_name = expr_to_var_sym(expr->right, &right_sym);
921f5207b7SJohn Levon if (!right_name || !right_sym)
931f5207b7SJohn Levon goto free;
941f5207b7SJohn Levon
951f5207b7SJohn Levon store_link(link_id, right_name, right_sym, left_name, left_sym);
961f5207b7SJohn Levon
971f5207b7SJohn Levon free:
981f5207b7SJohn Levon free_string(left_name);
991f5207b7SJohn Levon free_string(right_name);
1001f5207b7SJohn Levon }
1011f5207b7SJohn Levon
record_param_assignment(struct expression * expr,int param,char * key,char * value)1021f5207b7SJohn Levon static void record_param_assignment(struct expression *expr, int param, char *key, char *value)
1031f5207b7SJohn Levon {
1041f5207b7SJohn Levon struct expression *arg, *right;
1051f5207b7SJohn Levon struct symbol *sym;
1061f5207b7SJohn Levon char *name;
1071f5207b7SJohn Levon char *p;
1081f5207b7SJohn Levon int right_param;
1091f5207b7SJohn Levon
1101f5207b7SJohn Levon while (expr->type == EXPR_ASSIGNMENT)
1111f5207b7SJohn Levon expr = strip_expr(expr->right);
1121f5207b7SJohn Levon if (!expr || expr->type != EXPR_CALL)
1131f5207b7SJohn Levon return;
1141f5207b7SJohn Levon
1151f5207b7SJohn Levon p = strstr(value, "[$");
1161f5207b7SJohn Levon if (!p)
1171f5207b7SJohn Levon return;
1181f5207b7SJohn Levon
1191f5207b7SJohn Levon p += 2;
1201f5207b7SJohn Levon right_param = strtol(p, &p, 10);
1211f5207b7SJohn Levon if (*p != ']')
1221f5207b7SJohn Levon return;
1231f5207b7SJohn Levon
1241f5207b7SJohn Levon arg = get_argument_from_call_expr(expr->args, param);
1251f5207b7SJohn Levon right = get_argument_from_call_expr(expr->args, right_param);
1261f5207b7SJohn Levon if (!right || !arg)
1271f5207b7SJohn Levon return;
1281f5207b7SJohn Levon name = get_variable_from_key(arg, key, &sym);
1291f5207b7SJohn Levon if (!name || !sym)
1301f5207b7SJohn Levon goto free;
1311f5207b7SJohn Levon
132efe51d0cSJohn Levon skip_mod = expr;
1331f5207b7SJohn Levon set_state(my_id, name, sym, alloc_state_expr(right));
1341f5207b7SJohn Levon free:
1351f5207b7SJohn Levon free_string(name);
1361f5207b7SJohn Levon }
1371f5207b7SJohn Levon
register_assigned_expr(int id)1381f5207b7SJohn Levon void register_assigned_expr(int id)
1391f5207b7SJohn Levon {
1401f5207b7SJohn Levon my_id = check_assigned_expr_id = id;
141efe51d0cSJohn Levon set_dynamic_states(check_assigned_expr_id);
1421f5207b7SJohn Levon add_hook(&match_assignment, ASSIGNMENT_HOOK_AFTER);
1431f5207b7SJohn Levon add_modification_hook(my_id, &undef);
1441f5207b7SJohn Levon select_return_states_hook(PARAM_SET, &record_param_assignment);
1451f5207b7SJohn Levon }
1461f5207b7SJohn Levon
register_assigned_expr_links(int id)1471f5207b7SJohn Levon void register_assigned_expr_links(int id)
1481f5207b7SJohn Levon {
1491f5207b7SJohn Levon link_id = id;
150efe51d0cSJohn Levon set_dynamic_states(link_id);
1511f5207b7SJohn Levon db_ignore_states(link_id);
1521f5207b7SJohn Levon set_up_link_functions(my_id, link_id);
1531f5207b7SJohn Levon }
1541f5207b7SJohn Levon
155