]> git.lizzy.rs Git - rust.git/blobdiff - src/librustc_driver/pretty.rs
Rollup merge of #68074 - matthew-healy:skip-llvm-rebuild-option, r=Centril
[rust.git] / src / librustc_driver / pretty.rs
index 11603f6d9c121c10cfe739747fe2d92cde08e64f..8804a05b596ee1fa421ac6f6aaa97ff5614f05c7 100644 (file)
@@ -1,26 +1,26 @@
 //! The various pretty-printing routines.
 
-use rustc::hir;
 use rustc::hir::map as hir_map;
-use rustc::hir::print as pprust_hir;
-use rustc::hir::def_id::LOCAL_CRATE;
+use rustc::session::config::{Input, PpMode, PpSourceMode};
 use rustc::session::Session;
-use rustc::session::config::{PpMode, PpSourceMode, Input};
 use rustc::ty::{self, TyCtxt};
 use rustc::util::common::ErrorReported;
-use rustc_mir::util::{write_mir_pretty, write_mir_graphviz};
+use rustc_hir as hir;
+use rustc_hir::def_id::LOCAL_CRATE;
+use rustc_hir::print as pprust_hir;
+use rustc_mir::util::{write_mir_graphviz, write_mir_pretty};
 
+use rustc_span::FileName;
 use syntax::ast;
-use syntax::print::{pprust};
-use syntax_pos::FileName;
+use syntax::print::pprust;
 
 use std::cell::Cell;
 use std::fs::File;
 use std::io::Write;
 use std::path::Path;
 
-pub use self::PpSourceMode::*;
 pub use self::PpMode::*;
+pub use self::PpSourceMode::*;
 use crate::abort_on_err;
 
 // This slightly awkward construction is to allow for each PpMode to
