-
Notifications
You must be signed in to change notification settings - Fork 14
/
check_continue_vs_break.c
129 lines (108 loc) · 2.87 KB
/
check_continue_vs_break.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
/*
* Copyright (C) 2015 Oracle.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, see http://www.gnu.org/copyleft/gpl.txt
*/
/*
* If you have code like:
* do {
* if (xxx)
* continue;
* }
* while (0);
*
* Then the continue is equivalent of a break. So what was really intended?
*/
#include "smatch.h"
#include "smatch_slist.h"
static int my_id;
static struct statement_list *iterator_stack;
static int is_do_while_zero(struct statement *stmt)
{
if (!stmt->iterator_post_condition)
return 0;
if (!expr_is_zero(stmt->iterator_post_condition))
return 0;
return 1;
}
static void push_statement(struct statement_list **stack, struct statement *stmt)
{
add_ptr_list(stack, stmt);
}
static void pop_statement(struct statement_list **stack)
{
delete_ptr_list_last((struct ptr_list **)stack);
}
static int inside_do_while_zero(void)
{
struct statement *stmt;
stmt = last_ptr_list((struct ptr_list *)iterator_stack);
return !!stmt;
}
static int loop_is_macro(void)
{
struct statement *stmt;
stmt = last_ptr_list((struct ptr_list *)iterator_stack);
if (!stmt)
return 0;
if (get_macro_name(stmt->iterator_post_condition->pos))
return 1;
return 0;
}
static void match_stmt(struct statement *stmt)
{
if (stmt->type != STMT_ITERATOR)
return;
if (is_do_while_zero(stmt))
push_statement(&iterator_stack, stmt);
else
push_statement(&iterator_stack, NULL);
}
static void match_stmt_after(struct statement *stmt)
{
if (stmt->type != STMT_ITERATOR)
return;
pop_statement(&iterator_stack);
}
static void match_inline_start(struct expression *expr)
{
push_statement(&iterator_stack, NULL);
}
static void match_inline_end(struct expression *expr)
{
pop_statement(&iterator_stack);
}
static void match_continue(struct statement *stmt)
{
if (stmt->type != STMT_GOTO)
return;
if (!stmt->goto_label || stmt->goto_label->type != SYM_NODE)
return;
if (strcmp(stmt->goto_label->ident->name, "continue") != 0)
return;
if (!inside_do_while_zero())
return;
if (loop_is_macro())
return;
sm_warning("continue to end of do { ... } while(0); loop");
}
void check_continue_vs_break(int id)
{
my_id = id;
add_hook(&match_stmt, STMT_HOOK);
add_hook(&match_stmt_after, STMT_HOOK_AFTER);
add_hook(&match_inline_start, INLINE_FN_START);
add_hook(&match_inline_end, INLINE_FN_END);
add_hook(&match_continue, STMT_HOOK);
}