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_state(const char *fn
, struct expression
*expr
, void *info
)
33 struct expression
*check_arg
, *state_arg
;
37 check_arg
= get_argument_from_call_expr(expr
->args
, 0);
38 if (check_arg
->type
!= EXPR_STRING
) {
39 sm_msg("error: the check_name argument to %s is supposed to be a string literal", fn
);
42 state_arg
= get_argument_from_call_expr(expr
->args
, 1);
43 if (!state_arg
|| state_arg
->type
!= EXPR_STRING
) {
44 sm_msg("error: the state_name argument to %s is supposed to be a string literal", fn
);
48 FOR_EACH_PTR(__get_cur_slist(), sm
) {
49 if (strcmp(check_name(sm
->owner
), check_arg
->string
->data
) != 0)
51 if (strcmp(sm
->name
, state_arg
->string
->data
) != 0)
53 sm_msg("'%s' = '%s'", sm
->name
, sm
->state
->name
);
55 } END_FOR_EACH_PTR(sm
);
58 sm_msg("%s '%s' not found", check_arg
->string
->data
, state_arg
->string
->data
);
61 static void match_states(const char *fn
, struct expression
*expr
, void *info
)
63 struct expression
*check_arg
;
67 check_arg
= get_argument_from_call_expr(expr
->args
, 0);
68 if (check_arg
->type
!= EXPR_STRING
) {
69 sm_msg("error: the check_name argument to %s is supposed to be a string literal", fn
);
73 FOR_EACH_PTR(__get_cur_slist(), sm
) {
74 if (strcmp(check_name(sm
->owner
), check_arg
->string
->data
) != 0)
76 sm_msg("'%s' = '%s'", sm
->name
, sm
->state
->name
);
78 } END_FOR_EACH_PTR(sm
);
81 sm_msg("%s: no states", check_arg
->string
->data
);
84 static void match_print_value(const char *fn
, struct expression
*expr
, void *info
)
86 struct state_list
*slist
;
88 struct expression
*arg_expr
;
90 arg_expr
= get_argument_from_call_expr(expr
->args
, 0);
91 if (arg_expr
->type
!= EXPR_STRING
) {
92 sm_msg("error: the argument to %s is supposed to be a string literal", fn
);
96 slist
= get_all_states(SMATCH_EXTRA
);
97 FOR_EACH_PTR(slist
, tmp
) {
98 if (!strcmp(tmp
->name
, arg_expr
->string
->data
))
99 sm_msg("%s = %s", tmp
->name
, tmp
->state
->name
);
100 } END_FOR_EACH_PTR(tmp
);
104 static void match_print_implied(const char *fn
, struct expression
*expr
, void *info
)
106 struct expression
*arg
;
107 struct range_list
*rl
= NULL
;
110 arg
= get_argument_from_call_expr(expr
->args
, 0);
111 get_implied_rl(arg
, &rl
);
113 name
= expr_to_str(arg
);
114 sm_msg("implied: %s = '%s'", name
, show_rl(rl
));
118 static void match_print_implied_min(const char *fn
, struct expression
*expr
, void *info
)
120 struct expression
*arg
;
124 arg
= get_argument_from_call_expr(expr
->args
, 0);
125 name
= expr_to_str(arg
);
127 if (get_implied_min(arg
, &sval
))
128 sm_msg("implied min: %s = %s", name
, sval_to_str(sval
));
130 sm_msg("implied min: %s = <unknown>", name
);
135 static void match_print_implied_max(const char *fn
, struct expression
*expr
, void *info
)
137 struct expression
*arg
;
141 arg
= get_argument_from_call_expr(expr
->args
, 0);
142 name
= expr_to_str(arg
);
144 if (get_implied_max(arg
, &sval
))
145 sm_msg("implied max: %s = %s", name
, sval_to_str(sval
));
147 sm_msg("implied max: %s = <unknown>", name
);
152 static void match_print_hard_max(const char *fn
, struct expression
*expr
, void *info
)
154 struct expression
*arg
;
158 arg
= get_argument_from_call_expr(expr
->args
, 0);
159 name
= expr_to_str(arg
);
161 if (get_hard_max(arg
, &sval
))
162 sm_msg("hard max: %s = %s", name
, sval_to_str(sval
));
164 sm_msg("hard max: %s = <unknown>", name
);
169 static void match_print_fuzzy_max(const char *fn
, struct expression
*expr
, void *info
)
171 struct expression
*arg
;
175 arg
= get_argument_from_call_expr(expr
->args
, 0);
176 name
= expr_to_str(arg
);
178 if (get_fuzzy_max(arg
, &sval
))
179 sm_msg("fuzzy max: %s = %s", name
, sval_to_str(sval
));
181 sm_msg("fuzzy max: %s = <unknown>", name
);
186 static void match_print_absolute_min(const char *fn
, struct expression
*expr
, void *info
)
188 struct expression
*arg
;
192 arg
= get_argument_from_call_expr(expr
->args
, 0);
193 name
= expr_to_str(arg
);
195 if (get_absolute_min(arg
, &sval
))
196 sm_msg("absolute min: %s = %s", name
, sval_to_str(sval
));
198 sm_msg("absolute min: %s = <unknown>", name
);
203 static void match_print_absolute_max(const char *fn
, struct expression
*expr
, void *info
)
205 struct expression
*arg
;
209 arg
= get_argument_from_call_expr(expr
->args
, 0);
210 get_absolute_max(arg
, &sval
);
212 name
= expr_to_str(arg
);
213 sm_msg("absolute max: %s = %s", name
, sval_to_str(sval
));
217 static void match_sval_info(const char *fn
, struct expression
*expr
, void *info
)
219 struct expression
*arg
;
223 arg
= get_argument_from_call_expr(expr
->args
, 0);
224 name
= expr_to_str(arg
);
226 if (!get_implied_value(arg
, &sval
)) {
227 sm_msg("no sval for '%s'", name
);
231 sm_msg("implied: %s %c%d ->value = %llx", name
, sval_unsigned(sval
) ? 'u' : 's', sval_bits(sval
), sval
.value
);
236 static void match_member_name(const char *fn
, struct expression
*expr
, void *info
)
238 struct expression
*arg
;
239 char *name
, *member_name
;
241 arg
= get_argument_from_call_expr(expr
->args
, 0);
242 name
= expr_to_str(arg
);
243 member_name
= get_member_name(arg
);
244 sm_msg("member name: '%s => %s'", name
, member_name
);
245 free_string(member_name
);
249 static void print_possible(struct sm_state
*sm
)
251 struct sm_state
*tmp
;
253 sm_msg("Possible values for %s", sm
->name
);
254 FOR_EACH_PTR(sm
->possible
, tmp
) {
255 printf("%s\n", tmp
->state
->name
);
256 } END_FOR_EACH_PTR(tmp
);
260 static void match_possible(const char *fn
, struct expression
*expr
, void *info
)
262 struct state_list
*slist
;
263 struct sm_state
*tmp
;
264 struct expression
*arg_expr
;
266 arg_expr
= get_argument_from_call_expr(expr
->args
, 0);
267 if (arg_expr
->type
!= EXPR_STRING
) {
268 sm_msg("error: the argument to %s is supposed to be a string literal", fn
);
272 slist
= get_all_states(SMATCH_EXTRA
);
273 FOR_EACH_PTR(slist
, tmp
) {
274 if (!strcmp(tmp
->name
, arg_expr
->string
->data
))
276 } END_FOR_EACH_PTR(tmp
);
280 static void match_strlen(const char *fn
, struct expression
*expr
, void *info
)
282 struct expression
*arg
;
283 struct range_list
*rl
;
286 arg
= get_argument_from_call_expr(expr
->args
, 0);
287 get_implied_strlen(arg
, &rl
);
289 name
= expr_to_str(arg
);
290 sm_msg("strlen: '%s' %s characters", name
, show_rl(rl
));
294 static void match_buf_size(const char *fn
, struct expression
*expr
, void *info
)
296 struct expression
*arg
;
300 arg
= get_argument_from_call_expr(expr
->args
, 0);
301 elements
= get_array_size(arg
);
302 bytes
= get_array_size_bytes(arg
);
304 name
= expr_to_str(arg
);
305 sm_msg("buf size: '%s' %d elements, %d bytes", name
, elements
, bytes
);
309 static void match_buf_size_rl(const char *fn
, struct expression
*expr
, void *info
)
311 struct expression
*arg
;
312 struct range_list
*rl
;
316 arg
= get_argument_from_call_expr(expr
->args
, 0);
317 rl
= get_array_size_bytes_rl(arg
);
318 elements
= get_array_size(arg
);
319 bytes
= get_array_size_bytes(arg
);
321 name
= expr_to_str(arg
);
322 sm_msg("buf size: '%s' %s %d elements, %d bytes", name
, show_rl(rl
), elements
, bytes
);
326 static void match_note(const char *fn
, struct expression
*expr
, void *info
)
328 struct expression
*arg_expr
;
330 arg_expr
= get_argument_from_call_expr(expr
->args
, 0);
331 if (arg_expr
->type
!= EXPR_STRING
) {
332 sm_msg("error: the argument to %s is supposed to be a string literal", fn
);
335 sm_msg("%s", arg_expr
->string
->data
);
338 static void print_related(struct sm_state
*sm
)
340 struct relation
*rel
;
342 if (!estate_related(sm
->state
))
346 sm_printf("%s: ", sm
->name
);
347 FOR_EACH_PTR(estate_related(sm
->state
), rel
) {
348 sm_printf("%s ", rel
->name
);
349 } END_FOR_EACH_PTR(rel
);
353 static void match_dump_related(const char *fn
, struct expression
*expr
, void *info
)
355 struct state_list
*slist
;
356 struct sm_state
*tmp
;
358 slist
= get_all_states(SMATCH_EXTRA
);
359 FOR_EACH_PTR(slist
, tmp
) {
361 } END_FOR_EACH_PTR(tmp
);
365 static void match_compare(const char *fn
, struct expression
*expr
, void *info
)
367 struct expression
*one
, *two
;
368 char *one_name
, *two_name
;
372 one
= get_argument_from_call_expr(expr
->args
, 0);
373 two
= get_argument_from_call_expr(expr
->args
, 1);
375 comparison
= get_comparison(one
, two
);
377 snprintf(buf
, sizeof(buf
), "<none>");
379 snprintf(buf
, sizeof(buf
), "%s", show_special(comparison
));
381 one_name
= expr_to_str(one
);
382 two_name
= expr_to_str(two
);
384 sm_msg("%s %s %s", one_name
, buf
, two_name
);
386 free_string(one_name
);
387 free_string(two_name
);
390 static void match_debug_on(const char *fn
, struct expression
*expr
, void *info
)
395 static void match_debug_off(const char *fn
, struct expression
*expr
, void *info
)
400 static void match_local_debug_on(const char *fn
, struct expression
*expr
, void *info
)
405 static void match_local_debug_off(const char *fn
, struct expression
*expr
, void *info
)
410 static void match_debug_implied_on(const char *fn
, struct expression
*expr
, void *info
)
412 option_debug_implied
= 1;
415 static void match_debug_implied_off(const char *fn
, struct expression
*expr
, void *info
)
417 option_debug_implied
= 0;
420 static void match_about(const char *fn
, struct expression
*expr
, void *info
)
422 struct expression
*arg
;
426 sm_msg("---- about ----");
427 match_print_implied(fn
, expr
, NULL
);
428 match_buf_size(fn
, expr
, NULL
);
429 match_strlen(fn
, expr
, NULL
);
431 arg
= get_argument_from_call_expr(expr
->args
, 0);
432 name
= expr_to_str(arg
);
434 sm_msg("info: not a straight forward variable.");
438 FOR_EACH_PTR(__get_cur_slist(), sm
) {
439 if (strcmp(sm
->name
, name
) != 0)
441 sm_msg("%s '%s' = '%s'", check_name(sm
->owner
), sm
->name
, sm
->state
->name
);
442 } END_FOR_EACH_PTR(sm
);
445 void check_debug(int id
)
448 add_function_hook("__smatch_about", &match_about
, NULL
);
449 add_function_hook("__smatch_all_values", &match_all_values
, NULL
);
450 add_function_hook("__smatch_state", &match_state
, NULL
);
451 add_function_hook("__smatch_states", &match_states
, NULL
);
452 add_function_hook("__smatch_value", &match_print_value
, NULL
);
453 add_function_hook("__smatch_implied", &match_print_implied
, NULL
);
454 add_function_hook("__smatch_implied_min", &match_print_implied_min
, NULL
);
455 add_function_hook("__smatch_implied_max", &match_print_implied_max
, NULL
);
456 add_function_hook("__smatch_hard_max", &match_print_hard_max
, NULL
);
457 add_function_hook("__smatch_fuzzy_max", &match_print_fuzzy_max
, NULL
);
458 add_function_hook("__smatch_absolute_min", &match_print_absolute_min
, NULL
);
459 add_function_hook("__smatch_absolute_max", &match_print_absolute_max
, NULL
);
460 add_function_hook("__smatch_sval_info", &match_sval_info
, NULL
);
461 add_function_hook("__smatch_member_name", &match_member_name
, NULL
);
462 add_function_hook("__smatch_possible", &match_possible
, NULL
);
463 add_function_hook("__smatch_cur_slist", &match_cur_slist
, NULL
);
464 add_function_hook("__smatch_strlen", &match_strlen
, NULL
);
465 add_function_hook("__smatch_buf_size", &match_buf_size
, NULL
);
466 add_function_hook("__smatch_buf_size_rl", &match_buf_size_rl
, NULL
);
467 add_function_hook("__smatch_note", &match_note
, NULL
);
468 add_function_hook("__smatch_dump_related", &match_dump_related
, NULL
);
469 add_function_hook("__smatch_compare", &match_compare
, NULL
);
470 add_function_hook("__smatch_debug_on", &match_debug_on
, NULL
);
471 add_function_hook("__smatch_debug_off", &match_debug_off
, NULL
);
472 add_function_hook("__smatch_local_debug_on", &match_local_debug_on
, NULL
);
473 add_function_hook("__smatch_local_debug_off", &match_local_debug_off
, NULL
);
474 add_function_hook("__smatch_debug_implied_on", &match_debug_implied_on
, NULL
);
475 add_function_hook("__smatch_debug_implied_off", &match_debug_implied_off
, NULL
);