check_locking: move to the new function hook.
[smatch.git] / smatch.h
blob9f812e570b3c7cd56889c3f04e9a5055e35b1151
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 #define KERNEL
22 #define MAXSMSTATES 1000000 /* Limits RAM use. Give up on big functions */
23 struct smatch_state {
24 const char *name;
25 void *data;
27 #define STATE(_x) static struct smatch_state _x = { .name = #_x }
28 extern struct smatch_state undefined;
29 extern struct smatch_state merged;
30 extern struct smatch_state true_state;
31 extern struct smatch_state false_state;
33 struct sm_state {
34 char *name;
35 unsigned int owner;
36 struct symbol *sym;
37 struct smatch_state *state;
38 int line;
39 struct state_list_stack *my_pools;
40 struct state_list_stack *all_pools;
41 struct state_list *possible;
44 enum hook_type {
45 EXPR_HOOK,
46 STMT_HOOK,
47 SYM_HOOK,
48 DECLARATION_HOOK,
49 ASSIGNMENT_HOOK,
50 ASSIGNMENT_AFTER_HOOK,
51 CONDITION_HOOK,
52 WHOLE_CONDITION_HOOK,
53 FUNCTION_CALL_HOOK,
54 FUNCTION_CALL_AFTER_HOOK,
55 OP_HOOK,
56 DEREF_HOOK,
57 BASE_HOOK,
58 FUNC_DEF_HOOK,
59 END_FUNC_HOOK,
60 RETURN_HOOK,
61 END_FILE_HOOK,
63 void add_hook(void *func, enum hook_type type);
64 typedef struct smatch_state *(merge_func_t)(const char *name,
65 struct symbol *sym,
66 struct smatch_state *s1,
67 struct smatch_state *s2);
68 typedef struct smatch_state *(unmatched_func_t)(struct sm_state *state);
69 void add_merge_hook(int client_id, merge_func_t *func);
70 void add_unmatched_state_hook(int client_id, unmatched_func_t *func);
71 void add_function_hook(const char *lock_for, void *call_back, void *data);
73 #define smatch_msg(msg...) \
74 do { \
75 printf("%s +%d %s(%d) ", get_filename(), get_lineno(), \
76 get_function(), get_func_pos()); \
77 printf(msg); \
78 printf("\n"); \
79 } while (0)
81 #define SM_DEBUG(msg...) do { if (debug_states) printf(msg); } while (0)
82 #define DIMPLIED(msg...) do { if (debug_implied_states) printf(msg); } while (0)
84 #define UNDEFINED INT_MIN
86 struct smatch_state *get_state(const char *name, int owner, struct symbol *sym);
87 struct state_list *get_possible_states(const char *name, int owner,
88 struct symbol *sym);
89 void set_state(const char *name, int owner, struct symbol *sym,
90 struct smatch_state *state);
91 void delete_state(const char *name, int owner, struct symbol *sym);
92 void set_true_false_states(const char *name, int owner, struct symbol *sym,
93 struct smatch_state *true_state,
94 struct smatch_state *false_state);
96 struct state_list *get_all_states(int id);
97 int is_reachable();
98 char *get_filename();
99 char *get_function();
100 int get_lineno();
101 int get_func_pos();
103 /* smatch_helper.c */
104 char *alloc_string(const char *str);
105 void free_string(char *str);
106 struct expression *get_argument_from_call_expr(struct expression_list *args,
107 int num);
108 char *get_variable_from_expr_complex(struct expression *expr,
109 struct symbol **sym_ptr);
110 char *get_variable_from_expr(struct expression *expr,
111 struct symbol **sym_ptr);
112 int sym_name_is(const char *name, struct expression *expr);
113 int get_value(struct expression *expr);
114 int is_zero(struct expression *expr);
115 const char *show_state(struct smatch_state *state);
116 struct statement *get_block_thing(struct expression *expr);
117 struct expression *strip_expr(struct expression *expr);
119 /* smatch_ignore.c */
120 struct tracker {
121 char *name;
122 int owner;
123 struct symbol *sym;
125 DECLARE_PTR_LIST(tracker_list, struct tracker);
127 void add_ignore(const char *name, int owner, struct symbol *sym);
128 int is_ignored(const char *name, int owner, struct symbol *sym);
130 /* smatch_tracker */
131 void add_tracker(struct tracker_list **list, const char *name, int owner,
132 struct symbol *sym);
133 int in_tracker_list(struct tracker_list *list, const char *name, int owner,
134 struct symbol *sym);
135 void free_tracker_list(struct tracker_list **list);
136 void free_trackers_and_list(struct tracker_list **list);
138 /* smatch_conditions */
139 int in_condition();
141 /* ----------------------------------------------------------------
142 The stuff below is all used internally and shouldn't
143 be called from other programs
144 -----------------------------------------------------------------*/
146 /* smatch_flow.c */
148 void smatch (int argc, char **argv);
149 void __split_expr(struct expression *expr);
150 void __split_statements(struct statement *stmt);
151 extern int option_assume_loops;
152 extern int option_known_conditions;
154 /* smatch_conditions */
155 void __split_whole_condition(struct expression *expr);
157 /* smatch_implied.c */
158 extern int debug_implied_states;
159 extern int option_no_implied;
161 /* smatch_extras.c */
162 #define SMATCH_EXTRA 1 /* this is my_id from smatch extra set in smatch.c */
163 int true_comparison(int left, int comparison, int right);
164 int known_condition_true(struct expression *expr);
165 int known_condition_false(struct expression *expr);
167 /* smatch_states.c */
168 extern int debug_states;
170 struct state_list *__get_cur_slist();
171 void __set_true_false_sm(struct sm_state *true_state,
172 struct sm_state *false_state);
173 void nullify_path();
174 void __unnullify_path();
175 int __path_is_null();
176 void clear_all_states();
178 struct sm_state *get_sm_state(const char *name, int owner,
179 struct symbol *sym);
180 void __use_false_only_stack();
181 void __pop_false_only_stack();
182 void __push_true_states();
183 void __use_false_states();
184 void __pop_false_states();
185 void __merge_false_states();
186 void __merge_true_states();
188 void __negate_cond_stacks();
189 void __save_false_states_for_later();
190 void __use_previously_stored_false_states();
191 void __use_cond_true_states();
192 void __use_cond_false_states();
193 void __push_cond_stacks();
194 void __and_cond_states();
195 void __or_cond_states();
196 void __save_pre_cond_states();
197 void __pop_pre_cond_states();
198 void __use_cond_states();
200 void __warn_on_silly_pre_loops();
202 void __push_continues();
203 void __pop_continues();
204 void __process_continues();
205 void __merge_continues();
207 void __push_breaks();
208 void __process_breaks();
209 void __merge_breaks();
210 void __use_breaks();
212 void __save_switch_states();
213 void __pop_switches();
214 void __merge_switches();
215 void __push_default();
216 void __set_default();
217 int __pop_default();
219 void __push_conditions();
220 void __pop_conditions();
222 void __save_gotos(const char *name);
223 void __merge_gotos(const char *name);
225 void __print_cur_slist();
227 /* smatch_hooks.c */
228 void __pass_to_client(void *data, enum hook_type type);
229 void __pass_to_client_no_data(enum hook_type type);
230 void __pass_declarations_to_client(struct symbol_list *sym_list);
231 int __has_merge_function(int client_id);
232 struct smatch_state *__client_merge_function(int owner, const char *name,
233 struct symbol *sym,
234 struct smatch_state *s1,
235 struct smatch_state *s2);
236 struct smatch_state *__client_unmatched_state_function(struct sm_state *sm);
238 #endif /* !SMATCH_H_ */