4 * Copyright (C) 2006 Dan Carpenter.
6 * Licensed under the Open Software License version 1.1
19 #include "expression.h"
22 #define MAXSMSTATES 1000000 /* Limits RAM use. Give up on big functions */
27 #define STATE(_x) static struct smatch_state _x = { .name = #_x }
28 extern struct smatch_state undefined
;
29 extern struct smatch_state merged
;
30 extern struct smatch_state true_state
;
31 extern struct smatch_state false_state
;
32 DECLARE_ALLOCATOR(smatch_state
);
34 static inline void *INT_PTR(int i
)
36 return (void *)(long)i
;
39 static inline int PTR_INT(void *p
)
48 struct smatch_state
*state
;
50 struct state_list_stack
*my_pools
;
51 struct state_list_stack
*all_pools
;
52 struct state_list
*possible
;
74 void add_hook(void *func
, enum hook_type type
);
75 typedef struct smatch_state
*(merge_func_t
)(const char *name
,
77 struct smatch_state
*s1
,
78 struct smatch_state
*s2
);
79 typedef struct smatch_state
*(unmatched_func_t
)(struct sm_state
*state
);
80 void add_merge_hook(int client_id
, merge_func_t
*func
);
81 void add_unmatched_state_hook(int client_id
, unmatched_func_t
*func
);
82 typedef void (func_hook
)(const char *fn
, struct expression
*expr
, void *data
);
83 void add_function_hook(const char *lock_for
, func_hook
*call_back
, void *data
);
85 void add_conditional_hook(const char *look_for
, func_hook
*call_back
, void *data
);
86 void add_function_assign_hook(const char *look_for
, func_hook
*call_back
,
89 #define smatch_msg(msg...) \
91 printf("%s +%d %s(%d) ", get_filename(), get_lineno(), \
92 get_function(), get_func_pos()); \
97 #define SM_DEBUG(msg...) do { if (debug_states) printf(msg); } while (0)
98 #define DIMPLIED(msg...) do { if (debug_implied_states) printf(msg); } while (0)
100 #define UNDEFINED INT_MIN
102 struct smatch_state
*get_state(const char *name
, int owner
, struct symbol
*sym
);
103 struct state_list
*get_possible_states(const char *name
, int owner
,
105 void set_state(const char *name
, int owner
, struct symbol
*sym
,
106 struct smatch_state
*state
);
107 void delete_state(const char *name
, int owner
, struct symbol
*sym
);
108 void set_true_false_states(const char *name
, int owner
, struct symbol
*sym
,
109 struct smatch_state
*true_state
,
110 struct smatch_state
*false_state
);
112 struct state_list
*get_all_states(int id
);
114 char *get_filename();
115 char *get_function();
119 /* smatch_helper.c */
120 char *alloc_string(const char *str
);
121 void free_string(char *str
);
122 struct expression
*get_argument_from_call_expr(struct expression_list
*args
,
124 char *get_variable_from_expr_complex(struct expression
*expr
,
125 struct symbol
**sym_ptr
);
126 char *get_variable_from_expr(struct expression
*expr
,
127 struct symbol
**sym_ptr
);
128 struct symbol
*get_ptr_type(struct expression
*expr
);
129 int sym_name_is(const char *name
, struct expression
*expr
);
130 int get_value(struct expression
*expr
);
131 int is_zero(struct expression
*expr
);
132 const char *show_state(struct smatch_state
*state
);
133 struct statement
*get_block_thing(struct expression
*expr
);
134 struct expression
*strip_expr(struct expression
*expr
);
136 /* smatch_ignore.c */
142 DECLARE_PTR_LIST(tracker_list
, struct tracker
);
144 void add_ignore(const char *name
, int owner
, struct symbol
*sym
);
145 int is_ignored(const char *name
, int owner
, struct symbol
*sym
);
148 void add_tracker(struct tracker_list
**list
, const char *name
, int owner
,
150 void del_tracker(struct tracker_list
**list
, const char *name
, int owner
,
152 int in_tracker_list(struct tracker_list
*list
, const char *name
, int owner
,
154 void free_tracker_list(struct tracker_list
**list
);
155 void free_trackers_and_list(struct tracker_list
**list
);
157 /* smatch_conditions */
160 /* ----------------------------------------------------------------
161 The stuff below is all used internally and shouldn't
162 be called from other programs
163 -----------------------------------------------------------------*/
167 void smatch (int argc
, char **argv
);
168 void __split_expr(struct expression
*expr
);
169 void __split_statements(struct statement
*stmt
);
170 extern int option_assume_loops
;
171 extern int option_known_conditions
;
173 /* smatch_conditions */
174 void __split_whole_condition(struct expression
*expr
);
176 /* smatch_implied.c */
177 extern int debug_implied_states
;
178 extern int option_no_implied
;
179 void get_implications(char *name
, struct symbol
*sym
, int comparison
, int num
,
180 struct state_list
**true_states
,
181 struct state_list
**false_states
);
183 /* smatch_extras.c */
184 #define SMATCH_EXTRA 1 /* this is my_id from smatch extra set in smatch.c */
185 int get_implied_value(struct expression
*expr
);
186 int true_comparison(int left
, int comparison
, int right
);
187 int known_condition_true(struct expression
*expr
);
188 int known_condition_false(struct expression
*expr
);
189 int implied_condition_true(struct expression
*expr
);
190 int implied_condition_false(struct expression
*expr
);
192 /* smatch_states.c */
193 extern int debug_states
;
194 extern int __fake_conditions
;
195 extern struct state_list
*__fake_cond_true
;
196 extern struct state_list
*__fake_cond_false
;
199 void __set_state(struct sm_state
*sm
);
200 struct state_list
*__get_cur_slist();
201 void __set_true_false_sm(struct sm_state
*true_state
,
202 struct sm_state
*false_state
);
204 void __match_nullify_path_hook(const char *fn
, struct expression
*expr
,
206 void __unnullify_path();
207 int __path_is_null();
208 void clear_all_states();
210 struct sm_state
*get_sm_state(const char *name
, int owner
,
212 void __use_false_only_stack();
213 void __pop_false_only_stack();
214 void __push_true_states();
215 void __use_false_states();
216 void __pop_false_states();
217 void __merge_false_states();
218 void __merge_true_states();
220 void __negate_cond_stacks();
221 void __save_false_states_for_later();
222 void __use_previously_stored_false_states();
223 void __use_cond_true_states();
224 void __use_cond_false_states();
225 void __push_cond_stacks();
226 void __and_cond_states();
227 void __or_cond_states();
228 void __save_pre_cond_states();
229 void __pop_pre_cond_states();
230 void __use_cond_states();
232 void __warn_on_silly_pre_loops();
234 void __push_continues();
235 void __pop_continues();
236 void __process_continues();
237 void __merge_continues();
239 void __push_breaks();
240 void __process_breaks();
241 void __merge_breaks();
244 void __save_switch_states();
245 void __pop_switches();
246 void __merge_switches();
247 void __push_default();
248 void __set_default();
251 void __push_conditions();
252 void __pop_conditions();
254 void __save_gotos(const char *name
);
255 void __merge_gotos(const char *name
);
257 void __print_cur_slist();
260 void __pass_to_client(void *data
, enum hook_type type
);
261 void __pass_to_client_no_data(enum hook_type type
);
262 void __pass_case_to_client(struct expression
*switch_expr
,
263 struct expression
*case_expr
);
264 int __has_merge_function(int client_id
);
265 struct smatch_state
*__client_merge_function(int owner
, const char *name
,
267 struct smatch_state
*s1
,
268 struct smatch_state
*s2
);
269 struct smatch_state
*__client_unmatched_state_function(struct sm_state
*sm
);
271 /* smatch_function_hooks.c */
274 func_hook
*call_back
;
277 DECLARE_ALLOCATOR(fcall_back
);
278 DECLARE_PTR_LIST(call_back_list
, struct fcall_back
);
279 void create_function_hash(void);
280 void __match_initializer_call(struct symbol
*sym
);
283 struct token
*get_tokens_file(const char *filename
);
286 extern char *data_dir
;
287 extern int option_no_data
;
289 #endif /* !SMATCH_H_ */