4 * Copyright (C) 2006 Dan Carpenter.
6 * Licensed under the Open Software License version 1.1
16 int option_no_data
= 0;
17 int option_spammy
= 0;
19 typedef void (*reg_func
) (int id
);
20 void register_smatch_extra(int id
);
21 void register_smatch_ignore(int id
);
22 void register_implications(int id
);
23 void register_function_hooks(int id
);
24 void register_modification_hooks(int id
);
25 void register_containers(int id
);
26 void check_debug(int id
);
27 void check_assigned_expr(int id
);
28 void check_null_deref(int id
);
29 void check_overflow(int id
);
30 void check_locking(int id
);
31 void check_memory(int id
);
32 void check_frees_argument(int id
);
33 void check_puts_argument(int id
);
34 void check_leaks(int id
);
35 void check_type(int id
);
36 void check_allocation_funcs(int id
);
37 void check_err_ptr(int id
);
38 void check_err_ptr_deref(int id
);
39 void check_balanced(int id
);
40 void check_initializer_deref(int id
);
41 void check_deref_check(int id
);
42 /* <- your test goes here */
43 /* void register_template(int id); */
45 static const reg_func reg_funcs
[] = {
46 ®ister_smatch_extra
, /* smatch_extra always has to be first */
47 ®ister_smatch_ignore
,
55 &check_allocation_funcs
,
57 &check_frees_argument
,
62 &check_initializer_deref
,
65 /* <- your test goes here */
66 /* ®ister_template, */
68 ®ister_modification_hooks
,
70 ®ister_implications
, /* implications always has to be last */
74 struct smatch_state
*default_state
[sizeof(reg_funcs
)/sizeof(reg_func
)];
76 static void help(void)
78 printf("Usage: smatch [smatch arguments][sparse arguments] file.c\n");
79 printf("--debug: print lots of debug output.\n");
80 printf("--debug-implied: print debug output about implications.\n");
81 printf("--oom <num>: number of mB memory to use before giving up.\n");
82 printf("--no-implied: ignore implications.\n");
83 printf("--assume-loops: assume loops always go through at least once.\n");
84 printf("--known-conditions: don't branch for known conditions.\n");
85 printf("--no-data: do not use the /smatch_data/ directory.\n");
86 printf("--spammy: print superfluous crap.\n");
87 printf("--help: print this helpfull message.\n");
91 void parse_args(int *argcp
, char ***argvp
)
94 if (!strcmp((*argvp
)[1], "--debug")) {
96 (*argvp
)[1] = (*argvp
)[0];
97 } else if (!strcmp((*argvp
)[1], "--debug-implied")) {
98 debug_implied_states
= 1;
99 (*argvp
)[1] = (*argvp
)[0];
100 } else if (!strcmp((*argvp
)[1], "--oom")) {
101 option_oom_kb
= atoi((*argvp
)[2]) * 1000;
102 (*argvp
)[2] = (*argvp
)[0];
105 } else if (!strcmp((*argvp
)[1], "--no-implied")) {
106 option_no_implied
= 1;
107 (*argvp
)[1] = (*argvp
)[0];
108 } else if (!strcmp((*argvp
)[1], "--assume-loops")) {
109 option_assume_loops
= 1;
110 (*argvp
)[1] = (*argvp
)[0];
111 } else if (!strcmp((*argvp
)[1], "--known-conditions")) {
112 option_known_conditions
= 1;
113 (*argvp
)[1] = (*argvp
)[0];
114 } else if (!strcmp((*argvp
)[1], "--no-data")) {
116 (*argvp
)[1] = (*argvp
)[0];
117 } else if (!strcmp((*argvp
)[1], "--spammy")) {
119 (*argvp
)[1] = (*argvp
)[0];
120 } else if (!strcmp((*argvp
)[1], "--help")) {
130 static char *get_data_dir(char *arg0
)
136 if (option_no_data
) {
139 bin_dir
= dirname(alloc_string(arg0
));
140 strncpy(buf
, bin_dir
, 254);
142 strncat(buf
, "/smatch_data/", 254 - strlen(buf
));
143 dir
= alloc_string(buf
);
144 if (!access(dir
, R_OK
))
146 printf("Warning: %s is not accessible.\n", dir
);
147 printf("Use --no-data to suppress this message.\n");
151 int main(int argc
, char **argv
)
156 parse_args(&argc
, &argv
);
158 data_dir
= get_data_dir(argv
[0]);
160 /* The script IDs start at 1.
161 0 is used for internal stuff. */
162 create_function_hash();
163 for(i
= 0; (func
= reg_funcs
[i
]); i
++){
166 register_function_hooks(-1);
169 free_string(data_dir
);