Skip to content

Commit

Permalink
Generalized the pretty-print entry points to support -o <file>.
Browse files Browse the repository at this point in the history
  • Loading branch information
pnkfelix committed Apr 8, 2014
1 parent 0deb16a commit 0a26495
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 5 deletions.
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 }

5 comments on commit 0a26495

@bors
Copy link
Contributor

@bors bors commented on 0a26495 Apr 9, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

saw approval from alexcrichton
at pnkfelix@0a26495

@bors
Copy link
Contributor

@bors bors commented on 0a26495 Apr 9, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

merging pnkfelix/rust/fsk-make-pp-respect-ofile = 0a26495 into auto

@bors
Copy link
Contributor

@bors bors commented on 0a26495 Apr 9, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

pnkfelix/rust/fsk-make-pp-respect-ofile = 0a26495 merged ok, testing candidate = 44466344

@bors
Copy link
Contributor

@bors bors commented on 0a26495 Apr 9, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@pnkfelix
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bors: retry

Please sign in to comment.