type: cleanp get_member_symbol()
[smatch.git] / smatch.h
blob744918b456bb6791321407c0d75c677f8faa4319
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 END_FUNC_HOOK,
100 RETURN_HOOK,
101 END_FILE_HOOK,
104 #define TRUE 1
105 #define FALSE 0
107 struct range_list;
109 void add_hook(void *func, enum hook_type type);
110 typedef struct smatch_state *(merge_func_t)(struct smatch_state *s1, struct smatch_state *s2);
111 typedef struct smatch_state *(unmatched_func_t)(struct sm_state *state);
112 void add_merge_hook(int client_id, merge_func_t *func);
113 void add_unmatched_state_hook(int client_id, unmatched_func_t *func);
114 typedef void (scope_hook)(void *data);
115 void add_scope_hook(scope_hook *hook, void *data);
116 typedef void (func_hook)(const char *fn, struct expression *expr, void *data);
117 typedef void (implication_hook)(const char *fn, struct expression *call_expr,
118 struct expression *assign_expr, void *data);
119 typedef void (return_implies_hook)(struct expression *call_expr,
120 int param, char *key, char *value);
121 typedef int (implied_return_hook)(struct expression *call_expr, void *info, struct range_list **rl);
122 void add_function_hook(const char *look_for, func_hook *call_back, void *data);
124 void add_function_assign_hook(const char *look_for, func_hook *call_back,
125 void *info);
126 void add_implied_return_hook(const char *look_for,
127 implied_return_hook *call_back,
128 void *info);
129 void add_macro_assign_hook(const char *look_for, func_hook *call_back,
130 void *info);
131 void add_macro_assign_hook_extra(const char *look_for, func_hook *call_back,
132 void *info);
133 void return_implies_state(const char *look_for, long long start, long long end,
134 implication_hook *call_back, void *info);
135 void add_db_return_implies_callback(int type, return_implies_hook *callback);
136 void add_db_return_states_callback(int type, return_implies_hook *callback);
137 int get_implied_return(struct expression *expr, struct range_list **rl);
139 typedef void (modification_hook)(struct sm_state *sm);
140 void add_modification_hook(int owner, modification_hook *call_back);
141 void add_indirect_modification_hook(int owner, modification_hook *call_back);
143 const char *get_filename(void);
144 char *get_function(void);
145 int get_lineno(void);
146 extern int final_pass;
147 extern struct symbol *cur_func_sym;
148 extern int option_debug;
149 extern int local_debug;
151 extern FILE *sm_outfd;
152 #define sm_printf(msg...) do { if (final_pass) fprintf(sm_outfd, msg); } while (0)
154 static inline void sm_prefix(void)
156 sm_printf("%s:%d %s() ", get_filename(), get_lineno(), get_function());
159 static inline void print_implied_debug_msg();
161 #define sm_msg(msg...) \
162 do { \
163 print_implied_debug_msg(); \
164 if (!option_debug && !final_pass) \
165 break; \
166 sm_prefix(); \
167 sm_printf(msg); \
168 sm_printf("\n"); \
169 } while (0)
171 extern char *implied_debug_msg;
172 static inline void print_implied_debug_msg()
174 static struct symbol *last_printed = NULL;
176 if (!implied_debug_msg)
177 return;
178 if (last_printed == cur_func_sym)
179 return;
180 last_printed = cur_func_sym;
181 sm_msg("%s", implied_debug_msg);
184 #define sm_debug(msg...) do { if (option_debug) sm_printf(msg); } while (0)
186 #define sm_info(msg...) do { \
187 if (option_debug || (option_info && final_pass)) { \
188 sm_prefix(); \
189 sm_printf("info: "); \
190 sm_printf(msg); \
191 sm_printf("\n"); \
193 } while(0)
195 struct smatch_state *get_state(int owner, const char *name, struct symbol *sym);
196 struct smatch_state *get_state_expr(int owner, struct expression *expr);
197 struct state_list *get_possible_states(int owner, const char *name,
198 struct symbol *sym);
199 struct state_list *get_possible_states_expr(int owner, struct expression *expr);
200 struct sm_state *set_state(int owner, const char *name, struct symbol *sym,
201 struct smatch_state *state);
202 struct sm_state *set_state_expr(int owner, struct expression *expr,
203 struct smatch_state *state);
204 void delete_state(int owner, const char *name, struct symbol *sym);
205 void delete_state_expr(int owner, struct expression *expr);
206 void set_true_false_states(int owner, const char *name, struct symbol *sym,
207 struct smatch_state *true_state,
208 struct smatch_state *false_state);
209 void set_true_false_states_expr(int owner, struct expression *expr,
210 struct smatch_state *true_state,
211 struct smatch_state *false_state);
213 struct state_list *get_all_states(int id);
214 int is_reachable(void);
216 /* smatch_helper.c */
217 char *alloc_string(const char *str);
218 void free_string(char *str);
219 struct smatch_state *alloc_state_num(int num);
220 struct expression *get_argument_from_call_expr(struct expression_list *args,
221 int num);
222 char *get_variable_from_expr_complex(struct expression *expr,
223 struct symbol **sym_ptr);
224 char *get_variable_from_expr(struct expression *expr,
225 struct symbol **sym_ptr);
226 int sym_name_is(const char *name, struct expression *expr);
227 int get_value(struct expression *expr, sval_t *val);
228 int get_implied_value(struct expression *expr, sval_t *val);
229 int get_implied_min(struct expression *expr, sval_t *sval);
230 int get_implied_max(struct expression *expr, sval_t *val);
231 int get_hard_max(struct expression *expr, sval_t *sval);
232 int get_fuzzy_min(struct expression *expr, sval_t *min);
233 int get_fuzzy_max(struct expression *expr, sval_t *max);
234 int get_absolute_min(struct expression *expr, sval_t *sval);
235 int get_absolute_max(struct expression *expr, sval_t *sval);
236 int parse_call_math(struct expression *expr, char *math, sval_t *val);
237 int is_zero(struct expression *expr);
238 int known_condition_true(struct expression *expr);
239 int known_condition_false(struct expression *expr);
240 int implied_condition_true(struct expression *expr);
241 int implied_condition_false(struct expression *expr);
243 int is_array(struct expression *expr);
244 struct expression *get_array_name(struct expression *expr);
245 struct expression *get_array_offset(struct expression *expr);
246 const char *show_state(struct smatch_state *state);
247 struct statement *get_expression_statement(struct expression *expr);
248 struct expression *strip_parens(struct expression *expr);
249 struct expression *strip_expr(struct expression *expr);
250 void scoped_state(int my_id, const char *name, struct symbol *sym);
251 int is_error_return(struct expression *expr);
252 int getting_address(void);
253 char *get_member_name(struct expression *expr);
254 char *get_fnptr_name(struct expression *expr);
255 int positions_eq(struct position pos1, struct position pos2);
256 struct statement *get_current_statement(void);
258 /* smatch_type.c */
259 struct symbol *get_real_base_type(struct symbol *sym);
260 int type_bits(struct symbol *type);
261 int type_positive_bits(struct symbol *type);
262 struct symbol *get_pointer_type(struct expression *expr);
263 struct symbol *get_type(struct expression *expr);
264 int type_unsigned(struct symbol *base_type);
265 int type_signed(struct symbol *base_type);
266 int expr_unsigned(struct expression *expr);
267 int returns_unsigned(struct symbol *base_type);
268 int is_pointer(struct expression *expr);
269 int returns_pointer(struct symbol *base_type);
270 sval_t sval_type_max(struct symbol *base_type);
271 sval_t sval_type_min(struct symbol *base_type);
272 int nr_bits(struct expression *expr);
273 int is_static(struct expression *expr);
274 int types_equiv(struct symbol *one, struct symbol *two);
275 const char *global_static();
276 struct symbol *cur_func_return_type(void);
277 struct symbol *get_arg_type(struct expression *fn, int arg);
278 struct symbol *get_param_type_from_key(struct symbol *sym, char *key);
280 /* smatch_ignore.c */
281 void add_ignore(int owner, const char *name, struct symbol *sym);
282 int is_ignored(int owner, const char *name, struct symbol *sym);
284 /* smatch_tracker */
285 struct tracker *alloc_tracker(int owner, const char *name, struct symbol *sym);
286 void add_tracker(struct tracker_list **list, int owner, const char *name,
287 struct symbol *sym);
288 void add_tracker_expr(struct tracker_list **list, int owner, struct expression *expr);
289 void del_tracker(struct tracker_list **list, int owner, const char *name,
290 struct symbol *sym);
291 int in_tracker_list(struct tracker_list *list, int owner, const char *name,
292 struct symbol *sym);
293 void free_tracker_list(struct tracker_list **list);
294 void free_trackers_and_list(struct tracker_list **list);
296 /* smatch_conditions */
297 int in_condition(void);
299 /* smatch_flow.c */
301 void smatch (int argc, char **argv);
302 int inside_loop(void);
303 int in_expression_statement(void);
304 void __split_expr(struct expression *expr);
305 void __split_stmt(struct statement *stmt);
306 extern int option_assume_loops;
307 extern int option_known_conditions;
308 extern int option_two_passes;
309 extern int option_no_db;
310 extern int option_file_output;
311 extern struct expression_list *big_expression_stack;
312 extern struct statement_list *big_statement_stack;
313 extern int __in_pre_condition;
314 extern int __bail_on_rest_of_function;
316 /* smatch_conditions */
317 void __split_whole_condition(struct expression *expr);
318 void __handle_logic(struct expression *expr);
319 int __is_condition_assign(struct expression *expr);
320 int __handle_condition_assigns(struct expression *expr);
321 int __handle_select_assigns(struct expression *expr);
322 int __handle_expr_statement_assigns(struct expression *expr);
324 /* smatch_implied.c */
325 extern int option_debug_implied;
326 extern int option_debug_related;
327 extern int option_no_implied;
328 void get_implications(char *name, struct symbol *sym, int comparison, long long num,
329 struct state_list **true_states,
330 struct state_list **false_states);
331 struct range_list_stack;
332 struct state_list *__implied_case_slist(struct expression *switch_expr,
333 struct expression *case_expr,
334 struct range_list_stack **remaining_cases,
335 struct state_list **raw_slist);
336 struct range_list *__get_implied_values(struct expression *switch_expr);
338 /* smatch_extras.c */
339 #define SMATCH_EXTRA 1 /* this is my_id from smatch extra set in smatch.c */
341 struct data_range {
342 sval_t min;
343 sval_t max;
346 static const long long valid_ptr_max = LONG_MAX;
347 static const long long valid_ptr_min = 4096;
348 static const sval_t valid_ptr_max_sval = {
349 .type = &ptr_ctype,
350 .value = LONG_MAX,
352 static const sval_t valid_ptr_min_sval = {
353 .type = &ptr_ctype,
354 .value = 4096,
357 /* smatch_absolute.c */
358 int get_absolute_min_helper(struct expression *expr, sval_t *sval);
359 int get_absolute_max_helper(struct expression *expr, sval_t *sval);
361 /* smatch_states.c */
362 void __push_fake_cur_slist();
363 struct state_list *__pop_fake_cur_slist();
364 void __merge_slist_into_cur(struct state_list *slist);
366 int unreachable(void);
367 void __set_sm(struct sm_state *sm);
368 struct state_list *__get_cur_slist(void);
369 void __set_true_false_sm(struct sm_state *true_state,
370 struct sm_state *false_state);
371 void nullify_path(void);
372 void __match_nullify_path_hook(const char *fn, struct expression *expr,
373 void *unused);
374 void __unnullify_path(void);
375 int __path_is_null(void);
376 void clear_all_states(void);
378 struct sm_state *get_sm_state(int owner, const char *name,
379 struct symbol *sym);
380 struct sm_state *get_sm_state_expr(int owner, struct expression *expr);
381 void __push_true_states(void);
382 void __use_false_states(void);
383 void __discard_false_states(void);
384 void __merge_false_states(void);
385 void __merge_true_states(void);
387 void __negate_cond_stacks(void);
388 void __use_pre_cond_states(void);
389 void __use_cond_true_states(void);
390 void __use_cond_false_states(void);
391 void __push_cond_stacks(void);
392 struct state_list *__copy_cond_true_states(void);
393 struct state_list *__copy_cond_false_states(void);
394 struct state_list *__pop_cond_true_stack(void);
395 struct state_list *__pop_cond_false_stack(void);
396 void __and_cond_states(void);
397 void __or_cond_states(void);
398 void __save_pre_cond_states(void);
399 void __discard_pre_cond_states(void);
400 void __use_cond_states(void);
401 extern struct state_list *__last_base_slist;
403 void __warn_on_silly_pre_loops(void);
405 void __push_continues(void);
406 void __discard_continues(void);
407 void __process_continues(void);
408 void __merge_continues(void);
410 void __push_breaks(void);
411 void __process_breaks(void);
412 void __merge_breaks(void);
413 void __use_breaks(void);
415 void __save_switch_states(struct expression *switch_expr);
416 void __discard_switches(void);
417 void __merge_switches(struct expression *switch_expr, struct expression *case_expr);
418 void __push_default(void);
419 void __set_default(void);
420 int __pop_default(void);
422 void __push_conditions(void);
423 void __discard_conditions(void);
425 void __save_gotos(const char *name);
426 void __merge_gotos(const char *name);
428 void __print_cur_slist(void);
430 /* smatch_hooks.c */
431 void __pass_to_client(void *data, enum hook_type type);
432 void __pass_to_client_no_data(enum hook_type type);
433 void __pass_case_to_client(struct expression *switch_expr,
434 struct expression *case_expr);
435 int __has_merge_function(int client_id);
436 struct smatch_state *__client_merge_function(int owner,
437 struct smatch_state *s1,
438 struct smatch_state *s2);
439 struct smatch_state *__client_unmatched_state_function(struct sm_state *sm);
440 void __push_scope_hooks(void);
441 void __call_scope_hooks(void);
443 /* smatch_function_hooks.c */
444 void create_function_hook_hash(void);
445 void __match_initializer_call(struct symbol *sym);
447 /* smatch_db.c */
448 enum info_type {
449 INTERNAL = 0,
450 PARAM_VALUE,
451 BUF_SIZE,
452 USER_DATA,
453 CAPPED_DATA,
454 RETURN_VALUE,
455 DEREFERENCE,
456 RANGE_CAP,
457 LOCK_HELD,
458 LOCK_RELEASED,
459 ABSOLUTE_LIMITS,
462 int get_return_id(void);
463 void add_definition_db_callback(void (*callback)(const char *name, struct symbol *sym, char *key, char *value), int type);
464 void add_member_info_callback(int owner, void (*callback)(char *fn, char *global_static, int param, char *printed_name, struct smatch_state *state));
465 void add_db_fn_call_callback(int type, void (*callback)(struct expression *arg, char *value));
466 struct range_list *db_return_vals(struct expression *expr);
468 #define run_sql(call_back, sql...) \
469 do { \
470 char sql_txt[1024]; \
471 snprintf(sql_txt, 1024, sql); \
472 sm_debug("%s\n", sql_txt); \
473 sql_exec(call_back, sql_txt); \
474 } while (0)
476 void sql_exec(int (*callback)(void*, int, char**, char**), const char *sql);
477 void open_smatch_db(void);
479 /* smatch_files.c */
480 struct token *get_tokens_file(const char *filename);
482 /* smatch.c */
483 extern char *option_project_str;
484 extern char *data_dir;
485 extern int option_no_data;
486 extern int option_spammy;
487 extern int option_full_path;
488 extern int option_param_mapper;
489 extern int option_info;
490 extern int option_call_tree;
491 extern int num_checks;
493 enum project_type {
494 PROJ_NONE,
495 PROJ_KERNEL,
496 PROJ_WINE,
498 extern enum project_type option_project;
499 const char *check_name(unsigned short id);
502 /* smatch_buf_size.c */
503 int get_array_size(struct expression *expr);
504 int get_array_size_bytes(struct expression *expr);
505 int get_array_size_bytes_min(struct expression *expr);
507 /* smatch_capped.c */
508 int is_capped(struct expression *expr);
510 /* check_user_data.c */
511 int is_user_data(struct expression *expr);
513 /* check_locking.c */
514 void print_held_locks();
516 /* check_assigned_expr.c */
517 struct expression *get_assigned_expr(struct expression *expr);
519 /* smatch_sval.c */
520 sval_t *sval_alloc(sval_t sval);
521 sval_t *sval_alloc_permanent(sval_t sval);
522 sval_t sval_blank(struct expression *expr);
523 sval_t sval_type_val(struct symbol *type, long long val);
524 sval_t sval_from_val(struct expression *expr, long long val);
525 int sval_unsigned(sval_t sval);
526 int sval_signed(sval_t sval);
527 int sval_bits(sval_t sval);
528 int sval_positive_bits(sval_t sval);
529 int sval_bits_used(sval_t sval);
530 int sval_is_negative(sval_t sval);
531 int sval_is_positive(sval_t sval);
532 int sval_is_min(sval_t sval);
533 int sval_is_max(sval_t sval);
534 int sval_is_a_min(sval_t sval);
535 int sval_is_a_max(sval_t sval);
536 int sval_cmp(sval_t one, sval_t two);
537 int sval_cmp_t(struct symbol *type, sval_t one, sval_t two);
538 int sval_cmp_val(sval_t one, long long val);
539 int sval_too_low(struct symbol *type, sval_t sval);
540 int sval_too_high(struct symbol *type, sval_t sval);
541 int sval_fits(struct symbol *type, sval_t sval);
542 sval_t sval_cast(struct symbol *type, sval_t sval);
543 sval_t sval_preop(sval_t sval, int op);
544 sval_t sval_binop(sval_t left, int op, sval_t right);
545 const char *sval_to_str(sval_t sval);
546 const char *sval_to_numstr(sval_t sval);
547 sval_t ll_to_sval(long long val);
549 #endif /* !SMATCH_H_ */