From 7ffb2cb7e8d3f1845455a51f83fde3fd4759790c Mon Sep 17 00:00:00 2001 From: Haitao Li Date: Thu, 19 Jan 2012 16:50:51 +0800 Subject: [PATCH] rustc: Name the lint-style check module `lint` Issue #1543 --- man/rustc.1 | 6 ++- src/comp/driver/driver.rs | 17 ++++---- src/comp/driver/rustc.rs | 2 +- src/comp/driver/session.rs | 3 +- src/comp/middle/check_usage.rs | 59 -------------------------- src/comp/middle/lint.rs | 75 ++++++++++++++++++++++++++++++++++ src/comp/rustc.rc | 2 +- 7 files changed, 93 insertions(+), 71 deletions(-) delete mode 100644 src/comp/middle/check_usage.rs create mode 100644 src/comp/middle/lint.rs diff --git a/man/rustc.1 b/man/rustc.1 index 20f24a1307a..741f9a2cba8 100644 --- a/man/rustc.1 +++ b/man/rustc.1 @@ -124,8 +124,10 @@ Build a test harness. \fB--warn-unused-imports\fR: Warn about unnecessary imports. .TP -\fB--no-check-usage\fR: -Disables various one-off usage analyses. +\fB--no-lint-ctypes\fR: +Disables checking of possibly incorrect usage of Rust int or uint types in +native function declarations, where types defined in libcore::ctypes should be +used instead. Ctypes check emits warnings by default. .SH "BUGS" See \fBhttps://github.com/mozilla/rust/issues\fR for a list of known bugs. .SH "AUTHOR" diff --git a/src/comp/driver/driver.rs b/src/comp/driver/driver.rs index 54ca5db4294..6c2a646f35d 100644 --- a/src/comp/driver/driver.rs +++ b/src/comp/driver/driver.rs @@ -6,7 +6,7 @@ import syntax::parse::{parser}; import syntax::{ast, codemap}; import front::attr; import middle::{trans, resolve, freevars, kind, ty, typeck, fn_usage, - last_use, check_usage}; + last_use, lint}; import syntax::print::{pp, pprust}; import util::{ppaux, filesearch}; import back::link; @@ -203,9 +203,9 @@ fn compile_upto(sess: session, cfg: ast::crate_cfg, bind last_use::find_last_uses(crate, def_map, ref_map, ty_cx)); time(time_passes, "kind checking", bind kind::check_crate(ty_cx, method_map, last_uses, crate)); - if sess.opts.check_usage { - time(time_passes, "usage analyses", - bind check_usage::check_crate(ty_cx, crate)); + if vec::len(sess.opts.lint_opts) > 0u { + let timer = bind time(time_passes, _, _); + lint::check_crate(ty_cx, crate, sess.opts.lint_opts, timer) } if upto == cu_no_trans { ret {crate: crate, tcx: some(ty_cx), src: src}; } @@ -384,6 +384,10 @@ fn build_session_options(match: getopts::match, let parse_only = opt_present(match, "parse-only"); let no_trans = opt_present(match, "no-trans"); + let lint_opts : [lint::option] = []; + if !opt_present(match, "no-lint-ctypes") { + lint_opts += [lint::ctypes]; + } let output_type = if parse_only || no_trans { @@ -399,7 +403,6 @@ fn build_session_options(match: getopts::match, } else { link::output_type_exe }; let libcore = !opt_present(match, "no-core"); let verify = !opt_present(match, "no-verify"); - let check_usage = !opt_present(match, "no-usage-check"); let save_temps = opt_present(match, "save-temps"); let extra_debuginfo = opt_present(match, "xg"); let debuginfo = opt_present(match, "g") || extra_debuginfo; @@ -451,7 +454,7 @@ fn build_session_options(match: getopts::match, debuginfo: debuginfo, extra_debuginfo: extra_debuginfo, verify: verify, - check_usage: check_usage, + lint_opts: lint_opts, save_temps: save_temps, stats: stats, time_passes: time_passes, @@ -520,7 +523,7 @@ fn opts() -> [getopts::opt] { optopt("sysroot"), optopt("target"), optflag("stats"), optflag("time-passes"), optflag("time-llvm-passes"), optflag("no-verify"), - optflag("no-usage-check"), + optflag("no-lint-ctypes"), optmulti("cfg"), optflag("test"), optflag("no-core"), optflag("lib"), optflag("bin"), optflag("static"), optflag("gc"), diff --git a/src/comp/driver/rustc.rs b/src/comp/driver/rustc.rs index fcd5f1852ae..8ca9a3e6f72 100644 --- a/src/comp/driver/rustc.rs +++ b/src/comp/driver/rustc.rs @@ -38,7 +38,6 @@ options: --ls list the symbols defined by a crate file -L add a directory to the library search path --no-verify suppress LLVM verification step (slight speedup) - --no-check-usage suppress various one-off usage analyses --parse-only parse only; do not compile, assemble, or link --no-trans run all passes except translation; no output -g produce debug info @@ -59,6 +58,7 @@ options: --gc garbage collect shared data (experimental/temporary) --warn-unused-imports warn about unnecessary imports + --no-lint-ctypes suppress lint-style ctypes usage check "); } diff --git a/src/comp/driver/session.rs b/src/comp/driver/session.rs index 4b7154a5a99..67a8268f5fd 100644 --- a/src/comp/driver/session.rs +++ b/src/comp/driver/session.rs @@ -8,6 +8,7 @@ import option::{some, none}; import syntax::parse::parser::parse_sess; import util::filesearch; import back::target_strs; +import middle::lint; tag os { os_win32; os_macos; os_linux; os_freebsd; } @@ -33,7 +34,7 @@ type options = debuginfo: bool, extra_debuginfo: bool, verify: bool, - check_usage: bool, + lint_opts: [lint::option], save_temps: bool, stats: bool, time_passes: bool, diff --git a/src/comp/middle/check_usage.rs b/src/comp/middle/check_usage.rs deleted file mode 100644 index 227e563ed7c..00000000000 --- a/src/comp/middle/check_usage.rs +++ /dev/null @@ -1,59 +0,0 @@ -import driver::session::session; -import middle::ty::ctxt; -import syntax::{ast, visit}; - -type crate_ctxt = {tcx: ty::ctxt}; - -fn check_native_fn(ccx: @crate_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_int(ast::ty_i.) { - ccx.tcx.sess.span_warn( - ty.span, "found rust type `int` in native module, while " + - "ctypes::c_int or ctypes::long should be used"); - } - ast::ty_uint(ast::ty_u.) { - ccx.tcx.sess.span_warn( - ty.span, "found rust type `uint` in native module, while " + - "ctypes::c_uint or ctypes::ulong should be used"); - } - _ { } - } - } -} - -fn check_item(ccx: @crate_ctxt, it: @ast::item) { - alt it.node { - ast::item_native_mod(nmod) { - for ni in nmod.items { - alt ni.node { - ast::native_item_fn(decl, tps) { - check_native_fn(ccx, decl); - } - _ { } - } - } - } - _ {/* nothing to do */ } - } -} - -fn check_crate(tcx: ty::ctxt, crate: @ast::crate) { - let ccx = @{tcx: tcx}; - let visit = visit::mk_simple_visitor(@{ - visit_item: bind check_item(ccx, _) - 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: -// diff --git a/src/comp/middle/lint.rs b/src/comp/middle/lint.rs new file mode 100644 index 00000000000..1b19fd60066 --- /dev/null +++ b/src/comp/middle/lint.rs @@ -0,0 +1,75 @@ +import driver::session::session; +import middle::ty::ctxt; +import syntax::{ast, visit}; + +type crate_ctxt = {tcx: ty::ctxt}; + +enum option { + ctypes; +} + +fn check_crate(tcx: ty::ctxt, crate: @ast::crate, + checks: [option], timer: block(str, fn@())) { + let ccx = @{tcx: tcx}; + vec::iter(checks) {|c| + alt c { + ctypes { + timer("ctypes usage checking", bind check_ctypes(ccx, crate)) + } + } + } +} + +fn check_ctypes(ccx: @crate_ctxt, crate: @ast::crate) { + fn check_native_fn(ccx: @crate_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_int(ast::ty_i) { + ccx.tcx.sess.span_warn( + ty.span, + "found rust type `int` in native module, while \ + ctypes::c_int or ctypes::long should be used"); + } + ast::ty_uint(ast::ty_u) { + ccx.tcx.sess.span_warn( + ty.span, + "found rust type `uint` in native module, while \ + ctypes::c_uint or ctypes::ulong should be used"); + } + _ { } + } + } + } + + fn check_item(ccx: @crate_ctxt, it: @ast::item) { + alt it.node { + ast::item_native_mod(nmod) { + for ni in nmod.items { + alt ni.node { + ast::native_item_fn(decl, tps) { + check_native_fn(ccx, decl); + } + _ { } + } + } + } + _ {/* nothing to do */ } + } + } + + let visit = visit::mk_simple_visitor(@{ + visit_item: bind check_item(ccx, _) + with *visit::default_simple_visitor() + }); + visit::visit_crate(*crate, (), visit); +} +// +// Local Variables: +// mode: rust +// fill-column: 78; +// indent-tabs-mode: nil +// c-basic-offset: 4 +// buffer-file-coding-system: utf-8-unix +// End: +// diff --git a/src/comp/rustc.rc b/src/comp/rustc.rc index f6405b963ba..20364cc5369 100644 --- a/src/comp/rustc.rc +++ b/src/comp/rustc.rc @@ -29,7 +29,7 @@ mod middle { mod fn_usage; mod check_alt; mod check_const; - mod check_usage; + mod lint; mod mut; mod alias; mod last_use;