Skip to content

Commit d4945b4

Browse files
committed
rustc_driver: move pretty-printing to the new driver controller API.
1 parent e70c9a0 commit d4945b4

File tree

3 files changed

+121
-172
lines changed

3 files changed

+121
-172
lines changed

src/librustc_driver/driver.rs

Lines changed: 38 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -69,10 +69,18 @@ pub fn compile_input(sess: Session,
6969
let (outputs, expanded_crate, id) = {
7070
let krate = phase_1_parse_input(&sess, cfg, input);
7171

72+
let krate = if control.every_body_loops {
73+
use syntax::fold::Folder;
74+
::pretty::ReplaceBodyWithLoop::new().fold_crate(krate)
75+
} else {
76+
krate
77+
};
78+
7279
controller_entry_point!(after_parse,
7380
CompileState::state_after_parse(input,
7481
&sess,
7582
outdir,
83+
output,
7684
&krate));
7785

7886
let outputs = build_output_filenames(input,
@@ -99,6 +107,7 @@ pub fn compile_input(sess: Session,
99107
CompileState::state_after_expand(input,
100108
&sess,
101109
outdir,
110+
output,
102111
&expanded_crate,
103112
&id[..]));
104113

@@ -112,6 +121,7 @@ pub fn compile_input(sess: Session,
112121
CompileState::state_after_write_deps(input,
113122
&sess,
114123
outdir,
124+
output,
115125
&ast_map,
116126
&ast_map.krate(),
117127
&id[..]));
@@ -126,6 +136,7 @@ pub fn compile_input(sess: Session,
126136
CompileState::state_after_analysis(input,
127137
&analysis.ty_cx.sess,
128138
outdir,
139+
output,
129140
analysis.ty_cx.map.krate(),
130141
&analysis,
131142
&analysis.ty_cx));
@@ -152,6 +163,7 @@ pub fn compile_input(sess: Session,
152163
CompileState::state_after_llvm(input,
153164
&sess,
154165
outdir,
166+
output,
155167
&trans));
156168

157169
phase_6_link_output(&sess, &trans, &outputs);
@@ -193,6 +205,7 @@ pub struct CompileController<'a> {
193205
pub after_llvm: PhaseController<'a>,
194206

195207
pub make_glob_map: resolve::MakeGlobMap,
208+
pub every_body_loops: bool,
196209
}
197210

198211
impl<'a> CompileController<'a> {
@@ -204,6 +217,7 @@ impl<'a> CompileController<'a> {
204217
after_analysis: PhaseController::basic(),
205218
after_llvm: PhaseController::basic(),
206219
make_glob_map: resolve::MakeGlobMap::No,
220+
every_body_loops: false,
207221
}
208222
}
209223
}
@@ -225,30 +239,33 @@ impl<'a> PhaseController<'a> {
225239
/// State that is passed to a callback. What state is available depends on when
226240
/// during compilation the callback is made. See the various constructor methods
227241
/// (`state_*`) in the impl to see which data is provided for any given entry point.
228-
pub struct CompileState<'a, 'ast: 'a, 'tcx: 'a> {
242+
pub struct CompileState<'a, 'tcx: 'a> {
229243
pub input: &'a Input,
230244
pub session: &'a Session,
231245
pub cfg: Option<&'a ast::CrateConfig>,
232246
pub krate: Option<&'a ast::Crate>,
233247
pub crate_name: Option<&'a str>,
234248
pub output_filenames: Option<&'a OutputFilenames>,
235249
pub out_dir: Option<&'a Path>,
250+
pub output: Option<&'a Path>,
236251
pub expanded_crate: Option<&'a ast::Crate>,
237-
pub ast_map: Option<&'a ast_map::Map<'ast>>,
252+
pub ast_map: Option<&'a ast_map::Map<'tcx>>,
238253
pub analysis: Option<&'a ty::CrateAnalysis<'tcx>>,
239254
pub tcx: Option<&'a ty::ctxt<'tcx>>,
240255
pub trans: Option<&'a trans::CrateTranslation>,
241256
}
242257

243-
impl<'a, 'ast, 'tcx> CompileState<'a, 'ast, 'tcx> {
258+
impl<'a, 'tcx> CompileState<'a, 'tcx> {
244259
fn empty(input: &'a Input,
245260
session: &'a Session,
246-
out_dir: &'a Option<Path>)
247-
-> CompileState<'a, 'ast, 'tcx> {
261+
out_dir: &'a Option<Path>,
262+
output: &'a Option<Path>)
263+
-> CompileState<'a, 'tcx> {
248264
CompileState {
249265
input: input,
250266
session: session,
251267
out_dir: out_dir.as_ref(),
268+
output: output.as_ref(),
252269
cfg: None,
253270
krate: None,
254271
crate_name: None,
@@ -264,66 +281,71 @@ impl<'a, 'ast, 'tcx> CompileState<'a, 'ast, 'tcx> {
264281
fn state_after_parse(input: &'a Input,
265282
session: &'a Session,
266283
out_dir: &'a Option<Path>,
284+
output: &'a Option<Path>,
267285
krate: &'a ast::Crate)
268-
-> CompileState<'a, 'ast, 'tcx> {
286+
-> CompileState<'a, 'tcx> {
269287
CompileState {
270288
krate: Some(krate),
271-
.. CompileState::empty(input, session, out_dir)
289+
.. CompileState::empty(input, session, out_dir, output)
272290
}
273291
}
274292

275293
fn state_after_expand(input: &'a Input,
276294
session: &'a Session,
277295
out_dir: &'a Option<Path>,
296+
output: &'a Option<Path>,
278297
expanded_crate: &'a ast::Crate,
279298
crate_name: &'a str)
280-
-> CompileState<'a, 'ast, 'tcx> {
299+
-> CompileState<'a, 'tcx> {
281300
CompileState {
282301
crate_name: Some(crate_name),
283302
expanded_crate: Some(expanded_crate),
284-
.. CompileState::empty(input, session, out_dir)
303+
.. CompileState::empty(input, session, out_dir, output)
285304
}
286305
}
287306

288307
fn state_after_write_deps(input: &'a Input,
289308
session: &'a Session,
290309
out_dir: &'a Option<Path>,
291-
ast_map: &'a ast_map::Map<'ast>,
310+
output: &'a Option<Path>,
311+
ast_map: &'a ast_map::Map<'tcx>,
292312
expanded_crate: &'a ast::Crate,
293313
crate_name: &'a str)
294-
-> CompileState<'a, 'ast, 'tcx> {
314+
-> CompileState<'a, 'tcx> {
295315
CompileState {
296316
crate_name: Some(crate_name),
297317
ast_map: Some(ast_map),
298318
expanded_crate: Some(expanded_crate),
299-
.. CompileState::empty(input, session, out_dir)
319+
.. CompileState::empty(input, session, out_dir, output)
300320
}
301321
}
302322

303323
fn state_after_analysis(input: &'a Input,
304324
session: &'a Session,
305325
out_dir: &'a Option<Path>,
326+
output: &'a Option<Path>,
306327
expanded_crate: &'a ast::Crate,
307328
analysis: &'a ty::CrateAnalysis<'tcx>,
308329
tcx: &'a ty::ctxt<'tcx>)
309-
-> CompileState<'a, 'ast, 'tcx> {
330+
-> CompileState<'a, 'tcx> {
310331
CompileState {
311332
analysis: Some(analysis),
312333
tcx: Some(tcx),
313334
expanded_crate: Some(expanded_crate),
314-
.. CompileState::empty(input, session, out_dir)
335+
.. CompileState::empty(input, session, out_dir, output)
315336
}
316337
}
317338

318339

319340
fn state_after_llvm(input: &'a Input,
320341
session: &'a Session,
321342
out_dir: &'a Option<Path>,
343+
output: &'a Option<Path>,
322344
trans: &'a trans::CrateTranslation)
323-
-> CompileState<'a, 'ast, 'tcx> {
345+
-> CompileState<'a, 'tcx> {
324346
CompileState {
325347
trans: Some(trans),
326-
.. CompileState::empty(input, session, out_dir)
348+
.. CompileState::empty(input, session, out_dir, output)
327349
}
328350
}
329351
}

src/librustc_driver/lib.rs

Lines changed: 35 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ const BUG_REPORT_URL: &'static str =
9898

9999

100100
pub fn run(args: Vec<String>) -> int {
101-
monitor(move || run_compiler(&args, &mut RustcDefaultCalls));
101+
monitor(move || run_compiler(&args, &mut RustcDefaultCalls::new()));
102102
0
103103
}
104104

@@ -142,13 +142,6 @@ pub fn run_compiler<'a>(args: &[String],
142142

143143
do_or_return!(callbacks.late_callback(&matches, &sess, &input, &odir, &ofile));
144144

145-
// It is somewhat unfortunate that this is hardwired in - this is forced by
146-
// the fact that pretty_print_input requires the session by value.
147-
if let Some((ppm, opt_uii)) = callbacks.parse_pretty(&sess, &matches) {
148-
pretty::pretty_print_input(sess, cfg, &input, ppm, opt_uii, ofile).unwrap();
149-
return;
150-
}
151-
152145
let plugins = sess.opts.debugging_opts.extra_plugins.clone();
153146
let control = callbacks.build_controller(&sess);
154147
driver::compile_input(sess, cfg, &input, &odir, &ofile, Some(plugins), control);
@@ -235,33 +228,15 @@ pub trait CompilerCalls<'a> {
235228
&diagnostics::registry::Registry)
236229
-> Option<(Input, Option<Path>)>;
237230

238-
// Parse pretty printing information from the arguments. The implementer can
239-
// choose to ignore this (the default will return None) which will skip pretty
240-
// printing. If you do want to pretty print, it is recommended to use the
241-
// implementation of this method from RustcDefaultCalls.
242-
// FIXME, this is a terrible bit of API. Parsing of pretty printing stuff
243-
// should be done as part of the framework and the implementor should customise
244-
// handling of it. However, that is not possible atm because pretty printing
245-
// essentially goes off and takes another path through the compiler which
246-
// means the session is either moved or not depending on what parse_pretty
247-
// returns (we could fix this by cloning, but it's another hack). The proper
248-
// solution is to handle pretty printing as if it were a compiler extension,
249-
// extending CompileController to make this work (see for example the treatment
250-
// of save-analysis in RustcDefaultCalls::build_controller).
251-
fn parse_pretty(&mut self,
252-
_sess: &Session,
253-
_matches: &getopts::Matches)
254-
-> Option<(PpMode, Option<UserIdentifiedItem>)> {
255-
None
256-
}
257-
258231
// Create a CompilController struct for controlling the behaviour of compilation.
259232
fn build_controller(&mut self, &Session) -> CompileController<'a>;
260233
}
261234

262235
// CompilerCalls instance for a regular rustc build.
263-
#[derive(Copy)]
264-
pub struct RustcDefaultCalls;
236+
pub struct RustcDefaultCalls {
237+
save_analysis: bool,
238+
pretty_print: Option<(PpMode, Option<UserIdentifiedItem>)>
239+
}
265240

266241
impl<'a> CompilerCalls<'a> for RustcDefaultCalls {
267242
fn early_callback(&mut self,
@@ -316,35 +291,25 @@ impl<'a> CompilerCalls<'a> for RustcDefaultCalls {
316291
None
317292
}
318293

319-
fn parse_pretty(&mut self,
320-
sess: &Session,
321-
matches: &getopts::Matches)
322-
-> Option<(PpMode, Option<UserIdentifiedItem>)> {
323-
let pretty = if sess.opts.debugging_opts.unstable_options {
324-
matches.opt_default("pretty", "normal").map(|a| {
325-
// stable pretty-print variants only
326-
pretty::parse_pretty(sess, &a, false)
327-
})
328-
} else {
329-
None
330-
};
331-
if pretty.is_none() && sess.unstable_options() {
332-
matches.opt_str("xpretty").map(|a| {
333-
// extended with unstable pretty-print variants
334-
pretty::parse_pretty(sess, &a, true)
335-
})
336-
} else {
337-
pretty
338-
}
339-
}
340-
341294
fn late_callback(&mut self,
342295
matches: &getopts::Matches,
343296
sess: &Session,
344297
input: &Input,
345298
odir: &Option<Path>,
346299
ofile: &Option<Path>)
347300
-> Compilation {
301+
self.save_analysis = sess.opts.debugging_opts.save_analysis;
302+
303+
if sess.unstable_options() {
304+
self.pretty_print = matches.opt_default("pretty", "normal").map(|a| {
305+
// stable pretty-print variants only
306+
pretty::parse_pretty(sess, &a, false)
307+
}).or_else(|| matches.opt_str("xpretty").map(|a| {
308+
// extended with unstable pretty-print variants
309+
pretty::parse_pretty(sess, &a, true)
310+
}));
311+
}
312+
348313
RustcDefaultCalls::print_crate_info(sess, Some(input), odir, ofile).and_then(
349314
|| RustcDefaultCalls::list_metadata(sess, matches, input))
350315
}
@@ -370,7 +335,17 @@ impl<'a> CompilerCalls<'a> for RustcDefaultCalls {
370335
control.after_llvm.stop = Compilation::Stop;
371336
}
372337

373-
if sess.opts.debugging_opts.save_analysis {
338+
if let Some((ppm, opt_uii)) = self.pretty_print.take() {
339+
let phase = pretty::printing_phase(&mut control, ppm, opt_uii.as_ref());
340+
341+
phase.callback = box move |state| {
342+
let output = state.output;
343+
pretty::print_from_phase(state, ppm, opt_uii.as_ref(), output).unwrap();
344+
};
345+
phase.stop = Compilation::Stop;
346+
}
347+
348+
if self.save_analysis {
374349
control.after_analysis.callback = box |state| {
375350
time(state.session.time_passes(),
376351
"save analysis", ||
@@ -387,6 +362,13 @@ impl<'a> CompilerCalls<'a> for RustcDefaultCalls {
387362
}
388363

389364
impl RustcDefaultCalls {
365+
pub fn new() -> RustcDefaultCalls {
366+
RustcDefaultCalls {
367+
save_analysis: false,
368+
pretty_print: None
369+
}
370+
}
371+
390372
pub fn list_metadata(sess: &Session,
391373
matches: &getopts::Matches,
392374
input: &Input)

0 commit comments

Comments
 (0)