]> git.lizzy.rs Git - rust.git/blob - src/librustc_plugin_impl/registry.rs
Rollup merge of #66503 - thomasetter:panic-error-msg, r=joshtriplett
[rust.git] / src / librustc_plugin_impl / registry.rs
1 //! Used by plugin crates to tell `rustc` about the plugins they provide.
2
3 use rustc::lint::LintStore;
4 use rustc::session::Session;
5 use syntax::ast;
6 use syntax_pos::Span;
7
8 use std::borrow::ToOwned;
9
10 /// Structure used to register plugins.
11 ///
12 /// A plugin registrar function takes an `&mut Registry` and should call
13 /// methods to register its plugins.
14 ///
15 /// This struct has public fields and other methods for use by `rustc`
16 /// itself. They are not documented here, and plugin authors should
17 /// not use them.
18 pub struct Registry<'a> {
19     /// Compiler session. Useful if you want to emit diagnostic messages
20     /// from the plugin registrar.
21     pub sess: &'a Session,
22
23     /// The `LintStore` allows plugins to register new lints.
24     pub lint_store: &'a mut LintStore,
25
26     #[doc(hidden)]
27     pub args_hidden: Option<Vec<ast::NestedMetaItem>>,
28
29     #[doc(hidden)]
30     pub krate_span: Span,
31
32     #[doc(hidden)]
33     pub llvm_passes: Vec<String>,
34 }
35
36 impl<'a> Registry<'a> {
37     #[doc(hidden)]
38     pub fn new(sess: &'a Session, lint_store: &'a mut LintStore, krate_span: Span) -> Registry<'a> {
39         Registry {
40             sess,
41             lint_store,
42             args_hidden: None,
43             krate_span,
44             llvm_passes: vec![],
45         }
46     }
47
48     /// Gets the plugin's arguments, if any.
49     ///
50     /// These are specified inside the `plugin` crate attribute as
51     ///
52     /// ```no_run
53     /// #![plugin(my_plugin_name(... args ...))]
54     /// ```
55     ///
56     /// Returns empty slice in case the plugin was loaded
57     /// with `--extra-plugins`
58     pub fn args(&self) -> &[ast::NestedMetaItem] {
59         self.args_hidden.as_ref().map(|v| &v[..]).unwrap_or(&[])
60     }
61
62     /// Register an LLVM pass.
63     ///
64     /// Registration with LLVM itself is handled through static C++ objects with
65     /// constructors. This method simply adds a name to the list of passes to
66     /// execute.
67     pub fn register_llvm_pass(&mut self, name: &str) {
68         self.llvm_passes.push(name.to_owned());
69     }
70 }