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 * There are a number of ways that variables are modified:
21 * 2) increment/decrement
23 * 4) inside functions.
25 * For setting stuff inside a function then, of course, it's more accurate if
26 * you have the cross function database built. Otherwise we are super
27 * aggressive about marking things as modified and if you have "frob(foo);" then
28 * we assume "foo->bar" is modified.
34 #include "smatch_extra.h"
35 #include "smatch_slist.h"
43 static modification_hook
**hooks
;
44 static modification_hook
**indirect_hooks
; /* parent struct modified etc */
46 ALLOCATOR(modification_data
, "modification data");
49 static struct smatch_state
*alloc_my_state(struct expression
*expr
, struct smatch_state
*prev
)
51 struct smatch_state
*state
;
52 struct modification_data
*data
;
55 state
= __alloc_smatch_state(0);
56 expr
= strip_expr(expr
);
57 name
= expr_to_str(expr
);
58 state
->name
= alloc_sname(name
);
61 data
= __alloc_modification_data(0);
69 void add_modification_hook(int owner
, modification_hook
*call_back
)
71 hooks
[owner
] = call_back
;
74 void add_indirect_modification_hook(int owner
, modification_hook
*call_back
)
76 indirect_hooks
[owner
] = call_back
;
79 static int matches(char *name
, struct symbol
*sym
, struct sm_state
*sm
)
87 if (strncmp(sm
->name
, name
, len
) == 0) {
88 if (sm
->name
[len
] == '\0')
90 if (sm
->name
[len
] == '-' || sm
->name
[len
] == '.')
91 return match_indirect
;
93 if (sm
->name
[0] != '*')
95 if (strncmp(sm
->name
+ 1, name
, len
) == 0) {
96 if (sm
->name
[len
+ 1] == '\0')
97 return match_indirect
;
98 if (sm
->name
[len
+ 1] == '-' || sm
->name
[len
+ 1] == '.')
99 return match_indirect
;
104 static void call_modification_hooks_name_sym(char *name
, struct symbol
*sym
, struct expression
*mod_expr
)
108 struct smatch_state
*prev
;
111 prev
= get_state(my_id
, name
, sym
);
112 set_state(my_id
, name
, sym
, alloc_my_state(mod_expr
, prev
));
114 stree
= __get_cur_stree();
116 FOR_EACH_SM(stree
, sm
) {
117 if (sm
->owner
> num_checks
)
119 match
= matches(name
, sym
, sm
);
121 if (match
&& hooks
[sm
->owner
])
122 (hooks
[sm
->owner
])(sm
, mod_expr
);
124 if (match
== match_indirect
&& indirect_hooks
[sm
->owner
])
125 (indirect_hooks
[sm
->owner
])(sm
, mod_expr
);
126 } END_FOR_EACH_SM(sm
);
129 static void call_modification_hooks(struct expression
*expr
, struct expression
*mod_expr
)
134 name
= expr_to_known_chunk_sym(expr
, &sym
);
137 call_modification_hooks_name_sym(name
, sym
, mod_expr
);
142 static void db_param_add(struct expression
*expr
, int param
, char *key
, char *value
)
144 struct expression
*arg
;
148 while (expr
->type
== EXPR_ASSIGNMENT
)
149 expr
= strip_expr(expr
->right
);
150 if (expr
->type
!= EXPR_CALL
)
153 arg
= get_argument_from_call_expr(expr
->args
, param
);
157 name
= get_variable_from_key(arg
, key
, &sym
);
161 call_modification_hooks_name_sym(name
, sym
, expr
);
166 static void match_assign(struct expression
*expr
)
168 call_modification_hooks(expr
->left
, expr
);
171 static void unop_expr(struct expression
*expr
)
173 if (expr
->op
!= SPECIAL_DECREMENT
&& expr
->op
!= SPECIAL_INCREMENT
)
176 call_modification_hooks(expr
->unop
, expr
);
179 static void match_call(struct expression
*expr
)
181 struct expression
*arg
, *tmp
;
183 FOR_EACH_PTR(expr
->args
, arg
) {
184 tmp
= strip_expr(arg
);
185 if (tmp
->type
== EXPR_PREOP
&& tmp
->op
== '&')
186 call_modification_hooks(tmp
->unop
, expr
);
187 else if (option_no_db
)
188 call_modification_hooks(deref_expression(tmp
), expr
);
189 } END_FOR_EACH_PTR(arg
);
192 static void asm_expr(struct statement
*stmt
)
194 struct expression
*expr
;
197 FOR_EACH_PTR(stmt
->asm_outputs
, expr
) {
199 case 0: /* identifier */
200 case 1: /* constraint */
203 case 2: /* expression */
205 call_modification_hooks(expr
, NULL
);
208 } END_FOR_EACH_PTR(expr
);
211 static void scope_end(void *_sym
)
213 struct symbol
*sym
= _sym
;
214 struct expression
*expr
= symbol_expression(sym
);
216 call_modification_hooks(expr
, NULL
);
219 static void match_declaration(struct symbol
*sym
)
221 add_scope_hook(&scope_end
, sym
);
224 struct smatch_state
*get_modification_state(struct expression
*expr
)
226 return get_state_expr(my_id
, expr
);
229 void register_modification_hooks(int id
)
233 hooks
= malloc((num_checks
+ 1) * sizeof(*hooks
));
234 memset(hooks
, 0, (num_checks
+ 1) * sizeof(*hooks
));
235 indirect_hooks
= malloc((num_checks
+ 1) * sizeof(*hooks
));
236 memset(indirect_hooks
, 0, (num_checks
+ 1) * sizeof(*hooks
));
238 add_hook(&match_assign
, ASSIGNMENT_HOOK
);
239 add_hook(&unop_expr
, OP_HOOK
);
240 add_hook(&asm_expr
, ASM_HOOK
);
243 void register_modification_hooks_late(int id
)
245 add_hook(&match_call
, FUNCTION_CALL_HOOK
);
246 select_return_states_hook(PARAM_ADD
, &db_param_add
);
247 select_return_states_hook(PARAM_SET
, &db_param_add
);
248 add_hook(&match_declaration
, DECLARATION_HOOK
);