2 * Copyright (C) 2015 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
19 * If you have code like:
25 * Then the continue is equivalent of a break. So what was really intended?
29 #include "smatch_slist.h"
33 static struct statement_list
*iterator_stack
;
35 static int is_do_while_zero(struct statement
*stmt
)
37 if (!stmt
->iterator_post_condition
)
39 if (!expr_is_zero(stmt
->iterator_post_condition
))
44 static void push_statement(struct statement_list
**stack
, struct statement
*stmt
)
46 add_ptr_list(stack
, stmt
);
49 static void pop_statement(struct statement_list
**stack
)
51 delete_ptr_list_last((struct ptr_list
**)stack
);
54 static int inside_do_while_zero(void)
56 struct statement
*stmt
;
58 stmt
= last_ptr_list((struct ptr_list
*)iterator_stack
);
62 static int loop_is_macro(void)
64 struct statement
*stmt
;
66 stmt
= last_ptr_list((struct ptr_list
*)iterator_stack
);
69 if (get_macro_name(stmt
->iterator_post_condition
->pos
))
74 static void match_stmt(struct statement
*stmt
)
76 if (stmt
->type
!= STMT_ITERATOR
)
79 if (is_do_while_zero(stmt
)) {
80 push_statement(&iterator_stack
, stmt
);
82 push_statement(&iterator_stack
, NULL
);
85 static void match_stmt_after(struct statement
*stmt
)
87 if (stmt
->type
!= STMT_ITERATOR
)
90 pop_statement(&iterator_stack
);
93 static void match_inline_start(struct expression
*expr
)
95 push_statement(&iterator_stack
, NULL
);
98 static void match_inline_end(struct expression
*expr
)
100 pop_statement(&iterator_stack
);
103 static void match_continue(struct statement
*stmt
)
105 if (stmt
->type
!= STMT_GOTO
)
108 if (!stmt
->goto_label
|| stmt
->goto_label
->type
!= SYM_NODE
)
110 if (strcmp(stmt
->goto_label
->ident
->name
, "continue") != 0)
112 if (!inside_do_while_zero())
116 sm_warning("continue to end of do { ... } while(0); loop");
119 void check_continue_vs_break(int id
)
122 add_hook(&match_stmt
, STMT_HOOK
);
123 add_hook(&match_stmt_after
, STMT_HOOK_AFTER
);
124 add_hook(&match_inline_start
, INLINE_FN_START
);
125 add_hook(&match_inline_end
, INLINE_FN_END
);
127 add_hook(&match_continue
, STMT_HOOK
);