Skip to content

Commit

Permalink
rustc: Pass optional additional plugins to compile_input
Browse files Browse the repository at this point in the history
This provides a way for clients of the rustc library to add
their own features to the pipeline.
  • Loading branch information
brson authored and alexcrichton committed Jul 21, 2014
1 parent 1c3655b commit c88bf10
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 12 deletions.
14 changes: 9 additions & 5 deletions src/librustc/driver/driver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,8 @@ pub fn compile_input(sess: Session,
cfg: ast::CrateConfig,
input: &Input,
outdir: &Option<Path>,
output: &Option<Path>) {
output: &Option<Path>,
addl_plugins: Option<Plugins>) {
// We need nested scopes here, because the intermediate results can keep
// large chunks of memory alive and we want to free them as soon as
// possible to keep the peak memory usage low
Expand All @@ -85,7 +86,8 @@ pub fn compile_input(sess: Session,
let id = link::find_crate_name(Some(&sess), krate.attrs.as_slice(),
input);
let (expanded_crate, ast_map)
= match phase_2_configure_and_expand(&sess, krate, id.as_slice()) {
= match phase_2_configure_and_expand(&sess, krate, id.as_slice(),
addl_plugins) {
None => return,
Some(p) => p,
};
Expand Down Expand Up @@ -186,7 +188,8 @@ pub fn phase_1_parse_input(sess: &Session, cfg: ast::CrateConfig, input: &Input)
/// Returns `None` if we're aborting after handling -W help.
pub fn phase_2_configure_and_expand(sess: &Session,
mut krate: ast::Crate,
crate_name: &str)
crate_name: &str,
addl_plugins: Option<Plugins>)
-> Option<(ast::Crate, syntax::ast_map::Map)> {
let time_passes = sess.time_passes();

Expand All @@ -212,9 +215,10 @@ pub fn phase_2_configure_and_expand(sess: &Session,
krate = time(time_passes, "configuration 1", krate, |krate|
front::config::strip_unconfigured_items(krate));

let mut addl_plugins = Some(addl_plugins);
let Plugins { macros, registrars }
= time(time_passes, "plugin loading", (), |_|
plugin::load::load_plugins(sess, &krate));
plugin::load::load_plugins(sess, &krate, addl_plugins.take_unwrap()));

let mut registry = Registry::new(&krate);

Expand Down Expand Up @@ -697,7 +701,7 @@ pub fn pretty_print_input(sess: Session,
PpmExpanded | PpmExpandedIdentified | PpmTyped | PpmFlowGraph(_) => {
let (krate, ast_map)
= match phase_2_configure_and_expand(&sess, krate,
id.as_slice()) {
id.as_slice(), None) {
None => return,
Some(p) => p,
};
Expand Down
2 changes: 1 addition & 1 deletion src/librustc/driver/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ fn run_compiler(args: &[String]) {
return;
}

driver::compile_input(sess, cfg, &input, &odir, &ofile);
driver::compile_input(sess, cfg, &input, &odir, &ofile, None);
}

/// Prints version information and returns None on success or an error
Expand Down
2 changes: 1 addition & 1 deletion src/librustc/middle/typeck/infer/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ fn test_env(_test_name: &str,
let input = driver::StrInput(source_string.to_string());
let krate = driver::phase_1_parse_input(&sess, krate_config, &input);
let (krate, ast_map) =
driver::phase_2_configure_and_expand(&sess, krate, "test")
driver::phase_2_configure_and_expand(&sess, krate, "test", None)
.expect("phase 2 aborted");

// run just enough stuff to build a tcx:
Expand Down
18 changes: 16 additions & 2 deletions src/librustc/plugin/load.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,24 @@ impl<'a> PluginLoader<'a> {
}

/// Read plugin metadata and dynamically load registrar functions.
pub fn load_plugins(sess: &Session, krate: &ast::Crate) -> Plugins {
pub fn load_plugins(sess: &Session, krate: &ast::Crate,
addl_plugins: Option<Plugins>) -> Plugins {
let mut loader = PluginLoader::new(sess);
visit::walk_crate(&mut loader, krate, ());
loader.plugins

let mut plugins = loader.plugins;

match addl_plugins {
Some(addl_plugins) => {
// Add in the additional plugins requested by the frontend
let Plugins { macros: addl_macros, registrars: addl_registrars } = addl_plugins;
plugins.macros.push_all_move(addl_macros);
plugins.registrars.push_all_move(addl_registrars);
}
None => ()
}

return plugins;
}

// note that macros aren't expanded yet, and therefore macros can't add plugins.
Expand Down
2 changes: 1 addition & 1 deletion src/librustdoc/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ fn get_ast_and_resolve(cpath: &Path, libs: HashSet<Path>, cfgs: Vec<String>)
&input);

let (krate, ast_map)
= phase_2_configure_and_expand(&sess, krate, name.as_slice())
= phase_2_configure_and_expand(&sess, krate, name.as_slice(), None)
.expect("phase_2_configure_and_expand aborted in rustdoc!");

let driver::driver::CrateAnalysis {
Expand Down
4 changes: 2 additions & 2 deletions src/librustdoc/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ pub fn run(input: &str,
}));
let krate = driver::phase_1_parse_input(&sess, cfg, &input);
let (krate, _) = driver::phase_2_configure_and_expand(&sess, krate,
"rustdoc-test")
"rustdoc-test", None)
.expect("phase_2_configure_and_expand aborted in rustdoc!");

let ctx = box(GC) core::DocContext {
Expand Down Expand Up @@ -166,7 +166,7 @@ fn runtest(test: &str, cratename: &str, libs: HashSet<Path>, should_fail: bool,
let out = Some(outdir.path().clone());
let cfg = config::build_configuration(&sess);
let libdir = sess.target_filesearch().get_lib_path();
driver::compile_input(sess, cfg, &input, &out, &None);
driver::compile_input(sess, cfg, &input, &out, &None, None);

if no_run { return }

Expand Down

0 comments on commit c88bf10

Please sign in to comment.