2014-11-27 08:57:47 -06:00
|
|
|
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
|
2012-12-03 18:48:01 -06:00
|
|
|
// file at the top-level directory of this distribution and at
|
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
|
2014-11-27 08:57:47 -06:00
|
|
|
//! The Rust compiler.
|
|
|
|
//!
|
|
|
|
//! # Note
|
|
|
|
//!
|
|
|
|
//! This API is completely unstable and subject to change.
|
|
|
|
|
|
|
|
#![crate_name = "rustc_driver"]
|
|
|
|
#![experimental]
|
|
|
|
#![crate_type = "dylib"]
|
|
|
|
#![crate_type = "rlib"]
|
|
|
|
#![doc(html_logo_url = "http://www.rust-lang.org/logos/rust-logo-128x128-blk-v2.png",
|
|
|
|
html_favicon_url = "http://www.rust-lang.org/favicon.ico",
|
|
|
|
html_root_url = "http://doc.rust-lang.org/nightly/")]
|
|
|
|
|
2015-01-06 11:24:46 -06:00
|
|
|
#![feature(quote)]
|
2014-11-27 08:57:47 -06:00
|
|
|
#![feature(slicing_syntax, unsafe_destructor)]
|
|
|
|
#![feature(rustc_diagnostic_macros)]
|
|
|
|
|
|
|
|
extern crate arena;
|
|
|
|
extern crate flate;
|
|
|
|
extern crate getopts;
|
|
|
|
extern crate graphviz;
|
|
|
|
extern crate libc;
|
|
|
|
extern crate rustc;
|
|
|
|
extern crate rustc_back;
|
2014-12-05 13:17:35 -06:00
|
|
|
extern crate rustc_borrowck;
|
2014-12-18 16:46:26 -06:00
|
|
|
extern crate rustc_resolve;
|
2014-11-27 08:57:47 -06:00
|
|
|
extern crate rustc_trans;
|
2014-12-05 13:17:35 -06:00
|
|
|
extern crate rustc_typeck;
|
2014-11-27 08:57:47 -06:00
|
|
|
extern crate serialize;
|
|
|
|
extern crate "rustc_llvm" as llvm;
|
2015-01-06 11:24:46 -06:00
|
|
|
#[macro_use] extern crate log;
|
|
|
|
#[macro_use] extern crate syntax;
|
2014-12-31 22:43:46 -06:00
|
|
|
|
2014-11-27 08:57:47 -06:00
|
|
|
pub use syntax::diagnostic;
|
2014-05-06 06:38:01 -05:00
|
|
|
|
2014-11-27 08:57:47 -06:00
|
|
|
use rustc_trans::back::link;
|
|
|
|
use rustc::session::{config, Session, build_session};
|
2014-12-15 18:03:39 -06:00
|
|
|
use rustc::session::config::{Input, PrintRequest};
|
2014-11-27 08:57:47 -06:00
|
|
|
use rustc::lint::Lint;
|
|
|
|
use rustc::lint;
|
|
|
|
use rustc::metadata;
|
2014-11-15 19:30:33 -06:00
|
|
|
use rustc::DIAGNOSTICS;
|
|
|
|
|
2014-12-22 11:04:23 -06:00
|
|
|
use std::cmp::Ordering::Equal;
|
2014-05-06 06:38:01 -05:00
|
|
|
use std::io;
|
2014-12-10 21:46:38 -06:00
|
|
|
use std::iter::repeat;
|
2014-05-06 06:38:01 -05:00
|
|
|
use std::os;
|
2014-12-23 13:53:35 -06:00
|
|
|
use std::sync::mpsc::channel;
|
2014-12-06 20:34:37 -06:00
|
|
|
use std::thread;
|
2014-05-06 06:38:01 -05:00
|
|
|
|
2014-11-27 08:57:47 -06:00
|
|
|
use rustc::session::early_error;
|
2014-11-15 19:30:33 -06:00
|
|
|
|
2014-05-06 06:38:01 -05:00
|
|
|
use syntax::ast;
|
|
|
|
use syntax::parse;
|
|
|
|
use syntax::diagnostic::Emitter;
|
2014-07-01 11:39:41 -05:00
|
|
|
use syntax::diagnostics;
|
2014-05-06 06:38:01 -05:00
|
|
|
|
2014-11-27 08:57:47 -06:00
|
|
|
#[cfg(test)]
|
|
|
|
pub mod test;
|
2014-05-06 06:38:01 -05:00
|
|
|
|
2013-01-29 17:16:07 -06:00
|
|
|
pub mod driver;
|
2014-08-11 05:59:35 -05:00
|
|
|
pub mod pretty;
|
2014-05-06 06:38:01 -05:00
|
|
|
|
2014-09-27 14:10:45 -05:00
|
|
|
pub fn run(args: Vec<String>) -> int {
|
2014-11-26 07:12:18 -06:00
|
|
|
monitor(move |:| run_compiler(args.as_slice()));
|
2014-05-06 06:38:01 -05:00
|
|
|
0
|
|
|
|
}
|
|
|
|
|
|
|
|
static BUG_REPORT_URL: &'static str =
|
2014-05-21 21:55:39 -05:00
|
|
|
"http://doc.rust-lang.org/complement-bugreport.html";
|
2014-05-06 06:38:01 -05:00
|
|
|
|
2014-05-22 18:57:53 -05:00
|
|
|
fn run_compiler(args: &[String]) {
|
2014-10-15 01:05:01 -05:00
|
|
|
let matches = match handle_options(args.to_vec()) {
|
2014-05-06 06:38:01 -05:00
|
|
|
Some(matches) => matches,
|
|
|
|
None => return
|
|
|
|
};
|
2014-06-04 16:35:58 -05:00
|
|
|
|
2014-11-15 19:30:33 -06:00
|
|
|
let descriptions = diagnostics::registry::Registry::new(&DIAGNOSTICS);
|
2014-07-01 11:39:41 -05:00
|
|
|
match matches.opt_str("explain") {
|
|
|
|
Some(ref code) => {
|
2015-01-01 18:56:28 -06:00
|
|
|
match descriptions.find_description(code.index(&FullRange)) {
|
2014-07-01 11:39:41 -05:00
|
|
|
Some(ref description) => {
|
|
|
|
println!("{}", description);
|
|
|
|
}
|
|
|
|
None => {
|
2015-01-01 18:56:28 -06:00
|
|
|
early_error(format!("no extended information for {}", code).index(&FullRange));
|
2014-07-01 11:39:41 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
},
|
|
|
|
None => ()
|
|
|
|
}
|
|
|
|
|
|
|
|
let sopts = config::build_session_options(&matches);
|
2014-12-15 18:03:39 -06:00
|
|
|
let odir = matches.opt_str("out-dir").map(|o| Path::new(o));
|
|
|
|
let ofile = matches.opt_str("o").map(|o| Path::new(o));
|
2014-05-06 06:38:01 -05:00
|
|
|
let (input, input_file_path) = match matches.free.len() {
|
2014-06-10 16:03:19 -05:00
|
|
|
0u => {
|
|
|
|
if sopts.describe_lints {
|
|
|
|
let mut ls = lint::LintStore::new();
|
|
|
|
ls.register_builtin(None);
|
2014-06-18 19:26:14 -05:00
|
|
|
describe_lints(&ls, false);
|
2014-06-10 16:03:19 -05:00
|
|
|
return;
|
|
|
|
}
|
2014-11-13 02:39:27 -06:00
|
|
|
let sess = build_session(sopts, None, descriptions);
|
2014-12-15 18:03:39 -06:00
|
|
|
if print_crate_info(&sess, None, &odir, &ofile) {
|
2014-11-13 02:39:27 -06:00
|
|
|
return;
|
|
|
|
}
|
2014-06-10 16:03:19 -05:00
|
|
|
early_error("no input filename given");
|
|
|
|
}
|
2014-05-06 06:38:01 -05:00
|
|
|
1u => {
|
2015-01-01 18:56:28 -06:00
|
|
|
let ifile = matches.free[0].index(&FullRange);
|
2014-05-06 06:38:01 -05:00
|
|
|
if ifile == "-" {
|
|
|
|
let contents = io::stdin().read_to_end().unwrap();
|
2014-06-30 09:41:30 -05:00
|
|
|
let src = String::from_utf8(contents).unwrap();
|
2014-11-27 06:21:26 -06:00
|
|
|
(Input::Str(src), None)
|
2014-05-06 06:38:01 -05:00
|
|
|
} else {
|
2014-11-27 06:21:26 -06:00
|
|
|
(Input::File(Path::new(ifile)), Some(Path::new(ifile)))
|
2014-05-06 06:38:01 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => early_error("multiple input filenames provided")
|
|
|
|
};
|
|
|
|
|
2014-12-27 04:43:14 -06:00
|
|
|
let mut sess = build_session(sopts, input_file_path, descriptions);
|
2014-05-06 06:38:01 -05:00
|
|
|
let cfg = config::build_configuration(&sess);
|
2014-12-15 18:03:39 -06:00
|
|
|
if print_crate_info(&sess, Some(&input), &odir, &ofile) {
|
|
|
|
return
|
|
|
|
}
|
2014-05-06 06:38:01 -05:00
|
|
|
|
|
|
|
let pretty = matches.opt_default("pretty", "normal").map(|a| {
|
2014-12-17 08:13:38 -06:00
|
|
|
// stable pretty-print variants only
|
|
|
|
pretty::parse_pretty(&sess, a.as_slice(), false)
|
2014-05-06 06:38:01 -05:00
|
|
|
});
|
2014-12-17 08:13:38 -06:00
|
|
|
let pretty = if pretty.is_none() &&
|
2014-12-27 03:19:27 -06:00
|
|
|
sess.unstable_options() {
|
2014-12-17 08:13:38 -06:00
|
|
|
matches.opt_str("xpretty").map(|a| {
|
|
|
|
// extended with unstable pretty-print variants
|
|
|
|
pretty::parse_pretty(&sess, a.as_slice(), true)
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
pretty
|
|
|
|
};
|
|
|
|
|
2014-12-15 18:03:39 -06:00
|
|
|
match pretty.into_iter().next() {
|
pretty-printer: let users choose particular items to pretty print.
With this change:
* `--pretty variant=<node-id>` will print the item associated with
`<node-id>` (where `<node-id>` is an integer for some node-id in
the AST, and `variant` means one of {`normal`,`expanded`,...}).
* `--pretty variant=<path-suffix>` will print all of the items that
match the `<path-suffix>` (where `<path-suffix>` is a suffix of a
path, and `variant` again means one of {`normal`,`expanded`,...}).
Example 1: the suffix `typeck::check::check_struct` matches the
item with the path `rustc::middle::typeck::check::check_struct`
when compiling the `rustc` crate.
Example 2: the suffix `and` matches `core::option::Option::and`
and `core::result::Result::and` when compiling the `core` crate.
Both of the `--pretty variant=...` modes will include the full path to
the item in a comment that follows the item.
Note that when multiple paths match, then either:
1. all matching items are printed, in series; this is what happens in
the usual pretty-print variants, or
2. the compiler signals an error; this is what happens in flowgraph
printing.
----
Some drive-by improvements:
Heavily refactored the pretty-printing glue in driver.rs, introducing
a couple local traits to avoid cut-and-pasting very code segments that
differed only in how they accessed the `Session` or the
`ast_map::Map`. (Note the previous code had three similar calls to
`print_crate` which have all been unified in this revision; the
addition of printing individual node-ids exacerbated the situation
beyond tolerance.) We may want to consider promoting some of these
traits, e.g. `SessionCarrier`, for use more generally elsewhere in the
compiler; right now I have to double check how to access the `Session`
depending on what context I am hacking in.
Refactored `PpMode` to make the data directly reflect the fundamental
difference in the categories (in terms of printing source-code with
various annotations, versus printing a control-flow graph).
(also, addressed review feedback.)
2014-08-07 11:22:24 -05:00
|
|
|
Some((ppm, opt_uii)) => {
|
2014-08-11 05:59:35 -05:00
|
|
|
pretty::pretty_print_input(sess, cfg, &input, ppm, opt_uii, ofile);
|
2014-05-06 06:38:01 -05:00
|
|
|
return;
|
|
|
|
}
|
pretty-printer: let users choose particular items to pretty print.
With this change:
* `--pretty variant=<node-id>` will print the item associated with
`<node-id>` (where `<node-id>` is an integer for some node-id in
the AST, and `variant` means one of {`normal`,`expanded`,...}).
* `--pretty variant=<path-suffix>` will print all of the items that
match the `<path-suffix>` (where `<path-suffix>` is a suffix of a
path, and `variant` again means one of {`normal`,`expanded`,...}).
Example 1: the suffix `typeck::check::check_struct` matches the
item with the path `rustc::middle::typeck::check::check_struct`
when compiling the `rustc` crate.
Example 2: the suffix `and` matches `core::option::Option::and`
and `core::result::Result::and` when compiling the `core` crate.
Both of the `--pretty variant=...` modes will include the full path to
the item in a comment that follows the item.
Note that when multiple paths match, then either:
1. all matching items are printed, in series; this is what happens in
the usual pretty-print variants, or
2. the compiler signals an error; this is what happens in flowgraph
printing.
----
Some drive-by improvements:
Heavily refactored the pretty-printing glue in driver.rs, introducing
a couple local traits to avoid cut-and-pasting very code segments that
differed only in how they accessed the `Session` or the
`ast_map::Map`. (Note the previous code had three similar calls to
`print_crate` which have all been unified in this revision; the
addition of printing individual node-ids exacerbated the situation
beyond tolerance.) We may want to consider promoting some of these
traits, e.g. `SessionCarrier`, for use more generally elsewhere in the
compiler; right now I have to double check how to access the `Session`
depending on what context I am hacking in.
Refactored `PpMode` to make the data directly reflect the fundamental
difference in the categories (in terms of printing source-code with
various annotations, versus printing a control-flow graph).
(also, addressed review feedback.)
2014-08-07 11:22:24 -05:00
|
|
|
None => {/* continue */ }
|
2014-05-06 06:38:01 -05:00
|
|
|
}
|
|
|
|
|
2014-12-27 04:43:14 -06:00
|
|
|
if sess.unstable_options() {
|
|
|
|
sess.opts.show_span = matches.opt_str("show-span");
|
|
|
|
}
|
|
|
|
|
2014-05-06 06:38:01 -05:00
|
|
|
let r = matches.opt_strs("Z");
|
2014-05-25 05:17:19 -05:00
|
|
|
if r.contains(&("ls".to_string())) {
|
2014-05-06 06:38:01 -05:00
|
|
|
match input {
|
2014-11-27 06:21:26 -06:00
|
|
|
Input::File(ref ifile) => {
|
2014-05-06 06:38:01 -05:00
|
|
|
let mut stdout = io::stdout();
|
|
|
|
list_metadata(&sess, &(*ifile), &mut stdout).unwrap();
|
|
|
|
}
|
2014-11-27 06:21:26 -06:00
|
|
|
Input::Str(_) => {
|
2014-05-06 06:38:01 -05:00
|
|
|
early_error("can not list metadata for stdin");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-07-19 23:54:37 -05:00
|
|
|
driver::compile_input(sess, cfg, &input, &odir, &ofile, None);
|
2014-05-06 06:38:01 -05:00
|
|
|
}
|
|
|
|
|
2014-10-01 06:38:21 -05:00
|
|
|
/// Returns a version string such as "0.12.0-dev".
|
|
|
|
pub fn release_str() -> Option<&'static str> {
|
|
|
|
option_env!("CFG_RELEASE")
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns the full SHA1 hash of HEAD of the Git repo from which rustc was built.
|
|
|
|
pub fn commit_hash_str() -> Option<&'static str> {
|
|
|
|
option_env!("CFG_VER_HASH")
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns the "commit date" of HEAD of the Git repo from which rustc was built as a static string.
|
|
|
|
pub fn commit_date_str() -> Option<&'static str> {
|
|
|
|
option_env!("CFG_VER_DATE")
|
|
|
|
}
|
|
|
|
|
2014-05-18 22:37:07 -05:00
|
|
|
/// Prints version information and returns None on success or an error
|
2014-11-08 09:47:51 -06:00
|
|
|
/// message on panic.
|
2014-12-15 18:03:39 -06:00
|
|
|
pub fn version(binary: &str, matches: &getopts::Matches) {
|
|
|
|
let verbose = matches.opt_present("verbose");
|
2014-05-18 22:37:07 -05:00
|
|
|
|
2014-10-01 06:38:21 -05:00
|
|
|
println!("{} {}", binary, option_env!("CFG_VERSION").unwrap_or("unknown version"));
|
2014-05-18 22:37:07 -05:00
|
|
|
if verbose {
|
2014-10-01 06:38:21 -05:00
|
|
|
fn unw(x: Option<&str>) -> &str { x.unwrap_or("unknown") }
|
2014-05-18 22:37:07 -05:00
|
|
|
println!("binary: {}", binary);
|
2014-10-01 06:38:21 -05:00
|
|
|
println!("commit-hash: {}", unw(commit_hash_str()));
|
|
|
|
println!("commit-date: {}", unw(commit_date_str()));
|
2014-11-15 19:30:33 -06:00
|
|
|
println!("host: {}", config::host_triple());
|
2014-10-01 06:38:21 -05:00
|
|
|
println!("release: {}", unw(release_str()));
|
2014-05-18 22:37:07 -05:00
|
|
|
}
|
2014-05-06 06:38:01 -05:00
|
|
|
}
|
|
|
|
|
2014-12-17 07:42:50 -06:00
|
|
|
fn usage(verbose: bool, include_unstable_options: bool) {
|
2014-12-15 18:03:39 -06:00
|
|
|
let groups = if verbose {
|
2014-12-17 07:42:50 -06:00
|
|
|
config::rustc_optgroups()
|
2014-12-15 18:03:39 -06:00
|
|
|
} else {
|
2014-12-17 07:42:50 -06:00
|
|
|
config::rustc_short_optgroups()
|
2014-12-15 18:03:39 -06:00
|
|
|
};
|
2014-12-17 07:42:50 -06:00
|
|
|
let groups : Vec<_> = groups.into_iter()
|
|
|
|
.filter(|x| include_unstable_options || x.is_stable())
|
|
|
|
.map(|x|x.opt_group)
|
|
|
|
.collect();
|
2014-05-12 17:39:07 -05:00
|
|
|
let message = format!("Usage: rustc [OPTIONS] INPUT");
|
2014-12-15 18:03:39 -06:00
|
|
|
let extra_help = if verbose {
|
|
|
|
""
|
|
|
|
} else {
|
|
|
|
"\n --help -v Print the full set of options rustc accepts"
|
|
|
|
};
|
2014-05-06 06:38:01 -05:00
|
|
|
println!("{}\n\
|
|
|
|
Additional help:
|
|
|
|
-C help Print codegen options
|
|
|
|
-W help Print 'lint' options and default settings
|
2014-12-15 18:03:39 -06:00
|
|
|
-Z help Print internal options for debugging rustc{}\n",
|
|
|
|
getopts::usage(message.as_slice(), groups.as_slice()),
|
|
|
|
extra_help);
|
2014-05-06 06:38:01 -05:00
|
|
|
}
|
|
|
|
|
2014-06-18 19:26:14 -05:00
|
|
|
fn describe_lints(lint_store: &lint::LintStore, loaded_plugins: bool) {
|
2014-05-06 06:38:01 -05:00
|
|
|
println!("
|
|
|
|
Available lint options:
|
|
|
|
-W <foo> Warn about <foo>
|
|
|
|
-A <foo> Allow <foo>
|
|
|
|
-D <foo> Deny <foo>
|
|
|
|
-F <foo> Forbid <foo> (deny, and deny all overrides)
|
2014-06-10 16:03:19 -05:00
|
|
|
|
2014-05-06 06:38:01 -05:00
|
|
|
");
|
|
|
|
|
2014-10-29 20:21:37 -05:00
|
|
|
fn sort_lints(lints: Vec<(&'static Lint, bool)>) -> Vec<&'static Lint> {
|
|
|
|
let mut lints: Vec<_> = lints.into_iter().map(|(x, _)| x).collect();
|
|
|
|
lints.sort_by(|x: &&Lint, y: &&Lint| {
|
|
|
|
match x.default_level.cmp(&y.default_level) {
|
|
|
|
// The sort doesn't case-fold but it's doubtful we care.
|
|
|
|
Equal => x.name.cmp(y.name),
|
|
|
|
r => r,
|
|
|
|
}
|
|
|
|
});
|
|
|
|
lints
|
|
|
|
}
|
|
|
|
|
|
|
|
fn sort_lint_groups(lints: Vec<(&'static str, Vec<lint::LintId>, bool)>)
|
|
|
|
-> Vec<(&'static str, Vec<lint::LintId>)> {
|
|
|
|
let mut lints: Vec<_> = lints.into_iter().map(|(x, y, _)| (x, y)).collect();
|
|
|
|
lints.sort_by(|&(x, _): &(&'static str, Vec<lint::LintId>),
|
|
|
|
&(y, _): &(&'static str, Vec<lint::LintId>)| {
|
|
|
|
x.cmp(y)
|
|
|
|
});
|
|
|
|
lints
|
|
|
|
}
|
|
|
|
|
2014-12-30 12:51:18 -06:00
|
|
|
let (plugin, builtin): (Vec<_>, _) = lint_store.get_lints()
|
|
|
|
.iter().cloned().partition(|&(_, p)| p);
|
2014-06-18 19:26:14 -05:00
|
|
|
let plugin = sort_lints(plugin);
|
2014-06-10 16:03:19 -05:00
|
|
|
let builtin = sort_lints(builtin);
|
|
|
|
|
2014-12-30 12:51:18 -06:00
|
|
|
let (plugin_groups, builtin_groups): (Vec<_>, _) = lint_store.get_lint_groups()
|
|
|
|
.iter().cloned().partition(|&(_, _, p)| p);
|
2014-07-20 22:27:59 -05:00
|
|
|
let plugin_groups = sort_lint_groups(plugin_groups);
|
|
|
|
let builtin_groups = sort_lint_groups(builtin_groups);
|
|
|
|
|
2014-06-18 19:26:14 -05:00
|
|
|
let max_name_len = plugin.iter().chain(builtin.iter())
|
2014-07-20 22:27:59 -05:00
|
|
|
.map(|&s| s.name.width(true))
|
2014-06-04 16:35:58 -05:00
|
|
|
.max().unwrap_or(0);
|
2014-12-30 19:56:05 -06:00
|
|
|
let padded = |&: x: &str| {
|
2014-12-10 21:46:38 -06:00
|
|
|
let mut s = repeat(" ").take(max_name_len - x.chars().count())
|
|
|
|
.collect::<String>();
|
2014-10-15 01:05:01 -05:00
|
|
|
s.push_str(x);
|
|
|
|
s
|
2014-06-04 16:35:58 -05:00
|
|
|
};
|
2014-05-06 06:38:01 -05:00
|
|
|
|
2014-06-10 16:03:19 -05:00
|
|
|
println!("Lint checks provided by rustc:\n");
|
2014-11-17 13:29:38 -06:00
|
|
|
println!(" {} {:7.7} {}", padded("name"), "default", "meaning");
|
|
|
|
println!(" {} {:7.7} {}", padded("----"), "-------", "-------");
|
2014-06-04 16:35:58 -05:00
|
|
|
|
2014-12-30 19:56:05 -06:00
|
|
|
let print_lints = |&: lints: Vec<&Lint>| {
|
2014-09-14 22:27:36 -05:00
|
|
|
for lint in lints.into_iter() {
|
2014-06-13 15:04:52 -05:00
|
|
|
let name = lint.name_lower().replace("_", "-");
|
2014-11-17 13:29:38 -06:00
|
|
|
println!(" {} {:7.7} {}",
|
2015-01-01 18:56:28 -06:00
|
|
|
padded(name.index(&FullRange)), lint.default_level.as_str(), lint.desc);
|
2014-06-10 16:03:19 -05:00
|
|
|
}
|
|
|
|
println!("\n");
|
|
|
|
};
|
|
|
|
|
|
|
|
print_lints(builtin);
|
|
|
|
|
2014-07-20 22:27:59 -05:00
|
|
|
|
|
|
|
|
|
|
|
let max_name_len = plugin_groups.iter().chain(builtin_groups.iter())
|
|
|
|
.map(|&(s, _)| s.width(true))
|
|
|
|
.max().unwrap_or(0);
|
2014-12-30 19:56:05 -06:00
|
|
|
let padded = |&: x: &str| {
|
2014-12-10 21:46:38 -06:00
|
|
|
let mut s = repeat(" ").take(max_name_len - x.chars().count())
|
|
|
|
.collect::<String>();
|
2014-10-15 01:05:01 -05:00
|
|
|
s.push_str(x);
|
|
|
|
s
|
2014-07-20 22:27:59 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
println!("Lint groups provided by rustc:\n");
|
|
|
|
println!(" {} {}", padded("name"), "sub-lints");
|
|
|
|
println!(" {} {}", padded("----"), "---------");
|
|
|
|
|
2014-12-30 19:56:05 -06:00
|
|
|
let print_lint_groups = |&: lints: Vec<(&'static str, Vec<lint::LintId>)>| {
|
2014-09-14 22:27:36 -05:00
|
|
|
for (name, to) in lints.into_iter() {
|
2014-07-20 22:27:59 -05:00
|
|
|
let name = name.chars().map(|x| x.to_lowercase())
|
|
|
|
.collect::<String>().replace("_", "-");
|
2014-10-10 20:34:45 -05:00
|
|
|
let desc = to.into_iter().map(|x| x.as_str().replace("_", "-"))
|
|
|
|
.collect::<Vec<String>>().connect(", ");
|
2014-07-20 22:27:59 -05:00
|
|
|
println!(" {} {}",
|
2015-01-01 18:56:28 -06:00
|
|
|
padded(name.index(&FullRange)), desc);
|
2014-06-18 19:26:14 -05:00
|
|
|
}
|
2014-07-20 22:27:59 -05:00
|
|
|
println!("\n");
|
|
|
|
};
|
|
|
|
|
|
|
|
print_lint_groups(builtin_groups);
|
|
|
|
|
|
|
|
match (loaded_plugins, plugin.len(), plugin_groups.len()) {
|
|
|
|
(false, 0, _) | (false, _, 0) => {
|
|
|
|
println!("Compiler plugins can provide additional lints and lint groups. To see a \
|
|
|
|
listing of these, re-run `rustc -W help` with a crate filename.");
|
|
|
|
}
|
2014-10-09 14:17:22 -05:00
|
|
|
(false, _, _) => panic!("didn't load lint plugins but got them anyway!"),
|
2014-07-20 22:27:59 -05:00
|
|
|
(true, 0, 0) => println!("This crate does not load any lint plugins or lint groups."),
|
|
|
|
(true, l, g) => {
|
|
|
|
if l > 0 {
|
|
|
|
println!("Lint checks provided by plugins loaded by this crate:\n");
|
|
|
|
print_lints(plugin);
|
|
|
|
}
|
|
|
|
if g > 0 {
|
|
|
|
println!("Lint groups provided by plugins loaded by this crate:\n");
|
|
|
|
print_lint_groups(plugin_groups);
|
|
|
|
}
|
2014-06-18 19:26:14 -05:00
|
|
|
}
|
|
|
|
}
|
2014-05-06 06:38:01 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
fn describe_debug_flags() {
|
|
|
|
println!("\nAvailable debug options:\n");
|
|
|
|
let r = config::debugging_opts_map();
|
|
|
|
for tuple in r.iter() {
|
|
|
|
match *tuple {
|
|
|
|
(ref name, ref desc, _) => {
|
2014-11-17 13:29:38 -06:00
|
|
|
println!(" -Z {:>20} -- {}", *name, *desc);
|
2014-05-06 06:38:01 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn describe_codegen_flags() {
|
|
|
|
println!("\nAvailable codegen options:\n");
|
2014-11-15 07:51:22 -06:00
|
|
|
for &(name, _, opt_type_desc, desc) in config::CG_OPTIONS.iter() {
|
|
|
|
let (width, extra) = match opt_type_desc {
|
|
|
|
Some(..) => (21, "=val"),
|
|
|
|
None => (25, "")
|
2014-05-06 06:38:01 -05:00
|
|
|
};
|
2014-11-17 13:29:38 -06:00
|
|
|
println!(" -C {:>width$}{} -- {}", name.replace("_", "-"),
|
2014-05-06 06:38:01 -05:00
|
|
|
extra, desc, width=width);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-08 23:00:52 -05:00
|
|
|
/// Process command line options. Emits messages as appropriate. If compilation
|
2014-05-06 06:38:01 -05:00
|
|
|
/// should continue, returns a getopts::Matches object parsed from args, otherwise
|
|
|
|
/// returns None.
|
2014-05-22 18:57:53 -05:00
|
|
|
pub fn handle_options(mut args: Vec<String>) -> Option<getopts::Matches> {
|
2014-05-12 17:39:07 -05:00
|
|
|
// Throw away the first argument, the name of the binary
|
2014-12-30 12:51:18 -06:00
|
|
|
let _binary = args.remove(0);
|
2014-05-06 06:38:01 -05:00
|
|
|
|
2014-05-14 23:39:11 -05:00
|
|
|
if args.is_empty() {
|
2014-12-17 07:42:50 -06:00
|
|
|
// user did not write `-v` nor `-Z unstable-options`, so do not
|
|
|
|
// include that extra information.
|
|
|
|
usage(false, false);
|
2014-05-14 23:39:11 -05:00
|
|
|
return None;
|
|
|
|
}
|
2014-05-06 06:38:01 -05:00
|
|
|
|
|
|
|
let matches =
|
2015-01-01 18:56:28 -06:00
|
|
|
match getopts::getopts(args.index(&FullRange), config::optgroups().index(&FullRange)) {
|
2014-05-06 06:38:01 -05:00
|
|
|
Ok(m) => m,
|
2014-12-17 07:42:50 -06:00
|
|
|
Err(f_stable_attempt) => {
|
|
|
|
// redo option parsing, including unstable options this time,
|
|
|
|
// in anticipation that the mishandled option was one of the
|
|
|
|
// unstable ones.
|
|
|
|
let all_groups : Vec<getopts::OptGroup>
|
|
|
|
= config::rustc_optgroups().into_iter().map(|x|x.opt_group).collect();
|
|
|
|
match getopts::getopts(args.as_slice(), all_groups.as_slice()) {
|
|
|
|
Ok(m_unstable) => {
|
|
|
|
let r = m_unstable.opt_strs("Z");
|
|
|
|
let include_unstable_options = r.iter().any(|x| *x == "unstable-options");
|
|
|
|
if include_unstable_options {
|
|
|
|
m_unstable
|
|
|
|
} else {
|
|
|
|
early_error(f_stable_attempt.to_string().as_slice());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Err(_) => {
|
|
|
|
// ignore the error from the unstable attempt; just
|
|
|
|
// pass the error we got from the first try.
|
|
|
|
early_error(f_stable_attempt.to_string().as_slice());
|
|
|
|
}
|
|
|
|
}
|
2014-05-06 06:38:01 -05:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2014-12-17 07:42:50 -06:00
|
|
|
let r = matches.opt_strs("Z");
|
|
|
|
let include_unstable_options = r.iter().any(|x| *x == "unstable-options");
|
|
|
|
|
2014-05-06 06:38:01 -05:00
|
|
|
if matches.opt_present("h") || matches.opt_present("help") {
|
2014-12-17 07:42:50 -06:00
|
|
|
usage(matches.opt_present("verbose"), include_unstable_options);
|
2014-05-06 06:38:01 -05:00
|
|
|
return None;
|
|
|
|
}
|
|
|
|
|
2014-06-04 16:35:58 -05:00
|
|
|
// Don't handle -W help here, because we might first load plugins.
|
2014-05-06 06:38:01 -05:00
|
|
|
|
|
|
|
let r = matches.opt_strs("Z");
|
2014-11-27 13:10:25 -06:00
|
|
|
if r.iter().any(|x| *x == "help") {
|
2014-05-06 06:38:01 -05:00
|
|
|
describe_debug_flags();
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
|
|
|
|
let cg_flags = matches.opt_strs("C");
|
2014-11-27 13:10:25 -06:00
|
|
|
if cg_flags.iter().any(|x| *x == "help") {
|
2014-05-06 06:38:01 -05:00
|
|
|
describe_codegen_flags();
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
|
2014-05-25 05:17:19 -05:00
|
|
|
if cg_flags.contains(&"passes=list".to_string()) {
|
2014-07-07 19:58:01 -05:00
|
|
|
unsafe { ::llvm::LLVMRustPrintPasses(); }
|
2014-05-06 06:38:01 -05:00
|
|
|
return None;
|
|
|
|
}
|
|
|
|
|
2014-05-18 22:37:07 -05:00
|
|
|
if matches.opt_present("version") {
|
2014-12-15 18:03:39 -06:00
|
|
|
version("rustc", &matches);
|
|
|
|
return None;
|
2014-05-06 06:38:01 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
Some(matches)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn print_crate_info(sess: &Session,
|
2014-12-15 18:03:39 -06:00
|
|
|
input: Option<&Input>,
|
2014-05-06 06:38:01 -05:00
|
|
|
odir: &Option<Path>,
|
|
|
|
ofile: &Option<Path>)
|
|
|
|
-> bool {
|
2014-12-15 18:03:39 -06:00
|
|
|
if sess.opts.prints.len() == 0 { return false }
|
|
|
|
|
|
|
|
let attrs = input.map(|input| parse_crate_attrs(sess, input));
|
|
|
|
for req in sess.opts.prints.iter() {
|
|
|
|
match *req {
|
|
|
|
PrintRequest::Sysroot => println!("{}", sess.sysroot().display()),
|
|
|
|
PrintRequest::FileNames |
|
|
|
|
PrintRequest::CrateName => {
|
|
|
|
let input = match input {
|
|
|
|
Some(input) => input,
|
|
|
|
None => early_error("no input file provided"),
|
|
|
|
};
|
|
|
|
let attrs = attrs.as_ref().unwrap().as_slice();
|
|
|
|
let t_outputs = driver::build_output_filenames(input,
|
|
|
|
odir,
|
|
|
|
ofile,
|
|
|
|
attrs,
|
|
|
|
sess);
|
|
|
|
let id = link::find_crate_name(Some(sess), attrs.as_slice(),
|
|
|
|
input);
|
|
|
|
if *req == PrintRequest::CrateName {
|
|
|
|
println!("{}", id);
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
let crate_types = driver::collect_crate_types(sess, attrs);
|
|
|
|
let metadata = driver::collect_crate_metadata(sess, attrs);
|
|
|
|
*sess.crate_metadata.borrow_mut() = metadata;
|
|
|
|
for &style in crate_types.iter() {
|
|
|
|
let fname = link::filename_for_input(sess, style,
|
|
|
|
id.as_slice(),
|
|
|
|
&t_outputs.with_extension(""));
|
|
|
|
println!("{}", fname.filename_display());
|
|
|
|
}
|
2014-05-06 06:38:01 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-12-15 18:03:39 -06:00
|
|
|
return true;
|
2014-05-06 06:38:01 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
fn parse_crate_attrs(sess: &Session, input: &Input) ->
|
|
|
|
Vec<ast::Attribute> {
|
|
|
|
let result = match *input {
|
2014-11-27 06:21:26 -06:00
|
|
|
Input::File(ref ifile) => {
|
2014-05-06 06:38:01 -05:00
|
|
|
parse::parse_crate_attrs_from_file(ifile,
|
|
|
|
Vec::new(),
|
|
|
|
&sess.parse_sess)
|
|
|
|
}
|
2014-11-27 06:21:26 -06:00
|
|
|
Input::Str(ref src) => {
|
2014-05-06 06:38:01 -05:00
|
|
|
parse::parse_crate_attrs_from_source_str(
|
2014-05-25 05:17:19 -05:00
|
|
|
driver::anon_src().to_string(),
|
|
|
|
src.to_string(),
|
2014-05-06 06:38:01 -05:00
|
|
|
Vec::new(),
|
|
|
|
&sess.parse_sess)
|
|
|
|
}
|
|
|
|
};
|
2014-09-14 22:27:36 -05:00
|
|
|
result.into_iter().collect()
|
2014-05-06 06:38:01 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn list_metadata(sess: &Session, path: &Path,
|
|
|
|
out: &mut io::Writer) -> io::IoResult<()> {
|
2014-07-23 13:56:36 -05:00
|
|
|
metadata::loader::list_file_metadata(sess.target.target.options.is_like_osx, path, out)
|
2014-05-06 06:38:01 -05:00
|
|
|
}
|
|
|
|
|
2014-11-08 09:47:51 -06:00
|
|
|
/// Run a procedure which will detect panics in the compiler and print nicer
|
2014-05-06 06:38:01 -05:00
|
|
|
/// error messages rather than just failing the test.
|
|
|
|
///
|
|
|
|
/// The diagnostic emitter yielded to the procedure should be used for reporting
|
|
|
|
/// errors of the compiler.
|
2014-11-26 07:12:18 -06:00
|
|
|
pub fn monitor<F:FnOnce()+Send>(f: F) {
|
2014-12-15 17:21:08 -06:00
|
|
|
static STACK_SIZE: uint = 8 * 1024 * 1024; // 8MB
|
2014-05-06 06:38:01 -05:00
|
|
|
|
2014-06-16 18:17:59 -05:00
|
|
|
let (tx, rx) = channel();
|
2014-12-14 02:05:32 -06:00
|
|
|
let w = io::ChanWriter::new(tx);
|
2014-06-16 18:17:59 -05:00
|
|
|
let mut r = io::ChanReader::new(rx);
|
|
|
|
|
2014-12-14 02:05:32 -06:00
|
|
|
let mut cfg = thread::Builder::new().name("rustc".to_string());
|
2014-05-06 06:38:01 -05:00
|
|
|
|
|
|
|
// FIXME: Hacks on hacks. If the env is trying to override the stack size
|
|
|
|
// then *don't* set it explicitly.
|
|
|
|
if os::getenv("RUST_MIN_STACK").is_none() {
|
2014-12-06 20:34:37 -06:00
|
|
|
cfg = cfg.stack_size(STACK_SIZE);
|
2014-05-06 06:38:01 -05:00
|
|
|
}
|
|
|
|
|
2015-01-05 23:59:45 -06:00
|
|
|
match cfg.scoped(move || { std::io::stdio::set_stderr(box w); f() }).join() {
|
2014-05-06 06:38:01 -05:00
|
|
|
Ok(()) => { /* fallthrough */ }
|
|
|
|
Err(value) => {
|
2014-12-26 15:04:27 -06:00
|
|
|
// Thread panicked without emitting a fatal diagnostic
|
2014-05-06 06:38:01 -05:00
|
|
|
if !value.is::<diagnostic::FatalError>() {
|
2014-07-01 11:39:41 -05:00
|
|
|
let mut emitter = diagnostic::EmitterWriter::stderr(diagnostic::Auto, None);
|
2014-05-06 06:38:01 -05:00
|
|
|
|
|
|
|
// a .span_bug or .bug call has already printed what
|
|
|
|
// it wants to print.
|
|
|
|
if !value.is::<diagnostic::ExplicitBug>() {
|
|
|
|
emitter.emit(
|
|
|
|
None,
|
2014-10-09 14:17:22 -05:00
|
|
|
"unexpected panic",
|
2014-07-01 11:39:41 -05:00
|
|
|
None,
|
2014-05-06 06:38:01 -05:00
|
|
|
diagnostic::Bug);
|
|
|
|
}
|
|
|
|
|
|
|
|
let xs = [
|
2014-10-09 14:17:22 -05:00
|
|
|
"the compiler unexpectedly panicked. this is a bug.".to_string(),
|
2014-05-19 19:23:26 -05:00
|
|
|
format!("we would appreciate a bug report: {}",
|
|
|
|
BUG_REPORT_URL),
|
2014-05-25 05:10:11 -05:00
|
|
|
"run with `RUST_BACKTRACE=1` for a backtrace".to_string(),
|
2014-05-06 06:38:01 -05:00
|
|
|
];
|
|
|
|
for note in xs.iter() {
|
2015-01-01 18:56:28 -06:00
|
|
|
emitter.emit(None, note.index(&FullRange), None, diagnostic::Note)
|
2014-05-06 06:38:01 -05:00
|
|
|
}
|
|
|
|
|
2014-06-21 05:39:03 -05:00
|
|
|
match r.read_to_string() {
|
2014-05-06 06:38:01 -05:00
|
|
|
Ok(s) => println!("{}", s),
|
2014-05-16 12:45:16 -05:00
|
|
|
Err(e) => {
|
|
|
|
emitter.emit(None,
|
|
|
|
format!("failed to read internal \
|
2015-01-01 18:56:28 -06:00
|
|
|
stderr: {}", e).index(&FullRange),
|
2014-07-01 11:39:41 -05:00
|
|
|
None,
|
2014-05-16 12:45:16 -05:00
|
|
|
diagnostic::Error)
|
|
|
|
}
|
2014-05-06 06:38:01 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-09 14:17:22 -05:00
|
|
|
// Panic so the process returns a failure code, but don't pollute the
|
2014-11-08 09:47:51 -06:00
|
|
|
// output with some unnecessary panic messages, we've already
|
2014-05-06 06:38:01 -05:00
|
|
|
// printed everything that we needed to.
|
|
|
|
io::stdio::set_stderr(box io::util::NullWriter);
|
2014-10-09 14:17:22 -05:00
|
|
|
panic!();
|
2014-05-06 06:38:01 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-11-15 19:30:33 -06:00
|
|
|
|
2014-11-27 08:57:47 -06:00
|
|
|
pub fn main() {
|
|
|
|
let args = std::os::args();
|
|
|
|
let result = run(args);
|
|
|
|
std::os::set_exit_status(result);
|
|
|
|
}
|