Skip to content

Commit 0a26495

Browse files
committed
Generalized the pretty-print entry points to support -o <file>.
1 parent 0deb16a commit 0a26495

File tree

5 files changed

+50
-5
lines changed

5 files changed

+50
-5
lines changed

src/librustc/driver/driver.rs

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -666,7 +666,8 @@ impl pprust::PpAnn for TypedAnnotation {
666666
pub fn pretty_print_input(sess: Session,
667667
cfg: ast::CrateConfig,
668668
input: &Input,
669-
ppm: PpMode) {
669+
ppm: PpMode,
670+
ofile: Option<Path>) {
670671
let krate = phase_1_parse_input(&sess, cfg, input);
671672
let id = link::find_crate_id(krate.attrs.as_slice(), input.filestem());
672673

@@ -684,14 +685,25 @@ pub fn pretty_print_input(sess: Session,
684685
let src = Vec::from_slice(sess.codemap().get_filemap(src_name).src.as_bytes());
685686
let mut rdr = MemReader::new(src);
686687

688+
let out = match ofile {
689+
None => ~io::stdout() as ~Writer,
690+
Some(p) => {
691+
let r = io::File::create(&p);
692+
match r {
693+
Ok(w) => ~w as ~Writer,
694+
Err(e) => fail!("print-print failed to open {} due to {}",
695+
p.display(), e),
696+
}
697+
}
698+
};
687699
match ppm {
688700
PpmIdentified | PpmExpandedIdentified => {
689701
pprust::print_crate(sess.codemap(),
690702
sess.diagnostic(),
691703
&krate,
692704
src_name,
693705
&mut rdr,
694-
~io::stdout(),
706+
out,
695707
&IdentifiedAnnotation,
696708
is_expanded)
697709
}
@@ -706,7 +718,7 @@ pub fn pretty_print_input(sess: Session,
706718
&krate,
707719
src_name,
708720
&mut rdr,
709-
~io::stdout(),
721+
out,
710722
&annotation,
711723
is_expanded)
712724
}
@@ -716,7 +728,7 @@ pub fn pretty_print_input(sess: Session,
716728
&krate,
717729
src_name,
718730
&mut rdr,
719-
~io::stdout(),
731+
out,
720732
&pprust::NoAnn,
721733
is_expanded)
722734
}

src/librustc/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -293,7 +293,7 @@ pub fn run_compiler(args: &[~str]) {
293293
});
294294
match pretty {
295295
Some::<d::PpMode>(ppm) => {
296-
d::pretty_print_input(sess, cfg, &input, ppm);
296+
d::pretty_print_input(sess, cfg, &input, ppm, ofile);
297297
return;
298298
}
299299
None::<d::PpMode> => {/* continue */ }
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
-include ../tools.mk
2+
3+
all:
4+
$(RUSTC) -o $(TMPDIR)/input.out --pretty=normal input.rs
5+
diff -u $(TMPDIR)/input.out input.pp
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
12+
#[crate_type = "lib"]
13+
pub fn foo() -> i32 { 45 }
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
#[crate_type="lib"]
12+
13+
pub fn
14+
foo() -> i32
15+
{ 45 }

0 commit comments

Comments
 (0)