diff options
| author | Joshua Nelson <jyn514@gmail.com> | 2020-11-10 16:23:35 -0500 |
|---|---|---|
| committer | Joshua Nelson <joshua@yottadb.com> | 2020-11-10 20:02:56 -0500 |
| commit | ad2ef2af6b3b712f0317bd6ef318768095107625 (patch) | |
| tree | be1a989fa843d8666445a45c6d095bba9cd0b989 /src/doc/rustc-dev-guide | |
| parent | 2638846c4993887d5646e70fc43dd2c5c53ade22 (diff) | |
| download | rust-ad2ef2af6b3b712f0317bd6ef318768095107625.tar.gz rust-ad2ef2af6b3b712f0317bd6ef318768095107625.zip | |
Add some more examples of using the compiler
Diffstat (limited to 'src/doc/rustc-dev-guide')
| -rw-r--r-- | src/doc/rustc-dev-guide/examples/rustc-driver-interacting-with-the-ast.rs | 18 |
1 files changed, 15 insertions, 3 deletions
diff --git a/src/doc/rustc-dev-guide/examples/rustc-driver-interacting-with-the-ast.rs b/src/doc/rustc-dev-guide/examples/rustc-driver-interacting-with-the-ast.rs index e33c2ee9571..8ee38206f7c 100644 --- a/src/doc/rustc-dev-guide/examples/rustc-driver-interacting-with-the-ast.rs +++ b/src/doc/rustc-dev-guide/examples/rustc-driver-interacting-with-the-ast.rs @@ -1,8 +1,9 @@ #![feature(rustc_private)] // NOTE: For the example to compile, you will need to first run the following: -// rustup component add rustc-dev +// rustup component add rustc-dev llvm-tools-preview +extern crate rustc_ast_pretty; extern crate rustc_error_codes; extern crate rustc_errors; extern crate rustc_hash; @@ -11,8 +12,11 @@ extern crate rustc_interface; extern crate rustc_session; extern crate rustc_span; +use rustc_ast_pretty::pprust::item_to_string; use rustc_errors::registry; use rustc_session::config; +use rustc_session::config::PpMode::PpmSource; +use rustc_session::config::PpSourceMode::PpmExpanded; use rustc_span::source_map; use std::path; use std::process; @@ -46,16 +50,24 @@ fn main() { lint_caps: rustc_hash::FxHashMap::default(), register_lints: None, override_queries: None, + make_codegen_backend: None, registry: registry::Registry::new(&rustc_error_codes::DIAGNOSTICS), }; rustc_interface::run_compiler(config, |compiler| { compiler.enter(|queries| { + // TODO: add this to -Z unpretty + let ast_krate = queries.parse().unwrap().take(); + let ast_krate_mod = ast_krate.module; + for item in ast_krate_mod.items { + println!("{}", item_to_string(&item)); + } + // Analyze the crate and inspect the types under the cursor. queries.global_ctxt().unwrap().take().enter(|tcx| { // Every compilation contains a single crate. - let krate = tcx.hir().krate(); + let hir_krate = tcx.hir().krate(); // Iterate over the top-level items in the crate, looking for the main function. - for (_, item) in &krate.items { + for (_, item) in &hir_krate.items { // Use pattern-matching to find a specific node inside the main function. if let rustc_hir::ItemKind::Fn(_, _, body_id) = item.kind { let expr = &tcx.hir().body(body_id).value; |
