Dual pass system [patch #2]
[smatch.git] / smatch.c
bloba4260232cd37250fc1c6e46f939518faaa8985db
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 *data_dir;
16 int option_no_data = 0;
17 int option_spammy = 0;
19 typedef void (*reg_func) (int id);
20 void register_smatch_extra(int id);
21 void register_smatch_ignore(int id);
22 void register_implications(int id);
23 void register_function_hooks(int id);
24 void register_modification_hooks(int id);
25 void register_containers(int id);
26 void check_debug(int id);
27 void check_assigned_expr(int id);
28 void check_null_deref(int id);
29 void check_overflow(int id);
30 void check_locking(int id);
31 void check_memory(int id);
32 void check_frees_argument(int id);
33 void check_puts_argument(int id);
34 void check_leaks(int id);
35 void check_type(int id);
36 void check_allocation_funcs(int id);
37 void check_err_ptr(int id);
38 void check_err_ptr_deref(int id);
39 void check_balanced(int id);
40 void check_initializer_deref(int id);
41 void check_deref_check(int id);
42 /* <- your test goes here */
43 /* void register_template(int id); */
45 static const reg_func reg_funcs[] = {
46 &register_smatch_extra, /* smatch_extra always has to be first */
47 &register_smatch_ignore,
48 &check_debug,
49 &check_assigned_expr,
50 &check_null_deref,
51 &check_overflow,
52 &check_locking,
53 &check_memory,
54 &check_type,
55 &check_allocation_funcs,
56 &check_leaks,
57 &check_frees_argument,
58 &check_puts_argument,
59 &check_err_ptr,
60 &check_err_ptr_deref,
61 &check_balanced,
62 &check_initializer_deref,
63 &check_deref_check,
65 /* <- your test goes here */
66 /* &register_template, */
68 &register_modification_hooks,
69 &register_containers,
70 &register_implications, /* implications always has to be last */
71 NULL
74 struct smatch_state *default_state[sizeof(reg_funcs)/sizeof(reg_func)];
76 static void help(void)
78 printf("Usage: smatch [smatch arguments][sparse arguments] file.c\n");
79 printf("--debug: print lots of debug output.\n");
80 printf("--debug-implied: print debug output about implications.\n");
81 printf("--oom <num>: number of mB memory to use before giving up.\n");
82 printf("--no-implied: ignore implications.\n");
83 printf("--assume-loops: assume loops always go through at least once.\n");
84 printf("--known-conditions: don't branch for known conditions.\n");
85 printf("--no-data: do not use the /smatch_data/ directory.\n");
86 printf("--spammy: print superfluous crap.\n");
87 printf("--help: print this helpfull message.\n");
88 exit(1);
91 void parse_args(int *argcp, char ***argvp)
93 while(*argcp >= 2) {
94 if (!strcmp((*argvp)[1], "--debug")) {
95 debug_states = 1;
96 (*argvp)[1] = (*argvp)[0];
97 } else if (!strcmp((*argvp)[1], "--debug-implied")) {
98 debug_implied_states = 1;
99 (*argvp)[1] = (*argvp)[0];
100 } else if (!strcmp((*argvp)[1], "--oom")) {
101 option_oom_kb = atoi((*argvp)[2]) * 1000;
102 (*argvp)[2] = (*argvp)[0];
103 (*argcp)--;
104 (*argvp)++;
105 } else if (!strcmp((*argvp)[1], "--no-implied")) {
106 option_no_implied = 1;
107 (*argvp)[1] = (*argvp)[0];
108 } else if (!strcmp((*argvp)[1], "--assume-loops")) {
109 option_assume_loops = 1;
110 (*argvp)[1] = (*argvp)[0];
111 } else if (!strcmp((*argvp)[1], "--known-conditions")) {
112 option_known_conditions = 1;
113 (*argvp)[1] = (*argvp)[0];
114 } else if (!strcmp((*argvp)[1], "--no-data")) {
115 option_no_data = 1;
116 (*argvp)[1] = (*argvp)[0];
117 } else if (!strcmp((*argvp)[1], "--spammy")) {
118 option_spammy = 1;
119 (*argvp)[1] = (*argvp)[0];
120 } else if (!strcmp((*argvp)[1], "--help")) {
121 help();
122 } else {
123 break;
125 (*argcp)--;
126 (*argvp)++;
130 static char *get_data_dir(char *arg0)
132 char *bin_dir;
133 char buf[256];
134 char *dir;
136 if (option_no_data) {
137 return NULL;
139 bin_dir = dirname(alloc_string(arg0));
140 strncpy(buf, bin_dir, 254);
141 buf[255] = '\0';
142 strncat(buf, "/smatch_data/", 254 - strlen(buf));
143 dir = alloc_string(buf);
144 if (!access(dir, R_OK))
145 return dir;
146 printf("Warning: %s is not accessible.\n", dir);
147 printf("Use --no-data to suppress this message.\n");
148 return NULL;
151 int main(int argc, char **argv)
153 int i;
154 reg_func func;
156 parse_args(&argc, &argv);
158 data_dir = get_data_dir(argv[0]);
160 /* The script IDs start at 1.
161 0 is used for internal stuff. */
162 create_function_hash();
163 for(i = 0; (func = reg_funcs[i]); i++){
164 func(i + 1);
166 register_function_hooks(-1);
168 smatch(argc, argv);
169 free_string(data_dir);
170 return 0;