]> git.lizzy.rs Git - rust.git/blob - src/librustc_plugin/load.rs
Rollup merge of #61715 - RalfJung:test-ascii-lowercase, r=varkor
[rust.git] / src / librustc_plugin / load.rs
1 //! Used by `rustc` when loading a plugin.
2
3 use rustc::session::Session;
4 use rustc_metadata::creader::CrateLoader;
5 use rustc_metadata::cstore::CStore;
6 use crate::registry::Registry;
7
8 use std::borrow::ToOwned;
9 use std::env;
10 use std::mem;
11 use std::path::PathBuf;
12 use syntax::ast;
13 use syntax::struct_span_err;
14 use syntax::symbol::{Symbol, kw, sym};
15 use syntax_pos::{Span, DUMMY_SP};
16
17 /// Pointer to a registrar function.
18 pub type PluginRegistrarFun =
19     fn(&mut Registry<'_>);
20
21 pub struct PluginRegistrar {
22     pub fun: PluginRegistrarFun,
23     pub args: Vec<ast::NestedMetaItem>,
24 }
25
26 struct PluginLoader<'a> {
27     sess: &'a Session,
28     reader: CrateLoader<'a>,
29     plugins: Vec<PluginRegistrar>,
30 }
31
32 fn call_malformed_plugin_attribute(sess: &Session, span: Span) {
33     struct_span_err!(sess, span, E0498, "malformed `plugin` attribute")
34         .span_label(span, "malformed attribute")
35         .emit();
36 }
37
38 /// Read plugin metadata and dynamically load registrar functions.
39 pub fn load_plugins(sess: &Session,
40                     cstore: &CStore,
41                     krate: &ast::Crate,
42                     crate_name: &str,
43                     addl_plugins: Option<Vec<String>>) -> Vec<PluginRegistrar> {
44     let mut loader = PluginLoader::new(sess, cstore, crate_name);
45
46     // do not report any error now. since crate attributes are
47     // not touched by expansion, every use of plugin without
48     // the feature enabled will result in an error later...
49     if sess.features_untracked().plugin {
50         for attr in &krate.attrs {
51             if !attr.check_name(sym::plugin) {
52                 continue;
53             }
54
55             let plugins = match attr.meta_item_list() {
56                 Some(xs) => xs,
57                 None => continue,
58             };
59
60             for plugin in plugins {
61                 // plugins must have a name and can't be key = value
62                 let name = plugin.name_or_empty();
63                 if name != kw::Invalid && !plugin.is_value_str() {
64                     let args = plugin.meta_item_list().map(ToOwned::to_owned);
65                     loader.load_plugin(plugin.span(), name, args.unwrap_or_default());
66                 } else {
67                     call_malformed_plugin_attribute(sess, attr.span);
68                 }
69             }
70         }
71     }
72
73     if let Some(plugins) = addl_plugins {
74         for plugin in plugins {
75             loader.load_plugin(DUMMY_SP, Symbol::intern(&plugin), vec![]);
76         }
77     }
78
79     loader.plugins
80 }
81
82 impl<'a> PluginLoader<'a> {
83     fn new(sess: &'a Session, cstore: &'a CStore, crate_name: &str) -> Self {
84         PluginLoader {
85             sess,
86             reader: CrateLoader::new(sess, cstore, crate_name),
87             plugins: vec![],
88         }
89     }
90
91     fn load_plugin(&mut self, span: Span, name: Symbol, args: Vec<ast::NestedMetaItem>) {
92         let registrar = self.reader.find_plugin_registrar(span, name);
93
94         if let Some((lib, disambiguator)) = registrar {
95             let symbol = self.sess.generate_plugin_registrar_symbol(disambiguator);
96             let fun = self.dylink_registrar(span, lib, symbol);
97             self.plugins.push(PluginRegistrar {
98                 fun,
99                 args,
100             });
101         }
102     }
103
104     // Dynamically link a registrar function into the compiler process.
105     fn dylink_registrar(&mut self,
106                         span: Span,
107                         path: PathBuf,
108                         symbol: String) -> PluginRegistrarFun {
109         use rustc_metadata::dynamic_lib::DynamicLibrary;
110
111         // Make sure the path contains a / or the linker will search for it.
112         let path = env::current_dir().unwrap().join(&path);
113
114         let lib = match DynamicLibrary::open(Some(&path)) {
115             Ok(lib) => lib,
116             // this is fatal: there are almost certainly macros we need
117             // inside this crate, so continue would spew "macro undefined"
118             // errors
119             Err(err) => {
120                 self.sess.span_fatal(span, &err)
121             }
122         };
123
124         unsafe {
125             let registrar =
126                 match lib.symbol(&symbol) {
127                     Ok(registrar) => {
128                         mem::transmute::<*mut u8,PluginRegistrarFun>(registrar)
129                     }
130                     // again fatal if we can't register macros
131                     Err(err) => {
132                         self.sess.span_fatal(span, &err)
133                     }
134                 };
135
136             // Intentionally leak the dynamic library. We can't ever unload it
137             // since the library can make things that will live arbitrarily long
138             // (e.g., an @-box cycle or a thread).
139             mem::forget(lib);
140
141             registrar
142         }
143     }
144 }