Fix some reversed if conditions
[smatch.git] / smatch.c
blob2a0448bb4084a0535c47326a9eb2e528df8898b8
1 /*
2 * sparse/smatch.c
4 * Copyright (C) 2006 Dan Carpenter.
6 * Licensed under the Open Software License version 1.1
8 */
10 #include <stdio.h>
11 #include <unistd.h>
12 #include <libgen.h>
13 #include "smatch.h"
15 char *option_project_str = (char *)"";
16 enum project_type option_project = PROJ_NONE;
17 char *data_dir;
18 int option_no_data = 0;
19 int option_spammy = 0;
21 typedef void (*reg_func) (int id);
22 void register_smatch_extra(int id);
23 void register_smatch_ignore(int id);
24 void register_implications(int id);
25 void register_function_hooks(int id);
26 void register_modification_hooks(int id);
27 void register_containers(int id);
28 void check_debug(int id);
29 void check_assigned_expr(int id);
30 void check_null_deref(int id);
31 void check_overflow(int id);
32 void check_locking(int id);
33 void check_memory(int id);
34 void check_frees_argument(int id);
35 void check_puts_argument(int id);
36 void check_leaks(int id);
37 void check_type(int id);
38 void check_allocation_funcs(int id);
39 void check_err_ptr(int id);
40 void check_err_ptr_deref(int id);
41 void check_balanced(int id);
42 void check_deref_check(int id);
43 void check_hold_dev(int id);
44 void check_redundant_null_check(int id);
45 /* <- your test goes here */
47 /* may as well put wine scripts all together */
48 void check_wine_filehandles(int id);
49 void check_wine_WtoA(int id);
51 /* void register_template(int id); */
53 static const reg_func reg_funcs[] = {
54 &register_smatch_extra, /* smatch_extra always has to be first */
55 &register_smatch_ignore,
56 &check_debug,
57 &check_assigned_expr,
59 &check_null_deref,
60 &check_overflow,
61 &check_memory,
62 &check_type,
63 &check_allocation_funcs,
64 &check_leaks,
65 &check_frees_argument,
66 &check_balanced,
67 &check_deref_check,
68 &check_redundant_null_check,
70 /* <- your test goes here */
71 /* &register_template, */
73 /* kernel specific */
74 &check_locking,
75 &check_puts_argument,
76 &check_err_ptr,
77 &check_err_ptr_deref,
78 &check_hold_dev,
80 /* wine specific stuff */
81 &check_wine_filehandles,
82 &check_wine_WtoA,
85 &register_modification_hooks,
86 &register_containers,
87 &register_implications, /* implications always has to be last */
88 NULL
91 struct smatch_state *default_state[sizeof(reg_funcs)/sizeof(reg_func)];
93 static void help(void)
95 printf("Usage: smatch [smatch arguments][sparse arguments] file.c\n");
96 printf("--project=<name> or -p=<name>: project specific tests\n");
97 printf("--debug: print lots of debug output.\n");
98 printf("--debug-implied: print debug output about implications.\n");
99 printf("--oom <num>: number of mB memory to use before giving up.\n");
100 printf("--no-implied: ignore implications.\n");
101 printf("--assume-loops: assume loops always go through at least once.\n");
102 printf("--known-conditions: don't branch for known conditions.\n");
103 printf("--no-data: do not use the /smatch_data/ directory.\n");
104 printf("--spammy: print superfluous crap.\n");
105 printf("--two-passes: use a two pass system for each function.\n");
106 printf("--help: print this helpfull message.\n");
107 exit(1);
110 void parse_args(int *argcp, char ***argvp)
112 while(*argcp >= 2) {
113 if (!strncmp((*argvp)[1], "--project=", 10)) {
114 option_project_str = (*argvp)[1] + 10;
115 (*argvp)[1] = (*argvp)[0];
116 } else if (!strncmp((*argvp)[1], "-p=", 3)) {
117 option_project_str = (*argvp)[1] + 3;
118 (*argvp)[1] = (*argvp)[0];
119 } else if (!strcmp((*argvp)[1], "--debug")) {
120 debug_states = 1;
121 (*argvp)[1] = (*argvp)[0];
122 } else if (!strcmp((*argvp)[1], "--debug-implied")) {
123 debug_implied_states = 1;
124 (*argvp)[1] = (*argvp)[0];
125 } else if (!strcmp((*argvp)[1], "--oom")) {
126 option_oom_kb = atoi((*argvp)[2]) * 1000;
127 (*argvp)[2] = (*argvp)[0];
128 (*argcp)--;
129 (*argvp)++;
130 } else if (!strcmp((*argvp)[1], "--no-implied")) {
131 option_no_implied = 1;
132 (*argvp)[1] = (*argvp)[0];
133 } else if (!strcmp((*argvp)[1], "--assume-loops")) {
134 option_assume_loops = 1;
135 (*argvp)[1] = (*argvp)[0];
136 } else if (!strcmp((*argvp)[1], "--known-conditions")) {
137 option_known_conditions = 1;
138 (*argvp)[1] = (*argvp)[0];
139 } else if (!strcmp((*argvp)[1], "--no-data")) {
140 option_no_data = 1;
141 (*argvp)[1] = (*argvp)[0];
142 } else if (!strcmp((*argvp)[1], "--spammy")) {
143 option_spammy = 1;
144 (*argvp)[1] = (*argvp)[0];
145 } else if (!strcmp((*argvp)[1], "--two-passes")) {
146 option_two_passes = 1;
147 (*argvp)[1] = (*argvp)[0];
148 } else if (!strcmp((*argvp)[1], "--help")) {
149 help();
150 } else {
151 break;
153 (*argcp)--;
154 (*argvp)++;
157 if (!strcmp(option_project_str, "kernel"))
158 option_project = PROJ_KERNEL;
159 if (!strcmp(option_project_str, "wine"))
160 option_project = PROJ_WINE;
163 static char *get_data_dir(char *arg0)
165 char *bin_dir;
166 char buf[256];
167 char *dir;
169 if (option_no_data) {
170 return NULL;
172 bin_dir = dirname(alloc_string(arg0));
173 strncpy(buf, bin_dir, 254);
174 buf[255] = '\0';
175 strncat(buf, "/smatch_data/", 254 - strlen(buf));
176 dir = alloc_string(buf);
177 if (!access(dir, R_OK))
178 return dir;
179 printf("Warning: %s is not accessible.\n", dir);
180 printf("Use --no-data to suppress this message.\n");
181 return NULL;
184 int main(int argc, char **argv)
186 int i;
187 reg_func func;
189 parse_args(&argc, &argv);
191 data_dir = get_data_dir(argv[0]);
193 /* The script IDs start at 1.
194 0 is used for internal stuff. */
195 create_function_hash();
196 for(i = 0; (func = reg_funcs[i]); i++){
197 func(i + 1);
199 register_function_hooks(-1);
201 smatch(argc, argv);
202 free_string(data_dir);
203 return 0;