From 34ffe3cc1c1c9855ecb47479761d812f64436c85 Mon Sep 17 00:00:00 2001 From: Corey Richardson Date: Wed, 19 Feb 2014 02:27:49 -0500 Subject: [PATCH] rustc: support dumping the AST as JSON This is mostly useful for working on rustc, when one is unfamiliar with the AST a particular construct will produce. It's a -Z flag as it's very much for debugging. Closes #10485 --- src/librustc/driver/driver.rs | 31 +++++++++++++++++++++++++------ src/librustc/driver/session.rs | 6 +++++- 2 files changed, 30 insertions(+), 7 deletions(-) diff --git a/src/librustc/driver/driver.rs b/src/librustc/driver/driver.rs index cbc72b5a918..5edf7a3251c 100644 --- a/src/librustc/driver/driver.rs +++ b/src/librustc/driver/driver.rs @@ -27,6 +27,9 @@ use middle; use util::common::time; use util::ppaux; +use extra::json; +use serialize::Encodable; + use std::cell::{Cell, RefCell}; use std::hashmap::{HashMap,HashSet}; use std::io; @@ -154,7 +157,7 @@ pub enum Input { pub fn phase_1_parse_input(sess: Session, cfg: ast::CrateConfig, input: &Input) -> ast::Crate { - time(sess.time_passes(), "parsing", (), |_| { + let krate = time(sess.time_passes(), "parsing", (), |_| { match *input { FileInput(ref file) => { parse::parse_crate_from_file(&(*file), cfg.clone(), sess.parse_sess) @@ -166,7 +169,15 @@ pub fn phase_1_parse_input(sess: Session, cfg: ast::CrateConfig, input: &Input) sess.parse_sess) } } - }) + }); + + if sess.opts.debugging_opts & session::AST_JSON_NOEXPAND != 0 { + let mut stdout = io::stdout(); + let mut json = json::PrettyEncoder::new(&mut stdout); + krate.encode(&mut json); + } + + krate } // For continuing compilation after a parsed crate has been @@ -220,8 +231,16 @@ pub fn phase_2_configure_and_expand(sess: Session, krate = time(time_passes, "prelude injection", krate, |krate| front::std_inject::maybe_inject_prelude(sess, krate)); - time(time_passes, "assinging node ids and indexing ast", krate, |krate| - front::assign_node_ids_and_map::assign_node_ids_and_map(sess, krate)) + let (krate, map) = time(time_passes, "assinging node ids and indexing ast", krate, |krate| + front::assign_node_ids_and_map::assign_node_ids_and_map(sess, krate)); + + if sess.opts.debugging_opts & session::AST_JSON != 0 { + let mut stdout = io::stdout(); + let mut json = json::PrettyEncoder::new(&mut stdout); + krate.encode(&mut json); + } + + (krate, map) } pub struct CrateAnalysis { @@ -428,7 +447,7 @@ pub fn stop_after_phase_1(sess: Session) -> bool { debug!("invoked with --parse-only, returning early from compile_input"); return true; } - return false; + return sess.opts.debugging_opts & session::AST_JSON_NOEXPAND != 0; } pub fn stop_after_phase_2(sess: Session) -> bool { @@ -436,7 +455,7 @@ pub fn stop_after_phase_2(sess: Session) -> bool { debug!("invoked with --no-analysis, returning early from compile_input"); return true; } - return false; + return sess.opts.debugging_opts & session::AST_JSON != 0; } pub fn stop_after_phase_5(sess: Session) -> bool { diff --git a/src/librustc/driver/session.rs b/src/librustc/driver/session.rs index aff89974ca6..d43a68e4f87 100644 --- a/src/librustc/driver/session.rs +++ b/src/librustc/driver/session.rs @@ -65,7 +65,9 @@ debugging_opts!( GC, PRINT_LINK_ARGS, PRINT_LLVM_PASSES, - LTO + LTO, + AST_JSON, + AST_JSON_NOEXPAND ] 0 ) @@ -97,6 +99,8 @@ pub fn debugging_opts_map() -> ~[(&'static str, &'static str, u64)] { "Prints the llvm optimization passes being run", PRINT_LLVM_PASSES), ("lto", "Perform LLVM link-time optimizations", LTO), + ("ast-json", "Print the AST as JSON and halt", AST_JSON), + ("ast-json-noexpand", "Print the pre-expansion AST as JSON and halt", AST_JSON_NOEXPAND), ] }