smatch: Implement --show-checks
[smatch.git] / smatch.c
blob65f9f65cbb5063f0a16d7db15d6474bb30c1c302
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 *)"";
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_debug_related;
36 int option_file_output;
37 int option_time;
38 char *option_datadir_str;
39 FILE *sm_outfd;
41 typedef void (*reg_func) (int id);
42 #define CK(_x) {.name = #_x, .func = &_x},
43 static struct reg_func_info {
44 const char *name;
45 reg_func func;
46 } reg_funcs[] = {
47 {NULL, NULL},
48 #include "check_list.h"
50 #undef CK
51 int num_checks = ARRAY_SIZE(reg_funcs) - 1;
53 const char *check_name(unsigned short id)
55 if (id >= ARRAY_SIZE(reg_funcs))
56 return "internal";
58 return reg_funcs[id].name;
61 int id_from_name(const char *name)
63 int i;
65 for (i = 1; i < ARRAY_SIZE(reg_funcs); i++) {
66 if (!strcmp(name, reg_funcs[i].name))
67 return i;
69 return 0;
72 static void show_checks(void)
74 int i;
76 for (i = 1; i < ARRAY_SIZE(reg_funcs); i++) {
77 if (!strncmp(reg_funcs[i].name, "check_", 6))
78 printf("%3d. %s\n", i, reg_funcs[i].name);
80 exit(1);
83 static void help(void)
85 printf("Usage: smatch [smatch arguments][sparse arguments] file.c\n");
86 printf("--project=<name> or -p=<name>: project specific tests\n");
87 printf("--spammy: print superfluous crap.\n");
88 printf("--info: print info used to fill smatch_data/.\n");
89 printf("--debug: print lots of debug output.\n");
90 printf("--param-mapper: enable param_mapper output.\n");
91 printf("--no-data: do not use the /smatch_data/ directory.\n");
92 printf("--data=<dir>: overwrite path to default smatch data directory.\n");
93 printf("--full-path: print the full pathname.\n");
94 printf("--debug-implied: print debug output about implications.\n");
95 printf("--no-implied: ignore implications.\n");
96 printf("--assume-loops: assume loops always go through at least once.\n");
97 printf("--known-conditions: don't branch for known conditions.\n");
98 printf("--two-passes: use a two pass system for each function.\n");
99 printf("--file-output: instead of printing stdout, print to \"file.c.smatch_out\".\n");
100 printf("--help: print this helpful message.\n");
101 exit(1);
104 static int match_option(const char *arg, const char *option)
106 char *str;
107 char *tmp;
108 int ret = 0;
110 str = malloc(strlen(option) + 3);
111 snprintf(str, strlen(option) + 3, "--%s", option);
112 tmp = str;
113 while (*tmp) {
114 if (*tmp == '_')
115 *tmp = '-';
116 tmp++;
118 if (!strcmp(arg, str))
119 ret = 1;
120 free(str);
121 return ret;
124 #define OPTION(_x) do { \
125 if (!found && match_option((*argvp)[1], #_x)) { \
126 found = 1; \
127 option_##_x = 1; \
128 (*argvp)[1] = (*argvp)[0]; \
130 } while (0)
132 void parse_args(int *argcp, char ***argvp)
134 while (*argcp >= 2) {
135 int found = 0;
136 if (!strcmp((*argvp)[1], "--help"))
137 help();
139 if (!strcmp((*argvp)[1], "--show-checks"))
140 show_checks();
142 if (!found && !strncmp((*argvp)[1], "--project=", 10)) {
143 option_project_str = (*argvp)[1] + 10;
144 (*argvp)[1] = (*argvp)[0];
145 found = 1;
147 if (!found && !strncmp((*argvp)[1], "-p=", 3)) {
148 option_project_str = (*argvp)[1] + 3;
149 (*argvp)[1] = (*argvp)[0];
150 found = 1;
152 if (!found && !strncmp((*argvp)[1], "--data=", 7)) {
153 option_datadir_str = (*argvp)[1] + 7;
154 (*argvp)[1] = (*argvp)[0];
155 found = 1;
157 if (!found && !strncmp((*argvp)[1], "--debug=", 8)) {
158 option_debug_check = (*argvp)[1] + 8;
159 (*argvp)[1] = (*argvp)[0];
160 found = 1;
162 if (!found && strncmp((*argvp)[1], "--trace=", 8) == 0) {
163 trace_variable = (*argvp)[1] + 8;
164 (*argvp)[1] = (*argvp)[0];
165 found = 1;
168 OPTION(spammy);
169 OPTION(info);
170 OPTION(debug);
171 OPTION(debug_implied);
172 OPTION(debug_related);
173 OPTION(no_implied);
174 OPTION(assume_loops);
175 OPTION(known_conditions);
176 OPTION(no_data);
177 OPTION(two_passes);
178 OPTION(full_path);
179 OPTION(param_mapper);
180 OPTION(call_tree);
181 OPTION(file_output);
182 OPTION(time);
183 OPTION(no_db);
184 if (!found)
185 break;
186 (*argcp)--;
187 (*argvp)++;
190 if (!strcmp(option_project_str, "kernel"))
191 option_project = PROJ_KERNEL;
192 if (!strcmp(option_project_str, "wine"))
193 option_project = PROJ_WINE;
196 static char *get_data_dir(char *arg0)
198 char *bin_dir;
199 char *orig;
200 char buf[256];
201 char *dir;
203 if (option_no_data)
204 return NULL;
206 if (option_datadir_str) {
207 if (access(option_datadir_str, R_OK))
208 printf("Warning: %s is not accessible -- ignore.\n",
209 option_datadir_str);
210 else
211 return alloc_string(option_datadir_str);
214 orig = alloc_string(arg0);
215 bin_dir = dirname(orig);
216 strncpy(buf, bin_dir, 254);
217 free_string(orig);
219 buf[255] = '\0';
220 strncat(buf, "/smatch_data/", 254 - strlen(buf));
221 dir = alloc_string(buf);
222 if (!access(dir, R_OK))
223 return dir;
224 free_string(dir);
225 snprintf(buf, 254, "%s/smatch_data/", SMATCHDATADIR);
226 dir = alloc_string(buf);
227 if (!access(dir, R_OK))
228 return dir;
230 printf("Warning: %s is not accessible.\n", dir);
231 printf("Use --no-data or --data to suppress this message.\n");
232 return NULL;
235 int main(int argc, char **argv)
237 int i;
238 reg_func func;
240 sm_outfd = stdout;
241 parse_args(&argc, &argv);
243 /* this gets set back to zero when we parse the first function */
244 final_pass = 1;
246 data_dir = get_data_dir(argv[0]);
248 allocate_hook_memory();
249 create_function_hook_hash();
250 open_smatch_db();
251 for (i = 1; i < ARRAY_SIZE(reg_funcs); i++) {
252 func = reg_funcs[i].func;
253 /* The script IDs start at 1.
254 0 is used for internal stuff. */
255 func(i);
258 smatch(argc, argv);
259 free_string(data_dir);
260 return 0;