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"
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
);
25 static void match_cur_slist(const char *fn
, struct expression
*expr
, void *info
)
30 static void match_print_value(const char *fn
, struct expression
*expr
, void *info
)
32 struct state_list
*slist
;
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
);
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
);
50 static void match_print_implied(const char *fn
, struct expression
*expr
, void *info
)
52 struct expression
*arg
;
53 struct range_list
*rl
= NULL
;
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
));
64 static void match_print_implied_min(const char *fn
, struct expression
*expr
, void *info
)
66 struct expression
*arg
;
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
));
76 sm_msg("implied min: %s = <unknown>", name
);
81 static void match_print_implied_max(const char *fn
, struct expression
*expr
, void *info
)
83 struct expression
*arg
;
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
));
93 sm_msg("implied max: %s = <unknown>", name
);
98 static void match_print_absolute_min(const char *fn
, struct expression
*expr
, void *info
)
100 struct expression
*arg
;
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
));
110 sm_msg("absolute min: %s = <unknown>", name
);
115 static void match_print_absolute_max(const char *fn
, struct expression
*expr
, void *info
)
117 struct expression
*arg
;
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
));
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
);
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
);
152 slist
= get_all_states(SMATCH_EXTRA
);
153 FOR_EACH_PTR(slist
, tmp
) {
154 if (!strcmp(tmp
->name
, arg_expr
->string
->data
))
156 } END_FOR_EACH_PTR(tmp
);
160 static void match_buf_size(const char *fn
, struct expression
*expr
, void *info
)
162 struct expression
*arg
;
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
);
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
);
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
))
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
);
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
) {
210 } END_FOR_EACH_PTR(tmp
);
214 static void match_debug_on(const char *fn
, struct expression
*expr
, void *info
)
219 static void match_debug_off(const char *fn
, struct expression
*expr
, void *info
)
224 void check_debug(int 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
);