]> git.lizzy.rs Git - rust.git/commitdiff
Generalized the pretty-print entry points to support `-o <file>`.
authorFelix S. Klock II <pnkfelix@pnkfx.org>
Tue, 8 Apr 2014 20:07:15 +0000 (22:07 +0200)
committerAlex Crichton <alex@alexcrichton.com>
Thu, 10 Apr 2014 22:21:59 +0000 (15:21 -0700)
src/librustc/driver/driver.rs
src/librustc/lib.rs
src/test/run-make/pretty-print-to-file/Makefile [new file with mode: 0644]
src/test/run-make/pretty-print-to-file/input.pp [new file with mode: 0644]
src/test/run-make/pretty-print-to-file/input.rs [new file with mode: 0644]

index 8a593d5f92a2add5a7ba6a3bae490598f8b76be6..1b3653c6948a699910344b7f12e88c22c2ec1a12 100644 (file)
@@ -664,7 +664,8 @@ fn post(&self,
 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());
 
@@ -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)
         }
index 3f72be673e0a00aae4c3f9f1f134dbe52cd97a33..20f9e868c30e14e3ab365216386bebf155610bc5 100644 (file)
@@ -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 */ }
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 (file)
index 0000000..1c1242a
--- /dev/null
@@ -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 (file)
index 0000000..a6dd6b6
--- /dev/null
@@ -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 }
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 (file)
index 0000000..8e3ec36
--- /dev/null
@@ -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 }