From 9960f1d777c4d453f2da3239e4eb7b48ca238220 Mon Sep 17 00:00:00 2001 From: Dan Carpenter Date: Thu, 5 Mar 2009 08:20:19 +0300 Subject: [PATCH] Add a --debug-implied option Signed-off-by: Dan Carpenter --- smatch.c | 21 ++++++++++++++++--- smatch.h | 2 ++ smatch_implied.c | 55 +++++++++++++++++++++++++++++++++++-------------- smatch_scripts/kchecker | 3 +++ smatch_slist.c | 3 +++ 5 files changed, 65 insertions(+), 19 deletions(-) diff --git a/smatch.c b/smatch.c index 25df631e..2813523e 100644 --- a/smatch.c +++ b/smatch.c @@ -37,6 +37,13 @@ const reg_func reg_funcs[] = { NULL }; +void help() +{ + printf("Usage: smatch [--debug][--debug-implied][sparse arguments]" + " file.c\n"); + exit(1); +} + int main(int argc, char **argv) { int i; @@ -48,12 +55,20 @@ int main(int argc, char **argv) func(i + 1); } - if (argc >= 2 && !strcmp(argv[1], "--debug")) { - debug_states = 1; + while(argc >= 2) { + if (!strcmp(argv[1], "--debug")) { + debug_states = 1; + } else if (!strcmp(argv[1], "--debug-implied")) { + debug_implied_states = 1; + } else if (!strcmp(argv[1], "--help")) { + help(); + } else { + break; + } argc--; argv++; } - + smatch(argc, argv); return 0; } diff --git a/smatch.h b/smatch.h index 8027c858..82077a30 100644 --- a/smatch.h +++ b/smatch.h @@ -75,6 +75,7 @@ do { \ } while (0) #define SM_DEBUG(msg...) do { if (debug_states) printf(msg); } while (0) +#define DIMPLIED(msg...) do { if (debug_implied_states) printf(msg); } while (0) #define UNDEFINED INT_MIN @@ -148,6 +149,7 @@ void __split_statements(struct statement *stmt); void __split_whole_condition(struct expression *expr); /* smatch_implied.c */ +extern int debug_implied_states; void __implied_states_hook(struct expression *expr); /* smatch_extras.c */ diff --git a/smatch_implied.c b/smatch_implied.c index 7df9669d..5a0fc4c8 100644 --- a/smatch_implied.c +++ b/smatch_implied.c @@ -51,6 +51,8 @@ #define EQUALS 0 #define NOTEQUALS 1 +int debug_implied_states = 0; + /* * What are the implications if (foo == num) ... */ @@ -66,11 +68,15 @@ static struct state_list_stack *get_eq_neq(struct sm_state *sm_state, s = get_state_slist(list, sm_state->name, sm_state->owner, sm_state->sym); if (s == &undefined) { - free_stack(&ret); + free_stack(&ret); + DIMPLIED("%d '%s' is undefined\n", get_lineno(), + sm_state->name); return NULL; } if (s->data && ((eq_neq == EQUALS && *(int *)s->data == num) || (eq_neq == NOTEQUALS && *(int *)s->data != num))) { + DIMPLIED("added pool where %s is %s\n", sm_state->name, + show_state(s)); push_slist(&ret, list); } } END_FOR_EACH_PTR(list); @@ -84,10 +90,16 @@ static struct state_list *filter_stack(struct state_list_stack *stack) int i = 0; FOR_EACH_PTR(stack, tmp) { - if (!i++) + if (!i++) { ret = clone_states_in_pool(tmp, __get_cur_slist()); - else + if (debug_implied_states) { + printf("The first implied pool is:\n"); + __print_slist(ret); + } + } else { filter(&ret, tmp, __get_cur_slist()); + DIMPLIED("filtered\n"); + } } END_FOR_EACH_PTR(tmp); return ret; } @@ -111,34 +123,45 @@ void __implied_states_hook(struct expression *expr) free_string(name); if (!state) return; - if (!state->my_pools) + if (!state->my_pools) { + DIMPLIED("%d '%s' has no pools.\n", get_lineno(), state->name); return; + } + if (debug_implied_states) { + printf("%s has the following possible states:\n", state->name); + __print_slist(state->possible); + } + + DIMPLIED("Gettin the implied states for (%s != 0)\n", state->name); true_pools = get_eq_neq(state, NOTEQUALS, 0); - false_pools = get_eq_neq(state, EQUALS, 0); + DIMPLIED("There are %s implied pools for (%s != 0).\n", (true_pools?"some":"no"), state->name); implied_true = filter_stack(true_pools); - implied_false = filter_stack(false_pools); - if (debug_states) { - printf("Setting the following implied states for the true path.\n"); + if (implied_true && (debug_states || debug_implied_states)) { + printf("Setting the following implied states for (%s != 0).\n", + state->name); __print_slist(implied_true); } + DIMPLIED("Gettin the implied states for (%s == 0)\n", state->name); + false_pools = get_eq_neq(state, EQUALS, 0); + DIMPLIED("There are %s implied pools for (%s == 0).\n", (true_pools?"some":"no"), state->name); + implied_false = filter_stack(false_pools); + if (implied_false && (debug_states || debug_implied_states)) { + printf("Setting the following implied states for (%s == 0).\n", + state->name); + __print_slist(implied_false); + } FOR_EACH_PTR(implied_true, state) { __set_true_false_sm(state, NULL); } END_FOR_EACH_PTR(state); - - if (debug_states) { - printf("Setting the following implied states for the false path.\n"); - __print_slist(implied_false); - } + free_stack(&true_pools); + free_slist(&implied_true); FOR_EACH_PTR(implied_false, state) { __set_true_false_sm(NULL, state); } END_FOR_EACH_PTR(state); - - free_stack(&true_pools); free_stack(&false_pools); - free_slist(&implied_true); free_slist(&implied_false); } diff --git a/smatch_scripts/kchecker b/smatch_scripts/kchecker index 4d741304..86f531bf 100755 --- a/smatch_scripts/kchecker +++ b/smatch_scripts/kchecker @@ -9,6 +9,9 @@ while true ; do if [[ "$1" == "--debug" ]] ; then POST="$POST --debug" shift + elif [[ "$1" == "--debug-implied" ]] ; then + POST="$POST --debug-implied" + shift elif [[ "$1" == "--valgrind" ]] ; then PRE="valgrind" shift diff --git a/smatch_slist.c b/smatch_slist.c index 8b95da18..4cbcdf9c 100644 --- a/smatch_slist.c +++ b/smatch_slist.c @@ -679,11 +679,14 @@ void filter(struct state_list **slist, struct state_list *filter, if (!s_one || !s_two) break; if (cmp_tracker(s_one, s_two) < 0) { + DIMPLIED("removed %s\n", s_one->name); NEXT_PTR_LIST(s_one); } else if (cmp_tracker(s_one, s_two) == 0) { tmp = merge_implied(s_one, s_two, filter, cur_slist); if (tmp) add_ptr_list(&results, tmp); + else + DIMPLIED("removed %s\n", s_one->name); NEXT_PTR_LIST(s_one); NEXT_PTR_LIST(s_two); } else { -- 2.11.4.GIT