db: split success/fail returns as best we can
[smatch.git] / smatch_function_hooks.c
blob631c1f49b76ff2eea2d10c159a817ee26f7e6777
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 * There are several types of function hooks:
20 * add_function_hook() - For any time a function is called.
21 * add_function_assign_hook() - foo = the_function().
22 * add_implied_return_hook() - Calculates the implied return value.
23 * add_macro_assign_hook() - foo = the_macro().
24 * return_implies_state() - For when a return value of 1 implies locked
25 * and 0 implies unlocked. etc. etc.
29 #include <stdlib.h>
30 #include <stdio.h>
31 #include "smatch.h"
32 #include "smatch_slist.h"
33 #include "smatch_extra.h"
34 #include "smatch_function_hashtable.h"
36 struct fcall_back {
37 int type;
38 struct data_range *range;
39 union {
40 func_hook *call_back;
41 implication_hook *ranged;
42 implied_return_hook *implied_return;
43 } u;
44 void *info;
47 ALLOCATOR(fcall_back, "call backs");
48 DECLARE_PTR_LIST(call_back_list, struct fcall_back);
50 DEFINE_FUNCTION_HASHTABLE_STATIC(callback, struct fcall_back, struct call_back_list);
51 static struct hashtable *func_hash;
53 #define REGULAR_CALL 0
54 #define RANGED_CALL 1
55 #define ASSIGN_CALL 2
56 #define IMPLIED_RETURN 3
57 #define MACRO_ASSIGN 4
58 #define MACRO_ASSIGN_EXTRA 5
60 struct return_implies_callback {
61 int type;
62 return_implies_hook *callback;
64 ALLOCATOR(return_implies_callback, "return_implies callbacks");
65 DECLARE_PTR_LIST(db_implies_list, struct return_implies_callback);
66 static struct db_implies_list *db_return_states_list;
68 typedef void (void_fn)(void);
69 DECLARE_PTR_LIST(void_fn_list, void_fn *);
70 static struct void_fn_list *return_states_before;
71 static struct void_fn_list *return_states_after;
73 static struct fcall_back *alloc_fcall_back(int type, void *call_back,
74 void *info)
76 struct fcall_back *cb;
78 cb = __alloc_fcall_back(0);
79 cb->type = type;
80 cb->u.call_back = call_back;
81 cb->info = info;
82 return cb;
85 void add_function_hook(const char *look_for, func_hook *call_back, void *info)
87 struct fcall_back *cb;
89 cb = alloc_fcall_back(REGULAR_CALL, call_back, info);
90 add_callback(func_hash, look_for, cb);
93 void add_function_assign_hook(const char *look_for, func_hook *call_back,
94 void *info)
96 struct fcall_back *cb;
98 cb = alloc_fcall_back(ASSIGN_CALL, call_back, info);
99 add_callback(func_hash, look_for, cb);
102 void add_implied_return_hook(const char *look_for,
103 implied_return_hook *call_back,
104 void *info)
106 struct fcall_back *cb;
108 cb = alloc_fcall_back(IMPLIED_RETURN, call_back, info);
109 add_callback(func_hash, look_for, cb);
112 void add_macro_assign_hook(const char *look_for, func_hook *call_back,
113 void *info)
115 struct fcall_back *cb;
117 cb = alloc_fcall_back(MACRO_ASSIGN, call_back, info);
118 add_callback(func_hash, look_for, cb);
121 void add_macro_assign_hook_extra(const char *look_for, func_hook *call_back,
122 void *info)
124 struct fcall_back *cb;
126 cb = alloc_fcall_back(MACRO_ASSIGN_EXTRA, call_back, info);
127 add_callback(func_hash, look_for, cb);
130 void return_implies_state(const char *look_for, long long start, long long end,
131 implication_hook *call_back, void *info)
133 struct fcall_back *cb;
135 cb = alloc_fcall_back(RANGED_CALL, call_back, info);
136 cb->range = alloc_range_perm(ll_to_sval(start), ll_to_sval(end));
137 add_callback(func_hash, look_for, cb);
140 void select_return_states_hook(int type, return_implies_hook *callback)
142 struct return_implies_callback *cb = __alloc_return_implies_callback(0);
144 cb->type = type;
145 cb->callback = callback;
146 add_ptr_list(&db_return_states_list, cb);
149 void select_return_states_before(void_fn *fn)
151 void_fn **p = malloc(sizeof(void_fn *));
152 *p = fn;
153 add_ptr_list(&return_states_before, p);
156 void select_return_states_after(void_fn *fn)
158 void_fn **p = malloc(sizeof(void_fn *));
159 *p = fn;
160 add_ptr_list(&return_states_after, p);
163 static void call_return_states_before_hooks(void)
165 void_fn **fn;
167 FOR_EACH_PTR(return_states_before, fn) {
168 (*fn)();
169 } END_FOR_EACH_PTR(fn);
172 static void call_return_states_after_hooks(void)
174 void_fn **fn;
176 FOR_EACH_PTR(return_states_after, fn) {
177 (*fn)();
178 } END_FOR_EACH_PTR(fn);
181 static int call_call_backs(struct call_back_list *list, int type,
182 const char *fn, struct expression *expr)
184 struct fcall_back *tmp;
185 int handled = 0;
187 FOR_EACH_PTR(list, tmp) {
188 if (tmp->type == type) {
189 (tmp->u.call_back)(fn, expr, tmp->info);
190 handled = 1;
192 } END_FOR_EACH_PTR(tmp);
194 return handled;
197 static void call_ranged_call_backs(struct call_back_list *list,
198 const char *fn, struct expression *call_expr,
199 struct expression *assign_expr)
201 struct fcall_back *tmp;
203 FOR_EACH_PTR(list, tmp) {
204 (tmp->u.ranged)(fn, call_expr, assign_expr, tmp->info);
205 } END_FOR_EACH_PTR(tmp);
208 static struct call_back_list *get_same_ranged_call_backs(struct call_back_list *list,
209 struct data_range *drange)
211 struct call_back_list *ret = NULL;
212 struct fcall_back *tmp;
214 FOR_EACH_PTR(list, tmp) {
215 if (tmp->type != RANGED_CALL)
216 continue;
217 if (ranges_equiv(tmp->range, drange))
218 add_ptr_list(&ret, tmp);
219 } END_FOR_EACH_PTR(tmp);
220 return ret;
223 static int in_list_exact_sval(struct range_list *list, struct data_range *drange)
225 struct data_range *tmp;
227 FOR_EACH_PTR(list, tmp) {
228 if (ranges_equiv(tmp, drange))
229 return 1;
230 } END_FOR_EACH_PTR(tmp);
231 return 0;
234 static int assign_ranged_funcs(const char *fn, struct expression *expr,
235 struct call_back_list *call_backs)
237 struct fcall_back *tmp;
238 struct sm_state *sm;
239 char *var_name;
240 struct symbol *sym;
241 struct smatch_state *estate;
242 struct stree *tmp_stree;
243 struct stree *final_states = NULL;
244 struct range_list *handled_ranges = NULL;
245 struct call_back_list *same_range_call_backs = NULL;
246 int handled = 0;
248 if (!call_backs)
249 return 0;
251 var_name = expr_to_var_sym(expr->left, &sym);
252 if (!var_name || !sym)
253 goto free;
255 FOR_EACH_PTR(call_backs, tmp) {
256 if (tmp->type != RANGED_CALL)
257 continue;
259 if (in_list_exact_sval(handled_ranges, tmp->range))
260 continue;
261 __push_fake_cur_stree();
262 tack_on(&handled_ranges, tmp->range);
264 same_range_call_backs = get_same_ranged_call_backs(call_backs, tmp->range);
265 call_ranged_call_backs(same_range_call_backs, fn, expr->right, expr);
266 __free_ptr_list((struct ptr_list **)&same_range_call_backs);
268 estate = alloc_estate_range(tmp->range->min, tmp->range->max);
269 set_extra_mod(var_name, sym, estate);
271 tmp_stree = __pop_fake_cur_stree();
272 merge_fake_stree(&final_states, tmp_stree);
273 free_stree(&tmp_stree);
274 handled = 1;
275 } END_FOR_EACH_PTR(tmp);
277 FOR_EACH_SM(final_states, sm) {
278 __set_sm(sm);
279 } END_FOR_EACH_SM(sm);
281 free_stree(&final_states);
282 free:
283 free_string(var_name);
284 return handled;
287 static void call_implies_callbacks(int comparison, struct expression *expr, sval_t sval, int left, struct stree **implied_true, struct stree **implied_false)
289 struct call_back_list *call_backs;
290 struct fcall_back *tmp;
291 const char *fn;
292 struct data_range *value_range;
293 struct stree *true_states = NULL;
294 struct stree *false_states = NULL;
295 struct stree *tmp_stree;
297 *implied_true = NULL;
298 *implied_false = NULL;
299 if (expr->fn->type != EXPR_SYMBOL || !expr->fn->symbol)
300 return;
301 fn = expr->fn->symbol->ident->name;
302 call_backs = search_callback(func_hash, (char *)expr->fn->symbol->ident->name);
303 if (!call_backs)
304 return;
305 value_range = alloc_range(sval, sval);
307 /* set true states */
308 __push_fake_cur_stree();
309 FOR_EACH_PTR(call_backs, tmp) {
310 if (tmp->type != RANGED_CALL)
311 continue;
312 if (!true_comparison_range_LR(comparison, tmp->range, value_range, left))
313 continue;
314 (tmp->u.ranged)(fn, expr, NULL, tmp->info);
315 } END_FOR_EACH_PTR(tmp);
316 tmp_stree = __pop_fake_cur_stree();
317 merge_fake_stree(&true_states, tmp_stree);
318 free_stree(&tmp_stree);
320 /* set false states */
321 __push_fake_cur_stree();
322 FOR_EACH_PTR(call_backs, tmp) {
323 if (tmp->type != RANGED_CALL)
324 continue;
325 if (!false_comparison_range_LR(comparison, tmp->range, value_range, left))
326 continue;
327 (tmp->u.ranged)(fn, expr, NULL, tmp->info);
328 } END_FOR_EACH_PTR(tmp);
329 tmp_stree = __pop_fake_cur_stree();
330 merge_fake_stree(&false_states, tmp_stree);
331 free_stree(&tmp_stree);
333 *implied_true = true_states;
334 *implied_false = false_states;
337 struct db_callback_info {
338 int true_side;
339 int comparison;
340 struct expression *expr;
341 struct range_list *rl;
342 int left;
343 struct stree *stree;
344 struct db_implies_list *callbacks;
345 int prev_return_id;
346 int cull;
347 struct smatch_state *ret_state;
350 static void store_return_state(struct db_callback_info *db_info, struct smatch_state *state)
352 db_info->ret_state = state;
355 static void set_return_state(struct db_callback_info *db_info)
357 if (db_info->expr->type != EXPR_ASSIGNMENT) {
358 sm_msg("Smatch Internal Error: expected an assignment");
359 return;
361 if (!db_info->ret_state)
362 return;
364 set_extra_expr_mod(db_info->expr->left, db_info->ret_state);
365 db_info->ret_state = NULL;
368 static void handle_ret_equals_param(char *ret_string, struct range_list *rl, struct expression *call)
370 char *str;
371 long long param;
372 struct expression *arg;
373 struct range_list *orig;
375 str = strstr(ret_string, "==$");
376 if (!str)
377 return;
378 str += 3;
379 param = strtoll(str, NULL, 10);
380 arg = get_argument_from_call_expr(call->args, param);
381 if (!arg)
382 return;
383 get_absolute_rl(arg, &orig);
384 rl = rl_intersection(orig, rl);
385 if (!rl)
386 return;
387 set_extra_expr_nomod(arg, alloc_estate_rl(rl));
390 static int impossible_limit(struct expression *expr, int param, char *key, char *value)
392 struct expression *arg;
393 struct range_list *passed;
394 struct range_list *limit;
395 struct symbol *compare_type;
397 if (strcmp(key, "$") != 0)
398 return 0;
400 while (expr->type == EXPR_ASSIGNMENT)
401 expr = strip_expr(expr->right);
402 if (expr->type != EXPR_CALL)
403 return 0;
405 arg = get_argument_from_call_expr(expr->args, param);
406 if (!arg)
407 return 0;
408 if (!get_implied_rl(arg, &passed))
409 return 0;
410 if (!passed || is_whole_rl(passed))
411 return 0;
413 compare_type = get_arg_type(expr->fn, param);
414 call_results_to_rl(expr, compare_type, value, &limit);
415 if (!limit || is_whole_rl(limit))
416 return 0;
417 if (possibly_true_rl(passed, SPECIAL_EQUAL, limit))
418 return 0;
419 return 1;
422 static int db_compare_callback(void *_info, int argc, char **argv, char **azColName)
424 struct db_callback_info *db_info = _info;
425 struct range_list *ret_range;
426 int type, param;
427 char *key, *value;
428 struct return_implies_callback *tmp;
429 struct stree *stree;
430 int return_id;
431 int comparison;
433 if (argc != 6)
434 return 0;
436 return_id = atoi(argv[0]);
437 type = atoi(argv[2]);
438 param = atoi(argv[3]);
439 key = argv[4];
440 value = argv[5];
442 if (db_info->prev_return_id != -1 && return_id != db_info->prev_return_id) {
443 stree = __pop_fake_cur_stree();
444 if (!db_info->cull)
445 merge_fake_stree(&db_info->stree, stree);
446 free_stree(&stree);
447 __push_fake_cur_stree();
448 db_info->cull = 0;
450 db_info->prev_return_id = return_id;
452 if (type == CULL_PATH)
453 db_info->cull = 1;
454 if (type == LIMITED_VALUE && impossible_limit(db_info->expr, param, key, value))
455 db_info->cull = 1;
456 if (db_info->cull)
457 return 0;
459 call_results_to_rl(db_info->expr, get_type(strip_expr(db_info->expr)), argv[1], &ret_range);
460 ret_range = cast_rl(get_type(db_info->expr), ret_range);
461 if (!ret_range)
462 ret_range = alloc_whole_rl(get_type(db_info->expr));
464 comparison = db_info->comparison;
465 if (!db_info->left)
466 comparison = flip_comparison(comparison);
468 if (db_info->true_side) {
469 if (!possibly_true_rl(ret_range, comparison, db_info->rl))
470 return 0;
471 filter_by_comparison(&ret_range, comparison, db_info->rl);
472 } else {
473 if (!possibly_false_rl(ret_range, comparison, db_info->rl))
474 return 0;
475 filter_by_comparison(&ret_range, negate_comparison(comparison), db_info->rl);
478 handle_ret_equals_param(argv[1], ret_range, db_info->expr);
480 FOR_EACH_PTR(db_info->callbacks, tmp) {
481 if (tmp->type == type)
482 tmp->callback(db_info->expr, param, key, value);
483 } END_FOR_EACH_PTR(tmp);
484 return 0;
487 static void compare_db_return_states_callbacks(int comparison, struct expression *expr, sval_t sval, int left, struct stree *implied_true, struct stree *implied_false)
489 struct stree *stree;
490 struct stree *true_states;
491 struct stree *false_states;
492 struct sm_state *sm;
493 struct db_callback_info db_info = {};
495 db_info.comparison = comparison;
496 db_info.expr = expr;
497 db_info.rl = alloc_rl(sval, sval);
498 db_info.left = left;
499 db_info.callbacks = db_return_states_list;
501 call_return_states_before_hooks();
503 db_info.true_side = 1;
504 db_info.stree = NULL;
505 db_info.prev_return_id = -1;
506 __push_fake_cur_stree();
507 sql_select_return_states("return_id, return, type, parameter, key, value", expr,
508 db_compare_callback, &db_info);
509 stree = __pop_fake_cur_stree();
510 if (!db_info.cull)
511 merge_fake_stree(&db_info.stree, stree);
512 free_stree(&stree);
513 true_states = db_info.stree;
515 db_info.true_side = 0;
516 db_info.stree = NULL;
517 db_info.prev_return_id = -1;
518 db_info.cull = 0;
519 __push_fake_cur_stree();
520 sql_select_return_states("return_id, return, type, parameter, key, value", expr,
521 db_compare_callback, &db_info);
522 stree = __pop_fake_cur_stree();
523 if (!db_info.cull)
524 merge_fake_stree(&db_info.stree, stree);
525 free_stree(&stree);
526 false_states = db_info.stree;
528 FOR_EACH_SM(true_states, sm) {
529 __set_true_false_sm(sm, NULL);
530 } END_FOR_EACH_SM(sm);
531 FOR_EACH_SM(false_states, sm) {
532 __set_true_false_sm(NULL, sm);
533 } END_FOR_EACH_SM(sm);
535 free_stree(&true_states);
536 free_stree(&false_states);
538 call_return_states_after_hooks();
540 FOR_EACH_SM(implied_true, sm) {
541 __set_true_false_sm(sm, NULL);
542 } END_FOR_EACH_SM(sm);
543 FOR_EACH_SM(implied_false, sm) {
544 __set_true_false_sm(NULL, sm);
545 } END_FOR_EACH_SM(sm);
549 void function_comparison(int comparison, struct expression *expr, sval_t sval, int left)
551 struct stree *implied_true, *implied_false;
553 call_implies_callbacks(comparison, expr, sval, left, &implied_true, &implied_false);
554 compare_db_return_states_callbacks(comparison, expr, sval, left, implied_true, implied_false);
555 free_stree(&implied_true);
556 free_stree(&implied_false);
559 static int db_assign_return_states_callback(void *_info, int argc, char **argv, char **azColName)
561 struct db_callback_info *db_info = _info;
562 struct range_list *ret_range;
563 int type, param;
564 char *key, *value;
565 struct return_implies_callback *tmp;
566 struct stree *stree;
567 int return_id;
569 if (argc != 6)
570 return 0;
572 return_id = atoi(argv[0]);
573 type = atoi(argv[2]);
574 param = atoi(argv[3]);
575 key = argv[4];
576 value = argv[5];
578 if (db_info->prev_return_id != -1 && return_id != db_info->prev_return_id) {
579 set_return_state(db_info);
580 stree = __pop_fake_cur_stree();
581 if (!db_info->cull)
582 merge_fake_stree(&db_info->stree, stree);
583 free_stree(&stree);
584 __push_fake_cur_stree();
585 db_info->cull = 0;
587 db_info->prev_return_id = return_id;
589 if (type == CULL_PATH)
590 db_info->cull = 1;
591 if (type == LIMITED_VALUE && impossible_limit(db_info->expr, param, key, value))
592 db_info->cull = 1;
593 if (db_info->cull)
594 return 0;
596 call_results_to_rl(db_info->expr->right, get_type(strip_expr(db_info->expr->right)), argv[1], &ret_range);
597 __add_comparison_info(db_info->expr->left, strip_expr(db_info->expr->right), argv[1]);
598 if (!ret_range)
599 ret_range = alloc_whole_rl(get_type(strip_expr(db_info->expr->right)));
600 ret_range = cast_rl(get_type(db_info->expr->right), ret_range);
602 FOR_EACH_PTR(db_return_states_list, tmp) {
603 if (tmp->type == type)
604 tmp->callback(db_info->expr, param, key, value);
605 } END_FOR_EACH_PTR(tmp);
606 ret_range = cast_rl(get_type(db_info->expr->left), ret_range);
607 store_return_state(db_info, alloc_estate_rl(ret_range));
609 return 0;
612 static int db_return_states_assign(struct expression *expr)
614 struct expression *right;
615 struct sm_state *sm;
616 struct stree *stree;
617 int handled = 0;
618 struct db_callback_info db_info = {};
620 right = strip_expr(expr->right);
622 db_info.prev_return_id = -1;
623 db_info.expr = expr;
624 db_info.stree = NULL;
626 call_return_states_before_hooks();
628 __push_fake_cur_stree();
629 sql_select_return_states("return_id, return, type, parameter, key, value",
630 right, db_assign_return_states_callback, &db_info);
631 set_return_state(&db_info);
632 stree = __pop_fake_cur_stree();
633 if (!db_info.cull)
634 merge_fake_stree(&db_info.stree, stree);
635 free_stree(&stree);
637 FOR_EACH_SM(db_info.stree, sm) {
638 __set_sm(sm);
639 handled = 1;
640 } END_FOR_EACH_SM(sm);
642 free_stree(&db_info.stree);
643 call_return_states_after_hooks();
645 return handled;
648 static int handle_implied_return(struct expression *expr)
650 struct range_list *rl;
652 if (!get_implied_return(expr->right, &rl))
653 return 0;
654 rl = cast_rl(get_type(expr->left), rl);
655 set_extra_expr_mod(expr->left, alloc_estate_rl(rl));
656 return 1;
659 static void match_assign_call(struct expression *expr)
661 struct call_back_list *call_backs;
662 const char *fn;
663 struct expression *right;
664 int handled = 0;
665 struct range_list *rl;
667 if (expr->op != '=')
668 return;
670 right = strip_expr(expr->right);
671 if (right->fn->type != EXPR_SYMBOL || !right->fn->symbol) {
672 handled |= db_return_states_assign(expr);
673 if (!handled)
674 goto assigned_unknown;
675 return;
677 if (is_fake_call(right)) {
678 if (!get_state_expr(SMATCH_EXTRA, expr->left))
679 return;
680 set_extra_expr_mod(expr->left, alloc_estate_whole(get_type(expr->left)));
681 return;
684 fn = right->fn->symbol->ident->name;
687 * some of these conflict (they try to set smatch extra twice), so we
688 * call them in order from least important to most important.
691 call_backs = search_callback(func_hash, (char *)fn);
692 call_call_backs(call_backs, ASSIGN_CALL, fn, expr);
694 handled |= db_return_states_assign(expr);
695 handled |= assign_ranged_funcs(fn, expr, call_backs);
696 handled |= handle_implied_return(expr);
698 if (handled)
699 return;
701 assigned_unknown:
702 get_absolute_rl(expr->right, &rl);
703 rl = cast_rl(get_type(expr->left), rl);
704 set_extra_expr_mod(expr->left, alloc_estate_rl(rl));
707 static int db_return_states_callback(void *_info, int argc, char **argv, char **azColName)
709 struct db_callback_info *db_info = _info;
710 struct range_list *ret_range;
711 int type, param;
712 char *key, *value;
713 struct return_implies_callback *tmp;
714 struct stree *stree;
715 int return_id;
716 char buf[64];
718 if (argc != 6)
719 return 0;
721 return_id = atoi(argv[0]);
722 type = atoi(argv[2]);
723 param = atoi(argv[3]);
724 key = argv[4];
725 value = argv[5];
727 if (db_info->prev_return_id != -1 && return_id != db_info->prev_return_id) {
728 stree = __pop_fake_cur_stree();
729 if (!db_info->cull)
730 merge_fake_stree(&db_info->stree, stree);
731 free_stree(&stree);
732 __push_fake_cur_stree();
733 __unnullify_path();
734 db_info->cull = 0;
736 db_info->prev_return_id = return_id;
738 if (type == CULL_PATH)
739 db_info->cull = 1;
740 if (type == LIMITED_VALUE && impossible_limit(db_info->expr, param, key, value))
741 db_info->cull = 1;
742 if (db_info->cull)
743 return 0;
745 call_results_to_rl(db_info->expr, get_type(strip_expr(db_info->expr)), argv[1], &ret_range);
746 ret_range = cast_rl(get_type(db_info->expr), ret_range);
748 FOR_EACH_PTR(db_return_states_list, tmp) {
749 if (tmp->type == type)
750 tmp->callback(db_info->expr, param, key, value);
751 } END_FOR_EACH_PTR(tmp);
754 * We want to store the return values so that we can split the strees
755 * in smatch_db.c. This uses set_state() directly because it's not a
756 * real smatch_extra state.
758 snprintf(buf, sizeof(buf), "return %p", db_info->expr);
759 set_state(SMATCH_EXTRA, buf, NULL, alloc_estate_rl(ret_range));
761 return 0;
764 static void db_return_states(struct expression *expr)
766 struct sm_state *sm;
767 struct stree *stree;
768 struct db_callback_info db_info = {};
770 if (!__get_cur_stree()) /* no return functions */
771 return;
773 db_info.prev_return_id = -1;
774 db_info.expr = expr;
775 db_info.stree = NULL;
777 call_return_states_before_hooks();
779 __push_fake_cur_stree();
780 __unnullify_path();
781 sql_select_return_states("return_id, return, type, parameter, key, value",
782 expr, db_return_states_callback, &db_info);
783 stree = __pop_fake_cur_stree();
784 if (!db_info.cull)
785 merge_fake_stree(&db_info.stree, stree);
786 free_stree(&stree);
788 FOR_EACH_SM(db_info.stree, sm) {
789 __set_sm(sm);
790 } END_FOR_EACH_SM(sm);
792 free_stree(&db_info.stree);
793 call_return_states_after_hooks();
796 static void db_return_states_call(struct expression *expr)
798 if (is_assigned_call(expr))
799 return;
800 db_return_states(expr);
803 static void match_function_call(struct expression *expr)
805 struct call_back_list *call_backs;
807 if (expr->fn->type == EXPR_SYMBOL && expr->fn->symbol) {
808 call_backs = search_callback(func_hash, (char *)expr->fn->symbol->ident->name);
809 if (call_backs)
810 call_call_backs(call_backs, REGULAR_CALL,
811 expr->fn->symbol->ident->name, expr);
813 db_return_states_call(expr);
816 static void match_macro_assign(struct expression *expr)
818 struct call_back_list *call_backs;
819 const char *macro;
820 struct expression *right;
822 right = strip_expr(expr->right);
823 macro = get_macro_name(right->pos);
824 call_backs = search_callback(func_hash, (char *)macro);
825 if (!call_backs)
826 return;
827 call_call_backs(call_backs, MACRO_ASSIGN, macro, expr);
828 call_call_backs(call_backs, MACRO_ASSIGN_EXTRA, macro, expr);
831 int get_implied_return(struct expression *expr, struct range_list **rl)
833 struct call_back_list *call_backs;
834 struct fcall_back *tmp;
835 int handled = 0;
836 char *fn;
838 *rl = NULL;
840 expr = strip_expr(expr);
841 fn = expr_to_var(expr->fn);
842 if (!fn)
843 goto out;
845 call_backs = search_callback(func_hash, fn);
847 FOR_EACH_PTR(call_backs, tmp) {
848 if (tmp->type == IMPLIED_RETURN) {
849 (tmp->u.implied_return)(expr, tmp->info, rl);
850 handled = 1;
852 } END_FOR_EACH_PTR(tmp);
854 out:
855 free_string(fn);
856 return handled;
859 void create_function_hook_hash(void)
861 func_hash = create_function_hashtable(5000);
864 void register_function_hooks(int id)
866 add_hook(&match_function_call, CALL_HOOK_AFTER_INLINE);
867 add_hook(&match_assign_call, CALL_ASSIGNMENT_HOOK);
868 add_hook(&match_macro_assign, MACRO_ASSIGNMENT_HOOK);