db: caller info needs to record the -1 parameters
[smatch.git] / smatch.c
blobdebb9d294881c7c53fc08cad7fdeadea5600bb07
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_file_output;
28 char *option_datadir_str;
29 FILE *sm_outfd;
31 typedef void (*reg_func) (int id);
32 #define CK(_x) {.name = #_x, .func = &_x},
33 static struct reg_func_info {
34 const char *name;
35 reg_func func;
36 } reg_funcs[] = {
37 #include "check_list.h"
39 #undef CK
40 int num_checks = ARRAY_SIZE(reg_funcs);
42 const char *check_name(unsigned short id)
44 if (id > ARRAY_SIZE(reg_funcs)) {
45 return "internal";
47 return reg_funcs[id - 1].name;
50 int id_from_name(const char *name)
52 int i;
54 for (i = 0; i < ARRAY_SIZE(reg_funcs); i++) {
55 if (!strcmp(name, reg_funcs[i].name))
56 return i + 1;
58 return 0;
61 static void help(void)
63 printf("Usage: smatch [smatch arguments][sparse arguments] file.c\n");
64 printf("--project=<name> or -p=<name>: project specific tests\n");
65 printf("--spammy: print superfluous crap.\n");
66 printf("--info: print info used to fill smatch_data/.\n");
67 printf("--debug: print lots of debug output.\n");
68 printf("--param-mapper: enable param_mapper output.\n");
69 printf("--no-data: do not use the /smatch_data/ directory.\n");
70 printf("--data=<dir>: overwrite path to default smatch data directory.\n");
71 printf("--full-path: print the full pathname.\n");
72 printf("--debug-implied: print debug output about implications.\n");
73 printf("--no-implied: ignore implications.\n");
74 printf("--assume-loops: assume loops always go through at least once.\n");
75 printf("--known-conditions: don't branch for known conditions.\n");
76 printf("--two-passes: use a two pass system for each function.\n");
77 printf("--file-output: instead of printing stdout, print to \"file.c.smatch_out\".\n");
78 printf("--help: print this helpfull message.\n");
79 exit(1);
82 static int match_option(const char *arg, const char *option)
84 char *str;
85 char *tmp;
86 int ret = 0;
88 str = malloc(strlen(option) + 3);
89 snprintf(str, strlen(option) + 3, "--%s", option);
90 tmp = str;
91 while (*tmp) {
92 if (*tmp == '_')
93 *tmp = '-';
94 tmp++;
96 if (!strcmp(arg, str))
97 ret = 1;
98 free(str);
99 return ret;
102 #define OPTION(_x) do { \
103 if (!found && match_option((*argvp)[1], #_x)) { \
104 found = 1; \
105 option_##_x = 1; \
106 (*argvp)[1] = (*argvp)[0]; \
108 } while (0)
110 void parse_args(int *argcp, char ***argvp)
112 while(*argcp >= 2) {
113 int found = 0;
114 if (!strcmp((*argvp)[1], "--help")) {
115 help();
117 if (!found && !strncmp((*argvp)[1], "--project=", 10)) {
118 option_project_str = (*argvp)[1] + 10;
119 (*argvp)[1] = (*argvp)[0];
120 found = 1;
122 if (!found && !strncmp((*argvp)[1], "-p=", 3)) {
123 option_project_str = (*argvp)[1] + 3;
124 (*argvp)[1] = (*argvp)[0];
125 found = 1;
127 if (!found && !strncmp((*argvp)[1], "--data=", 7)) {
128 option_datadir_str = (*argvp)[1] + 7;
129 (*argvp)[1] = (*argvp)[0];
130 found = 1;
133 OPTION(spammy);
134 OPTION(info);
135 OPTION(debug);
136 OPTION(debug_implied);
137 OPTION(no_implied);
138 OPTION(assume_loops);
139 OPTION(known_conditions);
140 OPTION(no_data);
141 OPTION(two_passes);
142 OPTION(full_path);
143 OPTION(param_mapper);
144 OPTION(call_tree);
145 OPTION(file_output);
146 if (!found)
147 break;
148 (*argcp)--;
149 (*argvp)++;
152 if (!strcmp(option_project_str, "kernel"))
153 option_project = PROJ_KERNEL;
154 if (!strcmp(option_project_str, "wine"))
155 option_project = PROJ_WINE;
158 static char *get_data_dir(char *arg0)
160 char *bin_dir;
161 char *orig;
162 char buf[256];
163 char *dir;
165 if (option_no_data) {
166 return NULL;
169 if (option_datadir_str) {
170 if (access(option_datadir_str, R_OK))
171 printf("Warning: %s is not accessible -- ignore.\n",
172 option_datadir_str);
173 else
174 return alloc_string(option_datadir_str);
177 orig = alloc_string(arg0);
178 bin_dir = dirname(orig);
179 strncpy(buf, bin_dir, 254);
180 free_string(orig);
182 buf[255] = '\0';
183 strncat(buf, "/smatch_data/", 254 - strlen(buf));
184 dir = alloc_string(buf);
185 if (!access(dir, R_OK))
186 return dir;
187 free_string(dir);
188 snprintf(buf, 254, "%s/smatch_data/", SMATCHDATADIR);
189 dir = alloc_string(buf);
190 if (!access(dir, R_OK))
191 return dir;
193 printf("Warning: %s is not accessible.\n", dir);
194 printf("Use --no-data or --data to suppress this message.\n");
195 return NULL;
198 int main(int argc, char **argv)
200 int i;
201 reg_func func;
203 sm_outfd = stdout;
204 parse_args(&argc, &argv);
206 data_dir = get_data_dir(argv[0]);
208 create_function_hook_hash();
209 open_smatch_db();
210 for (i = 0; i < ARRAY_SIZE(reg_funcs); i++) {
211 func = reg_funcs[i].func;
212 /* The script IDs start at 1.
213 0 is used for internal stuff. */
214 func(i + 1);
216 register_function_hooks(-1);
218 smatch(argc, argv);
219 free_string(data_dir);
220 return 0;