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
)
46 unsigned int merged
:1;
47 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 void add_function_hook(const char *lock_for
, func_hook
*call_back
, void *data
);
98 void add_conditional_hook(const char *look_for
, func_hook
*call_back
, void *data
);
99 void add_function_assign_hook(const char *look_for
, func_hook
*call_back
,
101 void return_implies_state(const char *look_for
, long long start
, long long end
,
102 func_hook
*call_back
, void *info
);
104 #define smatch_msg(msg...) \
106 printf("%s +%d %s(%d) ", get_filename(), get_lineno(), \
107 get_function(), get_func_pos()); \
112 #define SM_DEBUG(msg...) do { if (debug_states) printf(msg); } while (0)
113 #define DIMPLIED(msg...) do { if (debug_implied_states) printf(msg); } while (0)
115 #define UNDEFINED INT_MIN
117 struct smatch_state
*get_state(const char *name
, int owner
, struct symbol
*sym
);
118 struct state_list
*get_possible_states(const char *name
, int owner
,
120 void set_state(const char *name
, int owner
, struct symbol
*sym
,
121 struct smatch_state
*state
);
122 void delete_state(const char *name
, int owner
, struct symbol
*sym
);
123 void set_true_false_states(const char *name
, int owner
, struct symbol
*sym
,
124 struct smatch_state
*true_state
,
125 struct smatch_state
*false_state
);
127 struct state_list
*get_all_states(int id
);
129 char *get_filename();
130 char *get_function();
134 /* smatch_helper.c */
135 char *alloc_string(const char *str
);
136 void free_string(char *str
);
137 struct expression
*get_argument_from_call_expr(struct expression_list
*args
,
139 char *get_variable_from_expr_complex(struct expression
*expr
,
140 struct symbol
**sym_ptr
);
141 char *get_variable_from_expr(struct expression
*expr
,
142 struct symbol
**sym_ptr
);
143 struct symbol
*get_ptr_type(struct expression
*expr
);
144 int sym_name_is(const char *name
, struct expression
*expr
);
145 int get_value(struct expression
*expr
);
146 int is_zero(struct expression
*expr
);
147 const char *show_state(struct smatch_state
*state
);
148 struct statement
*get_block_thing(struct expression
*expr
);
149 struct expression
*strip_expr(struct expression
*expr
);
150 void scoped_state(const char *name
, int my_id
, struct symbol
*sym
);
152 /* smatch_ignore.c */
153 void add_ignore(const char *name
, int owner
, struct symbol
*sym
);
154 int is_ignored(const char *name
, int owner
, struct symbol
*sym
);
157 struct tracker
*alloc_tracker(const char *name
, int owner
, struct symbol
*sym
);
158 void add_tracker(struct tracker_list
**list
, const char *name
, int owner
,
160 void del_tracker(struct tracker_list
**list
, const char *name
, int owner
,
162 int in_tracker_list(struct tracker_list
*list
, const char *name
, int owner
,
164 void free_tracker_list(struct tracker_list
**list
);
165 void free_trackers_and_list(struct tracker_list
**list
);
167 /* smatch_conditions */
170 /* ----------------------------------------------------------------
171 The stuff below is all used internally and shouldn't
172 be called from other programs
173 -----------------------------------------------------------------*/
177 void smatch (int argc
, char **argv
);
178 void __split_expr(struct expression
*expr
);
179 void __split_statements(struct statement
*stmt
);
180 extern int option_assume_loops
;
181 extern int option_known_conditions
;
183 /* smatch_conditions */
184 void __split_whole_condition(struct expression
*expr
);
186 /* smatch_implied.c */
187 extern int debug_implied_states
;
188 extern int option_no_implied
;
189 void get_implications(char *name
, struct symbol
*sym
, int comparison
, int num
,
190 struct state_list
**true_states
,
191 struct state_list
**false_states
);
192 struct range_list_stack
;
193 struct state_list
*__implied_case_slist(struct expression
*switch_expr
,
194 struct expression
*case_expr
,
195 struct range_list_stack
**remaining_cases
,
196 struct state_list
**raw_slist
);
197 struct range_list
*__get_implied_values(struct expression
*switch_expr
);
199 /* smatch_extras.c */
200 #define SMATCH_EXTRA 1 /* this is my_id from smatch extra set in smatch.c */
206 extern struct data_range whole_range
;
208 int get_implied_value(struct expression
*expr
);
209 int true_comparison(int left
, int comparison
, int right
);
210 int known_condition_true(struct expression
*expr
);
211 int known_condition_false(struct expression
*expr
);
212 int implied_condition_true(struct expression
*expr
);
213 int implied_condition_false(struct expression
*expr
);
215 /* smatch_states.c */
216 extern int debug_states
;
218 extern int __fake_cur
;
219 extern struct state_list
*__fake_cur_slist
;
220 extern int __fake_conditions
;
221 extern struct state_list
*__fake_cond_true
;
222 extern struct state_list
*__fake_cond_false
;
225 void __set_state(struct sm_state
*sm
);
226 struct state_list
*__get_cur_slist();
227 void __set_true_false_sm(struct sm_state
*true_state
,
228 struct sm_state
*false_state
);
230 void __match_nullify_path_hook(const char *fn
, struct expression
*expr
,
232 void __unnullify_path();
233 int __path_is_null();
234 void clear_all_states();
236 struct sm_state
*get_sm_state(const char *name
, int owner
,
238 void __use_false_only_stack();
239 void __pop_false_only_stack();
240 void __push_true_states();
241 void __use_false_states();
242 void __pop_false_states();
243 void __merge_false_states();
244 void __merge_true_states();
246 void __negate_cond_stacks();
247 void __save_false_states_for_later();
248 void __use_previously_stored_false_states();
249 void __use_cond_true_states();
250 void __use_cond_false_states();
251 void __push_cond_stacks();
252 void __and_cond_states();
253 void __or_cond_states();
254 void __save_pre_cond_states();
255 void __pop_pre_cond_states();
256 void __use_cond_states();
258 void __warn_on_silly_pre_loops();
260 void __push_continues();
261 void __pop_continues();
262 void __process_continues();
263 void __merge_continues();
265 void __push_breaks();
266 void __process_breaks();
267 void __merge_breaks();
270 void __save_switch_states(struct expression
*switch_expr
);
271 void __pop_switches();
272 void __merge_switches(struct expression
*switch_expr
, struct expression
*case_expr
);
273 void __push_default();
274 void __set_default();
277 void __push_conditions();
278 void __pop_conditions();
280 void __save_gotos(const char *name
);
281 void __merge_gotos(const char *name
);
283 void __print_cur_slist();
286 void __pass_to_client(void *data
, enum hook_type type
);
287 void __pass_to_client_no_data(enum hook_type type
);
288 void __pass_case_to_client(struct expression
*switch_expr
,
289 struct expression
*case_expr
);
290 int __has_merge_function(int client_id
);
291 struct smatch_state
*__client_merge_function(int owner
, const char *name
,
293 struct smatch_state
*s1
,
294 struct smatch_state
*s2
);
295 struct smatch_state
*__client_unmatched_state_function(struct sm_state
*sm
);
296 void __push_scope_hooks(void);
297 void __call_scope_hooks(void);
299 /* smatch_function_hooks.c */
302 struct data_range
*range
;
303 func_hook
*call_back
;
306 DECLARE_ALLOCATOR(fcall_back
);
307 DECLARE_PTR_LIST(call_back_list
, struct fcall_back
);
308 void create_function_hash(void);
309 void __match_initializer_call(struct symbol
*sym
);
312 struct token
*get_tokens_file(const char *filename
);
315 extern int option_oom_kb
;
319 extern char *data_dir
;
320 extern int option_no_data
;
322 #endif /* !SMATCH_H_ */