diff --git a/src/librustc/driver/driver.rs b/src/librustc/driver/driver.rs index 0668abea2b12f..6fdfffc77f676 100644 --- a/src/librustc/driver/driver.rs +++ b/src/librustc/driver/driver.rs @@ -666,7 +666,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()); @@ -684,6 +685,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(), @@ -691,7 +703,7 @@ pub fn pretty_print_input(sess: Session, &krate, src_name, &mut rdr, - ~io::stdout(), + out, &IdentifiedAnnotation, is_expanded) } @@ -706,7 +718,7 @@ pub fn pretty_print_input(sess: Session, &krate, src_name, &mut rdr, - ~io::stdout(), + out, &annotation, is_expanded) } @@ -716,7 +728,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 95627cd1039af..0918b06574270 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 0000000000000..1c1242ada8a6d --- /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 0000000000000..a6dd6b6778ed0 --- /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 0000000000000..8e3ec36318749 --- /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 }