sval: update smatch_math.c to use range_list_sval
[smatch.git] / smatch.c
blob6befae15accdc971e103d3a635a9299b0033a54d
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_project_str = (char *)"";
18 enum project_type option_project = PROJ_NONE;
19 char *data_dir;
20 int option_no_data = 0;
21 int option_spammy = 0;
22 int option_info = 0;
23 int option_full_path = 0;
24 int option_param_mapper = 0;
25 int option_call_tree = 0;
26 int option_no_db = 0;
27 int option_debug_related;
28 int option_file_output;
29 char *option_datadir_str;
30 FILE *sm_outfd;
32 typedef void (*reg_func) (int id);
33 #define CK(_x) {.name = #_x, .func = &_x},
34 static struct reg_func_info {
35 const char *name;
36 reg_func func;
37 } reg_funcs[] = {
38 #include "check_list.h"
40 #undef CK
41 int num_checks = ARRAY_SIZE(reg_funcs);
43 const char *check_name(unsigned short id)
45 if (id > ARRAY_SIZE(reg_funcs))
46 return "internal";
48 return reg_funcs[id - 1].name;
51 int id_from_name(const char *name)
53 int i;
55 for (i = 0; i < ARRAY_SIZE(reg_funcs); i++) {
56 if (!strcmp(name, reg_funcs[i].name))
57 return i + 1;
59 return 0;
62 static void help(void)
64 printf("Usage: smatch [smatch arguments][sparse arguments] file.c\n");
65 printf("--project=<name> or -p=<name>: project specific tests\n");
66 printf("--spammy: print superfluous crap.\n");
67 printf("--info: print info used to fill smatch_data/.\n");
68 printf("--debug: print lots of debug output.\n");
69 printf("--param-mapper: enable param_mapper output.\n");
70 printf("--no-data: do not use the /smatch_data/ directory.\n");
71 printf("--data=<dir>: overwrite path to default smatch data directory.\n");
72 printf("--full-path: print the full pathname.\n");
73 printf("--debug-implied: print debug output about implications.\n");
74 printf("--no-implied: ignore implications.\n");
75 printf("--assume-loops: assume loops always go through at least once.\n");
76 printf("--known-conditions: don't branch for known conditions.\n");
77 printf("--two-passes: use a two pass system for each function.\n");
78 printf("--file-output: instead of printing stdout, print to \"file.c.smatch_out\".\n");
79 printf("--help: print this helpful message.\n");
80 exit(1);
83 static int match_option(const char *arg, const char *option)
85 char *str;
86 char *tmp;
87 int ret = 0;
89 str = malloc(strlen(option) + 3);
90 snprintf(str, strlen(option) + 3, "--%s", option);
91 tmp = str;
92 while (*tmp) {
93 if (*tmp == '_')
94 *tmp = '-';
95 tmp++;
97 if (!strcmp(arg, str))
98 ret = 1;
99 free(str);
100 return ret;
103 #define OPTION(_x) do { \
104 if (!found && match_option((*argvp)[1], #_x)) { \
105 found = 1; \
106 option_##_x = 1; \
107 (*argvp)[1] = (*argvp)[0]; \
109 } while (0)
111 void parse_args(int *argcp, char ***argvp)
113 while (*argcp >= 2) {
114 int found = 0;
115 if (!strcmp((*argvp)[1], "--help"))
116 help();
118 if (!found && !strncmp((*argvp)[1], "--project=", 10)) {
119 option_project_str = (*argvp)[1] + 10;
120 (*argvp)[1] = (*argvp)[0];
121 found = 1;
123 if (!found && !strncmp((*argvp)[1], "-p=", 3)) {
124 option_project_str = (*argvp)[1] + 3;
125 (*argvp)[1] = (*argvp)[0];
126 found = 1;
128 if (!found && !strncmp((*argvp)[1], "--data=", 7)) {
129 option_datadir_str = (*argvp)[1] + 7;
130 (*argvp)[1] = (*argvp)[0];
131 found = 1;
134 OPTION(spammy);
135 OPTION(info);
136 OPTION(debug);
137 OPTION(debug_implied);
138 OPTION(debug_related);
139 OPTION(no_implied);
140 OPTION(assume_loops);
141 OPTION(known_conditions);
142 OPTION(no_data);
143 OPTION(two_passes);
144 OPTION(full_path);
145 OPTION(param_mapper);
146 OPTION(call_tree);
147 OPTION(file_output);
148 if (!found)
149 break;
150 (*argcp)--;
151 (*argvp)++;
154 if (!strcmp(option_project_str, "kernel"))
155 option_project = PROJ_KERNEL;
156 if (!strcmp(option_project_str, "wine"))
157 option_project = PROJ_WINE;
160 static char *get_data_dir(char *arg0)
162 char *bin_dir;
163 char *orig;
164 char buf[256];
165 char *dir;
167 if (option_no_data)
168 return NULL;
170 if (option_datadir_str) {
171 if (access(option_datadir_str, R_OK))
172 printf("Warning: %s is not accessible -- ignore.\n",
173 option_datadir_str);
174 else
175 return alloc_string(option_datadir_str);
178 orig = alloc_string(arg0);
179 bin_dir = dirname(orig);
180 strncpy(buf, bin_dir, 254);
181 free_string(orig);
183 buf[255] = '\0';
184 strncat(buf, "/smatch_data/", 254 - strlen(buf));
185 dir = alloc_string(buf);
186 if (!access(dir, R_OK))
187 return dir;
188 free_string(dir);
189 snprintf(buf, 254, "%s/smatch_data/", SMATCHDATADIR);
190 dir = alloc_string(buf);
191 if (!access(dir, R_OK))
192 return dir;
194 printf("Warning: %s is not accessible.\n", dir);
195 printf("Use --no-data or --data to suppress this message.\n");
196 return NULL;
199 int main(int argc, char **argv)
201 int i;
202 reg_func func;
204 sm_outfd = stdout;
205 parse_args(&argc, &argv);
207 /* this gets set back to zero when we parse the first function */
208 final_pass = 1;
210 data_dir = get_data_dir(argv[0]);
212 create_function_hook_hash();
213 open_smatch_db();
214 for (i = 0; i < ARRAY_SIZE(reg_funcs); i++) {
215 func = reg_funcs[i].func;
216 /* The script IDs start at 1.
217 0 is used for internal stuff. */
218 func(i + 1);
221 smatch(argc, argv);
222 free_string(data_dir);
223 return 0;