Skip to content

Commit

Permalink
Review changes
Browse files Browse the repository at this point in the history
  • Loading branch information
nrc committed Feb 9, 2015
1 parent cacd6b6 commit f0e1e09
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 39 deletions.
2 changes: 1 addition & 1 deletion src/librustc/session/config.rs
Expand Up @@ -36,8 +36,8 @@ use syntax::parse::token::InternedString;
use getopts;
use std::collections::HashMap;
use std::collections::hash_map::Entry::{Occupied, Vacant};
use std::env;
use std::fmt;
use std::os;

use llvm;

Expand Down
9 changes: 5 additions & 4 deletions src/librustc_driver/driver.rs
@@ -1,4 +1,4 @@
// Copyright 2012-2013 The Rust Project Developers. See the COPYRIGHT
// Copyright 2012-2015 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
Expand Down Expand Up @@ -27,6 +27,7 @@ use rustc_trans::back::write;
use rustc_trans::trans;
use rustc_typeck as typeck;
use rustc_privacy;
use super::Compilation;

use serialize::json;

Expand Down Expand Up @@ -55,7 +56,7 @@ pub fn compile_input(sess: Session,
let state = $make_state;
(control.$point.callback)(state);
}
if control.$point.stop {
if control.$point.stop == Compilation::Stop {
return;
}
})}
Expand Down Expand Up @@ -206,14 +207,14 @@ impl<'a> CompileController<'a> {
}

pub struct PhaseController<'a> {
pub stop: bool,
pub stop: Compilation,
pub callback: Box<Fn(CompileState) -> () + 'a>,
}

