sval: debug: fix reversed if condition
[smatch.git] / check_debug.c
blob1278c6427f59782b57ad34cc5ef9f47e2f702a6e
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 sval_t sval;
68 char *name;
70 arg = get_argument_from_call_expr(expr->args, 0);
71 name = get_variable_from_expr_complex(arg, NULL);
73 if (get_implied_min_sval(arg, &sval))
74 sm_msg("implied min: %s = %s", name, sval_to_str(sval));
75 else
76 sm_msg("implied min: %s = <unknown>", name);
78 free_string(name);
81 static void match_print_implied_max(const char *fn, struct expression *expr, void *info)
83 struct expression *arg;
84 sval_t sval;
85 char *name;
87 arg = get_argument_from_call_expr(expr->args, 0);
88 name = get_variable_from_expr_complex(arg, NULL);
90 if (!get_implied_max_sval(arg, &sval))
91 sm_msg("implied max: %s = %s", name, sval_to_str(sval));
92 else
93 sm_msg("implied max: %s = <unknown>", name);
95 free_string(name);
98 static void match_print_absolute_min(const char *fn, struct expression *expr, void *info)
100 struct expression *arg;
101 sval_t sval;
102 char *name;
104 arg = get_argument_from_call_expr(expr->args, 0);
105 name = get_variable_from_expr_complex(arg, NULL);
107 if (get_absolute_min_sval(arg, &sval))
108 sm_msg("absolute min: %s = %s", name, sval_to_str(sval));
109 else
110 sm_msg("absolute min: %s = <unknown>", name);
112 free_string(name);
115 static void match_print_absolute_max(const char *fn, struct expression *expr, void *info)
117 struct expression *arg;
118 sval_t sval;
119 char *name;
121 arg = get_argument_from_call_expr(expr->args, 0);
122 get_absolute_max_sval(arg, &sval);
124 name = get_variable_from_expr_complex(arg, NULL);
125 sm_msg("absolute max: %s = %s", name, sval_to_str(sval));
126 free_string(name);
129 static void print_possible(struct sm_state *sm)
131 struct sm_state *tmp;
133 sm_msg("Possible values for %s", sm->name);
134 FOR_EACH_PTR(sm->possible, tmp) {
135 printf("%s\n", tmp->state->name);
136 } END_FOR_EACH_PTR(tmp);
137 sm_msg("===");
140 static void match_possible(const char *fn, struct expression *expr, void *info)
142 struct state_list *slist;
143 struct sm_state *tmp;
144 struct expression *arg_expr;
146 arg_expr = get_argument_from_call_expr(expr->args, 0);
147 if (arg_expr->type != EXPR_STRING) {
148 sm_msg("error: the argument to %s is supposed to be a string literal", fn);
149 return;
152 slist = get_all_states(SMATCH_EXTRA);
153 FOR_EACH_PTR(slist, tmp) {
154 if (!strcmp(tmp->name, arg_expr->string->data))
155 print_possible(tmp);
156 } END_FOR_EACH_PTR(tmp);
157 free_slist(&slist);
160 static void match_buf_size(const char *fn, struct expression *expr, void *info)
162 struct expression *arg;
163 int elements, bytes;
164 char *name;
166 arg = get_argument_from_call_expr(expr->args, 0);
167 elements = get_array_size(arg);
168 bytes = get_array_size_bytes(arg);
170 name = get_variable_from_expr_complex(arg, NULL);
171 sm_msg("buf size: '%s' %d elements, %d bytes", name, elements, bytes);
172 free_string(name);
175 static void match_note(const char *fn, struct expression *expr, void *info)
177 struct expression *arg_expr;
179 arg_expr = get_argument_from_call_expr(expr->args, 0);
180 if (arg_expr->type != EXPR_STRING) {
181 sm_msg("error: the argument to %s is supposed to be a string literal", fn);
182 return;
184 sm_msg("%s", arg_expr->string->data);
187 static void print_related(struct sm_state *sm)
189 struct relation *rel;
191 if (!estate_related(sm->state))
192 return;
194 sm_prefix();
195 sm_printf("%s: ", sm->name);
196 FOR_EACH_PTR(estate_related(sm->state), rel) {
197 sm_printf("%s %s ", show_special(rel->op), rel->name);
198 } END_FOR_EACH_PTR(rel);
199 sm_printf("\n");
202 static void match_dump_related(const char *fn, struct expression *expr, void *info)
204 struct state_list *slist;
205 struct sm_state *tmp;
207 slist = get_all_states(SMATCH_EXTRA);
208 FOR_EACH_PTR(slist, tmp) {
209 print_related(tmp);
210 } END_FOR_EACH_PTR(tmp);
211 free_slist(&slist);
214 static void match_debug_on(const char *fn, struct expression *expr, void *info)
216 option_debug = 1;
219 static void match_debug_off(const char *fn, struct expression *expr, void *info)
221 option_debug = 0;
224 void check_debug(int id)
226 my_id = id;
227 add_function_hook("__smatch_all_values", &match_all_values, NULL);
228 add_function_hook("__smatch_value", &match_print_value, NULL);
229 add_function_hook("__smatch_implied", &match_print_implied, NULL);
230 add_function_hook("__smatch_implied_min", &match_print_implied_min, NULL);
231 add_function_hook("__smatch_implied_max", &match_print_implied_max, NULL);
232 add_function_hook("__smatch_absolute_min", &match_print_absolute_min, NULL);
233 add_function_hook("__smatch_absolute_max", &match_print_absolute_max, NULL);
234 add_function_hook("__smatch_possible", &match_possible, NULL);
235 add_function_hook("__smatch_cur_slist", &match_cur_slist, NULL);
236 add_function_hook("__smatch_buf_size", &match_buf_size, NULL);
237 add_function_hook("__smatch_note", &match_note, NULL);
238 add_function_hook("__smatch_dump_related", &match_dump_related, NULL);
239 add_function_hook("__smatch_debug_on", &match_debug_on, NULL);
240 add_function_hook("__smatch_debug_off", &match_debug_off, NULL);