db: don't split by NULL vs non-NULL when there are only non-NULL values
[smatch.git] / check_assigned_expr.c
blob3470434117dc73d7ed56d791a84e9c1c97e52022
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 void undef(struct sm_state *sm, struct expression *mod_expr)
33 set_state(my_id, sm->name, sm->sym, &undefined);
36 struct expression *get_assigned_expr(struct expression *expr)
38 struct smatch_state *state;
40 state = get_state_expr(my_id, expr);
41 if (!state)
42 return NULL;
43 return (struct expression *)state->data;
46 struct expression *get_assigned_expr_name_sym(const char *name, struct symbol *sym)
48 struct smatch_state *state;
50 state = get_state(my_id, name, sym);
51 if (!state)
52 return NULL;
53 return (struct expression *)state->data;
56 static void match_assignment(struct expression *expr)
58 struct symbol *left_sym, *right_sym;
59 char *left_name = NULL;
60 char *right_name = NULL;
62 if (expr->op != '=')
63 return;
64 if (is_fake_call(expr->right) || __in_fake_assign)
65 return;
67 left_name = expr_to_var_sym(expr->left, &left_sym);
68 if (!left_name || !left_sym)
69 goto free;
70 set_state(my_id, left_name, left_sym, alloc_state_expr(strip_expr(expr->right)));
72 right_name = expr_to_var_sym(expr->right, &right_sym);
73 if (!right_name || !right_sym)
74 goto free;
76 store_link(link_id, right_name, right_sym, left_name, left_sym);
78 free:
79 free_string(left_name);
80 free_string(right_name);
83 static void record_param_assignment(struct expression *expr, int param, char *key, char *value)
85 struct expression *arg, *right;
86 struct symbol *sym;
87 char *name;
88 char *p;
89 int right_param;
91 while (expr->type == EXPR_ASSIGNMENT)
92 expr = strip_expr(expr->right);
93 if (!expr || expr->type != EXPR_CALL)
94 return;
96 p = strstr(value, "[$");
97 if (!p)
98 return;
100 p += 2;
101 right_param = strtol(p, &p, 10);
102 if (*p != ']')
103 return;
105 arg = get_argument_from_call_expr(expr->args, param);
106 right = get_argument_from_call_expr(expr->args, right_param);
107 if (!right || !arg)
108 return;
109 name = get_variable_from_key(arg, key, &sym);
110 if (!name || !sym)
111 goto free;
113 if (local_debug)
114 sm_msg("FAKE ASSIGN: %s = %s. sym = %p", name, expr_to_str(right), sym);
115 set_state(my_id, name, sym, alloc_state_expr(right));
116 free:
117 free_string(name);
120 void check_assigned_expr(int id)
122 my_id = check_assigned_expr_id = id;
123 add_hook(&match_assignment, ASSIGNMENT_HOOK_AFTER);
124 add_modification_hook(my_id, &undef);
125 select_return_states_hook(PARAM_SET, &record_param_assignment);
128 void check_assigned_expr_links(int id)
130 link_id = id;
131 set_up_link_functions(my_id, link_id);