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