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
);
89 void add_hook(void *func
, enum hook_type type
);
90 typedef struct smatch_state
*(merge_func_t
)(const char *name
,
92 struct smatch_state
*s1
,
93 struct smatch_state
*s2
);
94 typedef struct smatch_state
*(unmatched_func_t
)(struct sm_state
*state
);
95 void add_merge_hook(int client_id
, merge_func_t
*func
);
96 void add_unmatched_state_hook(int client_id
, unmatched_func_t
*func
);
97 typedef void (scope_hook
)(void *data
);
98 void add_scope_hook(scope_hook
*hook
, void *data
);
99 typedef void (func_hook
)(const char *fn
, struct expression
*expr
, void *data
);
100 typedef void (implication_hook
)(const char *fn
, struct expression
*call_expr
,
101 struct expression
*assign_expr
, void *data
);
102 void add_function_hook(const char *lock_for
, func_hook
*call_back
, void *data
);
104 void add_conditional_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(const char *variable
, modification_hook
*hook
,
113 void add_modification_hook_expr(struct expression
*expr
, modification_hook
*call_back
, void *info
);
115 const char *get_filename(void);
116 char *get_function(void);
117 int get_lineno(void);
118 int get_func_pos(void);
119 extern int final_pass
;
121 #define sm_printf(msg...) do { if (final_pass) printf(msg); } while (0) \
123 static inline void sm_prefix(void)
125 printf("%s +%d %s(%d) ", get_filename(), get_lineno(), get_function(), get_func_pos());
128 #define sm_msg(msg...) \
130 if (!option_debug && !final_pass) \
137 #define sm_debug(msg...) do { if (option_debug) printf(msg); } while (0)
139 #define sm_info(msg...) do { \
140 if (option_debug || (option_info && final_pass)) { \
148 #define POINTER_MAX 0xffffffff
150 struct smatch_state
*get_state(int owner
, const char *name
, struct symbol
*sym
);
151 struct smatch_state
*get_state_expr(int owner
, struct expression
*expr
);
152 struct state_list
*get_possible_states(int owner
, const char *name
,
154 struct state_list
*get_possible_states_expr(int owner
, struct expression
*expr
);
155 void set_state(int owner
, const char *name
, struct symbol
*sym
,
156 struct smatch_state
*state
);
157 void set_state_expr(int owner
, struct expression
*expr
,
158 struct smatch_state
*state
);
159 void delete_state(int owner
, const char *name
, struct symbol
*sym
);
160 void delete_state_expr(int owner
, struct expression
*expr
);
161 void set_true_false_states(int owner
, const char *name
, struct symbol
*sym
,
162 struct smatch_state
*true_state
,
163 struct smatch_state
*false_state
);
164 void set_true_false_states_expr(int owner
, struct expression
*expr
,
165 struct smatch_state
*true_state
,
166 struct smatch_state
*false_state
);
168 struct state_list
*get_all_states(int id
);
169 int is_reachable(void);
171 /* smatch_helper.c */
172 char *alloc_string(const char *str
);
173 void free_string(char *str
);
174 struct smatch_state
*alloc_state_num(int num
);
175 struct expression
*get_argument_from_call_expr(struct expression_list
*args
,
177 char *get_variable_from_expr_complex(struct expression
*expr
,
178 struct symbol
**sym_ptr
);
179 char *get_variable_from_expr(struct expression
*expr
,
180 struct symbol
**sym_ptr
);
181 int sym_name_is(const char *name
, struct expression
*expr
);
182 int get_value(struct expression
*expr
, long long *val
);
183 int get_implied_value(struct expression
*expr
, long long *val
);
184 int get_fuzzy_max(struct expression
*expr
, long long *max
);
185 int is_zero(struct expression
*expr
);
186 int is_array(struct expression
*expr
);
187 struct expression
*get_array_name(struct expression
*expr
);
188 struct expression
*get_array_offset(struct expression
*expr
);
189 const char *show_state(struct smatch_state
*state
);
190 struct statement
*get_block_thing(struct expression
*expr
);
191 struct expression
*strip_parens(struct expression
*expr
);
192 struct expression
*strip_expr(struct expression
*expr
);
193 void scoped_state(int my_id
, const char *name
, struct symbol
*sym
);
194 int is_error_return(struct expression
*expr
);
195 int getting_address(void);
198 struct symbol
*get_type(struct expression
*expr
);
199 long long type_max(struct symbol
*base_type
);
200 long long type_min(struct symbol
*base_type
);
202 /* smatch_ignore.c */
203 void add_ignore(int owner
, const char *name
, struct symbol
*sym
);
204 int is_ignored(int owner
, const char *name
, struct symbol
*sym
);
207 struct tracker
*alloc_tracker(int owner
, const char *name
, struct symbol
*sym
);
208 void add_tracker(struct tracker_list
**list
, int owner
, const char *name
,
210 void del_tracker(struct tracker_list
**list
, int owner
, const char *name
,
212 int in_tracker_list(struct tracker_list
*list
, int owner
, const char *name
,
214 void free_tracker_list(struct tracker_list
**list
);
215 void free_trackers_and_list(struct tracker_list
**list
);
217 /* smatch_conditions */
218 int in_condition(void);
222 void smatch (int argc
, char **argv
);
223 void __split_expr(struct expression
*expr
);
224 void __split_statements(struct statement
*stmt
);
225 extern int option_assume_loops
;
226 extern int option_known_conditions
;
227 extern int option_two_passes
;
228 extern struct symbol
*cur_func_sym
;
229 extern struct expression_list
*big_expression_stack
;
231 /* smatch_conditions */
232 void __split_whole_condition(struct expression
*expr
);
234 /* smatch_implied.c */
235 extern int option_debug_implied
;
236 extern int option_no_implied
;
237 void get_implications(char *name
, struct symbol
*sym
, int comparison
, int num
,
238 struct state_list
**true_states
,
239 struct state_list
**false_states
);
240 struct range_list_stack
;
241 struct state_list
*__implied_case_slist(struct expression
*switch_expr
,
242 struct expression
*case_expr
,
243 struct range_list_stack
**remaining_cases
,
244 struct state_list
**raw_slist
);
245 struct range_list
*__get_implied_values(struct expression
*switch_expr
);
247 /* smatch_extras.c */
248 #define SMATCH_EXTRA 1 /* this is my_id from smatch extra set in smatch.c */
254 extern struct data_range whole_range
;
256 int get_implied_single_val(struct expression
*expr
, long long *val
);
257 int get_implied_single_fuzzy_max(struct expression
*expr
, long long *max
);
258 int get_implied_max(struct expression
*expr
, long long *val
);
259 int get_implied_min(struct expression
*expr
, long long *val
);
260 int true_comparison(int left
, int comparison
, int right
);
261 int known_condition_true(struct expression
*expr
);
262 int known_condition_false(struct expression
*expr
);
263 int implied_condition_true(struct expression
*expr
);
264 int implied_condition_false(struct expression
*expr
);
266 /* smatch_states.c */
267 extern int option_debug
;
269 extern int __fake_cur
;
270 extern struct state_list
*__fake_cur_slist
;
271 extern int __fake_conditions
;
272 extern struct state_list
*__fake_cond_true
;
273 extern struct state_list
*__fake_cond_false
;
276 int unreachable(void);
277 void __set_state(struct sm_state
*sm
);
278 struct state_list
*__get_cur_slist(void);
279 void __set_true_false_sm(struct sm_state
*true_state
,
280 struct sm_state
*false_state
);
281 void nullify_path(void);
282 void __match_nullify_path_hook(const char *fn
, struct expression
*expr
,
284 void __unnullify_path(void);
285 int __path_is_null(void);
286 void clear_all_states(void);
288 struct sm_state
*get_sm_state(int owner
, const char *name
,
290 struct sm_state
*get_sm_state_expr(int owner
, struct expression
*expr
);
291 void __push_true_states(void);
292 void __use_false_states(void);
293 void __pop_false_states(void);
294 void __merge_false_states(void);
295 void __merge_true_states(void);
297 void __negate_cond_stacks(void);
298 void __save_false_states_for_later(void);
299 void __use_previously_stored_false_states(void);
300 void __use_cond_true_states(void);
301 void __use_cond_false_states(void);
302 void __push_cond_stacks(void);
303 void __and_cond_states(void);
304 void __or_cond_states(void);
305 void __save_pre_cond_states(void);
306 void __pop_pre_cond_states(void);
307 void __use_cond_states(void);
309 void __warn_on_silly_pre_loops(void);
311 void __push_continues(void);
312 void __pop_continues(void);
313 void __process_continues(void);
314 void __merge_continues(void);
316 void __push_breaks(void);
317 void __process_breaks(void);
318 void __merge_breaks(void);
319 void __use_breaks(void);
321 void __save_switch_states(struct expression
*switch_expr
);
322 void __pop_switches(void);
323 void __merge_switches(struct expression
*switch_expr
, struct expression
*case_expr
);
324 void __push_default(void);
325 void __set_default(void);
326 int __pop_default(void);
328 void __push_conditions(void);
329 void __pop_conditions(void);
331 void __save_gotos(const char *name
);
332 void __merge_gotos(const char *name
);
334 void __print_cur_slist(void);
337 void __pass_to_client(void *data
, enum hook_type type
);
338 void __pass_to_client_no_data(enum hook_type type
);
339 void __pass_case_to_client(struct expression
*switch_expr
,
340 struct expression
*case_expr
);
341 int __has_merge_function(int client_id
);
342 struct smatch_state
*__client_merge_function(int owner
, const char *name
,
344 struct smatch_state
*s1
,
345 struct smatch_state
*s2
);
346 struct smatch_state
*__client_unmatched_state_function(struct sm_state
*sm
);
347 void __push_scope_hooks(void);
348 void __call_scope_hooks(void);
350 /* smatch_function_hooks.c */
351 void create_function_hook_hash(void);
352 void __match_initializer_call(struct symbol
*sym
);
355 struct token
*get_tokens_file(const char *filename
);
358 extern int option_oom_kb
;
359 int out_of_memory(void);
362 extern char *option_project_str
;
363 extern char *data_dir
;
364 extern int option_no_data
;
365 extern int option_spammy
;
366 extern int option_rare
;
367 extern int option_full_path
;
368 extern int option_param_mapper
;
369 extern int option_info
;
370 extern int option_call_tree
;
377 extern enum project_type option_project
;
378 const char *check_name(unsigned short id
);
380 #endif /* !SMATCH_H_ */