Repository navigation
Expand file tree
/
Copy pathcheck_missing_break.c
More file actions
185 lines (152 loc) · 4.16 KB
/
Copy pathcheck_missing_break.c
File metadata and controls
185 lines (152 loc) · 4.16 KB
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
/*
* Copyright (C) 2013 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
*/
/*
* The way I'm detecting missing breaks is if there is an assignment inside a
* switch statement which is over written.
*
*/
#include "smatch.h"
#include "smatch_slist.h"
static int my_id;
static struct expression *skip_this;
/*
* It goes like this:
* - Allocate a state which stores the switch expression. I wanted to
* just have a state &assigned but we need to know the switch statement where
* it was assigned.
* - If it gets used then we change it to &used.
* - For unmatched states we use &used (because of cleanness, not because we need
* to).
* - If we merge inside a case statement and one of the states is &assigned (or
* if it is &nobreak) then &nobreak is used.
*
* We print an error when we assign something to a &no_break symbol.
*
*/
STATE(used);
STATE(no_break);
static int in_switch_stmt;
static struct smatch_state *alloc_my_state(struct expression *expr)
{
struct smatch_state *state;
char *name;
state = __alloc_smatch_state(0);
expr = strip_expr(expr);
name = expr_to_str(expr);
if (!name)
name = alloc_string("");
state->name = alloc_sname(name);
free_string(name);
state->data = expr;
return state;
}
struct expression *last_print_expr;
static void print_missing_break(struct expression *expr)
{
char *name;
if (get_switch_expr() == last_print_expr)
return;
last_print_expr = get_switch_expr();
name = expr_to_var(expr);
sm_msg("warn: missing break? reassigning '%s'", name);
free_string(name);
}
static void match_assign(struct expression *expr)
{
struct expression *left;
if (expr->op != '=')
return;
if (!get_switch_expr())
return;
left = strip_expr(expr->left);
if (get_state_expr(my_id, left) == &no_break)
print_missing_break(left);
set_state_expr(my_id, left, alloc_my_state(get_switch_expr()));
skip_this = left;
}
static void match_symbol(struct expression *expr)
{
if (outside_of_function())
return;
if (!get_switch_expr())
return;
expr = strip_expr(expr);
if (expr == skip_this)
return;
set_state_expr(my_id, expr, &used);
}
static struct smatch_state *unmatched_state(struct sm_state *sm)
{
return &used;
}
static int in_case;
static struct smatch_state *merge_hook(struct smatch_state *s1, struct smatch_state *s2)
{
struct expression *switch_expr;
if (s1 == &no_break || s2 == &no_break)
return &no_break;
if (!in_case)
return &used;
switch_expr = get_switch_expr();
if (s1->data == switch_expr || s2->data == switch_expr)
return &no_break;
return &used;
}
static void match_stmt(struct statement *stmt)
{
if (stmt->type == STMT_CASE)
in_case = 1;
else
in_case = 0;
}
static void match_switch(struct statement *stmt)
{
if (stmt->type != STMT_SWITCH)
return;
in_switch_stmt++;
}
static void delete_my_states(int owner)
{
struct state_list *slist = NULL;
struct sm_state *sm;
FOR_EACH_MY_SM(owner, __get_cur_stree(), sm) {
add_ptr_list(&slist, sm);
} END_FOR_EACH_SM(sm);
FOR_EACH_PTR(slist, sm) {
delete_state(sm->owner, sm->name, sm->sym);
} END_FOR_EACH_PTR(sm);
free_slist(&slist);
}
static void match_switch_end(struct statement *stmt)
{
if (stmt->type != STMT_SWITCH)
return;
in_switch_stmt--;
if (!in_switch_stmt)
delete_my_states(my_id);
}
void check_missing_break(int id)
{
my_id = id;
add_unmatched_state_hook(my_id, &unmatched_state);
add_merge_hook(my_id, &merge_hook);
add_hook(&match_assign, ASSIGNMENT_HOOK);
add_hook(&match_symbol, SYM_HOOK);
add_hook(&match_stmt, STMT_HOOK);
add_hook(&match_switch, STMT_HOOK);
add_hook(&match_switch_end, STMT_HOOK_AFTER);
}