add_is_merged_hook()
[smatch.git] / smatch.h
blob566ac4afa93db0ec94800d1205756a97a74ffbf9
1 /*
2 * sparse/smatch.h
4 * Copyright (C) 2006 Dan Carpenter.
6 * Licensed under the Open Software License version 1.1
8 */
10 #ifndef SMATCH_H_
11 # define SMATCH_H_
13 #include <stdio.h>
14 #include <string.h>
15 #include <limits.h>
16 #include "lib.h"
17 #include "allocate.h"
18 #include "parse.h"
19 #include "expression.h"
21 #define KERNEL
22 #define MAXSMSTATES 1000000 /* Limits RAM use. Give up on big functions */
23 struct smatch_state {
24 const char *name;
25 void *data;
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)
41 return (int)(long)p;
44 struct sm_state {
45 const char *name;
46 unsigned int owner;
47 struct symbol *sym;
48 struct smatch_state *state;
49 int line;
50 struct state_list_stack *my_pools;
51 struct state_list_stack *all_pools;
52 struct state_list *possible;
55 enum hook_type {
56 EXPR_HOOK,
57 STMT_HOOK,
58 SYM_HOOK,
59 DECLARATION_HOOK,
60 ASSIGNMENT_HOOK,
61 CONDITION_HOOK,
62 WHOLE_CONDITION_HOOK,
63 FUNCTION_CALL_HOOK,
64 CALL_ASSIGNMENT_HOOK,
65 OP_HOOK,
66 DEREF_HOOK,
67 CASE_HOOK,
68 BASE_HOOK,
69 FUNC_DEF_HOOK,
70 END_FUNC_HOOK,
71 RETURN_HOOK,
72 END_FILE_HOOK,
74 void add_hook(void *func, enum hook_type type);
75 typedef struct smatch_state *(merge_func_t)(const char *name,
76 struct symbol *sym,
77 struct smatch_state *s1,
78 struct smatch_state *s2);
79 typedef int (is_merged_func_t)(struct smatch_state *state);
80 typedef struct smatch_state *(unmatched_func_t)(struct sm_state *state);
81 void add_merge_hook(int client_id, merge_func_t *func);
82 void add_unmatched_state_hook(int client_id, unmatched_func_t *func);
83 void add_is_merged_hook(int client_id, is_merged_func_t *func);
84 typedef void (func_hook)(const char *fn, struct expression *expr, void *data);
85 void add_function_hook(const char *lock_for, func_hook *call_back, void *data);
87 void add_conditional_hook(const char *look_for, func_hook *call_back, void *data);
88 void add_function_assign_hook(const char *look_for, func_hook *call_back,
89 void *info);
90 void return_implies_state(const char *look_for, long long start, long long end,
91 func_hook *call_back, void *info);
93 #define smatch_msg(msg...) \
94 do { \
95 printf("%s +%d %s(%d) ", get_filename(), get_lineno(), \
96 get_function(), get_func_pos()); \
97 printf(msg); \
98 printf("\n"); \
99 } while (0)
101 #define SM_DEBUG(msg...) do { if (debug_states) printf(msg); } while (0)
102 #define DIMPLIED(msg...) do { if (debug_implied_states) printf(msg); } while (0)
104 #define UNDEFINED INT_MIN
106 struct smatch_state *get_state(const char *name, int owner, struct symbol *sym);
107 struct state_list *get_possible_states(const char *name, int owner,
108 struct symbol *sym);
109 void set_state(const char *name, int owner, struct symbol *sym,
110 struct smatch_state *state);
111 void delete_state(const char *name, int owner, struct symbol *sym);
112 void set_true_false_states(const char *name, int owner, struct symbol *sym,
113 struct smatch_state *true_state,
114 struct smatch_state *false_state);
116 struct state_list *get_all_states(int id);
117 int is_reachable();
118 char *get_filename();
119 char *get_function();
120 int get_lineno();
121 int get_func_pos();
123 /* smatch_helper.c */
124 char *alloc_string(const char *str);
125 void free_string(char *str);
126 struct expression *get_argument_from_call_expr(struct expression_list *args,
127 int num);
128 char *get_variable_from_expr_complex(struct expression *expr,
129 struct symbol **sym_ptr);
130 char *get_variable_from_expr(struct expression *expr,
131 struct symbol **sym_ptr);
132 struct symbol *get_ptr_type(struct expression *expr);
133 int sym_name_is(const char *name, struct expression *expr);
134 int get_value(struct expression *expr);
135 int is_zero(struct expression *expr);
136 const char *show_state(struct smatch_state *state);
137 struct statement *get_block_thing(struct expression *expr);
138 struct expression *strip_expr(struct expression *expr);
140 /* smatch_ignore.c */
141 struct tracker {
142 char *name;
143 int owner;
144 struct symbol *sym;
146 DECLARE_PTR_LIST(tracker_list, struct tracker);
148 void add_ignore(const char *name, int owner, struct symbol *sym);
149 int is_ignored(const char *name, int owner, struct symbol *sym);
151 /* smatch_tracker */
152 void add_tracker(struct tracker_list **list, const char *name, int owner,
153 struct symbol *sym);
154 void del_tracker(struct tracker_list **list, const char *name, int owner,
155 struct symbol *sym);
156 int in_tracker_list(struct tracker_list *list, const char *name, int owner,
157 struct symbol *sym);
158 void free_tracker_list(struct tracker_list **list);
159 void free_trackers_and_list(struct tracker_list **list);
161 /* smatch_conditions */
162 int in_condition();
164 /* ----------------------------------------------------------------
165 The stuff below is all used internally and shouldn't
166 be called from other programs
167 -----------------------------------------------------------------*/
169 /* smatch_flow.c */
171 void smatch (int argc, char **argv);
172 void __split_expr(struct expression *expr);
173 void __split_statements(struct statement *stmt);
174 extern int option_assume_loops;
175 extern int option_known_conditions;
177 /* smatch_conditions */
178 void __split_whole_condition(struct expression *expr);
180 /* smatch_implied.c */
181 extern int debug_implied_states;
182 extern int option_no_implied;
183 void get_implications(char *name, struct symbol *sym, int comparison, int num,
184 struct state_list **true_states,
185 struct state_list **false_states);
187 /* smatch_extras.c */
188 #define SMATCH_EXTRA 1 /* this is my_id from smatch extra set in smatch.c */
190 struct data_range {
191 long long min;
192 long long max;
194 extern struct data_range whole_range;
196 int get_implied_value(struct expression *expr);
197 int true_comparison(int left, int comparison, int right);
198 int known_condition_true(struct expression *expr);
199 int known_condition_false(struct expression *expr);
200 int implied_condition_true(struct expression *expr);
201 int implied_condition_false(struct expression *expr);
203 /* smatch_states.c */
204 extern int debug_states;
206 extern int __fake_cur;
207 extern struct state_list *__fake_cur_slist;
208 extern int __fake_conditions;
209 extern struct state_list *__fake_cond_true;
210 extern struct state_list *__fake_cond_false;
213 void __set_state(struct sm_state *sm);
214 struct state_list *__get_cur_slist();
215 void __set_true_false_sm(struct sm_state *true_state,
216 struct sm_state *false_state);
217 void nullify_path();
218 void __match_nullify_path_hook(const char *fn, struct expression *expr,
219 void *unused);
220 void __unnullify_path();
221 int __path_is_null();
222 void clear_all_states();
224 struct sm_state *get_sm_state(const char *name, int owner,
225 struct symbol *sym);
226 void __use_false_only_stack();
227 void __pop_false_only_stack();
228 void __push_true_states();
229 void __use_false_states();
230 void __pop_false_states();
231 void __merge_false_states();
232 void __merge_true_states();
234 void __negate_cond_stacks();
235 void __save_false_states_for_later();
236 void __use_previously_stored_false_states();
237 void __use_cond_true_states();
238 void __use_cond_false_states();
239 void __push_cond_stacks();
240 void __and_cond_states();
241 void __or_cond_states();
242 void __save_pre_cond_states();
243 void __pop_pre_cond_states();
244 void __use_cond_states();
246 void __warn_on_silly_pre_loops();
248 void __push_continues();
249 void __pop_continues();
250 void __process_continues();
251 void __merge_continues();
253 void __push_breaks();
254 void __process_breaks();
255 void __merge_breaks();
256 void __use_breaks();
258 void __save_switch_states();
259 void __pop_switches();
260 void __merge_switches();
261 void __push_default();
262 void __set_default();
263 int __pop_default();
265 void __push_conditions();
266 void __pop_conditions();
268 void __save_gotos(const char *name);
269 void __merge_gotos(const char *name);
271 void __print_cur_slist();
273 /* smatch_hooks.c */
274 void __pass_to_client(void *data, enum hook_type type);
275 void __pass_to_client_no_data(enum hook_type type);
276 void __pass_case_to_client(struct expression *switch_expr,
277 struct expression *case_expr);
278 int __has_merge_function(int client_id);
279 struct smatch_state *__client_merge_function(int owner, const char *name,
280 struct symbol *sym,
281 struct smatch_state *s1,
282 struct smatch_state *s2);
283 int __is_merged(struct sm_state *sm);
284 struct smatch_state *__client_unmatched_state_function(struct sm_state *sm);
286 /* smatch_function_hooks.c */
287 struct fcall_back {
288 int type;
289 struct data_range *range;
290 func_hook *call_back;
291 void *info;
293 DECLARE_ALLOCATOR(fcall_back);
294 DECLARE_PTR_LIST(call_back_list, struct fcall_back);
295 void create_function_hash(void);
296 void __match_initializer_call(struct symbol *sym);
298 /* smatch_files.c */
299 struct token *get_tokens_file(const char *filename);
301 /* smatch.c */
302 extern char *data_dir;
303 extern int option_no_data;
305 #endif /* !SMATCH_H_ */