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 "smatch_slist.h"
23 #include "check_list.h"
25 char *option_debug_check
;
26 char *option_debug_var
;
27 char *option_project_str
= (char *)"smatch_generic";
28 static char *option_db_file
= (char *)"smatch_db.sqlite";
29 enum project_type option_project
= PROJ_NONE
;
32 int option_no_data
= 0;
33 int option_spammy
= 0;
35 int option_print_names
;
37 int option_full_path
= 0;
38 int option_call_tree
= 0;
40 int option_enable
= 0;
41 int option_disable
= 0;
42 int option_file_output
;
46 char *option_datadir_str
;
47 int option_fatal_checks
;
58 bool __silence_warnings_for_stmt
;
60 typedef void (*reg_func
) (int id
);
61 #define CK(_x) {.name = #_x, .func = &_x, .enabled = 0},
62 static struct reg_func_info
{
68 #include "check_list.h"
71 int num_checks
= ARRAY_SIZE(reg_funcs
) - 1;
73 const char *check_name(unsigned short id
)
75 if (id
>= ARRAY_SIZE(reg_funcs
))
78 return reg_funcs
[id
].name
;
81 int id_from_name(const char *name
)
85 for (i
= 1; i
< ARRAY_SIZE(reg_funcs
); i
++) {
86 if (!strcmp(name
, reg_funcs
[i
].name
))
92 static void show_checks(void)
96 for (i
= 1; i
< ARRAY_SIZE(reg_funcs
); i
++) {
97 if (!strncmp(reg_funcs
[i
].name
, "check_", 6))
98 printf("%3d. %s\n", i
, reg_funcs
[i
].name
);
103 static void enable_disable_checks(char *s
, bool enable
)
110 next
= strchr(s
, ',');
117 if (strncmp(s
, "check_", 6) == 0)
118 snprintf(buf
, sizeof(buf
), "%s", s
);
120 snprintf(buf
, sizeof(buf
), "check_%s", s
);
123 for (i
= 1; i
< ARRAY_SIZE(reg_funcs
); i
++) {
124 if (strcmp(reg_funcs
[i
].name
, buf
) == 0) {
125 reg_funcs
[i
].enabled
= (enable
== true) ? 1 : -1;
130 if (i
== ARRAY_SIZE(reg_funcs
))
131 sm_fatal("'%s' not found", s
);
133 } while ((s
= next
));
136 static void help(void)
138 printf("Usage: smatch [smatch arguments][sparse arguments] file.c\n");
139 printf("--project=<name> or -p=<name>: project specific tests\n");
140 printf("--succeed: don't exit with an error\n");
141 printf("--spammy: print superfluous crap.\n");
142 printf("--pedantic: intended for reviewing new drivers.\n");
143 printf("--info: print info used to fill smatch_data/.\n");
144 printf("--debug: print lots of debug output.\n");
145 printf("--no-data: do not use the /smatch_data/ directory.\n");
146 printf("--data=<dir>: overwrite path to default smatch data directory.\n");
147 printf("--full-path: print the full pathname.\n");
148 printf("--debug-implied: print debug output about implications.\n");
149 printf("--assume-loops: assume loops always go through at least once.\n");
150 printf("--two-passes: use a two pass system for each function.\n");
151 printf("--file-output: instead of printing stdout, print to \"file.c.smatch_out\".\n");
152 printf("--fatal-checks: check output is treated as an error.\n");
153 printf("--help: print this helpful message.\n");
157 static int match_option(const char *arg
, const char *option
)
163 str
= malloc(strlen(option
) + 3);
164 snprintf(str
, strlen(option
) + 3, "--%s", option
);
171 if (!strcmp(arg
, str
))
177 #define OPTION(_x) do { \
178 if (!found && match_option((*argvp)[1], #_x)) { \
181 (*argvp)[1] = (*argvp)[0]; \
185 void parse_args(int *argcp
, char ***argvp
)
187 while (*argcp
>= 2) {
189 if (!strcmp((*argvp
)[1], "--help"))
192 if (!strcmp((*argvp
)[1], "--show-checks"))
195 if (!found
&& !strncmp((*argvp
)[1], "--project=", 10)) {
196 option_project_str
= (*argvp
)[1] + 10;
197 (*argvp
)[1] = (*argvp
)[0];
200 if (!found
&& !strncmp((*argvp
)[1], "-p=", 3)) {
201 option_project_str
= (*argvp
)[1] + 3;
202 (*argvp
)[1] = (*argvp
)[0];
205 if (!found
&& !strncmp((*argvp
)[1], "--db-file=", 10)) {
206 option_db_file
= (*argvp
)[1] + 10;
207 (*argvp
)[1] = (*argvp
)[0];
210 if (!found
&& !strncmp((*argvp
)[1], "--data=", 7)) {
211 option_datadir_str
= (*argvp
)[1] + 7;
212 (*argvp
)[1] = (*argvp
)[0];
215 if (!found
&& !strncmp((*argvp
)[1], "--debug=", 8)) {
216 option_debug_check
= (*argvp
)[1] + 8;
217 (*argvp
)[1] = (*argvp
)[0];
220 if (!found
&& strncmp((*argvp
)[1], "--trace=", 8) == 0) {
221 trace_variable
= (*argvp
)[1] + 8;
222 (*argvp
)[1] = (*argvp
)[0];
225 if (!found
&& strncmp((*argvp
)[1], "--enable=", 9) == 0) {
226 enable_disable_checks((*argvp
)[1] + 9, 1);
228 (*argvp
)[1] = (*argvp
)[0];
231 if (!found
&& strncmp((*argvp
)[1], "--disable=", 10) == 0) {
232 enable_disable_checks((*argvp
)[1] + 10, 0);
235 (*argvp
)[1] = (*argvp
)[0];
239 OPTION(fatal_checks
);
244 OPTION(assume_loops
);
262 if (strcmp(option_project_str
, "smatch_generic") != 0)
263 option_project
= PROJ_UNKNOWN
;
265 if (strcmp(option_project_str
, "kernel") == 0)
266 option_project
= PROJ_KERNEL
;
267 else if (strcmp(option_project_str
, "wine") == 0)
268 option_project
= PROJ_WINE
;
269 else if (strcmp(option_project_str
, "illumos_kernel") == 0)
270 option_project
= PROJ_ILLUMOS_KERNEL
;
271 else if (strcmp(option_project_str
, "illumos_user") == 0)
272 option_project
= PROJ_ILLUMOS_USER
;
275 static char *read_bin_filename(void)
277 char filename
[PATH_MAX
] = {};
280 pid_t pid
= getpid();
281 sprintf(proc
, "/proc/%d/exe", pid
);
282 if (readlink(proc
, filename
, PATH_MAX
) < 0)
284 return alloc_string(filename
);
287 static char *get_bin_dir(char *arg0
)
291 orig
= read_bin_filename();
293 orig
= alloc_string(arg0
);
294 return dirname(orig
);
297 static char *get_data_dir(char *arg0
)
305 if (option_datadir_str
) {
306 if (access(option_datadir_str
, R_OK
))
307 sm_warning("%s is not accessible -- ignored.",
310 return alloc_string(option_datadir_str
);
313 strncpy(buf
, "smatch_data/", sizeof(buf
));
314 dir
= alloc_string(buf
);
315 if (!access(dir
, R_OK
))
318 strncpy(buf
, bin_dir
, 254);
321 strncat(buf
, "/smatch_data/", 254 - strlen(buf
));
322 dir
= alloc_string(buf
);
323 if (!access(dir
, R_OK
))
326 snprintf(buf
, 254, "%s/smatch_data/", SMATCHDATADIR
);
327 dir
= alloc_string(buf
);
328 if (!access(dir
, R_OK
))
331 sm_warning("%s is not accessible.", dir
);
332 sm_warning("Use --no-data or --data to suppress this message.");
336 int main(int argc
, char **argv
)
338 struct string_list
*filelist
= NULL
;
344 caller_info_fd
= stdout
;
346 parse_args(&argc
, &argv
);
351 /* this gets set back to zero when we parse the first function */
354 bin_dir
= get_bin_dir(argv
[0]);
355 data_dir
= get_data_dir(argv
[0]);
357 allocate_hook_memory();
358 allocate_dynamic_states_array(num_checks
);
359 allocate_tracker_array(num_checks
);
360 create_function_hook_hash();
361 open_smatch_db(option_db_file
);
362 sparse_initialize(argc
, argv
, &filelist
);
363 alloc_valid_ptr_rl();
364 SMATCH_EXTRA
= id_from_name("register_smatch_extra");
365 allocate_modification_hooks();
367 for (i
= 1; i
< ARRAY_SIZE(reg_funcs
); i
++) {
368 func
= reg_funcs
[i
].func
;
369 /* The script IDs start at 1.
370 0 is used for internal stuff. */
371 if (!option_enable
|| reg_funcs
[i
].enabled
== 1 ||
372 (option_disable
&& reg_funcs
[i
].enabled
!= -1) ||
373 strncmp(reg_funcs
[i
].name
, "register_", 9) == 0)
378 free_string(data_dir
);
382 if (sm_nr_errors
> 0)
384 if (sm_nr_checks
> 0 && option_fatal_checks
)