From ef7ae6d1da61d839e82ce415e0b019d5eb1cfe52 Mon Sep 17 00:00:00 2001 From: Michael Smith Date: Tue, 29 Nov 2016 20:00:44 -0800 Subject: [PATCH 1/3] Add -Zast and -Zast-noexpand to dump AST w/o JSON -Zast behaves like -Zast-json but uses {:#?} pretty-printing instead of converting to JSON. -Zast-noexpand is the pretty-printing counterpart to -Zast-json-noexpand. --- src/librustc/session/config.rs | 8 ++++++++ src/librustc_driver/driver.rs | 8 ++++++++ src/librustc_driver/lib.rs | 2 ++ 3 files changed, 18 insertions(+) diff --git a/src/librustc/session/config.rs b/src/librustc/session/config.rs index 7d8f7fcefe639..37dc0a43f6fdd 100644 --- a/src/librustc/session/config.rs +++ b/src/librustc/session/config.rs @@ -862,6 +862,10 @@ options! {DebuggingOptions, DebuggingSetter, basic_debugging_options, "print the arguments passed to the linker"), print_llvm_passes: bool = (false, parse_bool, [UNTRACKED], "prints the llvm optimization passes being run"), + ast: bool = (false, parse_bool, [UNTRACKED], + "pretty-print the AST and halt"), + ast_noexpand: bool = (false, parse_bool, [UNTRACKED], + "pretty-print the pre-expansion AST and halt"), ast_json: bool = (false, parse_bool, [UNTRACKED], "print the AST as JSON and halt"), ast_json_noexpand: bool = (false, parse_bool, [UNTRACKED], @@ -2404,6 +2408,10 @@ mod tests { assert_eq!(reference.dep_tracking_hash(), opts.dep_tracking_hash()); opts.debugging_opts.print_llvm_passes = true; assert_eq!(reference.dep_tracking_hash(), opts.dep_tracking_hash()); + opts.debugging_opts.ast = true; + assert_eq!(reference.dep_tracking_hash(), opts.dep_tracking_hash()); + opts.debugging_opts.ast_noexpand = true; + assert_eq!(reference.dep_tracking_hash(), opts.dep_tracking_hash()); opts.debugging_opts.ast_json = true; assert_eq!(reference.dep_tracking_hash(), opts.dep_tracking_hash()); opts.debugging_opts.ast_json_noexpand = true; diff --git a/src/librustc_driver/driver.rs b/src/librustc_driver/driver.rs index 83dec4b0b77ab..e0a78ea6bc8f9 100644 --- a/src/librustc_driver/driver.rs +++ b/src/librustc_driver/driver.rs @@ -501,6 +501,10 @@ pub fn phase_1_parse_input<'a>(sess: &'a Session, input: &Input) -> PResult<'a, sess.diagnostic().set_continue_after_error(true); + if sess.opts.debugging_opts.ast_noexpand { + println!("{:#?}", &krate); + } + if sess.opts.debugging_opts.ast_json_noexpand { println!("{}", json::as_json(&krate)); } @@ -733,6 +737,10 @@ pub fn phase_2_configure_and_expand(sess: &Session, hir_stats::print_ast_stats(&krate, "POST EXPANSION AST STATS"); } + if sess.opts.debugging_opts.ast { + println!("{:#?}", &krate); + } + if sess.opts.debugging_opts.ast_json { println!("{}", json::as_json(&krate)); } diff --git a/src/librustc_driver/lib.rs b/src/librustc_driver/lib.rs index ad2aefbb79539..f4e2907991a1c 100644 --- a/src/librustc_driver/lib.rs +++ b/src/librustc_driver/lib.rs @@ -484,11 +484,13 @@ impl<'a> CompilerCalls<'a> for RustcDefaultCalls { if sess.opts.debugging_opts.parse_only || sess.opts.debugging_opts.show_span.is_some() || + sess.opts.debugging_opts.ast_noexpand || sess.opts.debugging_opts.ast_json_noexpand { control.after_parse.stop = Compilation::Stop; } if sess.opts.debugging_opts.no_analysis || + sess.opts.debugging_opts.ast || sess.opts.debugging_opts.ast_json { control.after_hir_lowering.stop = Compilation::Stop; } From f205a0aca501a0364c767d43d341d4acef590c48 Mon Sep 17 00:00:00 2001 From: Michael Smith Date: Thu, 1 Dec 2016 11:31:24 -0800 Subject: [PATCH 2/3] Move AST printing to an --unpretty option --- src/librustc/session/config.rs | 9 +++----- src/librustc_driver/pretty.rs | 42 +++++++++++++++++++--------------- 2 files changed, 27 insertions(+), 24 deletions(-) diff --git a/src/librustc/session/config.rs b/src/librustc/session/config.rs index 37dc0a43f6fdd..acc2e3a233b12 100644 --- a/src/librustc/session/config.rs +++ b/src/librustc/session/config.rs @@ -1240,8 +1240,9 @@ pub fn rustc_optgroups() -> Vec { valid types are any of the types for `--pretty`, as well as: `flowgraph=` (graphviz formatted flowgraph for node), `everybody_loops` (all function bodies replaced with `loop {}`), - `hir` (the HIR), `hir,identified`, or - `hir,typed` (HIR with types for each node).", + `hir` (the HIR), `hir,identified`, + `hir,typed` (HIR with types for each node), or + `ast` (pretty-printed internal AST).", "TYPE"), // new options here should **not** use the `_ubnr` functions, all new @@ -2408,10 +2409,6 @@ mod tests { assert_eq!(reference.dep_tracking_hash(), opts.dep_tracking_hash()); opts.debugging_opts.print_llvm_passes = true; assert_eq!(reference.dep_tracking_hash(), opts.dep_tracking_hash()); - opts.debugging_opts.ast = true; - assert_eq!(reference.dep_tracking_hash(), opts.dep_tracking_hash()); - opts.debugging_opts.ast_noexpand = true; - assert_eq!(reference.dep_tracking_hash(), opts.dep_tracking_hash()); opts.debugging_opts.ast_json = true; assert_eq!(reference.dep_tracking_hash(), opts.dep_tracking_hash()); opts.debugging_opts.ast_json_noexpand = true; diff --git a/src/librustc_driver/pretty.rs b/src/librustc_driver/pretty.rs index 3c8a529bdaee8..5c284baf44942 100644 --- a/src/librustc_driver/pretty.rs +++ b/src/librustc_driver/pretty.rs @@ -81,6 +81,7 @@ pub enum PpMode { PpmFlowGraph(PpFlowGraphMode), PpmMir, PpmMirCFG, + PpmAST, } impl PpMode { @@ -90,6 +91,8 @@ impl PpMode { PpmSource(PpmEveryBodyLoops) | PpmSource(PpmIdentified) => opt_uii.is_some(), + PpmAST => false, + PpmSource(PpmExpanded) | PpmSource(PpmExpandedIdentified) | PpmSource(PpmExpandedHygiene) | @@ -130,6 +133,7 @@ pub fn parse_pretty(sess: &Session, ("mir-cfg", true) => PpmMirCFG, ("flowgraph", true) => PpmFlowGraph(PpFlowGraphMode::Default), ("flowgraph,unlabelled", true) => PpmFlowGraph(PpFlowGraphMode::UnlabelledEdges), + ("ast", true) => PpmAST, _ => { if extended { sess.fatal(&format!("argument to `unpretty` must be one of `normal`, \ @@ -831,24 +835,26 @@ pub fn print_after_parsing(sess: &Session, let mut rdr = &*src; let mut out = Vec::new(); - if let PpmSource(s) = ppm { - // Silently ignores an identified node. - let out: &mut Write = &mut out; - s.call_with_pp_support(sess, None, box out, |annotation, out| { - debug!("pretty printing source code {:?}", s); - let sess = annotation.sess(); - pprust::print_crate(sess.codemap(), - &sess.parse_sess, - krate, - src_name.to_string(), - &mut rdr, - out, - annotation.pp_ann(), - false) - }) - .unwrap() - } else { - unreachable!(); + match ppm { + PpmSource(s) => { + // Silently ignores an identified node. + let out: &mut Write = &mut out; + s.call_with_pp_support(sess, None, box out, |annotation, out| { + debug!("pretty printing source code {:?}", s); + let sess = annotation.sess(); + pprust::print_crate(sess.codemap(), + &sess.parse_sess, + krate, + src_name.to_string(), + &mut rdr, + out, + annotation.pp_ann(), + false) + }) + .unwrap() + } + PpmAST => write!(out, "{:#?}", krate).unwrap(), + _ => unreachable!(), }; write_output(out, ofile); From 467e183e9e85c410b9a1b0527ec0fe34502ad9d9 Mon Sep 17 00:00:00 2001 From: Michael Smith Date: Thu, 1 Dec 2016 11:36:26 -0800 Subject: [PATCH 3/3] Scrape out remaining bits of -Zast --- src/librustc/session/config.rs | 4 ---- src/librustc_driver/driver.rs | 8 -------- src/librustc_driver/lib.rs | 2 -- 3 files changed, 14 deletions(-) diff --git a/src/librustc/session/config.rs b/src/librustc/session/config.rs index acc2e3a233b12..13235cf6f6483 100644 --- a/src/librustc/session/config.rs +++ b/src/librustc/session/config.rs @@ -862,10 +862,6 @@ options! {DebuggingOptions, DebuggingSetter, basic_debugging_options, "print the arguments passed to the linker"), print_llvm_passes: bool = (false, parse_bool, [UNTRACKED], "prints the llvm optimization passes being run"), - ast: bool = (false, parse_bool, [UNTRACKED], - "pretty-print the AST and halt"), - ast_noexpand: bool = (false, parse_bool, [UNTRACKED], - "pretty-print the pre-expansion AST and halt"), ast_json: bool = (false, parse_bool, [UNTRACKED], "print the AST as JSON and halt"), ast_json_noexpand: bool = (false, parse_bool, [UNTRACKED], diff --git a/src/librustc_driver/driver.rs b/src/librustc_driver/driver.rs index e0a78ea6bc8f9..83dec4b0b77ab 100644 --- a/src/librustc_driver/driver.rs +++ b/src/librustc_driver/driver.rs @@ -501,10 +501,6 @@ pub fn phase_1_parse_input<'a>(sess: &'a Session, input: &Input) -> PResult<'a, sess.diagnostic().set_continue_after_error(true); - if sess.opts.debugging_opts.ast_noexpand { - println!("{:#?}", &krate); - } - if sess.opts.debugging_opts.ast_json_noexpand { println!("{}", json::as_json(&krate)); } @@ -737,10 +733,6 @@ pub fn phase_2_configure_and_expand(sess: &Session, hir_stats::print_ast_stats(&krate, "POST EXPANSION AST STATS"); } - if sess.opts.debugging_opts.ast { - println!("{:#?}", &krate); - } - if sess.opts.debugging_opts.ast_json { println!("{}", json::as_json(&krate)); } diff --git a/src/librustc_driver/lib.rs b/src/librustc_driver/lib.rs index f4e2907991a1c..ad2aefbb79539 100644 --- a/src/librustc_driver/lib.rs +++ b/src/librustc_driver/lib.rs @@ -484,13 +484,11 @@ impl<'a> CompilerCalls<'a> for RustcDefaultCalls { if sess.opts.debugging_opts.parse_only || sess.opts.debugging_opts.show_span.is_some() || - sess.opts.debugging_opts.ast_noexpand || sess.opts.debugging_opts.ast_json_noexpand { control.after_parse.stop = Compilation::Stop; } if sess.opts.debugging_opts.no_analysis || - sess.opts.debugging_opts.ast || sess.opts.debugging_opts.ast_json { control.after_hir_lowering.stop = Compilation::Stop; }