4 * Copyright (C) 2009 Dan Carpenter.
6 * Licensed under the Open Software License version 1.1
11 #include "smatch_slist.h"
12 #include "smatch_extra.h"
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
);
26 static void match_cur_slist(const char *fn
, struct expression
*expr
, void *info
)
31 static void match_print_value(const char *fn
, struct expression
*expr
, void *info
)
33 struct state_list
*slist
;
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
);
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
);
51 static void match_print_implied(const char *fn
, struct expression
*expr
, void *info
)
53 struct expression
*arg
;
54 struct range_list
*rl
= NULL
;
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
));
65 static void match_print_implied_min(const char *fn
, struct expression
*expr
, void *info
)
67 struct expression
*arg
;
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
));
77 sm_msg("implied min: %s = <unknown>", name
);
82 static void match_print_implied_max(const char *fn
, struct expression
*expr
, void *info
)
84 struct expression
*arg
;
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
));
94 sm_msg("implied max: %s = <unknown>", name
);
99 static void match_print_hard_max(const char *fn
, struct expression
*expr
, void *info
)
101 struct expression
*arg
;
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
));
111 sm_msg("hard max: %s = <unknown>", name
);
116 static void match_print_fuzzy_max(const char *fn
, struct expression
*expr
, void *info
)
118 struct expression
*arg
;
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
));
128 sm_msg("fuzzy max: %s = <unknown>", name
);
133 static void match_print_absolute_min(const char *fn
, struct expression
*expr
, void *info
)
135 struct expression
*arg
;
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
));
145 sm_msg("absolute min: %s = <unknown>", name
);
150 static void match_print_absolute_max(const char *fn
, struct expression
*expr
, void *info
)
152 struct expression
*arg
;
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
));
164 static void match_sval_info(const char *fn
, struct expression
*expr
, void *info
)
166 struct expression
*arg
;
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
);
178 sm_msg("implied: %s %c%d ->value = %llx", name
, sval_unsigned(sval
) ? 'u' : 's', sval_bits(sval
), sval
.value
);
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
);
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
);
206 slist
= get_all_states(SMATCH_EXTRA
);
207 FOR_EACH_PTR(slist
, tmp
) {
208 if (!strcmp(tmp
->name
, arg_expr
->string
->data
))
210 } END_FOR_EACH_PTR(tmp
);
214 static void match_buf_size(const char *fn
, struct expression
*expr
, void *info
)
216 struct expression
*arg
;
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
);
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
);
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
))
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
);
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
) {
264 } END_FOR_EACH_PTR(tmp
);
268 static void match_compare(const char *fn
, struct expression
*expr
, void *info
)
270 struct expression
*one
, *two
;
271 char *one_name
, *two_name
;
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
);
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
)
298 static void match_debug_off(const char *fn
, struct expression
*expr
, void *info
)
303 static void match_local_debug_on(const char *fn
, struct expression
*expr
, void *info
)
308 static void match_local_debug_off(const char *fn
, struct expression
*expr
, void *info
)
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
)
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
);