code churn: rename ->pre_left => ->left, ->pre_right => ->right
[smatch.git] / smatch.h
blob5c5f47b90b8a8d6afe426721204c9b0123712ae0
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 struct smatch_state {
23 const char *name;
24 void *data;
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)
40 return (int)(long)p;
43 struct sm_state {
44 const char *name;
45 unsigned short owner;
46 unsigned int merged:1;
47 unsigned int implied:1;
48 struct symbol *sym;
49 struct smatch_state *state;
50 unsigned int line;
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;
58 enum hook_type {
59 EXPR_HOOK,
60 STMT_HOOK,
61 SYM_HOOK,
62 DECLARATION_HOOK,
63 ASSIGNMENT_HOOK,
64 CONDITION_HOOK,
65 WHOLE_CONDITION_HOOK,
66 FUNCTION_CALL_HOOK,
67 CALL_ASSIGNMENT_HOOK,
68 OP_HOOK,
69 DEREF_HOOK,
70 CASE_HOOK,
71 BASE_HOOK,
72 FUNC_DEF_HOOK,
73 END_FUNC_HOOK,
74 RETURN_HOOK,
75 END_FILE_HOOK,
77 void add_hook(void *func, enum hook_type type);
78 typedef struct smatch_state *(merge_func_t)(const char *name,
79 struct symbol *sym,
80 struct smatch_state *s1,
81 struct smatch_state *s2);
82 typedef struct smatch_state *(unmatched_func_t)(struct sm_state *state);
83 void add_merge_hook(int client_id, merge_func_t *func);
84 void add_unmatched_state_hook(int client_id, unmatched_func_t *func);
85 typedef void (func_hook)(const char *fn, struct expression *expr, void *data);
86 void add_function_hook(const char *lock_for, func_hook *call_back, void *data);
88 void add_conditional_hook(const char *look_for, func_hook *call_back, void *data);
89 void add_function_assign_hook(const char *look_for, func_hook *call_back,
90 void *info);
91 void return_implies_state(const char *look_for, long long start, long long end,
92 func_hook *call_back, void *info);
94 #define smatch_msg(msg...) \
95 do { \
96 printf("%s +%d %s(%d) ", get_filename(), get_lineno(), \
97 get_function(), get_func_pos()); \
98 printf(msg); \
99 printf("\n"); \
100 } while (0)
102 #define SM_DEBUG(msg...) do { if (debug_states) printf(msg); } while (0)
103 #define DIMPLIED(msg...) do { if (debug_implied_states) printf(msg); } while (0)
105 #define UNDEFINED INT_MIN
107 struct smatch_state *get_state(const char *name, int owner, struct symbol *sym);
108 struct state_list *get_possible_states(const char *name, int owner,
109 struct symbol *sym);
110 void set_state(const char *name, int owner, struct symbol *sym,
111 struct smatch_state *state);
112 void delete_state(const char *name, int owner, struct symbol *sym);
113 void set_true_false_states(const char *name, int owner, struct symbol *sym,
114 struct smatch_state *true_state,
115 struct smatch_state *false_state);
117 struct state_list *get_all_states(int id);
118 int is_reachable();
119 char *get_filename();
120 char *get_function();
121 int get_lineno();
122 int get_func_pos();
124 /* smatch_helper.c */
125 char *alloc_string(const char *str);
126 void free_string(char *str);
127 struct expression *get_argument_from_call_expr(struct expression_list *args,
128 int num);
129 char *get_variable_from_expr_complex(struct expression *expr,
130 struct symbol **sym_ptr);
131 char *get_variable_from_expr(struct expression *expr,
132 struct symbol **sym_ptr);
133 struct symbol *get_ptr_type(struct expression *expr);
134 int sym_name_is(const char *name, struct expression *expr);
135 int get_value(struct expression *expr);
136 int is_zero(struct expression *expr);
137 const char *show_state(struct smatch_state *state);
138 struct statement *get_block_thing(struct expression *expr);
139 struct expression *strip_expr(struct expression *expr);
141 /* smatch_ignore.c */
142 struct tracker {
143 char *name;
144 int owner;
145 struct symbol *sym;
147 DECLARE_PTR_LIST(tracker_list, struct tracker);
149 void add_ignore(const char *name, int owner, struct symbol *sym);
150 int is_ignored(const char *name, int owner, struct symbol *sym);
152 /* smatch_tracker */
153 void add_tracker(struct tracker_list **list, const char *name, int owner,
154 struct symbol *sym);
155 void del_tracker(struct tracker_list **list, const char *name, int owner,
156 struct symbol *sym);
157 int in_tracker_list(struct tracker_list *list, const char *name, int owner,
158 struct symbol *sym);
159 void free_tracker_list(struct tracker_list **list);
160 void free_trackers_and_list(struct tracker_list **list);
162 /* smatch_conditions */
163 int in_condition();
165 /* ----------------------------------------------------------------
166 The stuff below is all used internally and shouldn't
167 be called from other programs
168 -----------------------------------------------------------------*/
170 /* smatch_flow.c */
172 void smatch (int argc, char **argv);
173 void __split_expr(struct expression *expr);
174 void __split_statements(struct statement *stmt);
175 extern int option_assume_loops;
176 extern int option_known_conditions;
178 /* smatch_conditions */
179 void __split_whole_condition(struct expression *expr);
181 /* smatch_implied.c */
182 extern int debug_implied_states;
183 extern int option_no_implied;
184 void get_implications(char *name, struct symbol *sym, int comparison, int num,
185 struct state_list **true_states,
186 struct state_list **false_states);
187 struct state_list *__implied_case_slist(struct expression *switch_expr,
188 struct expression *case_expr,
189 struct state_list **raw_slist);
191 /* smatch_extras.c */
192 #define SMATCH_EXTRA 1 /* this is my_id from smatch extra set in smatch.c */
194 struct data_range {
195 long long min;
196 long long max;
198 extern struct data_range whole_range;
200 int get_implied_value(struct expression *expr);
201 int true_comparison(int left, int comparison, int right);
202 int known_condition_true(struct expression *expr);
203 int known_condition_false(struct expression *expr);
204 int implied_condition_true(struct expression *expr);
205 int implied_condition_false(struct expression *expr);
207 /* smatch_states.c */
208 extern int debug_states;
210 extern int __fake_cur;
211 extern struct state_list *__fake_cur_slist;
212 extern int __fake_conditions;
213 extern struct state_list *__fake_cond_true;
214 extern struct state_list *__fake_cond_false;
217 void __set_state(struct sm_state *sm);
218 struct state_list *__get_cur_slist();
219 void __set_true_false_sm(struct sm_state *true_state,
220 struct sm_state *false_state);
221 void nullify_path();
222 void __match_nullify_path_hook(const char *fn, struct expression *expr,
223 void *unused);
224 void __unnullify_path();
225 int __path_is_null();
226 void clear_all_states();
228 struct sm_state *get_sm_state(const char *name, int owner,
229 struct symbol *sym);
230 void __use_false_only_stack();
231 void __pop_false_only_stack();
232 void __push_true_states();
233 void __use_false_states();
234 void __pop_false_states();
235 void __merge_false_states();
236 void __merge_true_states();
238 void __negate_cond_stacks();
239 void __save_false_states_for_later();
240 void __use_previously_stored_false_states();
241 void __use_cond_true_states();
242 void __use_cond_false_states();
243 void __push_cond_stacks();
244 void __and_cond_states();
245 void __or_cond_states();
246 void __save_pre_cond_states();
247 void __pop_pre_cond_states();
248 void __use_cond_states();
250 void __warn_on_silly_pre_loops();
252 void __push_continues();
253 void __pop_continues();
254 void __process_continues();
255 void __merge_continues();
257 void __push_breaks();
258 void __process_breaks();
259 void __merge_breaks();
260 void __use_breaks();
262 void __save_switch_states();
263 void __pop_switches();
264 void __merge_switches(struct expression *switch_expr, struct expression *case_expr);
265 void __push_default();
266 void __set_default();
267 int __pop_default();
269 void __push_conditions();
270 void __pop_conditions();
272 void __save_gotos(const char *name);
273 void __merge_gotos(const char *name);
275 void __print_cur_slist();
277 /* smatch_hooks.c */
278 void __pass_to_client(void *data, enum hook_type type);
279 void __pass_to_client_no_data(enum hook_type type);
280 void __pass_case_to_client(struct expression *switch_expr,
281 struct expression *case_expr);
282 int __has_merge_function(int client_id);
283 struct smatch_state *__client_merge_function(int owner, const char *name,
284 struct symbol *sym,
285 struct smatch_state *s1,
286 struct smatch_state *s2);
287 struct smatch_state *__client_unmatched_state_function(struct sm_state *sm);
289 /* smatch_function_hooks.c */
290 struct fcall_back {
291 int type;
292 struct data_range *range;
293 func_hook *call_back;
294 void *info;
296 DECLARE_ALLOCATOR(fcall_back);
297 DECLARE_PTR_LIST(call_back_list, struct fcall_back);
298 void create_function_hash(void);
299 void __match_initializer_call(struct symbol *sym);
301 /* smatch_files.c */
302 struct token *get_tokens_file(const char *filename);
304 /* smatch_oom.c */
305 extern int option_oom_kb;
306 int out_of_memory();
308 /* smatch.c */
309 extern char *data_dir;
310 extern int option_no_data;
312 #endif /* !SMATCH_H_ */