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 void check_bitwise_or(int id
);
43 /* <- your test goes here */
44 /* void register_template(int id); */
46 static const reg_func reg_funcs
[] = {
47 ®ister_smatch_extra
, /* smatch_extra always has to be first */
48 ®ister_smatch_ignore
,
56 &check_allocation_funcs
,
58 &check_frees_argument
,
63 &check_initializer_deref
,
67 /* <- your test goes here */
68 /* ®ister_template, */
70 ®ister_modification_hooks
,
72 ®ister_implications
, /* implications always has to be last */
76 struct smatch_state
*default_state
[sizeof(reg_funcs
)/sizeof(reg_func
)];
78 static void help(void)
80 printf("Usage: smatch [smatch arguments][sparse arguments] file.c\n");
81 printf("--debug: print lots of debug output.\n");
82 printf("--debug-implied: print debug output about implications.\n");
83 printf("--oom <num>: number of mB memory to use before giving up.\n");
84 printf("--no-implied: ignore implications.\n");
85 printf("--assume-loops: assume loops always go through at least once.\n");
86 printf("--known-conditions: don't branch for known conditions.\n");
87 printf("--no-data: do not use the /smatch_data/ directory.\n");
88 printf("--spammy: print superfluous crap.\n");
89 printf("--two-passes: use a two pass system for each function.\n");
90 printf("--help: print this helpfull message.\n");
94 void parse_args(int *argcp
, char ***argvp
)
97 if (!strcmp((*argvp
)[1], "--debug")) {
99 (*argvp
)[1] = (*argvp
)[0];
100 } else if (!strcmp((*argvp
)[1], "--debug-implied")) {
101 debug_implied_states
= 1;
102 (*argvp
)[1] = (*argvp
)[0];
103 } else if (!strcmp((*argvp
)[1], "--oom")) {
104 option_oom_kb
= atoi((*argvp
)[2]) * 1000;
105 (*argvp
)[2] = (*argvp
)[0];
108 } else if (!strcmp((*argvp
)[1], "--no-implied")) {
109 option_no_implied
= 1;
110 (*argvp
)[1] = (*argvp
)[0];
111 } else if (!strcmp((*argvp
)[1], "--assume-loops")) {
112 option_assume_loops
= 1;
113 (*argvp
)[1] = (*argvp
)[0];
114 } else if (!strcmp((*argvp
)[1], "--known-conditions")) {
115 option_known_conditions
= 1;
116 (*argvp
)[1] = (*argvp
)[0];
117 } else if (!strcmp((*argvp
)[1], "--no-data")) {
119 (*argvp
)[1] = (*argvp
)[0];
120 } else if (!strcmp((*argvp
)[1], "--spammy")) {
122 (*argvp
)[1] = (*argvp
)[0];
123 } else if (!strcmp((*argvp
)[1], "--two-passes")) {
124 option_two_passes
= 1;
125 (*argvp
)[1] = (*argvp
)[0];
126 } else if (!strcmp((*argvp
)[1], "--help")) {
136 static char *get_data_dir(char *arg0
)
142 if (option_no_data
) {
145 bin_dir
= dirname(alloc_string(arg0
));
146 strncpy(buf
, bin_dir
, 254);
148 strncat(buf
, "/smatch_data/", 254 - strlen(buf
));
149 dir
= alloc_string(buf
);
150 if (!access(dir
, R_OK
))
152 printf("Warning: %s is not accessible.\n", dir
);
153 printf("Use --no-data to suppress this message.\n");
157 int main(int argc
, char **argv
)
162 parse_args(&argc
, &argv
);
164 data_dir
= get_data_dir(argv
[0]);
166 /* The script IDs start at 1.
167 0 is used for internal stuff. */
168 create_function_hash();
169 for(i
= 0; (func
= reg_funcs
[i
]); i
++){
172 register_function_hooks(-1);
175 free_string(data_dir
);