param_key: fix container of when no struct member is referenced
[smatch.git] / check_unreachable.c
bloba3ec686633572fa18cb911e79f5131ed90e76d83
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 void print_unreached_initializers(struct symbol_list *sym_list)
37 struct symbol *sym;
39 FOR_EACH_PTR(sym_list, sym) {
40 if (sym->initializer && !(sym->ctype.modifiers & MOD_STATIC))
41 sm_msg("info: '%s' is not actually initialized (unreached code).",
42 (sym->ident ? sym->ident->name : "this variable"));
43 } END_FOR_EACH_PTR(sym);
46 static int is_ignored_macro(struct statement *stmt)
48 char *name;
49 char *tmp;
51 name = get_macro_name(stmt->pos);
52 if (!name)
53 return 0;
55 FOR_EACH_PTR(ignore_names, tmp) {
56 if (strcmp(tmp, name) == 0)
57 return 1;
58 } END_FOR_EACH_PTR(tmp);
60 return 0;
63 static int prev_line_was_endif(struct statement *stmt)
65 struct token *token;
66 struct position pos = stmt->pos;
68 pos.line--;
69 pos.pos = 2;
71 token = pos_get_token(pos);
72 if (token && token_type(token) == TOKEN_IDENT &&
73 strcmp(show_ident(token->ident), "endif") == 0)
74 return 1;
76 pos.line--;
77 token = pos_get_token(pos);
78 if (token && token_type(token) == TOKEN_IDENT &&
79 strcmp(show_ident(token->ident), "endif") == 0)
80 return 1;
82 return 0;
85 static int we_jumped_into_the_middle_of_a_loop(struct statement *stmt)
87 struct statement *prev;
90 * Smatch doesn't handle loops correctly and this is a hack. What we
91 * do is that if the first unreachable statement is a loop and the
92 * previous statement was a goto then it's probably code like this:
93 * goto first;
94 * for (;;) {
95 * frob();
96 * first:
97 * more_frob();
98 * }
99 * Every statement is reachable but only on the second iteration.
102 if (stmt->type != STMT_ITERATOR)
103 return 0;
104 prev = get_prev_statement();
105 if (prev && prev->type == STMT_GOTO)
106 return 1;
107 return 0;
110 static void unreachable_stmt(struct statement *stmt)
113 if (__inline_fn)
114 return;
116 if (!__path_is_null()) {
117 print_unreached = 1;
118 return;
121 /* if we hit a label then assume there is a matching goto */
122 if (stmt->type == STMT_LABEL)
123 print_unreached = 0;
124 if (prev_line_was_endif(stmt))
125 print_unreached = 0;
126 if (we_jumped_into_the_middle_of_a_loop(stmt))
127 print_unreached = 0;
129 if (!print_unreached)
130 return;
131 if (empty_statement(stmt))
132 return;
133 if (is_ignored_macro(stmt))
134 return;
136 switch (stmt->type) {
137 case STMT_COMPOUND: /* after a switch before a case stmt */
138 case STMT_RANGE:
139 case STMT_CASE:
140 return;
141 case STMT_DECLARATION: /* switch (x) { int a; case foo: ... */
142 print_unreached_initializers(stmt->declaration);
143 return;
144 case STMT_RETURN: /* gcc complains if you don't have a return statement */
145 if (is_last_stmt(stmt))
146 return;
147 break;
148 case STMT_GOTO:
149 /* people put extra breaks inside switch statements */
150 if (stmt->goto_label && stmt->goto_label->type == SYM_NODE &&
151 strcmp(stmt->goto_label->ident->name, "break") == 0)
152 return;
153 break;
154 default:
155 break;
157 sm_warning("ignoring unreachable code.");
158 print_unreached = 0;
161 static int is_turn_off(char *name)
163 char *tmp;
165 if (!name)
166 return 0;
168 FOR_EACH_PTR(turn_off_names, tmp) {
169 if (strcmp(tmp, name) == 0)
170 return 1;
171 } END_FOR_EACH_PTR(tmp);
173 return 0;
176 static char *get_function_name(struct statement *stmt)
178 struct expression *expr;
180 if (stmt->type != STMT_EXPRESSION)
181 return NULL;
182 expr = stmt->expression;
183 if (!expr || expr->type != EXPR_CALL)
184 return NULL;
185 if (expr->fn->type != EXPR_SYMBOL || !expr->fn->symbol_name)
186 return NULL;
187 return expr->fn->symbol_name->name;
190 static void turn_off_unreachable(struct statement *stmt)
192 char *name;
194 name = get_macro_name(stmt->pos);
195 if (is_turn_off(name)) {
196 print_unreached = 0;
197 return;
200 if (stmt->type == STMT_IF &&
201 known_condition_true(stmt->if_conditional) && __path_is_null()) {
202 print_unreached = 0;
203 return;
206 name = get_function_name(stmt);
207 if (is_turn_off(name))
208 print_unreached = 0;
211 static void register_turn_off_macros(void)
213 struct token *token;
214 char *macro;
215 char name[256];
217 if (option_project == PROJ_NONE)
218 strcpy(name, "unreachable.turn_off");
219 else
220 snprintf(name, 256, "%s.unreachable.turn_off", option_project_str);
222 token = get_tokens_file(name);
223 if (!token)
224 return;
225 if (token_type(token) != TOKEN_STREAMBEGIN)
226 return;
227 token = token->next;
228 while (token_type(token) != TOKEN_STREAMEND) {
229 if (token_type(token) != TOKEN_IDENT)
230 return;
231 macro = alloc_string(show_ident(token->ident));
232 add_ptr_list(&turn_off_names, macro);
233 token = token->next;
235 clear_token_alloc();
238 static void register_ignored_macros(void)
240 struct token *token;
241 char *macro;
242 char name[256];
244 if (option_project == PROJ_NONE)
245 strcpy(name, "unreachable.ignore");
246 else
247 snprintf(name, 256, "%s.unreachable.ignore", option_project_str);
249 token = get_tokens_file(name);
250 if (!token)
251 return;
252 if (token_type(token) != TOKEN_STREAMBEGIN)
253 return;
254 token = token->next;
255 while (token_type(token) != TOKEN_STREAMEND) {
256 if (token_type(token) != TOKEN_IDENT)
257 return;
258 macro = alloc_string(show_ident(token->ident));
259 add_ptr_list(&ignore_names, macro);
260 token = token->next;
262 clear_token_alloc();
265 void check_unreachable(int id)
267 my_id = id;
269 register_turn_off_macros();
270 register_ignored_macros();
271 add_hook(&unreachable_stmt, STMT_HOOK);
272 add_hook(&turn_off_unreachable, STMT_HOOK_AFTER);