5 * Copyright (C) 2006 Dan Carpenter.
7 * Licensed under the Open Software License version 1.1
15 #include "check_list.h"
17 char *option_project_str
= (char *)"";
18 enum project_type option_project
= PROJ_NONE
;
20 int option_no_data
= 0;
21 int option_spammy
= 0;
24 int option_full_path
= 0;
25 int option_param_mapper
= 0;
26 int option_call_tree
= 0;
28 typedef void (*reg_func
) (int id
);
29 #define CK(_x) {.name = #_x, .func = &_x},
30 static struct reg_func_info
{
34 #include "check_list.h"
38 const char *check_name(unsigned short id
)
40 if (id
> ARRAY_SIZE(reg_funcs
)) {
43 return reg_funcs
[id
- 1].name
;
46 int id_from_name(const char *name
)
50 for(i
= 0; i
< ARRAY_SIZE(reg_funcs
); i
++){
51 if (!strcmp(name
, reg_funcs
[i
].name
))
57 struct smatch_state
*default_state
[ARRAY_SIZE(reg_funcs
)];
59 static void help(void)
61 printf("Usage: smatch [smatch arguments][sparse arguments] file.c\n");
62 printf("--project=<name> or -p=<name>: project specific tests\n");
63 printf("--spammy: print superfluous crap.\n");
64 printf("--rare: do checks for rare bugs.\n");
65 printf("--info: print info used to fill smatch_data/.\n");
66 printf("--debug: print lots of debug output.\n");
67 printf("--param-mapper: enable param_mapper output.\n");
68 printf("--no-data: do not use the /smatch_data/ directory.\n");
69 printf("--full-path: print the full pathname.\n");
70 printf("--debug-implied: print debug output about implications.\n");
71 printf("--oom <num>: number of mB memory to use before giving up.\n");
72 printf("--no-implied: ignore implications.\n");
73 printf("--assume-loops: assume loops always go through at least once.\n");
74 printf("--known-conditions: don't branch for known conditions.\n");
75 printf("--two-passes: use a two pass system for each function.\n");
76 printf("--help: print this helpfull message.\n");
80 static int match_option(const char *arg
, const char *option
)
86 str
= malloc(strlen(option
) + 3);
87 sprintf(str
, "--%s", option
);
94 if (!strcmp(arg
, str
))
100 #define OPTION(_x) do { \
101 if (!found && match_option((*argvp)[1], #_x)) { \
104 (*argvp)[1] = (*argvp)[0]; \
108 void parse_args(int *argcp
, char ***argvp
)
112 if (!strcmp((*argvp
)[1], "--help")) {
115 if (!found
&& !strncmp((*argvp
)[1], "--project=", 10)) {
116 option_project_str
= (*argvp
)[1] + 10;
117 (*argvp
)[1] = (*argvp
)[0];
120 if (!found
&& !strncmp((*argvp
)[1], "-p=", 3)) {
121 option_project_str
= (*argvp
)[1] + 3;
122 (*argvp
)[1] = (*argvp
)[0];
125 if (!found
&& !strcmp((*argvp
)[1], "--oom")) {
126 option_oom_kb
= atoi((*argvp
)[2]) * 1000;
127 (*argvp
)[2] = (*argvp
)[0];
135 OPTION(debug_implied
);
137 OPTION(assume_loops
);
138 OPTION(known_conditions
);
142 OPTION(param_mapper
);
150 if (!strcmp(option_project_str
, "kernel"))
151 option_project
= PROJ_KERNEL
;
152 if (!strcmp(option_project_str
, "wine"))
153 option_project
= PROJ_WINE
;
156 static char *get_data_dir(char *arg0
)
162 if (option_no_data
) {
165 bin_dir
= dirname(alloc_string(arg0
));
166 strncpy(buf
, bin_dir
, 254);
168 strncat(buf
, "/smatch_data/", 254 - strlen(buf
));
169 dir
= alloc_string(buf
);
170 if (!access(dir
, R_OK
))
173 snprintf(buf
, 254, "%s/smatch_data/", SMATCHDATADIR
);
174 dir
= alloc_string(buf
);
175 if (!access(dir
, R_OK
))
177 printf("Warning: %s is not accessible.\n", dir
);
178 printf("Use --no-data to suppress this message.\n");
182 int main(int argc
, char **argv
)
187 parse_args(&argc
, &argv
);
189 data_dir
= get_data_dir(argv
[0]);
191 create_function_hook_hash();
192 for(i
= 0; i
< ARRAY_SIZE(reg_funcs
); i
++){
193 func
= reg_funcs
[i
].func
;
194 /* The script IDs start at 1.
195 0 is used for internal stuff. */
198 register_function_hooks(-1);
201 free_string(data_dir
);