5 * Copyright (C) 2006 Dan Carpenter.
7 * Licensed under the Open Software License version 1.1
16 char *option_project_str
= (char *)"";
17 enum project_type option_project
= PROJ_NONE
;
19 int option_no_data
= 0;
20 int option_spammy
= 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
{
72 CK(register_smatch_extra
), /* smatch_extra always has to be first */
73 CK(register_smatch_ignore
),
75 CK(check_assigned_expr
),
79 CK(register_check_overflow_again
),
82 CK(check_allocation_funcs
),
84 CK(check_frees_argument
),
86 CK(check_deref_check
),
87 CK(check_redundant_null_check
),
90 CK(check_format_string
),
92 CK(check_dma_on_stack
),
93 CK(check_param_mapper
),
96 /* <- your test goes here */
97 /* CK(register_template), */
101 CK(check_puts_argument
),
103 CK(check_err_ptr_deref
),
106 /* wine specific stuff */
108 CK(check_wine_filehandles
),
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
)) {
121 return reg_funcs
[id
- 1].name
;
124 int id_from_name(const char *name
)
128 for(i
= 0; i
< ARRAY_SIZE(reg_funcs
); i
++){
129 if (!strcmp(name
, reg_funcs
[i
].name
))
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");
157 static int match_option(const char *arg
, const char *option
)
163 str
= malloc(strlen(option
) + 3);
164 sprintf(str
, "--%s", option
);
171 if (!strcmp(arg
, str
))
177 #define OPTION(_x) do { \
178 if (!found && match_option((*argvp)[1], #_x)) { \
181 (*argvp)[1] = (*argvp)[0]; \
185 void parse_args(int *argcp
, char ***argvp
)
189 if (!strcmp((*argvp
)[1], "--help")) {
192 if (!found
&& !strncmp((*argvp
)[1], "--project=", 10)) {
193 option_project_str
= (*argvp
)[1] + 10;
194 (*argvp
)[1] = (*argvp
)[0];
197 if (!found
&& !strncmp((*argvp
)[1], "-p=", 3)) {
198 option_project_str
= (*argvp
)[1] + 3;
199 (*argvp
)[1] = (*argvp
)[0];
202 if (!found
&& !strcmp((*argvp
)[1], "--oom")) {
203 option_oom_kb
= atoi((*argvp
)[2]) * 1000;
204 (*argvp
)[2] = (*argvp
)[0];
211 OPTION(debug_implied
);
213 OPTION(assume_loops
);
214 OPTION(known_conditions
);
218 OPTION(param_mapper
);
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
)
238 if (option_no_data
) {
241 bin_dir
= dirname(alloc_string(arg0
));
242 strncpy(buf
, bin_dir
, 254);
244 strncat(buf
, "/smatch_data/", 254 - strlen(buf
));
245 dir
= alloc_string(buf
);
246 if (!access(dir
, R_OK
))
248 printf("Warning: %s is not accessible.\n", dir
);
249 printf("Use --no-data to suppress this message.\n");
253 int main(int argc
, char **argv
)
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. */
269 register_function_hooks(-1);
272 free_string(data_dir
);