2012-01-19 02:50:51 -06:00
|
|
|
import driver::session::session;
|
2012-04-12 19:30:52 -05:00
|
|
|
import middle::ty;
|
2012-01-19 02:50:51 -06:00
|
|
|
import syntax::{ast, visit};
|
2012-03-22 20:16:22 -05:00
|
|
|
import syntax::attr;
|
2012-04-12 19:30:52 -05:00
|
|
|
import syntax::codemap::span;
|
|
|
|
import std::map::{map,hashmap,hash_from_strs};
|
2012-01-21 11:20:22 -06:00
|
|
|
import io::writer_util;
|
2012-01-19 02:50:51 -06:00
|
|
|
|
2012-04-12 19:30:52 -05:00
|
|
|
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 miscallaneous 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
|
|
|
|
correctly at all.
|
|
|
|
|
|
|
|
"]
|
|
|
|
|
|
|
|
enum lint {
|
2012-01-19 19:56:05 -06:00
|
|
|
ctypes,
|
2012-04-12 19:30:52 -05:00
|
|
|
unused_imports,
|
2012-01-19 02:50:51 -06:00
|
|
|
}
|
|
|
|
|
2012-04-12 19:30:52 -05:00
|
|
|
enum level {
|
|
|
|
ignore, warn, error
|
2012-01-21 11:20:22 -06:00
|
|
|
}
|
|
|
|
|
2012-04-12 19:30:52 -05:00
|
|
|
type lint_spec = @{lint: lint,
|
|
|
|
desc: str,
|
|
|
|
default: level};
|
|
|
|
|
|
|
|
type lint_dict = hashmap<str,lint_spec>;
|
|
|
|
|
|
|
|
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})
|
|
|
|
];
|
|
|
|
hash_from_strs(v)
|
2012-01-21 11:20:22 -06:00
|
|
|
}
|
|
|
|
|
2012-04-12 19:30:52 -05:00
|
|
|
type ctxt = @{dict: lint_dict,
|
|
|
|
curr: hashmap<lint, level>,
|
|
|
|
tcx: ty::ctxt};
|
2012-01-21 11:20:22 -06:00
|
|
|
|
2012-04-12 19:30:52 -05:00
|
|
|
impl methods for ctxt {
|
|
|
|
fn get_level(lint: lint) -> level {
|
|
|
|
alt self.curr.find(lint) {
|
|
|
|
some(c) { c }
|
|
|
|
none { ignore }
|
|
|
|
}
|
2012-01-21 11:20:22 -06:00
|
|
|
}
|
|
|
|
|
2012-04-12 19:30:52 -05:00
|
|
|
fn set_level(lint: lint, level: level) {
|
|
|
|
if level == ignore {
|
|
|
|
self.curr.remove(lint);
|
|
|
|
} else {
|
|
|
|
self.curr.insert(lint, level);
|
|
|
|
}
|
2012-01-21 11:20:22 -06:00
|
|
|
}
|
|
|
|
|
2012-04-12 19:30:52 -05:00
|
|
|
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); }
|
2012-01-21 11:20:22 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-04-12 19:30:52 -05:00
|
|
|
#[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)) {
|
2012-01-21 11:20:22 -06:00
|
|
|
|
2012-04-12 19:30:52 -05:00
|
|
|
let mut undo = [];
|
2012-01-21 11:20:22 -06:00
|
|
|
|
2012-04-12 19:30:52 -05:00
|
|
|
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");
|
|
|
|
}
|
|
|
|
}
|
2012-01-21 11:20:22 -06:00
|
|
|
}
|
2012-04-12 19:30:52 -05:00
|
|
|
}
|
|
|
|
_ {
|
|
|
|
self.tcx.sess.span_err(meta.span,
|
|
|
|
"malformed warning attribute");
|
|
|
|
}
|
2012-01-21 11:20:22 -06:00
|
|
|
}
|
|
|
|
}
|
2012-04-12 19:30:52 -05:00
|
|
|
|
|
|
|
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)
|
2012-01-21 11:20:22 -06:00
|
|
|
};
|
2012-04-12 19:30:52 -05:00
|
|
|
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));
|
|
|
|
}
|
2012-01-21 11:20:22 -06:00
|
|
|
|
2012-04-12 19:30:52 -05:00
|
|
|
fn check_item(cx: ctxt, i: @ast::item) {
|
|
|
|
cx.with_warn_attrs(i.attrs) {|cx|
|
|
|
|
cx.curr.items {|lint, level|
|
|
|
|
alt lint {
|
|
|
|
ctypes { check_item_ctypes(cx, level, i); }
|
|
|
|
unused_imports { check_item_unused_imports(cx, level, i); }
|
|
|
|
}
|
2012-01-21 11:20:22 -06:00
|
|
|
}
|
|
|
|
}
|
2012-04-12 19:30:52 -05:00
|
|
|
}
|
2012-01-21 11:20:22 -06:00
|
|
|
|
2012-04-12 19:30:52 -05:00
|
|
|
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.
|
2012-01-21 11:20:22 -06:00
|
|
|
}
|
|
|
|
|
2012-04-12 19:30:52 -05:00
|
|
|
fn check_item_ctypes(cx: ctxt, level: level, it: @ast::item) {
|
|
|
|
|
|
|
|
fn check_native_fn(cx: ctxt, level: level, decl: ast::fn_decl) {
|
2012-01-19 02:50:51 -06:00
|
|
|
let tys = vec::map(decl.inputs) {|a| a.ty };
|
2012-04-06 13:01:43 -05:00
|
|
|
for vec::each(tys + [decl.output]) {|ty|
|
2012-01-19 02:50:51 -06:00
|
|
|
alt ty.node {
|
2012-02-06 08:29:56 -06:00
|
|
|
ast::ty_path(_, id) {
|
2012-04-12 19:30:52 -05:00
|
|
|
alt cx.tcx.def_map.get(id) {
|
2012-02-06 08:29:56 -06:00
|
|
|
ast::def_prim_ty(ast::ty_int(ast::ty_i)) {
|
2012-04-12 19:30:52 -05:00
|
|
|
cx.span_lint(
|
|
|
|
level, ty.span,
|
2012-02-06 08:29:56 -06:00
|
|
|
"found rust type `int` in native module, while \
|
2012-03-12 22:04:27 -05:00
|
|
|
libc::c_int or libc::c_long should be used");
|
2012-02-06 08:29:56 -06:00
|
|
|
}
|
|
|
|
ast::def_prim_ty(ast::ty_uint(ast::ty_u)) {
|
2012-04-12 19:30:52 -05:00
|
|
|
cx.span_lint(
|
|
|
|
level, ty.span,
|
2012-02-06 08:29:56 -06:00
|
|
|
"found rust type `uint` in native module, while \
|
2012-03-12 22:04:27 -05:00
|
|
|
libc::c_uint or libc::c_ulong should be used");
|
2012-02-06 08:29:56 -06:00
|
|
|
}
|
|
|
|
_ { }
|
|
|
|
}
|
2012-01-19 02:50:51 -06:00
|
|
|
}
|
|
|
|
_ { }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-04-12 19:30:52 -05:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
_ { }
|
2012-01-19 02:50:51 -06:00
|
|
|
}
|
|
|
|
}
|
2012-04-12 19:30:52 -05:00
|
|
|
}
|
|
|
|
_ {/* nothing to do */ }
|
2012-01-19 02:50:51 -06:00
|
|
|
}
|
|
|
|
}
|
2012-01-21 11:20:22 -06:00
|
|
|
|
2012-04-12 19:30:52 -05:00
|
|
|
|
2012-01-21 11:20:22 -06:00
|
|
|
fn check_crate(tcx: ty::ctxt, crate: @ast::crate,
|
2012-04-12 19:30:52 -05:00
|
|
|
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.
|
|
|
|
cx.dict.items {|_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);
|
2012-01-21 11:20:22 -06:00
|
|
|
}
|
|
|
|
}
|
2012-04-12 19:30:52 -05:00
|
|
|
|
|
|
|
tcx.sess.abort_if_errors();
|
2012-01-21 11:20:22 -06:00
|
|
|
}
|
2012-04-12 19:30:52 -05:00
|
|
|
|
2012-01-19 02:50:51 -06:00
|
|
|
//
|
|
|
|
// Local Variables:
|
|
|
|
// mode: rust
|
|
|
|
// fill-column: 78;
|
|
|
|
// indent-tabs-mode: nil
|
|
|
|
// c-basic-offset: 4
|
|
|
|
// buffer-file-coding-system: utf-8-unix
|
|
|
|
// End:
|
|
|
|
//
|