Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generalized the pretty-print entry points to support -o <file>. #13406

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 16 additions & 4 deletions src/librustc/driver/driver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Path>) {
let krate = phase_1_parse_input(&sess, cfg, input);
let id = link::find_crate_id(krate.attrs.as_slice(), input.filestem());

Expand All @@ -684,14 +685,25 @@ 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(),
sess.diagnostic(),
&krate,
src_name,
&mut rdr,
~io::stdout(),
out,
&IdentifiedAnnotation,
is_expanded)
}
Expand All @@ -706,7 +718,7 @@ pub fn pretty_print_input(sess: Session,
&krate,
src_name,
&mut rdr,
~io::stdout(),
out,
&annotation,
is_expanded)
}
Expand All @@ -716,7 +728,7 @@ pub fn pretty_print_input(sess: Session,
&krate,
src_name,
&mut rdr,
~io::stdout(),
out,
&pprust::NoAnn,
is_expanded)
}
Expand Down
2 changes: 1 addition & 1 deletion src/librustc/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,7 @@ pub fn run_compiler(args: &[~str]) {
});
match pretty {
Some::<d::PpMode>(ppm) => {
d::pretty_print_input(sess, cfg, &input, ppm);
d::pretty_print_input(sess, cfg, &input, ppm, ofile);
return;
}
None::<d::PpMode> => {/* continue */ }
Expand Down
5 changes: 5 additions & 0 deletions src/test/run-make/pretty-print-to-file/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
-include ../tools.mk

all:
$(RUSTC) -o $(TMPDIR)/input.out --pretty=normal input.rs
diff -u $(TMPDIR)/input.out input.pp
13 changes: 13 additions & 0 deletions src/test/run-make/pretty-print-to-file/input.pp
Original file line number Diff line number Diff line change
@@ -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 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, 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 }
15 changes: 15 additions & 0 deletions src/test/run-make/pretty-print-to-file/input.rs
Original file line number Diff line number Diff line change
@@ -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 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, 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 }