Remap only source files in the command line

This commit is contained in:
Igor Matuszewski 2018-09-24 16:28:53 +02:00
parent d45f87701c
commit 1e593be593
3 changed files with 32 additions and 39 deletions

View File

@ -980,6 +980,7 @@ pub fn enable_save_analysis(control: &mut CompileController) {
state.expanded_crate.unwrap(),
state.analysis.unwrap(),
state.crate_name.unwrap(),
state.input,
None,
DumpHandler::new(state.out_dir,
state.crate_name.unwrap()))

View File

@ -25,12 +25,12 @@
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 std::fs;
use syntax::ast::{self, Attribute, NodeId, PatKind, CRATE_NODE_ID};
use syntax::parse::token;
@ -173,20 +173,13 @@ impl<'l, 'tcx: 'l, 'll, O: DumpOutput + 'll> DumpVisitor<'l, 'tcx, 'll, O> {
self.dumper.crate_prelude(data);
}
pub fn dump_compilation_options(&mut self, crate_name: &str) {
// Apply possible `remap-path-prefix` remapping to the raw command
let command = {
let mapping = self.tcx.sess.source_map().path_mapping();
let remap_arg = |x: &str| -> String {
match fs::canonicalize(x) {
Ok(path) => mapping.map_prefix(path).0.to_str().unwrap().to_owned(),
Err(_) => x.to_owned(), // Probably not a path, ignore
}
};
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();
// rustc args are guaranteed to be valid UTF-8 (checked early)
// 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);
@ -198,26 +191,30 @@ impl<'l, 'tcx: 'l, 'll, O: DumpOutput + 'll> DumpVisitor<'l, 'tcx, 'll, O> {
indices
};
let args = env::args()
let mut args = env::args()
.enumerate()
.filter(|(i, _)| !remap_arg_indices.contains(i))
.map(|(_, elem)| {
let mut arg = elem.splitn(2, '=');
match (arg.next(), arg.next()) {
// Apart from `--remap...`, in `a=b` args usually only
// `b` is a path (e.g. `--extern some_crate=/path/to..`)
(Some(a), Some(b)) => format!("{}={}", a, remap_arg(b)),
(Some(a), _) => remap_arg(a),
_ => unreachable!(),
.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,
}
}).collect::<Vec<_>>();
});
args.as_slice().join(" ")
(args.next().unwrap(), args.collect())
};
let data = CompilationOptions {
directory: self.tcx.sess.working_dir.0.clone(),
command,
program,
arguments,
output: self.save_ctxt.compilation_output(crate_name),
};

View File

@ -46,7 +46,7 @@ use rustc::hir::def::Def as HirDef;
use rustc::hir::Node;
use rustc::hir::def_id::{DefId, LOCAL_CRATE};
use rustc::middle::cstore::ExternCrate;
use rustc::session::config::{CrateType, OutputType};
use rustc::session::config::{CrateType, Input, OutputType};
use rustc::ty::{self, TyCtxt};
use rustc_typeck::hir_ty_to_ty;
use rustc_codegen_utils::link::{filename_for_metadata, out_filename};
@ -74,7 +74,7 @@ use span_utils::SpanUtils;
use rls_data::config::Config;
use rls_data::{
CrateSource, Def, DefKind, ExternalCrateData, GlobalCrateId, Impl, ImplKind, MacroRef, Ref,
Def, DefKind, ExternalCrateData, GlobalCrateId, Impl, ImplKind, MacroRef, Ref,
RefKind, Relation, RelationKind, SpanData,
};
@ -143,11 +143,7 @@ impl<'l, 'tcx: 'l> SaveContext<'l, 'tcx> {
continue;
}
};
let src = self.tcx.used_crate_source(n);
let lo_loc = self.span_utils.sess.source_map().lookup_char_pos(span.lo());
let map_prefix = |path: &PathBuf| -> PathBuf {
self.tcx.sess.source_map().path_mapping().map_prefix(path.to_owned()).0
};
result.push(ExternalCrateData {
// FIXME: change file_name field to PathBuf in rls-data
@ -158,11 +154,6 @@ impl<'l, 'tcx: 'l> SaveContext<'l, 'tcx> {
name: self.tcx.crate_name(n).to_string(),
disambiguator: self.tcx.crate_disambiguator(n).to_fingerprint().as_value(),
},
source: CrateSource {
dylib: src.dylib.as_ref().map(|(path, _)| map_prefix(path)),
rlib: src.rlib.as_ref().map(|(path, _)| map_prefix(path)),
rmeta: src.rmeta.as_ref().map(|(path, _)| map_prefix(path)),
}
});
}
@ -1046,6 +1037,7 @@ pub trait SaveHandler {
save_ctxt: SaveContext<'l, 'tcx>,
krate: &ast::Crate,
cratename: &str,
input: &'l Input,
);
}
@ -1111,13 +1103,14 @@ impl<'a> SaveHandler for DumpHandler<'a> {
save_ctxt: SaveContext<'l, 'tcx>,
krate: &ast::Crate,
cratename: &str,
input: &'l Input,
) {
let output = &mut self.output_file(&save_ctxt);
let mut dumper = JsonDumper::new(output, save_ctxt.config.clone());
let mut visitor = DumpVisitor::new(save_ctxt, &mut dumper);
visitor.dump_crate_info(cratename, krate);
visitor.dump_compilation_options(cratename);
visitor.dump_compilation_options(input, cratename);
visit::walk_crate(&mut visitor, krate);
}
}
@ -1133,6 +1126,7 @@ impl<'b> SaveHandler for CallbackHandler<'b> {
save_ctxt: SaveContext<'l, 'tcx>,
krate: &ast::Crate,
cratename: &str,
input: &'l Input,
) {
// We're using the JsonDumper here because it has the format of the
// save-analysis results that we will pass to the callback. IOW, we are
@ -1143,7 +1137,7 @@ impl<'b> SaveHandler for CallbackHandler<'b> {
let mut visitor = DumpVisitor::new(save_ctxt, &mut dumper);
visitor.dump_crate_info(cratename, krate);
visitor.dump_compilation_options(cratename);
visitor.dump_compilation_options(input, cratename);
visit::walk_crate(&mut visitor, krate);
}
}
@ -1153,6 +1147,7 @@ pub fn process_crate<'l, 'tcx, H: SaveHandler>(
krate: &ast::Crate,
analysis: &'l ty::CrateAnalysis,
cratename: &str,
input: &'l Input,
config: Option<Config>,
mut handler: H,
) {
@ -1170,7 +1165,7 @@ pub fn process_crate<'l, 'tcx, H: SaveHandler>(
impl_counter: Cell::new(0),
};
handler.save(save_ctxt, krate, cratename)
handler.save(save_ctxt, krate, cratename, input)
})
}