Skip to content

Commit df102ef

Browse files
committed
rustc: Pass optional additional plugins to compile_input
This provides a way for clients of the rustc library to add their own features to the pipeline.
1 parent a232626 commit df102ef

File tree

6 files changed

+30
-12
lines changed

6 files changed

+30
-12
lines changed

src/librustc/driver/driver.rs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,8 @@ pub fn compile_input(sess: Session,
6969
cfg: ast::CrateConfig,
7070
input: &Input,
7171
outdir: &Option<Path>,
72-
output: &Option<Path>) {
72+
output: &Option<Path>,
73+
addl_plugins: Option<Plugins>) {
7374
// We need nested scopes here, because the intermediate results can keep
7475
// large chunks of memory alive and we want to free them as soon as
7576
// possible to keep the peak memory usage low
@@ -85,7 +86,8 @@ pub fn compile_input(sess: Session,
8586
let id = link::find_crate_name(Some(&sess), krate.attrs.as_slice(),
8687
input);
8788
let (expanded_crate, ast_map)
88-
= match phase_2_configure_and_expand(&sess, krate, id.as_slice()) {
89+
= match phase_2_configure_and_expand(&sess, krate, id.as_slice(),
90+
addl_plugins) {
8991
None => return,
9092
Some(p) => p,
9193
};
@@ -186,7 +188,8 @@ pub fn phase_1_parse_input(sess: &Session, cfg: ast::CrateConfig, input: &Input)
186188
/// Returns `None` if we're aborting after handling -W help.
187189
pub fn phase_2_configure_and_expand(sess: &Session,
188190
mut krate: ast::Crate,
189-
crate_name: &str)
191+
crate_name: &str,
192+
addl_plugins: Option<Plugins>)
190193
-> Option<(ast::Crate, syntax::ast_map::Map)> {
191194
let time_passes = sess.time_passes();
192195

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

218+
let mut addl_plugins = Some(addl_plugins);
215219
let Plugins { macros, registrars }
216220
= time(time_passes, "plugin loading", (), |_|
217-
plugin::load::load_plugins(sess, &krate));
221+
plugin::load::load_plugins(sess, &krate, addl_plugins.take_unwrap()));
218222

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

@@ -697,7 +701,7 @@ pub fn pretty_print_input(sess: Session,
697701
PpmExpanded | PpmExpandedIdentified | PpmTyped | PpmFlowGraph(_) => {
698702
let (krate, ast_map)
699703
= match phase_2_configure_and_expand(&sess, krate,
700-
id.as_slice()) {
704+
id.as_slice(), None) {
701705
None => return,
702706
Some(p) => p,
703707
};

src/librustc/driver/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ fn run_compiler(args: &[String]) {
124124
return;
125125
}
126126

127-
driver::compile_input(sess, cfg, &input, &odir, &ofile);
127+
driver::compile_input(sess, cfg, &input, &odir, &ofile, None);
128128
}
129129

130130
/// Prints version information and returns None on success or an error

src/librustc/middle/typeck/infer/test.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ fn test_env(_test_name: &str,
117117
let input = driver::StrInput(source_string.to_string());
118118
let krate = driver::phase_1_parse_input(&sess, krate_config, &input);
119119
let (krate, ast_map) =
120-
driver::phase_2_configure_and_expand(&sess, krate, "test")
120+
driver::phase_2_configure_and_expand(&sess, krate, "test", None)
121121
.expect("phase 2 aborted");
122122

123123
// run just enough stuff to build a tcx:

src/librustc/plugin/load.rs

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,10 +66,24 @@ impl<'a> PluginLoader<'a> {
6666
}
6767

6868
/// Read plugin metadata and dynamically load registrar functions.
69-
pub fn load_plugins(sess: &Session, krate: &ast::Crate) -> Plugins {
69+
pub fn load_plugins(sess: &Session, krate: &ast::Crate,
70+
addl_plugins: Option<Plugins>) -> Plugins {
7071
let mut loader = PluginLoader::new(sess);
7172
visit::walk_crate(&mut loader, krate, ());
72-
loader.plugins
73+
74+
let mut plugins = loader.plugins;
75+
76+
match addl_plugins {
77+
Some(addl_plugins) => {
78+
// Add in the additional plugins requested by the frontend
79+
let Plugins { macros: addl_macros, registrars: addl_registrars } = addl_plugins;
80+
plugins.macros.push_all_move(addl_macros);
81+
plugins.registrars.push_all_move(addl_registrars);
82+
}
83+
None => ()
84+
}
85+
86+
return plugins;
7387
}
7488

7589
// note that macros aren't expanded yet, and therefore macros can't add plugins.

src/librustdoc/core.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ fn get_ast_and_resolve(cpath: &Path, libs: HashSet<Path>, cfgs: Vec<String>)
121121
&input);
122122

123123
let (krate, ast_map)
124-
= phase_2_configure_and_expand(&sess, krate, name.as_slice())
124+
= phase_2_configure_and_expand(&sess, krate, name.as_slice(), None)
125125
.expect("phase_2_configure_and_expand aborted in rustdoc!");
126126

127127
let driver::driver::CrateAnalysis {

src/librustdoc/test.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ pub fn run(input: &str,
6969
}));
7070
let krate = driver::phase_1_parse_input(&sess, cfg, &input);
7171
let (krate, _) = driver::phase_2_configure_and_expand(&sess, krate,
72-
"rustdoc-test")
72+
"rustdoc-test", None)
7373
.expect("phase_2_configure_and_expand aborted in rustdoc!");
7474

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

171171
if no_run { return }
172172

0 commit comments

Comments
 (0)