Skip to content

Commit 496c8ba

Browse files
committed
rustc_driver: add env var RUSTC_PRETTY_DUMP=mode:dir which acts like --xpretty=mode -Z pretty-keep-going -Z pretty-dump-dir=dir.
1 parent c082ee2 commit 496c8ba

File tree

2 files changed

+91
-67
lines changed

2 files changed

+91
-67
lines changed

Diff for: src/librustc_driver/lib.rs

+9-47
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,6 @@ use std::cmp::Ordering::Equal;
7676
use std::old_io::{self, stdio};
7777
use std::iter::repeat;
7878
use std::env;
79-
use std::os;
8079
use std::sync::mpsc::channel;
8180
use std::thread;
8281

@@ -236,8 +235,7 @@ pub trait CompilerCalls<'a> {
236235
// CompilerCalls instance for a regular rustc build.
237236
pub struct RustcDefaultCalls {
238237
save_analysis: bool,
239-
pretty_print: Option<(PpMode, Option<UserIdentifiedItem>)>,
240-
pretty_dump_dir: Option<Path>
238+
pretty_print: Option<(PpMode, Option<UserIdentifiedItem>)>
241239
}
242240

243241
impl<'a> CompilerCalls<'a> for RustcDefaultCalls {
@@ -305,19 +303,13 @@ impl<'a> CompilerCalls<'a> for RustcDefaultCalls {
305303
if sess.unstable_options() {
306304
self.pretty_print = matches.opt_default("pretty", "normal").map(|a| {
307305
// stable pretty-print variants only
308-
pretty::parse_pretty(sess, &a, false)
306+
pretty::parse_pretty(&a, false).unwrap_or_else(|e| sess.fatal(&e))
309307
}).or_else(|| matches.opt_str("xpretty").map(|a| {
310308
// extended with unstable pretty-print variants
311-
pretty::parse_pretty(sess, &a, true)
309+
pretty::parse_pretty(&a, true).unwrap_or_else(|e| sess.fatal(&e))
312310
}));
313311
}
314312

315-
if let Some(ref dir) = sess.opts.debugging_opts.pretty_dump_dir {
316-
let pretty_dump_dir = os::getcwd().unwrap().join(dir);
317-
assert!(pretty_dump_dir.is_absolute());
318-
self.pretty_dump_dir = Some(pretty_dump_dir);
319-
}
320-
321313
RustcDefaultCalls::print_crate_info(sess, Some(input), odir, ofile).and_then(
322314
|| RustcDefaultCalls::list_metadata(sess, matches, input))
323315
}
@@ -343,40 +335,11 @@ impl<'a> CompilerCalls<'a> for RustcDefaultCalls {
343335
control.after_llvm.stop = Compilation::Stop;
344336
}
345337

346-
if let Some((ppm, opt_uii)) = self.pretty_print.take() {
347-
let phase = pretty::printing_phase(&mut control, ppm, opt_uii.as_ref());
348-
349-
let dump_dir = self.pretty_dump_dir.take();
350-
phase.callback = box move |state| {
351-
let pretty_output_path;
352-
let output = if let Some(ref dir) = dump_dir {
353-
let file_path = if let Some(outputs) = state.output_filenames {
354-
outputs.with_extension("rs")
355-
} else {
356-
state.session.fatal(
357-
"-Z pretty-dump-dir cannot be used with --pretty \
358-
options that print before expansion");
359-
};
360-
let file_path = os::getcwd().unwrap().join(&file_path);
361-
assert!(file_path.is_absolute());
362-
363-
// Cheap isomorphism: /foo/bar--bar/baz <-> foo--bar----bar--baz.
364-
let components: Vec<_> = file_path.components().map(|bytes| {
365-
String::from_utf8_lossy(bytes).replace("--", "----")
366-
}).collect();
367-
368-
pretty_output_path = dir.join(components.connect("--"));
369-
Some(&pretty_output_path)
370-
} else {
371-
state.output
372-
};
373-
pretty::print_from_phase(state, ppm, opt_uii.as_ref(), output).unwrap();
374-
};
375-
376-
if !sess.opts.debugging_opts.pretty_keep_going {
377-
phase.stop = Compilation::Stop;
378-
}
379-
}
338+
pretty::setup_controller(&mut control,
339+
self.pretty_print.take(),
340+
sess.opts.debugging_opts.pretty_dump_dir
341+
.as_ref().map(|s| &s[..]),
342+
sess.opts.debugging_opts.pretty_keep_going);
380343

381344
if self.save_analysis {
382345
control.after_analysis.callback = box |state| {
@@ -398,8 +361,7 @@ impl RustcDefaultCalls {
398361
pub fn new() -> RustcDefaultCalls {
399362
RustcDefaultCalls {
400363
save_analysis: false,
401-
pretty_print: None,
402-
pretty_dump_dir: None
364+
pretty_print: None
403365
}
404366
}
405367

Diff for: src/librustc_driver/pretty.rs

+82-20
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,10 @@ use syntax::ptr::P;
3535
use graphviz as dot;
3636

3737
use std::borrow::{Cow, IntoCow};
38+
use std::env;
3839
use std::old_io::{self, BufReader};
3940
use std::option;
41+
use std::os;
4042
use std::str::FromStr;
4143

4244
#[derive(Copy, PartialEq, Debug)]
@@ -67,9 +69,8 @@ pub enum PpMode {
6769
PpmFlowGraph(PpFlowGraphMode),
6870
}
6971

70-
pub fn parse_pretty(sess: &Session,
71-
name: &str,
72-
extended: bool) -> (PpMode, Option<UserIdentifiedItem>) {
72+
pub fn parse_pretty(name: &str, extended: bool)
73+
-> Result<(PpMode, Option<UserIdentifiedItem>), String> {
7374
let mut split = name.splitn(1, '=');
7475
let first = split.next().unwrap();
7576
let opt_second = split.next();
@@ -84,23 +85,23 @@ pub fn parse_pretty(sess: &Session,
8485
("identified", _) => PpmSource(PpmIdentified),
8586
("flowgraph", true) => PpmFlowGraph(PpFlowGraphMode::Default),
8687
("flowgraph,unlabelled", true) => PpmFlowGraph(PpFlowGraphMode::UnlabelledEdges),
87-
_ => {
88+
_ => return Err({
8889
if extended {
89-
sess.fatal(&format!(
90+
format!(
9091
"argument to `xpretty` must be one of `normal`, \
9192
`expanded`, `flowgraph[,unlabelled]=<nodeid>`, `typed`, \
9293
`typed,unsuffixed_literals`, `identified`, \
93-
`expanded,identified`, or `everybody_loops`; got {}", name));
94+
`expanded,identified`, or `everybody_loops`; got {}", name)
9495
} else {
95-
sess.fatal(&format!(
96+
format!(
9697
"argument to `pretty` must be one of `normal`, \
9798
`expanded`, `typed`, `identified`, \
98-
or `expanded,identified`; got {}", name));
99+
or `expanded,identified`; got {}", name)
99100
}
100-
}
101+
})
101102
};
102103
let opt_second = opt_second.and_then(|s| s.parse::<UserIdentifiedItem>().ok());
103-
(first, opt_second)
104+
Ok((first, opt_second))
104105
}
105106

106107
struct NoAnn;
@@ -391,15 +392,46 @@ impl fold::Folder for ReplaceBodyWithLoop {
391392
}
392393
}
393394

394-
pub fn printing_phase<'a, 'b>(control: &'a mut driver::CompileController<'b>,
395-
ppm: PpMode,
396-
opt_uii: Option<&UserIdentifiedItem>)
397-
-> &'a mut driver::PhaseController<'b> {
395+
pub fn setup_controller(control: &mut driver::CompileController,
396+
ppm_and_uui: Option<(PpMode, Option<UserIdentifiedItem>)>,
397+
dump_dir: Option<&str>,
398+
keep_going: bool) {
399+
400+
fn mk_absolute(path: &str) -> Path {
401+
let path = os::getcwd().unwrap().join(path);
402+
assert!(path.is_absolute());
403+
path
404+
}
405+
406+
let (ppm, opt_uii, dump_dir, keep_going) = match ppm_and_uui {
407+
Some((ppm, opt_uii)) => {
408+
(ppm, opt_uii, dump_dir.map(mk_absolute), keep_going)
409+
}
410+
None => {
411+
let decoded = env::var("RUSTC_PRETTY_DUMP").ok().and_then(|s| {
412+
let mut s = s.split(":");
413+
414+
s.next().and_then(|mode| {
415+
parse_pretty(mode, true).ok()
416+
}).and_then(|(ppm, opt_uii)| {
417+
s.next().map(|dump_dir| {
418+
(ppm, opt_uii, Some(mk_absolute(dump_dir)), true)
419+
})
420+
})
421+
});
422+
if let Some(parts) = decoded {
423+
parts
424+
} else {
425+
return;
426+
}
427+
}
428+
};
429+
398430
if ppm == PpmSource(PpmEveryBodyLoops) {
399431
control.every_body_loops = true;
400432
}
401433

402-
match ppm {
434+
let phase = match ppm {
403435
PpmSource(PpmNormal) |
404436
PpmSource(PpmEveryBodyLoops) |
405437
PpmSource(PpmIdentified) => {
@@ -421,14 +453,44 @@ pub fn printing_phase<'a, 'b>(control: &'a mut driver::CompileController<'b>,
421453
PpmFlowGraph(_) => {
422454
&mut control.after_analysis
423455
}
456+
};
457+
458+
phase.callback = box move |state| {
459+
let pretty_output_path;
460+
let output = if let Some(ref dir) = dump_dir {
461+
let file_path = if let Some(outputs) = state.output_filenames {
462+
outputs.with_extension("rs")
463+
} else {
464+
state.session.fatal(
465+
"-Z pretty-dump-dir cannot be used with --pretty \
466+
options that print before expansion");
467+
};
468+
let file_path = os::getcwd().unwrap().join(&file_path);
469+
assert!(file_path.is_absolute());
470+
471+
// Cheap isomorphism: /foo/bar--bar/baz <-> foo--bar----bar--baz.
472+
let components: Vec<_> = file_path.components().map(|bytes| {
473+
String::from_utf8_lossy(bytes).replace("--", "----")
474+
}).collect();
475+
476+
pretty_output_path = dir.join(components.connect("--"));
477+
Some(&pretty_output_path)
478+
} else {
479+
state.output
480+
};
481+
print_from_phase(state, ppm, opt_uii.as_ref(), output).unwrap();
482+
};
483+
484+
if !keep_going {
485+
phase.stop = ::Compilation::Stop;
424486
}
425487
}
426488

427-
pub fn print_from_phase(state: driver::CompileState,
428-
ppm: PpMode,
429-
opt_uii: Option<&UserIdentifiedItem>,
430-
output: Option<&Path>)
431-
-> old_io::IoResult<()> {
489+
fn print_from_phase(state: driver::CompileState,
490+
ppm: PpMode,
491+
opt_uii: Option<&UserIdentifiedItem>,
492+
output: Option<&Path>)
493+
-> old_io::IoResult<()> {
432494
let sess = state.session;
433495
let krate = state.krate.or(state.expanded_crate)
434496
.expect("--pretty=typed missing crate");

0 commit comments

Comments
 (0)