4 * Copyright (C) 2006 Dan Carpenter.
6 * Licensed under the Open Software License version 1.1
19 #include "expression.h"
25 #define STATE(_x) static struct smatch_state _x = { .name = #_x }
26 extern struct smatch_state undefined
;
27 extern struct smatch_state merged
;
28 extern struct smatch_state true_state
;
29 extern struct smatch_state false_state
;
30 DECLARE_ALLOCATOR(smatch_state
);
32 static inline void *INT_PTR(int i
)
34 return (void *)(long)i
;
37 static inline int PTR_INT(void *p
)
44 unsigned int merged
:1;
45 unsigned int implied
:1;
48 struct smatch_state
*state
;
50 struct state_list
*my_pool
;
51 struct sm_state
*left
;
52 struct sm_state
*right
;
53 unsigned int nr_children
;
54 struct state_list
*possible
;
62 DECLARE_ALLOCATOR(tracker
);
63 DECLARE_PTR_LIST(tracker_list
, struct tracker
);
90 void add_hook(void *func
, enum hook_type type
);
91 typedef struct smatch_state
*(merge_func_t
)(const char *name
,
93 struct smatch_state
*s1
,
94 struct smatch_state
*s2
);
95 typedef struct smatch_state
*(unmatched_func_t
)(struct sm_state
*state
);
96 void add_merge_hook(int client_id
, merge_func_t
*func
);
97 void add_unmatched_state_hook(int client_id
, unmatched_func_t
*func
);
98 typedef void (scope_hook
)(void *data
);
99 void add_scope_hook(scope_hook
*hook
, void *data
);
100 typedef void (func_hook
)(const char *fn
, struct expression
*expr
, void *data
);
101 typedef void (implication_hook
)(const char *fn
, struct expression
*call_expr
,
102 struct expression
*assign_expr
, void *data
);
103 void add_function_hook(const char *look_for
, func_hook
*call_back
, void *data
);
105 void add_function_assign_hook(const char *look_for
, func_hook
*call_back
,
107 void return_implies_state(const char *look_for
, long long start
, long long end
,
108 implication_hook
*call_back
, void *info
);
109 typedef void (modification_hook
)(const char *name
, struct symbol
*sym
,
110 struct expression
*expr
, void *data
);
111 void add_modification_hook(int owner
, const char *variable
, modification_hook
*hook
,
113 void add_modification_hook_expr(int owner
, struct expression
*expr
, modification_hook
*call_back
, void *info
);
114 void set_default_modification_hook(int owner
, modification_hook
*call_back
);
115 void __use_default_modification_hook(int owner
, const char *variable
);
117 const char *get_filename(void);
118 char *get_function(void);
119 int get_lineno(void);
120 int get_func_pos(void);
121 extern int final_pass
;
123 #define sm_printf(msg...) do { if (final_pass) printf(msg); } while (0) \
125 static inline void sm_prefix(void)
127 printf("%s +%d %s(%d) ", get_filename(), get_lineno(), get_function(), get_func_pos());
130 #define sm_msg(msg...) \
132 if (!option_debug && !final_pass) \
139 #define sm_debug(msg...) do { if (option_debug) printf(msg); } while (0)
141 #define sm_info(msg...) do { \
142 if (option_debug || (option_info && final_pass)) { \
150 #define POINTER_MAX 0xffffffff
152 struct smatch_state
*get_state(int owner
, const char *name
, struct symbol
*sym
);
153 struct smatch_state
*get_state_expr(int owner
, struct expression
*expr
);
154 struct state_list
*get_possible_states(int owner
, const char *name
,
156 struct state_list
*get_possible_states_expr(int owner
, struct expression
*expr
);
157 struct sm_state
*set_state(int owner
, const char *name
, struct symbol
*sym
,
158 struct smatch_state
*state
);
159 struct sm_state
*set_state_expr(int owner
, struct expression
*expr
,
160 struct smatch_state
*state
);
161 void delete_state(int owner
, const char *name
, struct symbol
*sym
);
162 void delete_state_expr(int owner
, struct expression
*expr
);
163 void set_true_false_states(int owner
, const char *name
, struct symbol
*sym
,
164 struct smatch_state
*true_state
,
165 struct smatch_state
*false_state
);
166 void set_true_false_states_expr(int owner
, struct expression
*expr
,
167 struct smatch_state
*true_state
,
168 struct smatch_state
*false_state
);
170 struct state_list
*get_all_states(int id
);
171 int is_reachable(void);
173 /* smatch_helper.c */
174 char *alloc_string(const char *str
);
175 void free_string(char *str
);
176 struct smatch_state
*alloc_state_num(int num
);
177 struct expression
*get_argument_from_call_expr(struct expression_list
*args
,
179 char *get_variable_from_expr_complex(struct expression
*expr
,
180 struct symbol
**sym_ptr
);
181 char *get_variable_from_expr(struct expression
*expr
,
182 struct symbol
**sym_ptr
);
183 int sym_name_is(const char *name
, struct expression
*expr
);
184 int get_value(struct expression
*expr
, long long *val
);
185 int get_implied_value(struct expression
*expr
, long long *val
);
186 int get_implied_max(struct expression
*expr
, long long *val
);
187 int get_implied_min(struct expression
*expr
, long long *val
);
188 int get_fuzzy_min(struct expression
*expr
, long long *min
);
189 int get_fuzzy_max(struct expression
*expr
, long long *max
);
190 int get_absolute_min(struct expression
*expr
, long long *val
);
191 int get_absolute_max(struct expression
*expr
, long long *val
);
192 int is_zero(struct expression
*expr
);
193 int is_array(struct expression
*expr
);
194 struct expression
*get_array_name(struct expression
*expr
);
195 struct expression
*get_array_offset(struct expression
*expr
);
196 const char *show_state(struct smatch_state
*state
);
197 struct statement
*get_expression_statement(struct expression
*expr
);
198 struct expression
*strip_parens(struct expression
*expr
);
199 struct expression
*strip_expr(struct expression
*expr
);
200 void scoped_state(int my_id
, const char *name
, struct symbol
*sym
);
201 int is_error_return(struct expression
*expr
);
202 int getting_address(void);
205 struct symbol
*get_type(struct expression
*expr
);
206 int type_unsigned(struct symbol
*base_type
);
207 long long type_max(struct symbol
*base_type
);
208 long long type_min(struct symbol
*base_type
);
210 /* smatch_ignore.c */
211 void add_ignore(int owner
, const char *name
, struct symbol
*sym
);
212 int is_ignored(int owner
, const char *name
, struct symbol
*sym
);
215 struct tracker
*alloc_tracker(int owner
, const char *name
, struct symbol
*sym
);
216 void add_tracker(struct tracker_list
**list
, int owner
, const char *name
,
218 void add_tracker_expr(struct tracker_list
**list
, int owner
, struct expression
*expr
);
219 void del_tracker(struct tracker_list
**list
, int owner
, const char *name
,
221 int in_tracker_list(struct tracker_list
*list
, int owner
, const char *name
,
223 struct tracker_list
*clone_tracker_list(struct tracker_list
*orig_list
);
224 void free_tracker_list(struct tracker_list
**list
);
225 void free_trackers_and_list(struct tracker_list
**list
);
227 /* smatch_conditions */
228 int in_condition(void);
232 void smatch (int argc
, char **argv
);
233 int inside_loop(void);
234 int in_expression_statement(void);
235 struct statement
*get_next_stmt(void);
236 void __split_expr(struct expression
*expr
);
237 void __split_stmt(struct statement
*stmt
);
238 extern int option_assume_loops
;
239 extern int option_known_conditions
;
240 extern int option_two_passes
;
241 extern struct symbol
*cur_func_sym
;
242 extern struct expression_list
*big_expression_stack
;
243 extern struct statement_list
*big_statement_stack
;
244 extern int __in_pre_condition
;
245 extern int __bail_on_rest_of_function
;
247 /* smatch_conditions */
248 void __split_whole_condition(struct expression
*expr
);
249 void __handle_logic(struct expression
*expr
);
250 int __handle_condition_assigns(struct expression
*expr
);
251 int __handle_select_assigns(struct expression
*expr
);
253 /* smatch_implied.c */
254 extern int option_debug_implied
;
255 extern int option_no_implied
;
256 void get_implications(char *name
, struct symbol
*sym
, int comparison
, long long num
,
257 struct state_list
**true_states
,
258 struct state_list
**false_states
);
259 struct range_list_stack
;
260 struct state_list
*__implied_case_slist(struct expression
*switch_expr
,
261 struct expression
*case_expr
,
262 struct range_list_stack
**remaining_cases
,
263 struct state_list
**raw_slist
);
264 struct range_list
*__get_implied_values(struct expression
*switch_expr
);
266 /* smatch_extras.c */
267 #define SMATCH_EXTRA 1 /* this is my_id from smatch extra set in smatch.c */
273 extern struct data_range whole_range
;
275 int known_condition_true(struct expression
*expr
);
276 int known_condition_false(struct expression
*expr
);
277 int implied_condition_true(struct expression
*expr
);
278 int implied_condition_false(struct expression
*expr
);
280 /* smatch_states.c */
281 extern int option_debug
;
283 void __push_fake_cur_slist();
284 struct state_list
*__pop_fake_cur_slist();
286 int unreachable(void);
287 void __set_sm(struct sm_state
*sm
);
288 struct state_list
*__get_cur_slist(void);
289 void __set_true_false_sm(struct sm_state
*true_state
,
290 struct sm_state
*false_state
);
291 void nullify_path(void);
292 void __match_nullify_path_hook(const char *fn
, struct expression
*expr
,
294 void __unnullify_path(void);
295 int __path_is_null(void);
296 void clear_all_states(void);
298 struct sm_state
*get_sm_state(int owner
, const char *name
,
300 struct sm_state
*get_sm_state_expr(int owner
, struct expression
*expr
);
301 void __push_true_states(void);
302 void __use_false_states(void);
303 void __discard_false_states(void);
304 void __merge_false_states(void);
305 void __merge_true_states(void);
307 void __negate_cond_stacks(void);
308 void __use_pre_cond_states(void);
309 void __use_cond_true_states(void);
310 void __use_cond_false_states(void);
311 void __push_cond_stacks(void);
312 struct state_list
*__copy_cond_true_states(void);
313 struct state_list
*__copy_cond_false_states(void);
314 struct state_list
*__pop_cond_true_stack(void);
315 struct state_list
*__pop_cond_false_stack(void);
316 void __and_cond_states(void);
317 void __or_cond_states(void);
318 void __save_pre_cond_states(void);
319 void __discard_pre_cond_states(void);
320 void __use_cond_states(void);
322 void __warn_on_silly_pre_loops(void);
324 void __push_continues(void);
325 void __discard_continues(void);
326 void __process_continues(void);
327 void __merge_continues(void);
329 void __push_breaks(void);
330 void __process_breaks(void);
331 void __merge_breaks(void);
332 void __use_breaks(void);
334 void __save_switch_states(struct expression
*switch_expr
);
335 void __discard_switches(void);
336 void __merge_switches(struct expression
*switch_expr
, struct expression
*case_expr
);
337 void __push_default(void);
338 void __set_default(void);
339 int __pop_default(void);
341 void __push_conditions(void);
342 void __discard_conditions(void);
344 void __save_gotos(const char *name
);
345 void __merge_gotos(const char *name
);
347 void __print_cur_slist(void);
350 void __pass_to_client(void *data
, enum hook_type type
);
351 void __pass_to_client_no_data(enum hook_type type
);
352 void __pass_case_to_client(struct expression
*switch_expr
,
353 struct expression
*case_expr
);
354 int __has_merge_function(int client_id
);
355 struct smatch_state
*__client_merge_function(int owner
, const char *name
,
357 struct smatch_state
*s1
,
358 struct smatch_state
*s2
);
359 struct smatch_state
*__client_unmatched_state_function(struct sm_state
*sm
);
360 void __push_scope_hooks(void);
361 void __call_scope_hooks(void);
363 /* smatch_function_hooks.c */
364 void create_function_hook_hash(void);
365 void __match_initializer_call(struct symbol
*sym
);
368 struct token
*get_tokens_file(const char *filename
);
371 extern int option_oom_kb
;
372 int out_of_memory(void);
375 extern char *option_project_str
;
376 extern char *data_dir
;
377 extern int option_no_data
;
378 extern int option_spammy
;
379 extern int option_full_path
;
380 extern int option_param_mapper
;
381 extern int option_print_returns
;
382 extern int option_info
;
383 extern int option_call_tree
;
384 extern int num_checks
;
391 extern enum project_type option_project
;
392 const char *check_name(unsigned short id
);
394 /* check_locking.c */
395 void print_held_locks();
397 #endif /* !SMATCH_H_ */