type: simplify get_binop_type()
[smatch.git] / check_debug.c
blob685e80146ea4275a2811d116371dfc7138038a1c
1 /*
2 * sparse/check_debug.c
4 * Copyright (C) 2009 Dan Carpenter.
6 * Licensed under the Open Software License version 1.1
8 */
10 #include "smatch.h"
11 #include "smatch_slist.h"
12 #include "smatch_extra.h"
14 static int my_id;
16 static void match_all_values(const char *fn, struct expression *expr, void *info)
18 struct state_list *slist;
20 slist = get_all_states(SMATCH_EXTRA);
21 __print_slist(slist);
22 free_slist(&slist);
25 static void match_cur_slist(const char *fn, struct expression *expr, void *info)
27 __print_cur_slist();
30 static void match_print_value(const char *fn, struct expression *expr, void *info)
32 struct state_list *slist;
33 struct sm_state *tmp;
34 struct expression *arg_expr;
36 arg_expr = get_argument_from_call_expr(expr->args, 0);
37 if (arg_expr->type != EXPR_STRING) {
38 sm_msg("error: the argument to %s is supposed to be a string literal", fn);
39 return;
42 slist = get_all_states(SMATCH_EXTRA);
43 FOR_EACH_PTR(slist, tmp) {
44 if (!strcmp(tmp->name, arg_expr->string->data))
45 sm_msg("%s = %s", tmp->name, tmp->state->name);
46 } END_FOR_EACH_PTR(tmp);
47 free_slist(&slist);
50 static void match_print_implied(const char *fn, struct expression *expr, void *info)
52 struct expression *arg;
53 struct range_list *rl = NULL;
54 char *name;
56 arg = get_argument_from_call_expr(expr->args, 0);
57 get_implied_range_list(arg, &rl);
59 name = get_variable_from_expr_complex(arg, NULL);
60 sm_msg("implied: %s = '%s'", name, show_ranges(rl));
61 free_string(name);
64 static void match_print_implied_min(const char *fn, struct expression *expr, void *info)
66 struct expression *arg;
67 long long val;
68 char *name;
70 arg = get_argument_from_call_expr(expr->args, 0);
71 if (!get_implied_min(arg, &val))
72 val = whole_range.min;
74 name = get_variable_from_expr_complex(arg, NULL);
75 sm_msg("implied min: %s = %lld", name, val);
76 free_string(name);
79 static void match_print_implied_max(const char *fn, struct expression *expr, void *info)
81 struct expression *arg;
82 long long val;
83 char *name;
85 arg = get_argument_from_call_expr(expr->args, 0);
86 if (!get_implied_max(arg, &val))
87 val = whole_range.max;
89 name = get_variable_from_expr_complex(arg, NULL);
90 sm_msg("implied max: %s = %lld", name, val);
91 free_string(name);
94 static void print_possible(struct sm_state *sm)
96 struct sm_state *tmp;
98 sm_msg("Possible values for %s", sm->name);
99 FOR_EACH_PTR(sm->possible, tmp) {
100 printf("%s\n", tmp->state->name);
101 } END_FOR_EACH_PTR(tmp);
102 sm_msg("===");
105 static void match_possible(const char *fn, struct expression *expr, void *info)
107 struct state_list *slist;
108 struct sm_state *tmp;
109 struct expression *arg_expr;
111 arg_expr = get_argument_from_call_expr(expr->args, 0);
112 if (arg_expr->type != EXPR_STRING) {
113 sm_msg("error: the argument to %s is supposed to be a string literal", fn);
114 return;
117 slist = get_all_states(SMATCH_EXTRA);
118 FOR_EACH_PTR(slist, tmp) {
119 if (!strcmp(tmp->name, arg_expr->string->data))
120 print_possible(tmp);
121 } END_FOR_EACH_PTR(tmp);
122 free_slist(&slist);
125 static void match_buf_size(const char *fn, struct expression *expr, void *info)
127 struct expression *arg;
128 int elements, bytes;
129 char *name;
131 arg = get_argument_from_call_expr(expr->args, 0);
132 elements = get_array_size(arg);
133 bytes = get_array_size_bytes(arg);
135 name = get_variable_from_expr_complex(arg, NULL);
136 sm_msg("buf size: '%s' %d elements, %d bytes", name, elements, bytes);
137 free_string(name);
140 static void match_note(const char *fn, struct expression *expr, void *info)
142 struct expression *arg_expr;
144 arg_expr = get_argument_from_call_expr(expr->args, 0);
145 if (arg_expr->type != EXPR_STRING) {
146 sm_msg("error: the argument to %s is supposed to be a string literal", fn);
147 return;
149 sm_msg("%s", arg_expr->string->data);
152 static void print_related(struct sm_state *sm)
154 struct relation *rel;
156 if (!estate_related(sm->state))
157 return;
159 sm_prefix();
160 sm_printf("%s: ", sm->name);
161 FOR_EACH_PTR(estate_related(sm->state), rel) {
162 sm_printf("%s %s ", show_special(rel->op), rel->name);
163 } END_FOR_EACH_PTR(rel);
164 sm_printf("\n");
167 static void match_dump_related(const char *fn, struct expression *expr, void *info)
169 struct state_list *slist;
170 struct sm_state *tmp;
172 slist = get_all_states(SMATCH_EXTRA);
173 FOR_EACH_PTR(slist, tmp) {
174 print_related(tmp);
175 } END_FOR_EACH_PTR(tmp);
176 free_slist(&slist);
179 static void match_debug_on(const char *fn, struct expression *expr, void *info)
181 option_debug = 1;
184 static void match_debug_off(const char *fn, struct expression *expr, void *info)
186 option_debug = 0;
188 void check_debug(int id)
190 my_id = id;
191 add_function_hook("__smatch_all_values", &match_all_values, NULL);
192 add_function_hook("__smatch_value", &match_print_value, NULL);
193 add_function_hook("__smatch_implied", &match_print_implied, NULL);
194 add_function_hook("__smatch_implied_min", &match_print_implied_min, NULL);
195 add_function_hook("__smatch_implied_max", &match_print_implied_max, NULL);
196 add_function_hook("__smatch_possible", &match_possible, NULL);
197 add_function_hook("__smatch_cur_slist", &match_cur_slist, NULL);
198 add_function_hook("__smatch_buf_size", &match_buf_size, NULL);
199 add_function_hook("__smatch_note", &match_note, NULL);
200 add_function_hook("__smatch_dump_related", &match_dump_related, NULL);
201 add_function_hook("__smatch_debug_on", &match_debug_on, NULL);
202 add_function_hook("__smatch_debug_off", &match_debug_off, NULL);