extra: handle "if (a - 3 > 100) "
[smatch.git] / check_debug.c
blob588ce38176388f986ebf1ce7e91a260b046a2dc1
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 int local_debug;
15 static int my_id;
17 static void match_all_values(const char *fn, struct expression *expr, void *info)
19 struct state_list *slist;
21 slist = get_all_states(SMATCH_EXTRA);
22 __print_slist(slist);
23 free_slist(&slist);
26 static void match_cur_slist(const char *fn, struct expression *expr, void *info)
28 __print_cur_slist();
31 static void match_print_value(const char *fn, struct expression *expr, void *info)
33 struct state_list *slist;
34 struct sm_state *tmp;
35 struct expression *arg_expr;
37 arg_expr = get_argument_from_call_expr(expr->args, 0);
38 if (arg_expr->type != EXPR_STRING) {
39 sm_msg("error: the argument to %s is supposed to be a string literal", fn);
40 return;
43 slist = get_all_states(SMATCH_EXTRA);
44 FOR_EACH_PTR(slist, tmp) {
45 if (!strcmp(tmp->name, arg_expr->string->data))
46 sm_msg("%s = %s", tmp->name, tmp->state->name);
47 } END_FOR_EACH_PTR(tmp);
48 free_slist(&slist);
51 static void match_print_implied(const char *fn, struct expression *expr, void *info)
53 struct expression *arg;
54 struct range_list *rl = NULL;
55 char *name;
57 arg = get_argument_from_call_expr(expr->args, 0);
58 get_implied_rl(arg, &rl);
60 name = expr_to_str(arg);
61 sm_msg("implied: %s = '%s'", name, show_rl(rl));
62 free_string(name);
65 static void match_print_implied_min(const char *fn, struct expression *expr, void *info)
67 struct expression *arg;
68 sval_t sval;
69 char *name;
71 arg = get_argument_from_call_expr(expr->args, 0);
72 name = expr_to_str(arg);
74 if (get_implied_min(arg, &sval))
75 sm_msg("implied min: %s = %s", name, sval_to_str(sval));
76 else
77 sm_msg("implied min: %s = <unknown>", name);
79 free_string(name);
82 static void match_print_implied_max(const char *fn, struct expression *expr, void *info)
84 struct expression *arg;
85 sval_t sval;
86 char *name;
88 arg = get_argument_from_call_expr(expr->args, 0);
89 name = expr_to_str(arg);
91 if (get_implied_max(arg, &sval))
92 sm_msg("implied max: %s = %s", name, sval_to_str(sval));
93 else
94 sm_msg("implied max: %s = <unknown>", name);
96 free_string(name);
99 static void match_print_hard_max(const char *fn, struct expression *expr, void *info)
101 struct expression *arg;
102 sval_t sval;
103 char *name;
105 arg = get_argument_from_call_expr(expr->args, 0);
106 name = expr_to_str(arg);
108 if (get_hard_max(arg, &sval))
109 sm_msg("hard max: %s = %s", name, sval_to_str(sval));
110 else
111 sm_msg("hard max: %s = <unknown>", name);
113 free_string(name);
116 static void match_print_fuzzy_max(const char *fn, struct expression *expr, void *info)
118 struct expression *arg;
119 sval_t sval;
120 char *name;
122 arg = get_argument_from_call_expr(expr->args, 0);
123 name = expr_to_str(arg);
125 if (get_fuzzy_max(arg, &sval))
126 sm_msg("fuzzy max: %s = %s", name, sval_to_str(sval));
127 else
128 sm_msg("fuzzy max: %s = <unknown>", name);
130 free_string(name);
133 static void match_print_absolute_min(const char *fn, struct expression *expr, void *info)
135 struct expression *arg;
136 sval_t sval;
137 char *name;
139 arg = get_argument_from_call_expr(expr->args, 0);
140 name = expr_to_str(arg);
142 if (get_absolute_min(arg, &sval))
143 sm_msg("absolute min: %s = %s", name, sval_to_str(sval));
144 else
145 sm_msg("absolute min: %s = <unknown>", name);
147 free_string(name);
150 static void match_print_absolute_max(const char *fn, struct expression *expr, void *info)
152 struct expression *arg;
153 sval_t sval;
154 char *name;
156 arg = get_argument_from_call_expr(expr->args, 0);
157 get_absolute_max(arg, &sval);
159 name = expr_to_str(arg);
160 sm_msg("absolute max: %s = %s", name, sval_to_str(sval));
161 free_string(name);
164 static void match_sval_info(const char *fn, struct expression *expr, void *info)
166 struct expression *arg;
167 sval_t sval;
168 char *name;
170 arg = get_argument_from_call_expr(expr->args, 0);
171 name = expr_to_str(arg);
173 if (!get_implied_value(arg, &sval)) {
174 sm_msg("no sval for '%s'", name);
175 goto free;
178 sm_msg("implied: %s %c%d ->value = %llx", name, sval_unsigned(sval) ? 'u' : 's', sval_bits(sval), sval.value);
179 free:
180 free_string(name);
183 static void print_possible(struct sm_state *sm)
185 struct sm_state *tmp;
187 sm_msg("Possible values for %s", sm->name);
188 FOR_EACH_PTR(sm->possible, tmp) {
189 printf("%s\n", tmp->state->name);
190 } END_FOR_EACH_PTR(tmp);
191 sm_msg("===");
194 static void match_possible(const char *fn, struct expression *expr, void *info)
196 struct state_list *slist;
197 struct sm_state *tmp;
198 struct expression *arg_expr;
200 arg_expr = get_argument_from_call_expr(expr->args, 0);
201 if (arg_expr->type != EXPR_STRING) {
202 sm_msg("error: the argument to %s is supposed to be a string literal", fn);
203 return;
206 slist = get_all_states(SMATCH_EXTRA);
207 FOR_EACH_PTR(slist, tmp) {
208 if (!strcmp(tmp->name, arg_expr->string->data))
209 print_possible(tmp);
210 } END_FOR_EACH_PTR(tmp);
211 free_slist(&slist);
214 static void match_buf_size(const char *fn, struct expression *expr, void *info)
216 struct expression *arg;
217 int elements, bytes;
218 char *name;
220 arg = get_argument_from_call_expr(expr->args, 0);
221 elements = get_array_size(arg);
222 bytes = get_array_size_bytes(arg);
224 name = expr_to_str(arg);
225 sm_msg("buf size: '%s' %d elements, %d bytes", name, elements, bytes);
226 free_string(name);
229 static void match_note(const char *fn, struct expression *expr, void *info)
231 struct expression *arg_expr;
233 arg_expr = get_argument_from_call_expr(expr->args, 0);
234 if (arg_expr->type != EXPR_STRING) {
235 sm_msg("error: the argument to %s is supposed to be a string literal", fn);
236 return;
238 sm_msg("%s", arg_expr->string->data);
241 static void print_related(struct sm_state *sm)
243 struct relation *rel;
245 if (!estate_related(sm->state))
246 return;
248 sm_prefix();
249 sm_printf("%s: ", sm->name);
250 FOR_EACH_PTR(estate_related(sm->state), rel) {
251 sm_printf("%s ", rel->name);
252 } END_FOR_EACH_PTR(rel);
253 sm_printf("\n");
256 static void match_dump_related(const char *fn, struct expression *expr, void *info)
258 struct state_list *slist;
259 struct sm_state *tmp;
261 slist = get_all_states(SMATCH_EXTRA);
262 FOR_EACH_PTR(slist, tmp) {
263 print_related(tmp);
264 } END_FOR_EACH_PTR(tmp);
265 free_slist(&slist);
268 static void match_compare(const char *fn, struct expression *expr, void *info)
270 struct expression *one, *two;
271 char *one_name, *two_name;
272 int comparison;
273 const char *str;
275 one = get_argument_from_call_expr(expr->args, 0);
276 two = get_argument_from_call_expr(expr->args, 1);
278 comparison = get_comparison(one, two);
279 if (!comparison)
280 str = "<nothing>";
281 else
282 str = show_special(comparison);
284 one_name = expr_to_var(one);
285 two_name = expr_to_var(two);
287 sm_msg("%s %s %s", one_name, str, two_name);
289 free_string(one_name);
290 free_string(two_name);
293 static void match_debug_on(const char *fn, struct expression *expr, void *info)
295 option_debug = 1;
298 static void match_debug_off(const char *fn, struct expression *expr, void *info)
300 option_debug = 0;
303 static void match_local_debug_on(const char *fn, struct expression *expr, void *info)
305 local_debug = 1;
308 static void match_local_debug_off(const char *fn, struct expression *expr, void *info)
310 local_debug = 0;
313 static void match_debug_implied_on(const char *fn, struct expression *expr, void *info)
315 option_debug_implied = 1;
318 static void match_debug_implied_off(const char *fn, struct expression *expr, void *info)
320 option_debug_implied = 0;
323 void check_debug(int id)
325 my_id = id;
326 add_function_hook("__smatch_all_values", &match_all_values, NULL);
327 add_function_hook("__smatch_value", &match_print_value, NULL);
328 add_function_hook("__smatch_implied", &match_print_implied, NULL);
329 add_function_hook("__smatch_implied_min", &match_print_implied_min, NULL);
330 add_function_hook("__smatch_implied_max", &match_print_implied_max, NULL);
331 add_function_hook("__smatch_hard_max", &match_print_hard_max, NULL);
332 add_function_hook("__smatch_fuzzy_max", &match_print_fuzzy_max, NULL);
333 add_function_hook("__smatch_absolute_min", &match_print_absolute_min, NULL);
334 add_function_hook("__smatch_absolute_max", &match_print_absolute_max, NULL);
335 add_function_hook("__smatch_sval_info", &match_sval_info, NULL);
336 add_function_hook("__smatch_possible", &match_possible, NULL);
337 add_function_hook("__smatch_cur_slist", &match_cur_slist, NULL);
338 add_function_hook("__smatch_buf_size", &match_buf_size, NULL);
339 add_function_hook("__smatch_note", &match_note, NULL);
340 add_function_hook("__smatch_dump_related", &match_dump_related, NULL);
341 add_function_hook("__smatch_compare", &match_compare, NULL);
342 add_function_hook("__smatch_debug_on", &match_debug_on, NULL);
343 add_function_hook("__smatch_debug_off", &match_debug_off, NULL);
344 add_function_hook("__smatch_local_debug_on", &match_local_debug_on, NULL);
345 add_function_hook("__smatch_local_debug_off", &match_local_debug_off, NULL);
346 add_function_hook("__smatch_debug_implied_on", &match_debug_implied_on, NULL);
347 add_function_hook("__smatch_debug_implied_off", &match_debug_implied_off, NULL);