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;
23 int option_full_path
= 0;
24 int option_param_mapper
= 0;
25 int option_print_returns
= 0;
26 int option_call_tree
= 0;
29 typedef void (*reg_func
) (int id
);
30 #define CK(_x) {.name = #_x, .func = &_x},
31 static struct reg_func_info
{
35 #include "check_list.h"
38 int num_checks
= ARRAY_SIZE(reg_funcs
);
40 const char *check_name(unsigned short id
)
42 if (id
> ARRAY_SIZE(reg_funcs
)) {
45 return reg_funcs
[id
- 1].name
;
48 int id_from_name(const char *name
)
52 for (i
= 0; i
< ARRAY_SIZE(reg_funcs
); i
++) {
53 if (!strcmp(name
, reg_funcs
[i
].name
))
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("--info: print info used to fill smatch_data/.\n");
65 printf("--debug: print lots of debug output.\n");
66 printf("--param-mapper: enable param_mapper output.\n");
67 printf("--no-data: do not use the /smatch_data/ directory.\n");
68 printf("--full-path: print the full pathname.\n");
69 printf("--debug-implied: print debug output about implications.\n");
70 printf("--no-implied: ignore implications.\n");
71 printf("--assume-loops: assume loops always go through at least once.\n");
72 printf("--known-conditions: don't branch for known conditions.\n");
73 printf("--two-passes: use a two pass system for each function.\n");
74 printf("--help: print this helpfull message.\n");
78 static int match_option(const char *arg
, const char *option
)
84 str
= malloc(strlen(option
) + 3);
85 snprintf(str
, strlen(option
) + 3, "--%s", option
);
92 if (!strcmp(arg
, str
))
98 #define OPTION(_x) do { \
99 if (!found && match_option((*argvp)[1], #_x)) { \
102 (*argvp)[1] = (*argvp)[0]; \
106 void parse_args(int *argcp
, char ***argvp
)
110 if (!strcmp((*argvp
)[1], "--help")) {
113 if (!found
&& !strncmp((*argvp
)[1], "--project=", 10)) {
114 option_project_str
= (*argvp
)[1] + 10;
115 (*argvp
)[1] = (*argvp
)[0];
118 if (!found
&& !strncmp((*argvp
)[1], "-p=", 3)) {
119 option_project_str
= (*argvp
)[1] + 3;
120 (*argvp
)[1] = (*argvp
)[0];
126 OPTION(debug_implied
);
128 OPTION(assume_loops
);
129 OPTION(known_conditions
);
133 OPTION(param_mapper
);
134 OPTION(print_returns
);
142 if (!strcmp(option_project_str
, "kernel"))
143 option_project
= PROJ_KERNEL
;
144 if (!strcmp(option_project_str
, "wine"))
145 option_project
= PROJ_WINE
;
148 static char *get_data_dir(char *arg0
)
155 if (option_no_data
) {
159 orig
= alloc_string(arg0
);
160 bin_dir
= dirname(orig
);
161 strncpy(buf
, bin_dir
, 254);
165 strncat(buf
, "/smatch_data/", 254 - strlen(buf
));
166 dir
= alloc_string(buf
);
167 if (!access(dir
, R_OK
))
170 snprintf(buf
, 254, "%s/smatch_data/", SMATCHDATADIR
);
171 dir
= alloc_string(buf
);
172 if (!access(dir
, R_OK
))
174 printf("Warning: %s is not accessible.\n", dir
);
175 printf("Use --no-data to suppress this message.\n");
179 int main(int argc
, char **argv
)
184 parse_args(&argc
, &argv
);
186 data_dir
= get_data_dir(argv
[0]);
188 create_function_hook_hash();
190 for (i
= 0; i
< ARRAY_SIZE(reg_funcs
); i
++) {
191 func
= reg_funcs
[i
].func
;
192 /* The script IDs start at 1.
193 0 is used for internal stuff. */
196 register_function_hooks(-1);
199 free_string(data_dir
);