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 bool empty_statement(struct statement
*stmt
)
30 if (stmt
->type
== STMT_EXPRESSION
&& !stmt
->expression
)
35 static void print_unreached_initializers(struct symbol_list
*sym_list
)
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 bool is_ignored_macro(struct statement
*stmt
)
51 name
= get_macro_name(stmt
->pos
);
55 if (strncmp(name
, "for_", 4) == 0)
58 FOR_EACH_PTR(ignore_names
, tmp
) {
59 if (strcmp(tmp
, name
) == 0)
61 } END_FOR_EACH_PTR(tmp
);
66 static bool prev_line_was_endif(struct statement
*stmt
)
69 struct position pos
= stmt
->pos
;
74 token
= pos_get_token(pos
);
75 if (token
&& token_type(token
) == TOKEN_IDENT
&&
76 strcmp(show_ident(token
->ident
), "endif") == 0)
80 token
= pos_get_token(pos
);
81 if (token
&& token_type(token
) == TOKEN_IDENT
&&
82 strcmp(show_ident(token
->ident
), "endif") == 0)
88 static bool we_jumped_into_the_middle_of_a_loop(struct statement
*stmt
)
90 struct statement
*prev
;
93 * Smatch doesn't handle loops correctly and this is a hack. What we
94 * do is that if the first unreachable statement is a loop and the
95 * previous statement was a goto then it's probably code like this:
102 * Every statement is reachable but only on the second iteration.
105 if (stmt
->type
!= STMT_ITERATOR
)
107 prev
= get_prev_statement();
108 if (prev
&& prev
->type
== STMT_GOTO
)
113 static void unreachable_stmt(struct statement
*stmt
)
119 if (!__path_is_null()) {
124 /* if we hit a label then assume there is a matching goto */
125 if (stmt
->type
== STMT_LABEL
)
127 if (prev_line_was_endif(stmt
))
129 if (we_jumped_into_the_middle_of_a_loop(stmt
))
132 if (!print_unreached
)
134 if (empty_statement(stmt
))
136 if (is_ignored_macro(stmt
))
139 switch (stmt
->type
) {
140 case STMT_COMPOUND
: /* after a switch before a case stmt */
144 case STMT_DECLARATION
: /* switch (x) { int a; case foo: ... */
145 print_unreached_initializers(stmt
->declaration
);
147 case STMT_RETURN
: /* gcc complains if you don't have a return statement */
148 if (is_last_stmt(stmt
))
152 /* people put extra breaks inside switch statements */
153 if (stmt
->goto_label
&& stmt
->goto_label
->type
== SYM_NODE
&&
154 strcmp(stmt
->goto_label
->ident
->name
, "break") == 0)
160 sm_warning("ignoring unreachable code.");
164 static bool is_turn_off(char *name
)
171 FOR_EACH_PTR(turn_off_names
, tmp
) {
172 if (strcmp(tmp
, name
) == 0)
174 } END_FOR_EACH_PTR(tmp
);
179 static char *get_function_name(struct statement
*stmt
)
181 struct expression
*expr
;
183 if (stmt
->type
!= STMT_EXPRESSION
)
185 expr
= stmt
->expression
;
186 if (!expr
|| expr
->type
!= EXPR_CALL
)
188 if (expr
->fn
->type
!= EXPR_SYMBOL
|| !expr
->fn
->symbol_name
)
190 return expr
->fn
->symbol_name
->name
;
193 static void turn_off_unreachable(struct statement
*stmt
)
197 name
= get_macro_name(stmt
->pos
);
198 if (is_turn_off(name
)) {
203 if (stmt
->type
== STMT_IF
&&
204 known_condition_true(stmt
->if_conditional
) && __path_is_null()) {
209 name
= get_function_name(stmt
);
210 if (is_turn_off(name
))
214 static void register_turn_off_macros(void)
220 if (option_project
== PROJ_NONE
)
221 strcpy(name
, "unreachable.turn_off");
223 snprintf(name
, 256, "%s.unreachable.turn_off", option_project_str
);
225 token
= get_tokens_file(name
);
228 if (token_type(token
) != TOKEN_STREAMBEGIN
)
231 while (token_type(token
) != TOKEN_STREAMEND
) {
232 if (token_type(token
) != TOKEN_IDENT
)
234 macro
= alloc_string(show_ident(token
->ident
));
235 add_ptr_list(&turn_off_names
, macro
);
241 static void register_ignored_macros(void)
247 if (option_project
== PROJ_NONE
)
248 strcpy(name
, "unreachable.ignore");
250 snprintf(name
, 256, "%s.unreachable.ignore", option_project_str
);
252 token
= get_tokens_file(name
);
255 if (token_type(token
) != TOKEN_STREAMBEGIN
)
258 while (token_type(token
) != TOKEN_STREAMEND
) {
259 if (token_type(token
) != TOKEN_IDENT
)
261 macro
= alloc_string(show_ident(token
->ident
));
262 add_ptr_list(&ignore_names
, macro
);
268 void check_unreachable(int id
)
272 register_turn_off_macros();
273 register_ignored_macros();
274 add_hook(&unreachable_stmt
, STMT_HOOK
);
275 add_hook(&turn_off_unreachable
, STMT_HOOK_AFTER
);