/src/librustc_plugin/lib.rs

https://gitlab.com/jianglu/rust · Rust · 83 lines · 15 code · 8 blank · 60 comment · 0 complexity · 22417be286f7973c4079814c492110b8 MD5 · raw file

  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.
  4. //
  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.
  10. //! Infrastructure for compiler plugins.
  11. //!
  12. //! Plugins are Rust libraries which extend the behavior of `rustc`
  13. //! in various ways.
  14. //!
  15. //! Plugin authors will use the `Registry` type re-exported by
  16. //! this module, along with its methods. The rest of the module
  17. //! is for use by `rustc` itself.
  18. //!
  19. //! To define a plugin, build a dylib crate with a
  20. //! `#[plugin_registrar]` function:
  21. //!
  22. //! ```no_run
  23. //! #![crate_name = "myplugin"]
  24. //! #![crate_type = "dylib"]
  25. //! #![feature(plugin_registrar)]
  26. //! #![feature(rustc_private)]
  27. //!
  28. //! extern crate rustc_plugin;
  29. //! extern crate syntax;
  30. //! extern crate syntax_pos;
  31. //!
  32. //! use rustc_plugin::Registry;
  33. //! use syntax::ext::base::{ExtCtxt, MacResult};
  34. //! use syntax_pos::Span;
  35. //! use syntax::tokenstream::TokenTree;
  36. //!
  37. //! #[plugin_registrar]
  38. //! pub fn plugin_registrar(reg: &mut Registry) {
  39. //! reg.register_macro("mymacro", expand_mymacro);
  40. //! }
  41. //!
  42. //! fn expand_mymacro(cx: &mut ExtCtxt, span: Span, tt: &[TokenTree]) -> Box<MacResult> {
  43. //! unimplemented!()
  44. //! }
  45. //!
  46. //! # fn main() {}
  47. //! ```
  48. //!
  49. //! WARNING: We currently don't check that the registrar function
  50. //! has the appropriate type!
  51. //!
  52. //! To use a plugin while compiling another crate:
  53. //!
  54. //! ```rust
  55. //! #![feature(plugin)]
  56. //! #![plugin(myplugin)]
  57. //! ```
  58. //!
  59. //! See the [`plugin` feature](../unstable-book/language-features/plugin.html) of
  60. //! the Unstable Book for more examples.
  61. #![doc(html_logo_url = "https://www.rust-lang.org/logos/rust-logo-128x128-blk-v2.png",
  62. html_favicon_url = "https://doc.rust-lang.org/favicon.ico",
  63. html_root_url = "https://doc.rust-lang.org/nightly/")]
  64. #![feature(rustc_diagnostic_macros)]
  65. #[macro_use] extern crate syntax;
  66. extern crate rustc;
  67. extern crate rustc_metadata;
  68. extern crate syntax_pos;
  69. extern crate rustc_errors as errors;
  70. pub use self::registry::Registry;
  71. mod diagnostics;
  72. pub mod registry;
  73. pub mod load;
  74. pub mod build;
  75. __build_diagnostic_array! { librustc_plugin, DIAGNOSTICS }