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