2 * Copyright (C) 2006 Dan Carpenter.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, see http://www.gnu.org/copyleft/gpl.txt
22 #include "check_list.h"
24 char *option_debug_check
= (char *)"";
25 char *option_project_str
= (char *)"";
26 enum project_type option_project
= PROJ_NONE
;
28 int option_no_data
= 0;
29 int option_spammy
= 0;
31 int option_full_path
= 0;
32 int option_param_mapper
= 0;
33 int option_call_tree
= 0;
35 int option_debug_related
;
36 int option_file_output
;
38 char *option_datadir_str
;
41 typedef void (*reg_func
) (int id
);
42 #define CK(_x) {.name = #_x, .func = &_x},
43 static struct reg_func_info
{
47 #include "check_list.h"
50 int num_checks
= ARRAY_SIZE(reg_funcs
);
52 const char *check_name(unsigned short id
)
54 if (id
> ARRAY_SIZE(reg_funcs
))
57 return reg_funcs
[id
- 1].name
;
60 int id_from_name(const char *name
)
64 for (i
= 0; i
< ARRAY_SIZE(reg_funcs
); i
++) {
65 if (!strcmp(name
, reg_funcs
[i
].name
))
71 static void help(void)
73 printf("Usage: smatch [smatch arguments][sparse arguments] file.c\n");
74 printf("--project=<name> or -p=<name>: project specific tests\n");
75 printf("--spammy: print superfluous crap.\n");
76 printf("--info: print info used to fill smatch_data/.\n");
77 printf("--debug: print lots of debug output.\n");
78 printf("--param-mapper: enable param_mapper output.\n");
79 printf("--no-data: do not use the /smatch_data/ directory.\n");
80 printf("--data=<dir>: overwrite path to default smatch data directory.\n");
81 printf("--full-path: print the full pathname.\n");
82 printf("--debug-implied: print debug output about implications.\n");
83 printf("--no-implied: ignore implications.\n");
84 printf("--assume-loops: assume loops always go through at least once.\n");
85 printf("--known-conditions: don't branch for known conditions.\n");
86 printf("--two-passes: use a two pass system for each function.\n");
87 printf("--file-output: instead of printing stdout, print to \"file.c.smatch_out\".\n");
88 printf("--help: print this helpful message.\n");
92 static int match_option(const char *arg
, const char *option
)
98 str
= malloc(strlen(option
) + 3);
99 snprintf(str
, strlen(option
) + 3, "--%s", option
);
106 if (!strcmp(arg
, str
))
112 #define OPTION(_x) do { \
113 if (!found && match_option((*argvp)[1], #_x)) { \
116 (*argvp)[1] = (*argvp)[0]; \
120 void parse_args(int *argcp
, char ***argvp
)
122 while (*argcp
>= 2) {
124 if (!strcmp((*argvp
)[1], "--help"))
127 if (!found
&& !strncmp((*argvp
)[1], "--project=", 10)) {
128 option_project_str
= (*argvp
)[1] + 10;
129 (*argvp
)[1] = (*argvp
)[0];
132 if (!found
&& !strncmp((*argvp
)[1], "-p=", 3)) {
133 option_project_str
= (*argvp
)[1] + 3;
134 (*argvp
)[1] = (*argvp
)[0];
137 if (!found
&& !strncmp((*argvp
)[1], "--data=", 7)) {
138 option_datadir_str
= (*argvp
)[1] + 7;
139 (*argvp
)[1] = (*argvp
)[0];
142 if (!found
&& !strncmp((*argvp
)[1], "--debug=", 8)) {
143 option_debug_check
= (*argvp
)[1] + 8;
144 (*argvp
)[1] = (*argvp
)[0];
147 if (!found
&& strncmp((*argvp
)[1], "--trace=", 8) == 0) {
148 trace_variable
= (*argvp
)[1] + 8;
149 (*argvp
)[1] = (*argvp
)[0];
156 OPTION(debug_implied
);
157 OPTION(debug_related
);
159 OPTION(assume_loops
);
160 OPTION(known_conditions
);
164 OPTION(param_mapper
);
175 if (!strcmp(option_project_str
, "kernel"))
176 option_project
= PROJ_KERNEL
;
177 if (!strcmp(option_project_str
, "wine"))
178 option_project
= PROJ_WINE
;
181 static char *get_data_dir(char *arg0
)
191 if (option_datadir_str
) {
192 if (access(option_datadir_str
, R_OK
))
193 printf("Warning: %s is not accessible -- ignore.\n",
196 return alloc_string(option_datadir_str
);
199 orig
= alloc_string(arg0
);
200 bin_dir
= dirname(orig
);
201 strncpy(buf
, bin_dir
, 254);
205 strncat(buf
, "/smatch_data/", 254 - strlen(buf
));
206 dir
= alloc_string(buf
);
207 if (!access(dir
, R_OK
))
210 snprintf(buf
, 254, "%s/smatch_data/", SMATCHDATADIR
);
211 dir
= alloc_string(buf
);
212 if (!access(dir
, R_OK
))
215 printf("Warning: %s is not accessible.\n", dir
);
216 printf("Use --no-data or --data to suppress this message.\n");
220 int main(int argc
, char **argv
)
226 parse_args(&argc
, &argv
);
228 /* this gets set back to zero when we parse the first function */
231 data_dir
= get_data_dir(argv
[0]);
233 create_function_hook_hash();
235 for (i
= 0; i
< ARRAY_SIZE(reg_funcs
); i
++) {
236 func
= reg_funcs
[i
].func
;
237 /* The script IDs start at 1.
238 0 is used for internal stuff. */
243 free_string(data_dir
);