Print this page
11506 smatch resync
Split |
Close |
Expand all |
Collapse all |
--- old/usr/src/tools/smatch/src/check_macro_side_effects.c
+++ new/usr/src/tools/smatch/src/check_macro_side_effects.c
1 1 /*
2 2 * Copyright (C) 2012 Oracle.
3 3 *
4 4 * This program is free software; you can redistribute it and/or
5 5 * modify it under the terms of the GNU General Public License
6 6 * as published by the Free Software Foundation; either version 2
7 7 * of the License, or (at your option) any later version.
8 8 *
9 9 * This program is distributed in the hope that it will be useful,
10 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 12 * GNU General Public License for more details.
13 13 *
14 14 * You should have received a copy of the GNU General Public License
15 15 * along with this program; if not, see http://www.gnu.org/copyleft/gpl.txt
16 16 */
17 17
18 18 #include "scope.h"
19 19 #include "smatch.h"
20 20 #include "smatch_slist.h"
21 21 #include "smatch_expression_stacks.h"
22 22
↓ open down ↓ |
22 lines elided |
↑ open up ↑ |
23 23 static int my_id;
24 24
25 25 static struct string_list *ignored_macros;
26 26 static struct position old_pos;
27 27
28 28 static struct smatch_state *alloc_my_state(struct expression *expr)
29 29 {
30 30 struct smatch_state *state;
31 31 char *name;
32 32
33 - state = __alloc_smatch_state(0);
34 33 expr = strip_expr(expr);
35 34 name = expr_to_str(expr);
35 + if (!name)
36 + return NULL;
37 +
38 + state = __alloc_smatch_state(0);
36 39 state->name = alloc_sname(name);
37 40 free_string(name);
38 41 state->data = expr;
39 42 return state;
40 43 }
41 44
42 45 static int defined_inside_macro(struct position macro_pos, struct expression *expr)
43 46 {
44 47 char *name;
45 48 struct symbol *sym;
46 49 int ret = 0;
47 50
48 51 name = expr_to_var_sym(expr, &sym);
49 52 if (!name || !sym)
50 53 goto free;
51 54 if (!sym->scope || !sym->scope->token)
52 55 goto free;
53 56 if (positions_eq(macro_pos, sym->scope->token->pos))
54 57 ret = 1;
55 58 free:
56 59 free_string(name);
57 60 return ret;
58 61 }
59 62
60 63 static int affected_inside_macro_before(struct expression *expr)
61 64 {
62 65 struct sm_state *sm;
63 66 struct sm_state *tmp;
64 67 struct expression *old_mod;
65 68
66 69 sm = get_sm_state_expr(my_id, expr);
67 70 if (!sm)
68 71 return 0;
69 72
70 73 FOR_EACH_PTR(sm->possible, tmp) {
71 74 old_mod = tmp->state->data;
72 75 if (!old_mod)
73 76 continue;
74 77 if (positions_eq(old_mod->pos, expr->pos))
75 78 return 1;
76 79 } END_FOR_EACH_PTR(tmp);
77 80 return 0;
78 81 }
79 82
80 83 static int is_ignored_macro(const char *macro)
81 84 {
82 85 char *tmp;
83 86
84 87 FOR_EACH_PTR(ignored_macros, tmp) {
85 88 if (!strcmp(tmp, macro))
86 89 return 1;
87 90 } END_FOR_EACH_PTR(tmp);
88 91 return 0;
89 92 }
90 93
91 94 static void match_unop(struct expression *raw_expr)
92 95 {
93 96 struct expression *expr;
94 97 char *macro, *name;
95 98
96 99 if (raw_expr->op != SPECIAL_INCREMENT && raw_expr->op != SPECIAL_DECREMENT)
97 100 return;
98 101
99 102 macro = get_macro_name(raw_expr->pos);
100 103 if (!macro)
101 104 return;
102 105
103 106 expr = strip_expr(raw_expr->unop);
104 107
105 108 if (defined_inside_macro(expr->pos, expr))
106 109 return;
107 110
108 111 if (is_ignored_macro(macro))
109 112 return;
110 113
111 114 if (!affected_inside_macro_before(expr)) {
112 115 set_state_expr(my_id, expr, alloc_my_state(expr));
113 116 old_pos = expr->pos;
114 117 return;
115 118 }
116 119
117 120 if (!positions_eq(old_pos, expr->pos))
118 121 return;
119 122
120 123 name = expr_to_str(raw_expr);
121 124 sm_warning("side effect in macro '%s' doing '%s'",
122 125 macro, name);
123 126 free_string(name);
124 127 }
125 128
126 129 static void match_stmt(struct statement *stmt)
127 130 {
128 131 if (!positions_eq(old_pos, stmt->pos))
129 132 old_pos.line = 0;
130 133 }
131 134
132 135 static void register_ignored_macros(void)
133 136 {
134 137 struct token *token;
135 138 char *macro;
136 139 char name[256];
137 140
138 141 snprintf(name, 256, "%s.ignore_side_effects", option_project_str);
139 142
140 143 token = get_tokens_file(name);
141 144 if (!token)
142 145 return;
143 146 if (token_type(token) != TOKEN_STREAMBEGIN)
144 147 return;
145 148 token = token->next;
146 149 while (token_type(token) != TOKEN_STREAMEND) {
147 150 if (token_type(token) != TOKEN_IDENT)
148 151 return;
149 152 macro = alloc_string(show_ident(token->ident));
150 153 add_ptr_list(&ignored_macros, macro);
151 154 token = token->next;
152 155 }
↓ open down ↓ |
107 lines elided |
↑ open up ↑ |
153 156 clear_token_alloc();
154 157 }
155 158
156 159 void check_macro_side_effects(int id)
157 160 {
158 161 my_id = id;
159 162
160 163 if (!option_spammy)
161 164 return;
162 165
166 + set_dynamic_states(my_id);
163 167 add_hook(&match_unop, OP_HOOK);
164 168 add_hook(&match_stmt, STMT_HOOK);
165 169 register_ignored_macros();
166 170 }
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX