Skip to content

Commit c41cafb

Browse files
committed
librustc_driver: Add support for loading plugins via command line (fixes #15446)
1 parent 9f5f706 commit c41cafb

File tree

5 files changed

+26
-23
lines changed

5 files changed

+26
-23
lines changed

src/librustc/metadata/creader.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -516,7 +516,7 @@ impl<'a> CrateReader<'a> {
516516
}
517517
}
518518

519-
#[deriving(Copy)]
519+
#[derive(Copy)]
520520
pub enum CrateOrString<'a> {
521521
Krate(&'a ast::ViewItem),
522522
Str(&'a str)

src/librustc/plugin/load.rs

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,11 @@ pub struct Plugins {
4444
pub registrars: Vec<PluginRegistrar>,
4545
}
4646

47-
struct PluginLoader<'a> {
47+
pub struct PluginLoader<'a> {
4848
sess: &'a Session,
4949
span_whitelist: HashSet<Span>,
5050
reader: CrateReader<'a>,
51-
plugins: Plugins,
51+
pub plugins: Plugins,
5252
}
5353

5454
impl<'a> PluginLoader<'a> {
@@ -67,7 +67,7 @@ impl<'a> PluginLoader<'a> {
6767

6868
/// Read plugin metadata and dynamically load registrar functions.
6969
pub fn load_plugins(sess: &Session, krate: &ast::Crate,
70-
addl_plugins: Option<Plugins>) -> Plugins {
70+
addl_plugins: Option<Vec<String>>) -> Plugins {
7171
let mut loader = PluginLoader::new(sess);
7272

7373
// We need to error on `#[macro_use] extern crate` when it isn't at the
@@ -79,19 +79,14 @@ pub fn load_plugins(sess: &Session, krate: &ast::Crate,
7979

8080
visit::walk_crate(&mut loader, krate);
8181

82-
let mut plugins = loader.plugins;
83-
84-
match addl_plugins {
85-
Some(addl_plugins) => {
86-
// Add in the additional plugins requested by the frontend
87-
let Plugins { macros: addl_macros, registrars: addl_registrars } = addl_plugins;
88-
plugins.macros.extend(addl_macros.into_iter());
89-
plugins.registrars.extend(addl_registrars.into_iter());
82+
if let Some(plugins) = addl_plugins {
83+
for plugin in plugins.iter() {
84+
loader.load_plugin(CrateOrString::Str(plugin.as_slice()),
85+
None, None, None)
9086
}
91-
None => ()
9287
}
9388

94-
return plugins;
89+
return loader.plugins;
9590
}
9691

9792
// note that macros aren't expanded yet, and therefore macros can't add plugins.
@@ -160,7 +155,7 @@ impl<'a, 'v> Visitor<'v> for PluginLoader<'a> {
160155
}
161156
}
162157

163-
self.load_plugin(CrateOrString::Krate(vi), plugin_attr, macro_selection, reexport)
158+
self.load_plugin(CrateOrString::Krate(vi), plugin_attr, macro_selection, Some(reexport))
164159
}
165160

166161
fn visit_mac(&mut self, _: &ast::Mac) {
@@ -174,13 +169,13 @@ impl<'a> PluginLoader<'a> {
174169
c: CrateOrString<'b>,
175170
plugin_attr: Option<P<ast::MetaItem>>,
176171
macro_selection: Option<HashSet<token::InternedString>>,
177-
reexport: HashSet<token::InternedString>) {
172+
reexport: Option<HashSet<token::InternedString>>) {
178173
let mut macros = vec![];
179174
let mut registrar = None;
180175

181-
let load_macros = match macro_selection.as_ref() {
182-
Some(sel) => sel.len() != 0 || reexport.len() != 0,
183-
None => true,
176+
let load_macros = match (macro_selection.as_ref(), reexport.as_ref()) {
177+
(Some(sel), Some(re)) => sel.len() != 0 || re.len() != 0,
178+
_ => true,
184179
};
185180
let load_registrar = plugin_attr.is_some();
186181

@@ -207,7 +202,11 @@ impl<'a> PluginLoader<'a> {
207202
None => true,
208203
Some(sel) => sel.contains(&name),
209204
};
210-
def.export = reexport.contains(&name);
205+
def.export = if let Some(ref re) = reexport {
206+
re.contains(&name)
207+
} else {
208+
false // Don't reexport macros from crates loaded from the command line
209+
};
211210
self.plugins.macros.push(def);
212211
}
213212

src/librustc/session/config.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -574,6 +574,8 @@ options! {DebuggingOptions, DebuggingSetter, basic_debugging_options,
574574
"Run all passes except translation; no output"),
575575
no_analysis: bool = (false, parse_bool,
576576
"Parse and expand the source, but run no analysis"),
577+
extra_plugins: Vec<String> = (Vec::new(), parse_list,
578+
"load extra plugins"),
577579
unstable_options: bool = (false, parse_bool,
578580
"Adds unstable command line options to rustc interface"),
579581
print_enum_sizes: bool = (false, parse_bool,

src/librustc_driver/driver.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ pub fn compile_input(sess: Session,
4747
input: &Input,
4848
outdir: &Option<Path>,
4949
output: &Option<Path>,
50-
addl_plugins: Option<Plugins>) {
50+
addl_plugins: Option<Vec<String>>) {
5151
// We need nested scopes here, because the intermediate results can keep
5252
// large chunks of memory alive and we want to free them as soon as
5353
// possible to keep the peak memory usage low
@@ -166,7 +166,7 @@ pub fn phase_1_parse_input(sess: &Session, cfg: ast::CrateConfig, input: &Input)
166166
pub fn phase_2_configure_and_expand(sess: &Session,
167167
mut krate: ast::Crate,
168168
crate_name: &str,
169-
addl_plugins: Option<Plugins>)
169+
addl_plugins: Option<Vec<String>>)
170170
-> Option<ast::Crate> {
171171
let time_passes = sess.time_passes();
172172

src/librustc_driver/lib.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ use rustc::session::config::{Input, PrintRequest, UnstableFeatures};
5353
use rustc::lint::Lint;
5454
use rustc::lint;
5555
use rustc::metadata;
56+
use rustc::metadata::creader::CrateOrString::Str;
5657
use rustc::DIAGNOSTICS;
5758

5859
use std::cmp::Ordering::Equal;
@@ -185,7 +186,8 @@ fn run_compiler(args: &[String]) {
185186
return;
186187
}
187188

188-
driver::compile_input(sess, cfg, &input, &odir, &ofile, None);
189+
let plugins = sess.opts.debugging_opts.extra_plugins.clone();
190+
driver::compile_input(sess, cfg, &input, &odir, &ofile, Some(plugins));
189191
}
190192

191193
pub fn get_unstable_features_setting() -> UnstableFeatures {

0 commit comments

Comments
 (0)