Skip to content

Commit 9f5f706

Browse files
committed
librustc::plugin : make PluginLoader usable for loading argument-specified plugins
1 parent efaf613 commit 9f5f706

File tree

2 files changed

+40
-17
lines changed

2 files changed

+40
-17
lines changed

src/librustc/metadata/creader.rs

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

519+
#[deriving(Copy)]
519520
pub enum CrateOrString<'a> {
520521
Krate(&'a ast::ViewItem),
521522
Str(&'a str)

src/librustc/plugin/load.rs

Lines changed: 39 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,21 @@ impl<'a, 'v> Visitor<'v> for PluginLoader<'a> {
160160
}
161161
}
162162

163+
self.load_plugin(CrateOrString::Krate(vi), plugin_attr, macro_selection, reexport)
164+
}
165+
166+
fn visit_mac(&mut self, _: &ast::Mac) {
167+
// bummer... can't see plugins inside macros.
168+
// do nothing.
169+
}
170+
}
171+
172+
impl<'a> PluginLoader<'a> {
173+
pub fn load_plugin<'b>(&mut self,
174+
c: CrateOrString<'b>,
175+
plugin_attr: Option<P<ast::MetaItem>>,
176+
macro_selection: Option<HashSet<token::InternedString>>,
177+
reexport: HashSet<token::InternedString>) {
163178
let mut macros = vec![];
164179
let mut registrar = None;
165180

@@ -169,13 +184,15 @@ impl<'a, 'v> Visitor<'v> for PluginLoader<'a> {
169184
};
170185
let load_registrar = plugin_attr.is_some();
171186

172-
if load_macros && !self.span_whitelist.contains(&vi.span) {
173-
self.sess.span_err(vi.span, "an `extern crate` loading macros must be at \
174-
the crate root");
175-
}
187+
if let CrateOrString::Krate(vi) = c {
188+
if load_macros && !self.span_whitelist.contains(&vi.span) {
189+
self.sess.span_err(vi.span, "an `extern crate` loading macros must be at \
190+
the crate root");
191+
}
192+
}
176193

177194
if load_macros || load_registrar {
178-
let pmd = self.reader.read_plugin_metadata(CrateOrString::Krate(vi));
195+
let pmd = self.reader.read_plugin_metadata(c);
179196
if load_macros {
180197
macros = pmd.exported_macros();
181198
}
@@ -195,24 +212,17 @@ impl<'a, 'v> Visitor<'v> for PluginLoader<'a> {
195212
}
196213

197214
if let Some((lib, symbol)) = registrar {
198-
let fun = self.dylink_registrar(vi, lib, symbol);
215+
let fun = self.dylink_registrar(c, lib, symbol);
199216
self.plugins.registrars.push(PluginRegistrar {
200217
fun: fun,
201218
args: plugin_attr.unwrap(),
202219
});
203220
}
204221
}
205222

206-
fn visit_mac(&mut self, _: &ast::Mac) {
207-
// bummer... can't see plugins inside macros.
208-
// do nothing.
209-
}
210-
}
211-
212-
impl<'a> PluginLoader<'a> {
213223
// Dynamically link a registrar function into the compiler process.
214-
fn dylink_registrar(&mut self,
215-
vi: &ast::ViewItem,
224+
fn dylink_registrar<'b>(&mut self,
225+
c: CrateOrString<'b>,
216226
path: Path,
217227
symbol: String) -> PluginRegistrarFun {
218228
// Make sure the path contains a / or the linker will search for it.
@@ -223,7 +233,13 @@ impl<'a> PluginLoader<'a> {
223233
// this is fatal: there are almost certainly macros we need
224234
// inside this crate, so continue would spew "macro undefined"
225235
// errors
226-
Err(err) => self.sess.span_fatal(vi.span, &err[])
236+
Err(err) => {
237+
if let CrateOrString::Krate(cr) = c {
238+
self.sess.span_fatal(cr.span, &err[])
239+
} else {
240+
self.sess.fatal(&err[])
241+
}
242+
}
227243
};
228244

229245
unsafe {
@@ -233,7 +249,13 @@ impl<'a> PluginLoader<'a> {
233249
mem::transmute::<*mut u8,PluginRegistrarFun>(registrar)
234250
}
235251
// again fatal if we can't register macros
236-
Err(err) => self.sess.span_fatal(vi.span, &err[])
252+
Err(err) => {
253+
if let CrateOrString::Krate(cr) = c {
254+
self.sess.span_fatal(cr.span, &err[])
255+
} else {
256+
self.sess.fatal(&err[])
257+
}
258+
}
237259
};
238260

239261
// Intentionally leak the dynamic library. We can't ever unload it

0 commit comments

Comments
 (0)