Skip to content

Commit 327a15d

Browse files
committed
rustc: Add a usage pass to collect one-off analyses
This patch starts from move the analysis which checkes of probably incorrectly usage of `int|uint` in native fn. Issue #1543
1 parent 45c0651 commit 327a15d

File tree

8 files changed

+75
-31
lines changed

8 files changed

+75
-31
lines changed

man/rustc.1

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,9 @@ Build a test harness.
123123
.TP
124124
\fB--warn-unused-imports\fR:
125125
Warn about unnecessary imports.
126+
.TP
127+
\fB--no-check-usage\fR:
128+
Disables various one-off usage analyses.
126129
.SH "BUGS"
127130
See \fBhttps://github.com/mozilla/rust/issues\fR for a list of known bugs.
128131
.SH "AUTHOR"

src/comp/driver/driver.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import syntax::parse::{parser};
66
import syntax::{ast, codemap};
77
import front::attr;
88
import middle::{trans, resolve, freevars, kind, ty, typeck, fn_usage,
9-
last_use};
9+
last_use, check_usage};
1010
import syntax::print::{pp, pprust};
1111
import util::{ppaux, filesearch};
1212
import back::link;
@@ -203,6 +203,10 @@ fn compile_upto(sess: session, cfg: ast::crate_cfg,
203203
bind last_use::find_last_uses(crate, def_map, ref_map, ty_cx));
204204
time(time_passes, "kind checking",
205205
bind kind::check_crate(ty_cx, method_map, last_uses, crate));
206+
if sess.opts.check_usage {
207+
time(time_passes, "usage analyses",
208+
bind check_usage::check_crate(ty_cx, crate));
209+
}
206210

