librustc_driver: Add support for loading plugins via command line (fixes )

This commit is contained in:
Manish Goregaokar 2014-12-09 22:55:37 +05:30
parent 9f5f706f96
commit c41cafb10c
5 changed files with 26 additions and 23 deletions
src
librustc
metadata
plugin
session
librustc_driver

@ -516,7 +516,7 @@ impl<'a> CrateReader<'a> {
}
}
#[deriving(Copy)]
#[derive(Copy)]
pub enum CrateOrString<'a> {
Krate(&'a ast::ViewItem),
Str(&'a str)

@ -44,11 +44,11 @@ pub struct Plugins {
pub registrars: Vec<PluginRegistrar>,
}
struct PluginLoader<'a> {
pub struct PluginLoader<'a> {
sess: &'a Session,
span_whitelist: HashSet<Span>,
reader: CrateReader<'a>,
plugins: Plugins,
pub plugins: Plugins,
}
impl<'a> PluginLoader<'a> {
@ -67,7 +67,7 @@ impl<'a> PluginLoader<'a> {
/// Read plugin metadata and dynamically load registrar functions.
pub fn load_plugins(sess: &Session, krate: &ast::Crate,
addl_plugins: Option<Plugins>) -> Plugins {
addl_plugins: Option<Vec<String>>) -> Plugins {
let mut loader = PluginLoader::new(sess);
// 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,
visit::walk_crate(&mut loader, krate);
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.extend(addl_macros.into_iter());
plugins.registrars.extend(addl_registrars.into_iter());
if let Some(plugins) = addl_plugins {
for plugin in plugins.iter() {
loader.load_plugin(CrateOrString::Str(plugin.as_slice()),
None, None, None)
}
None => ()
}
return plugins;
return loader.plugins;
}
// 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> {
}
}
self.load_plugin(CrateOrString::Krate(vi), plugin_attr, macro_selection, reexport)
self.load_plugin(CrateOrString::Krate(vi), plugin_attr, macro_selection, Some(reexport))
}
fn visit_mac(&mut self, _: &ast::Mac) {
@ -174,13 +169,13 @@ impl<'a> PluginLoader<'a> {
c: CrateOrString<'b>,
plugin_attr: Option<P<ast::MetaItem>>,
macro_selection: Option<HashSet<token::InternedString>>,
reexport: HashSet<token::InternedString>) {
reexport: Option<HashSet<token::InternedString>>) {
let mut macros = vec![];
let mut registrar = None;
let load_macros = match macro_selection.as_ref() {
Some(sel) => sel.len() != 0 || reexport.len() != 0,
None => true,
let load_macros = match (macro_selection.as_ref(), reexport.as_ref()) {
(Some(sel), Some(re)) => sel.len() != 0 || re.len() != 0,
_ => true,
};
let load_registrar = plugin_attr.is_some();
@ -207,7 +202,11 @@ impl<'a> PluginLoader<'a> {
None => true,
Some(sel) => sel.contains(&name),
};
def.export = reexport.contains(&name);
def.export = if let Some(ref re) = reexport {
re.contains(&name)
} else {
false // Don't reexport macros from crates loaded from the command line
};
self.plugins.macros.push(def);
}

@ -574,6 +574,8 @@ options! {DebuggingOptions, DebuggingSetter, basic_debugging_options,
"Run all passes except translation; no output"),
no_analysis: bool = (false, parse_bool,
"Parse and expand the source, but run no analysis"),
extra_plugins: Vec<String> = (Vec::new(), parse_list,
"load extra plugins"),
unstable_options: bool = (false, parse_bool,
"Adds unstable command line options to rustc interface"),
print_enum_sizes: bool = (false, parse_bool,

@ -47,7 +47,7 @@ pub fn compile_input(sess: Session,
input: &Input,
outdir: &Option<Path>,
output: &Option<Path>,
addl_plugins: Option<Plugins>) {
addl_plugins: Option<Vec<String>>) {
// 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
@ -166,7 +166,7 @@ pub fn phase_1_parse_input(sess: &Session, cfg: ast::CrateConfig, input: &Input)
pub fn phase_2_configure_and_expand(sess: &Session,
mut krate: ast::Crate,
crate_name: &str,
addl_plugins: Option<Plugins>)
addl_plugins: Option<Vec<String>>)
-> Option<ast::Crate> {
let time_passes = sess.time_passes();

@ -53,6 +53,7 @@ use rustc::session::config::{Input, PrintRequest, UnstableFeatures};
use rustc::lint::Lint;
use rustc::lint;
use rustc::metadata;
use rustc::metadata::creader::CrateOrString::Str;
use rustc::DIAGNOSTICS;
use std::cmp::Ordering::Equal;
@ -185,7 +186,8 @@ fn run_compiler(args: &[String]) {
return;
}
driver::compile_input(sess, cfg, &input, &odir, &ofile, None);
let plugins = sess.opts.debugging_opts.extra_plugins.clone();
driver::compile_input(sess, cfg, &input, &odir, &ofile, Some(plugins));
}
pub fn get_unstable_features_setting() -> UnstableFeatures {