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
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
)
30 if (stmt
->type
== STMT_EXPRESSION
&& !stmt
->expression
)
35 static int is_last_stmt(struct statement
*cur_stmt
)
37 struct symbol
*fn
= get_base_type(cur_func_sym
);
38 struct statement
*stmt
;
44 stmt
= fn
->inline_stmt
;
45 if (!stmt
|| stmt
->type
!= STMT_COMPOUND
)
47 stmt
= last_ptr_list((struct ptr_list
*)stmt
->stmts
);
53 static void print_unreached_initializers(struct symbol_list
*sym_list
)
57 FOR_EACH_PTR(sym_list
, sym
) {
58 if (sym
->initializer
&& !(sym
->ctype
.modifiers
& MOD_STATIC
))
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
)
69 name
= get_macro_name(stmt
->pos
);
73 FOR_EACH_PTR(ignore_names
, tmp
) {
74 if (strcmp(tmp
, name
) == 0)
76 } END_FOR_EACH_PTR(tmp
);
81 static int prev_line_was_endif(struct statement
*stmt
)
84 struct position pos
= stmt
->pos
;
89 token
= pos_get_token(pos
);
90 if (token
&& token_type(token
) == TOKEN_IDENT
&&
91 strcmp(show_ident(token
->ident
), "endif") == 0)
95 token
= pos_get_token(pos
);
96 if (token
&& token_type(token
) == TOKEN_IDENT
&&
97 strcmp(show_ident(token
->ident
), "endif") == 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:
117 * Every statement is reachable but only on the second iteration.
120 if (stmt
->type
!= STMT_ITERATOR
)
122 prev
= get_prev_statement();
123 if (prev
&& prev
->type
== STMT_GOTO
)
128 static void unreachable_stmt(struct statement
*stmt
)
134 if (!__path_is_null()) {
139 /* if we hit a label then assume there is a matching goto */
140 if (stmt
->type
== STMT_LABEL
)
142 if (prev_line_was_endif(stmt
))
144 if (we_jumped_into_the_middle_of_a_loop(stmt
))
147 if (!print_unreached
)
149 if (empty_statement(stmt
))
151 if (is_ignored_macro(stmt
))
154 switch (stmt
->type
) {
155 case STMT_COMPOUND
: /* after a switch before a case stmt */
159 case STMT_DECLARATION
: /* switch (x) { int a; case foo: ... */
160 print_unreached_initializers(stmt
->declaration
);
162 case STMT_RETURN
: /* gcc complains if you don't have a return statement */
163 if (is_last_stmt(stmt
))
167 /* people put extra breaks inside switch statements */
168 if (stmt
->goto_label
&& stmt
->goto_label
->type
== SYM_NODE
&&
169 strcmp(stmt
->goto_label
->ident
->name
, "break") == 0)
175 sm_warning("ignoring unreachable code.");
179 static int is_turn_off(char *name
)
186 FOR_EACH_PTR(turn_off_names
, tmp
) {
187 if (strcmp(tmp
, name
) == 0)
189 } END_FOR_EACH_PTR(tmp
);
194 static char *get_function_name(struct statement
*stmt
)
196 struct expression
*expr
;
198 if (stmt
->type
!= STMT_EXPRESSION
)
200 expr
= stmt
->expression
;
201 if (!expr
|| expr
->type
!= EXPR_CALL
)
203 if (expr
->fn
->type
!= EXPR_SYMBOL
|| !expr
->fn
->symbol_name
)
205 return expr
->fn
->symbol_name
->name
;
208 static void turn_off_unreachable(struct statement
*stmt
)
212 name
= get_macro_name(stmt
->pos
);
213 if (is_turn_off(name
)) {
218 if (stmt
->type
== STMT_IF
&&
219 known_condition_true(stmt
->if_conditional
) && __path_is_null()) {
224 name
= get_function_name(stmt
);
225 if (is_turn_off(name
))
229 static void register_turn_off_macros(void)
235 if (option_project
== PROJ_NONE
)
236 strcpy(name
, "unreachable.turn_off");
238 snprintf(name
, 256, "%s.unreachable.turn_off", option_project_str
);
240 token
= get_tokens_file(name
);
243 if (token_type(token
) != TOKEN_STREAMBEGIN
)
246 while (token_type(token
) != TOKEN_STREAMEND
) {
247 if (token_type(token
) != TOKEN_IDENT
)
249 macro
= alloc_string(show_ident(token
->ident
));
250 add_ptr_list(&turn_off_names
, macro
);
256 static void register_ignored_macros(void)
262 if (option_project
== PROJ_NONE
)
263 strcpy(name
, "unreachable.ignore");
265 snprintf(name
, 256, "%s.unreachable.ignore", option_project_str
);
267 token
= get_tokens_file(name
);
270 if (token_type(token
) != TOKEN_STREAMBEGIN
)
273 while (token_type(token
) != TOKEN_STREAMEND
) {
274 if (token_type(token
) != TOKEN_IDENT
)
276 macro
= alloc_string(show_ident(token
->ident
));
277 add_ptr_list(&ignore_names
, macro
);
283 void check_unreachable(int id
)
287 register_turn_off_macros();
288 register_ignored_macros();
289 add_hook(&unreachable_stmt
, STMT_HOOK
);
290 add_hook(&turn_off_unreachable
, STMT_HOOK_AFTER
);