param_key: fix container of when no struct member is referenced
[smatch.git] / smatch.c
blob4afe06ae99411030947e2d08b22dea0025cf5655
1 /*
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
18 #include <stdio.h>
19 #include <unistd.h>
20 #include <libgen.h>
21 #include "smatch.h"
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;
30 char *bin_dir;
31 char *data_dir;
32 int option_no_data = 0;
33 int option_spammy = 0;
34 int option_pedantic;
35 int option_print_names;
36 int option_info = 0;
37 int option_full_path = 0;
38 int option_call_tree = 0;
39 int option_no_db = 0;
40 int option_enable = 0;
41 int option_disable = 0;
42 int option_file_output;
43 int option_time;
44 int option_time_stmt;
45 int option_mem;
46 char *option_datadir_str;
47 int option_fatal_checks;
48 int option_succeed;
49 int SMATCH_EXTRA;
51 FILE *sm_outfd;
52 FILE *sql_outfd;
53 FILE *caller_info_fd;
55 int sm_nr_errors;
56 int sm_nr_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 {
63 const char *name;
64 reg_func func;
65 int enabled;
66 } reg_funcs[] = {
67 {NULL, NULL},
68 #include "check_list.h"
70 #undef CK
71 int num_checks = ARRAY_SIZE(reg_funcs) - 1;
73 const char *check_name(unsigned short id)
75 if (id >= ARRAY_SIZE(reg_funcs))
76 return "internal";
78 return reg_funcs[id].name;
81 int id_from_name(const char *name)
83 int i;
85 for (i = 1; i < ARRAY_SIZE(reg_funcs); i++) {
86 if (!strcmp(name, reg_funcs[i].name))
87 return i;
89 return 0;
92 static void show_checks(void)
94 int i;
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);
100 exit(0);
103 static void enable_disable_checks(char *s, bool enable)
105 char buf[128];
106 char *next;
107 int i;
109 do {
110 next = strchr(s, ',');
111 if (next) {
112 *next = '\0';
113 next++;
115 if (*s == '\0')
116 return;
117 if (strncmp(s, "check_", 6) == 0)
118 snprintf(buf, sizeof(buf), "%s", s);
119 else
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;
126 break;
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");
154 exit(1);
157 static int match_option(const char *arg, const char *option)
159 char *str;
160 char *tmp;
161 int ret = 0;
163 str = malloc(strlen(option) + 3);
164 snprintf(str, strlen(option) + 3, "--%s", option);
165 tmp = str;
166 while (*tmp) {
167 if (*tmp == '_')
168 *tmp = '-';
169 tmp++;
171 if (!strcmp(arg, str))
172 ret = 1;
173 free(str);
174 return ret;
177 #define OPTION(_x) do { \
178 if (!found && match_option((*argvp)[1], #_x)) { \
179 found = 1; \
180 option_##_x = 1; \
181 (*argvp)[1] = (*argvp)[0]; \
183 } while (0)
185 void parse_args(int *argcp, char ***argvp)
187 while (*argcp >= 2) {
188 int found = 0;
189 if (!strcmp((*argvp)[1], "--help"))
190 help();
192 if (!strcmp((*argvp)[1], "--show-checks"))
193 show_checks();
195 if (!found && !strncmp((*argvp)[1], "--project=", 10)) {
196 option_project_str = (*argvp)[1] + 10;
197 (*argvp)[1] = (*argvp)[0];
198 found = 1;
200 if (!found && !strncmp((*argvp)[1], "-p=", 3)) {
201 option_project_str = (*argvp)[1] + 3;
202 (*argvp)[1] = (*argvp)[0];
203 found = 1;
205 if (!found && !strncmp((*argvp)[1], "--db-file=", 10)) {
206 option_db_file = (*argvp)[1] + 10;
207 (*argvp)[1] = (*argvp)[0];
208 found = 1;
210 if (!found && !strncmp((*argvp)[1], "--data=", 7)) {
211 option_datadir_str = (*argvp)[1] + 7;
212 (*argvp)[1] = (*argvp)[0];
213 found = 1;
215 if (!found && !strncmp((*argvp)[1], "--debug=", 8)) {
216 option_debug_check = (*argvp)[1] + 8;
217 (*argvp)[1] = (*argvp)[0];
218 found = 1;
220 if (!found && strncmp((*argvp)[1], "--trace=", 8) == 0) {
221 trace_variable = (*argvp)[1] + 8;
222 (*argvp)[1] = (*argvp)[0];
223 found = 1;
225 if (!found && strncmp((*argvp)[1], "--enable=", 9) == 0) {
226 enable_disable_checks((*argvp)[1] + 9, 1);
227 option_enable = 1;
228 (*argvp)[1] = (*argvp)[0];
229 found = 1;
231 if (!found && strncmp((*argvp)[1], "--disable=", 10) == 0) {
232 enable_disable_checks((*argvp)[1] + 10, 0);
233 option_enable = 1;
234 option_disable = 1;
235 (*argvp)[1] = (*argvp)[0];
236 found = 1;
239 OPTION(fatal_checks);
240 OPTION(spammy);
241 OPTION(pedantic);
242 OPTION(info);
243 OPTION(debug);
244 OPTION(assume_loops);
245 OPTION(no_data);
246 OPTION(two_passes);
247 OPTION(full_path);
248 OPTION(call_tree);
249 OPTION(file_output);
250 OPTION(time);
251 OPTION(time_stmt);
252 OPTION(mem);
253 OPTION(no_db);
254 OPTION(succeed);
255 OPTION(print_names);
256 if (!found)
257 break;
258 (*argcp)--;
259 (*argvp)++;
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] = {};
278 char proc[PATH_MAX];
280 pid_t pid = getpid();
281 sprintf(proc, "/proc/%d/exe", pid);
282 if (readlink(proc, filename, PATH_MAX) < 0)
283 return NULL;
284 return alloc_string(filename);
287 static char *get_bin_dir(char *arg0)
289 char *orig;
291 orig = read_bin_filename();
292 if (!orig)
293 orig = alloc_string(arg0);
294 return dirname(orig);
297 static char *get_data_dir(char *arg0)
299 char buf[256];
300 char *dir;
302 if (option_no_data)
303 return NULL;
305 if (option_datadir_str) {
306 if (access(option_datadir_str, R_OK))
307 sm_warning("%s is not accessible -- ignored.",
308 option_datadir_str);
309 else
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))
316 return dir;
318 strncpy(buf, bin_dir, 254);
320 buf[255] = '\0';
321 strncat(buf, "/smatch_data/", 254 - strlen(buf));
322 dir = alloc_string(buf);
323 if (!access(dir, R_OK))
324 return dir;
325 free_string(dir);
326 snprintf(buf, 254, "%s/smatch_data/", SMATCHDATADIR);
327 dir = alloc_string(buf);
328 if (!access(dir, R_OK))
329 return dir;
331 sm_warning("%s is not accessible.", dir);
332 sm_warning("Use --no-data or --data to suppress this message.");
333 return NULL;
336 int main(int argc, char **argv)
338 struct string_list *filelist = NULL;
339 int i;
340 reg_func func;
342 sm_outfd = stdout;
343 sql_outfd = stdout;
344 caller_info_fd = stdout;
346 parse_args(&argc, &argv);
348 if (argc < 2)
349 help();
351 /* this gets set back to zero when we parse the first function */
352 final_pass = 1;
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)
374 func(i);
377 smatch(filelist);
378 free_string(data_dir);
380 if (option_succeed)
381 return 0;
382 if (sm_nr_errors > 0)
383 return 1;
384 if (sm_nr_checks > 0 && option_fatal_checks)
385 return 1;
386 return 0;