1 // Copyright 2014-2015 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
11 //! The Rust compiler.
15 //! This API is completely unstable and subject to change.
17 #![crate_name = "rustc_driver"]
18 #![crate_type = "dylib"]
19 #![crate_type = "rlib"]
20 #![doc(html_logo_url = "https://www.rust-lang.org/logos/rust-logo-128x128-blk-v2.png",
21 html_favicon_url = "https://doc.rust-lang.org/favicon.ico",
22 html_root_url = "https://doc.rust-lang.org/nightly/")]
25 #![feature(box_syntax)]
28 #![feature(rustc_diagnostic_macros)]
29 #![feature(set_stdio)]
33 extern crate graphviz;
34 extern crate env_logger;
37 extern crate rustc_back;
38 extern crate rustc_borrowck;
39 extern crate rustc_const_eval;
40 extern crate rustc_data_structures;
41 extern crate rustc_errors as errors;
42 extern crate rustc_passes;
43 extern crate rustc_lint;
44 extern crate rustc_plugin;
45 extern crate rustc_privacy;
46 extern crate rustc_incremental;
47 extern crate rustc_metadata;
48 extern crate rustc_mir;
49 extern crate rustc_resolve;
50 extern crate rustc_save_analysis;
51 extern crate rustc_trans;
52 extern crate rustc_typeck;
53 extern crate serialize;
57 extern crate syntax_ext;
58 extern crate syntax_pos;
60 use driver::CompileController;
61 use pretty::{PpMode, UserIdentifiedItem};
63 use rustc_resolve as resolve;
64 use rustc_save_analysis as save;
65 use rustc_save_analysis::DumpHandler;
66 use rustc_trans::back::link;
67 use rustc_trans::back::write::{RELOC_MODEL_ARGS, CODE_GEN_MODEL_ARGS};
68 use rustc::dep_graph::DepGraph;
69 use rustc::session::{self, config, Session, build_session, CompileResult};
70 use rustc::session::config::{Input, PrintRequest, OutputType, ErrorOutputType};
71 use rustc::session::config::nightly_options;
72 use rustc::session::{early_error, early_warn};
73 use rustc::lint::Lint;
75 use rustc_metadata::locator;
76 use rustc_metadata::cstore::CStore;
77 use rustc::util::common::time;
79 use serialize::json::ToJson;
83 use std::cmp::Ordering::Equal;
84 use std::default::Default;
86 use std::io::{self, Read, Write};
87 use std::iter::repeat;
88 use std::path::PathBuf;
92 use std::sync::{Arc, Mutex};
96 use syntax::codemap::{CodeMap, FileLoader, RealFileLoader};
97 use syntax::feature_gate::{GatedCfg, UnstableFeatures};
98 use syntax::parse::{self, PResult};
99 use syntax_pos::{DUMMY_SP, MultiSpan};
106 pub mod target_features;
107 mod derive_registrar;
109 const BUG_REPORT_URL: &'static str = "https://github.com/rust-lang/rust/blob/master/CONTRIBUTING.\
113 fn abort_msg(err_count: usize) -> String {
115 0 => "aborting with no errors (maybe a bug?)".to_owned(),
116 _ => "aborting due to previous error(s)".to_owned(),
120 pub fn abort_on_err<T>(result: Result<T, usize>, sess: &Session) -> T {
123 sess.fatal(&abort_msg(err_count));
129 pub fn run<F>(run_compiler: F) -> isize
130 where F: FnOnce() -> (CompileResult, Option<Session>) + Send + 'static
133 let (result, session) = run_compiler();
134 if let Err(err_count) = result {
137 Some(sess) => sess.fatal(&abort_msg(err_count)),
140 errors::emitter::EmitterWriter::stderr(errors::ColorConfig::Auto, None);
141 let handler = errors::Handler::with_emitter(true, false, Box::new(emitter));
142 handler.emit(&MultiSpan::new(),
143 &abort_msg(err_count),
144 errors::Level::Fatal);
154 // Parse args and run the compiler. This is the primary entry point for rustc.
155 // See comments on CompilerCalls below for details about the callbacks argument.
156 // The FileLoader provides a way to load files from sources other than the file system.
157 pub fn run_compiler<'a>(args: &[String],
158 callbacks: &mut CompilerCalls<'a>,
159 file_loader: Option<Box<FileLoader + 'static>>,
160 emitter_dest: Option<Box<Write + Send>>)
161 -> (CompileResult, Option<Session>)
163 macro_rules! do_or_return {($expr: expr, $sess: expr) => {
165 Compilation::Stop => return (Ok(()), $sess),
166 Compilation::Continue => {}
170 let matches = match handle_options(args) {
171 Some(matches) => matches,
172 None => return (Ok(()), None),
175 let (sopts, cfg) = config::build_session_options_and_crate_config(&matches);
177 if sopts.debugging_opts.debug_llvm {
178 rustc_trans::enable_llvm_debug();
181 let descriptions = diagnostics_registry();
183 do_or_return!(callbacks.early_callback(&matches,
190 let (odir, ofile) = make_output(&matches);
191 let (input, input_file_path) = match make_input(&matches.free) {
192 Some((input, input_file_path)) => callbacks.some_input(input, input_file_path),
193 None => match callbacks.no_input(&matches, &sopts, &cfg, &odir, &ofile, &descriptions) {
194 Some((input, input_file_path)) => (input, input_file_path),
195 None => return (Ok(()), None),
199 let dep_graph = DepGraph::new(sopts.build_dep_graph());
200 let cstore = Rc::new(CStore::new(&dep_graph, box rustc_trans::LlvmMetadataLoader));
202 let loader = file_loader.unwrap_or(box RealFileLoader);
203 let codemap = Rc::new(CodeMap::with_file_loader(loader, sopts.file_path_mapping()));
204 let mut sess = session::build_session_with_codemap(
205 sopts, &dep_graph, input_file_path, descriptions, cstore.clone(), codemap, emitter_dest,
207 rustc_trans::init(&sess);
208 rustc_lint::register_builtins(&mut sess.lint_store.borrow_mut(), Some(&sess));
210 let mut cfg = config::build_configuration(&sess, cfg);
211 target_features::add_configuration(&mut cfg, &sess);
212 sess.parse_sess.config = cfg;
214 do_or_return!(callbacks.late_callback(&matches, &sess, &input, &odir, &ofile), Some(sess));
216 let plugins = sess.opts.debugging_opts.extra_plugins.clone();
217 let control = callbacks.build_controller(&sess, &matches);
218 (driver::compile_input(&sess, &cstore, &input, &odir, &ofile, Some(plugins), &control),
222 // Extract output directory and file from matches.
223 fn make_output(matches: &getopts::Matches) -> (Option<PathBuf>, Option<PathBuf>) {
224 let odir = matches.opt_str("out-dir").map(|o| PathBuf::from(&o));
225 let ofile = matches.opt_str("o").map(|o| PathBuf::from(&o));
229 // Extract input (string or file and optional path) from matches.
230 fn make_input(free_matches: &[String]) -> Option<(Input, Option<PathBuf>)> {
231 if free_matches.len() == 1 {
232 let ifile = &free_matches[0];
234 let mut src = String::new();
235 io::stdin().read_to_string(&mut src).unwrap();
236 Some((Input::Str { name: driver::anon_src(), input: src },
239 Some((Input::File(PathBuf::from(ifile)),
240 Some(PathBuf::from(ifile))))
247 fn parse_pretty(sess: &Session,
248 matches: &getopts::Matches)
249 -> Option<(PpMode, Option<UserIdentifiedItem>)> {
250 let pretty = if sess.opts.debugging_opts.unstable_options {
251 matches.opt_default("pretty", "normal").map(|a| {
252 // stable pretty-print variants only
253 pretty::parse_pretty(sess, &a, false)
258 if pretty.is_none() && sess.unstable_options() {
259 matches.opt_str("unpretty").map(|a| {
260 // extended with unstable pretty-print variants
261 pretty::parse_pretty(sess, &a, true)
268 // Whether to stop or continue compilation.
269 #[derive(Copy, Clone, Debug, Eq, PartialEq)]
270 pub enum Compilation {
276 pub fn and_then<F: FnOnce() -> Compilation>(self, next: F) -> Compilation {
278 Compilation::Stop => Compilation::Stop,
279 Compilation::Continue => next(),
284 // A trait for customising the compilation process. Offers a number of hooks for
285 // executing custom code or customising input.
286 pub trait CompilerCalls<'a> {
287 // Hook for a callback early in the process of handling arguments. This will
288 // be called straight after options have been parsed but before anything
289 // else (e.g., selecting input and output).
290 fn early_callback(&mut self,
291 _: &getopts::Matches,
293 _: &ast::CrateConfig,
294 _: &errors::registry::Registry,
297 Compilation::Continue
300 // Hook for a callback late in the process of handling arguments. This will
301 // be called just before actual compilation starts (and before build_controller
302 // is called), after all arguments etc. have been completely handled.
303 fn late_callback(&mut self,
304 _: &getopts::Matches,
310 Compilation::Continue
313 // Called after we extract the input from the arguments. Gives the implementer
314 // an opportunity to change the inputs or to add some custom input handling.
315 // The default behaviour is to simply pass through the inputs.
316 fn some_input(&mut self,
318 input_path: Option<PathBuf>)
319 -> (Input, Option<PathBuf>) {
323 // Called after we extract the input from the arguments if there is no valid
324 // input. Gives the implementer an opportunity to supply alternate input (by
325 // returning a Some value) or to add custom behaviour for this error such as
326 // emitting error messages. Returning None will cause compilation to stop
328 fn no_input(&mut self,
329 _: &getopts::Matches,
331 _: &ast::CrateConfig,
334 _: &errors::registry::Registry)
335 -> Option<(Input, Option<PathBuf>)> {
339 // Create a CompilController struct for controlling the behaviour of
341 fn build_controller(&mut self, _: &Session, _: &getopts::Matches) -> CompileController<'a>;
344 // CompilerCalls instance for a regular rustc build.
345 #[derive(Copy, Clone)]
346 pub struct RustcDefaultCalls;
348 fn handle_explain(code: &str,
349 descriptions: &errors::registry::Registry,
350 output: ErrorOutputType) {
351 let normalised = if code.starts_with("E") {
354 format!("E{0:0>4}", code)
356 match descriptions.find_description(&normalised) {
357 Some(ref description) => {
358 // Slice off the leading newline and print.
359 print!("{}", &(&description[1..]).split("\n").map(|x| {
360 format!("{}\n", if x.starts_with("```") {
365 }).collect::<String>());
368 early_error(output, &format!("no extended information for {}", code));
373 impl<'a> CompilerCalls<'a> for RustcDefaultCalls {
374 fn early_callback(&mut self,
375 matches: &getopts::Matches,
377 _: &ast::CrateConfig,
378 descriptions: &errors::registry::Registry,
379 output: ErrorOutputType)
381 if let Some(ref code) = matches.opt_str("explain") {
382 handle_explain(code, descriptions, output);
383 return Compilation::Stop;
386 Compilation::Continue
389 fn no_input(&mut self,
390 matches: &getopts::Matches,
391 sopts: &config::Options,
392 cfg: &ast::CrateConfig,
393 odir: &Option<PathBuf>,
394 ofile: &Option<PathBuf>,
395 descriptions: &errors::registry::Registry)
396 -> Option<(Input, Option<PathBuf>)> {
397 match matches.free.len() {
399 if sopts.describe_lints {
400 let mut ls = lint::LintStore::new();
401 rustc_lint::register_builtins(&mut ls, None);
402 describe_lints(&ls, false);
405 let dep_graph = DepGraph::new(sopts.build_dep_graph());
406 let cstore = Rc::new(CStore::new(&dep_graph, box rustc_trans::LlvmMetadataLoader));
407 let mut sess = build_session(sopts.clone(),
410 descriptions.clone(),
412 rustc_trans::init(&sess);
413 rustc_lint::register_builtins(&mut sess.lint_store.borrow_mut(), Some(&sess));
414 let mut cfg = config::build_configuration(&sess, cfg.clone());
415 target_features::add_configuration(&mut cfg, &sess);
416 sess.parse_sess.config = cfg;
418 RustcDefaultCalls::print_crate_info(&sess, None, odir, ofile);
420 if should_stop == Compilation::Stop {
423 early_error(sopts.error_format, "no input filename given");
425 1 => panic!("make_input should have provided valid inputs"),
426 _ => early_error(sopts.error_format, "multiple input filenames provided"),
430 fn late_callback(&mut self,
431 matches: &getopts::Matches,
434 odir: &Option<PathBuf>,
435 ofile: &Option<PathBuf>)
437 RustcDefaultCalls::print_crate_info(sess, Some(input), odir, ofile)
438 .and_then(|| RustcDefaultCalls::list_metadata(sess, matches, input))
441 fn build_controller(&mut self,
443 matches: &getopts::Matches)
444 -> CompileController<'a> {
445 let mut control = CompileController::basic();
447 if let Some((ppm, opt_uii)) = parse_pretty(sess, matches) {
448 if ppm.needs_ast_map(&opt_uii) {
449 control.after_hir_lowering.stop = Compilation::Stop;
451 control.after_parse.callback = box move |state| {
452 state.krate = Some(pretty::fold_crate(state.krate.take().unwrap(), ppm));
454 control.after_hir_lowering.callback = box move |state| {
455 pretty::print_after_hir_lowering(state.session,
456 state.hir_map.unwrap(),
457 state.analysis.unwrap(),
458 state.resolutions.unwrap(),
460 &state.expanded_crate.take().unwrap(),
461 state.crate_name.unwrap(),
463 state.arena.unwrap(),
464 state.arenas.unwrap(),
469 control.after_parse.stop = Compilation::Stop;
471 control.after_parse.callback = box move |state| {
472 let krate = pretty::fold_crate(state.krate.take().unwrap(), ppm);
473 pretty::print_after_parsing(state.session,
484 if sess.opts.debugging_opts.parse_only ||
485 sess.opts.debugging_opts.show_span.is_some() ||
486 sess.opts.debugging_opts.ast_json_noexpand {
487 control.after_parse.stop = Compilation::Stop;
490 if sess.opts.debugging_opts.no_analysis ||
491 sess.opts.debugging_opts.ast_json {
492 control.after_hir_lowering.stop = Compilation::Stop;
495 if !sess.opts.output_types.keys().any(|&i| i == OutputType::Exe ||
496 i == OutputType::Metadata) {
497 control.after_llvm.stop = Compilation::Stop;
500 if save_analysis(sess) {
501 control.after_analysis.callback = box |state| {
502 time(state.session.time_passes(), "save analysis", || {
503 save::process_crate(state.tcx.unwrap(),
504 state.expanded_crate.unwrap(),
505 state.analysis.unwrap(),
506 state.crate_name.unwrap(),
507 DumpHandler::new(save_analysis_format(state.session),
509 state.crate_name.unwrap()))
512 control.after_analysis.run_callback_on_error = true;
513 control.make_glob_map = resolve::MakeGlobMap::Yes;
516 if sess.print_fuel_crate.is_some() {
517 let old_callback = control.compilation_done.callback;
518 control.compilation_done.callback = box move |state| {
520 let sess = state.session;
521 println!("Fuel used by {}: {}",
522 sess.print_fuel_crate.as_ref().unwrap(),
523 sess.print_fuel.get());
530 fn save_analysis(sess: &Session) -> bool {
531 sess.opts.debugging_opts.save_analysis ||
532 sess.opts.debugging_opts.save_analysis_api
535 fn save_analysis_format(sess: &Session) -> save::Format {
536 if sess.opts.debugging_opts.save_analysis {
538 } else if sess.opts.debugging_opts.save_analysis_api {
539 save::Format::JsonApi
545 impl RustcDefaultCalls {
546 pub fn list_metadata(sess: &Session, matches: &getopts::Matches, input: &Input) -> Compilation {
547 let r = matches.opt_strs("Z");
548 if r.contains(&("ls".to_string())) {
550 &Input::File(ref ifile) => {
551 let path = &(*ifile);
552 let mut v = Vec::new();
553 locator::list_file_metadata(&sess.target.target,
555 sess.cstore.metadata_loader(),
558 println!("{}", String::from_utf8(v).unwrap());
560 &Input::Str { .. } => {
561 early_error(ErrorOutputType::default(), "cannot list metadata for stdin");
564 return Compilation::Stop;
567 return Compilation::Continue;
571 fn print_crate_info(sess: &Session,
572 input: Option<&Input>,
573 odir: &Option<PathBuf>,
574 ofile: &Option<PathBuf>)
576 if sess.opts.prints.is_empty() {
577 return Compilation::Continue;
580 let attrs = match input {
583 let result = parse_crate_attrs(sess, input);
585 Ok(attrs) => Some(attrs),
586 Err(mut parse_error) => {
588 return Compilation::Stop;
593 for req in &sess.opts.prints {
595 PrintRequest::TargetList => {
596 let mut targets = rustc_back::target::get_targets().collect::<Vec<String>>();
598 println!("{}", targets.join("\n"));
600 PrintRequest::Sysroot => println!("{}", sess.sysroot().display()),
601 PrintRequest::TargetSpec => println!("{}", sess.target.target.to_json().pretty()),
602 PrintRequest::FileNames |
603 PrintRequest::CrateName => {
604 let input = match input {
605 Some(input) => input,
606 None => early_error(ErrorOutputType::default(), "no input file provided"),
608 let attrs = attrs.as_ref().unwrap();
609 let t_outputs = driver::build_output_filenames(input, odir, ofile, attrs, sess);
610 let id = link::find_crate_name(Some(sess), attrs, input);
611 if *req == PrintRequest::CrateName {
615 let crate_types = driver::collect_crate_types(sess, attrs);
616 for &style in &crate_types {
617 let fname = link::filename_for_input(sess, style, &id, &t_outputs);
624 PrintRequest::Cfg => {
625 let allow_unstable_cfg = UnstableFeatures::from_environment()
628 let mut cfgs = Vec::new();
629 for &(name, ref value) in sess.parse_sess.config.iter() {
630 let gated_cfg = GatedCfg::gate(&ast::MetaItem {
632 node: ast::MetaItemKind::Word,
636 // Note that crt-static is a specially recognized cfg
637 // directive that's printed out here as part of
638 // rust-lang/rust#37406, but in general the
639 // `target_feature` cfg is gated under
640 // rust-lang/rust#29717. For now this is just
641 // specifically allowing the crt-static cfg and that's
642 // it, this is intended to get into Cargo and then go
643 // through to build scripts.
644 let value = value.as_ref().map(|s| s.as_str());
645 let value = value.as_ref().map(|s| s.as_ref());
646 if name != "target_feature" || value != Some("crt-static") {
647 if !allow_unstable_cfg && gated_cfg.is_some() {
652 cfgs.push(if let Some(value) = value {
653 format!("{}=\"{}\"", name, value)
664 PrintRequest::RelocationModels => {
665 println!("Available relocation models:");
666 for &(name, _) in RELOC_MODEL_ARGS.iter() {
667 println!(" {}", name);
671 PrintRequest::CodeModels => {
672 println!("Available code models:");
673 for &(name, _) in CODE_GEN_MODEL_ARGS.iter(){
674 println!(" {}", name);
678 PrintRequest::TargetCPUs | PrintRequest::TargetFeatures => {
679 rustc_trans::print(*req, sess);
683 return Compilation::Stop;
687 /// Returns a version string such as "0.12.0-dev".
688 pub fn release_str() -> Option<&'static str> {
689 option_env!("CFG_RELEASE")
692 /// Returns the full SHA1 hash of HEAD of the Git repo from which rustc was built.
693 pub fn commit_hash_str() -> Option<&'static str> {
694 option_env!("CFG_VER_HASH")
697 /// Returns the "commit date" of HEAD of the Git repo from which rustc was built as a static string.
698 pub fn commit_date_str() -> Option<&'static str> {
699 option_env!("CFG_VER_DATE")
702 /// Prints version information
703 pub fn version(binary: &str, matches: &getopts::Matches) {
704 let verbose = matches.opt_present("verbose");
708 option_env!("CFG_VERSION").unwrap_or("unknown version"));
710 fn unw(x: Option<&str>) -> &str {
711 x.unwrap_or("unknown")
713 println!("binary: {}", binary);
714 println!("commit-hash: {}", unw(commit_hash_str()));
715 println!("commit-date: {}", unw(commit_date_str()));
716 println!("host: {}", config::host_triple());
717 println!("release: {}", unw(release_str()));
718 rustc_trans::print_version();
722 fn usage(verbose: bool, include_unstable_options: bool) {
723 let groups = if verbose {
724 config::rustc_optgroups()
726 config::rustc_short_optgroups()
728 let mut options = getopts::Options::new();
729 for option in groups.iter().filter(|x| include_unstable_options || x.is_stable()) {
730 (option.apply)(&mut options);
732 let message = format!("Usage: rustc [OPTIONS] INPUT");
733 let extra_help = if verbose {
736 "\n --help -v Print the full set of options rustc accepts"
738 println!("{}\nAdditional help:
739 -C help Print codegen options
741 Print 'lint' options and default settings
742 -Z help Print internal \
743 options for debugging rustc{}\n",
744 options.usage(&message),
748 fn describe_lints(lint_store: &lint::LintStore, loaded_plugins: bool) {
750 Available lint options:
751 -W <foo> Warn about <foo>
755 -F <foo> Forbid <foo> \
756 (deny <foo> and all attempts to override)
760 fn sort_lints(lints: Vec<(&'static Lint, bool)>) -> Vec<&'static Lint> {
761 let mut lints: Vec<_> = lints.into_iter().map(|(x, _)| x).collect();
762 lints.sort_by(|x: &&Lint, y: &&Lint| {
763 match x.default_level.cmp(&y.default_level) {
764 // The sort doesn't case-fold but it's doubtful we care.
765 Equal => x.name.cmp(y.name),
772 fn sort_lint_groups(lints: Vec<(&'static str, Vec<lint::LintId>, bool)>)
773 -> Vec<(&'static str, Vec<lint::LintId>)> {
774 let mut lints: Vec<_> = lints.into_iter().map(|(x, y, _)| (x, y)).collect();
775 lints.sort_by(|&(x, _): &(&'static str, Vec<lint::LintId>),
776 &(y, _): &(&'static str, Vec<lint::LintId>)| {
782 let (plugin, builtin): (Vec<_>, _) = lint_store.get_lints()
785 .partition(|&(_, p)| p);
786 let plugin = sort_lints(plugin);
787 let builtin = sort_lints(builtin);
789 let (plugin_groups, builtin_groups): (Vec<_>, _) = lint_store.get_lint_groups()
792 .partition(|&(.., p)| p);
793 let plugin_groups = sort_lint_groups(plugin_groups);
794 let builtin_groups = sort_lint_groups(builtin_groups);
796 let max_name_len = plugin.iter()
798 .map(|&s| s.name.chars().count())
801 let padded = |x: &str| {
802 let mut s = repeat(" ")
803 .take(max_name_len - x.chars().count())
804 .collect::<String>();
809 println!("Lint checks provided by rustc:\n");
810 println!(" {} {:7.7} {}", padded("name"), "default", "meaning");
811 println!(" {} {:7.7} {}", padded("----"), "-------", "-------");
813 let print_lints = |lints: Vec<&Lint>| {
815 let name = lint.name_lower().replace("_", "-");
816 println!(" {} {:7.7} {}",
818 lint.default_level.as_str(),
824 print_lints(builtin);
828 let max_name_len = max("warnings".len(),
830 .chain(&builtin_groups)
831 .map(|&(s, _)| s.chars().count())
835 let padded = |x: &str| {
836 let mut s = repeat(" ")
837 .take(max_name_len - x.chars().count())
838 .collect::<String>();
843 println!("Lint groups provided by rustc:\n");
844 println!(" {} {}", padded("name"), "sub-lints");
845 println!(" {} {}", padded("----"), "---------");
846 println!(" {} {}", padded("warnings"), "all built-in lints");
848 let print_lint_groups = |lints: Vec<(&'static str, Vec<lint::LintId>)>| {
849 for (name, to) in lints {
850 let name = name.to_lowercase().replace("_", "-");
851 let desc = to.into_iter()
852 .map(|x| x.to_string().replace("_", "-"))
853 .collect::<Vec<String>>()
855 println!(" {} {}", padded(&name), desc);
860 print_lint_groups(builtin_groups);
862 match (loaded_plugins, plugin.len(), plugin_groups.len()) {
863 (false, 0, _) | (false, _, 0) => {
864 println!("Compiler plugins can provide additional lints and lint groups. To see a \
865 listing of these, re-run `rustc -W help` with a crate filename.");
867 (false, ..) => panic!("didn't load lint plugins but got them anyway!"),
868 (true, 0, 0) => println!("This crate does not load any lint plugins or lint groups."),
871 println!("Lint checks provided by plugins loaded by this crate:\n");
875 println!("Lint groups provided by plugins loaded by this crate:\n");
876 print_lint_groups(plugin_groups);
882 fn describe_debug_flags() {
883 println!("\nAvailable debug options:\n");
884 print_flag_list("-Z", config::DB_OPTIONS);
887 fn describe_codegen_flags() {
888 println!("\nAvailable codegen options:\n");
889 print_flag_list("-C", config::CG_OPTIONS);
892 fn print_flag_list<T>(cmdline_opt: &str,
893 flag_list: &[(&'static str, T, Option<&'static str>, &'static str)]) {
894 let max_len = flag_list.iter()
895 .map(|&(name, _, opt_type_desc, _)| {
896 let extra_len = match opt_type_desc {
900 name.chars().count() + extra_len
905 for &(name, _, opt_type_desc, desc) in flag_list {
906 let (width, extra) = match opt_type_desc {
907 Some(..) => (max_len - 4, "=val"),
908 None => (max_len, ""),
910 println!(" {} {:>width$}{} -- {}",
912 name.replace("_", "-"),
919 /// Process command line options. Emits messages as appropriate. If compilation
920 /// should continue, returns a getopts::Matches object parsed from args,
921 /// otherwise returns None.
923 /// The compiler's handling of options is a little complicated as it ties into
924 /// our stability story, and it's even *more* complicated by historical
925 /// accidents. The current intention of each compiler option is to have one of
928 /// 1. An option is stable and can be used everywhere.
929 /// 2. An option is unstable, but was historically allowed on the stable
931 /// 3. An option is unstable, and can only be used on nightly.
933 /// Like unstable library and language features, however, unstable options have
934 /// always required a form of "opt in" to indicate that you're using them. This
935 /// provides the easy ability to scan a code base to check to see if anything
936 /// unstable is being used. Currently, this "opt in" is the `-Z` "zed" flag.
938 /// All options behind `-Z` are considered unstable by default. Other top-level
939 /// options can also be considered unstable, and they were unlocked through the
940 /// `-Z unstable-options` flag. Note that `-Z` remains to be the root of
941 /// instability in both cases, though.
943 /// So with all that in mind, the comments below have some more detail about the
944 /// contortions done here to get things to work out correctly.
945 pub fn handle_options(args: &[String]) -> Option<getopts::Matches> {
946 // Throw away the first argument, the name of the binary
947 let args = &args[1..];
950 // user did not write `-v` nor `-Z unstable-options`, so do not
951 // include that extra information.
956 // Parse with *all* options defined in the compiler, we don't worry about
957 // option stability here we just want to parse as much as possible.
958 let mut options = getopts::Options::new();
959 for option in config::rustc_optgroups() {
960 (option.apply)(&mut options);
962 let matches = match options.parse(args) {
964 Err(f) => early_error(ErrorOutputType::default(), &f.to_string()),
967 // For all options we just parsed, we check a few aspects:
969 // * If the option is stable, we're all good
970 // * If the option wasn't passed, we're all good
971 // * If `-Z unstable-options` wasn't passed (and we're not a -Z option
972 // ourselves), then we require the `-Z unstable-options` flag to unlock
973 // this option that was passed.
974 // * If we're a nightly compiler, then unstable options are now unlocked, so
976 // * Otherwise, if we're a truly unstable option then we generate an error
977 // (unstable option being used on stable)
978 // * If we're a historically stable-but-should-be-unstable option then we
979 // emit a warning that we're going to turn this into an error soon.
980 nightly_options::check_nightly_options(&matches, &config::rustc_optgroups());
982 if matches.opt_present("h") || matches.opt_present("help") {
983 // Only show unstable options in --help if we *really* accept unstable
984 // options, which catches the case where we got `-Z unstable-options` on
985 // the stable channel of Rust which was accidentally allowed
987 usage(matches.opt_present("verbose"),
988 nightly_options::is_unstable_enabled(&matches));
992 // Don't handle -W help here, because we might first load plugins.
993 let r = matches.opt_strs("Z");
994 if r.iter().any(|x| *x == "help") {
995 describe_debug_flags();
999 let cg_flags = matches.opt_strs("C");
1000 if cg_flags.iter().any(|x| *x == "help") {
1001 describe_codegen_flags();
1005 if cg_flags.iter().any(|x| *x == "no-stack-check") {
1006 early_warn(ErrorOutputType::default(),
1007 "the --no-stack-check flag is deprecated and does nothing");
1010 if cg_flags.contains(&"passes=list".to_string()) {
1011 rustc_trans::print_passes();
1015 if matches.opt_present("version") {
1016 version("rustc", &matches);
1023 fn parse_crate_attrs<'a>(sess: &'a Session, input: &Input) -> PResult<'a, Vec<ast::Attribute>> {
1025 Input::File(ref ifile) => {
1026 parse::parse_crate_attrs_from_file(ifile, &sess.parse_sess)
1028 Input::Str { ref name, ref input } => {
1029 parse::parse_crate_attrs_from_source_str(name.clone(), input.clone(), &sess.parse_sess)
1034 /// Runs `f` in a suitable thread for running `rustc`; returns a
1035 /// `Result` with either the return value of `f` or -- if a panic
1036 /// occurs -- the panic value.
1037 pub fn in_rustc_thread<F, R>(f: F) -> Result<R, Box<Any + Send>>
1038 where F: FnOnce() -> R + Send + 'static,
1041 // Temporarily have stack size set to 16MB to deal with nom-using crates failing
1042 const STACK_SIZE: usize = 16 * 1024 * 1024; // 16MB
1044 let mut cfg = thread::Builder::new().name("rustc".to_string());
1046 // FIXME: Hacks on hacks. If the env is trying to override the stack size
1047 // then *don't* set it explicitly.
1048 if env::var_os("RUST_MIN_STACK").is_none() {
1049 cfg = cfg.stack_size(STACK_SIZE);
1052 let thread = cfg.spawn(f);
1053 thread.unwrap().join()
1056 /// Run a procedure which will detect panics in the compiler and print nicer
1057 /// error messages rather than just failing the test.
1059 /// The diagnostic emitter yielded to the procedure should be used for reporting
1060 /// errors of the compiler.
1061 pub fn monitor<F: FnOnce() + Send + 'static>(f: F) {
1062 struct Sink(Arc<Mutex<Vec<u8>>>);
1063 impl Write for Sink {
1064 fn write(&mut self, data: &[u8]) -> io::Result<usize> {
1065 Write::write(&mut *self.0.lock().unwrap(), data)
1067 fn flush(&mut self) -> io::Result<()> {
1072 let data = Arc::new(Mutex::new(Vec::new()));
1073 let err = Sink(data.clone());
1075 let result = in_rustc_thread(move || {
1076 io::set_panic(Some(box err));
1080 if let Err(value) = result {
1081 // Thread panicked without emitting a fatal diagnostic
1082 if !value.is::<errors::FatalError>() {
1084 Box::new(errors::emitter::EmitterWriter::stderr(errors::ColorConfig::Auto, None));
1085 let handler = errors::Handler::with_emitter(true, false, emitter);
1087 // a .span_bug or .bug call has already printed what
1088 // it wants to print.
1089 if !value.is::<errors::ExplicitBug>() {
1090 handler.emit(&MultiSpan::new(),
1092 errors::Level::Bug);
1095 let xs = ["the compiler unexpectedly panicked. this is a bug.".to_string(),
1096 format!("we would appreciate a bug report: {}", BUG_REPORT_URL),
1097 format!("rustc {} running on {}",
1098 option_env!("CFG_VERSION").unwrap_or("unknown_version"),
1099 config::host_triple())];
1101 handler.emit(&MultiSpan::new(),
1103 errors::Level::Note);
1105 if match env::var_os("RUST_BACKTRACE") {
1106 Some(val) => &val != "0",
1109 handler.emit(&MultiSpan::new(),
1110 "run with `RUST_BACKTRACE=1` for a backtrace",
1111 errors::Level::Note);
1114 writeln!(io::stderr(), "{}", str::from_utf8(&data.lock().unwrap()).unwrap()).unwrap();
1121 fn exit_on_err() -> ! {
1122 // Panic so the process returns a failure code, but don't pollute the
1123 // output with some unnecessary panic messages, we've already
1124 // printed everything that we needed to.
1125 io::set_panic(Some(box io::sink()));
1129 pub fn diagnostics_registry() -> errors::registry::Registry {
1130 use errors::registry::Registry;
1132 let mut all_errors = Vec::new();
1133 all_errors.extend_from_slice(&rustc::DIAGNOSTICS);
1134 all_errors.extend_from_slice(&rustc_typeck::DIAGNOSTICS);
1135 all_errors.extend_from_slice(&rustc_borrowck::DIAGNOSTICS);
1136 all_errors.extend_from_slice(&rustc_resolve::DIAGNOSTICS);
1137 all_errors.extend_from_slice(&rustc_privacy::DIAGNOSTICS);
1138 all_errors.extend_from_slice(&rustc_trans::DIAGNOSTICS);
1139 all_errors.extend_from_slice(&rustc_const_eval::DIAGNOSTICS);
1140 all_errors.extend_from_slice(&rustc_metadata::DIAGNOSTICS);
1142 Registry::new(&all_errors)
1145 fn get_args() -> Vec<String> {
1146 env::args_os().enumerate()
1147 .map(|(i, arg)| arg.into_string().unwrap_or_else(|arg| {
1148 early_error(ErrorOutputType::default(),
1149 &format!("Argument {} is not valid Unicode: {:?}", i, arg))
1155 env_logger::init().unwrap();
1156 let result = run(|| run_compiler(&get_args(),
1157 &mut RustcDefaultCalls,
1160 process::exit(result as i32);