2 * Copyright (C) 2020 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 #include "smatch_slist.h"
20 #include "smatch_extra.h"
26 static void ok_to_use(struct sm_state
*sm
, struct expression
*mod_expr
)
28 set_state(my_id
, sm
->name
, sm
->sym
, &undefined
);
31 static struct expression
*get_iterator(struct statement
*stmt
)
33 struct expression
*expr
;
36 stmt
->type
!= STMT_ITERATOR
||
37 !stmt
->iterator_pre_statement
||
38 stmt
->iterator_pre_statement
->type
!= STMT_EXPRESSION
)
42 expr
= strip_expr(stmt
->iterator_pre_statement
->expression
);
43 if (!expr
|| expr
->type
!= EXPR_ASSIGNMENT
)
46 return strip_expr(expr
->left
);
49 static void match_loop(struct statement
*stmt
)
51 struct expression
*pos
;
54 if (stmt
->type
!= STMT_ITERATOR
)
56 if (!stmt
->iterator_pre_statement
||
57 !stmt
->iterator_pre_condition
||
58 !stmt
->iterator_post_statement
)
61 macro
= get_macro_name(stmt
->pos
);
64 if (strncmp(macro
, "list_for_each", strlen("list_for_each")) != 0)
66 pos
= get_iterator(stmt
);
70 set_state_expr(my_id
, pos
, &iterator
);
73 static bool getting_prev_next(struct expression
*expr
)
75 struct expression
*parent
;
79 while ((parent
= expr_get_parent_expr(parent
))) {
80 if (parent
->type
== EXPR_PREOP
&& parent
->op
== '(')
82 if (parent
->type
== EXPR_DEREF
&&
84 (strcmp(parent
->member
->name
, "prev") == 0 ||
85 strcmp(parent
->member
->name
, "next") == 0))
94 static void match_dereference(struct expression
*expr
)
96 struct expression
*orig
= expr
;
100 if (expr
->type
== EXPR_PREOP
&& expr
->op
== '*')
101 expr
= strip_expr(expr
->unop
);
103 sm
= get_sm_state_expr(my_id
, expr
);
104 if (!sm
|| !slist_has_state(sm
->possible
, &iterator
))
107 if (getting_prev_next(orig
))
110 name
= expr_to_str(expr
);
111 sm_warning("iterator used outside loop: '%s'", name
);
114 set_state_expr(my_id
, expr
, &undefined
);
117 void check_iterator_outside_loop(int id
)
121 if (option_project
!= PROJ_KERNEL
)
124 add_hook(match_loop
, AFTER_LOOP_NO_BREAKS
);
125 add_modification_hook(my_id
, &ok_to_use
);
126 add_hook(&match_dereference
, DEREF_HOOK
);