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
;
57 bool __silence_warnings_for_stmt
;
59 typedef void (*reg_func
) (int id
);
60 #define CK(_x) {.name = #_x, .func = &_x, .enabled = 0},
61 static struct reg_func_info
{
67 #include "check_list.h"
70 int num_checks
= ARRAY_SIZE(reg_funcs
) - 1;
72 const char *check_name(unsigned short id
)
74 if (id
>= ARRAY_SIZE(reg_funcs
))
77 return reg_funcs
[id
].name
;
80 int id_from_name(const char *name
)
84 for (i
= 1; i
< ARRAY_SIZE(reg_funcs
); i
++) {
85 if (!strcmp(name
, reg_funcs
[i
].name
))
91 static void show_checks(void)
95 for (i
= 1; i
< ARRAY_SIZE(reg_funcs
); i
++) {
96 if (!strncmp(reg_funcs
[i
].name
, "check_", 6))
97 printf("%3d. %s\n", i
, reg_funcs
[i
].name
);
102 static void enable_disable_checks(char *s
, bool enable
)
109 next
= strchr(s
, ',');
116 if (strncmp(s
, "check_", 6) == 0)
117 snprintf(buf
, sizeof(buf
), "%s", s
);
119 snprintf(buf
, sizeof(buf
), "check_%s", s
);
122 for (i
= 1; i
< ARRAY_SIZE(reg_funcs
); i
++) {
123 if (strcmp(reg_funcs
[i
].name
, buf
) == 0) {
124 reg_funcs
[i
].enabled
= (enable
== true) ? 1 : -1;
129 if (i
== ARRAY_SIZE(reg_funcs
))
130 sm_fatal("'%s' not found", s
);
132 } while ((s
= next
));
135 static void help(void)
137 printf("Usage: smatch [smatch arguments][sparse arguments] file.c\n");
138 printf("--project=<name> or -p=<name>: project specific tests\n");
139 printf("--succeed: don't exit with an error\n");
140 printf("--spammy: print superfluous crap.\n");
141 printf("--pedantic: intended for reviewing new drivers.\n");
142 printf("--info: print info used to fill smatch_data/.\n");
143 printf("--debug: print lots of debug output.\n");
144 printf("--no-data: do not use the /smatch_data/ directory.\n");
145 printf("--data=<dir>: overwrite path to default smatch data directory.\n");
146 printf("--full-path: print the full pathname.\n");
147 printf("--debug-implied: print debug output about implications.\n");
148 printf("--assume-loops: assume loops always go through at least once.\n");
149 printf("--two-passes: use a two pass system for each function.\n");
150 printf("--file-output: instead of printing stdout, print to \"file.c.smatch_out\".\n");
151 printf("--fatal-checks: check output is treated as an error.\n");
152 printf("--help: print this helpful message.\n");
156 static int match_option(const char *arg
, const char *option
)
162 str
= malloc(strlen(option
) + 3);
163 snprintf(str
, strlen(option
) + 3, "--%s", option
);
170 if (!strcmp(arg
, str
))
176 #define OPTION(_x) do { \
177 if (!found && match_option((*argvp)[1], #_x)) { \
180 (*argvp)[1] = (*argvp)[0]; \
184 void parse_args(int *argcp
, char ***argvp
)
186 while (*argcp
>= 2) {
188 if (!strcmp((*argvp
)[1], "--help"))
191 if (!strcmp((*argvp
)[1], "--show-checks"))
194 if (!found
&& !strncmp((*argvp
)[1], "--project=", 10)) {
195 option_project_str
= (*argvp
)[1] + 10;
196 (*argvp
)[1] = (*argvp
)[0];
199 if (!found
&& !strncmp((*argvp
)[1], "-p=", 3)) {
200 option_project_str
= (*argvp
)[1] + 3;
201 (*argvp
)[1] = (*argvp
)[0];
204 if (!found
&& !strncmp((*argvp
)[1], "--db-file=", 10)) {
205 option_db_file
= (*argvp
)[1] + 10;
206 (*argvp
)[1] = (*argvp
)[0];
209 if (!found
&& !strncmp((*argvp
)[1], "--data=", 7)) {
210 option_datadir_str
= (*argvp
)[1] + 7;
211 (*argvp
)[1] = (*argvp
)[0];
214 if (!found
&& !strncmp((*argvp
)[1], "--debug=", 8)) {
215 option_debug_check
= (*argvp
)[1] + 8;
216 (*argvp
)[1] = (*argvp
)[0];
219 if (!found
&& strncmp((*argvp
)[1], "--trace=", 8) == 0) {
220 trace_variable
= (*argvp
)[1] + 8;
221 (*argvp
)[1] = (*argvp
)[0];
224 if (!found
&& strncmp((*argvp
)[1], "--enable=", 9) == 0) {
225 enable_disable_checks((*argvp
)[1] + 9, 1);
227 (*argvp
)[1] = (*argvp
)[0];
230 if (!found
&& strncmp((*argvp
)[1], "--disable=", 10) == 0) {
231 enable_disable_checks((*argvp
)[1] + 10, 0);
234 (*argvp
)[1] = (*argvp
)[0];
238 OPTION(fatal_checks
);
243 OPTION(assume_loops
);
261 if (strcmp(option_project_str
, "smatch_generic") != 0)
262 option_project
= PROJ_UNKNOWN
;
264 if (strcmp(option_project_str
, "kernel") == 0)
265 option_project
= PROJ_KERNEL
;
266 else if (strcmp(option_project_str
, "wine") == 0)
267 option_project
= PROJ_WINE
;
268 else if (strcmp(option_project_str
, "illumos_kernel") == 0)
269 option_project
= PROJ_ILLUMOS_KERNEL
;
270 else if (strcmp(option_project_str
, "illumos_user") == 0)
271 option_project
= PROJ_ILLUMOS_USER
;
274 static char *read_bin_filename(void)
276 char filename
[PATH_MAX
] = {};
279 pid_t pid
= getpid();
280 sprintf(proc
, "/proc/%d/exe", pid
);
281 if (readlink(proc
, filename
, PATH_MAX
) < 0)
283 return alloc_string(filename
);
286 static char *get_bin_dir(char *arg0
)
290 orig
= read_bin_filename();
292 orig
= alloc_string(arg0
);
293 return dirname(orig
);
296 static char *get_data_dir(char *arg0
)
304 if (option_datadir_str
) {
305 if (access(option_datadir_str
, R_OK
))
306 sm_warning("%s is not accessible -- ignored.",
309 return alloc_string(option_datadir_str
);
312 strncpy(buf
, "smatch_data/", sizeof(buf
));
313 dir
= alloc_string(buf
);
314 if (!access(dir
, R_OK
))
317 strncpy(buf
, bin_dir
, 254);
320 strncat(buf
, "/smatch_data/", 254 - strlen(buf
));
321 dir
= alloc_string(buf
);
322 if (!access(dir
, R_OK
))
325 snprintf(buf
, 254, "%s/smatch_data/", SMATCHDATADIR
);
326 dir
= alloc_string(buf
);
327 if (!access(dir
, R_OK
))
330 sm_warning("%s is not accessible.", dir
);
331 sm_warning("Use --no-data or --data to suppress this message.");
335 int main(int argc
, char **argv
)
337 struct string_list
*filelist
= NULL
;
343 caller_info_fd
= stdout
;
345 parse_args(&argc
, &argv
);
350 /* this gets set back to zero when we parse the first function */
353 bin_dir
= get_bin_dir(argv
[0]);
354 data_dir
= get_data_dir(argv
[0]);
356 allocate_hook_memory();
357 allocate_dynamic_states_array(num_checks
);
358 allocate_tracker_array(num_checks
);
359 create_function_hook_hash();
360 open_smatch_db(option_db_file
);
361 sparse_initialize(argc
, argv
, &filelist
);
362 alloc_valid_ptr_rl();
364 for (i
= 1; i
< ARRAY_SIZE(reg_funcs
); i
++) {
365 func
= reg_funcs
[i
].func
;
366 /* The script IDs start at 1.
367 0 is used for internal stuff. */
368 if (!option_enable
|| reg_funcs
[i
].enabled
== 1 ||
369 (option_disable
&& reg_funcs
[i
].enabled
!= -1) ||
370 strncmp(reg_funcs
[i
].name
, "register_", 9) == 0)
375 free_string(data_dir
);
379 if (sm_nr_errors
> 0)
381 if (sm_nr_checks
> 0 && option_fatal_checks
)