4 * Copyright (C) 2006 Dan Carpenter.
6 * Licensed under the Open Software License version 1.1
19 #include "expression.h"
26 #define STATE(_x) static struct smatch_state _x = { .name = #_x }
27 extern struct smatch_state undefined
;
28 extern struct smatch_state merged
;
29 extern struct smatch_state true_state
;
30 extern struct smatch_state false_state
;
31 DECLARE_ALLOCATOR(smatch_state
);
33 static inline void *INT_PTR(int i
)
35 return (void *)(long)i
;
38 static inline int PTR_INT(void *p
)
45 unsigned int merged
:1;
46 unsigned int implied
:1;
49 struct smatch_state
*state
;
51 struct state_list
*my_pool
;
52 struct sm_state
*left
;
53 struct sm_state
*right
;
54 unsigned int nr_children
;
55 struct state_list
*possible
;
63 DECLARE_ALLOCATOR(tracker
);
64 DECLARE_PTR_LIST(tracker_list
, struct tracker
);
85 void add_hook(void *func
, enum hook_type type
);
86 typedef struct smatch_state
*(merge_func_t
)(const char *name
,
88 struct smatch_state
*s1
,
89 struct smatch_state
*s2
);
90 typedef struct smatch_state
*(unmatched_func_t
)(struct sm_state
*state
);
91 void add_merge_hook(int client_id
, merge_func_t
*func
);
92 void add_unmatched_state_hook(int client_id
, unmatched_func_t
*func
);
93 typedef void (scope_hook
)(void *data
);
94 void add_scope_hook(scope_hook
*hook
, void *data
);
95 typedef void (func_hook
)(const char *fn
, struct expression
*expr
, void *data
);
96 typedef void (implication_hook
)(const char *fn
, struct expression
*call_expr
,
97 struct expression
*assign_expr
, void *data
);
98 void add_function_hook(const char *lock_for
, func_hook
*call_back
, void *data
);
100 void add_conditional_hook(const char *look_for
, func_hook
*call_back
, void *data
);
101 void add_function_assign_hook(const char *look_for
, func_hook
*call_back
,
103 void return_implies_state(const char *look_for
, long long start
, long long end
,
104 implication_hook
*call_back
, void *info
);
105 typedef void (modification_hook
)(const char *name
, struct symbol
*sym
,
106 struct expression
*expr
, void *data
);
107 void add_modification_hook(const char *variable
, modification_hook
*hook
,
109 int is_member(struct expression
*expr
);
110 void reset_on_container_modified(int owner
, struct expression
*expr
);
111 void set_default_state(int owner
, struct smatch_state
*state
);
113 extern int final_pass
;
115 #define sm_printf(msg...) do { if (final_pass) printf(msg); } while (0) \
117 #define sm_msg(msg...) \
121 printf("%s +%d %s(%d) ", get_filename(), get_lineno(), \
122 get_function(), get_func_pos()); \
127 #define sm_debug(msg...) do { if (debug_states) printf(msg); } while (0)
129 #define UNDEFINED INT_MIN
130 #define POINTER_MAX 0xffffffff
132 struct smatch_state
*get_state(int owner
, const char *name
, struct symbol
*sym
);
133 struct smatch_state
*get_state_expr(int owner
, struct expression
*expr
);
134 struct state_list
*get_possible_states(int owner
, const char *name
,
136 struct state_list
*get_possible_states_expr(int owner
, struct expression
*expr
);
137 void set_state(int owner
, const char *name
, struct symbol
*sym
,
138 struct smatch_state
*state
);
139 void set_state_expr(int owner
, struct expression
*expr
,
140 struct smatch_state
*state
);
141 void delete_state(int owner
, const char *name
, struct symbol
*sym
);
142 void delete_state_expr(int owner
, struct expression
*expr
);
143 void set_true_false_states(int owner
, const char *name
, struct symbol
*sym
,
144 struct smatch_state
*true_state
,
145 struct smatch_state
*false_state
);
146 void set_true_false_states_expr(int owner
, struct expression
*expr
,
147 struct smatch_state
*true_state
,
148 struct smatch_state
*false_state
);
150 struct state_list
*get_all_states(int id
);
152 char *get_filename();
153 char *get_function();
157 /* smatch_helper.c */
158 char *alloc_string(const char *str
);
159 void free_string(char *str
);
160 struct expression
*get_argument_from_call_expr(struct expression_list
*args
,
162 char *get_variable_from_expr_complex(struct expression
*expr
,
163 struct symbol
**sym_ptr
);
164 char *get_variable_from_expr(struct expression
*expr
,
165 struct symbol
**sym_ptr
);
166 struct symbol
*get_ptr_type(struct expression
*expr
);
167 int sym_name_is(const char *name
, struct expression
*expr
);
168 int get_value(struct expression
*expr
);
169 int get_implied_value(struct expression
*expr
);
170 int is_zero(struct expression
*expr
);
171 int is_array(struct expression
*expr
);
172 struct expression
*get_array_name(struct expression
*expr
);
173 struct expression
*get_array_offset(struct expression
*expr
);
174 const char *show_state(struct smatch_state
*state
);
175 struct statement
*get_block_thing(struct expression
*expr
);
176 struct expression
*strip_expr(struct expression
*expr
);
177 void scoped_state(const char *name
, int my_id
, struct symbol
*sym
);
178 int is_error_return(struct expression
*expr
);
180 /* smatch_ignore.c */
181 void add_ignore(int owner
, const char *name
, struct symbol
*sym
);
182 int is_ignored(int owner
, const char *name
, struct symbol
*sym
);
185 struct tracker
*alloc_tracker(int owner
, const char *name
, struct symbol
*sym
);
186 void add_tracker(struct tracker_list
**list
, int owner
, const char *name
,
188 void del_tracker(struct tracker_list
**list
, int owner
, const char *name
,
190 int in_tracker_list(struct tracker_list
*list
, int owner
, const char *name
,
192 void free_tracker_list(struct tracker_list
**list
);
193 void free_trackers_and_list(struct tracker_list
**list
);
195 /* smatch_conditions */
198 /* ----------------------------------------------------------------
199 The stuff below is all used internally and shouldn't
200 be called from other programs
201 -----------------------------------------------------------------*/
205 void smatch (int argc
, char **argv
);
206 void __split_expr(struct expression
*expr
);
207 void __split_statements(struct statement
*stmt
);
208 extern int option_assume_loops
;
209 extern int option_known_conditions
;
210 extern int option_two_passes
;
211 extern struct symbol
*cur_func_sym
;
213 /* smatch_conditions */
214 void __split_whole_condition(struct expression
*expr
);
216 /* smatch_implied.c */
217 extern int debug_implied_states
;
218 extern int option_no_implied
;
219 void get_implications(char *name
, struct symbol
*sym
, int comparison
, int num
,
220 struct state_list
**true_states
,
221 struct state_list
**false_states
);
222 struct range_list_stack
;
223 struct state_list
*__implied_case_slist(struct expression
*switch_expr
,
224 struct expression
*case_expr
,
225 struct range_list_stack
**remaining_cases
,
226 struct state_list
**raw_slist
);
227 struct range_list
*__get_implied_values(struct expression
*switch_expr
);
229 /* smatch_extras.c */
230 #define SMATCH_EXTRA 1 /* this is my_id from smatch extra set in smatch.c */
236 extern struct data_range whole_range
;
238 int get_implied_single_val(struct expression
*expr
);
239 int get_implied_max(struct expression
*expr
);
240 int get_implied_min(struct expression
*expr
);
241 int true_comparison(int left
, int comparison
, int right
);
242 int known_condition_true(struct expression
*expr
);
243 int known_condition_false(struct expression
*expr
);
244 int implied_condition_true(struct expression
*expr
);
245 int implied_condition_false(struct expression
*expr
);
247 /* smatch_states.c */
248 extern int debug_states
;
250 extern int __fake_cur
;
251 extern struct state_list
*__fake_cur_slist
;
252 extern int __fake_conditions
;
253 extern struct state_list
*__fake_cond_true
;
254 extern struct state_list
*__fake_cond_false
;
257 void __set_state(struct sm_state
*sm
);
258 struct state_list
*__get_cur_slist();
259 void __set_true_false_sm(struct sm_state
*true_state
,
260 struct sm_state
*false_state
);
262 void __match_nullify_path_hook(const char *fn
, struct expression
*expr
,
264 void __unnullify_path();
265 int __path_is_null();
266 void clear_all_states();
268 struct sm_state
*get_sm_state(int owner
, const char *name
,
270 struct sm_state
*get_sm_state_expr(int owner
, struct expression
*expr
);
271 void __use_false_only_stack();
272 void __pop_false_only_stack();
273 void __push_true_states();
274 void __use_false_states();
275 void __pop_false_states();
276 void __merge_false_states();
277 void __merge_true_states();
279 void __negate_cond_stacks();
280 void __save_false_states_for_later();
281 void __use_previously_stored_false_states();
282 void __use_cond_true_states();
283 void __use_cond_false_states();
284 void __push_cond_stacks();
285 void __and_cond_states();
286 void __or_cond_states();
287 void __save_pre_cond_states();
288 void __pop_pre_cond_states();
289 void __use_cond_states();
291 void __warn_on_silly_pre_loops();
293 void __push_continues();
294 void __pop_continues();
295 void __process_continues();
296 void __merge_continues();
298 void __push_breaks();
299 void __process_breaks();
300 void __merge_breaks();
303 void __save_switch_states(struct expression
*switch_expr
);
304 void __pop_switches();
305 void __merge_switches(struct expression
*switch_expr
, struct expression
*case_expr
);
306 void __push_default();
307 void __set_default();
310 void __push_conditions();
311 void __pop_conditions();
313 void __save_gotos(const char *name
);
314 void __merge_gotos(const char *name
);
316 void __print_cur_slist();
319 void __pass_to_client(void *data
, enum hook_type type
);
320 void __pass_to_client_no_data(enum hook_type type
);
321 void __pass_case_to_client(struct expression
*switch_expr
,
322 struct expression
*case_expr
);
323 int __has_merge_function(int client_id
);
324 struct smatch_state
*__client_merge_function(int owner
, const char *name
,
326 struct smatch_state
*s1
,
327 struct smatch_state
*s2
);
328 struct smatch_state
*__client_unmatched_state_function(struct sm_state
*sm
);
329 void __push_scope_hooks(void);
330 void __call_scope_hooks(void);
332 /* smatch_function_hooks.c */
333 void create_function_hash(void);
334 void __match_initializer_call(struct symbol
*sym
);
337 struct token
*get_tokens_file(const char *filename
);
340 extern int option_oom_kb
;
344 extern char *data_dir
;
345 extern int option_no_data
;
346 extern int option_spammy
;
347 extern struct smatch_state
*default_state
[];
349 #endif /* !SMATCH_H_ */