impl<'a> PhaseController<'a> {
pub fn basic() -> PhaseController<'a> {
PhaseController {
stop: false,
stop: Compilation::Continue,
callback: box |_| {},
}
}
Expand Down
73 changes: 46 additions & 27 deletions src/librustc_driver/lib.rs
@@ -1,4 +1,4 @@
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
// Copyright 2014-2015 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
Expand Down Expand Up @@ -108,8 +108,9 @@ pub fn run(args: Vec<String>) -> int {
pub fn run_compiler<'a>(args: &[String],
callbacks: &mut CompilerCalls<'a>) {
macro_rules! do_or_return {($expr: expr) => {
if $expr {
return;
match $expr {
Compilation::Stop => return,
Compilation::Continue => {}
}
}}

Expand Down Expand Up @@ -144,7 +145,7 @@ pub fn run_compiler<'a>(args: &[String],
// It is somewhat unfortunate that this is hardwired in - this is forced by
// the fact that pretty_print_input requires the session by value.
let pretty = callbacks.parse_pretty(&sess, &matches);
match pretty.into_iter().next() {
match pretty {
Some((ppm, opt_uii)) => {
pretty::pretty_print_input(sess, cfg, &input, ppm, opt_uii, ofile);
return;
Expand Down Expand Up @@ -180,26 +181,43 @@ fn make_input(free_matches: &[String]) -> Option<(Input, Option<Path>)> {
}
}

// Whether to stop or continue compilation.
#[derive(Copy, Debug, Eq, PartialEq)]
pub enum Compilation {
Stop,
Continue,
}

impl Compilation {
pub fn and_then<F: FnOnce() -> Compilation>(self, next: F) -> Compilation {
match self {
Compilation::Stop => Compilation::Stop,
Compilation::Continue => next()
}
}
}

// A trait for customising the compilation process. Offers a number of hooks for
// executing custom code or customising input.
pub trait CompilerCalls<'a> {
// Hook for a callback early in the process of handling arguments. This will
// be called straight after options have been parsed but before anything
// else (e.g., selecting input and output). Return true to terminate compilation,
// false to continue.
fn early_callback(&mut self, &getopts::Matches, &diagnostics::registry::Registry) -> bool;
// else (e.g., selecting input and output).
fn early_callback(&mut self,
&getopts::Matches,
&diagnostics::registry::Registry)
-> Compilation;

// Hook for a callback late in the process of handling arguments. This will
// be called just before actual compilation starts (and before build_controller
// is called), after all arguments etc. have been completely handled. Return
// true to terminate compilation, false to continue.
// is called), after all arguments etc. have been completely handled.
fn late_callback(&mut self,
&getopts::Matches,
&Session,
&Input,
&Option<Path>,
&Option<Path>)
-> bool;
-> Compilation;

// Called after we extract the input from the arguments. Gives the implementer
// an opportunity to change the inputs or to add some custom input handling.
Expand Down Expand Up @@ -253,7 +271,7 @@ impl<'a> CompilerCalls<'a> for RustcDefaultCalls {
fn early_callback(&mut self,
matches: &getopts::Matches,
descriptions: &diagnostics::registry::Registry)
-> bool {
-> Compilation {
match matches.opt_str("explain") {
Some(ref code) => {
match descriptions.find_description(&code[]) {
Expand All @@ -264,12 +282,12 @@ impl<'a> CompilerCalls<'a> for RustcDefaultCalls {
early_error(&format!("no extended information for {}", code)[]);
}
}
return true;
return Compilation::Stop;
},
None => ()
}

return false;
return Compilation::Continue;
}

fn no_input(&mut self,
Expand All @@ -288,7 +306,8 @@ impl<'a> CompilerCalls<'a> for RustcDefaultCalls {
return None;
}
let sess = build_session(sopts.clone(), None, descriptions.clone());
if RustcDefaultCalls::print_crate_info(&sess, None, odir, ofile) {
let should_stop = RustcDefaultCalls::print_crate_info(&sess, None, odir, ofile);
if should_stop == Compilation::Stop {
return None;
}
early_error("no input filename given");
Expand Down Expand Up @@ -328,9 +347,9 @@ impl<'a> CompilerCalls<'a> for RustcDefaultCalls {
input: &Input,
odir: &Option<Path>,
ofile: &Option<Path>)
-> bool {
RustcDefaultCalls::print_crate_info(sess, Some(input), odir, ofile) ||
RustcDefaultCalls::list_metadata(sess, matches, input)
-> Compilation {
RustcDefaultCalls::print_crate_info(sess, Some(input), odir, ofile).and_then(
|| RustcDefaultCalls::list_metadata(sess, matches, input))
}

fn build_controller(&mut self, sess: &Session) -> CompileController<'a> {
Expand All @@ -339,19 +358,19 @@ impl<'a> CompilerCalls<'a> for RustcDefaultCalls {
if sess.opts.parse_only ||
sess.opts.show_span.is_some() ||
sess.opts.debugging_opts.ast_json_noexpand {
control.after_parse.stop = true;
control.after_parse.stop = Compilation::Stop;
}

if sess.opts.no_analysis || sess.opts.debugging_opts.ast_json {
control.after_write_deps.stop = true;
control.after_write_deps.stop = Compilation::Stop;
}

if sess.opts.no_trans {
control.after_analysis.stop = true;
control.after_analysis.stop = Compilation::Stop;
}

if !sess.opts.output_types.iter().any(|&i| i == config::OutputTypeExe) {
control.after_llvm.stop = true;
control.after_llvm.stop = Compilation::Stop;
}

if sess.opts.debugging_opts.save_analysis {
Expand All @@ -373,7 +392,7 @@ impl RustcDefaultCalls {
pub fn list_metadata(sess: &Session,
matches: &getopts::Matches,
input: &Input)
-> bool {
-> Compilation {
let r = matches.opt_strs("Z");
if r.contains(&("ls".to_string())) {
match input {
Expand All @@ -388,20 +407,20 @@ impl RustcDefaultCalls {
early_error("cannot list metadata for stdin");
}
}
return true;
return Compilation::Stop;
}

return false;
return Compilation::Continue;
}


fn print_crate_info(sess: &Session,
input: Option<&Input>,
odir: &Option<Path>,
ofile: &Option<Path>)
-> bool {
-> Compilation {
if sess.opts.prints.len() == 0 {
return false
return Compilation::Continue;
}

let attrs = input.map(|input| parse_crate_attrs(sess, input));
Expand Down Expand Up @@ -440,7 +459,7 @@ impl RustcDefaultCalls {
}
}
}
return true;
return Compilation::Stop;
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/librustdoc/test.rs
Expand Up @@ -23,7 +23,7 @@ use testing;
use rustc::session::{self, config};
use rustc::session::config::get_unstable_features_setting;
use rustc::session::search_paths::{SearchPaths, PathKind};
use rustc_driver::driver;
use rustc_driver::{driver, Compilation};
use syntax::ast;
use syntax::codemap::{CodeMap, dummy_spanned};
use syntax::diagnostic;
Expand Down Expand Up @@ -178,7 +178,7 @@ fn runtest(test: &str, cratename: &str, libs: SearchPaths,
let libdir = sess.target_filesearch(PathKind::All).get_lib_path();
let mut control = driver::CompileController::basic();
if no_run {
control.after_analysis.stop = true;
control.after_analysis.stop = Compilation::Stop;
}
driver::compile_input(sess, cfg, &input, &out, &None, None, control);

Expand Down
12 changes: 7 additions & 5 deletions src/test/run-pass-fulldeps/compiler-calls.rs
Expand Up @@ -10,6 +10,8 @@

// Test that the CompilerCalls interface to the compiler works.

// ignore-android

#![feature(rustc_private)]
#![feature(core)]

Expand All @@ -20,7 +22,7 @@ extern crate syntax;

use rustc::session::Session;
use rustc::session::config::{self, Input};
use rustc_driver::{driver, CompilerCalls};
use rustc_driver::{driver, CompilerCalls, Compilation};
use syntax::diagnostics;


Expand All @@ -32,9 +34,9 @@ impl<'a> CompilerCalls<'a> for TestCalls {
fn early_callback(&mut self,
_: &getopts::Matches,
_: &diagnostics::registry::Registry)
-> bool {
-> Compilation {
self.count *= 2;
false
Compilation::Continue
}

fn late_callback(&mut self,
Expand All @@ -43,9 +45,9 @@ impl<'a> CompilerCalls<'a> for TestCalls {
_: &Input,
_: &Option<Path>,
_: &Option<Path>)
-> bool {
-> Compilation {
self.count *= 3;
true
Compilation::Stop
}

fn some_input(&mut self, input: Input, input_path: Option<Path>) -> (Input, Option<Path>) {
Expand Down

0 comments on commit f0e1e09

Please sign in to comment.