Skip to content

Commit 84d93a4

Browse files
committed
Use a config file with save-analysis
Replaces the output path env var. Can be passed to save-analysis via a function call or env var.
1 parent 504328a commit 84d93a4

File tree

4 files changed

+62
-32
lines changed

4 files changed

+62
-32
lines changed

src/Cargo.lock

Lines changed: 11 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/librustc_driver/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -578,6 +578,7 @@ impl<'a> CompilerCalls<'a> for RustcDefaultCalls {
578578
state.expanded_crate.unwrap(),
579579
state.analysis.unwrap(),
580580
state.crate_name.unwrap(),
581+
None,
581582
DumpHandler::new(save_analysis_format(state.session),
582583
state.out_dir,
583584
state.crate_name.unwrap()))

src/librustc_save_analysis/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ rustc = { path = "../librustc" }
1414
rustc_typeck = { path = "../librustc_typeck" }
1515
syntax = { path = "../libsyntax" }
1616
syntax_pos = { path = "../libsyntax_pos" }
17-
rls-data = "0.7"
17+
rls-data = "0.9"
1818
rls-span = "0.4"
1919
# FIXME(#40527) should move rustc serialize out of tree
2020
rustc-serialize = "0.3"

src/librustc_save_analysis/lib.rs

Lines changed: 49 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,10 @@ use rustc::hir::def::Def as HirDef;
4343
use rustc::hir::map::{Node, NodeItem};
4444
use rustc::hir::def_id::DefId;
4545
use rustc::session::config::CrateType::CrateTypeExecutable;
46-
use rustc::session::Session;
4746
use rustc::ty::{self, TyCtxt};
4847
use rustc_typeck::hir_ty_to_ty;
4948

49+
use std::default::Default;
5050
use std::env;
5151
use std::fs::File;
5252
use std::path::{Path, PathBuf};
@@ -68,13 +68,15 @@ use span_utils::SpanUtils;
6868

6969
use rls_data::{Ref, RefKind, SpanData, MacroRef, Def, DefKind, Relation, RelationKind,
7070
ExternalCrateData, Import, CratePreludeData};
71+
use rls_data::config::Config;
7172

7273

7374
pub struct SaveContext<'l, 'tcx: 'l> {
7475
tcx: TyCtxt<'l, 'tcx, 'tcx>,
7576
tables: &'l ty::TypeckTables<'tcx>,
7677
analysis: &'l ty::CrateAnalysis,
7778
span_utils: SpanUtils<'tcx>,
79+
config: Config,
7880
}
7981

8082
#[derive(Debug)]
@@ -900,39 +902,41 @@ impl<'a> DumpHandler<'a> {
900902
}
901903
}
902904

903-
fn output_file(&self, sess: &Session) -> File {
904-
let mut root_path = match env::var_os("RUST_SAVE_ANALYSIS_FOLDER") {
905-
Some(val) => PathBuf::from(val),
906-
None => match self.odir {
907-
Some(val) => val.join("save-analysis"),
908-
None => PathBuf::from("save-analysis-temp"),
909-
},
910-
};
905+
fn output_file(&self, ctx: &SaveContext) -> File {
906+
let sess = &ctx.tcx.sess;
907+
let file_name = match ctx.config.output_file {
908+
Some(ref s) => PathBuf::from(s),
909+
None => {
910+
let mut root_path = match self.odir {
911+
Some(val) => val.join("save-analysis"),
912+
None => PathBuf::from("save-analysis-temp"),
913+
};
911914

912-
if let Err(e) = std::fs::create_dir_all(&root_path) {
913-
error!("Could not create directory {}: {}", root_path.display(), e);
914-
}
915+
if let Err(e) = std::fs::create_dir_all(&root_path) {
916+
error!("Could not create directory {}: {}", root_path.display(), e);
917+
}
915918

916-
{
917-
let disp = root_path.display();
918-
info!("Writing output to {}", disp);
919-
}
919+
let executable = sess.crate_types.borrow().iter().any(|ct| *ct == CrateTypeExecutable);
920+
let mut out_name = if executable {
921+
"".to_owned()
922+
} else {
923+
"lib".to_owned()
924+
};
925+
out_name.push_str(&self.cratename);
926+
out_name.push_str(&sess.opts.cg.extra_filename);
927+
out_name.push_str(self.format.extension());
928+
root_path.push(&out_name);
920929

921-
let executable = sess.crate_types.borrow().iter().any(|ct| *ct == CrateTypeExecutable);
922-
let mut out_name = if executable {
923-
"".to_owned()
924-
} else {
925-
"lib".to_owned()
930+
root_path
931+
}
926932
};
927-
out_name.push_str(&self.cratename);
928-
out_name.push_str(&sess.opts.cg.extra_filename);
929-
out_name.push_str(self.format.extension());
930-
root_path.push(&out_name);
931-
let output_file = File::create(&root_path).unwrap_or_else(|e| {
932-
let disp = root_path.display();
933-
sess.fatal(&format!("Could not open {}: {}", disp, e));
933+
934+
info!("Writing output to {}", file_name.display());
935+
936+
let output_file = File::create(&file_name).unwrap_or_else(|e| {
937+
sess.fatal(&format!("Could not open {}: {}", file_name.display(), e))
934938
});
935-
root_path.pop();
939+
936940
output_file
937941
}
938942
}
@@ -952,7 +956,7 @@ impl<'a> SaveHandler for DumpHandler<'a> {
952956
}}
953957
}
954958

955-
let output = &mut self.output_file(&save_ctxt.tcx.sess);
959+
let output = &mut self.output_file(&save_ctxt);
956960

957961
match self.format {
958962
Format::Json => dump!(JsonDumper::new(output)),
@@ -994,6 +998,7 @@ pub fn process_crate<'l, 'tcx, H: SaveHandler>(tcx: TyCtxt<'l, 'tcx, 'tcx>,
994998
krate: &ast::Crate,
995999
analysis: &'l ty::CrateAnalysis,
9961000
cratename: &str,
1001+
config: Option<Config>,
9971002
mut handler: H) {
9981003
let _ignore = tcx.dep_graph.in_ignore();
9991004

@@ -1006,11 +1011,25 @@ pub fn process_crate<'l, 'tcx, H: SaveHandler>(tcx: TyCtxt<'l, 'tcx, 'tcx>,
10061011
tables: &ty::TypeckTables::empty(),
10071012
analysis: analysis,
10081013
span_utils: SpanUtils::new(&tcx.sess),
1014+
config: find_config(config),
10091015
};
10101016

10111017
handler.save(save_ctxt, krate, cratename)
10121018
}
10131019

1020+
fn find_config(supplied: Option<Config>) -> Config {
1021+
if let Some(config) = supplied {
1022+
return config;
1023+
}
1024+
match env::var_os("RUST_SAVE_ANALYSIS_CONFIG") {
1025+
Some(config_string) => {
1026+
rustc_serialize::json::decode(config_string.to_str().unwrap())
1027+
.expect("Could not deserialize save-analysis config")
1028+
},
1029+
None => Config::default(),
1030+
}
1031+
}
1032+
10141033
// Utility functions for the module.
10151034

10161035
// Helper function to escape quotes in a string

0 commit comments

Comments
 (0)