get_type() fix crash on getting pointer types.
[smatch.git] / smatch.h
blob21141b5e4213ed5bd271fbd27e588156bec99763
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_min(struct expression *expr, long long *min);
186 int get_fuzzy_max(struct expression *expr, long long *max);
187 int is_zero(struct expression *expr);
188 int is_array(struct expression *expr);
189 struct expression *get_array_name(struct expression *expr);
190 struct expression *get_array_offset(struct expression *expr);
191 const char *show_state(struct smatch_state *state);
192 struct statement *get_block_thing(struct expression *expr);
193 struct expression *strip_parens(struct expression *expr);
194 struct expression *strip_expr(struct expression *expr);
195 void scoped_state(int my_id, const char *name, struct symbol *sym);
196 int is_error_return(struct expression *expr);
197 int getting_address(void);
199 /* smatch_type.c */
200 struct symbol *get_type(struct expression *expr);
201 int type_unsigned(struct symbol *base_type);
202 long long type_max(struct symbol *base_type);
203 long long type_min(struct symbol *base_type);
205 /* smatch_ignore.c */
206 void add_ignore(int owner, const char *name, struct symbol *sym);
207 int is_ignored(int owner, const char *name, struct symbol *sym);
209 /* smatch_tracker */
210 struct tracker *alloc_tracker(int owner, const char *name, struct symbol *sym);
211 void add_tracker(struct tracker_list **list, int owner, const char *name,
212 struct symbol *sym);
213 void del_tracker(struct tracker_list **list, int owner, const char *name,
214 struct symbol *sym);
215 int in_tracker_list(struct tracker_list *list, int owner, const char *name,
216 struct symbol *sym);
217 void free_tracker_list(struct tracker_list **list);
218 void free_trackers_and_list(struct tracker_list **list);
220 /* smatch_conditions */
221 int in_condition(void);
223 /* smatch_flow.c */
225 void smatch (int argc, char **argv);
226 void __split_expr(struct expression *expr);
227 void __split_statements(struct statement *stmt);
228 extern int option_assume_loops;
229 extern int option_known_conditions;
230 extern int option_two_passes;
231 extern struct symbol *cur_func_sym;
232 extern struct expression_list *big_expression_stack;
233 extern struct statement_list *big_statement_stack;
234 extern int __in_pre_condition;
235 extern int __bail_on_rest_of_function;
237 /* smatch_conditions */
238 void __split_whole_condition(struct expression *expr);
239 int __handle_condition_assigns(struct expression *expr);
241 /* smatch_implied.c */
242 extern int option_debug_implied;
243 extern int option_no_implied;
244 void get_implications(char *name, struct symbol *sym, int comparison, long long num,
245 struct state_list **true_states,
246 struct state_list **false_states);
247 struct range_list_stack;
248 struct state_list *__implied_case_slist(struct expression *switch_expr,
249 struct expression *case_expr,
250 struct range_list_stack **remaining_cases,
251 struct state_list **raw_slist);
252 struct range_list *__get_implied_values(struct expression *switch_expr);
254 /* smatch_extras.c */
255 #define SMATCH_EXTRA 1 /* this is my_id from smatch extra set in smatch.c */
257 struct data_range {
258 long long min;
259 long long max;
261 extern struct data_range whole_range;
263 int get_implied_single_val(struct expression *expr, long long *val);
264 int get_implied_single_fuzzy_min(struct expression *expr, long long *min);
265 int get_implied_single_fuzzy_max(struct expression *expr, long long *max);
266 int get_implied_max(struct expression *expr, long long *val);
267 int get_implied_min(struct expression *expr, long long *val);
268 int known_condition_true(struct expression *expr);
269 int known_condition_false(struct expression *expr);
270 int implied_condition_true(struct expression *expr);
271 int implied_condition_false(struct expression *expr);
273 /* smatch_states.c */
274 extern int option_debug;
276 extern int __fake_cur;
277 extern struct state_list *__fake_cur_slist;
278 extern int __fake_conditions;
279 extern struct state_list *__fake_cond_true;
280 extern struct state_list *__fake_cond_false;
283 int unreachable(void);
284 void __set_state(struct sm_state *sm);
285 struct state_list *__get_cur_slist(void);
286 void __set_true_false_sm(struct sm_state *true_state,
287 struct sm_state *false_state);
288 void nullify_path(void);
289 void __match_nullify_path_hook(const char *fn, struct expression *expr,
290 void *unused);
291 void __unnullify_path(void);
292 int __path_is_null(void);
293 void clear_all_states(void);
295 struct sm_state *get_sm_state(int owner, const char *name,
296 struct symbol *sym);
297 struct sm_state *get_sm_state_expr(int owner, struct expression *expr);
298 void __push_true_states(void);
299 void __use_false_states(void);
300 void __pop_false_states(void);
301 void __merge_false_states(void);
302 void __merge_true_states(void);
304 void __negate_cond_stacks(void);
305 void __save_false_states_for_later(void);
306 void __use_previously_stored_false_states(void);
307 void __use_cond_true_states(void);
308 void __use_cond_false_states(void);
309 void __push_cond_stacks(void);
310 void __and_cond_states(void);
311 void __or_cond_states(void);
312 void __save_pre_cond_states(void);
313 void __pop_pre_cond_states(void);
314 void __use_cond_states(void);
316 void __warn_on_silly_pre_loops(void);
318 void __push_continues(void);
319 void __pop_continues(void);
320 void __process_continues(void);
321 void __merge_continues(void);
323 void __push_breaks(void);
324 void __process_breaks(void);
325 void __merge_breaks(void);
326 void __use_breaks(void);
328 void __save_switch_states(struct expression *switch_expr);
329 void __pop_switches(void);
330 void __merge_switches(struct expression *switch_expr, struct expression *case_expr);
331 void __push_default(void);
332 void __set_default(void);
333 int __pop_default(void);
335 void __push_conditions(void);
336 void __pop_conditions(void);
338 void __save_gotos(const char *name);
339 void __merge_gotos(const char *name);
341 void __print_cur_slist(void);
343 /* smatch_hooks.c */
344 void __pass_to_client(void *data, enum hook_type type);
345 void __pass_to_client_no_data(enum hook_type type);
346 void __pass_case_to_client(struct expression *switch_expr,
347 struct expression *case_expr);
348 int __has_merge_function(int client_id);
349 struct smatch_state *__client_merge_function(int owner, const char *name,
350 struct symbol *sym,
351 struct smatch_state *s1,
352 struct smatch_state *s2);
353 struct smatch_state *__client_unmatched_state_function(struct sm_state *sm);
354 void __push_scope_hooks(void);
355 void __call_scope_hooks(void);
357 /* smatch_function_hooks.c */
358 void create_function_hook_hash(void);
359 void __match_initializer_call(struct symbol *sym);
361 /* smatch_files.c */
362 struct token *get_tokens_file(const char *filename);
364 /* smatch_oom.c */
365 extern int option_oom_kb;
366 int out_of_memory(void);
368 /* smatch.c */
369 extern char *option_project_str;
370 extern char *data_dir;
371 extern int option_no_data;
372 extern int option_spammy;
373 extern int option_full_path;
374 extern int option_param_mapper;
375 extern int option_info;
376 extern int option_call_tree;
377 extern int num_checks;
379 enum project_type {
380 PROJ_NONE,
381 PROJ_KERNEL,
382 PROJ_WINE,
384 extern enum project_type option_project;
385 const char *check_name(unsigned short id);
387 #endif /* !SMATCH_H_ */