helper: introduce get_prev_statement() (fix the build).
[smatch.git] / check_unreachable.c
blob9beab208bde7f45be86ed9bbf05f065e84985b5b
1 /*
2 * Copyright (C) 2014 Oracle.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, see http://www.gnu.org/copyleft/gpl.txt
18 #include "smatch.h"
20 static int my_id;
22 static int print_unreached = 1;
23 static struct string_list *turn_off_names;
24 static struct string_list *ignore_names;
26 static int empty_statement(struct statement *stmt)
28 if (!stmt)
29 return 0;
30 if (stmt->type == STMT_EXPRESSION && !stmt->expression)
31 return 1;
32 return 0;
35 static int is_last_stmt(struct statement *cur_stmt)
37 struct symbol *fn = get_base_type(cur_func_sym);
38 struct statement *stmt;
40 if (!fn)
41 return 0;
42 stmt = fn->stmt;
43 if (!stmt)
44 stmt = fn->inline_stmt;
45 if (!stmt || stmt->type != STMT_COMPOUND)
46 return 0;
47 stmt = last_ptr_list((struct ptr_list *)stmt->stmts);
48 if (stmt == cur_stmt)
49 return 1;
50 return 0;
53 static void print_unreached_initializers(struct symbol_list *sym_list)
55 struct symbol *sym;
57 FOR_EACH_PTR(sym_list, sym) {
58 if (sym->initializer)
59 sm_msg("info: '%s' is not actually initialized (unreached code).",
60 (sym->ident ? sym->ident->name : "this variable"));
61 } END_FOR_EACH_PTR(sym);
64 static int is_ignored_macro(struct statement *stmt)
66 char *name;
67 char *tmp;
69 name = get_macro_name(stmt->pos);
70 if (!name)
71 return 0;
73 FOR_EACH_PTR(ignore_names, tmp) {
74 if (strcmp(tmp, name) == 0)
75 return 1;
76 } END_FOR_EACH_PTR(tmp);
78 return 0;
81 static int prev_line_was_endif(struct statement *stmt)
83 struct token *token;
84 struct position pos = stmt->pos;
86 pos.line--;
87 pos.pos = 2;
89 token = pos_get_token(pos);
90 if (token && token_type(token) == TOKEN_IDENT &&
91 strcmp(show_ident(token->ident), "endif") == 0)
92 return 1;
94 pos.line--;
95 token = pos_get_token(pos);
96 if (token && token_type(token) == TOKEN_IDENT &&
97 strcmp(show_ident(token->ident), "endif") == 0)
98 return 1;
100 return 0;
103 static int we_jumped_into_the_middle_of_a_loop(struct statement *stmt)
105 struct statement *prev;
108 * Smatch doesn't handle loops correctly and this is a hack. What we
109 * do is that if the first unreachable statement is a loop and the
110 * previous statement was a goto then it's probably code like this:
111 * goto first;
112 * for (;;) {
113 * frob();
114 * first:
115 * more_frob();
117 * Every statement is reachable but only on the second iteration.
120 if (stmt->type != STMT_ITERATOR)
121 return 0;
122 prev = get_prev_statement();
123 if (prev && prev->type == STMT_GOTO)
124 return 1;
125 return 0;
128 static void unreachable_stmt(struct statement *stmt)
131 if (__inline_fn)
132 return;
134 if (!__path_is_null()) {
135 print_unreached = 1;
136 return;
139 /* if we hit a label then assume there is a matching goto */
140 if (stmt->type == STMT_LABEL)
141 print_unreached = 0;
142 if (prev_line_was_endif(stmt))
143 print_unreached = 0;
144 if (we_jumped_into_the_middle_of_a_loop(stmt))
145 print_unreached = 0;
147 if (!print_unreached)
148 return;
150 switch (stmt->type) {
151 case STMT_COMPOUND: /* after a switch before a case stmt */
152 case STMT_RANGE:
153 case STMT_CASE:
154 return;
155 case STMT_DECLARATION: /* switch (x) { int a; case foo: ... */
156 print_unreached_initializers(stmt->declaration);
157 return;
158 case STMT_RETURN: /* gcc complains if you don't have a return statement */
159 if (is_last_stmt(stmt))
160 return;
161 break;
162 case STMT_GOTO:
163 /* people put extra breaks inside switch statements */
164 if (stmt->goto_label && stmt->goto_label->type == SYM_NODE &&
165 strcmp(stmt->goto_label->ident->name, "break") == 0)
166 return;
167 break;
168 default:
169 break;
171 if (empty_statement(stmt))
172 return;
173 if (is_ignored_macro(stmt))
174 return;
175 if (!option_spammy)
176 return;
177 sm_msg("info: ignoring unreachable code.");
178 print_unreached = 0;
181 static int is_turn_off(char *name)
183 char *tmp;
185 if (!name)
186 return 0;
188 FOR_EACH_PTR(turn_off_names, tmp) {
189 if (strcmp(tmp, name) == 0)
190 return 1;
191 } END_FOR_EACH_PTR(tmp);
193 return 0;
196 static char *get_function_name(struct statement *stmt)
198 struct expression *expr;
200 if (stmt->type != STMT_EXPRESSION)
201 return NULL;
202 expr = stmt->expression;
203 if (!expr || expr->type != EXPR_CALL)
204 return NULL;
205 if (expr->fn->type != EXPR_SYMBOL || !expr->fn->symbol_name)
206 return NULL;
207 return expr->fn->symbol_name->name;
210 static void turn_off_unreachable(struct statement *stmt)
212 char *name;
214 name = get_macro_name(stmt->pos);
215 if (is_turn_off(name)) {
216 print_unreached = 0;
217 return;
220 if (stmt->type == STMT_IF &&
221 known_condition_true(stmt->if_conditional) && __path_is_null()) {
222 print_unreached = 0;
223 return;
226 name = get_function_name(stmt);
227 if (is_turn_off(name))
228 print_unreached = 0;
231 static void register_turn_off_macros(void)
233 struct token *token;
234 char *macro;
235 char name[256];
237 if (option_project == PROJ_NONE)
238 strcpy(name, "unreachable.turn_off");
239 else
240 snprintf(name, 256, "%s.unreachable.turn_off", option_project_str);
242 token = get_tokens_file(name);
243 if (!token)
244 return;
245 if (token_type(token) != TOKEN_STREAMBEGIN)
246 return;
247 token = token->next;
248 while (token_type(token) != TOKEN_STREAMEND) {
249 if (token_type(token) != TOKEN_IDENT)
250 return;
251 macro = alloc_string(show_ident(token->ident));
252 add_ptr_list(&turn_off_names, macro);
253 token = token->next;
255 clear_token_alloc();
258 static void register_ignored_macros(void)
260 struct token *token;
261 char *macro;
262 char name[256];
264 if (option_project == PROJ_NONE)
265 strcpy(name, "unreachable.ignore");
266 else
267 snprintf(name, 256, "%s.unreachable.ignore", option_project_str);
269 token = get_tokens_file(name);
270 if (!token)
271 return;
272 if (token_type(token) != TOKEN_STREAMBEGIN)
273 return;
274 token = token->next;
275 while (token_type(token) != TOKEN_STREAMEND) {
276 if (token_type(token) != TOKEN_IDENT)
277 return;
278 macro = alloc_string(show_ident(token->ident));
279 add_ptr_list(&ignore_names, macro);
280 token = token->next;
282 clear_token_alloc();
285 void check_unreachable(int id)
287 my_id = id;
289 register_turn_off_macros();
290 register_ignored_macros();
291 add_hook(&unreachable_stmt, STMT_HOOK);
292 add_hook(&turn_off_unreachable, STMT_HOOK_AFTER);