-
Notifications
You must be signed in to change notification settings - Fork 5
/
check_iterator_outside_loop.c
121 lines (98 loc) · 2.84 KB
/
check_iterator_outside_loop.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
/*
* Copyright (C) 2020 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
*/
#include "smatch.h"
#include "smatch_slist.h"
#include "smatch_extra.h"
static int my_id;
STATE(iterator);
static struct expression *get_iterator(struct statement *stmt)
{
struct expression *expr;
if (!stmt ||
stmt->type != STMT_ITERATOR ||
!stmt->iterator_pre_statement ||
stmt->iterator_pre_statement->type != STMT_EXPRESSION)
return NULL;
expr = strip_expr(stmt->iterator_pre_statement->expression);
if (!expr || expr->type != EXPR_ASSIGNMENT)
return NULL;
return strip_expr(expr->left);
}
static void match_loop(struct statement *stmt)
{
struct expression *pos;
char *macro;
if (stmt->type != STMT_ITERATOR)
return;
if (!stmt->iterator_pre_statement ||
!stmt->iterator_pre_condition ||
!stmt->iterator_post_statement)
return;
macro = get_macro_name(stmt->pos);
if (!macro)
return;
if (strncmp(macro, "list_for_each", strlen("list_for_each")) != 0)
return;
pos = get_iterator(stmt);
if (!pos)
return;
set_state_expr(my_id, pos, &iterator);
}
static bool getting_prev_next(struct expression *expr)
{
struct expression *parent;
int cnt = 0;
parent = expr;
while ((parent = expr_get_parent_expr(parent))) {
if (parent->type == EXPR_PREOP && parent->op == '(')
continue;
if (parent->type == EXPR_DEREF &&
parent->member &&
(strcmp(parent->member->name, "prev") == 0 ||
strcmp(parent->member->name, "next") == 0))
return true;
if (cnt++ > 3)
break;
}
return false;
}
static void match_dereference(struct expression *expr)
{
struct expression *orig = expr;
struct sm_state *sm;
char *name;
if (expr->type == EXPR_PREOP && expr->op == '*')
expr = strip_expr(expr->unop);
sm = get_sm_state_expr(my_id, expr);
if (!sm || !slist_has_state(sm->possible, &iterator))
return;
if (getting_prev_next(orig))
return;
name = expr_to_str(expr);
sm_warning("iterator used outside loop: '%s'", name);
free_string(name);
set_state_expr(my_id, expr, &undefined);
}
void check_iterator_outside_loop(int id)
{
my_id = id;
if (option_project != PROJ_KERNEL)
return;
add_hook(match_loop, AFTER_LOOP_NO_BREAKS);
add_modification_hook(my_id, &set_undefined);
add_hook(&match_dereference, DEREF_HOOK);
}