]> git.lizzy.rs Git - rust.git/commitdiff
Auto merge of #82304 - LeSeulArtichaut:unpretty-ast, r=spastorino
authorbors <bors@rust-lang.org>
Thu, 4 Mar 2021 05:46:43 +0000 (05:46 +0000)
committerbors <bors@rust-lang.org>
Thu, 4 Mar 2021 05:46:43 +0000 (05:46 +0000)
Add `-Z unpretty` flags for the AST

Implements rust-lang/compiler-team#408.
Builds on #82269, but if that PR is rejected or stalls out, I can implement this without #82269.
cc rust-lang/rustc-dev-guide#1062

compiler/rustc_driver/src/pretty.rs
compiler/rustc_session/src/config.rs
compiler/rustc_session/src/options.rs

index 1dcc4d147acf23a45de96335b4c4b9fba070d67e..38c493a920d26f0bee16a00d28f7c09b7d14154b 100644 (file)
@@ -9,7 +9,7 @@
 use rustc_middle::hir::map as hir_map;
 use rustc_middle::ty::{self, TyCtxt};
 use rustc_mir::util::{write_mir_graphviz, write_mir_pretty};
-use rustc_session::config::{Input, PpHirMode, PpMode, PpSourceMode};
+use rustc_session::config::{Input, PpAstTreeMode, PpHirMode, PpMode, PpSourceMode};
 use rustc_session::Session;
 use rustc_span::symbol::Ident;
 use rustc_span::FileName;
@@ -391,24 +391,29 @@ pub fn print_after_parsing(
 ) {
     let (src, src_name) = get_source(input, sess);
 
-    let out = if let Source(s) = ppm {
-        // Silently ignores an identified node.
-        call_with_pp_support(&s, sess, None, move |annotation| {
-            debug!("pretty printing source code {:?}", s);
-            let sess = annotation.sess();
-            let parse = &sess.parse_sess;
-            pprust::print_crate(
-                sess.source_map(),
-                krate,
-                src_name,
-                src,
-                annotation.pp_ann(),
-                false,
-                parse.edition,
-            )
-        })
-    } else {
-        unreachable!()
+    let out = match ppm {
+        Source(s) => {
+            // Silently ignores an identified node.
+            call_with_pp_support(&s, sess, None, move |annotation| {
+                debug!("pretty printing source code {:?}", s);
+                let sess = annotation.sess();
+                let parse = &sess.parse_sess;
+                pprust::print_crate(
+                    sess.source_map(),
+                    krate,
+                    src_name,
+                    src,
+                    annotation.pp_ann(),
+                    false,
+                    parse.edition,
+                )
+            })
+        }
+        AstTree(PpAstTreeMode::Normal) => {
+            debug!("pretty printing AST tree");
+            format!("{:#?}", krate)
+        }
+        _ => unreachable!(),
     };
 
     write_or_print(&out, ofile);
@@ -447,6 +452,11 @@ pub fn print_after_hir_lowering<'tcx>(
             })
         }
 
+        AstTree(PpAstTreeMode::Expanded) => {
+            debug!("pretty-printing expanded AST");
+            format!("{:#?}", krate)
+        }
+
         Hir(s) => call_with_pp_support_hir(&s, tcx, move |annotation, krate| {
             debug!("pretty printing HIR {:?}", s);
             let sess = annotation.sess();
index a3900ebcea99c687398df144d1bd0e4145cb5901..77a9a2b227ca0c9b799247b04feca030ed93ad01 100644 (file)
@@ -2066,6 +2066,8 @@ fn parse_pretty_inner(efmt: ErrorOutputType, name: &str, extended: bool) -> PpMo
             ("expanded", _) => Source(PpSourceMode::Expanded),
             ("expanded,identified", _) => Source(PpSourceMode::ExpandedIdentified),
             ("expanded,hygiene", _) => Source(PpSourceMode::ExpandedHygiene),
+            ("ast-tree", true) => AstTree(PpAstTreeMode::Normal),
+            ("ast-tree,expanded", true) => AstTree(PpAstTreeMode::Expanded),
             ("hir", true) => Hir(PpHirMode::Normal),
             ("hir,identified", true) => Hir(PpHirMode::Identified),
             ("hir,typed", true) => Hir(PpHirMode::Typed),
@@ -2080,8 +2082,8 @@ fn parse_pretty_inner(efmt: ErrorOutputType, name: &str, extended: bool) -> PpMo
                             "argument to `unpretty` must be one of `normal`, \
                                         `expanded`, `identified`, `expanded,identified`, \
                                         `expanded,hygiene`, `everybody_loops`, \
-                                        `hir`, `hir,identified`, `hir,typed`, `hir-tree`, \
-                                        `mir` or `mir-cfg`; got {}",
+                                        `ast-tree`, `ast-tree,expanded`, `hir`, `hir,identified`, \
+                                        `hir,typed`, `hir-tree`, `mir` or `mir-cfg`; got {}",
                             name
                         ),
                     );
@@ -2233,6 +2235,14 @@ pub enum PpSourceMode {
     ExpandedHygiene,
 }
 
+#[derive(Copy, Clone, PartialEq, Debug)]
+pub enum PpAstTreeMode {
+    /// `-Zunpretty=ast`
+    Normal,
+    /// `-Zunpretty=ast,expanded`
+    Expanded,
+}
+
 #[derive(Copy, Clone, PartialEq, Debug)]
 pub enum PpHirMode {
     /// `-Zunpretty=hir`
@@ -2248,6 +2258,7 @@ pub enum PpMode {
     /// Options that print the source code, i.e.
     /// `--pretty` and `-Zunpretty=everybody_loops`
     Source(PpSourceMode),
+    AstTree(PpAstTreeMode),
     /// Options that print the HIR, i.e. `-Zunpretty=hir`
     Hir(PpHirMode),
     /// `-Zunpretty=hir-tree`
@@ -2263,9 +2274,10 @@ pub fn needs_ast_map(&self) -> bool {
         use PpMode::*;
         use PpSourceMode::*;
         match *self {
-            Source(Normal | Identified) => false,
+            Source(Normal | Identified) | AstTree(PpAstTreeMode::Normal) => false,
 
             Source(Expanded | EveryBodyLoops | ExpandedIdentified | ExpandedHygiene)
+            | AstTree(PpAstTreeMode::Expanded)
             | Hir(_)
             | HirTree
             | Mir
index e2b6b1dc2437b40cb8e58a169c98471bbdacfc11..6e7d39547a143e0758fc9fd3db519e718334fd13 100644 (file)
@@ -1158,6 +1158,8 @@ fn parse_split_debuginfo(slot: &mut Option<SplitDebuginfo>, v: Option<&str>) ->
         `expanded`, `expanded,identified`,
         `expanded,hygiene` (with internal representations),
         `everybody_loops` (all function bodies replaced with `loop {}`),
+        `ast-tree` (raw AST before expansion),
+        `ast-tree,expanded` (raw AST after expansion),
         `hir` (the HIR), `hir,identified`,
         `hir,typed` (HIR with types for each node),
         `hir-tree` (dump the raw HIR),