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 empty_statement(struct statement
*stmt
)
26 if (stmt
->type
== STMT_EXPRESSION
&& !stmt
->expression
)
31 static int is_last_stmt(struct statement
*cur_stmt
)
33 struct symbol
*fn
= get_base_type(cur_func_sym
);
34 struct statement
*stmt
;
40 stmt
= fn
->inline_stmt
;
41 if (!stmt
|| stmt
->type
!= STMT_COMPOUND
)
43 stmt
= last_ptr_list((struct ptr_list
*)stmt
->stmts
);
49 static void print_unreached_initializers(struct symbol_list
*sym_list
)
53 FOR_EACH_PTR(sym_list
, sym
) {
55 sm_msg("info: '%s' is not actually initialized (unreached code).",
56 (sym
->ident
? sym
->ident
->name
: "this variable"));
57 } END_FOR_EACH_PTR(sym
);
60 static void print_unreached(struct statement
*stmt
)
67 if (!__path_is_null()) {
75 case STMT_COMPOUND
: /* after a switch before a case stmt */
80 case STMT_DECLARATION
: /* switch (x) { int a; case foo: ... */
81 print_unreached_initializers(stmt
->declaration
);
83 case STMT_RETURN
: /* gcc complains if you don't have a return statement */
84 if (is_last_stmt(stmt
))
88 /* people put extra breaks inside switch statements */
89 if (stmt
->goto_label
&& stmt
->goto_label
->type
== SYM_NODE
&&
90 strcmp(stmt
->goto_label
->ident
->name
, "break") == 0)
96 if (empty_statement(stmt
))
100 sm_msg("info: ignoring unreachable code.");
104 void check_unreachable(int id
)
108 add_hook(&print_unreached
, STMT_HOOK
);