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_enable
= 0;
36 int option_debug_related
;
37 int option_file_output
;
39 char *option_datadir_str
;
42 typedef void (*reg_func
) (int id
);
43 #define CK(_x) {.name = #_x, .func = &_x, .enabled = 0},
44 static struct reg_func_info
{
50 #include "check_list.h"
53 int num_checks
= ARRAY_SIZE(reg_funcs
) - 1;
55 const char *check_name(unsigned short id
)
57 if (id
>= ARRAY_SIZE(reg_funcs
))
60 return reg_funcs
[id
].name
;
63 int id_from_name(const char *name
)
67 for (i
= 1; i
< ARRAY_SIZE(reg_funcs
); i
++) {
68 if (!strcmp(name
, reg_funcs
[i
].name
))
74 static void show_checks(void)
78 for (i
= 1; i
< ARRAY_SIZE(reg_funcs
); i
++) {
79 if (!strncmp(reg_funcs
[i
].name
, "check_", 6))
80 printf("%3d. %s\n", i
, reg_funcs
[i
].name
);
84 static void enable_check(int i
)
86 if (1 <= i
&& i
< ARRAY_SIZE(reg_funcs
))
87 reg_funcs
[i
].enabled
= 1;
90 static void enable_checks(const char *s
)
92 int n
= 0, lo
= -1, i
;
101 for (i
= lo
; i
<= n
; ++i
)
111 n
= 10*n
+ (*s
- '0');
114 fprintf(stderr
, "invalid character '%c'\n", *s
);
121 static void help(void)
123 printf("Usage: smatch [smatch arguments][sparse arguments] file.c\n");
124 printf("--project=<name> or -p=<name>: project specific tests\n");
125 printf("--spammy: print superfluous crap.\n");
126 printf("--info: print info used to fill smatch_data/.\n");
127 printf("--debug: print lots of debug output.\n");
128 printf("--param-mapper: enable param_mapper output.\n");
129 printf("--no-data: do not use the /smatch_data/ directory.\n");
130 printf("--data=<dir>: overwrite path to default smatch data directory.\n");
131 printf("--full-path: print the full pathname.\n");
132 printf("--debug-implied: print debug output about implications.\n");
133 printf("--no-implied: ignore implications.\n");
134 printf("--assume-loops: assume loops always go through at least once.\n");
135 printf("--known-conditions: don't branch for known conditions.\n");
136 printf("--two-passes: use a two pass system for each function.\n");
137 printf("--file-output: instead of printing stdout, print to \"file.c.smatch_out\".\n");
138 printf("--help: print this helpful message.\n");
142 static int match_option(const char *arg
, const char *option
)
148 str
= malloc(strlen(option
) + 3);
149 snprintf(str
, strlen(option
) + 3, "--%s", option
);
156 if (!strcmp(arg
, str
))
162 #define OPTION(_x) do { \
163 if (!found && match_option((*argvp)[1], #_x)) { \
166 (*argvp)[1] = (*argvp)[0]; \
170 void parse_args(int *argcp
, char ***argvp
)
172 while (*argcp
>= 2) {
174 if (!strcmp((*argvp
)[1], "--help"))
177 if (!strcmp((*argvp
)[1], "--show-checks"))
180 if (!found
&& !strncmp((*argvp
)[1], "--project=", 10)) {
181 option_project_str
= (*argvp
)[1] + 10;
182 (*argvp
)[1] = (*argvp
)[0];
185 if (!found
&& !strncmp((*argvp
)[1], "-p=", 3)) {
186 option_project_str
= (*argvp
)[1] + 3;
187 (*argvp
)[1] = (*argvp
)[0];
190 if (!found
&& !strncmp((*argvp
)[1], "--data=", 7)) {
191 option_datadir_str
= (*argvp
)[1] + 7;
192 (*argvp
)[1] = (*argvp
)[0];
195 if (!found
&& !strncmp((*argvp
)[1], "--debug=", 8)) {
196 option_debug_check
= (*argvp
)[1] + 8;
197 (*argvp
)[1] = (*argvp
)[0];
200 if (!found
&& strncmp((*argvp
)[1], "--trace=", 8) == 0) {
201 trace_variable
= (*argvp
)[1] + 8;
202 (*argvp
)[1] = (*argvp
)[0];
205 if (!found
&& strncmp((*argvp
)[1], "--enable=", 9) == 0) {
206 enable_checks((*argvp
)[1] + 9);
208 (*argvp
)[1] = (*argvp
)[0];
215 OPTION(debug_implied
);
216 OPTION(debug_related
);
218 OPTION(assume_loops
);
219 OPTION(known_conditions
);
223 OPTION(param_mapper
);
234 if (!strcmp(option_project_str
, "kernel"))
235 option_project
= PROJ_KERNEL
;
236 if (!strcmp(option_project_str
, "wine"))
237 option_project
= PROJ_WINE
;
240 static char *get_data_dir(char *arg0
)
250 if (option_datadir_str
) {
251 if (access(option_datadir_str
, R_OK
))
252 printf("Warning: %s is not accessible -- ignore.\n",
255 return alloc_string(option_datadir_str
);
258 orig
= alloc_string(arg0
);
259 bin_dir
= dirname(orig
);
260 strncpy(buf
, bin_dir
, 254);
264 strncat(buf
, "/smatch_data/", 254 - strlen(buf
));
265 dir
= alloc_string(buf
);
266 if (!access(dir
, R_OK
))
269 snprintf(buf
, 254, "%s/smatch_data/", SMATCHDATADIR
);
270 dir
= alloc_string(buf
);
271 if (!access(dir
, R_OK
))
274 printf("Warning: %s is not accessible.\n", dir
);
275 printf("Use --no-data or --data to suppress this message.\n");
279 int main(int argc
, char **argv
)
285 parse_args(&argc
, &argv
);
287 /* this gets set back to zero when we parse the first function */
290 data_dir
= get_data_dir(argv
[0]);
292 allocate_hook_memory();
293 create_function_hook_hash();
295 for (i
= 1; i
< ARRAY_SIZE(reg_funcs
); i
++) {
296 func
= reg_funcs
[i
].func
;
297 /* The script IDs start at 1.
298 0 is used for internal stuff. */
299 if (!option_enable
|| reg_funcs
[i
].enabled
|| !strncmp(reg_funcs
[i
].name
, "register_", 9))
304 free_string(data_dir
);