Don't free bin_dir.
[smatch.git] / smatch.h
blob7890a0048c7018cd13e4c47e5338bee954edf29d
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 struct sm_state {
35 const char *name;
36 unsigned int owner;
37 struct symbol *sym;
38 struct smatch_state *state;
39 int line;
40 struct state_list_stack *my_pools;
41 struct state_list_stack *all_pools;
42 struct state_list *possible;
45 enum hook_type {
46 EXPR_HOOK,
47 STMT_HOOK,
48 SYM_HOOK,
49 DECLARATION_HOOK,
50 ASSIGNMENT_HOOK,
51 CONDITION_HOOK,
52 WHOLE_CONDITION_HOOK,
53 FUNCTION_CALL_HOOK,
54 CALL_ASSIGNMENT_HOOK,
55 OP_HOOK,
56 DEREF_HOOK,
57 CASE_HOOK,
58 BASE_HOOK,
59 FUNC_DEF_HOOK,
60 END_FUNC_HOOK,
61 RETURN_HOOK,
62 END_FILE_HOOK,
64 void add_hook(void *func, enum hook_type type);
65 typedef struct smatch_state *(merge_func_t)(const char *name,
66 struct symbol *sym,
67 struct smatch_state *s1,
68 struct smatch_state *s2);
69 typedef struct smatch_state *(unmatched_func_t)(struct sm_state *state);
70 void add_merge_hook(int client_id, merge_func_t *func);
71 void add_unmatched_state_hook(int client_id, unmatched_func_t *func);
72 typedef void (func_hook)(const char *fn, struct expression *expr, void *data);
73 void add_function_hook(const char *lock_for, func_hook *call_back, void *data);
75 void add_conditional_hook(const char *look_for, func_hook *call_back, void *data);
76 void set_cond_states(const char *name, int owner, struct symbol *sym,
77 struct smatch_state *true_state,
78 struct smatch_state *false_state);
79 void add_function_assign_hook(const char *look_for, func_hook *call_back,
80 void *info);
82 #define smatch_msg(msg...) \
83 do { \
84 printf("%s +%d %s(%d) ", get_filename(), get_lineno(), \
85 get_function(), get_func_pos()); \
86 printf(msg); \
87 printf("\n"); \
88 } while (0)
90 #define SM_DEBUG(msg...) do { if (debug_states) printf(msg); } while (0)
91 #define DIMPLIED(msg...) do { if (debug_implied_states) printf(msg); } while (0)
93 #define UNDEFINED INT_MIN
95 struct smatch_state *get_state(const char *name, int owner, struct symbol *sym);
96 struct state_list *get_possible_states(const char *name, int owner,
97 struct symbol *sym);
98 void set_state(const char *name, int owner, struct symbol *sym,
99 struct smatch_state *state);
100 void delete_state(const char *name, int owner, struct symbol *sym);
101 void set_true_false_states(const char *name, int owner, struct symbol *sym,
102 struct smatch_state *true_state,
103 struct smatch_state *false_state);
105 struct state_list *get_all_states(int id);
106 int is_reachable();
107 char *get_filename();
108 char *get_function();
109 int get_lineno();
110 int get_func_pos();
112 /* smatch_helper.c */
113 char *alloc_string(const char *str);
114 void free_string(char *str);
115 struct expression *get_argument_from_call_expr(struct expression_list *args,
116 int num);
117 char *get_variable_from_expr_complex(struct expression *expr,
118 struct symbol **sym_ptr);
119 char *get_variable_from_expr(struct expression *expr,
120 struct symbol **sym_ptr);
121 struct symbol *get_ptr_type(struct expression *expr);
122 int sym_name_is(const char *name, struct expression *expr);
123 int get_value(struct expression *expr);
124 int is_zero(struct expression *expr);
125 const char *show_state(struct smatch_state *state);
126 struct statement *get_block_thing(struct expression *expr);
127 struct expression *strip_expr(struct expression *expr);
129 /* smatch_ignore.c */
130 struct tracker {
131 char *name;
132 int owner;
133 struct symbol *sym;
135 DECLARE_PTR_LIST(tracker_list, struct tracker);
137 void add_ignore(const char *name, int owner, struct symbol *sym);
138 int is_ignored(const char *name, int owner, struct symbol *sym);
140 /* smatch_tracker */
141 void add_tracker(struct tracker_list **list, const char *name, int owner,
142 struct symbol *sym);
143 void del_tracker(struct tracker_list **list, const char *name, int owner,
144 struct symbol *sym);
145 int in_tracker_list(struct tracker_list *list, const char *name, int owner,
146 struct symbol *sym);
147 void free_tracker_list(struct tracker_list **list);
148 void free_trackers_and_list(struct tracker_list **list);
150 /* smatch_conditions */
151 int in_condition();
153 /* ----------------------------------------------------------------
154 The stuff below is all used internally and shouldn't
155 be called from other programs
156 -----------------------------------------------------------------*/
158 /* smatch_flow.c */
160 void smatch (int argc, char **argv);
161 void __split_expr(struct expression *expr);
162 void __split_statements(struct statement *stmt);
163 extern int option_assume_loops;
164 extern int option_known_conditions;
166 /* smatch_conditions */
167 void __split_whole_condition(struct expression *expr);
169 /* smatch_implied.c */
170 extern int debug_implied_states;
171 extern int option_no_implied;
172 void get_implications(char *name, struct symbol *sym, int comparison, int num,
173 struct state_list **true_states,
174 struct state_list **false_states);
176 /* smatch_extras.c */
177 #define SMATCH_EXTRA 1 /* this is my_id from smatch extra set in smatch.c */
178 int true_comparison(int left, int comparison, int right);
179 int known_condition_true(struct expression *expr);
180 int known_condition_false(struct expression *expr);
182 /* smatch_states.c */
183 extern int debug_states;
185 struct state_list *__get_cur_slist();
186 void __set_true_false_sm(struct sm_state *true_state,
187 struct sm_state *false_state);
188 void nullify_path();
189 void __match_nullify_path_hook(const char *fn, struct expression *expr,
190 void *unused);
191 void __unnullify_path();
192 int __path_is_null();
193 void clear_all_states();
195 struct sm_state *get_sm_state(const char *name, int owner,
196 struct symbol *sym);
197 void __use_false_only_stack();
198 void __pop_false_only_stack();
199 void __push_true_states();
200 void __use_false_states();
201 void __pop_false_states();
202 void __merge_false_states();
203 void __merge_true_states();
205 void __negate_cond_stacks();
206 void __save_false_states_for_later();
207 void __use_previously_stored_false_states();
208 void __use_cond_true_states();
209 void __use_cond_false_states();
210 void __push_cond_stacks();
211 void __and_cond_states();
212 void __or_cond_states();
213 void __save_pre_cond_states();
214 void __pop_pre_cond_states();
215 void __use_cond_states();
217 void __warn_on_silly_pre_loops();
219 void __push_continues();
220 void __pop_continues();
221 void __process_continues();
222 void __merge_continues();
224 void __push_breaks();
225 void __process_breaks();
226 void __merge_breaks();
227 void __use_breaks();
229 void __save_switch_states();
230 void __pop_switches();
231 void __merge_switches();
232 void __push_default();
233 void __set_default();
234 int __pop_default();
236 void __push_conditions();
237 void __pop_conditions();
239 void __save_gotos(const char *name);
240 void __merge_gotos(const char *name);
242 void __print_cur_slist();
244 /* smatch_hooks.c */
245 void __pass_to_client(void *data, enum hook_type type);
246 void __pass_to_client_no_data(enum hook_type type);
247 void __pass_case_to_client(struct expression *switch_expr,
248 struct expression *case_expr);
249 int __has_merge_function(int client_id);
250 struct smatch_state *__client_merge_function(int owner, const char *name,
251 struct symbol *sym,
252 struct smatch_state *s1,
253 struct smatch_state *s2);
254 struct smatch_state *__client_unmatched_state_function(struct sm_state *sm);
256 /* smatch_function_hooks.c */
257 struct fcall_back {
258 int type;
259 func_hook *call_back;
260 void *info;
262 DECLARE_ALLOCATOR(fcall_back);
263 DECLARE_PTR_LIST(call_back_list, struct fcall_back);
264 void create_function_hash(void);
265 void __match_initializer_call(struct symbol *sym);
267 /* smatch_files.c */
268 struct token *get_tokens_file(const char *filename);
270 /* smatch.c */
271 extern char *data_dir;
272 extern int option_no_data;
274 #endif /* !SMATCH_H_ */