"while ((1)) {" is a forever loop. Call strip_expr().
[smatch.git] / smatch.h
blob9ac800f22ee8e90201ac95598be0b1689815ac40
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 struct smatch_state {
23 const char *name;
24 void *data;
26 #define STATE(_x) static struct smatch_state _x = { .name = #_x }
27 extern struct smatch_state undefined;
28 extern struct smatch_state merged;
29 extern struct smatch_state true_state;
30 extern struct smatch_state false_state;
32 enum hook_type {
33 EXPR_HOOK,
34 STMT_HOOK,
35 SYM_HOOK,
36 DECLARATION_HOOK,
37 ASSIGNMENT_HOOK,
38 ASSIGNMENT_AFTER_HOOK,
39 CONDITION_HOOK,
40 WHOLE_CONDITION_HOOK,
41 FUNCTION_CALL_HOOK,
42 FUNCTION_CALL_AFTER_HOOK,
43 OP_HOOK,
44 DEREF_HOOK,
45 BASE_HOOK,
46 FUNC_DEF_HOOK,
47 END_FUNC_HOOK,
48 RETURN_HOOK,
49 END_FILE_HOOK,
51 void add_hook(void *func, enum hook_type type);
52 typedef struct smatch_state *(merge_func_t)(const char *name,
53 struct symbol *sym,
54 struct smatch_state *s1,
55 struct smatch_state *s2);
56 void add_merge_hook(int client_id, merge_func_t *func);
58 #define smatch_msg(msg...) \
59 do { \
60 printf("%s +%d %s(%d) ", get_filename(), get_lineno(), \
61 get_function(), get_func_pos()); \
62 printf(msg); \
63 printf("\n"); \
64 } while (0)
66 #define SM_DEBUG(msg...) do { if (debug_states) printf(msg); } while (0)
68 #define UNDEFINED INT_MIN
70 struct smatch_state *get_state(const char *name, int owner, struct symbol *sym);
71 struct state_list *get_possible_states(const char *name, int owner,
72 struct symbol *sym);
73 void set_state(const char *name, int owner, struct symbol *sym,
74 struct smatch_state *state);
75 void delete_state(const char *name, int owner, struct symbol *sym);
76 void set_true_false_states(const char *name, int owner, struct symbol *sym,
77 struct smatch_state *true_state,
78 struct smatch_state *false_state);
80 struct state_list *get_all_states(int id);
81 char *get_filename();
82 char *get_function();
83 int get_lineno();
84 int get_func_pos();
86 /* smatch_helper.c */
87 char *alloc_string(char *str);
88 void free_string(char *str);
89 struct expression *get_argument_from_call_expr(struct expression_list *args,
90 int num);
91 char *get_variable_from_expr_complex(struct expression *expr,
92 struct symbol **sym_ptr);
93 char *get_variable_from_expr(struct expression *expr,
94 struct symbol **sym_ptr);
95 int sym_name_is(const char *name, struct expression *expr);
96 int get_value(struct expression *expr);
97 int is_zero(struct expression *expr);
98 const char *show_state(struct smatch_state *state);
99 struct expression *strip_expr(struct expression *expr);
101 /* smatch_ignore.c */
102 struct tracker {
103 const char *name;
104 int owner;
105 struct symbol *sym;
107 DECLARE_PTR_LIST(tracker_list, struct tracker);
109 void add_ignore(const char *name, int owner, struct symbol *sym);
110 int is_ignored(const char *name, int owner, struct symbol *sym);
112 /* smatch_conditions */
113 int in_condition();
115 /* ----------------------------------------------------------------
116 The stuff below is all used internally and shouldn't
117 be called from other programs
118 -----------------------------------------------------------------*/
120 /* smatch_flow.c */
122 void smatch (int argc, char **argv);
123 void __split_expr(struct expression *expr);
125 /* smatch_conditions */
126 void __split_whole_condition(struct expression *expr);
128 /* smatch_implied.c */
129 void __implied_states_hook(struct expression *expr);
131 /* smatch_extras.c */
132 #define SMATCH_EXTRA 1 /* this is my_id from smatch extra set in smatch.c */
133 int known_condition_true(struct expression *expr);
135 /* smatch_states.c */
136 extern int debug_states;
138 struct state_list *__get_cur_slist();
139 void nullify_path();
140 void __unnullify_path();
141 int __path_is_null();
142 void clear_all_states();
144 struct sm_state *__get_sm_state(const char *name, int owner,
145 struct symbol *sym);
146 void __use_false_only_stack();
147 void __pop_false_only_stack();
148 void __push_true_states();
149 void __use_false_states();
150 void __pop_false_states();
151 void __merge_false_states();
152 void __merge_true_states();
154 void __negate_cond_stacks();
155 void __use_cond_true_states();
156 void __use_cond_false_states();
157 void __push_cond_stacks();
158 void __and_cond_states();
159 void __or_cond_states();
160 void __save_pre_cond_states();
161 void __pop_pre_cond_states();
162 void __use_cond_states();
164 void __push_continues();
165 void __pop_continues();
166 void __process_continues();
167 void __merge_continues();
169 void __push_breaks();
170 void __process_breaks();
171 void __merge_breaks();
172 void __use_breaks();
174 void __save_switch_states();
175 void __pop_switches();
176 void __merge_switches();
177 void __push_default();
178 void __set_default();
179 int __pop_default();
181 void __push_conditions();
182 void __pop_conditions();
184 void __save_gotos(const char *name);
185 void __merge_gotos(const char *name);
187 void __print_cur_slist();
189 /* smatch_hooks.c */
190 void __pass_to_client(void *data, enum hook_type type);
191 void __pass_to_client_no_data(enum hook_type type);
192 void __pass_declarations_to_client(struct symbol_list *sym_list);
193 int __has_merge_function(int client_id);
194 struct smatch_state *__client_merge_function(int owner, const char *name, struct symbol *sym,
195 struct smatch_state *s1, struct smatch_state *s2);
197 #endif /* !SMATCH_H_ */