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
);
79 MACRO_ASSIGNMENT_HOOK
,
95 void add_hook(void *func
, enum hook_type type
);
96 typedef struct smatch_state
*(merge_func_t
)(const char *name
,
98 struct smatch_state
*s1
,
99 struct smatch_state
*s2
);
100 typedef struct smatch_state
*(unmatched_func_t
)(struct sm_state
*state
);
101 void add_merge_hook(int client_id
, merge_func_t
*func
);
102 void add_unmatched_state_hook(int client_id
, unmatched_func_t
*func
);
103 typedef void (scope_hook
)(void *data
);
104 void add_scope_hook(scope_hook
*hook
, void *data
);
105 typedef void (func_hook
)(const char *fn
, struct expression
*expr
, void *data
);
106 typedef void (implication_hook
)(const char *fn
, struct expression
*call_expr
,
107 struct expression
*assign_expr
, void *data
);
108 void add_function_hook(const char *look_for
, func_hook
*call_back
, void *data
);
110 void add_function_assign_hook(const char *look_for
, func_hook
*call_back
,
112 void add_macro_assign_hook(const char *look_for
, func_hook
*call_back
,
114 void return_implies_state(const char *look_for
, long long start
, long long end
,
115 implication_hook
*call_back
, void *info
);
116 typedef void (modification_hook
)(const char *name
, struct symbol
*sym
,
117 struct expression
*expr
, void *data
);
118 void add_modification_hook(int owner
, const char *variable
, modification_hook
*hook
,
120 void add_modification_hook_expr(int owner
, struct expression
*expr
, modification_hook
*call_back
, void *info
);
121 void set_default_modification_hook(int owner
, modification_hook
*call_back
);
122 void __use_default_modification_hook(int owner
, const char *variable
);
124 const char *get_filename(void);
125 char *get_function(void);
126 int get_lineno(void);
127 int get_func_pos(void);
128 extern int final_pass
;
129 extern struct symbol
*cur_func_sym
;
130 extern int option_debug
;
132 #define sm_printf(msg...) do { if (final_pass) printf(msg); } while (0) \
134 static inline void sm_prefix(void)
136 printf("%s +%d %s(%d) ", get_filename(), get_lineno(), get_function(), get_func_pos());
140 static inline void print_implied_debug_msg();
142 #define sm_msg(msg...) \
144 print_implied_debug_msg(); \
145 if (!option_debug && !final_pass) \
152 extern char *implied_debug_msg
;
153 static inline void print_implied_debug_msg()
155 static struct symbol
*last_printed
= NULL
;
157 if (!implied_debug_msg
)
159 if (last_printed
== cur_func_sym
)
161 last_printed
= cur_func_sym
;
162 sm_msg("%s", implied_debug_msg
);
165 #define sm_debug(msg...) do { if (option_debug) printf(msg); } while (0)
167 #define sm_info(msg...) do { \
168 if (option_debug || (option_info && final_pass)) { \
176 #define POINTER_MAX 0xffffffff
178 struct smatch_state
*get_state(int owner
, const char *name
, struct symbol
*sym
);
179 struct smatch_state
*get_state_expr(int owner
, struct expression
*expr
);
180 struct state_list
*get_possible_states(int owner
, const char *name
,
182 struct state_list
*get_possible_states_expr(int owner
, struct expression
*expr
);
183 struct sm_state
*set_state(int owner
, const char *name
, struct symbol
*sym
,
184 struct smatch_state
*state
);
185 struct sm_state
*set_state_expr(int owner
, struct expression
*expr
,
186 struct smatch_state
*state
);
187 void delete_state(int owner
, const char *name
, struct symbol
*sym
);
188 void delete_state_expr(int owner
, struct expression
*expr
);
189 void set_true_false_states(int owner
, const char *name
, struct symbol
*sym
,
190 struct smatch_state
*true_state
,
191 struct smatch_state
*false_state
);
192 void set_true_false_states_expr(int owner
, struct expression
*expr
,
193 struct smatch_state
*true_state
,
194 struct smatch_state
*false_state
);
196 struct state_list
*get_all_states(int id
);
197 int is_reachable(void);
199 /* smatch_helper.c */
200 char *alloc_string(const char *str
);
201 void free_string(char *str
);
202 struct smatch_state
*alloc_state_num(int num
);
203 struct expression
*get_argument_from_call_expr(struct expression_list
*args
,
205 char *get_variable_from_expr_complex(struct expression
*expr
,
206 struct symbol
**sym_ptr
);
207 char *get_variable_from_expr(struct expression
*expr
,
208 struct symbol
**sym_ptr
);
209 int sym_name_is(const char *name
, struct expression
*expr
);
210 int get_value(struct expression
*expr
, long long *val
);
211 int get_implied_value(struct expression
*expr
, long long *val
);
212 int get_implied_max(struct expression
*expr
, long long *val
);
213 int get_implied_min(struct expression
*expr
, long long *val
);
214 int get_fuzzy_min(struct expression
*expr
, long long *min
);
215 int get_fuzzy_max(struct expression
*expr
, long long *max
);
216 int get_absolute_min(struct expression
*expr
, long long *val
);
217 int get_absolute_max(struct expression
*expr
, long long *val
);
218 int is_zero(struct expression
*expr
);
219 int is_array(struct expression
*expr
);
220 struct expression
*get_array_name(struct expression
*expr
);
221 struct expression
*get_array_offset(struct expression
*expr
);
222 const char *show_state(struct smatch_state
*state
);
223 struct statement
*get_expression_statement(struct expression
*expr
);
224 struct expression
*strip_parens(struct expression
*expr
);
225 struct expression
*strip_expr(struct expression
*expr
);
226 void scoped_state(int my_id
, const char *name
, struct symbol
*sym
);
227 int is_error_return(struct expression
*expr
);
228 int getting_address(void);
231 struct symbol
*get_type(struct expression
*expr
);
232 int type_unsigned(struct symbol
*base_type
);
233 int returns_unsigned(struct symbol
*base_type
);
234 int returns_pointer(struct symbol
*base_type
);
235 long long type_max(struct symbol
*base_type
);
236 long long type_min(struct symbol
*base_type
);
238 /* smatch_ignore.c */
239 void add_ignore(int owner
, const char *name
, struct symbol
*sym
);
240 int is_ignored(int owner
, const char *name
, struct symbol
*sym
);
243 struct tracker
*alloc_tracker(int owner
, const char *name
, struct symbol
*sym
);
244 void add_tracker(struct tracker_list
**list
, int owner
, const char *name
,
246 void add_tracker_expr(struct tracker_list
**list
, int owner
, struct expression
*expr
);
247 void del_tracker(struct tracker_list
**list
, int owner
, const char *name
,
249 int in_tracker_list(struct tracker_list
*list
, int owner
, const char *name
,
251 struct tracker_list
*clone_tracker_list(struct tracker_list
*orig_list
);
252 void free_tracker_list(struct tracker_list
**list
);
253 void free_trackers_and_list(struct tracker_list
**list
);
255 /* smatch_conditions */
256 int in_condition(void);
260 void smatch (int argc
, char **argv
);
261 int inside_loop(void);
262 int in_expression_statement(void);
263 void __split_expr(struct expression
*expr
);
264 void __split_stmt(struct statement
*stmt
);
265 extern int option_assume_loops
;
266 extern int option_known_conditions
;
267 extern int option_two_passes
;
268 extern int option_no_db
;
269 extern struct expression_list
*big_expression_stack
;
270 extern struct statement_list
*big_statement_stack
;
271 extern int __in_pre_condition
;
272 extern int __bail_on_rest_of_function
;
274 /* smatch_conditions */
275 void __split_whole_condition(struct expression
*expr
);
276 void __handle_logic(struct expression
*expr
);
277 int __handle_condition_assigns(struct expression
*expr
);
278 int __handle_select_assigns(struct expression
*expr
);
279 int __handle_expr_statement_assigns(struct expression
*expr
);
281 /* smatch_implied.c */
282 extern int option_debug_implied
;
283 extern int option_no_implied
;
284 void get_implications(char *name
, struct symbol
*sym
, int comparison
, long long num
,
285 struct state_list
**true_states
,
286 struct state_list
**false_states
);
287 struct range_list_stack
;
288 struct state_list
*__implied_case_slist(struct expression
*switch_expr
,
289 struct expression
*case_expr
,
290 struct range_list_stack
**remaining_cases
,
291 struct state_list
**raw_slist
);
292 struct range_list
*__get_implied_values(struct expression
*switch_expr
);
294 /* smatch_extras.c */
295 #define SMATCH_EXTRA 1 /* this is my_id from smatch extra set in smatch.c */
301 extern struct data_range whole_range
;
303 int known_condition_true(struct expression
*expr
);
304 int known_condition_false(struct expression
*expr
);
305 int implied_condition_true(struct expression
*expr
);
306 int implied_condition_false(struct expression
*expr
);
308 /* smatch_states.c */
309 void __push_fake_cur_slist();
310 struct state_list
*__pop_fake_cur_slist();
312 int unreachable(void);
313 void __set_sm(struct sm_state
*sm
);
314 struct state_list
*__get_cur_slist(void);
315 void __set_true_false_sm(struct sm_state
*true_state
,
316 struct sm_state
*false_state
);
317 void nullify_path(void);
318 void __match_nullify_path_hook(const char *fn
, struct expression
*expr
,
320 void __unnullify_path(void);
321 int __path_is_null(void);
322 void clear_all_states(void);
324 struct sm_state
*get_sm_state(int owner
, const char *name
,
326 struct sm_state
*get_sm_state_expr(int owner
, struct expression
*expr
);
327 void __push_true_states(void);
328 void __use_false_states(void);
329 void __discard_false_states(void);
330 void __merge_false_states(void);
331 void __merge_true_states(void);
333 void __negate_cond_stacks(void);
334 void __use_pre_cond_states(void);
335 void __use_cond_true_states(void);
336 void __use_cond_false_states(void);
337 void __push_cond_stacks(void);
338 struct state_list
*__copy_cond_true_states(void);
339 struct state_list
*__copy_cond_false_states(void);
340 struct state_list
*__pop_cond_true_stack(void);
341 struct state_list
*__pop_cond_false_stack(void);
342 void __and_cond_states(void);
343 void __or_cond_states(void);
344 void __save_pre_cond_states(void);
345 void __discard_pre_cond_states(void);
346 void __use_cond_states(void);
348 void __warn_on_silly_pre_loops(void);
350 void __push_continues(void);
351 void __discard_continues(void);
352 void __process_continues(void);
353 void __merge_continues(void);
355 void __push_breaks(void);
356 void __process_breaks(void);
357 void __merge_breaks(void);
358 void __use_breaks(void);
360 void __save_switch_states(struct expression
*switch_expr
);
361 void __discard_switches(void);
362 void __merge_switches(struct expression
*switch_expr
, struct expression
*case_expr
);
363 void __push_default(void);
364 void __set_default(void);
365 int __pop_default(void);
367 void __push_conditions(void);
368 void __discard_conditions(void);
370 void __save_gotos(const char *name
);
371 void __merge_gotos(const char *name
);
373 void __print_cur_slist(void);
376 void __pass_to_client(void *data
, enum hook_type type
);
377 void __pass_to_client_no_data(enum hook_type type
);
378 void __pass_case_to_client(struct expression
*switch_expr
,
379 struct expression
*case_expr
);
380 int __has_merge_function(int client_id
);
381 struct smatch_state
*__client_merge_function(int owner
, const char *name
,
383 struct smatch_state
*s1
,
384 struct smatch_state
*s2
);
385 struct smatch_state
*__client_unmatched_state_function(struct sm_state
*sm
);
386 void __push_scope_hooks(void);
387 void __call_scope_hooks(void);
389 /* smatch_function_hooks.c */
390 void create_function_hook_hash(void);
391 void __match_initializer_call(struct symbol
*sym
);
394 #define run_sql(call_back, sql...) \
396 char sql_txt[1024]; \
397 snprintf(sql_txt, 1024, sql); \
398 sql_txt[1023] = '\0'; \
399 sql_exec(call_back, sql_txt); \
402 void sql_exec(int (*callback
)(void*, int, char**, char**), const char *sql
);
403 void open_smatch_db(void);
406 struct token
*get_tokens_file(const char *filename
);
409 extern char *option_project_str
;
410 extern char *data_dir
;
411 extern int option_no_data
;
412 extern int option_spammy
;
413 extern int option_full_path
;
414 extern int option_param_mapper
;
415 extern int option_print_returns
;
416 extern int option_info
;
417 extern int option_call_tree
;
418 extern int num_checks
;
425 extern enum project_type option_project
;
426 const char *check_name(unsigned short id
);
429 /* smatch_buf_size.c */
430 int get_array_size(struct expression
*expr
);
431 int get_array_size_bytes(struct expression
*expr
);
433 /* check_locking.c */
434 void print_held_locks();
436 #endif /* !SMATCH_H_ */