/
unmodified.d
308 lines (270 loc) · 6.63 KB
/
unmodified.d
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
// Copyright Brian Schott (Hackerpilot) 2015.
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
module analysis.unmodified;
import analysis.base;
import dsymbol.scope_ : Scope;
import std.container;
import dparse.ast;
import dparse.lexer;
/**
* Checks for variables that could have been declared const or immutable
*/
class UnmodifiedFinder : BaseAnalyzer
{
alias visit = BaseAnalyzer.visit;
///
this(string fileName, const(Scope)* sc)
{
super(fileName, sc);
}
override void visit(const Module mod)
{
pushScope();
mod.accept(this);
popScope();
}
override void visit(const BlockStatement blockStatement)
{
pushScope();
blockStatementDepth++;
blockStatement.accept(this);
blockStatementDepth--;
popScope();
}
override void visit(const StructBody structBody)
{
pushScope();
immutable oldBlockStatementDepth = blockStatementDepth;
blockStatementDepth = 0;
structBody.accept(this);
blockStatementDepth = oldBlockStatementDepth;
popScope();
}
override void visit(const VariableDeclaration dec)
{
if (dec.autoDeclaration is null && blockStatementDepth > 0
&& isImmutable <= 0 && !canFindImmutable(dec))
{
foreach (d; dec.declarators)
{
if (initializedFromCast(d.initializer))
continue;
tree[$ - 1].insert(new VariableInfo(d.name.text, d.name.line,
d.name.column, isValueTypeSimple(dec.type)));
}
}
dec.accept(this);
}
override void visit(const AutoDeclaration autoDeclaration)
{
import std.algorithm : canFind;
if (blockStatementDepth > 0 && isImmutable <= 0
&& (!autoDeclaration.storageClasses.canFind!(a => a.token == tok!"const"
|| a.token == tok!"enum" || a.token == tok!"immutable")))
{
foreach (size_t i, id; autoDeclaration.identifiers)
{
if (initializedFromCast(autoDeclaration.initializers[i]))
continue;
tree[$ - 1].insert(new VariableInfo(id.text, id.line,
id.column));
}
}
autoDeclaration.accept(this);
}
override void visit(const AssignExpression assignExpression)
{
if (assignExpression.operator != tok!"")
{
interest++;
guaranteeUse++;
assignExpression.ternaryExpression.accept(this);
guaranteeUse--;
interest--;
if (assignExpression.operator == tok!"~=")
interest++;
assignExpression.expression.accept(this);
if (assignExpression.operator == tok!"~=")
interest--;
}
else
assignExpression.accept(this);
}
override void visit(const Declaration dec)
{
if (canFindImmutableOrConst(dec))
{
isImmutable++;
dec.accept(this);
isImmutable--;
}
else
dec.accept(this);
}
override void visit(const IdentifierChain ic)
{
if (ic.identifiers.length && interest > 0)
variableMightBeModified(ic.identifiers[0].text);
ic.accept(this);
}
override void visit(const IdentifierOrTemplateInstance ioti)
{
if (ioti.identifier != tok!"" && interest > 0)
variableMightBeModified(ioti.identifier.text);
ioti.accept(this);
}
mixin PartsMightModify!AsmPrimaryExp;
mixin PartsMightModify!IndexExpression;
mixin PartsMightModify!FunctionCallExpression;
mixin PartsMightModify!IdentifierOrTemplateChain;
mixin PartsMightModify!ReturnStatement;
override void visit(const UnaryExpression unary)
{
if (unary.prefix == tok!"++" || unary.prefix == tok!"--"
|| unary.suffix == tok!"++" || unary.suffix == tok!"--"
|| unary.prefix == tok!"*" || unary.prefix == tok!"&")
{
interest++;
guaranteeUse++;
unary.accept(this);
guaranteeUse--;
interest--;
}
else
unary.accept(this);
}
override void visit(const ForeachStatement foreachStatement)
{
if (foreachStatement.low !is null)
{
interest++;
foreachStatement.low.accept(this);
interest--;
}
foreachStatement.declarationOrStatement.accept(this);
}
override void visit(const TraitsExpression)
{
// issue #266: Ignore unmodified variables inside of `__traits` expressions
}
override void visit(const TypeofExpression)
{
// issue #270: Ignore unmodified variables inside of `typeof` expressions
}
override void visit(const AsmStatement a)
{
inAsm = true;
a.accept(this);
inAsm = false;
}
private:
template PartsMightModify(T)
{
override void visit(const T t)
{
interest++;
t.accept(this);
interest--;
}
}
void variableMightBeModified(string name)
{
size_t index = tree.length - 1;
auto vi = VariableInfo(name);
if (guaranteeUse == 0)
{
auto r = tree[index].equalRange(&vi);
if (!r.empty && r.front.isValueType && !inAsm)
return;
}
while (true)
{
if (tree[index].removeKey(&vi) != 0 || index == 0)
break;
index--;
}
}
bool initializedFromCast(const Initializer initializer)
{
import std.typecons : scoped;
static class CastFinder : ASTVisitor
{
alias visit = ASTVisitor.visit;
override void visit(const CastExpression castExpression)
{
foundCast = true;
castExpression.accept(this);
}
bool foundCast = false;
}
if (initializer is null)
return false;
auto finder = scoped!CastFinder();
finder.visit(initializer);
return finder.foundCast;
}
bool canFindImmutableOrConst(const Declaration dec)
{
import std.algorithm : canFind, map, filter;
return !dec.attributes.map!(a => a.attribute).filter!(
a => a == cast(IdType) tok!"immutable" || a == cast(IdType) tok!"const")
.empty;
}
bool canFindImmutable(const VariableDeclaration dec)
{
import std.algorithm : canFind;
foreach (storageClass; dec.storageClasses)
{
if (storageClass.token == tok!"enum")
return true;
}
foreach (attr; dec.attributes)
{
if (attr.attribute.type == tok!"immutable" || attr.attribute.type == tok!"const")
return true;
}
if (dec.type !is null)
{
if (dec.type.typeConstructors.canFind(cast(IdType) tok!"immutable"))
return true;
}
return false;
}
static struct VariableInfo
{
string name;
size_t line;
size_t column;
bool isValueType;
}
void popScope()
{
foreach (vi; tree[$ - 1])
{
immutable string errorMessage = "Variable " ~ vi.name
~ " is never modified and could have been declared const"
~ " or immutable.";
addErrorMessage(vi.line, vi.column, "dscanner.suspicious.unmodified",
errorMessage);
}
tree = tree[0 .. $ - 1];
}
void pushScope()
{
tree ~= new RedBlackTree!(VariableInfo*, "a.name < b.name");
}
int blockStatementDepth;
int interest;
int guaranteeUse;
int isImmutable;
bool inAsm;
RedBlackTree!(VariableInfo*, "a.name < b.name")[] tree;
}
bool isValueTypeSimple(const Type type) pure nothrow @nogc
{
if (type.type2 is null)
return false;
return type.type2.builtinType != tok!"" && type.typeSuffixes.length == 0;
}