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 *)"smatch_generic";
26 static char *option_db_file
= (char *)"smatch_db.sqlite";
27 enum project_type option_project
= PROJ_NONE
;
30 int option_no_data
= 0;
31 int option_spammy
= 0;
33 int option_full_path
= 0;
34 int option_param_mapper
= 0;
35 int option_call_tree
= 0;
37 int option_enable
= 0;
38 int option_disable
= 0;
39 int option_debug_related
;
40 int option_file_output
;
43 char *option_datadir_str
;
44 int option_fatal_checks
;
53 bool __silence_warnings_for_stmt
;
55 typedef void (*reg_func
) (int id
);
56 #define CK(_x) {.name = #_x, .func = &_x, .enabled = 0},
57 static struct reg_func_info
{
63 #include "check_list.h"
66 int num_checks
= ARRAY_SIZE(reg_funcs
) - 1;
68 const char *check_name(unsigned short id
)
70 if (id
>= ARRAY_SIZE(reg_funcs
))
73 return reg_funcs
[id
].name
;
76 int id_from_name(const char *name
)
80 for (i
= 1; i
< ARRAY_SIZE(reg_funcs
); i
++) {
81 if (!strcmp(name
, reg_funcs
[i
].name
))
87 static void show_checks(void)
91 for (i
= 1; i
< ARRAY_SIZE(reg_funcs
); i
++) {
92 if (!strncmp(reg_funcs
[i
].name
, "check_", 6))
93 printf("%3d. %s\n", i
, reg_funcs
[i
].name
);
98 static void enable_disable_checks(char *s
, bool enable
)
105 next
= strchr(s
, ',');
112 if (strncmp(s
, "check_", 6) == 0)
113 snprintf(buf
, sizeof(buf
), "%s", s
);
115 snprintf(buf
, sizeof(buf
), "check_%s", s
);
118 for (i
= 1; i
< ARRAY_SIZE(reg_funcs
); i
++) {
119 if (strcmp(reg_funcs
[i
].name
, buf
) == 0) {
120 reg_funcs
[i
].enabled
= (enable
== true) ? 1 : -1;
125 if (i
== ARRAY_SIZE(reg_funcs
))
126 sm_fatal("'%s' not found", s
);
128 } while ((s
= next
));
131 static void help(void)
133 printf("Usage: smatch [smatch arguments][sparse arguments] file.c\n");
134 printf("--project=<name> or -p=<name>: project specific tests\n");
135 printf("--spammy: print superfluous crap.\n");
136 printf("--info: print info used to fill smatch_data/.\n");
137 printf("--debug: print lots of debug output.\n");
138 printf("--param-mapper: enable param_mapper output.\n");
139 printf("--no-data: do not use the /smatch_data/ directory.\n");
140 printf("--data=<dir>: overwrite path to default smatch data directory.\n");
141 printf("--full-path: print the full pathname.\n");
142 printf("--debug-implied: print debug output about implications.\n");
143 printf("--assume-loops: assume loops always go through at least once.\n");
144 printf("--two-passes: use a two pass system for each function.\n");
145 printf("--file-output: instead of printing stdout, print to \"file.c.smatch_out\".\n");
146 printf("--fatal-checks: check output is treated as an error.\n");
147 printf("--help: print this helpful message.\n");
151 static int match_option(const char *arg
, const char *option
)
157 str
= malloc(strlen(option
) + 3);
158 snprintf(str
, strlen(option
) + 3, "--%s", option
);
165 if (!strcmp(arg
, str
))
171 #define OPTION(_x) do { \
172 if (!found && match_option((*argvp)[1], #_x)) { \
175 (*argvp)[1] = (*argvp)[0]; \
179 void parse_args(int *argcp
, char ***argvp
)
181 while (*argcp
>= 2) {
183 if (!strcmp((*argvp
)[1], "--help"))
186 if (!strcmp((*argvp
)[1], "--show-checks"))
189 if (!found
&& !strncmp((*argvp
)[1], "--project=", 10)) {
190 option_project_str
= (*argvp
)[1] + 10;
191 (*argvp
)[1] = (*argvp
)[0];
194 if (!found
&& !strncmp((*argvp
)[1], "-p=", 3)) {
195 option_project_str
= (*argvp
)[1] + 3;
196 (*argvp
)[1] = (*argvp
)[0];
199 if (!found
&& !strncmp((*argvp
)[1], "--db-file=", 10)) {
200 option_db_file
= (*argvp
)[1] + 10;
201 (*argvp
)[1] = (*argvp
)[0];
204 if (!found
&& !strncmp((*argvp
)[1], "--data=", 7)) {
205 option_datadir_str
= (*argvp
)[1] + 7;
206 (*argvp
)[1] = (*argvp
)[0];
209 if (!found
&& !strncmp((*argvp
)[1], "--debug=", 8)) {
210 option_debug_check
= (*argvp
)[1] + 8;
211 (*argvp
)[1] = (*argvp
)[0];
214 if (!found
&& strncmp((*argvp
)[1], "--trace=", 8) == 0) {
215 trace_variable
= (*argvp
)[1] + 8;
216 (*argvp
)[1] = (*argvp
)[0];
219 if (!found
&& strncmp((*argvp
)[1], "--enable=", 9) == 0) {
220 enable_disable_checks((*argvp
)[1] + 9, 1);
222 (*argvp
)[1] = (*argvp
)[0];
225 if (!found
&& strncmp((*argvp
)[1], "--disable=", 10) == 0) {
226 enable_disable_checks((*argvp
)[1] + 10, 0);
229 (*argvp
)[1] = (*argvp
)[0];
233 OPTION(fatal_checks
);
237 OPTION(debug_implied
);
238 OPTION(debug_related
);
239 OPTION(assume_loops
);
243 OPTION(param_mapper
);
255 if (strcmp(option_project_str
, "smatch_generic") != 0)
256 option_project
= PROJ_UNKNOWN
;
257 if (strcmp(option_project_str
, "kernel") == 0)
258 option_project
= PROJ_KERNEL
;
259 if (strcmp(option_project_str
, "wine") == 0)
260 option_project
= PROJ_WINE
;
263 static char *read_bin_filename(void)
265 char filename
[PATH_MAX
] = {};
268 pid_t pid
= getpid();
269 sprintf(proc
, "/proc/%d/exe", pid
);
270 if (readlink(proc
, filename
, PATH_MAX
) < 0)
272 return alloc_string(filename
);
275 static char *get_bin_dir(char *arg0
)
279 orig
= read_bin_filename();
281 orig
= alloc_string(arg0
);
282 return dirname(orig
);
285 static char *get_data_dir(char *arg0
)
293 if (option_datadir_str
) {
294 if (access(option_datadir_str
, R_OK
))
295 sm_warning("%s is not accessible -- ignored.",
298 return alloc_string(option_datadir_str
);
301 strncpy(buf
, "smatch_data/", sizeof(buf
));
302 dir
= alloc_string(buf
);
303 if (!access(dir
, R_OK
))
306 strncpy(buf
, bin_dir
, 254);
309 strncat(buf
, "/smatch_data/", 254 - strlen(buf
));
310 dir
= alloc_string(buf
);
311 if (!access(dir
, R_OK
))
314 snprintf(buf
, 254, "%s/smatch_data/", SMATCHDATADIR
);
315 dir
= alloc_string(buf
);
316 if (!access(dir
, R_OK
))
319 sm_warning("%s is not accessible.", dir
);
320 sm_warning("Use --no-data or --data to suppress this message.");
324 int main(int argc
, char **argv
)
331 caller_info_fd
= stdout
;
333 parse_args(&argc
, &argv
);
338 /* this gets set back to zero when we parse the first function */
341 bin_dir
= get_bin_dir(argv
[0]);
342 data_dir
= get_data_dir(argv
[0]);
344 allocate_hook_memory();
345 create_function_hook_hash();
346 open_smatch_db(option_db_file
);
347 for (i
= 1; i
< ARRAY_SIZE(reg_funcs
); i
++) {
348 func
= reg_funcs
[i
].func
;
349 /* The script IDs start at 1.
350 0 is used for internal stuff. */
351 if (!option_enable
|| reg_funcs
[i
].enabled
== 1 ||
352 (option_disable
&& reg_funcs
[i
].enabled
!= -1) ||
353 strncmp(reg_funcs
[i
].name
, "register_", 9) == 0)
358 free_string(data_dir
);
360 if (sm_nr_errors
> 0)
362 if (sm_nr_checks
> 0 && option_fatal_checks
)