summary refs log tree commit diff
path: root/src/rustc/middle/lint.rs
blob: 3ccbaed873c0f4c7ddaaf4698df9b2719c52f755 (plain)
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
import driver::session::session;
import middle::ty::ctxt;
import syntax::{ast, visit};
import front::attr;
import std::map::hashmap;
import io::writer_util;

enum option {
    ctypes,
}

impl opt_ for option {
    fn desc() -> str {
        "lint: " + alt self {
          ctypes { "ctypes usage checking" }
        }
    }
    fn run(tcx: ty::ctxt, crate: @ast::crate, time_pass: bool) {
        let checker = alt self {
          ctypes {
            bind check_ctypes(tcx, crate)
          }
        };
        time(time_pass, self.desc(), checker);
    }
}

// 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));
}

// Merge lint options specified by crate attributes and rustc command
// line. Precedence: cmdline > attribute > default
fn merge_opts(attrs: [ast::attribute], cmd_opts: [(option, bool)]) ->
    [(option, bool)] {
    fn str_to_option(name: str) -> (option, bool) {
        ret alt check name {
          "ctypes" { (ctypes, true) }
          "no_ctypes" { (ctypes, false) }
        }
    }

    fn meta_to_option(meta: @ast::meta_item) -> (option, bool) {
        ret alt meta.node {
          ast::meta_word(name) {
            str_to_option(name)
          }
          _ { fail "meta_to_option: meta_list contains a non-meta-word"; }
        };
    }

    fn default() -> [(option, bool)] {
        [(ctypes, true)]
    }

    fn contains(xs: [(option, bool)], x: option) -> bool {
        for (o, _) in xs {
            if o == x { ret true; }
        }
        ret false;
    }

    let mut result = cmd_opts;

    let lint_metas =
        attr::attr_metas(attr::find_attrs_by_name(attrs, "lint"));

    vec::iter(lint_metas) {|mi|
        alt mi.node {
          ast::meta_list(_, list) {
            vec::iter(list) {|e|
                let (o, v) = meta_to_option(e);
                if !contains(cmd_opts, o) {
                    result += [(o, v)];
                }
            }
          }
          _ { }
        }
    };

    for (o, v) in default() {
        if !contains(result, o) {
            result += [(o, v)];
        }
    }

    ret result;
}

fn check_ctypes(tcx: ty::ctxt, crate: @ast::crate) {
    fn check_native_fn(tcx: ty::ctxt, decl: ast::fn_decl) {
        let tys = vec::map(decl.inputs) {|a| a.ty };
        for ty in (tys + [decl.output]) {
            alt ty.node {
              ast::ty_path(_, id) {
                alt tcx.def_map.get(id) {
                  ast::def_prim_ty(ast::ty_int(ast::ty_i)) {
                    tcx.sess.span_warn(
                        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)) {
                    tcx.sess.span_warn(
                        ty.span,
                        "found rust type `uint` in native module, while \
                         libc::c_uint or libc::c_ulong should be used");
                  }
                  _ { }
                }
              }
              _ { }
            }
        }
    }

    fn check_item(tcx: ty::ctxt, it: @ast::item) {
        alt it.node {
          ast::item_native_mod(nmod) if attr::native_abi(it.attrs) !=
              either::right(ast::native_abi_rust_intrinsic) {
            for ni in nmod.items {
                alt ni.node {
                  ast::native_item_fn(decl, tps) {
                    check_native_fn(tcx, decl);
                  }
                  _ { }
                }
            }
          }
          _ {/* nothing to do */ }
        }
    }

    let visit = visit::mk_simple_visitor(@{
        visit_item: bind check_item(tcx, _)
        with *visit::default_simple_visitor()
    });
    visit::visit_crate(*crate, (), visit);
}

fn check_crate(tcx: ty::ctxt, crate: @ast::crate,
               opts: [(option, bool)], time: bool) {
    let lint_opts = lint::merge_opts(crate.node.attrs, opts);
    for (lopt, switch) in lint_opts {
        if switch == true {
            lopt.run(tcx, crate, time);
        }
    }
}
//
// Local Variables:
// mode: rust
// fill-column: 78;
// indent-tabs-mode: nil
// c-basic-offset: 4
// buffer-file-coding-system: utf-8-unix
// End:
//