handle assignments like: foo = x < 1;
[smatch.git] / smatch.h
blob9d080c82f9aaa5ba155eef20289cb8c7a811b2c3
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 struct smatch_state {
22 const char *name;
23 void *data;
25 #define STATE(_x) static struct smatch_state _x = { .name = #_x }
26 extern struct smatch_state undefined;
27 extern struct smatch_state merged;
28 extern struct smatch_state true_state;
29 extern struct smatch_state false_state;
30 DECLARE_ALLOCATOR(smatch_state);
32 static inline void *INT_PTR(int i)
34 return (void *)(long)i;
37 static inline int PTR_INT(void *p)
39 return (int)(long)p;
42 struct sm_state {
43 unsigned short owner;
44 unsigned int merged:1;
45 unsigned int implied:1;
46 const char *name;
47 struct symbol *sym;
48 struct smatch_state *state;
49 unsigned int line;
50 struct state_list *my_pool;
51 struct sm_state *left;
52 struct sm_state *right;
53 unsigned int nr_children;
54 struct state_list *possible;
57 struct tracker {
58 int owner;
59 char *name;
60 struct symbol *sym;
62 DECLARE_ALLOCATOR(tracker);
63 DECLARE_PTR_LIST(tracker_list, struct tracker);
65 enum hook_type {
66 EXPR_HOOK,
67 STMT_HOOK,
68 SYM_HOOK,
69 DECLARATION_HOOK,
70 ASSIGNMENT_HOOK,
71 CONDITION_HOOK,
72 WHOLE_CONDITION_HOOK,
73 FUNCTION_CALL_HOOK,
74 CALL_ASSIGNMENT_HOOK,
75 BINOP_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,
86 #define TRUE 1
87 #define FALSE 0
89 void add_hook(void *func, enum hook_type type);
90 typedef struct smatch_state *(merge_func_t)(const char *name,
91 struct symbol *sym,
92 struct smatch_state *s1,
93 struct smatch_state *s2);
94 typedef struct smatch_state *(unmatched_func_t)(struct sm_state *state);
95 void add_merge_hook(int client_id, merge_func_t *func);
96 void add_unmatched_state_hook(int client_id, unmatched_func_t *func);
97 typedef void (scope_hook)(void *data);
98 void add_scope_hook(scope_hook *hook, void *data);
99 typedef void (func_hook)(const char *fn, struct expression *expr, void *data);
100 typedef void (implication_hook)(const char *fn, struct expression *call_expr,
101 struct expression *assign_expr, void *data);
102 void add_function_hook(const char *lock_for, func_hook *call_back, void *data);
104 void add_function_assign_hook(const char *look_for, func_hook *call_back,
105 void *info);
106 void return_implies_state(const char *look_for, long long start, long long end,
107 implication_hook *call_back, void *info);
108 typedef void (modification_hook)(const char *name, struct symbol *sym,
109 struct expression *expr, void *data);
110 void add_modification_hook(int owner, const char *variable, modification_hook *hook,
111 void *data);
112 void add_modification_hook_expr(int owner, struct expression *expr, modification_hook *call_back, void *info);
113 void set_default_modification_hook(int owner, modification_hook *call_back);
114 void __use_default_modification_hook(int owner, const char *variable);
116 const char *get_filename(void);
117 char *get_function(void);
118 int get_lineno(void);
119 int get_func_pos(void);
120 extern int final_pass;
122 #define sm_printf(msg...) do { if (final_pass) printf(msg); } while (0) \
124 static inline void sm_prefix(void)
126 printf("%s +%d %s(%d) ", get_filename(), get_lineno(), get_function(), get_func_pos());
129 #define sm_msg(msg...) \
130 do { \
131 if (!option_debug && !final_pass) \
132 break; \
133 sm_prefix(); \
134 printf(msg); \
135 printf("\n"); \
136 } while (0)
138 #define sm_debug(msg...) do { if (option_debug) printf(msg); } while (0)
140 #define sm_info(msg...) do { \
141 if (option_debug || (option_info && final_pass)) { \
142 sm_prefix(); \
143 printf("info: "); \
144 printf(msg); \
145 printf("\n"); \
147 } while(0)
149 #define POINTER_MAX 0xffffffff
151 struct smatch_state *get_state(int owner, const char *name, struct symbol *sym);
152 struct smatch_state *get_state_expr(int owner, struct expression *expr);
153 struct state_list *get_possible_states(int owner, const char *name,
154 struct symbol *sym);
155 struct state_list *get_possible_states_expr(int owner, struct expression *expr);
156 void set_state(int owner, const char *name, struct symbol *sym,
157 struct smatch_state *state);
158 void set_state_expr(int owner, struct expression *expr,
159 struct smatch_state *state);
160 void delete_state(int owner, const char *name, struct symbol *sym);
161 void delete_state_expr(int owner, struct expression *expr);
162 void set_true_false_states(int owner, const char *name, struct symbol *sym,
163 struct smatch_state *true_state,
164 struct smatch_state *false_state);
165 void set_true_false_states_expr(int owner, struct expression *expr,
166 struct smatch_state *true_state,
167 struct smatch_state *false_state);
169 struct state_list *get_all_states(int id);
170 int is_reachable(void);
172 /* smatch_helper.c */
173 char *alloc_string(const char *str);
174 void free_string(char *str);
175 struct smatch_state *alloc_state_num(int num);
176 struct expression *get_argument_from_call_expr(struct expression_list *args,
177 int num);
178 char *get_variable_from_expr_complex(struct expression *expr,
179 struct symbol **sym_ptr);
180 char *get_variable_from_expr(struct expression *expr,
181 struct symbol **sym_ptr);
182 int sym_name_is(const char *name, struct expression *expr);
183 int get_value(struct expression *expr, long long *val);
184 int get_implied_value(struct expression *expr, long long *val);
185 int get_fuzzy_max(struct expression *expr, long long *max);
186 int is_zero(struct expression *expr);
187 int is_array(struct expression *expr);
188 struct expression *get_array_name(struct expression *expr);
189 struct expression *get_array_offset(struct expression *expr);
190 const char *show_state(struct smatch_state *state);
191 struct statement *get_block_thing(struct expression *expr);
192 struct expression *strip_parens(struct expression *expr);
193 struct expression *strip_expr(struct expression *expr);
194 void scoped_state(int my_id, const char *name, struct symbol *sym);
195 int is_error_return(struct expression *expr);
196 int getting_address(void);
198 /* smatch_type.c */
199 struct symbol *get_type(struct expression *expr);
200 int type_unsigned(struct symbol *base_type);
201 long long type_max(struct symbol *base_type);
202 long long type_min(struct symbol *base_type);
204 /* smatch_ignore.c */
205 void add_ignore(int owner, const char *name, struct symbol *sym);
206 int is_ignored(int owner, const char *name, struct symbol *sym);
208 /* smatch_tracker */
209 struct tracker *alloc_tracker(int owner, const char *name, struct symbol *sym);
210 void add_tracker(struct tracker_list **list, int owner, const char *name,
211 struct symbol *sym);
212 void del_tracker(struct tracker_list **list, int owner, const char *name,
213 struct symbol *sym);
214 int in_tracker_list(struct tracker_list *list, int owner, const char *name,
215 struct symbol *sym);
216 void free_tracker_list(struct tracker_list **list);
217 void free_trackers_and_list(struct tracker_list **list);
219 /* smatch_conditions */
220 int in_condition(void);
222 /* smatch_flow.c */
224 void smatch (int argc, char **argv);
225 void __split_expr(struct expression *expr);
226 void __split_statements(struct statement *stmt);
227 extern int option_assume_loops;
228 extern int option_known_conditions;
229 extern int option_two_passes;
230 extern struct symbol *cur_func_sym;
231 extern struct expression_list *big_expression_stack;
232 extern struct statement_list *big_statement_stack;
233 extern int __in_pre_condition;
234 extern int __bail_on_rest_of_function;
236 /* smatch_conditions */
237 void __split_whole_condition(struct expression *expr);
238 int __handle_condition_assigns(struct expression *expr);
240 /* smatch_implied.c */
241 extern int option_debug_implied;
242 extern int option_no_implied;
243 void get_implications(char *name, struct symbol *sym, int comparison, long long num,
244 struct state_list **true_states,
245 struct state_list **false_states);
246 struct range_list_stack;
247 struct state_list *__implied_case_slist(struct expression *switch_expr,
248 struct expression *case_expr,
249 struct range_list_stack **remaining_cases,
250 struct state_list **raw_slist);
251 struct range_list *__get_implied_values(struct expression *switch_expr);
253 /* smatch_extras.c */
254 #define SMATCH_EXTRA 1 /* this is my_id from smatch extra set in smatch.c */
256 struct data_range {
257 long long min;
258 long long max;
260 extern struct data_range whole_range;
262 int get_implied_single_val(struct expression *expr, long long *val);
263 int get_implied_single_fuzzy_max(struct expression *expr, long long *max);
264 int get_implied_max(struct expression *expr, long long *val);
265 int get_implied_min(struct expression *expr, long long *val);
266 int known_condition_true(struct expression *expr);
267 int known_condition_false(struct expression *expr);
268 int implied_condition_true(struct expression *expr);
269 int implied_condition_false(struct expression *expr);
271 /* smatch_states.c */
272 extern int option_debug;
274 extern int __fake_cur;
275 extern struct state_list *__fake_cur_slist;
276 extern int __fake_conditions;
277 extern struct state_list *__fake_cond_true;
278 extern struct state_list *__fake_cond_false;
281 int unreachable(void);
282 void __set_state(struct sm_state *sm);
283 struct state_list *__get_cur_slist(void);
284 void __set_true_false_sm(struct sm_state *true_state,
285 struct sm_state *false_state);
286 void nullify_path(void);
287 void __match_nullify_path_hook(const char *fn, struct expression *expr,
288 void *unused);
289 void __unnullify_path(void);
290 int __path_is_null(void);
291 void clear_all_states(void);
293 struct sm_state *get_sm_state(int owner, const char *name,
294 struct symbol *sym);
295 struct sm_state *get_sm_state_expr(int owner, struct expression *expr);
296 void __push_true_states(void);
297 void __use_false_states(void);
298 void __pop_false_states(void);
299 void __merge_false_states(void);
300 void __merge_true_states(void);
302 void __negate_cond_stacks(void);
303 void __save_false_states_for_later(void);
304 void __use_previously_stored_false_states(void);
305 void __use_cond_true_states(void);
306 void __use_cond_false_states(void);
307 void __push_cond_stacks(void);
308 void __and_cond_states(void);
309 void __or_cond_states(void);
310 void __save_pre_cond_states(void);
311 void __pop_pre_cond_states(void);
312 void __use_cond_states(void);
314 void __warn_on_silly_pre_loops(void);
316 void __push_continues(void);
317 void __pop_continues(void);
318 void __process_continues(void);
319 void __merge_continues(void);
321 void __push_breaks(void);
322 void __process_breaks(void);
323 void __merge_breaks(void);
324 void __use_breaks(void);
326 void __save_switch_states(struct expression *switch_expr);
327 void __pop_switches(void);
328 void __merge_switches(struct expression *switch_expr, struct expression *case_expr);
329 void __push_default(void);
330 void __set_default(void);
331 int __pop_default(void);
333 void __push_conditions(void);
334 void __pop_conditions(void);
336 void __save_gotos(const char *name);
337 void __merge_gotos(const char *name);
339 void __print_cur_slist(void);
341 /* smatch_hooks.c */
342 void __pass_to_client(void *data, enum hook_type type);
343 void __pass_to_client_no_data(enum hook_type type);
344 void __pass_case_to_client(struct expression *switch_expr,
345 struct expression *case_expr);
346 int __has_merge_function(int client_id);
347 struct smatch_state *__client_merge_function(int owner, const char *name,
348 struct symbol *sym,
349 struct smatch_state *s1,
350 struct smatch_state *s2);
351 struct smatch_state *__client_unmatched_state_function(struct sm_state *sm);
352 void __push_scope_hooks(void);
353 void __call_scope_hooks(void);
355 /* smatch_function_hooks.c */
356 void create_function_hook_hash(void);
357 void __match_initializer_call(struct symbol *sym);
359 /* smatch_files.c */
360 struct token *get_tokens_file(const char *filename);
362 /* smatch_oom.c */
363 extern int option_oom_kb;
364 int out_of_memory(void);
366 /* smatch.c */
367 extern char *option_project_str;
368 extern char *data_dir;
369 extern int option_no_data;
370 extern int option_spammy;
371 extern int option_rare;
372 extern int option_full_path;
373 extern int option_param_mapper;
374 extern int option_info;
375 extern int option_call_tree;
376 extern int num_checks;
378 enum project_type {
379 PROJ_NONE,
380 PROJ_KERNEL,
381 PROJ_WINE,
383 extern enum project_type option_project;
384 const char *check_name(unsigned short id);
386 #endif /* !SMATCH_H_ */