]> git.lizzy.rs Git - rust.git/blobdiff - src/driver.rs
Feed the dog
[rust.git] / src / driver.rs
index 359d2f8530cbbc8be025408ee5e7f02f629c9309..fda304afcbe184a3bc18d764e70f3ade2fc46df0 100644 (file)
@@ -1,16 +1,25 @@
 #![cfg_attr(feature = "deny-warnings", deny(warnings))]
+#![feature(result_map_or)]
 #![feature(rustc_private)]
 
 // FIXME: switch to something more ergonomic here, once available.
 // (Currently there is no way to opt into sysroot crates without `extern crate`.)
 #[allow(unused_extern_crates)]
+extern crate rustc;
+#[allow(unused_extern_crates)]
 extern crate rustc_driver;
 #[allow(unused_extern_crates)]
+extern crate rustc_errors;
+#[allow(unused_extern_crates)]
 extern crate rustc_interface;
 
+use rustc::ty::TyCtxt;
 use rustc_interface::interface;
 use rustc_tools_util::*;
 
+use lazy_static::lazy_static;
+use std::borrow::Cow;
+use std::panic;
 use std::path::{Path, PathBuf};
 use std::process::{exit, Command};
 
@@ -61,51 +70,26 @@ fn test_arg_value() {
 struct ClippyCallbacks;
 
 impl rustc_driver::Callbacks for ClippyCallbacks {
-    fn after_parsing(&mut self, compiler: &interface::Compiler) -> rustc_driver::Compilation {
-        let sess = compiler.session();
-        let mut registry = rustc_driver::plugin::registry::Registry::new(
-            sess,
-            compiler
-                .parse()
-                .expect(
-                    "at this compilation stage \
-                     the crate must be parsed",
-                )
-                .peek()
-                .span,
-        );
-        registry.args_hidden = Some(Vec::new());
-
-        let conf = clippy_lints::read_conf(&registry);
-        clippy_lints::register_plugins(&mut registry, &conf);
-
-        let rustc_driver::plugin::registry::Registry {
-            early_lint_passes,
-            late_lint_passes,
-            lint_groups,
-            llvm_passes,
-            attributes,
-            ..
-        } = registry;
-        let mut ls = sess.lint_store.borrow_mut();
-        for pass in early_lint_passes {
-            ls.register_early_pass(Some(sess), true, false, pass);
-        }
-        for pass in late_lint_passes {
-            ls.register_late_pass(Some(sess), true, false, false, pass);
-        }
-
-        for (name, (to, deprecated_name)) in lint_groups {
-            ls.register_group(Some(sess), true, name, deprecated_name, to);
-        }
-        clippy_lints::register_pre_expansion_lints(sess, &mut ls, &conf);
-        clippy_lints::register_renamed(&mut ls);
-
-        sess.plugin_llvm_passes.borrow_mut().extend(llvm_passes);
-        sess.plugin_attributes.borrow_mut().extend(attributes);
+    fn config(&mut self, config: &mut interface::Config) {
+        let previous = config.register_lints.take();
+        config.register_lints = Some(Box::new(move |sess, mut lint_store| {
+            // technically we're ~guaranteed that this is none but might as well call anything that
+            // is there already. Certainly it can't hurt.
+            if let Some(previous) = &previous {
+                (previous)(sess, lint_store);
+            }
 
-        // Continue execution
-        rustc_driver::Compilation::Continue
+            let conf = clippy_lints::read_conf(&[], &sess);
+            clippy_lints::register_plugins(&mut lint_store, &sess, &conf);
+            clippy_lints::register_pre_expansion_lints(&mut lint_store, &conf);
+            clippy_lints::register_renamed(&mut lint_store);
+        }));
+
+        // FIXME: #4825; This is required, because Clippy lints that are based on MIR have to be
+        // run on the unoptimized MIR. On the other hand this results in some false negatives. If
+        // MIR passes can be enabled / disabled separately, we should figure out, what passes to
+        // use for Clippy.
+        config.opts.debugging_opts.mir_opt_level = 0;
     }
 }
 
@@ -245,9 +229,64 @@ fn display_help() {
     );
 }
 
+const BUG_REPORT_URL: &str = "https://github.com/rust-lang/rust-clippy/issues/new";
+
+lazy_static! {
+    static ref ICE_HOOK: Box<dyn Fn(&panic::PanicInfo<'_>) + Sync + Send + 'static> = {
+        let hook = panic::take_hook();
+        panic::set_hook(Box::new(|info| report_clippy_ice(info, BUG_REPORT_URL)));
+        hook
+    };
+}
+
+fn report_clippy_ice(info: &panic::PanicInfo<'_>, bug_report_url: &str) {
+    // Invoke our ICE handler, which prints the actual panic message and optionally a backtrace
+    (*ICE_HOOK)(info);
+
+    // Separate the output with an empty line
+    eprintln!();
+
+    let emitter = Box::new(rustc_errors::emitter::EmitterWriter::stderr(
+        rustc_errors::ColorConfig::Auto,
+        None,
+        false,
+        false,
+        None,
+        false,
+    ));
+    let handler = rustc_errors::Handler::with_emitter(true, None, emitter);
+
+    // a .span_bug or .bug call has already printed what
+    // it wants to print.
+    if !info.payload().is::<rustc_errors::ExplicitBug>() {
+        let d = rustc_errors::Diagnostic::new(rustc_errors::Level::Bug, "unexpected panic");
+        handler.emit_diagnostic(&d);
+        handler.abort_if_errors_and_should_abort();
+    }
+
+    let version_info = rustc_tools_util::get_version_info!();
+
+    let xs: Vec<Cow<'static, str>> = vec![
+        "the compiler unexpectedly panicked. this is a bug.".into(),
+        format!("we would appreciate a bug report: {}", bug_report_url).into(),
+        format!("Clippy version: {}", version_info).into(),
+    ];
+
+    for note in &xs {
+        handler.note_without_error(&note);
+    }
+
+    // If backtraces are enabled, also print the query stack
+    let backtrace = std::env::var_os("RUST_BACKTRACE").map_or(false, |x| &x != "0");
+
+    if backtrace {
+        TyCtxt::try_print_query_stack(&handler);
+    }
+}
+
 pub fn main() {
     rustc_driver::init_rustc_env_logger();
-    rustc_driver::install_ice_hook();
+    lazy_static::initialize(&ICE_HOOK);
     exit(
         rustc_driver::catch_fatal_errors(move || {
             use std::env;
@@ -299,14 +338,14 @@ pub fn main() {
 
             // Setting RUSTC_WRAPPER causes Cargo to pass 'rustc' as the first argument.
             // We're invoking the compiler programmatically, so we ignore this/
-            let wrapper_mode = Path::new(&orig_args[1]).file_stem() == Some("rustc".as_ref());
+            let wrapper_mode = orig_args.get(1).map(Path::new).and_then(Path::file_stem) == Some("rustc".as_ref());
 
             if wrapper_mode {
                 // we still want to be able to invoke it normally though
                 orig_args.remove(1);
             }
 
-            if !wrapper_mode && std::env::args().any(|a| a == "--help" || a == "-h") {
+            if !wrapper_mode && (orig_args.iter().any(|a| a == "--help" || a == "-h") || orig_args.len() == 1) {
                 display_help();
                 exit(0);
             }
@@ -342,10 +381,9 @@ pub fn main() {
             };
 
             // this check ensures that dependencies are built but not linted and the final
-            // crate is
-            // linted but not built
-            let clippy_enabled = env::var("CLIPPY_TESTS").ok().map_or(false, |val| val == "true")
-                || arg_value(&orig_args, "--emit", |val| val.split(',').any(|e| e == "metadata")).is_some();
+            // crate is linted but not built
+            let clippy_enabled = env::var("CLIPPY_TESTS").map_or(false, |val| val == "true")
+                || arg_value(&orig_args, "--cap-lints", |val| val == "allow").is_none();
 
             if clippy_enabled {
                 args.extend_from_slice(&["--cfg".to_owned(), r#"feature="cargo-clippy""#.to_owned()]);