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;
22 typedef void (*reg_func
) (int id
);
23 void register_smatch_extra(int id
);
24 void register_smatch_ignore(int id
);
25 void register_implications(int id
);
26 void register_function_hooks(int id
);
27 void register_modification_hooks(int id
);
28 void register_containers(int id
);
29 void check_debug(int id
);
30 void check_assigned_expr(int id
);
31 void check_null_deref(int id
);
32 void check_overflow(int id
);
33 void check_locking(int id
);
34 void check_memory(int id
);
35 void check_frees_argument(int id
);
36 void check_puts_argument(int id
);
37 void check_leaks(int id
);
38 void check_type(int id
);
39 void check_allocation_funcs(int id
);
40 void check_err_ptr(int id
);
41 void check_err_ptr_deref(int id
);
42 void check_balanced(int id
);
43 void check_deref_check(int id
);
44 void check_hold_dev(int id
);
45 void check_redundant_null_check(int id
);
46 void check_signed(int id
);
47 /* <- your test goes here */
49 /* may as well put wine scripts all together */
50 void check_wine(int id
);
51 void check_wine_filehandles(int id
);
52 void check_wine_WtoA(int id
);
53 void check_wine_locking(int id
);
55 /* void register_template(int id); */
57 #define CK(_x) {.name = #_x, .func = &_x}
58 static struct reg_func_info
{
62 CK(register_smatch_extra
), /* smatch_extra always has to be first */
63 CK(register_smatch_ignore
),
65 CK(check_assigned_expr
),
71 CK(check_allocation_funcs
),
73 CK(check_frees_argument
),
75 CK(check_deref_check
),
76 CK(check_redundant_null_check
),
79 /* <- your test goes here */
80 /* CK(register_template), */
84 CK(check_puts_argument
),
86 CK(check_err_ptr_deref
),
89 /* wine specific stuff */
91 CK(check_wine_filehandles
),
93 CK(check_wine_locking
),
95 CK(register_modification_hooks
),
96 CK(register_containers
),
97 CK(register_implications
), /* implications always has to be last */
100 const char *check_name(unsigned short id
)
102 if (id
> ARRAY_SIZE(reg_funcs
)) {
105 return reg_funcs
[id
- 1].name
;
108 int id_from_name(const char *name
)
112 for(i
= 0; i
< ARRAY_SIZE(reg_funcs
); i
++){
113 if (!strcmp(name
, reg_funcs
[i
].name
))
119 struct smatch_state
*default_state
[ARRAY_SIZE(reg_funcs
)];
121 static void help(void)
123 printf("Usage: smatch [smatch arguments][sparse arguments] file.c\n");
124 printf("--project=<name> or -p=<name>: project specific tests\n");
125 printf("--spammy: print superfluous crap.\n");
126 printf("--debug: print lots of debug output.\n");
127 printf("--no-data: do not use the /smatch_data/ directory.\n");
128 printf("--full-path: print the full pathname.\n");
129 printf("--debug-implied: print debug output about implications.\n");
130 printf("--oom <num>: number of mB memory to use before giving up.\n");
131 printf("--no-implied: ignore implications.\n");
132 printf("--assume-loops: assume loops always go through at least once.\n");
133 printf("--known-conditions: don't branch for known conditions.\n");
134 printf("--two-passes: use a two pass system for each function.\n");
135 printf("--help: print this helpfull message.\n");
139 void parse_args(int *argcp
, char ***argvp
)
142 if (!strncmp((*argvp
)[1], "--project=", 10)) {
143 option_project_str
= (*argvp
)[1] + 10;
144 (*argvp
)[1] = (*argvp
)[0];
145 } else if (!strncmp((*argvp
)[1], "-p=", 3)) {
146 option_project_str
= (*argvp
)[1] + 3;
147 (*argvp
)[1] = (*argvp
)[0];
148 } else if (!strcmp((*argvp
)[1], "--debug")) {
150 (*argvp
)[1] = (*argvp
)[0];
151 } else if (!strcmp((*argvp
)[1], "--debug-implied")) {
152 debug_implied_states
= 1;
153 (*argvp
)[1] = (*argvp
)[0];
154 } else if (!strcmp((*argvp
)[1], "--oom")) {
155 option_oom_kb
= atoi((*argvp
)[2]) * 1000;
156 (*argvp
)[2] = (*argvp
)[0];
159 } else if (!strcmp((*argvp
)[1], "--no-implied")) {
160 option_no_implied
= 1;
161 (*argvp
)[1] = (*argvp
)[0];
162 } else if (!strcmp((*argvp
)[1], "--assume-loops")) {
163 option_assume_loops
= 1;
164 (*argvp
)[1] = (*argvp
)[0];
165 } else if (!strcmp((*argvp
)[1], "--known-conditions")) {
166 option_known_conditions
= 1;
167 (*argvp
)[1] = (*argvp
)[0];
168 } else if (!strcmp((*argvp
)[1], "--no-data")) {
170 (*argvp
)[1] = (*argvp
)[0];
171 } else if (!strcmp((*argvp
)[1], "--spammy")) {
173 (*argvp
)[1] = (*argvp
)[0];
174 } else if (!strcmp((*argvp
)[1], "--two-passes")) {
175 option_two_passes
= 1;
176 (*argvp
)[1] = (*argvp
)[0];
177 } else if (!strcmp((*argvp
)[1], "--full-path")) {
178 option_full_path
= 1;
179 (*argvp
)[1] = (*argvp
)[0];
180 } else if (!strcmp((*argvp
)[1], "--help")) {
189 if (!strcmp(option_project_str
, "kernel"))
190 option_project
= PROJ_KERNEL
;
191 if (!strcmp(option_project_str
, "wine"))
192 option_project
= PROJ_WINE
;
195 static char *get_data_dir(char *arg0
)
201 if (option_no_data
) {
204 bin_dir
= dirname(alloc_string(arg0
));
205 strncpy(buf
, bin_dir
, 254);
207 strncat(buf
, "/smatch_data/", 254 - strlen(buf
));
208 dir
= alloc_string(buf
);
209 if (!access(dir
, R_OK
))
211 printf("Warning: %s is not accessible.\n", dir
);
212 printf("Use --no-data to suppress this message.\n");
216 int main(int argc
, char **argv
)
221 parse_args(&argc
, &argv
);
223 data_dir
= get_data_dir(argv
[0]);
225 create_function_hash();
226 for(i
= 0; i
< ARRAY_SIZE(reg_funcs
); i
++){
227 func
= reg_funcs
[i
].func
;
228 /* The script IDs start at 1.
229 0 is used for internal stuff. */
232 register_function_hooks(-1);
235 free_string(data_dir
);