fix: do not ignore struct/union/enum type attributes
[smatch.git] / smatch.c
blob04dd1c23e4c9edfdfa979a1c7756530f376c853a
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 *data_dir;
28 int option_no_data = 0;
29 int option_spammy = 0;
30 int option_info = 0;
31 int option_full_path = 0;
32 int option_param_mapper = 0;
33 int option_call_tree = 0;
34 int option_no_db = 0;
35 int option_enable = 0;
36 int option_debug_related;
37 int option_file_output;
38 int option_time;
39 char *option_datadir_str;
40 FILE *sm_outfd;
41 FILE *sql_outfd;
42 FILE *caller_info_fd;
44 bool __silence_warnings_for_stmt;
46 typedef void (*reg_func) (int id);
47 #define CK(_x) {.name = #_x, .func = &_x, .enabled = 0},
48 static struct reg_func_info {
49 const char *name;
50 reg_func func;
51 int enabled;
52 } reg_funcs[] = {
53 {NULL, NULL},
54 #include "check_list.h"
56 #undef CK
57 int num_checks = ARRAY_SIZE(reg_funcs) - 1;
59 const char *check_name(unsigned short id)
61 if (id >= ARRAY_SIZE(reg_funcs))
62 return "internal";
64 return reg_funcs[id].name;
67 int id_from_name(const char *name)
69 int i;
71 for (i = 1; i < ARRAY_SIZE(reg_funcs); i++) {
72 if (!strcmp(name, reg_funcs[i].name))
73 return i;
75 return 0;
78 static void show_checks(void)
80 int i;
82 for (i = 1; i < ARRAY_SIZE(reg_funcs); i++) {
83 if (!strncmp(reg_funcs[i].name, "check_", 6))
84 printf("%3d. %s\n", i, reg_funcs[i].name);
86 exit(1);
88 static void enable_check(int i)
90 if (1 <= i && i < ARRAY_SIZE(reg_funcs))
91 reg_funcs[i].enabled = 1;
94 static void enable_checks(const char *s)
96 int n = 0, lo = -1, i;
98 do {
99 switch (*s) {
100 case ',':
101 case '\0':
102 if (lo < 0)
103 enable_check(n);
104 else
105 for (i = lo; i <= n; ++i)
106 enable_check(i);
107 lo = -1;
108 n = 0;
109 break;
110 case '-':
111 lo = n;
112 n = 0;
113 break;
114 case '0' ... '9':
115 n = 10*n + (*s - '0');
116 break;
117 default:
118 fprintf(stderr, "invalid character '%c'\n", *s);
119 exit(1);
121 } while (*s++);
124 static void help(void)
126 printf("Usage: smatch [smatch arguments][sparse arguments] file.c\n");
127 printf("--project=<name> or -p=<name>: project specific tests\n");
128 printf("--spammy: print superfluous crap.\n");
129 printf("--info: print info used to fill smatch_data/.\n");
130 printf("--debug: print lots of debug output.\n");
131 printf("--param-mapper: enable param_mapper output.\n");
132 printf("--no-data: do not use the /smatch_data/ directory.\n");
133 printf("--data=<dir>: overwrite path to default smatch data directory.\n");
134 printf("--full-path: print the full pathname.\n");
135 printf("--debug-implied: print debug output about implications.\n");
136 printf("--assume-loops: assume loops always go through at least once.\n");
137 printf("--two-passes: use a two pass system for each function.\n");
138 printf("--file-output: instead of printing stdout, print to \"file.c.smatch_out\".\n");
139 printf("--help: print this helpful message.\n");
140 exit(1);
143 static int match_option(const char *arg, const char *option)
145 char *str;
146 char *tmp;
147 int ret = 0;
149 str = malloc(strlen(option) + 3);
150 snprintf(str, strlen(option) + 3, "--%s", option);
151 tmp = str;
152 while (*tmp) {
153 if (*tmp == '_')
154 *tmp = '-';
155 tmp++;
157 if (!strcmp(arg, str))
158 ret = 1;
159 free(str);
160 return ret;
163 #define OPTION(_x) do { \
164 if (!found && match_option((*argvp)[1], #_x)) { \
165 found = 1; \
166 option_##_x = 1; \
167 (*argvp)[1] = (*argvp)[0]; \
169 } while (0)
171 void parse_args(int *argcp, char ***argvp)
173 while (*argcp >= 2) {
174 int found = 0;
175 if (!strcmp((*argvp)[1], "--help"))
176 help();
178 if (!strcmp((*argvp)[1], "--show-checks"))
179 show_checks();
181 if (!found && !strncmp((*argvp)[1], "--project=", 10)) {
182 option_project_str = (*argvp)[1] + 10;
183 (*argvp)[1] = (*argvp)[0];
184 found = 1;
186 if (!found && !strncmp((*argvp)[1], "-p=", 3)) {
187 option_project_str = (*argvp)[1] + 3;
188 (*argvp)[1] = (*argvp)[0];
189 found = 1;
191 if (!found && !strncmp((*argvp)[1], "--data=", 7)) {
192 option_datadir_str = (*argvp)[1] + 7;
193 (*argvp)[1] = (*argvp)[0];
194 found = 1;
196 if (!found && !strncmp((*argvp)[1], "--debug=", 8)) {
197 option_debug_check = (*argvp)[1] + 8;
198 (*argvp)[1] = (*argvp)[0];
199 found = 1;
201 if (!found && strncmp((*argvp)[1], "--trace=", 8) == 0) {
202 trace_variable = (*argvp)[1] + 8;
203 (*argvp)[1] = (*argvp)[0];
204 found = 1;
206 if (!found && strncmp((*argvp)[1], "--enable=", 9) == 0) {
207 enable_checks((*argvp)[1] + 9);
208 option_enable = 1;
209 (*argvp)[1] = (*argvp)[0];
210 found = 1;
213 OPTION(spammy);
214 OPTION(info);
215 OPTION(debug);
216 OPTION(debug_implied);
217 OPTION(debug_related);
218 OPTION(assume_loops);
219 OPTION(no_data);
220 OPTION(two_passes);
221 OPTION(full_path);
222 OPTION(param_mapper);
223 OPTION(call_tree);
224 OPTION(file_output);
225 OPTION(time);
226 OPTION(no_db);
227 if (!found)
228 break;
229 (*argcp)--;
230 (*argvp)++;
233 if (strcmp(option_project_str, "smatch_generic") != 0)
234 option_project = PROJ_UNKNOWN;
235 if (strcmp(option_project_str, "kernel") == 0)
236 option_project = PROJ_KERNEL;
237 if (strcmp(option_project_str, "wine") == 0)
238 option_project = PROJ_WINE;
241 static char *read_bin_filename(void)
243 char filename[PATH_MAX] = {};
244 char proc[PATH_MAX];
246 pid_t pid = getpid();
247 sprintf(proc, "/proc/%d/exe", pid);
248 if (readlink(proc, filename, PATH_MAX) < 0)
249 return NULL;
250 return alloc_string(filename);
253 static char *get_data_dir(char *arg0)
255 char *bin_dir;
256 char *orig;
257 char buf[256];
258 char *dir;
260 if (option_no_data)
261 return NULL;
263 if (option_datadir_str) {
264 if (access(option_datadir_str, R_OK))
265 printf("Warning: %s is not accessible -- ignore.\n",
266 option_datadir_str);
267 else
268 return alloc_string(option_datadir_str);
271 strncpy(buf, "smatch_data/", sizeof(buf));
272 dir = alloc_string(buf);
273 if (!access(dir, R_OK))
274 return dir;
276 orig = read_bin_filename();
277 if (!orig)
278 orig = alloc_string(arg0);
279 bin_dir = dirname(orig);
280 strncpy(buf, bin_dir, 254);
281 free_string(orig);
283 buf[255] = '\0';
284 strncat(buf, "/smatch_data/", 254 - strlen(buf));
285 dir = alloc_string(buf);
286 if (!access(dir, R_OK))
287 return dir;
288 free_string(dir);
289 snprintf(buf, 254, "%s/smatch_data/", SMATCHDATADIR);
290 dir = alloc_string(buf);
291 if (!access(dir, R_OK))
292 return dir;
294 printf("Warning: %s is not accessible.\n", dir);
295 printf("Use --no-data or --data to suppress this message.\n");
296 return NULL;
299 int main(int argc, char **argv)
301 int i;
302 reg_func func;
304 sm_outfd = stdout;
305 sql_outfd = stdout;
306 caller_info_fd = stdout;
307 parse_args(&argc, &argv);
309 /* this gets set back to zero when we parse the first function */
310 final_pass = 1;
312 data_dir = get_data_dir(argv[0]);
314 allocate_hook_memory();
315 create_function_hook_hash();
316 open_smatch_db();
317 for (i = 1; i < ARRAY_SIZE(reg_funcs); i++) {
318 func = reg_funcs[i].func;
319 /* The script IDs start at 1.
320 0 is used for internal stuff. */
321 if (!option_enable || reg_funcs[i].enabled || !strncmp(reg_funcs[i].name, "register_", 9))
322 func(i);
325 smatch(argc, argv);
326 free_string(data_dir);
327 return 0;