*new* check_macros: find macro precedence bugs
[smatch.git] / smatch.c
blobc986171d978a991e923e096d4d9efd2921bee2f5
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_print_returns = 0;
26 int option_call_tree = 0;
28 typedef void (*reg_func) (int id);
29 #define CK(_x) {.name = #_x, .func = &_x},
30 static struct reg_func_info {
31 const char *name;
32 reg_func func;
33 } reg_funcs[] = {
34 #include "check_list.h"
36 #undef CK
37 int num_checks = ARRAY_SIZE(reg_funcs);
39 const char *check_name(unsigned short id)
41 if (id > ARRAY_SIZE(reg_funcs)) {
42 return "internal";
44 return reg_funcs[id - 1].name;
47 int id_from_name(const char *name)
49 int i;
51 for (i = 0; i < ARRAY_SIZE(reg_funcs); i++) {
52 if (!strcmp(name, reg_funcs[i].name))
53 return i + 1;
55 return 0;
58 static void help(void)
60 printf("Usage: smatch [smatch arguments][sparse arguments] file.c\n");
61 printf("--project=<name> or -p=<name>: project specific tests\n");
62 printf("--spammy: print superfluous crap.\n");
63 printf("--info: print info used to fill smatch_data/.\n");
64 printf("--debug: print lots of debug output.\n");
65 printf("--param-mapper: enable param_mapper output.\n");
66 printf("--no-data: do not use the /smatch_data/ directory.\n");
67 printf("--full-path: print the full pathname.\n");
68 printf("--debug-implied: print debug output about implications.\n");
69 printf("--oom <num>: number of mB memory to use before giving up.\n");
70 printf("--no-implied: ignore implications.\n");
71 printf("--assume-loops: assume loops always go through at least once.\n");
72 printf("--known-conditions: don't branch for known conditions.\n");
73 printf("--two-passes: use a two pass system for each function.\n");
74 printf("--help: print this helpfull message.\n");
75 exit(1);
78 static int match_option(const char *arg, const char *option)
80 char *str;
81 char *tmp;
82 int ret = 0;
84 str = malloc(strlen(option) + 3);
85 snprintf(str, strlen(option) + 3, "--%s", option);
86 tmp = str;
87 while (*tmp) {
88 if (*tmp == '_')
89 *tmp = '-';
90 tmp++;
92 if (!strcmp(arg, str))
93 ret = 1;
94 free(str);
95 return ret;
98 #define OPTION(_x) do { \
99 if (!found && match_option((*argvp)[1], #_x)) { \
100 found = 1; \
101 option_##_x = 1; \
102 (*argvp)[1] = (*argvp)[0]; \
104 } while (0)
106 void parse_args(int *argcp, char ***argvp)
108 while(*argcp >= 2) {
109 int found = 0;
110 if (!strcmp((*argvp)[1], "--help")) {
111 help();
113 if (!found && !strncmp((*argvp)[1], "--project=", 10)) {
114 option_project_str = (*argvp)[1] + 10;
115 (*argvp)[1] = (*argvp)[0];
116 found = 1;
118 if (!found && !strncmp((*argvp)[1], "-p=", 3)) {
119 option_project_str = (*argvp)[1] + 3;
120 (*argvp)[1] = (*argvp)[0];
121 found = 1;
123 if (!found && !strcmp((*argvp)[1], "--oom")) {
124 option_oom_kb = atoi((*argvp)[2]) * 1000;
125 (*argvp)[2] = (*argvp)[0];
126 (*argcp)--;
127 (*argvp)++;
129 OPTION(spammy);
130 OPTION(info);
131 OPTION(debug);
132 OPTION(debug_implied);
133 OPTION(no_implied);
134 OPTION(assume_loops);
135 OPTION(known_conditions);
136 OPTION(no_data);
137 OPTION(two_passes);
138 OPTION(full_path);
139 OPTION(param_mapper);
140 OPTION(print_returns);
141 OPTION(call_tree);
142 if (!found)
143 break;
144 (*argcp)--;
145 (*argvp)++;
148 if (!strcmp(option_project_str, "kernel"))
149 option_project = PROJ_KERNEL;
150 if (!strcmp(option_project_str, "wine"))
151 option_project = PROJ_WINE;
154 static char *get_data_dir(char *arg0)
156 char *bin_dir;
157 char *orig;
158 char buf[256];
159 char *dir;
161 if (option_no_data) {
162 return NULL;
165 orig = alloc_string(arg0);
166 bin_dir = dirname(orig);
167 strncpy(buf, bin_dir, 254);
168 free_string(orig);
170 buf[255] = '\0';
171 strncat(buf, "/smatch_data/", 254 - strlen(buf));
172 dir = alloc_string(buf);
173 if (!access(dir, R_OK))
174 return dir;
175 free_string(dir);
176 snprintf(buf, 254, "%s/smatch_data/", SMATCHDATADIR);
177 dir = alloc_string(buf);
178 if (!access(dir, R_OK))
179 return dir;
180 printf("Warning: %s is not accessible.\n", dir);
181 printf("Use --no-data to suppress this message.\n");
182 return NULL;
185 int main(int argc, char **argv)
187 int i;
188 reg_func func;
190 parse_args(&argc, &argv);
192 data_dir = get_data_dir(argv[0]);
194 create_function_hook_hash();
195 for (i = 0; i < ARRAY_SIZE(reg_funcs); i++) {
196 func = reg_funcs[i].func;
197 /* The script IDs start at 1.
198 0 is used for internal stuff. */
199 func(i + 1);
201 register_function_hooks(-1);
203 smatch(argc, argv);
204 free_string(data_dir);
205 return 0;