Makefile: fix build error
[smatch.git] / smatch.h
blob0737340fcc657bba88b50ebb0b617bd9f6a6ade0
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 typedef struct {
22 struct symbol *type;
23 union {
24 long long value;
25 unsigned long long uvalue;
27 } sval_t;
29 struct smatch_state {
30 const char *name;
31 void *data;
33 #define STATE(_x) static struct smatch_state _x = { .name = #_x }
34 extern struct smatch_state undefined;
35 extern struct smatch_state merged;
36 extern struct smatch_state true_state;
37 extern struct smatch_state false_state;
38 DECLARE_ALLOCATOR(smatch_state);
40 static inline void *INT_PTR(int i)
42 return (void *)(long)i;
45 static inline int PTR_INT(void *p)
47 return (int)(long)p;
50 struct sm_state {
51 unsigned short owner;
52 unsigned int merged:1;
53 unsigned int implied:1;
54 const char *name;
55 struct symbol *sym;
56 struct smatch_state *state;
57 unsigned int line;
58 struct state_list *pool;
59 struct sm_state *left;
60 struct sm_state *right;
61 unsigned int nr_children;
62 struct state_list *possible;
65 struct tracker {
66 int owner;
67 char *name;
68 struct symbol *sym;
70 DECLARE_ALLOCATOR(tracker);
71 DECLARE_PTR_LIST(tracker_list, struct tracker);
73 enum hook_type {
74 EXPR_HOOK,
75 STMT_HOOK,
76 SYM_HOOK,
77 STRING_HOOK,
78 DECLARATION_HOOK,
79 ASSIGNMENT_HOOK,
80 RAW_ASSIGNMENT_HOOK,
81 GLOBAL_ASSIGNMENT_HOOK,
82 LOGIC_HOOK,
83 CONDITION_HOOK,
84 PRELOOP_HOOK,
85 SELECT_HOOK,
86 WHOLE_CONDITION_HOOK,
87 FUNCTION_CALL_HOOK,
88 CALL_ASSIGNMENT_HOOK,
89 MACRO_ASSIGNMENT_HOOK,
90 BINOP_HOOK,
91 OP_HOOK,
92 DEREF_HOOK,
93 CASE_HOOK,
94 ASM_HOOK,
95 CAST_HOOK,
96 SIZEOF_HOOK,
97 BASE_HOOK,
98 FUNC_DEF_HOOK,
99 AFTER_DEF_HOOK,
100 END_FUNC_HOOK,
101 RETURN_HOOK,
102 END_FILE_HOOK,
105 #define TRUE 1
106 #define FALSE 0
108 struct range_list;
110 void add_hook(void *func, enum hook_type type);
111 typedef struct smatch_state *(merge_func_t)(struct smatch_state *s1, struct smatch_state *s2);
112 typedef struct smatch_state *(unmatched_func_t)(struct sm_state *state);
113 void add_merge_hook(int client_id, merge_func_t *func);
114 void add_unmatched_state_hook(int client_id, unmatched_func_t *func);
115 typedef void (scope_hook)(void *data);
116 void add_scope_hook(scope_hook *hook, void *data);
117 typedef void (func_hook)(const char *fn, struct expression *expr, void *data);
118 typedef void (implication_hook)(const char *fn, struct expression *call_expr,
119 struct expression *assign_expr, void *data);
120 typedef void (return_implies_hook)(struct expression *call_expr,
121 int param, char *key, char *value);
122 typedef int (implied_return_hook)(struct expression *call_expr, void *info, struct range_list **rl);
123 void add_function_hook(const char *look_for, func_hook *call_back, void *data);
125 void add_function_assign_hook(const char *look_for, func_hook *call_back,
126 void *info);
127 void add_implied_return_hook(const char *look_for,
128 implied_return_hook *call_back,
129 void *info);
130 void add_macro_assign_hook(const char *look_for, func_hook *call_back,
131 void *info);
132 void add_macro_assign_hook_extra(const char *look_for, func_hook *call_back,
133 void *info);
134 void return_implies_state(const char *look_for, long long start, long long end,
135 implication_hook *call_back, void *info);
136 void add_db_return_states_callback(int type, return_implies_hook *callback);
137 void add_db_return_states_before(void (*fn)(void));
138 void add_db_return_states_after(void (*fn)(void));
139 int get_implied_return(struct expression *expr, struct range_list **rl);
141 typedef void (modification_hook)(struct sm_state *sm);
142 void add_modification_hook(int owner, modification_hook *call_back);
143 void add_indirect_modification_hook(int owner, modification_hook *call_back);
145 const char *get_filename(void);
146 char *get_function(void);
147 int get_lineno(void);
148 extern int final_pass;
149 extern struct symbol *cur_func_sym;
150 extern int option_debug;
151 extern int local_debug;
153 extern FILE *sm_outfd;
154 #define sm_printf(msg...) do { if (final_pass) fprintf(sm_outfd, msg); } while (0)
156 static inline void sm_prefix(void)
158 sm_printf("%s:%d %s() ", get_filename(), get_lineno(), get_function());
161 static inline void print_implied_debug_msg();
163 #define sm_msg(msg...) \
164 do { \
165 print_implied_debug_msg(); \
166 if (!option_debug && !final_pass) \
167 break; \
168 sm_prefix(); \
169 sm_printf(msg); \
170 sm_printf("\n"); \
171 } while (0)
173 extern char *implied_debug_msg;
174 static inline void print_implied_debug_msg()
176 static struct symbol *last_printed = NULL;
178 if (!implied_debug_msg)
179 return;
180 if (last_printed == cur_func_sym)
181 return;
182 last_printed = cur_func_sym;
183 sm_msg("%s", implied_debug_msg);
186 #define sm_debug(msg...) do { if (option_debug) sm_printf(msg); } while (0)
188 #define sm_info(msg...) do { \
189 if (option_debug || (option_info && final_pass)) { \
190 sm_prefix(); \
191 sm_printf("info: "); \
192 sm_printf(msg); \
193 sm_printf("\n"); \
195 } while(0)
197 struct smatch_state *get_state(int owner, const char *name, struct symbol *sym);
198 struct smatch_state *get_state_expr(int owner, struct expression *expr);
199 struct state_list *get_possible_states(int owner, const char *name,
200 struct symbol *sym);
201 struct state_list *get_possible_states_expr(int owner, struct expression *expr);
202 struct sm_state *set_state(int owner, const char *name, struct symbol *sym,
203 struct smatch_state *state);
204 struct sm_state *set_state_expr(int owner, struct expression *expr,
205 struct smatch_state *state);
206 void delete_state(int owner, const char *name, struct symbol *sym);
207 void delete_state_expr(int owner, struct expression *expr);
208 void set_true_false_states(int owner, const char *name, struct symbol *sym,
209 struct smatch_state *true_state,
210 struct smatch_state *false_state);
211 void set_true_false_states_expr(int owner, struct expression *expr,
212 struct smatch_state *true_state,
213 struct smatch_state *false_state);
215 struct state_list *get_all_states_slist(int owner, struct state_list *source);
216 struct state_list *get_all_states(int id);
217 int is_reachable(void);
219 /* smatch_helper.c */
220 char *alloc_string(const char *str);
221 void free_string(char *str);
222 struct smatch_state *alloc_state_num(int num);
223 struct expression *get_argument_from_call_expr(struct expression_list *args,
224 int num);
226 char *expr_to_str(struct expression *expr);
227 char *expr_to_str_complex(struct expression *expr);
228 char *expr_to_str_sym_complex(struct expression *expr,
229 struct symbol **sym_ptr);
230 char *expr_to_str_sym(struct expression *expr,
231 struct symbol **sym_ptr);
233 int sym_name_is(const char *name, struct expression *expr);
234 int get_value(struct expression *expr, sval_t *val);
235 int get_implied_value(struct expression *expr, sval_t *val);
236 int get_implied_min(struct expression *expr, sval_t *sval);
237 int get_implied_max(struct expression *expr, sval_t *val);
238 int get_hard_max(struct expression *expr, sval_t *sval);
239 int get_fuzzy_min(struct expression *expr, sval_t *min);
240 int get_fuzzy_max(struct expression *expr, sval_t *max);
241 int get_absolute_min(struct expression *expr, sval_t *sval);
242 int get_absolute_max(struct expression *expr, sval_t *sval);
243 int parse_call_math(struct expression *expr, char *math, sval_t *val);
244 int is_zero(struct expression *expr);
245 int known_condition_true(struct expression *expr);
246 int known_condition_false(struct expression *expr);
247 int implied_condition_true(struct expression *expr);
248 int implied_condition_false(struct expression *expr);
250 int is_array(struct expression *expr);
251 struct expression *get_array_name(struct expression *expr);
252 struct expression *get_array_offset(struct expression *expr);
253 const char *show_state(struct smatch_state *state);
254 struct statement *get_expression_statement(struct expression *expr);
255 struct expression *strip_parens(struct expression *expr);
256 struct expression *strip_expr(struct expression *expr);
257 void scoped_state(int my_id, const char *name, struct symbol *sym);
258 int is_error_return(struct expression *expr);
259 int getting_address(void);
260 char *get_member_name(struct expression *expr);
261 char *get_fnptr_name(struct expression *expr);
262 int positions_eq(struct position pos1, struct position pos2);
263 struct statement *get_current_statement(void);
264 int get_param_num_from_sym(struct symbol *sym);
266 /* smatch_type.c */
267 struct symbol *get_real_base_type(struct symbol *sym);
268 int type_bits(struct symbol *type);
269 int type_positive_bits(struct symbol *type);
270 struct symbol *get_pointer_type(struct expression *expr);
271 struct symbol *get_type(struct expression *expr);
272 int type_unsigned(struct symbol *base_type);
273 int type_signed(struct symbol *base_type);
274 int expr_unsigned(struct expression *expr);
275 int returns_unsigned(struct symbol *base_type);
276 int is_pointer(struct expression *expr);
277 int returns_pointer(struct symbol *base_type);
278 sval_t sval_type_max(struct symbol *base_type);
279 sval_t sval_type_min(struct symbol *base_type);
280 int nr_bits(struct expression *expr);
281 int is_static(struct expression *expr);
282 int types_equiv(struct symbol *one, struct symbol *two);
283 const char *global_static();
284 struct symbol *cur_func_return_type(void);
285 struct symbol *get_arg_type(struct expression *fn, int arg);
286 struct symbol *get_member_type_from_key(struct expression *expr, char *key);
288 /* smatch_ignore.c */
289 void add_ignore(int owner, const char *name, struct symbol *sym);
290 int is_ignored(int owner, const char *name, struct symbol *sym);
292 /* smatch_tracker */
293 struct tracker *alloc_tracker(int owner, const char *name, struct symbol *sym);
294 void add_tracker(struct tracker_list **list, int owner, const char *name,
295 struct symbol *sym);
296 void add_tracker_expr(struct tracker_list **list, int owner, struct expression *expr);
297 void del_tracker(struct tracker_list **list, int owner, const char *name,
298 struct symbol *sym);
299 int in_tracker_list(struct tracker_list *list, int owner, const char *name,
300 struct symbol *sym);
301 void free_tracker_list(struct tracker_list **list);
302 void free_trackers_and_list(struct tracker_list **list);
304 /* smatch_conditions */
305 int in_condition(void);
307 /* smatch_flow.c */
309 void smatch (int argc, char **argv);
310 int inside_loop(void);
311 struct expression *get_switch_expr(void);
312 int in_expression_statement(void);
313 void __split_expr(struct expression *expr);
314 void __split_stmt(struct statement *stmt);
315 extern int option_assume_loops;
316 extern int option_known_conditions;
317 extern int option_two_passes;
318 extern int option_no_db;
319 extern int option_file_output;
320 extern struct expression_list *big_expression_stack;
321 extern struct statement_list *big_statement_stack;
322 extern int __in_pre_condition;
323 extern int __bail_on_rest_of_function;
325 /* smatch_conditions */
326 void __split_whole_condition(struct expression *expr);
327 void __handle_logic(struct expression *expr);
328 int is_condition(struct expression *expr);
329 int __handle_condition_assigns(struct expression *expr);
330 int __handle_select_assigns(struct expression *expr);
331 int __handle_expr_statement_assigns(struct expression *expr);
333 /* smatch_implied.c */
334 extern int option_debug_implied;
335 extern int option_debug_related;
336 extern int option_no_implied;
337 void get_implications(char *name, struct symbol *sym, int comparison, long long num,
338 struct state_list **true_states,
339 struct state_list **false_states);
340 struct range_list_stack;
341 struct state_list *__implied_case_slist(struct expression *switch_expr,
342 struct expression *case_expr,
343 struct range_list_stack **remaining_cases,
344 struct state_list **raw_slist);
345 struct range_list *__get_implied_values(struct expression *switch_expr);
346 void overwrite_states_using_pool(struct sm_state *sm);
348 /* smatch_extras.c */
349 #define SMATCH_EXTRA 1 /* this is my_id from smatch extra set in smatch.c */
351 struct data_range {
352 sval_t min;
353 sval_t max;
356 static const long long valid_ptr_max = LONG_MAX;
357 static const long long valid_ptr_min = 4096;
358 static const sval_t valid_ptr_max_sval = {
359 .type = &ptr_ctype,
360 .value = LONG_MAX,
362 static const sval_t valid_ptr_min_sval = {
363 .type = &ptr_ctype,
364 .value = 4096,
367 /* smatch_absolute.c */
368 int get_absolute_min_helper(struct expression *expr, sval_t *sval);
369 int get_absolute_max_helper(struct expression *expr, sval_t *sval);
371 /* smatch_states.c */
372 void __push_fake_cur_slist();
373 struct state_list *__pop_fake_cur_slist();
374 void __merge_slist_into_cur(struct state_list *slist);
376 int unreachable(void);
377 void __set_sm(struct sm_state *sm);
378 struct state_list *__get_cur_slist(void);
379 void __set_true_false_sm(struct sm_state *true_state,
380 struct sm_state *false_state);
381 void nullify_path(void);
382 void __match_nullify_path_hook(const char *fn, struct expression *expr,
383 void *unused);
384 void __unnullify_path(void);
385 int __path_is_null(void);
386 void clear_all_states(void);
388 struct sm_state *get_sm_state(int owner, const char *name,
389 struct symbol *sym);
390 struct sm_state *get_sm_state_expr(int owner, struct expression *expr);
391 void __push_true_states(void);
392 void __use_false_states(void);
393 void __discard_false_states(void);
394 void __merge_false_states(void);
395 void __merge_true_states(void);
397 void __negate_cond_stacks(void);
398 void __use_pre_cond_states(void);
399 void __use_cond_true_states(void);
400 void __use_cond_false_states(void);
401 void __push_cond_stacks(void);
402 struct state_list *__copy_cond_true_states(void);
403 struct state_list *__copy_cond_false_states(void);
404 struct state_list *__pop_cond_true_stack(void);
405 struct state_list *__pop_cond_false_stack(void);
406 void __and_cond_states(void);
407 void __or_cond_states(void);
408 void __save_pre_cond_states(void);
409 void __discard_pre_cond_states(void);
410 void __use_cond_states(void);
411 extern struct state_list *__last_base_slist;
413 void __warn_on_silly_pre_loops(void);
415 void __push_continues(void);
416 void __discard_continues(void);
417 void __process_continues(void);
418 void __merge_continues(void);
420 void __push_breaks(void);
421 void __process_breaks(void);
422 int __has_breaks(void);
423 void __merge_breaks(void);
424 void __use_breaks(void);
426 void __save_switch_states(struct expression *switch_expr);
427 void __discard_switches(void);
428 void __merge_switches(struct expression *switch_expr, struct expression *case_expr);
429 void __push_default(void);
430 void __set_default(void);
431 int __pop_default(void);
433 void __push_conditions(void);
434 void __discard_conditions(void);
436 void __save_gotos(const char *name);
437 void __merge_gotos(const char *name);
439 void __print_cur_slist(void);
441 /* smatch_hooks.c */
442 void __pass_to_client(void *data, enum hook_type type);
443 void __pass_to_client_no_data(enum hook_type type);
444 void __pass_case_to_client(struct expression *switch_expr,
445 struct expression *case_expr);
446 int __has_merge_function(int client_id);
447 struct smatch_state *__client_merge_function(int owner,
448 struct smatch_state *s1,
449 struct smatch_state *s2);
450 struct smatch_state *__client_unmatched_state_function(struct sm_state *sm);
451 void __push_scope_hooks(void);
452 void __call_scope_hooks(void);
454 /* smatch_function_hooks.c */
455 void create_function_hook_hash(void);
456 void __match_initializer_call(struct symbol *sym);
458 /* smatch_db.c */
459 enum info_type {
460 INTERNAL = 0,
461 PARAM_VALUE,
462 BUF_SIZE,
463 USER_DATA,
464 CAPPED_DATA,
465 RETURN_VALUE,
466 DEREFERENCE,
467 RANGE_CAP,
468 LOCK_HELD,
469 LOCK_RELEASED,
470 ABSOLUTE_LIMITS,
471 LIMITED_VALUE,
472 ADDED_VALUE,
473 FILTER_VALUE,
476 void add_definition_db_callback(void (*callback)(const char *name, struct symbol *sym, char *key, char *value), int type);
477 void add_member_info_callback(int owner, void (*callback)(char *fn, char *global_static, int param, char *printed_name, struct smatch_state *state));
478 void add_returned_state_callback(void (*fn)(int return_id, char *return_ranges, struct expression *returned_expr, struct state_list *slist));
479 void add_returned_member_callback(int owner, void (*callback)(int return_id, char *return_ranges, char *printed_name, struct smatch_state *state));
480 void add_db_fn_call_callback(int type, void (*callback)(struct expression *arg, char *value));
481 struct range_list *db_return_vals(struct expression *expr);
483 #define run_sql(call_back, sql...) \
484 do { \
485 char sql_txt[1024]; \
486 snprintf(sql_txt, 1024, sql); \
487 sm_debug("%s\n", sql_txt); \
488 sql_exec(call_back, sql_txt); \
489 } while (0)
491 void sql_exec(int (*callback)(void*, int, char**, char**), const char *sql);
492 void open_smatch_db(void);
494 /* smatch_files.c */
495 struct token *get_tokens_file(const char *filename);
497 /* smatch.c */
498 extern char *option_project_str;
499 extern char *data_dir;
500 extern int option_no_data;
501 extern int option_spammy;
502 extern int option_full_path;
503 extern int option_param_mapper;
504 extern int option_info;
505 extern int option_call_tree;
506 extern int num_checks;
508 enum project_type {
509 PROJ_NONE,
510 PROJ_KERNEL,
511 PROJ_WINE,
513 extern enum project_type option_project;
514 const char *check_name(unsigned short id);
517 /* smatch_buf_size.c */
518 int get_array_size(struct expression *expr);
519 int get_array_size_bytes(struct expression *expr);
520 int get_array_size_bytes_min(struct expression *expr);
522 /* smatch_capped.c */
523 int is_capped(struct expression *expr);
525 /* check_user_data.c */
526 int is_user_data(struct expression *expr);
528 /* check_locking.c */
529 void print_held_locks();
531 /* check_assigned_expr.c */
532 struct expression *get_assigned_expr(struct expression *expr);
534 /* smatch_sval.c */
535 sval_t *sval_alloc(sval_t sval);
536 sval_t *sval_alloc_permanent(sval_t sval);
537 sval_t sval_blank(struct expression *expr);
538 sval_t sval_type_val(struct symbol *type, long long val);
539 sval_t sval_from_val(struct expression *expr, long long val);
540 int sval_unsigned(sval_t sval);
541 int sval_signed(sval_t sval);
542 int sval_bits(sval_t sval);
543 int sval_positive_bits(sval_t sval);
544 int sval_bits_used(sval_t sval);
545 int sval_is_negative(sval_t sval);
546 int sval_is_positive(sval_t sval);
547 int sval_is_min(sval_t sval);
548 int sval_is_max(sval_t sval);
549 int sval_is_a_min(sval_t sval);
550 int sval_is_a_max(sval_t sval);
551 int sval_cmp(sval_t one, sval_t two);
552 int sval_cmp_t(struct symbol *type, sval_t one, sval_t two);
553 int sval_cmp_val(sval_t one, long long val);
554 int sval_too_low(struct symbol *type, sval_t sval);
555 int sval_too_high(struct symbol *type, sval_t sval);
556 int sval_fits(struct symbol *type, sval_t sval);
557 sval_t sval_cast(struct symbol *type, sval_t sval);
558 sval_t sval_preop(sval_t sval, int op);
559 sval_t sval_binop(sval_t left, int op, sval_t right);
560 int sval_binop_overflows(sval_t left, int op, sval_t right);
561 const char *sval_to_str(sval_t sval);
562 const char *sval_to_numstr(sval_t sval);
563 sval_t ll_to_sval(long long val);
565 #endif /* !SMATCH_H_ */