about summary refs log tree commit diff
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2014-02-19 16:21:48 -0800
committerbors <bors@rust-lang.org>2014-02-19 16:21:48 -0800
commit879e8aa7be06a53cd6cd9cc714e85a13c909c0ea (patch)
tree53ae575ce9347c3378644ad2ecb7eb33c19d4525
parentea0058281cfea06a61e5eb23b31c15e9d1dcfda3 (diff)
parent34ffe3cc1c1c9855ecb47479761d812f64436c85 (diff)
auto merge of #12387 : cmr/rust/ast-json, r=alexcrichton
See the commits
-rw-r--r--src/librustc/driver/driver.rs31
-rw-r--r--src/librustc/driver/session.rs6
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),
     ]
 }