Handle if ((dev = foo()) == NULL) { ...
[smatch.git] / smatch.h
blob178d939eb795688b1f802cd2053e9411e550c730
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 unsigned short owner;
45 unsigned int merged:1;
46 unsigned int implied:1;
47 const char *name;
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 struct tracker {
59 int owner;
60 char *name;
61 struct symbol *sym;
63 DECLARE_ALLOCATOR(tracker);
64 DECLARE_PTR_LIST(tracker_list, struct tracker);
66 enum hook_type {
67 EXPR_HOOK,
68 STMT_HOOK,
69 SYM_HOOK,
70 DECLARATION_HOOK,
71 ASSIGNMENT_HOOK,
72 CONDITION_HOOK,
73 WHOLE_CONDITION_HOOK,
74 FUNCTION_CALL_HOOK,
75 CALL_ASSIGNMENT_HOOK,
76 OP_HOOK,
77 DEREF_HOOK,
78 CASE_HOOK,
79 BASE_HOOK,
80 FUNC_DEF_HOOK,
81 END_FUNC_HOOK,
82 RETURN_HOOK,
83 END_FILE_HOOK,
85 void add_hook(void *func, enum hook_type type);
86 typedef struct smatch_state *(merge_func_t)(const char *name,
87 struct symbol *sym,
88 struct smatch_state *s1,
89 struct smatch_state *s2);
90 typedef struct smatch_state *(unmatched_func_t)(struct sm_state *state);
91 void add_merge_hook(int client_id, merge_func_t *func);
92 void add_unmatched_state_hook(int client_id, unmatched_func_t *func);
93 typedef void (scope_hook)(void *data);
94 void add_scope_hook(scope_hook *hook, void *data);
95 typedef void (func_hook)(const char *fn, struct expression *expr, void *data);
96 void add_function_hook(const char *lock_for, func_hook *call_back, void *data);
98 void add_conditional_hook(const char *look_for, func_hook *call_back, void *data);
99 void add_function_assign_hook(const char *look_for, func_hook *call_back,
100 void *info);
101 void return_implies_state(const char *look_for, long long start, long long end,
102 func_hook *call_back, void *info);
103 typedef void (modification_hook)(const char *name, struct symbol *sym,
104 struct expression *expr, void *data);
105 void add_modification_hook(const char *variable, modification_hook *hook,
106 void *data);
107 int is_member(struct expression *expr);
108 void reset_on_container_modified(int owner, struct expression *expr);
109 void set_default_state(int owner, struct smatch_state *state);
111 extern int final_pass;
113 #define sm_printf(msg...) do { if (final_pass) printf(msg); } while (0) \
115 #define sm_msg(msg...) \
116 do { \
117 if (!final_pass) \
118 break; \
119 printf("%s +%d %s(%d) ", get_filename(), get_lineno(), \
120 get_function(), get_func_pos()); \
121 printf(msg); \
122 printf("\n"); \
123 } while (0)
125 #define sm_debug(msg...) do { if (debug_states) printf(msg); } while (0)
127 #define UNDEFINED INT_MIN
129 struct smatch_state *get_state(int owner, const char *name, struct symbol *sym);
130 struct smatch_state *get_state_expr(int owner, struct expression *expr);
131 struct state_list *get_possible_states(int owner, const char *name,
132 struct symbol *sym);
133 struct state_list *get_possible_states_expr(int owner, struct expression *expr);
134 void set_state(int owner, const char *name, struct symbol *sym,
135 struct smatch_state *state);
136 void set_state_expr(int owner, struct expression *expr,
137 struct smatch_state *state);
138 void delete_state(int owner, const char *name, struct symbol *sym);
139 void delete_state_expr(int owner, struct expression *expr);
140 void set_true_false_states(int owner, const char *name, struct symbol *sym,
141 struct smatch_state *true_state,
142 struct smatch_state *false_state);
143 void set_true_false_states_expr(int owner, struct expression *expr,
144 struct smatch_state *true_state,
145 struct smatch_state *false_state);
147 struct state_list *get_all_states(int id);
148 int is_reachable();
149 char *get_filename();
150 char *get_function();
151 int get_lineno();
152 int get_func_pos();
154 /* smatch_helper.c */
155 char *alloc_string(const char *str);
156 void free_string(char *str);
157 struct expression *get_argument_from_call_expr(struct expression_list *args,
158 int num);
159 char *get_variable_from_expr_complex(struct expression *expr,
160 struct symbol **sym_ptr);
161 char *get_variable_from_expr(struct expression *expr,
162 struct symbol **sym_ptr);
163 struct symbol *get_ptr_type(struct expression *expr);
164 int sym_name_is(const char *name, struct expression *expr);
165 int get_value(struct expression *expr);
166 int get_implied_value(struct expression *expr);
167 int is_zero(struct expression *expr);
168 int is_array(struct expression *expr);
169 struct expression *get_array_name(struct expression *expr);
170 struct expression *get_array_offset(struct expression *expr);
171 const char *show_state(struct smatch_state *state);
172 struct statement *get_block_thing(struct expression *expr);
173 struct expression *strip_expr(struct expression *expr);
174 void scoped_state(const char *name, int my_id, struct symbol *sym);
176 /* smatch_ignore.c */
177 void add_ignore(int owner, const char *name, struct symbol *sym);
178 int is_ignored(int owner, const char *name, struct symbol *sym);
180 /* smatch_tracker */
181 struct tracker *alloc_tracker(int owner, const char *name, struct symbol *sym);
182 void add_tracker(struct tracker_list **list, int owner, const char *name,
183 struct symbol *sym);
184 void del_tracker(struct tracker_list **list, int owner, const char *name,
185 struct symbol *sym);
186 int in_tracker_list(struct tracker_list *list, int owner, const char *name,
187 struct symbol *sym);
188 void free_tracker_list(struct tracker_list **list);
189 void free_trackers_and_list(struct tracker_list **list);
191 /* smatch_conditions */
192 int in_condition();
194 /* ----------------------------------------------------------------
195 The stuff below is all used internally and shouldn't
196 be called from other programs
197 -----------------------------------------------------------------*/
199 /* smatch_flow.c */
201 void smatch (int argc, char **argv);
202 void __split_expr(struct expression *expr);
203 void __split_statements(struct statement *stmt);
204 extern int option_assume_loops;
205 extern int option_known_conditions;
206 extern int option_two_passes;
208 /* smatch_conditions */
209 void __split_whole_condition(struct expression *expr);
211 /* smatch_implied.c */
212 extern int debug_implied_states;
213 extern int option_no_implied;
214 void get_implications(char *name, struct symbol *sym, int comparison, int num,
215 struct state_list **true_states,
216 struct state_list **false_states);
217 struct range_list_stack;
218 struct state_list *__implied_case_slist(struct expression *switch_expr,
219 struct expression *case_expr,
220 struct range_list_stack **remaining_cases,
221 struct state_list **raw_slist);
222 struct range_list *__get_implied_values(struct expression *switch_expr);
224 /* smatch_extras.c */
225 #define SMATCH_EXTRA 1 /* this is my_id from smatch extra set in smatch.c */
227 struct data_range {
228 long long min;
229 long long max;
231 extern struct data_range whole_range;
233 int get_implied_single_val(struct expression *expr);
234 int get_implied_max(struct expression *expr);
235 int get_implied_min(struct expression *expr);
236 int true_comparison(int left, int comparison, int right);
237 int known_condition_true(struct expression *expr);
238 int known_condition_false(struct expression *expr);
239 int implied_condition_true(struct expression *expr);
240 int implied_condition_false(struct expression *expr);
242 /* smatch_states.c */
243 extern int debug_states;
245 extern int __fake_cur;
246 extern struct state_list *__fake_cur_slist;
247 extern int __fake_conditions;
248 extern struct state_list *__fake_cond_true;
249 extern struct state_list *__fake_cond_false;
252 void __set_state(struct sm_state *sm);
253 struct state_list *__get_cur_slist();
254 void __set_true_false_sm(struct sm_state *true_state,
255 struct sm_state *false_state);
256 void nullify_path();
257 void __match_nullify_path_hook(const char *fn, struct expression *expr,
258 void *unused);
259 void __unnullify_path();
260 int __path_is_null();
261 void clear_all_states();
263 struct sm_state *get_sm_state(int owner, const char *name,
264 struct symbol *sym);
265 struct sm_state *get_sm_state_expr(int owner, struct expression *expr);
266 void __use_false_only_stack();
267 void __pop_false_only_stack();
268 void __push_true_states();
269 void __use_false_states();
270 void __pop_false_states();
271 void __merge_false_states();
272 void __merge_true_states();
274 void __negate_cond_stacks();
275 void __save_false_states_for_later();
276 void __use_previously_stored_false_states();
277 void __use_cond_true_states();
278 void __use_cond_false_states();
279 void __push_cond_stacks();
280 void __and_cond_states();
281 void __or_cond_states();
282 void __save_pre_cond_states();
283 void __pop_pre_cond_states();
284 void __use_cond_states();
286 void __warn_on_silly_pre_loops();
288 void __push_continues();
289 void __pop_continues();
290 void __process_continues();
291 void __merge_continues();
293 void __push_breaks();
294 void __process_breaks();
295 void __merge_breaks();
296 void __use_breaks();
298 void __save_switch_states(struct expression *switch_expr);
299 void __pop_switches();
300 void __merge_switches(struct expression *switch_expr, struct expression *case_expr);
301 void __push_default();
302 void __set_default();
303 int __pop_default();
305 void __push_conditions();
306 void __pop_conditions();
308 void __save_gotos(const char *name);
309 void __merge_gotos(const char *name);
311 void __print_cur_slist();
313 /* smatch_hooks.c */
314 void __pass_to_client(void *data, enum hook_type type);
315 void __pass_to_client_no_data(enum hook_type type);
316 void __pass_case_to_client(struct expression *switch_expr,
317 struct expression *case_expr);
318 int __has_merge_function(int client_id);
319 struct smatch_state *__client_merge_function(int owner, const char *name,
320 struct symbol *sym,
321 struct smatch_state *s1,
322 struct smatch_state *s2);
323 struct smatch_state *__client_unmatched_state_function(struct sm_state *sm);
324 void __push_scope_hooks(void);
325 void __call_scope_hooks(void);
327 /* smatch_function_hooks.c */
328 void create_function_hash(void);
329 void __match_initializer_call(struct symbol *sym);
331 /* smatch_files.c */
332 struct token *get_tokens_file(const char *filename);
334 /* smatch_oom.c */
335 extern int option_oom_kb;
336 int out_of_memory();
338 /* smatch.c */
339 extern char *data_dir;
340 extern int option_no_data;
341 extern int option_spammy;
342 extern struct smatch_state *default_state[];
344 #endif /* !SMATCH_H_ */