207211
if upto == cu_no_trans { ret {crate: crate, tcx: some(ty_cx), src: src}; }
208212
let outputs = option::get(outputs);
@@ -395,6 +399,7 @@ fn build_session_options(match: getopts::match,
395399
} else { link::output_type_exe };
396400
let libcore = !opt_present(match, "no-core");
397401
let verify = !opt_present(match, "no-verify");
402+
let check_usage = !opt_present(match, "no-usage-check");
398403
let save_temps = opt_present(match, "save-temps");
399404
let extra_debuginfo = opt_present(match, "xg");
400405
let debuginfo = opt_present(match, "g") || extra_debuginfo;
@@ -446,6 +451,7 @@ fn build_session_options(match: getopts::match,
446451
debuginfo: debuginfo,
447452
extra_debuginfo: extra_debuginfo,
448453
verify: verify,
454+
check_usage: check_usage,
449455
save_temps: save_temps,
450456
stats: stats,
451457
time_passes: time_passes,
@@ -514,6 +520,7 @@ fn opts() -> [getopts::opt] {
514520
optopt("sysroot"), optopt("target"), optflag("stats"),
515521
optflag("time-passes"), optflag("time-llvm-passes"),
516522
optflag("no-verify"),
523+
optflag("no-usage-check"),
517524
optmulti("cfg"), optflag("test"),
518525
optflag("no-core"),
519526
optflag("lib"), optflag("bin"), optflag("static"), optflag("gc"),

src/comp/driver/rustc.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ options:
3838
--ls list the symbols defined by a crate file
3939
-L <path> add a directory to the library search path
4040
--no-verify suppress LLVM verification step (slight speedup)
41+
--no-check-usage suppress various one-off usage analyses
4142
--parse-only parse only; do not compile, assemble, or link
4243
--no-trans run all passes except translation; no output
4344
-g produce debug info

src/comp/driver/session.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ type options =
3333
debuginfo: bool,
3434
extra_debuginfo: bool,
3535
verify: bool,
36+
check_usage: bool,
3637
save_temps: bool,
3738
stats: bool,
3839
time_passes: bool,

src/comp/middle/check_usage.rs

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
import driver::session::session;
2+
import middle::ty::ctxt;
3+
import syntax::{ast, visit};
4+
5+
type crate_ctxt = {tcx: ty::ctxt};
6+
7+
fn check_native_fn(ccx: @crate_ctxt, decl: ast::fn_decl) {
8+
let tys = vec::map(decl.inputs) {|a| a.ty };
9+
for ty in (tys + [decl.output]) {
10+
alt ty.node {
11+
ast::ty_int(ast::ty_i.) {
12+
ccx.tcx.sess.span_warn(
13+
ty.span, "found rust type `int` in native module, while " +
14+
"ctypes::c_int or ctypes::long should be used");
15+
}
16+
ast::ty_uint(ast::ty_u.) {
17+
ccx.tcx.sess.span_warn(
18+
ty.span, "found rust type `uint` in native module, while " +
19+
"ctypes::c_uint or ctypes::ulong should be used");
20+
}
21+
_ { }
22+
}
23+
}
24+
}
25+
26+
fn check_item(ccx: @crate_ctxt, it: @ast::item) {
27+
alt it.node {
28+
ast::item_native_mod(nmod) {
29+
for ni in nmod.items {
30+
alt ni.node {
31+
ast::native_item_fn(decl, tps) {
32+
check_native_fn(ccx, decl);
33+
}
34+
_ { }
35+
}
36+
}
37+
}
38+
_ {/* nothing to do */ }
39+
}
40+
}
41+
42+
fn check_crate(tcx: ty::ctxt, crate: @ast::crate) {
43+
let ccx = @{tcx: tcx};
44+
let visit = visit::mk_simple_visitor(@{
45+
visit_item: bind check_item(ccx, _)
46+
with *visit::default_simple_visitor()
47+
});
48+
visit::visit_crate(*crate, (), visit);
49+
tcx.sess.abort_if_errors();
50+
}
51+
//
52+
// Local Variables:
53+
// mode: rust
54+
// fill-column: 78;
55+
// indent-tabs-mode: nil
56+
// c-basic-offset: 4
57+
// buffer-file-coding-system: utf-8-unix
58+
// End:
59+
//

src/comp/middle/typeck.rs

Lines changed: 0 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -2680,25 +2680,6 @@ fn check_method(ccx: @crate_ctxt, method: @ast::method) {
26802680
check_fn(ccx, ast::proto_bare, method.decl, method.body, method.id, none);
26812681
}
26822682

2683-
fn check_native_fn(ccx: @crate_ctxt, decl: ast::fn_decl) {
2684-
let tys = vec::map(decl.inputs) {|a| a.ty };
2685-
for ty in (tys + [decl.output]) {
2686-
alt ty.node {
2687-
ast::ty_int(ast::ty_i) {
2688-
ccx.tcx.sess.span_warn(
2689-
ty.span, "found rust type `int` in native module, while " +
2690-
"ctypes::c_int or ctypes::long should be used");
2691-
}
2692-
ast::ty_uint(ast::ty_u) {
2693-
ccx.tcx.sess.span_warn(
2694-
ty.span, "found rust type `uint` in native module, while " +
2695-
"ctypes::c_uint or ctypes::ulong should be used");
2696-
}
2697-
_ { }
2698-
}
2699-
}
2700-
}
2701-
27022683
fn check_item(ccx: @crate_ctxt, it: @ast::item) {
27032684
alt it.node {
27042685
ast::item_const(_, e) { check_const(ccx, it.span, e, it.id); }
@@ -2709,16 +2690,6 @@ fn check_item(ccx: @crate_ctxt, it: @ast::item) {
27092690
ast::item_res(decl, tps, body, dtor_id, _) {
27102691
check_fn(ccx, ast::proto_bare, decl, body, dtor_id, none);
27112692
}
2712-
ast::item_native_mod(nmod) {
2713-
for ni in nmod.items {
2714-
alt ni.node {
2715-
ast::native_item_fn(decl, tps) {
2716-
check_native_fn(ccx, decl);
2717-
}
2718-
_ { }
2719-
}
2720-
}
2721-
}
27222693
ast::item_impl(tps, _, ty, ms) {
27232694
ccx.self_infos += [self_impl(ast_ty_to_ty(ccx.tcx, m_check, ty))];
27242695
for m in ms { check_method(ccx, m); }

src/comp/rustc.rc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ mod middle {
2929
mod fn_usage;
3030
mod check_alt;
3131
mod check_const;
32+
mod check_usage;
3233
mod mut;
3334
mod alias;
3435
mod last_use;

src/test/compile-fail/warn-native-int-types.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,6 @@ native mod xx {
55
}
66

77
fn main() {
8-
"let compile fail to verify warning message" = 999;
8+
// let it fail to verify warning message
9+
fail
910
}

0 commit comments

Comments
 (0)