debug: fix cut and paste bug in print
[smatch.git] / check_debug.c
blob04214a0ed482c481fcf39aeeb7d9a6a921c8735a
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 match_print_absolute_min(const char *fn, struct expression *expr, void *info)
96 struct expression *arg;
97 long long val;
98 char *name;
100 arg = get_argument_from_call_expr(expr->args, 0);
101 if (!get_absolute_min(arg, &val))
102 val = whole_range.min;
104 name = get_variable_from_expr_complex(arg, NULL);
105 sm_msg("absolute min: %s = %lld", name, val);
106 free_string(name);
109 static void match_print_absolute_max(const char *fn, struct expression *expr, void *info)
111 struct expression *arg;
112 long long val;
113 char *name;
115 arg = get_argument_from_call_expr(expr->args, 0);
116 if (!get_absolute_max(arg, &val))
117 val = whole_range.max;
119 name = get_variable_from_expr_complex(arg, NULL);
120 sm_msg("absolute max: %s = %lld", name, val);
121 free_string(name);
124 static void print_possible(struct sm_state *sm)
126 struct sm_state *tmp;
128 sm_msg("Possible values for %s", sm->name);
129 FOR_EACH_PTR(sm->possible, tmp) {
130 printf("%s\n", tmp->state->name);
131 } END_FOR_EACH_PTR(tmp);
132 sm_msg("===");
135 static void match_possible(const char *fn, struct expression *expr, void *info)
137 struct state_list *slist;
138 struct sm_state *tmp;
139 struct expression *arg_expr;
141 arg_expr = get_argument_from_call_expr(expr->args, 0);
142 if (arg_expr->type != EXPR_STRING) {
143 sm_msg("error: the argument to %s is supposed to be a string literal", fn);
144 return;
147 slist = get_all_states(SMATCH_EXTRA);
148 FOR_EACH_PTR(slist, tmp) {
149 if (!strcmp(tmp->name, arg_expr->string->data))
150 print_possible(tmp);
151 } END_FOR_EACH_PTR(tmp);
152 free_slist(&slist);
155 static void match_buf_size(const char *fn, struct expression *expr, void *info)
157 struct expression *arg;
158 int elements, bytes;
159 char *name;
161 arg = get_argument_from_call_expr(expr->args, 0);
162 elements = get_array_size(arg);
163 bytes = get_array_size_bytes(arg);
165 name = get_variable_from_expr_complex(arg, NULL);
166 sm_msg("buf size: '%s' %d elements, %d bytes", name, elements, bytes);
167 free_string(name);
170 static void match_note(const char *fn, struct expression *expr, void *info)
172 struct expression *arg_expr;
174 arg_expr = get_argument_from_call_expr(expr->args, 0);
175 if (arg_expr->type != EXPR_STRING) {
176 sm_msg("error: the argument to %s is supposed to be a string literal", fn);
177 return;
179 sm_msg("%s", arg_expr->string->data);
182 static void print_related(struct sm_state *sm)
184 struct relation *rel;
186 if (!estate_related(sm->state))
187 return;
189 sm_prefix();
190 sm_printf("%s: ", sm->name);
191 FOR_EACH_PTR(estate_related(sm->state), rel) {
192 sm_printf("%s %s ", show_special(rel->op), rel->name);
193 } END_FOR_EACH_PTR(rel);
194 sm_printf("\n");
197 static void match_dump_related(const char *fn, struct expression *expr, void *info)
199 struct state_list *slist;
200 struct sm_state *tmp;
202 slist = get_all_states(SMATCH_EXTRA);
203 FOR_EACH_PTR(slist, tmp) {
204 print_related(tmp);
205 } END_FOR_EACH_PTR(tmp);
206 free_slist(&slist);
209 static void match_debug_on(const char *fn, struct expression *expr, void *info)
211 option_debug = 1;
214 static void match_debug_off(const char *fn, struct expression *expr, void *info)
216 option_debug = 0;
219 void check_debug(int id)
221 my_id = id;
222 add_function_hook("__smatch_all_values", &match_all_values, NULL);
223 add_function_hook("__smatch_value", &match_print_value, NULL);
224 add_function_hook("__smatch_implied", &match_print_implied, NULL);
225 add_function_hook("__smatch_implied_min", &match_print_implied_min, NULL);
226 add_function_hook("__smatch_implied_max", &match_print_implied_max, NULL);
227 add_function_hook("__smatch_absolute_min", &match_print_absolute_min, NULL);
228 add_function_hook("__smatch_absolute_max", &match_print_absolute_max, NULL);
229 add_function_hook("__smatch_possible", &match_possible, NULL);
230 add_function_hook("__smatch_cur_slist", &match_cur_slist, NULL);
231 add_function_hook("__smatch_buf_size", &match_buf_size, NULL);
232 add_function_hook("__smatch_note", &match_note, NULL);
233 add_function_hook("__smatch_dump_related", &match_dump_related, NULL);
234 add_function_hook("__smatch_debug_on", &match_debug_on, NULL);
235 add_function_hook("__smatch_debug_off", &match_debug_off, NULL);