4 * Copyright (C) 2006 Dan Carpenter.
6 * Licensed under the Open Software License version 1.1
15 char *option_project_str
= (char *)"";
16 enum project_type option_project
= PROJ_NONE
;
18 int option_no_data
= 0;
19 int option_spammy
= 0;
20 int option_full_path
= 0;
21 int option_param_mapper
= 0;
23 typedef void (*reg_func
) (int id
);
24 void register_smatch_extra(int id
);
25 void register_smatch_ignore(int id
);
26 void register_implications(int id
);
27 void register_function_hooks(int id
);
28 void register_modification_hooks(int id
);
29 void register_containers(int id
);
30 void check_debug(int id
);
31 void check_assigned_expr(int id
);
32 void check_null_deref(int id
);
33 void check_overflow(int id
);
34 void check_locking(int id
);
35 void check_memory(int id
);
36 void check_frees_argument(int id
);
37 void check_puts_argument(int id
);
38 void check_leaks(int id
);
39 void check_type(int id
);
40 void check_allocation_funcs(int id
);
41 void check_err_ptr(int id
);
42 void check_err_ptr_deref(int id
);
43 void check_balanced(int id
);
44 void check_deref_check(int id
);
45 void check_hold_dev(int id
);
46 void check_redundant_null_check(int id
);
47 void check_signed(int id
);
48 void check_precedence(int id
);
49 void check_format_string(int id
);
50 void check_unused_ret(int id
);
51 void check_dma_on_stack(int id
);
52 void check_param_mapper(int id
);
53 /* <- your test goes here */
55 /* may as well put wine scripts all together */
56 void check_wine(int id
);
57 void check_wine_filehandles(int id
);
58 void check_wine_WtoA(int id
);
60 /* void register_template(int id); */
62 #define CK(_x) {.name = #_x, .func = &_x}
63 static struct reg_func_info
{
67 CK(register_smatch_extra
), /* smatch_extra always has to be first */
68 CK(register_smatch_ignore
),
70 CK(check_assigned_expr
),
76 CK(check_allocation_funcs
),
78 CK(check_frees_argument
),
80 CK(check_deref_check
),
81 CK(check_redundant_null_check
),
84 CK(check_format_string
),
86 CK(check_dma_on_stack
),
87 CK(check_param_mapper
),
89 /* <- your test goes here */
90 /* CK(register_template), */
94 CK(check_puts_argument
),
96 CK(check_err_ptr_deref
),
99 /* wine specific stuff */
101 CK(check_wine_filehandles
),
104 CK(register_modification_hooks
),
105 CK(register_containers
),
106 CK(register_implications
), /* implications always has to be last */
109 const char *check_name(unsigned short id
)
111 if (id
> ARRAY_SIZE(reg_funcs
)) {
114 return reg_funcs
[id
- 1].name
;
117 int id_from_name(const char *name
)
121 for(i
= 0; i
< ARRAY_SIZE(reg_funcs
); i
++){
122 if (!strcmp(name
, reg_funcs
[i
].name
))
128 struct smatch_state
*default_state
[ARRAY_SIZE(reg_funcs
)];
130 static void help(void)
132 printf("Usage: smatch [smatch arguments][sparse arguments] file.c\n");
133 printf("--project=<name> or -p=<name>: project specific tests\n");
134 printf("--spammy: print superfluous crap.\n");
135 printf("--debug: print lots of debug output.\n");
136 printf("--param-mapper: enable param_mapper output.\n");
137 printf("--no-data: do not use the /smatch_data/ directory.\n");
138 printf("--full-path: print the full pathname.\n");
139 printf("--debug-implied: print debug output about implications.\n");
140 printf("--oom <num>: number of mB memory to use before giving up.\n");
141 printf("--no-implied: ignore implications.\n");
142 printf("--assume-loops: assume loops always go through at least once.\n");
143 printf("--known-conditions: don't branch for known conditions.\n");
144 printf("--two-passes: use a two pass system for each function.\n");
145 printf("--help: print this helpfull message.\n");
149 void parse_args(int *argcp
, char ***argvp
)
152 if (!strncmp((*argvp
)[1], "--project=", 10)) {
153 option_project_str
= (*argvp
)[1] + 10;
154 (*argvp
)[1] = (*argvp
)[0];
155 } else if (!strncmp((*argvp
)[1], "-p=", 3)) {
156 option_project_str
= (*argvp
)[1] + 3;
157 (*argvp
)[1] = (*argvp
)[0];
158 } else if (!strcmp((*argvp
)[1], "--debug")) {
160 (*argvp
)[1] = (*argvp
)[0];
161 } else if (!strcmp((*argvp
)[1], "--debug-implied")) {
162 debug_implied_states
= 1;
163 (*argvp
)[1] = (*argvp
)[0];
164 } else if (!strcmp((*argvp
)[1], "--oom")) {
165 option_oom_kb
= atoi((*argvp
)[2]) * 1000;
166 (*argvp
)[2] = (*argvp
)[0];
169 } else if (!strcmp((*argvp
)[1], "--no-implied")) {
170 option_no_implied
= 1;
171 (*argvp
)[1] = (*argvp
)[0];
172 } else if (!strcmp((*argvp
)[1], "--assume-loops")) {
173 option_assume_loops
= 1;
174 (*argvp
)[1] = (*argvp
)[0];
175 } else if (!strcmp((*argvp
)[1], "--known-conditions")) {
176 option_known_conditions
= 1;
177 (*argvp
)[1] = (*argvp
)[0];
178 } else if (!strcmp((*argvp
)[1], "--no-data")) {
180 (*argvp
)[1] = (*argvp
)[0];
181 } else if (!strcmp((*argvp
)[1], "--spammy")) {
183 (*argvp
)[1] = (*argvp
)[0];
184 } else if (!strcmp((*argvp
)[1], "--two-passes")) {
185 option_two_passes
= 1;
186 (*argvp
)[1] = (*argvp
)[0];
187 } else if (!strcmp((*argvp
)[1], "--full-path")) {
188 option_full_path
= 1;
189 (*argvp
)[1] = (*argvp
)[0];
190 } else if (!strcmp((*argvp
)[1], "--param-mapper")) {
191 option_param_mapper
= 1;
192 (*argvp
)[1] = (*argvp
)[0];
193 } else if (!strcmp((*argvp
)[1], "--help")) {
202 if (!strcmp(option_project_str
, "kernel"))
203 option_project
= PROJ_KERNEL
;
204 if (!strcmp(option_project_str
, "wine"))
205 option_project
= PROJ_WINE
;
208 static char *get_data_dir(char *arg0
)
214 if (option_no_data
) {
217 bin_dir
= dirname(alloc_string(arg0
));
218 strncpy(buf
, bin_dir
, 254);
220 strncat(buf
, "/smatch_data/", 254 - strlen(buf
));
221 dir
= alloc_string(buf
);
222 if (!access(dir
, R_OK
))
224 printf("Warning: %s is not accessible.\n", dir
);
225 printf("Use --no-data to suppress this message.\n");
229 int main(int argc
, char **argv
)
234 parse_args(&argc
, &argv
);
236 data_dir
= get_data_dir(argv
[0]);
238 create_function_hash();
239 for(i
= 0; i
< ARRAY_SIZE(reg_funcs
); i
++){
240 func
= reg_funcs
[i
].func
;
241 /* The script IDs start at 1.
242 0 is used for internal stuff. */
245 register_function_hooks(-1);
248 free_string(data_dir
);