1 //! Used by plugin crates to tell `rustc` about the plugins they provide.
3 use rustc::lint::{EarlyLintPassObject, LateLintPassObject, LintId, Lint};
4 use rustc::session::Session;
5 use rustc::util::nodemap::FxHashMap;
7 use syntax::ext::base::{SyntaxExtension, NamedSyntaxExtension};
8 use syntax::ext::base::MacroExpanderFn;
9 use syntax::ext::hygiene::Transparency;
10 use syntax::symbol::{Symbol, sym};
12 use syntax::feature_gate::AttributeType;
15 use std::borrow::ToOwned;
17 /// Structure used to register plugins.
19 /// A plugin registrar function takes an `&mut Registry` and should call
20 /// methods to register its plugins.
22 /// This struct has public fields and other methods for use by `rustc`
23 /// itself. They are not documented here, and plugin authors should
25 pub struct Registry<'a> {
26 /// Compiler session. Useful if you want to emit diagnostic messages
27 /// from the plugin registrar.
28 pub sess: &'a Session,
31 pub args_hidden: Option<Vec<ast::NestedMetaItem>>,
37 pub syntax_exts: Vec<NamedSyntaxExtension>,
40 pub early_lint_passes: Vec<EarlyLintPassObject>,
43 pub late_lint_passes: Vec<LateLintPassObject>,
46 pub lint_groups: FxHashMap<&'static str, (Vec<LintId>, Option<&'static str>)>,
49 pub llvm_passes: Vec<String>,
52 pub attributes: Vec<(Symbol, AttributeType)>,
55 impl<'a> Registry<'a> {
57 pub fn new(sess: &'a Session, krate_span: Span) -> Registry<'a> {
63 early_lint_passes: vec![],
64 late_lint_passes: vec![],
65 lint_groups: FxHashMap::default(),
71 /// Gets the plugin's arguments, if any.
73 /// These are specified inside the `plugin` crate attribute as
76 /// #![plugin(my_plugin_name(... args ...))]
79 /// Returns empty slice in case the plugin was loaded
80 /// with `--extra-plugins`
81 pub fn args<'b>(&'b self) -> &'b [ast::NestedMetaItem] {
82 self.args_hidden.as_ref().map(|v| &v[..]).unwrap_or(&[])
85 /// Register a syntax extension of any kind.
87 /// This is the most general hook into `libsyntax`'s expansion behavior.
88 pub fn register_syntax_extension(&mut self, name: ast::Name, mut extension: SyntaxExtension) {
89 if name == sym::macro_rules {
90 panic!("user-defined macros may not be named `macro_rules`");
92 if let SyntaxExtension::LegacyBang { def_info: ref mut def_info @ None, .. } = extension {
93 *def_info = Some((ast::CRATE_NODE_ID, self.krate_span));
95 self.syntax_exts.push((name, extension));
98 /// Register a macro of the usual kind.
100 /// This is a convenience wrapper for `register_syntax_extension`.
101 /// It builds for you a `SyntaxExtension::LegacyBang` that calls `expander`,
102 /// and also takes care of interning the macro's name.
103 pub fn register_macro(&mut self, name: &str, expander: MacroExpanderFn) {
104 self.register_syntax_extension(Symbol::intern(name), SyntaxExtension::LegacyBang {
105 expander: Box::new(expander),
107 transparency: Transparency::SemiTransparent,
108 allow_internal_unstable: None,
109 allow_internal_unsafe: false,
110 local_inner_macros: false,
111 unstable_feature: None,
112 edition: self.sess.edition(),
116 /// Register a compiler lint pass.
117 pub fn register_early_lint_pass(&mut self, lint_pass: EarlyLintPassObject) {
118 self.early_lint_passes.push(lint_pass);
121 /// Register a compiler lint pass.
122 pub fn register_late_lint_pass(&mut self, lint_pass: LateLintPassObject) {
123 self.late_lint_passes.push(lint_pass);
125 /// Register a lint group.
126 pub fn register_lint_group(
129 deprecated_name: Option<&'static str>,
130 to: Vec<&'static Lint>
132 self.lint_groups.insert(name,
133 (to.into_iter().map(|x| LintId::of(x)).collect(),
137 /// Register an LLVM pass.
139 /// Registration with LLVM itself is handled through static C++ objects with
140 /// constructors. This method simply adds a name to the list of passes to
142 pub fn register_llvm_pass(&mut self, name: &str) {
143 self.llvm_passes.push(name.to_owned());
146 /// Register an attribute with an attribute type.
148 /// Registered attributes will bypass the `custom_attribute` feature gate.
149 /// `Whitelisted` attributes will additionally not trigger the `unused_attribute`
150 /// lint. `CrateLevel` attributes will not be allowed on anything other than a crate.
151 pub fn register_attribute(&mut self, name: Symbol, ty: AttributeType) {
152 self.attributes.push((name, ty));