smatch_extra: clean up. state can never be &merged here
[smatch.git] / smatch.c
blob8ece35c0c0d1d55d81637d3fb55cf08136526c91
2 /*
3 * sparse/smatch.c
5 * Copyright (C) 2006 Dan Carpenter.
7 * Licensed under the Open Software License version 1.1
9 */
11 #include <stdio.h>
12 #include <unistd.h>
13 #include <libgen.h>
14 #include "smatch.h"
16 char *option_project_str = (char *)"";
17 enum project_type option_project = PROJ_NONE;
18 char *data_dir;
19 int option_no_data = 0;
20 int option_spammy = 0;
21 int option_info = 0;
22 int option_full_path = 0;
23 int option_param_mapper = 0;
24 int option_call_tree = 0;
26 typedef void (*reg_func) (int id);
27 void register_smatch_extra(int id);
28 void register_smatch_ignore(int id);
29 void register_implications(int id);
30 void register_function_hooks(int id);
31 void register_modification_hooks(int id);
32 void register_containers(int id);
33 void check_debug(int id);
34 void check_assigned_expr(int id);
35 void check_null_deref(int id);
36 void check_overflow(int id);
37 void register_check_overflow_again(int id);
38 void check_locking(int id);
39 void check_memory(int id);
40 void check_frees_argument(int id);
41 void check_puts_argument(int id);
42 void check_leaks(int id);
43 void check_type(int id);
44 void check_allocation_funcs(int id);
45 void check_err_ptr(int id);
46 void check_err_ptr_deref(int id);
47 void check_balanced(int id);
48 void check_deref_check(int id);
49 void check_hold_dev(int id);
50 void check_redundant_null_check(int id);
51 void check_signed(int id);
52 void check_precedence(int id);
53 void check_format_string(int id);
54 void check_unused_ret(int id);
55 void check_dma_on_stack(int id);
56 void check_param_mapper(int id);
57 void check_call_tree(int id);
58 /* <- your test goes here */
60 /* may as well put wine scripts all together */
61 void check_wine(int id);
62 void check_wine_filehandles(int id);
63 void check_wine_WtoA(int id);
65 /* void register_template(int id); */
67 #define CK(_x) {.name = #_x, .func = &_x}
68 static struct reg_func_info {
69 const char *name;
70 reg_func func;
71 } reg_funcs[] = {
72 CK(register_smatch_extra), /* smatch_extra always has to be first */
73 CK(register_smatch_ignore),
74 CK(check_debug),
75 CK(check_assigned_expr),
77 CK(check_null_deref),
78 CK(check_overflow),
79 CK(register_check_overflow_again),
80 CK(check_memory),
81 CK(check_type),
82 CK(check_allocation_funcs),
83 CK(check_leaks),
84 CK(check_frees_argument),
85 CK(check_balanced),
86 CK(check_deref_check),
87 CK(check_redundant_null_check),
88 CK(check_signed),
89 CK(check_precedence),
90 CK(check_format_string),
91 CK(check_unused_ret),
92 CK(check_dma_on_stack),
93 CK(check_param_mapper),
94 CK(check_call_tree),
96 /* <- your test goes here */
97 /* CK(register_template), */
99 /* kernel specific */
100 CK(check_locking),
101 CK(check_puts_argument),
102 CK(check_err_ptr),
103 CK(check_err_ptr_deref),
104 CK(check_hold_dev),
106 /* wine specific stuff */
107 CK(check_wine),
108 CK(check_wine_filehandles),
109 CK(check_wine_WtoA),
111 CK(register_modification_hooks),
112 CK(register_containers),
113 CK(register_implications), /* implications always has to be last */
116 const char *check_name(unsigned short id)
118 if (id > ARRAY_SIZE(reg_funcs)) {
119 return "internal";
121 return reg_funcs[id - 1].name;
124 int id_from_name(const char *name)
126 int i;
128 for(i = 0; i < ARRAY_SIZE(reg_funcs); i++){
129 if (!strcmp(name, reg_funcs[i].name))
130 return i + 1;
132 return 0;
135 struct smatch_state *default_state[ARRAY_SIZE(reg_funcs)];
137 static void help(void)
139 printf("Usage: smatch [smatch arguments][sparse arguments] file.c\n");
140 printf("--project=<name> or -p=<name>: project specific tests\n");
141 printf("--spammy: print superfluous crap.\n");
142 printf("--info: print info used to fill smatch_data/.\n");
143 printf("--debug: print lots of debug output.\n");
144 printf("--param-mapper: enable param_mapper output.\n");
145 printf("--no-data: do not use the /smatch_data/ directory.\n");
146 printf("--full-path: print the full pathname.\n");
147 printf("--debug-implied: print debug output about implications.\n");
148 printf("--oom <num>: number of mB memory to use before giving up.\n");
149 printf("--no-implied: ignore implications.\n");
150 printf("--assume-loops: assume loops always go through at least once.\n");
151 printf("--known-conditions: don't branch for known conditions.\n");
152 printf("--two-passes: use a two pass system for each function.\n");
153 printf("--help: print this helpfull message.\n");
154 exit(1);
157 static int match_option(const char *arg, const char *option)
159 char *str;
160 char *tmp;
161 int ret = 0;
163 str = malloc(strlen(option) + 3);
164 sprintf(str, "--%s", option);
165 tmp = str;
166 while (*tmp) {
167 if (*tmp == '_')
168 *tmp = '-';
169 tmp++;
171 if (!strcmp(arg, str))
172 ret = 1;
173 free(str);
174 return ret;
177 #define OPTION(_x) do { \
178 if (!found && match_option((*argvp)[1], #_x)) { \
179 found = 1; \
180 option_##_x = 1; \
181 (*argvp)[1] = (*argvp)[0]; \
183 } while (0)
185 void parse_args(int *argcp, char ***argvp)
187 while(*argcp >= 2) {
188 int found = 0;
189 if (!strcmp((*argvp)[1], "--help")) {
190 help();
192 if (!found && !strncmp((*argvp)[1], "--project=", 10)) {
193 option_project_str = (*argvp)[1] + 10;
194 (*argvp)[1] = (*argvp)[0];
195 found = 1;
197 if (!found && !strncmp((*argvp)[1], "-p=", 3)) {
198 option_project_str = (*argvp)[1] + 3;
199 (*argvp)[1] = (*argvp)[0];
200 found = 1;
202 if (!found && !strcmp((*argvp)[1], "--oom")) {
203 option_oom_kb = atoi((*argvp)[2]) * 1000;
204 (*argvp)[2] = (*argvp)[0];
205 (*argcp)--;
206 (*argvp)++;
208 OPTION(spammy);
209 OPTION(info);
210 OPTION(debug);
211 OPTION(debug_implied);
212 OPTION(no_implied);
213 OPTION(assume_loops);
214 OPTION(known_conditions);
215 OPTION(no_data);
216 OPTION(two_passes);
217 OPTION(full_path);
218 OPTION(param_mapper);
219 OPTION(call_tree);
220 if (!found)
221 break;
222 (*argcp)--;
223 (*argvp)++;
226 if (!strcmp(option_project_str, "kernel"))
227 option_project = PROJ_KERNEL;
228 if (!strcmp(option_project_str, "wine"))
229 option_project = PROJ_WINE;
232 static char *get_data_dir(char *arg0)
234 char *bin_dir;
235 char buf[256];
236 char *dir;
238 if (option_no_data) {
239 return NULL;
241 bin_dir = dirname(alloc_string(arg0));
242 strncpy(buf, bin_dir, 254);
243 buf[255] = '\0';
244 strncat(buf, "/smatch_data/", 254 - strlen(buf));
245 dir = alloc_string(buf);
246 if (!access(dir, R_OK))
247 return dir;
248 printf("Warning: %s is not accessible.\n", dir);
249 printf("Use --no-data to suppress this message.\n");
250 return NULL;
253 int main(int argc, char **argv)
255 int i;
256 reg_func func;
258 parse_args(&argc, &argv);
260 data_dir = get_data_dir(argv[0]);
262 create_function_hash();
263 for(i = 0; i < ARRAY_SIZE(reg_funcs); i++){
264 func = reg_funcs[i].func;
265 /* The script IDs start at 1.
266 0 is used for internal stuff. */
267 func(i + 1);
269 register_function_hooks(-1);
271 smatch(argc, argv);
272 free_string(data_dir);
273 return 0;