]> git.lizzy.rs Git - rust.git/blobdiff - src/librustc_save_analysis/dump_visitor.rs
Rollup merge of #54488 - zackmdavis:and_the_case_of_the_unused_crate, r=estebank
[rust.git] / src / librustc_save_analysis / dump_visitor.rs
index adc63497daf2addfff96421cf82f16309379ecad..ab8f468672944eb070864a0b9df3257badcb85be 100644 (file)
 
 use rustc::hir::def::Def as HirDef;
 use rustc::hir::def_id::DefId;
+use rustc::session::config::Input;
 use rustc::ty::{self, TyCtxt};
 use rustc_data_structures::fx::FxHashSet;
 
 use std::path::Path;
+use std::env;
 
 use syntax::ast::{self, Attribute, NodeId, PatKind, CRATE_NODE_ID};
 use syntax::parse::token;
@@ -49,8 +51,8 @@
 use span_utils::SpanUtils;
 use sig;
 
-use rls_data::{CratePreludeData, Def, DefKind, GlobalCrateId, Import, ImportKind, Ref, RefKind,
-               Relation, RelationKind, SpanData};
+use rls_data::{CompilationOptions, CratePreludeData, Def, DefKind, GlobalCrateId, Import,
+               ImportKind, Ref, RefKind, Relation, RelationKind, SpanData};
 
 macro_rules! down_cast_data {
     ($id:ident, $kind:ident, $sp:expr) => {
@@ -147,9 +149,9 @@ pub fn dump_crate_info(&mut self, name: &str, krate: &ast::Crate) {
         let crate_root = source_file.map(|source_file| {
             let source_file = Path::new(source_file);
             match source_file.file_name() {
-                Some(_) => source_file.parent().unwrap().display().to_string(),
-                None => source_file.display().to_string(),
-            }
+                Some(_) => source_file.parent().unwrap().display(),
+                None => source_file.display(),
+            }.to_string()
         });
 
         let data = CratePreludeData {
@@ -169,6 +171,54 @@ pub fn dump_crate_info(&mut self, name: &str, krate: &ast::Crate) {
         self.dumper.crate_prelude(data);
     }
 
+    pub fn dump_compilation_options(&mut self, input: &Input, crate_name: &str) {
+        // Apply possible `remap-path-prefix` remapping to the input source file
+        // (and don't include remapping args anymore)
+        let (program, arguments) = {
+            let remap_arg_indices = {
+                let mut indices = FxHashSet();
+                // Args are guaranteed to be valid UTF-8 (checked early)
+                for (i, e) in env::args().enumerate() {
+                    if e.starts_with("--remap-path-prefix=") {
+                        indices.insert(i);
+                    } else if e == "--remap-path-prefix" {
+                        indices.insert(i);
+                        indices.insert(i + 1);
+                    }
+                }
+                indices
+            };
+
+            let mut args = env::args()
+                .enumerate()
+                .filter(|(i, _)| !remap_arg_indices.contains(i))
+                .map(|(_, arg)| {
+                    match input {
+                        Input::File(ref path) if path == Path::new(&arg) => {
+                            let mapped = &self.tcx.sess.local_crate_source_file;
+                            mapped
+                                .as_ref()
+                                .unwrap()
+                                .to_string_lossy()
+                                .into()
+                        },
+                        _ => arg,
+                    }
+                });
+
+            (args.next().unwrap(), args.collect())
+        };
+
+        let data = CompilationOptions {
+            directory: self.tcx.sess.working_dir.0.clone(),
+            program,
+            arguments,
+            output: self.save_ctxt.compilation_output(crate_name),
+        };
+
+        self.dumper.compilation_opts(data);
+    }
+
     // Return all non-empty prefixes of a path.
     // For each prefix, we return the span for the last segment in the prefix and
     // a str representation of the entire prefix.
@@ -176,8 +226,8 @@ fn process_path_prefixes(&self, path: &ast::Path) -> Vec<(Span, String)> {
         let segments = &path.segments[if path.is_global() { 1 } else { 0 }..];
 
         let mut result = Vec::with_capacity(segments.len());
+        let mut segs = Vec::with_capacity(segments.len());
 
-        let mut segs = vec![];
         for (i, seg) in segments.iter().enumerate() {
             segs.push(seg.clone());
             let sub_path = ast::Path {
@@ -591,9 +641,7 @@ fn process_enum(
 
         for variant in &enum_definition.variants {
             let name = variant.node.ident.name.to_string();
-            let mut qualname = enum_data.qualname.clone();
-            qualname.push_str("::");
-            qualname.push_str(&name);
+            let qualname = format!("{}::{}", enum_data.qualname, name);
 
             match variant.node.data {
                 ast::VariantData::Struct(ref fields, _) => {
@@ -973,9 +1021,9 @@ fn process_var_decl_multi(&mut self, pats: &'l [P<ast::Pat>]) {
             match self.save_ctxt.get_path_def(id) {
                 HirDef::Local(id) => {
                     let mut value = if immut == ast::Mutability::Immutable {
-                        self.span.snippet(ident.span).to_string()
+                        self.span.snippet(ident.span)
                     } else {
-                        "<mutable>".to_string()
+                        "<mutable>".to_owned()
                     };
                     let hir_id = self.tcx.hir.node_to_hir_id(id);
                     let typ = self.save_ctxt
@@ -1103,10 +1151,9 @@ fn process_var_decl(&mut self, p: &'l ast::Pat, value: String) {
     /// mac_uses and mac_defs sets to prevent multiples.
     fn process_macro_use(&mut self, span: Span) {
         let source_span = span.source_callsite();
-        if self.macro_calls.contains(&source_span) {
+        if !self.macro_calls.insert(source_span) {
             return;
         }
-        self.macro_calls.insert(source_span);
 
         let data = match self.save_ctxt.get_macro_use_data(span) {
             None => return,
@@ -1608,8 +1655,7 @@ fn visit_expr(&mut self, ex: &'l ast::Expr) {
                 }
             }
             ast::ExprKind::Closure(_, _, _, ref decl, ref body, _fn_decl_span) => {
-                let mut id = String::from("$");
-                id.push_str(&ex.id.to_string());
+                let id = format!("${}", ex.id);
 
                 // walk arg and return types
                 for arg in &decl.inputs {