wine: pp_internal_error() doesn't return
[smatch.git] / smatch.c
blobb9ffefb6d61d01cf06b81f5967eaeffe382179c7
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_rare = 0;
22 int option_info = 0;
23 int option_full_path = 0;
24 int option_param_mapper = 0;
25 int option_call_tree = 0;
27 typedef void (*reg_func) (int id);
28 void register_smatch_extra(int id);
29 void register_smatch_ignore(int id);
30 void register_implications(int id);
31 void register_function_hooks(int id);
32 void register_modification_hooks(int id);
33 void register_containers(int id);
34 void check_debug(int id);
35 void check_assigned_expr(int id);
36 void check_null_deref(int id);
37 void check_overflow(int id);
38 void register_check_overflow_again(int id);
39 void check_locking(int id);
40 void check_memory(int id);
41 void check_frees_argument(int id);
42 void check_puts_argument(int id);
43 void check_leaks(int id);
44 void check_type(int id);
45 void check_allocation_funcs(int id);
46 void check_err_ptr(int id);
47 void check_err_ptr_deref(int id);
48 void check_balanced(int id);
49 void check_deref_check(int id);
50 void check_hold_dev(int id);
51 void check_redundant_null_check(int id);
52 void check_signed(int id);
53 void check_precedence(int id);
54 void check_format_string(int id);
55 void check_unused_ret(int id);
56 void check_dma_on_stack(int id);
57 void check_param_mapper(int id);
58 void check_call_tree(int id);
59 void check_dev_queue_xmit(int id);
60 /* <- your test goes here */
62 /* may as well put wine scripts all together */
63 void check_wine(int id);
64 void check_wine_filehandles(int id);
65 void check_wine_WtoA(int id);
67 /* void register_template(int id); */
69 #define CK(_x) {.name = #_x, .func = &_x}
70 static struct reg_func_info {
71 const char *name;
72 reg_func func;
73 } reg_funcs[] = {
74 CK(register_smatch_extra), /* smatch_extra always has to be first */
75 CK(register_modification_hooks),
77 CK(register_smatch_ignore),
78 CK(check_debug),
79 CK(check_assigned_expr),
81 CK(check_null_deref),
82 CK(check_overflow),
83 CK(register_check_overflow_again),
84 CK(check_memory),
85 CK(check_type),
86 CK(check_allocation_funcs),
87 CK(check_leaks),
88 CK(check_frees_argument),
89 CK(check_balanced),
90 CK(check_deref_check),
91 CK(check_redundant_null_check),
92 CK(check_signed),
93 CK(check_precedence),
94 CK(check_format_string),
95 CK(check_unused_ret),
96 CK(check_dma_on_stack),
97 CK(check_param_mapper),
98 CK(check_call_tree),
99 CK(check_dev_queue_xmit),
101 /* <- your test goes here */
102 /* CK(register_template), */
104 /* kernel specific */
105 CK(check_locking),
106 CK(check_puts_argument),
107 CK(check_err_ptr),
108 CK(check_err_ptr_deref),
109 CK(check_hold_dev),
111 /* wine specific stuff */
112 CK(check_wine),
113 CK(check_wine_filehandles),
114 CK(check_wine_WtoA),
116 CK(register_containers),
117 CK(register_implications), /* implications always has to be last */
120 const char *check_name(unsigned short id)
122 if (id > ARRAY_SIZE(reg_funcs)) {
123 return "internal";
125 return reg_funcs[id - 1].name;
128 int id_from_name(const char *name)
130 int i;
132 for(i = 0; i < ARRAY_SIZE(reg_funcs); i++){
133 if (!strcmp(name, reg_funcs[i].name))
134 return i + 1;
136 return 0;
139 struct smatch_state *default_state[ARRAY_SIZE(reg_funcs)];
141 static void help(void)
143 printf("Usage: smatch [smatch arguments][sparse arguments] file.c\n");
144 printf("--project=<name> or -p=<name>: project specific tests\n");
145 printf("--spammy: print superfluous crap.\n");
146 printf("--rare: do checks for rare bugs.\n");
147 printf("--info: print info used to fill smatch_data/.\n");
148 printf("--debug: print lots of debug output.\n");
149 printf("--param-mapper: enable param_mapper output.\n");
150 printf("--no-data: do not use the /smatch_data/ directory.\n");
151 printf("--full-path: print the full pathname.\n");
152 printf("--debug-implied: print debug output about implications.\n");
153 printf("--oom <num>: number of mB memory to use before giving up.\n");
154 printf("--no-implied: ignore implications.\n");
155 printf("--assume-loops: assume loops always go through at least once.\n");
156 printf("--known-conditions: don't branch for known conditions.\n");
157 printf("--two-passes: use a two pass system for each function.\n");
158 printf("--help: print this helpfull message.\n");
159 exit(1);
162 static int match_option(const char *arg, const char *option)
164 char *str;
165 char *tmp;
166 int ret = 0;
168 str = malloc(strlen(option) + 3);
169 sprintf(str, "--%s", option);
170 tmp = str;
171 while (*tmp) {
172 if (*tmp == '_')
173 *tmp = '-';
174 tmp++;
176 if (!strcmp(arg, str))
177 ret = 1;
178 free(str);
179 return ret;
182 #define OPTION(_x) do { \
183 if (!found && match_option((*argvp)[1], #_x)) { \
184 found = 1; \
185 option_##_x = 1; \
186 (*argvp)[1] = (*argvp)[0]; \
188 } while (0)
190 void parse_args(int *argcp, char ***argvp)
192 while(*argcp >= 2) {
193 int found = 0;
194 if (!strcmp((*argvp)[1], "--help")) {
195 help();
197 if (!found && !strncmp((*argvp)[1], "--project=", 10)) {
198 option_project_str = (*argvp)[1] + 10;
199 (*argvp)[1] = (*argvp)[0];
200 found = 1;
202 if (!found && !strncmp((*argvp)[1], "-p=", 3)) {
203 option_project_str = (*argvp)[1] + 3;
204 (*argvp)[1] = (*argvp)[0];
205 found = 1;
207 if (!found && !strcmp((*argvp)[1], "--oom")) {
208 option_oom_kb = atoi((*argvp)[2]) * 1000;
209 (*argvp)[2] = (*argvp)[0];
210 (*argcp)--;
211 (*argvp)++;
213 OPTION(spammy);
214 OPTION(rare);
215 OPTION(info);
216 OPTION(debug);
217 OPTION(debug_implied);
218 OPTION(no_implied);
219 OPTION(assume_loops);
220 OPTION(known_conditions);
221 OPTION(no_data);
222 OPTION(two_passes);
223 OPTION(full_path);
224 OPTION(param_mapper);
225 OPTION(call_tree);
226 if (!found)
227 break;
228 (*argcp)--;
229 (*argvp)++;
232 if (!strcmp(option_project_str, "kernel"))
233 option_project = PROJ_KERNEL;
234 if (!strcmp(option_project_str, "wine"))
235 option_project = PROJ_WINE;
238 static char *get_data_dir(char *arg0)
240 char *bin_dir;
241 char buf[256];
242 char *dir;
244 if (option_no_data) {
245 return NULL;
247 bin_dir = dirname(alloc_string(arg0));
248 strncpy(buf, bin_dir, 254);
249 buf[255] = '\0';
250 strncat(buf, "/smatch_data/", 254 - strlen(buf));
251 dir = alloc_string(buf);
252 if (!access(dir, R_OK))
253 return dir;
254 printf("Warning: %s is not accessible.\n", dir);
255 printf("Use --no-data to suppress this message.\n");
256 return NULL;
259 int main(int argc, char **argv)
261 int i;
262 reg_func func;
264 parse_args(&argc, &argv);
266 data_dir = get_data_dir(argv[0]);
268 create_function_hash();
269 for(i = 0; i < ARRAY_SIZE(reg_funcs); i++){
270 func = reg_funcs[i].func;
271 /* The script IDs start at 1.
272 0 is used for internal stuff. */
273 func(i + 1);
275 register_function_hooks(-1);
277 smatch(argc, argv);
278 free_string(data_dir);
279 return 0;