double_checking: fix compile problem
[smatch.git] / smatch.c
blob3f566dac5c17a2471acde88b8c10e70a1f780678
2 /*
3 * sparse/smatch.c
5 * Copyright (C) 2006 Dan Carpenter.
7 * Licensed under the Open Software License version 1.1
9 */
11 #include <stdio.h>
12 #include <unistd.h>
13 #include <libgen.h>
14 #include "smatch.h"
15 #include "check_list.h"
17 char *option_debug_check = (char *)"";
18 char *option_project_str = (char *)"";
19 enum project_type option_project = PROJ_NONE;
20 char *data_dir;
21 int option_no_data = 0;
22 int option_spammy = 0;
23 int option_info = 0;
24 int option_full_path = 0;
25 int option_param_mapper = 0;
26 int option_call_tree = 0;
27 int option_no_db = 0;
28 int option_debug_related;
29 int option_file_output;
30 int option_time;
31 char *option_datadir_str;
32 FILE *sm_outfd;
34 typedef void (*reg_func) (int id);
35 #define CK(_x) {.name = #_x, .func = &_x},
36 static struct reg_func_info {
37 const char *name;
38 reg_func func;
39 } reg_funcs[] = {
40 #include "check_list.h"
42 #undef CK
43 int num_checks = ARRAY_SIZE(reg_funcs);
45 const char *check_name(unsigned short id)
47 if (id > ARRAY_SIZE(reg_funcs))
48 return "internal";
50 return reg_funcs[id - 1].name;
53 int id_from_name(const char *name)
55 int i;
57 for (i = 0; i < ARRAY_SIZE(reg_funcs); i++) {
58 if (!strcmp(name, reg_funcs[i].name))
59 return i + 1;
61 return 0;
64 static void help(void)
66 printf("Usage: smatch [smatch arguments][sparse arguments] file.c\n");
67 printf("--project=<name> or -p=<name>: project specific tests\n");
68 printf("--spammy: print superfluous crap.\n");
69 printf("--info: print info used to fill smatch_data/.\n");
70 printf("--debug: print lots of debug output.\n");
71 printf("--param-mapper: enable param_mapper output.\n");
72 printf("--no-data: do not use the /smatch_data/ directory.\n");
73 printf("--data=<dir>: overwrite path to default smatch data directory.\n");
74 printf("--full-path: print the full pathname.\n");
75 printf("--debug-implied: print debug output about implications.\n");
76 printf("--no-implied: ignore implications.\n");
77 printf("--assume-loops: assume loops always go through at least once.\n");
78 printf("--known-conditions: don't branch for known conditions.\n");
79 printf("--two-passes: use a two pass system for each function.\n");
80 printf("--file-output: instead of printing stdout, print to \"file.c.smatch_out\".\n");
81 printf("--help: print this helpful message.\n");
82 exit(1);
85 static int match_option(const char *arg, const char *option)
87 char *str;
88 char *tmp;
89 int ret = 0;
91 str = malloc(strlen(option) + 3);
92 snprintf(str, strlen(option) + 3, "--%s", option);
93 tmp = str;
94 while (*tmp) {
95 if (*tmp == '_')
96 *tmp = '-';
97 tmp++;
99 if (!strcmp(arg, str))
100 ret = 1;
101 free(str);
102 return ret;
105 #define OPTION(_x) do { \
106 if (!found && match_option((*argvp)[1], #_x)) { \
107 found = 1; \
108 option_##_x = 1; \
109 (*argvp)[1] = (*argvp)[0]; \
111 } while (0)
113 void parse_args(int *argcp, char ***argvp)
115 while (*argcp >= 2) {
116 int found = 0;
117 if (!strcmp((*argvp)[1], "--help"))
118 help();
120 if (!found && !strncmp((*argvp)[1], "--project=", 10)) {
121 option_project_str = (*argvp)[1] + 10;
122 (*argvp)[1] = (*argvp)[0];
123 found = 1;
125 if (!found && !strncmp((*argvp)[1], "-p=", 3)) {
126 option_project_str = (*argvp)[1] + 3;
127 (*argvp)[1] = (*argvp)[0];
128 found = 1;
130 if (!found && !strncmp((*argvp)[1], "--data=", 7)) {
131 option_datadir_str = (*argvp)[1] + 7;
132 (*argvp)[1] = (*argvp)[0];
133 found = 1;
135 if (!found && !strncmp((*argvp)[1], "--debug=", 8)) {
136 option_debug_check = (*argvp)[1] + 8;
137 (*argvp)[1] = (*argvp)[0];
138 found = 1;
141 OPTION(spammy);
142 OPTION(info);
143 OPTION(debug);
144 OPTION(debug_implied);
145 OPTION(debug_related);
146 OPTION(no_implied);
147 OPTION(assume_loops);
148 OPTION(known_conditions);
149 OPTION(no_data);
150 OPTION(two_passes);
151 OPTION(full_path);
152 OPTION(param_mapper);
153 OPTION(call_tree);
154 OPTION(file_output);
155 OPTION(time);
156 if (!found)
157 break;
158 (*argcp)--;
159 (*argvp)++;
162 if (!strcmp(option_project_str, "kernel"))
163 option_project = PROJ_KERNEL;
164 if (!strcmp(option_project_str, "wine"))
165 option_project = PROJ_WINE;
168 static char *get_data_dir(char *arg0)
170 char *bin_dir;
171 char *orig;
172 char buf[256];
173 char *dir;
175 if (option_no_data)
176 return NULL;
178 if (option_datadir_str) {
179 if (access(option_datadir_str, R_OK))
180 printf("Warning: %s is not accessible -- ignore.\n",
181 option_datadir_str);
182 else
183 return alloc_string(option_datadir_str);
186 orig = alloc_string(arg0);
187 bin_dir = dirname(orig);
188 strncpy(buf, bin_dir, 254);
189 free_string(orig);
191 buf[255] = '\0';
192 strncat(buf, "/smatch_data/", 254 - strlen(buf));
193 dir = alloc_string(buf);
194 if (!access(dir, R_OK))
195 return dir;
196 free_string(dir);
197 snprintf(buf, 254, "%s/smatch_data/", SMATCHDATADIR);
198 dir = alloc_string(buf);
199 if (!access(dir, R_OK))
200 return dir;
202 printf("Warning: %s is not accessible.\n", dir);
203 printf("Use --no-data or --data to suppress this message.\n");
204 return NULL;
207 int main(int argc, char **argv)
209 int i;
210 reg_func func;
212 sm_outfd = stdout;
213 parse_args(&argc, &argv);
215 /* this gets set back to zero when we parse the first function */
216 final_pass = 1;
218 data_dir = get_data_dir(argv[0]);
220 create_function_hook_hash();
221 open_smatch_db();
222 for (i = 0; i < ARRAY_SIZE(reg_funcs); i++) {
223 func = reg_funcs[i].func;
224 /* The script IDs start at 1.
225 0 is used for internal stuff. */
226 func(i + 1);
229 smatch(argc, argv);
230 free_string(data_dir);
231 return 0;