/
lint.rs
297 lines (258 loc) · 8.54 KB
/
lint.rs
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
import driver::session::session;
import middle::ty;
import syntax::{ast, visit};
import syntax::attr;
import syntax::codemap::span;
import std::map::{map,hashmap,hash_from_strs};
import io::writer_util;
import syntax::print::pprust::expr_to_str;
export lint, ctypes, unused_imports;
export level, ignore, warn, error;
export lookup_lint, lint_dict, get_lint_dict, check_crate;
#[doc="
A 'lint' check is a kind of miscellaneous constraint that a user _might_ want
to enforce, but might reasonably want to permit as well, on a module-by-module
basis. They contrast with static constraints enforced by other phases of the
compiler, which are generally required to hold in order to compile the program
at all.
"]
enum lint {
ctypes,
unused_imports,
while_true
}
enum level {
ignore, warn, error
}
type lint_spec = @{lint: lint,
desc: str,
default: level};
type lint_dict = hashmap<str,lint_spec>;
/*
Pass names should not contain a '-', as the compiler normalizes
'-' to '_' in command-line flags
*/
fn get_lint_dict() -> lint_dict {
let v = [
("ctypes",
@{lint: ctypes,
desc: "proper use of core::libc types in native modules",
default: warn}),
("unused_imports",
@{lint: unused_imports,
desc: "imports that are never used",
default: ignore}),
("while_true",
@{lint: while_true,
desc: "suggest using loop { } instead of while(true) { }",
default: warn})
];
hash_from_strs(v)
}
type ctxt = @{dict: lint_dict,
curr: hashmap<lint, level>,
tcx: ty::ctxt};
impl methods for ctxt {
fn get_level(lint: lint) -> level {
alt self.curr.find(lint) {
some(c) { c }
none { ignore }
}
}
fn set_level(lint: lint, level: level) {
if level == ignore {
self.curr.remove(lint);
} else {
self.curr.insert(lint, level);
}
}
fn span_lint(level: level, span: span, msg: str) {
alt level {
ignore { }
warn { self.tcx.sess.span_warn(span, msg); }
error { self.tcx.sess.span_err(span, msg); }
}
}
#[doc="
Merge the warnings specified by any `warn(...)` attributes into the
current lint context, call the provided function, then reset the
warnings in effect to their previous state.
"]
fn with_warn_attrs(attrs: [ast::attribute], f: fn(ctxt)) {
let mut undo = [];
let metas = attr::attr_metas(attr::find_attrs_by_name(attrs, "warn"));
for metas.each {|meta|
alt meta.node {
ast::meta_list(_, metas) {
for metas.each {|meta|
alt meta.node {
ast::meta_word(lintname) {
alt lookup_lint(self.dict, lintname) {
none {
self.tcx.sess.span_err(
meta.span,
#fmt("unknown warning: '%s'", lintname));
}
some((lint, new_level)) {
let old_level = self.get_level(lint);
self.set_level(lint, new_level);
undo += [(lint, old_level)]
}
}
}
_ {
self.tcx.sess.span_err(
meta.span,
"malformed warning attribute");
}
}
}
}
_ {
self.tcx.sess.span_err(meta.span,
"malformed warning attribute");
}
}
}
f(self);
for undo.each {|pair|
let (lint,old_level) = pair;
self.set_level(lint, old_level);
}
}
}
fn lookup_lint(dict: lint_dict, s: str)
-> option<(lint, level)> {
let s = str::replace(s, "-", "_");
let (name, level) = if s.starts_with("no_") {
(s.substr(3u, s.len() - 3u), ignore)
} else if s.starts_with("err_") {
(s.substr(4u, s.len() - 4u), error)
} else {
(s, warn)
};
alt dict.find(name) {
none { none }
some(spec) { some((spec.lint, level)) }
}
}
// FIXME: Copied from driver.rs, to work around a bug(#1566)
fn time(do_it: bool, what: str, thunk: fn()) {
if !do_it{ ret thunk(); }
let start = std::time::precise_time_s();
thunk();
let end = std::time::precise_time_s();
io::stdout().write_str(#fmt("time: %3.3f s\t%s\n",
end - start, what));
}
fn check_item(cx: ctxt, i: @ast::item) {
cx.with_warn_attrs(i.attrs) {|cx|
for cx.curr.each {|lint, level|
alt lint {
ctypes { check_item_ctypes(cx, level, i); }
unused_imports { check_item_unused_imports(cx, level, i); }
while_true { check_item_while_true(cx, level, i); }
}
}
}
}
fn check_item_while_true(cx: ctxt, level: level, it: @ast::item) {
let visit = visit::mk_simple_visitor(@{
visit_expr: fn@(e: @ast::expr) {
alt e.node {
ast::expr_while(cond, _) {
alt cond.node {
ast::expr_lit(@{node: ast::lit_bool(true),_}) {
cx.span_lint(
level, e.span,
"Denote infinite loops with loop { ... }");
}
_ {}
}
}
_ {}
}
}
with *visit::default_simple_visitor()
});
visit::visit_item(it, (), visit);
}
fn check_item_unused_imports(_cx: ctxt, _level: level, _it: @ast::item) {
// FIXME: Don't know how to check this in lint yet, it's currently being
// done over in resolve. When resolve is rewritten, do it here instead.
}
fn check_item_ctypes(cx: ctxt, level: level, it: @ast::item) {
fn check_native_fn(cx: ctxt, level: level, decl: ast::fn_decl) {
let tys = vec::map(decl.inputs) {|a| a.ty };
for vec::each(tys + [decl.output]) {|ty|
alt ty.node {
ast::ty_path(_, id) {
alt cx.tcx.def_map.get(id) {
ast::def_prim_ty(ast::ty_int(ast::ty_i)) {
cx.span_lint(
level, ty.span,
"found rust type `int` in native module, while \
libc::c_int or libc::c_long should be used");
}
ast::def_prim_ty(ast::ty_uint(ast::ty_u)) {
cx.span_lint(
level, ty.span,
"found rust type `uint` in native module, while \
libc::c_uint or libc::c_ulong should be used");
}
_ { }
}
}
_ { }
}
}
}
alt it.node {
ast::item_native_mod(nmod) if attr::native_abi(it.attrs) !=
either::right(ast::native_abi_rust_intrinsic) {
for nmod.items.each {|ni|
alt ni.node {
ast::native_item_fn(decl, tps) {
check_native_fn(cx, level, decl);
}
_ { }
}
}
}
_ {/* nothing to do */ }
}
}
fn check_crate(tcx: ty::ctxt, crate: @ast::crate,
lint_opts: [(lint, level)], time_pass: bool) {
fn hash_lint(&&lint: lint) -> uint { lint as uint }
fn eq_lint(&&a: lint, &&b: lint) -> bool { a == b }
let cx = @{dict: get_lint_dict(),
curr: hashmap(hash_lint, eq_lint),
tcx: tcx};
// Install defaults.
for cx.dict.each {|_k, spec| cx.set_level(spec.lint, spec.default); }
// Install command-line options, overriding defaults.
for lint_opts.each {|pair|
let (lint,level) = pair;
cx.set_level(lint, level);
}
time(time_pass, "lint checking") {||
cx.with_warn_attrs(crate.node.attrs) {|cx|
let visit = visit::mk_simple_visitor(@{
visit_item: fn@(i: @ast::item) { check_item(cx, i); }
with *visit::default_simple_visitor()
});
visit::visit_crate(*crate, (), visit);
}
}
tcx.sess.abort_if_errors();
}
//
// Local Variables:
// mode: rust
// fill-column: 78;
// indent-tabs-mode: nil
// c-basic-offset: 4
// buffer-file-coding-system: utf-8-unix
// End:
//