extra: cleanup is_whole_range()
[smatch.git] / smatch.h
blobd2cf4550fb41ed663560cd3253746690d5081fc3
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 STRING_HOOK,
70 DECLARATION_HOOK,
71 ASSIGNMENT_HOOK,
72 RAW_ASSIGNMENT_HOOK,
73 LOGIC_HOOK,
74 CONDITION_HOOK,
75 PRELOOP_HOOK,
76 SELECT_HOOK,
77 WHOLE_CONDITION_HOOK,
78 FUNCTION_CALL_HOOK,
79 CALL_ASSIGNMENT_HOOK,
80 MACRO_ASSIGNMENT_HOOK,
81 BINOP_HOOK,
82 OP_HOOK,
83 DEREF_HOOK,
84 CASE_HOOK,
85 ASM_HOOK,
86 BASE_HOOK,
87 FUNC_DEF_HOOK,
88 END_FUNC_HOOK,
89 RETURN_HOOK,
90 END_FILE_HOOK,
93 #define TRUE 1
94 #define FALSE 0
96 void add_hook(void *func, enum hook_type type);
97 typedef struct smatch_state *(merge_func_t)(const char *name,
98 struct symbol *sym,
99 struct smatch_state *s1,
100 struct smatch_state *s2);
101 typedef struct smatch_state *(unmatched_func_t)(struct sm_state *state);
102 void add_merge_hook(int client_id, merge_func_t *func);
103 void add_unmatched_state_hook(int client_id, unmatched_func_t *func);
104 typedef void (scope_hook)(void *data);
105 void add_scope_hook(scope_hook *hook, void *data);
106 typedef void (func_hook)(const char *fn, struct expression *expr, void *data);
107 typedef void (implication_hook)(const char *fn, struct expression *call_expr,
108 struct expression *assign_expr, void *data);
109 void add_function_hook(const char *look_for, func_hook *call_back, void *data);
111 void add_function_assign_hook(const char *look_for, func_hook *call_back,
112 void *info);
113 void add_macro_assign_hook(const char *look_for, func_hook *call_back,
114 void *info);
115 void return_implies_state(const char *look_for, long long start, long long end,
116 implication_hook *call_back, void *info);
117 typedef void (modification_hook)(const char *name, struct symbol *sym,
118 struct expression *expr, void *data);
119 void add_modification_hook(int owner, const char *variable, modification_hook *hook,
120 void *data);
121 void add_modification_hook_expr(int owner, struct expression *expr, modification_hook *call_back, void *info);
122 void set_default_modification_hook(int owner, modification_hook *call_back);
123 void __use_default_modification_hook(int owner, const char *variable);
125 const char *get_filename(void);
126 char *get_function(void);
127 int get_lineno(void);
128 int get_func_pos(void);
129 extern int final_pass;
130 extern struct symbol *cur_func_sym;
131 extern int option_debug;
133 extern FILE *sm_outfd;
134 #define sm_printf(msg...) do { if (final_pass) fprintf(sm_outfd, msg); } while (0)
136 static inline void sm_prefix(void)
138 sm_printf("%s:%d %s(%d) ", get_filename(), get_lineno(), get_function(), get_func_pos());
141 static inline void print_implied_debug_msg();
143 #define sm_msg(msg...) \
144 do { \
145 print_implied_debug_msg(); \
146 if (!option_debug && !final_pass) \
147 break; \
148 sm_prefix(); \
149 sm_printf(msg); \
150 sm_printf("\n"); \
151 } while (0)
153 extern char *implied_debug_msg;
154 static inline void print_implied_debug_msg()
156 static struct symbol *last_printed = NULL;
158 if (!implied_debug_msg)
159 return;
160 if (last_printed == cur_func_sym)
161 return;
162 last_printed = cur_func_sym;
163 sm_msg("%s", implied_debug_msg);
166 #define sm_debug(msg...) do { if (option_debug) sm_printf(msg); } while (0)
168 #define sm_info(msg...) do { \
169 if (option_debug || (option_info && final_pass)) { \
170 sm_prefix(); \
171 sm_printf("info: "); \
172 sm_printf(msg); \
173 sm_printf("\n"); \
175 } while(0)
177 #define POINTER_MAX 0xffffffff
179 struct smatch_state *get_state(int owner, const char *name, struct symbol *sym);
180 struct smatch_state *get_state_expr(int owner, struct expression *expr);
181 struct state_list *get_possible_states(int owner, const char *name,
182 struct symbol *sym);
183 struct state_list *get_possible_states_expr(int owner, struct expression *expr);
184 struct sm_state *set_state(int owner, const char *name, struct symbol *sym,
185 struct smatch_state *state);
186 struct sm_state *set_state_expr(int owner, struct expression *expr,
187 struct smatch_state *state);
188 void delete_state(int owner, const char *name, struct symbol *sym);
189 void delete_state_expr(int owner, struct expression *expr);
190 void set_true_false_states(int owner, const char *name, struct symbol *sym,
191 struct smatch_state *true_state,
192 struct smatch_state *false_state);
193 void set_true_false_states_expr(int owner, struct expression *expr,
194 struct smatch_state *true_state,
195 struct smatch_state *false_state);
197 struct state_list *get_all_states(int id);
198 int is_reachable(void);
200 /* smatch_helper.c */
201 char *alloc_string(const char *str);
202 void free_string(char *str);
203 struct smatch_state *alloc_state_num(int num);
204 struct expression *get_argument_from_call_expr(struct expression_list *args,
205 int num);
206 char *get_variable_from_expr_complex(struct expression *expr,
207 struct symbol **sym_ptr);
208 char *get_variable_from_expr(struct expression *expr,
209 struct symbol **sym_ptr);
210 int sym_name_is(const char *name, struct expression *expr);
211 int get_value(struct expression *expr, long long *val);
212 int get_implied_value(struct expression *expr, long long *val);
213 int get_implied_max(struct expression *expr, long long *val);
214 int get_implied_min(struct expression *expr, long long *val);
215 int get_fuzzy_min(struct expression *expr, long long *min);
216 int get_fuzzy_max(struct expression *expr, long long *max);
217 int get_absolute_min(struct expression *expr, long long *val);
218 int get_absolute_max(struct expression *expr, long long *val);
219 int is_zero(struct expression *expr);
220 int is_array(struct expression *expr);
221 struct expression *get_array_name(struct expression *expr);
222 struct expression *get_array_offset(struct expression *expr);
223 const char *show_state(struct smatch_state *state);
224 struct statement *get_expression_statement(struct expression *expr);
225 struct expression *strip_parens(struct expression *expr);
226 struct expression *strip_expr(struct expression *expr);
227 void scoped_state(int my_id, const char *name, struct symbol *sym);
228 int is_error_return(struct expression *expr);
229 int getting_address(void);
230 char *get_fnptr_name(struct expression *expr);
232 /* smatch_type.c */
233 struct symbol *get_type(struct expression *expr);
234 int type_unsigned(struct symbol *base_type);
235 int expr_unsigned(struct expression *expr);
236 int returns_unsigned(struct symbol *base_type);
237 int returns_pointer(struct symbol *base_type);
238 long long type_max(struct symbol *base_type);
239 long long type_min(struct symbol *base_type);
241 /* smatch_ignore.c */
242 void add_ignore(int owner, const char *name, struct symbol *sym);
243 int is_ignored(int owner, const char *name, struct symbol *sym);
245 /* smatch_tracker */
246 struct tracker *alloc_tracker(int owner, const char *name, struct symbol *sym);
247 void add_tracker(struct tracker_list **list, int owner, const char *name,
248 struct symbol *sym);
249 void add_tracker_expr(struct tracker_list **list, int owner, struct expression *expr);
250 void del_tracker(struct tracker_list **list, int owner, const char *name,
251 struct symbol *sym);
252 int in_tracker_list(struct tracker_list *list, int owner, const char *name,
253 struct symbol *sym);
254 struct tracker_list *clone_tracker_list(struct tracker_list *orig_list);
255 void free_tracker_list(struct tracker_list **list);
256 void free_trackers_and_list(struct tracker_list **list);
258 /* smatch_conditions */
259 int in_condition(void);
261 /* smatch_flow.c */
263 void smatch (int argc, char **argv);
264 int inside_loop(void);
265 int in_expression_statement(void);
266 void __split_expr(struct expression *expr);
267 void __split_stmt(struct statement *stmt);
268 extern int option_assume_loops;
269 extern int option_known_conditions;
270 extern int option_two_passes;
271 extern int option_no_db;
272 extern int option_file_output;
273 extern struct expression_list *big_expression_stack;
274 extern struct statement_list *big_statement_stack;
275 extern int __in_pre_condition;
276 extern int __bail_on_rest_of_function;
278 /* smatch_conditions */
279 void __split_whole_condition(struct expression *expr);
280 void __handle_logic(struct expression *expr);
281 int __is_condition_assign(struct expression *expr);
282 int __handle_condition_assigns(struct expression *expr);
283 int __handle_select_assigns(struct expression *expr);
284 int __handle_expr_statement_assigns(struct expression *expr);
286 /* smatch_implied.c */
287 extern int option_debug_implied;
288 extern int option_no_implied;
289 void get_implications(char *name, struct symbol *sym, int comparison, long long num,
290 struct state_list **true_states,
291 struct state_list **false_states);
292 struct range_list_stack;
293 struct state_list *__implied_case_slist(struct expression *switch_expr,
294 struct expression *case_expr,
295 struct range_list_stack **remaining_cases,
296 struct state_list **raw_slist);
297 struct range_list *__get_implied_values(struct expression *switch_expr);
299 /* smatch_extras.c */
300 #define SMATCH_EXTRA 1 /* this is my_id from smatch extra set in smatch.c */
302 struct data_range {
303 long long min;
304 long long max;
306 extern struct data_range whole_range;
308 int known_condition_true(struct expression *expr);
309 int known_condition_false(struct expression *expr);
310 int implied_condition_true(struct expression *expr);
311 int implied_condition_false(struct expression *expr);
313 /* smatch_states.c */
314 void __push_fake_cur_slist();
315 struct state_list *__pop_fake_cur_slist();
317 int unreachable(void);
318 void __set_sm(struct sm_state *sm);
319 struct state_list *__get_cur_slist(void);
320 void __set_true_false_sm(struct sm_state *true_state,
321 struct sm_state *false_state);
322 void nullify_path(void);
323 void __match_nullify_path_hook(const char *fn, struct expression *expr,
324 void *unused);
325 void __unnullify_path(void);
326 int __path_is_null(void);
327 void clear_all_states(void);
329 struct sm_state *get_sm_state(int owner, const char *name,
330 struct symbol *sym);
331 struct sm_state *get_sm_state_expr(int owner, struct expression *expr);
332 void __push_true_states(void);
333 void __use_false_states(void);
334 void __discard_false_states(void);
335 void __merge_false_states(void);
336 void __merge_true_states(void);
338 void __negate_cond_stacks(void);
339 void __use_pre_cond_states(void);
340 void __use_cond_true_states(void);
341 void __use_cond_false_states(void);
342 void __push_cond_stacks(void);
343 struct state_list *__copy_cond_true_states(void);
344 struct state_list *__copy_cond_false_states(void);
345 struct state_list *__pop_cond_true_stack(void);
346 struct state_list *__pop_cond_false_stack(void);
347 void __and_cond_states(void);
348 void __or_cond_states(void);
349 void __save_pre_cond_states(void);
350 void __discard_pre_cond_states(void);
351 void __use_cond_states(void);
353 void __warn_on_silly_pre_loops(void);
355 void __push_continues(void);
356 void __discard_continues(void);
357 void __process_continues(void);
358 void __merge_continues(void);
360 void __push_breaks(void);
361 void __process_breaks(void);
362 void __merge_breaks(void);
363 void __use_breaks(void);
365 void __save_switch_states(struct expression *switch_expr);
366 void __discard_switches(void);
367 void __merge_switches(struct expression *switch_expr, struct expression *case_expr);
368 void __push_default(void);
369 void __set_default(void);
370 int __pop_default(void);
372 void __push_conditions(void);
373 void __discard_conditions(void);
375 void __save_gotos(const char *name);
376 void __merge_gotos(const char *name);
378 void __print_cur_slist(void);
380 /* smatch_hooks.c */
381 void __pass_to_client(void *data, enum hook_type type);
382 void __pass_to_client_no_data(enum hook_type type);
383 void __pass_case_to_client(struct expression *switch_expr,
384 struct expression *case_expr);
385 int __has_merge_function(int client_id);
386 struct smatch_state *__client_merge_function(int owner, const char *name,
387 struct symbol *sym,
388 struct smatch_state *s1,
389 struct smatch_state *s2);
390 struct smatch_state *__client_unmatched_state_function(struct sm_state *sm);
391 void __push_scope_hooks(void);
392 void __call_scope_hooks(void);
394 /* smatch_function_hooks.c */
395 void create_function_hook_hash(void);
396 void __match_initializer_call(struct symbol *sym);
398 /* smatch_db.c */
399 enum info_type {
400 PARAM_VALUE = 1,
401 BUF_SIZE,
402 USER_DATA,
403 CAPPED_DATA,
404 RETURN_VALUE,
407 void add_definition_db_callback(void (*callback)(const char *name, struct symbol *sym, char *key, char *value), int type);
408 void add_member_info_callback(int owner, void (*callback)(char *fn, int param, char *printed_name, struct smatch_state *state));
409 struct range_list *db_return_vals(struct expression *expr);
411 #define run_sql(call_back, sql...) \
412 do { \
413 char sql_txt[1024]; \
414 snprintf(sql_txt, 1024, sql); \
415 sql_exec(call_back, sql_txt); \
416 } while (0)
418 void sql_exec(int (*callback)(void*, int, char**, char**), const char *sql);
419 void open_smatch_db(void);
421 /* smatch_files.c */
422 struct token *get_tokens_file(const char *filename);
424 /* smatch.c */
425 extern char *option_project_str;
426 extern char *data_dir;
427 extern int option_no_data;
428 extern int option_spammy;
429 extern int option_full_path;
430 extern int option_param_mapper;
431 extern int option_info;
432 extern int option_call_tree;
433 extern int num_checks;
435 enum project_type {
436 PROJ_NONE,
437 PROJ_KERNEL,
438 PROJ_WINE,
440 extern enum project_type option_project;
441 const char *check_name(unsigned short id);
444 /* smatch_buf_size.c */
445 int get_array_size(struct expression *expr);
446 int get_array_size_bytes(struct expression *expr);
448 /* smatch_capped.c */
449 int is_capped(struct expression *expr);
451 /* check_user_data.c */
452 int is_user_data(struct expression *expr);
454 /* check_locking.c */
455 void print_held_locks();
457 #endif /* !SMATCH_H_ */