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
);
91 void add_hook(void *func
, enum hook_type type
);
92 typedef struct smatch_state
*(merge_func_t
)(const char *name
,
94 struct smatch_state
*s1
,
95 struct smatch_state
*s2
);
96 typedef struct smatch_state
*(unmatched_func_t
)(struct sm_state
*state
);
97 void add_merge_hook(int client_id
, merge_func_t
*func
);
98 void add_unmatched_state_hook(int client_id
, unmatched_func_t
*func
);
99 typedef void (scope_hook
)(void *data
);
100 void add_scope_hook(scope_hook
*hook
, void *data
);
101 typedef void (func_hook
)(const char *fn
, struct expression
*expr
, void *data
);
102 typedef void (implication_hook
)(const char *fn
, struct expression
*call_expr
,
103 struct expression
*assign_expr
, void *data
);
104 void add_function_hook(const char *look_for
, func_hook
*call_back
, void *data
);
106 void add_function_assign_hook(const char *look_for
, func_hook
*call_back
,
108 void return_implies_state(const char *look_for
, long long start
, long long end
,
109 implication_hook
*call_back
, void *info
);
110 typedef void (modification_hook
)(const char *name
, struct symbol
*sym
,
111 struct expression
*expr
, void *data
);
112 void add_modification_hook(int owner
, const char *variable
, modification_hook
*hook
,
114 void add_modification_hook_expr(int owner
, struct expression
*expr
, modification_hook
*call_back
, void *info
);
115 void set_default_modification_hook(int owner
, modification_hook
*call_back
);
116 void __use_default_modification_hook(int owner
, const char *variable
);
118 const char *get_filename(void);
119 char *get_function(void);
120 int get_lineno(void);
121 int get_func_pos(void);
122 extern int final_pass
;
123 extern struct symbol
*cur_func_sym
;
124 extern int option_debug
;
126 #define sm_printf(msg...) do { if (final_pass) printf(msg); } while (0) \
128 static inline void sm_prefix(void)
130 printf("%s +%d %s(%d) ", get_filename(), get_lineno(), get_function(), get_func_pos());
134 static inline void print_implied_debug_msg();
136 #define sm_msg(msg...) \
138 print_implied_debug_msg(); \
139 if (!option_debug && !final_pass) \
146 extern char *implied_debug_msg
;
147 static inline void print_implied_debug_msg()
149 static struct symbol
*last_printed
= NULL
;
151 if (!implied_debug_msg
)
153 if (last_printed
== cur_func_sym
)
155 last_printed
= cur_func_sym
;
156 sm_msg("%s", implied_debug_msg
);
159 #define sm_debug(msg...) do { if (option_debug) printf(msg); } while (0)
161 #define sm_info(msg...) do { \
162 if (option_debug || (option_info && final_pass)) { \
170 #define POINTER_MAX 0xffffffff
172 struct smatch_state
*get_state(int owner
, const char *name
, struct symbol
*sym
);
173 struct smatch_state
*get_state_expr(int owner
, struct expression
*expr
);
174 struct state_list
*get_possible_states(int owner
, const char *name
,
176 struct state_list
*get_possible_states_expr(int owner
, struct expression
*expr
);
177 struct sm_state
*set_state(int owner
, const char *name
, struct symbol
*sym
,
178 struct smatch_state
*state
);
179 struct sm_state
*set_state_expr(int owner
, struct expression
*expr
,
180 struct smatch_state
*state
);
181 void delete_state(int owner
, const char *name
, struct symbol
*sym
);
182 void delete_state_expr(int owner
, struct expression
*expr
);
183 void set_true_false_states(int owner
, const char *name
, struct symbol
*sym
,
184 struct smatch_state
*true_state
,
185 struct smatch_state
*false_state
);
186 void set_true_false_states_expr(int owner
, struct expression
*expr
,
187 struct smatch_state
*true_state
,
188 struct smatch_state
*false_state
);
190 struct state_list
*get_all_states(int id
);
191 int is_reachable(void);
193 /* smatch_helper.c */
194 char *alloc_string(const char *str
);
195 void free_string(char *str
);
196 struct smatch_state
*alloc_state_num(int num
);
197 struct expression
*get_argument_from_call_expr(struct expression_list
*args
,
199 char *get_variable_from_expr_complex(struct expression
*expr
,
200 struct symbol
**sym_ptr
);
201 char *get_variable_from_expr(struct expression
*expr
,
202 struct symbol
**sym_ptr
);
203 int sym_name_is(const char *name
, struct expression
*expr
);
204 int get_value(struct expression
*expr
, long long *val
);
205 int get_implied_value(struct expression
*expr
, long long *val
);
206 int get_implied_max(struct expression
*expr
, long long *val
);
207 int get_implied_min(struct expression
*expr
, long long *val
);
208 int get_fuzzy_min(struct expression
*expr
, long long *min
);
209 int get_fuzzy_max(struct expression
*expr
, long long *max
);
210 int get_absolute_min(struct expression
*expr
, long long *val
);
211 int get_absolute_max(struct expression
*expr
, long long *val
);
212 int is_zero(struct expression
*expr
);
213 int is_array(struct expression
*expr
);
214 struct expression
*get_array_name(struct expression
*expr
);
215 struct expression
*get_array_offset(struct expression
*expr
);
216 const char *show_state(struct smatch_state
*state
);
217 struct statement
*get_expression_statement(struct expression
*expr
);
218 struct expression
*strip_parens(struct expression
*expr
);
219 struct expression
*strip_expr(struct expression
*expr
);
220 void scoped_state(int my_id
, const char *name
, struct symbol
*sym
);
221 int is_error_return(struct expression
*expr
);
222 int getting_address(void);
225 struct symbol
*get_type(struct expression
*expr
);
226 int type_unsigned(struct symbol
*base_type
);
227 long long type_max(struct symbol
*base_type
);
228 long long type_min(struct symbol
*base_type
);
230 /* smatch_ignore.c */
231 void add_ignore(int owner
, const char *name
, struct symbol
*sym
);
232 int is_ignored(int owner
, const char *name
, struct symbol
*sym
);
235 struct tracker
*alloc_tracker(int owner
, const char *name
, struct symbol
*sym
);
236 void add_tracker(struct tracker_list
**list
, int owner
, const char *name
,
238 void add_tracker_expr(struct tracker_list
**list
, int owner
, struct expression
*expr
);
239 void del_tracker(struct tracker_list
**list
, int owner
, const char *name
,
241 int in_tracker_list(struct tracker_list
*list
, int owner
, const char *name
,
243 struct tracker_list
*clone_tracker_list(struct tracker_list
*orig_list
);
244 void free_tracker_list(struct tracker_list
**list
);
245 void free_trackers_and_list(struct tracker_list
**list
);
247 /* smatch_conditions */
248 int in_condition(void);
252 void smatch (int argc
, char **argv
);
253 int inside_loop(void);
254 int in_expression_statement(void);
255 void __split_expr(struct expression
*expr
);
256 void __split_stmt(struct statement
*stmt
);
257 extern int option_assume_loops
;
258 extern int option_known_conditions
;
259 extern int option_two_passes
;
260 extern struct expression_list
*big_expression_stack
;
261 extern struct statement_list
*big_statement_stack
;
262 extern int __in_pre_condition
;
263 extern int __bail_on_rest_of_function
;
265 /* smatch_conditions */
266 void __split_whole_condition(struct expression
*expr
);
267 void __handle_logic(struct expression
*expr
);
268 int __handle_condition_assigns(struct expression
*expr
);
269 int __handle_select_assigns(struct expression
*expr
);
270 int __handle_expr_statement_assigns(struct expression
*expr
);
272 /* smatch_implied.c */
273 extern int option_debug_implied
;
274 extern int option_no_implied
;
275 void get_implications(char *name
, struct symbol
*sym
, int comparison
, long long num
,
276 struct state_list
**true_states
,
277 struct state_list
**false_states
);
278 struct range_list_stack
;
279 struct state_list
*__implied_case_slist(struct expression
*switch_expr
,
280 struct expression
*case_expr
,
281 struct range_list_stack
**remaining_cases
,
282 struct state_list
**raw_slist
);
283 struct range_list
*__get_implied_values(struct expression
*switch_expr
);
285 /* smatch_extras.c */
286 #define SMATCH_EXTRA 1 /* this is my_id from smatch extra set in smatch.c */
292 extern struct data_range whole_range
;
294 int known_condition_true(struct expression
*expr
);
295 int known_condition_false(struct expression
*expr
);
296 int implied_condition_true(struct expression
*expr
);
297 int implied_condition_false(struct expression
*expr
);
299 /* smatch_states.c */
300 void __push_fake_cur_slist();
301 struct state_list
*__pop_fake_cur_slist();
303 int unreachable(void);
304 void __set_sm(struct sm_state
*sm
);
305 struct state_list
*__get_cur_slist(void);
306 void __set_true_false_sm(struct sm_state
*true_state
,
307 struct sm_state
*false_state
);
308 void nullify_path(void);
309 void __match_nullify_path_hook(const char *fn
, struct expression
*expr
,
311 void __unnullify_path(void);
312 int __path_is_null(void);
313 void clear_all_states(void);
315 struct sm_state
*get_sm_state(int owner
, const char *name
,
317 struct sm_state
*get_sm_state_expr(int owner
, struct expression
*expr
);
318 void __push_true_states(void);
319 void __use_false_states(void);
320 void __discard_false_states(void);
321 void __merge_false_states(void);
322 void __merge_true_states(void);
324 void __negate_cond_stacks(void);
325 void __use_pre_cond_states(void);
326 void __use_cond_true_states(void);
327 void __use_cond_false_states(void);
328 void __push_cond_stacks(void);
329 struct state_list
*__copy_cond_true_states(void);
330 struct state_list
*__copy_cond_false_states(void);
331 struct state_list
*__pop_cond_true_stack(void);
332 struct state_list
*__pop_cond_false_stack(void);
333 void __and_cond_states(void);
334 void __or_cond_states(void);
335 void __save_pre_cond_states(void);
336 void __discard_pre_cond_states(void);
337 void __use_cond_states(void);
339 void __warn_on_silly_pre_loops(void);
341 void __push_continues(void);
342 void __discard_continues(void);
343 void __process_continues(void);
344 void __merge_continues(void);
346 void __push_breaks(void);
347 void __process_breaks(void);
348 void __merge_breaks(void);
349 void __use_breaks(void);
351 void __save_switch_states(struct expression
*switch_expr
);
352 void __discard_switches(void);
353 void __merge_switches(struct expression
*switch_expr
, struct expression
*case_expr
);
354 void __push_default(void);
355 void __set_default(void);
356 int __pop_default(void);
358 void __push_conditions(void);
359 void __discard_conditions(void);
361 void __save_gotos(const char *name
);
362 void __merge_gotos(const char *name
);
364 void __print_cur_slist(void);
367 void __pass_to_client(void *data
, enum hook_type type
);
368 void __pass_to_client_no_data(enum hook_type type
);
369 void __pass_case_to_client(struct expression
*switch_expr
,
370 struct expression
*case_expr
);
371 int __has_merge_function(int client_id
);
372 struct smatch_state
*__client_merge_function(int owner
, const char *name
,
374 struct smatch_state
*s1
,
375 struct smatch_state
*s2
);
376 struct smatch_state
*__client_unmatched_state_function(struct sm_state
*sm
);
377 void __push_scope_hooks(void);
378 void __call_scope_hooks(void);
380 /* smatch_function_hooks.c */
381 void create_function_hook_hash(void);
382 void __match_initializer_call(struct symbol
*sym
);
385 struct token
*get_tokens_file(const char *filename
);
388 extern int option_oom_kb
;
389 int out_of_memory(void);
392 extern char *option_project_str
;
393 extern char *data_dir
;
394 extern int option_no_data
;
395 extern int option_spammy
;
396 extern int option_full_path
;
397 extern int option_param_mapper
;
398 extern int option_print_returns
;
399 extern int option_info
;
400 extern int option_call_tree
;
401 extern int num_checks
;
408 extern enum project_type option_project
;
409 const char *check_name(unsigned short id
);
412 /* smatch_buf_size.c */
413 int get_array_size(struct expression
*expr
);
414 int get_array_size_bytes(struct expression
*expr
);
416 /* check_locking.c */
417 void print_held_locks();
419 #endif /* !SMATCH_H_ */