1 // Copyright 2012-2013 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
11 //! Used by `rustc` when compiling a plugin crate.
15 use syntax::codemap::Span;
16 use syntax::diagnostic;
17 use rustc_front::visit;
18 use rustc_front::visit::Visitor;
21 struct RegistrarFinder {
22 registrars: Vec<(ast::NodeId, Span)> ,
25 impl<'v> Visitor<'v> for RegistrarFinder {
26 fn visit_item(&mut self, item: &hir::Item) {
27 if let hir::ItemFn(..) = item.node {
28 if attr::contains_name(&item.attrs,
30 self.registrars.push((item.id, item.span));
34 visit::walk_item(self, item);
38 /// Find the function marked with `#[plugin_registrar]`, if any.
39 pub fn find_plugin_registrar(diagnostic: &diagnostic::SpanHandler,
41 -> Option<ast::NodeId> {
42 let mut finder = RegistrarFinder { registrars: Vec::new() };
43 visit::walk_crate(&mut finder, krate);
45 match finder.registrars.len() {
48 let (node_id, _) = finder.registrars.pop().unwrap();
52 diagnostic.handler().err("multiple plugin registration functions found");
53 for &(_, span) in &finder.registrars {
54 diagnostic.span_note(span, "one is here");
56 diagnostic.handler().abort_if_errors();