From da25539c1ab295ec40261109557dd4526923928c Mon Sep 17 00:00:00 2001 From: "Felix S. Klock II" Date: Tue, 8 Apr 2014 22:07:15 +0200 Subject: [PATCH] Generalized the pretty-print entry points to support `-o `. --- src/librustc/driver/driver.rs | 20 +++++++++++++++---- src/librustc/lib.rs | 2 +- .../run-make/pretty-print-to-file/Makefile | 5 +++++ .../run-make/pretty-print-to-file/input.pp | 13 ++++++++++++ .../run-make/pretty-print-to-file/input.rs | 15 ++++++++++++++ 5 files changed, 50 insertions(+), 5 deletions(-) create mode 100644 src/test/run-make/pretty-print-to-file/Makefile create mode 100644 src/test/run-make/pretty-print-to-file/input.pp create mode 100644 src/test/run-make/pretty-print-to-file/input.rs diff --git a/src/librustc/driver/driver.rs b/src/librustc/driver/driver.rs index 8a593d5f92a..1b3653c6948 100644 --- a/src/librustc/driver/driver.rs +++ b/src/librustc/driver/driver.rs @@ -664,7 +664,8 @@ impl pprust::PpAnn for TypedAnnotation { pub fn pretty_print_input(sess: Session, cfg: ast::CrateConfig, input: &Input, - ppm: PpMode) { + ppm: PpMode, + ofile: Option) { let krate = phase_1_parse_input(&sess, cfg, input); let id = link::find_crate_id(krate.attrs.as_slice(), input.filestem()); @@ -682,6 +683,17 @@ pub fn pretty_print_input(sess: Session, let src = Vec::from_slice(sess.codemap().get_filemap(src_name).src.as_bytes()); let mut rdr = MemReader::new(src); + let out = match ofile { + None => ~io::stdout() as ~Writer, + Some(p) => { + let r = io::File::create(&p); + match r { + Ok(w) => ~w as ~Writer, + Err(e) => fail!("print-print failed to open {} due to {}", + p.display(), e), + } + } + }; match ppm { PpmIdentified | PpmExpandedIdentified => { pprust::print_crate(sess.codemap(), @@ -689,7 +701,7 @@ pub fn pretty_print_input(sess: Session, &krate, src_name, &mut rdr, - ~io::stdout(), + out, &IdentifiedAnnotation, is_expanded) } @@ -704,7 +716,7 @@ pub fn pretty_print_input(sess: Session, &krate, src_name, &mut rdr, - ~io::stdout(), + out, &annotation, is_expanded) } @@ -714,7 +726,7 @@ pub fn pretty_print_input(sess: Session, &krate, src_name, &mut rdr, - ~io::stdout(), + out, &pprust::NoAnn, is_expanded) } diff --git a/src/librustc/lib.rs b/src/librustc/lib.rs index 3f72be673e0..20f9e868c30 100644 --- a/src/librustc/lib.rs +++ b/src/librustc/lib.rs @@ -293,7 +293,7 @@ pub fn run_compiler(args: &[~str]) { }); match pretty { Some::(ppm) => { - d::pretty_print_input(sess, cfg, &input, ppm); + d::pretty_print_input(sess, cfg, &input, ppm, ofile); return; } None:: => {/* continue */ } diff --git a/src/test/run-make/pretty-print-to-file/Makefile b/src/test/run-make/pretty-print-to-file/Makefile new file mode 100644 index 00000000000..1c1242ada8a --- /dev/null +++ b/src/test/run-make/pretty-print-to-file/Makefile @@ -0,0 +1,5 @@ +-include ../tools.mk + +all: + $(RUSTC) -o $(TMPDIR)/input.out --pretty=normal input.rs + diff -u $(TMPDIR)/input.out input.pp diff --git a/src/test/run-make/pretty-print-to-file/input.pp b/src/test/run-make/pretty-print-to-file/input.pp new file mode 100644 index 00000000000..a6dd6b6778e --- /dev/null +++ b/src/test/run-make/pretty-print-to-file/input.pp @@ -0,0 +1,13 @@ +// Copyright 2014 The Rust Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution and at +// http://rust-lang.org/COPYRIGHT. +// +// Licensed under the Apache License, Version 2.0 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + + +#[crate_type = "lib"] +pub fn foo() -> i32 { 45 } diff --git a/src/test/run-make/pretty-print-to-file/input.rs b/src/test/run-make/pretty-print-to-file/input.rs new file mode 100644 index 00000000000..8e3ec363187 --- /dev/null +++ b/src/test/run-make/pretty-print-to-file/input.rs @@ -0,0 +1,15 @@ +// Copyright 2014 The Rust Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution and at +// http://rust-lang.org/COPYRIGHT. +// +// Licensed under the Apache License, Version 2.0 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +#[crate_type="lib"] + +pub fn +foo() -> i32 +{ 45 }