@@ -48,24 +48,16 @@ fn call_with_pp_support<'tcx, A, F>(
 {
     match *ppmode {
         PpmNormal | PpmEveryBodyLoops | PpmExpanded => {
-            let annotation = NoAnn {
-                sess,
-                tcx,
-            };
+            let annotation = NoAnn { sess, tcx };
             f(&annotation)
         }
 
         PpmIdentified | PpmExpandedIdentified => {
-            let annotation = IdentifiedAnnotation {
-                sess,
-                tcx,
-            };
+            let annotation = IdentifiedAnnotation { sess, tcx };
             f(&annotation)
         }
         PpmExpandedHygiene => {
-            let annotation = HygieneAnnotation {
-                sess,
-            };
+            let annotation = HygieneAnnotation { sess };
             f(&annotation)
         }
         _ => panic!("Should use call_with_pp_support_hir"),
@@ -73,35 +65,24 @@ fn call_with_pp_support<'tcx, A, F>(
 }
 fn call_with_pp_support_hir<A, F>(ppmode: &PpSourceMode, tcx: TyCtxt<'_>, f: F) -> A
 where
-    F: FnOnce(&dyn HirPrinterSupport<'_>, &hir::Crate) -> A,
+    F: FnOnce(&dyn HirPrinterSupport<'_>, &hir::Crate<'_>) -> A,
 {
     match *ppmode {
         PpmNormal => {
-            let annotation = NoAnn {
-                sess: tcx.sess,
-                tcx: Some(tcx),
-            };
+            let annotation = NoAnn { sess: tcx.sess, tcx: Some(tcx) };
             f(&annotation, tcx.hir().forest.krate())
         }
 
         PpmIdentified => {
-            let annotation = IdentifiedAnnotation {
-                sess: tcx.sess,
-                tcx: Some(tcx),
-            };
+            let annotation = IdentifiedAnnotation { sess: tcx.sess, tcx: Some(tcx) };
             f(&annotation, tcx.hir().forest.krate())
         }
         PpmTyped => {
             abort_on_err(tcx.analysis(LOCAL_CRATE), tcx.sess);
 
             let empty_tables = ty::TypeckTables::empty(None);
-            let annotation = TypedAnnotation {
-                tcx,
-                tables: Cell::new(&empty_tables)
-            };
-            tcx.dep_graph.with_ignore(|| {
-                f(&annotation, tcx.hir().forest.krate())
-            })
+            let annotation = TypedAnnotation { tcx, tables: Cell::new(&empty_tables) };
+            tcx.dep_graph.with_ignore(|| f(&annotation, tcx.hir().forest.krate()))
         }
         _ => panic!("Should use call_with_pp_support"),
     }
@@ -136,14 +117,8 @@ trait HirPrinterSupport<'hir>: pprust_hir::PpAnn {
 
     /// Computes an user-readable representation of a path, if possible.
     fn node_path(&self, id: hir::HirId) -> Option<String> {
-        self.hir_map().and_then(|map| {
-            map.def_path_from_hir_id(id)
-        }).map(|path| {
-            path.data
-                .into_iter()
-                .map(|elem| elem.data.to_string())
-                .collect::<Vec<_>>()
-                .join("::")
+        self.hir_map().and_then(|map| map.def_path_from_hir_id(id)).map(|path| {
+            path.data.into_iter().map(|elem| elem.data.to_string()).collect::<Vec<_>>().join("::")
         })
     }
 }
@@ -210,9 +185,7 @@ fn pre(&self, s: &mut pprust::State<'_>, node: pprust::AnnNode<'_>) {
     }
     fn post(&self, s: &mut pprust::State<'_>, node: pprust::AnnNode<'_>) {
         match node {
-            pprust::AnnNode::Crate(_) |
-            pprust::AnnNode::Ident(_) |
-            pprust::AnnNode::Name(_) => {},
+            pprust::AnnNode::Crate(_) | pprust::AnnNode::Ident(_) | pprust::AnnNode::Name(_) => {}
 
             pprust::AnnNode::Item(item) => {
                 s.s.space();
@@ -267,7 +240,7 @@ fn pre(&self, s: &mut pprust_hir::State<'_>, node: pprust_hir::AnnNode<'_>) {
     }
     fn post(&self, s: &mut pprust_hir::State<'_>, node: pprust_hir::AnnNode<'_>) {
         match node {
-            pprust_hir::AnnNode::Name(_) => {},
+            pprust_hir::AnnNode::Name(_) => {}
             pprust_hir::AnnNode::Item(item) => {
                 s.s.space();
                 s.synth_comment(format!("hir_id: {}", item.hir_id));
@@ -298,7 +271,7 @@ fn post(&self, s: &mut pprust_hir::State<'_>, node: pprust_hir::AnnNode<'_>) {
 }
 
 struct HygieneAnnotation<'a> {
-    sess: &'a Session
+    sess: &'a Session,
 }
 
 impl<'a> PrinterSupport for HygieneAnnotation<'a> {
@@ -325,7 +298,7 @@ fn post(&self, s: &mut pprust::State<'_>, node: pprust::AnnNode<'_>) {
             pprust::AnnNode::Crate(_) => {
                 s.s.hardbreak();
                 let verbose = self.sess.verbose();
-                s.synth_comment(syntax_pos::hygiene::debug_hygiene_data(verbose));
+                s.synth_comment(rustc_span::hygiene::debug_hygiene_data(verbose));
                 s.s.hardbreak_if_not_bol();
             }
             _ => {}
@@ -380,39 +353,35 @@ fn post(&self, s: &mut pprust_hir::State<'_>, node: pprust_hir::AnnNode<'_>) {
                 s.s.word(self.tables.get().expr_ty(expr).to_string());
                 s.pclose();
             }
-            _ => {},
+            _ => {}
         }
     }
 }
 
 fn get_source(input: &Input, sess: &Session) -> (String, FileName) {
     let src_name = input.source_name();
-    let src = String::clone(&sess.source_map()
-        .get_source_file(&src_name)
-        .unwrap()
-        .src
-        .as_ref()
-        .unwrap());
+    let src =
+        String::clone(&sess.source_map().get_source_file(&src_name).unwrap().src.as_ref().unwrap());
     (src, src_name)
 }
 
 fn write_output(out: Vec<u8>, ofile: Option<&Path>) {
     match ofile {
         None => print!("{}", String::from_utf8(out).unwrap()),
-        Some(p) => {
-            match File::create(p) {
-                Ok(mut w) => w.write_all(&out).unwrap(),
-                Err(e) => panic!("print-print failed to open {} due to {}", p.display(), e),
-            }
-        }
+        Some(p) => match File::create(p) {
+            Ok(mut w) => w.write_all(&out).unwrap(),
+            Err(e) => panic!("print-print failed to open {} due to {}", p.display(), e),
+        },
     }
 }
 
-pub fn print_after_parsing(sess: &Session,
-                           input: &Input,
-                           krate: &ast::Crate,
-                           ppm: PpMode,
-                           ofile: Option<&Path>) {
+pub fn print_after_parsing(
+    sess: &Session,
+    input: &Input,
+    krate: &ast::Crate,
+    ppm: PpMode,
+    ofile: Option<&Path>,
+) {
     let (src, src_name) = get_source(input, sess);
 
     let mut out = String::new();
@@ -423,13 +392,15 @@ pub fn print_after_parsing(sess: &Session,
         call_with_pp_support(&s, sess, None, move |annotation| {
             debug!("pretty printing source code {:?}", s);
             let sess = annotation.sess();
-            *out = pprust::print_crate(sess.source_map(),
-                                &sess.parse_sess,
-                                krate,
-                                src_name,
-                                src,
-                                annotation.pp_ann(),
-                                false)
+            *out = pprust::print_crate(
+                sess.source_map(),
+                &sess.parse_sess,
+                krate,
+                src_name,
+                src,
+                annotation.pp_ann(),
+                false,
+            )
         })
     } else {
         unreachable!();
@@ -446,11 +417,7 @@ pub fn print_after_hir_lowering<'tcx>(
     ofile: Option<&Path>,
 ) {
     if ppm.needs_analysis() {
-        abort_on_err(print_with_analysis(
-            tcx,
-            ppm,
-            ofile
-        ), tcx.sess);
+        abort_on_err(print_with_analysis(tcx, ppm, ofile), tcx.sess);
         return;
     }
 
@@ -459,49 +426,53 @@ pub fn print_after_hir_lowering<'tcx>(
     let mut out = String::new();
 
     match ppm {
-            PpmSource(s) => {
-                // Silently ignores an identified node.
-                let out = &mut out;
-                let src = src.clone();
-                call_with_pp_support(&s, tcx.sess, Some(tcx), move |annotation| {
-                    debug!("pretty printing source code {:?}", s);
-                    let sess = annotation.sess();
-                    *out = pprust::print_crate(sess.source_map(),
-                                        &sess.parse_sess,
-                                        krate,
-                                        src_name,
-                                        src,
-                                        annotation.pp_ann(),
-                                        true)
-                })
-            }
-
-            PpmHir(s) => {
-                let out = &mut out;
-                let src = src.clone();
-                call_with_pp_support_hir(&s, tcx, move |annotation, krate| {
-                    debug!("pretty printing source code {:?}", s);
-                    let sess = annotation.sess();
-                    *out = pprust_hir::print_crate(sess.source_map(),
-                                            &sess.parse_sess,
-                                            krate,
-                                            src_name,
-                                            src,
-                                            annotation.pp_ann())
-                })
-            }
+        PpmSource(s) => {
+            // Silently ignores an identified node.
+            let out = &mut out;
+            let src = src.clone();
+            call_with_pp_support(&s, tcx.sess, Some(tcx), move |annotation| {
+                debug!("pretty printing source code {:?}", s);
+                let sess = annotation.sess();
+                *out = pprust::print_crate(
+                    sess.source_map(),
+                    &sess.parse_sess,
+                    krate,
+                    src_name,
+                    src,
+                    annotation.pp_ann(),
+                    true,
+                )
+            })
+        }
 
-            PpmHirTree(s) => {
-                let out = &mut out;
-                call_with_pp_support_hir(&s, tcx, move |_annotation, krate| {
-                    debug!("pretty printing source code {:?}", s);
-                    *out = format!("{:#?}", krate);
-                });
-            }
+        PpmHir(s) => {
+            let out = &mut out;
+            let src = src.clone();
+            call_with_pp_support_hir(&s, tcx, move |annotation, krate| {
+                debug!("pretty printing source code {:?}", s);
+                let sess = annotation.sess();
+                *out = pprust_hir::print_crate(
+                    sess.source_map(),
+                    &sess.parse_sess,
+                    krate,
+                    src_name,
+                    src,
+                    annotation.pp_ann(),
+                )
+            })
+        }
 
-            _ => unreachable!(),
+        PpmHirTree(s) => {
+            let out = &mut out;
+            call_with_pp_support_hir(&s, tcx, move |_annotation, krate| {
+                debug!("pretty printing source code {:?}", s);
+                *out = format!("{:#?}", krate);
+            });
         }
 
+        _ => unreachable!(),
+    }
+
     write_output(out.into_bytes(), ofile);
 }
 
@@ -519,15 +490,14 @@ fn print_with_analysis(
     tcx.analysis(LOCAL_CRATE)?;
 
     match ppm {
-        PpmMir | PpmMirCFG => {
-            match ppm {
-                PpmMir => write_mir_pretty(tcx, None, &mut out),
-                PpmMirCFG => write_mir_graphviz(tcx, None, &mut out),
-                _ => unreachable!(),
-            }
-        }
+        PpmMir | PpmMirCFG => match ppm {
+            PpmMir => write_mir_pretty(tcx, None, &mut out),
+            PpmMirCFG => write_mir_graphviz(tcx, None, &mut out),
+            _ => unreachable!(),
+        },
         _ => unreachable!(),
-    }.unwrap();
+    }
+    .unwrap();
 
     write_output(out, ofile);