Skip to content

Commit 879e8aa

Browse files
committed
auto merge of #12387 : cmr/rust/ast-json, r=alexcrichton
See the commits
2 parents ea00582 + 34ffe3c commit 879e8aa

File tree

2 files changed

+30
-7
lines changed

2 files changed

+30
-7
lines changed

src/librustc/driver/driver.rs

+25-6
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@ use middle;
2727
use util::common::time;
2828
use util::ppaux;
2929

30+
use extra::json;
31+
use serialize::Encodable;
32+
3033
use std::cell::{Cell, RefCell};
3134
use std::hashmap::{HashMap,HashSet};
3235
use std::io;
@@ -154,7 +157,7 @@ pub enum Input {
154157

155158
pub fn phase_1_parse_input(sess: Session, cfg: ast::CrateConfig, input: &Input)
156159
-> ast::Crate {
157-
time(sess.time_passes(), "parsing", (), |_| {
160+
let krate = time(sess.time_passes(), "parsing", (), |_| {
158161
match *input {
159162
FileInput(ref file) => {
160163
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)
166169
sess.parse_sess)
167170
}
168171
}
169-
})
172+
});
173+
174+
if sess.opts.debugging_opts & session::AST_JSON_NOEXPAND != 0 {
175+
let mut stdout = io::stdout();
176+
let mut json = json::PrettyEncoder::new(&mut stdout);
177+
krate.encode(&mut json);
178+
}
179+
180+
krate
170181
}
171182

172183
// For continuing compilation after a parsed crate has been
@@ -220,8 +231,16 @@ pub fn phase_2_configure_and_expand(sess: Session,
220231
krate = time(time_passes, "prelude injection", krate, |krate|
221232
front::std_inject::maybe_inject_prelude(sess, krate));
222233

223-
time(time_passes, "assinging node ids and indexing ast", krate, |krate|
224-
front::assign_node_ids_and_map::assign_node_ids_and_map(sess, krate))
234+
let (krate, map) = time(time_passes, "assinging node ids and indexing ast", krate, |krate|
235+
front::assign_node_ids_and_map::assign_node_ids_and_map(sess, krate));
236+
237+
if sess.opts.debugging_opts & session::AST_JSON != 0 {
238+
let mut stdout = io::stdout();
239+
let mut json = json::PrettyEncoder::new(&mut stdout);
240+
krate.encode(&mut json);
241+
}
242+
243+
(krate, map)
225244
}
226245

227246
pub struct CrateAnalysis {
@@ -428,15 +447,15 @@ pub fn stop_after_phase_1(sess: Session) -> bool {
428447
debug!("invoked with --parse-only, returning early from compile_input");
429448
return true;
430449
}
431-
return false;
450+
return sess.opts.debugging_opts & session::AST_JSON_NOEXPAND != 0;
432451
}
433452

434453
pub fn stop_after_phase_2(sess: Session) -> bool {
435454
if sess.opts.no_analysis {
436455
debug!("invoked with --no-analysis, returning early from compile_input");
437456
return true;
438457
}
439-
return false;
458+
return sess.opts.debugging_opts & session::AST_JSON != 0;
440459
}
441460

442461
pub fn stop_after_phase_5(sess: Session) -> bool {

src/librustc/driver/session.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,9 @@ debugging_opts!(
6565
GC,
6666
PRINT_LINK_ARGS,
6767
PRINT_LLVM_PASSES,
68-
LTO
68+
LTO,
69+
AST_JSON,
70+
AST_JSON_NOEXPAND
6971
]
7072
0
7173
)
@@ -97,6 +99,8 @@ pub fn debugging_opts_map() -> ~[(&'static str, &'static str, u64)] {
9799
"Prints the llvm optimization passes being run",
98100
PRINT_LLVM_PASSES),
99101
("lto", "Perform LLVM link-time optimizations", LTO),
102+
("ast-json", "Print the AST as JSON and halt", AST_JSON),
103+
("ast-json-noexpand", "Print the pre-expansion AST as JSON and halt", AST_JSON_NOEXPAND),
100104
]
101105
}
102106

0 commit comments

Comments
 (0)