user_data2: use function returns in points_to_user_data()
[smatch.git] / smatch.c
blobae5198c1c6db00293cc01422a14daf548eaba6d9
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 "check_list.h"
24 char *option_debug_check = (char *)"";
25 char *option_project_str = (char *)"smatch_generic";
26 enum project_type option_project = PROJ_NONE;
27 char *bin_dir;
28 char *data_dir;
29 int option_no_data = 0;
30 int option_spammy = 0;
31 int option_info = 0;
32 int option_full_path = 0;
33 int option_param_mapper = 0;
34 int option_call_tree = 0;
35 int option_no_db = 0;
36 int option_enable = 0;
37 int option_debug_related;
38 int option_file_output;
39 int option_time;
40 int option_mem;
41 char *option_datadir_str;
42 FILE *sm_outfd;
43 FILE *sql_outfd;
44 FILE *caller_info_fd;
46 bool __silence_warnings_for_stmt;
48 typedef void (*reg_func) (int id);
49 #define CK(_x) {.name = #_x, .func = &_x, .enabled = 0},
50 static struct reg_func_info {
51 const char *name;
52 reg_func func;
53 int enabled;
54 } reg_funcs[] = {
55 {NULL, NULL},
56 #include "check_list.h"
58 #undef CK
59 int num_checks = ARRAY_SIZE(reg_funcs) - 1;
61 const char *check_name(unsigned short id)
63 if (id >= ARRAY_SIZE(reg_funcs))
64 return "internal";
66 return reg_funcs[id].name;
69 int id_from_name(const char *name)
71 int i;
73 for (i = 1; i < ARRAY_SIZE(reg_funcs); i++) {
74 if (!strcmp(name, reg_funcs[i].name))
75 return i;
77 return 0;
80 static void show_checks(void)
82 int i;
84 for (i = 1; i < ARRAY_SIZE(reg_funcs); i++) {
85 if (!strncmp(reg_funcs[i].name, "check_", 6))
86 printf("%3d. %s\n", i, reg_funcs[i].name);
88 exit(1);
90 static void enable_check(int i)
92 if (1 <= i && i < ARRAY_SIZE(reg_funcs))
93 reg_funcs[i].enabled = 1;
96 static void enable_checks(const char *s)
98 int n = 0, lo = -1, i;
100 do {
101 switch (*s) {
102 case ',':
103 case '\0':
104 if (lo < 0)
105 enable_check(n);
106 else
107 for (i = lo; i <= n; ++i)
108 enable_check(i);
109 lo = -1;
110 n = 0;
111 break;
112 case '-':
113 lo = n;
114 n = 0;
115 break;
116 case '0' ... '9':
117 n = 10*n + (*s - '0');
118 break;
119 default:
120 fprintf(stderr, "invalid character '%c'\n", *s);
121 exit(1);
123 } while (*s++);
126 static void help(void)
128 printf("Usage: smatch [smatch arguments][sparse arguments] file.c\n");
129 printf("--project=<name> or -p=<name>: project specific tests\n");
130 printf("--spammy: print superfluous crap.\n");
131 printf("--info: print info used to fill smatch_data/.\n");
132 printf("--debug: print lots of debug output.\n");
133 printf("--param-mapper: enable param_mapper output.\n");
134 printf("--no-data: do not use the /smatch_data/ directory.\n");
135 printf("--data=<dir>: overwrite path to default smatch data directory.\n");
136 printf("--full-path: print the full pathname.\n");
137 printf("--debug-implied: print debug output about implications.\n");
138 printf("--assume-loops: assume loops always go through at least once.\n");
139 printf("--two-passes: use a two pass system for each function.\n");
140 printf("--file-output: instead of printing stdout, print to \"file.c.smatch_out\".\n");
141 printf("--help: print this helpful message.\n");
142 exit(1);
145 static int match_option(const char *arg, const char *option)
147 char *str;
148 char *tmp;
149 int ret = 0;
151 str = malloc(strlen(option) + 3);
152 snprintf(str, strlen(option) + 3, "--%s", option);
153 tmp = str;
154 while (*tmp) {
155 if (*tmp == '_')
156 *tmp = '-';
157 tmp++;
159 if (!strcmp(arg, str))
160 ret = 1;
161 free(str);
162 return ret;
165 #define OPTION(_x) do { \
166 if (!found && match_option((*argvp)[1], #_x)) { \
167 found = 1; \
168 option_##_x = 1; \
169 (*argvp)[1] = (*argvp)[0]; \
171 } while (0)
173 void parse_args(int *argcp, char ***argvp)
175 while (*argcp >= 2) {
176 int found = 0;
177 if (!strcmp((*argvp)[1], "--help"))
178 help();
180 if (!strcmp((*argvp)[1], "--show-checks"))
181 show_checks();
183 if (!found && !strncmp((*argvp)[1], "--project=", 10)) {
184 option_project_str = (*argvp)[1] + 10;
185 (*argvp)[1] = (*argvp)[0];
186 found = 1;
188 if (!found && !strncmp((*argvp)[1], "-p=", 3)) {
189 option_project_str = (*argvp)[1] + 3;
190 (*argvp)[1] = (*argvp)[0];
191 found = 1;
193 if (!found && !strncmp((*argvp)[1], "--data=", 7)) {
194 option_datadir_str = (*argvp)[1] + 7;
195 (*argvp)[1] = (*argvp)[0];
196 found = 1;
198 if (!found && !strncmp((*argvp)[1], "--debug=", 8)) {
199 option_debug_check = (*argvp)[1] + 8;
200 (*argvp)[1] = (*argvp)[0];
201 found = 1;
203 if (!found && strncmp((*argvp)[1], "--trace=", 8) == 0) {
204 trace_variable = (*argvp)[1] + 8;
205 (*argvp)[1] = (*argvp)[0];
206 found = 1;
208 if (!found && strncmp((*argvp)[1], "--enable=", 9) == 0) {
209 enable_checks((*argvp)[1] + 9);
210 option_enable = 1;
211 (*argvp)[1] = (*argvp)[0];
212 found = 1;
215 OPTION(spammy);
216 OPTION(info);
217 OPTION(debug);
218 OPTION(debug_implied);
219 OPTION(debug_related);
220 OPTION(assume_loops);
221 OPTION(no_data);
222 OPTION(two_passes);
223 OPTION(full_path);
224 OPTION(param_mapper);
225 OPTION(call_tree);
226 OPTION(file_output);
227 OPTION(time);
228 OPTION(mem);
229 OPTION(no_db);
230 if (!found)
231 break;
232 (*argcp)--;
233 (*argvp)++;
236 if (strcmp(option_project_str, "smatch_generic") != 0)
237 option_project = PROJ_UNKNOWN;
238 if (strcmp(option_project_str, "kernel") == 0)
239 option_project = PROJ_KERNEL;
240 if (strcmp(option_project_str, "wine") == 0)
241 option_project = PROJ_WINE;
244 static char *read_bin_filename(void)
246 char filename[PATH_MAX] = {};
247 char proc[PATH_MAX];
249 pid_t pid = getpid();
250 sprintf(proc, "/proc/%d/exe", pid);
251 if (readlink(proc, filename, PATH_MAX) < 0)
252 return NULL;
253 return alloc_string(filename);
256 static char *get_bin_dir(char *arg0)
258 char *orig;
260 orig = read_bin_filename();
261 if (!orig)
262 orig = alloc_string(arg0);
263 return dirname(orig);
266 static char *get_data_dir(char *arg0)
268 char buf[256];
269 char *dir;
271 if (option_no_data)
272 return NULL;
274 if (option_datadir_str) {
275 if (access(option_datadir_str, R_OK))
276 printf("Warning: %s is not accessible -- ignore.\n",
277 option_datadir_str);
278 else
279 return alloc_string(option_datadir_str);
282 strncpy(buf, "smatch_data/", sizeof(buf));
283 dir = alloc_string(buf);
284 if (!access(dir, R_OK))
285 return dir;
287 strncpy(buf, bin_dir, 254);
289 buf[255] = '\0';
290 strncat(buf, "/smatch_data/", 254 - strlen(buf));
291 dir = alloc_string(buf);
292 if (!access(dir, R_OK))
293 return dir;
294 free_string(dir);
295 snprintf(buf, 254, "%s/smatch_data/", SMATCHDATADIR);
296 dir = alloc_string(buf);
297 if (!access(dir, R_OK))
298 return dir;
300 printf("Warning: %s is not accessible.\n", dir);
301 printf("Use --no-data or --data to suppress this message.\n");
302 return NULL;
305 int main(int argc, char **argv)
307 int i;
308 reg_func func;
310 sm_outfd = stdout;
311 sql_outfd = stdout;
312 caller_info_fd = stdout;
313 parse_args(&argc, &argv);
315 /* this gets set back to zero when we parse the first function */
316 final_pass = 1;
318 bin_dir = get_bin_dir(argv[0]);
319 data_dir = get_data_dir(argv[0]);
321 allocate_hook_memory();
322 create_function_hook_hash();
323 open_smatch_db();
324 for (i = 1; i < ARRAY_SIZE(reg_funcs); i++) {
325 func = reg_funcs[i].func;
326 /* The script IDs start at 1.
327 0 is used for internal stuff. */
328 if (!option_enable || reg_funcs[i].enabled || !strncmp(reg_funcs[i].name, "register_", 9))
329 func(i);
332 smatch(argc, argv);
333 free_string(data_dir);
334 return 0;