]> git.lizzy.rs Git - rust.git/blob - src/test/run-pass-fulldeps/compiler-calls.rs
Auto merge of #54919 - alexcrichton:update-cargo, r=Mark-Simulacrum
[rust.git] / src / test / run-pass-fulldeps / compiler-calls.rs
1 // Copyright 2015 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 // Test that the CompilerCalls interface to the compiler works.
12
13 // ignore-cross-compile
14
15 #![feature(rustc_private)]
16
17 extern crate getopts;
18 extern crate rustc;
19 extern crate rustc_driver;
20 extern crate rustc_codegen_utils;
21 extern crate syntax;
22 extern crate rustc_errors as errors;
23 extern crate rustc_metadata;
24
25 use rustc::session::Session;
26 use rustc::session::config::{self, Input};
27 use rustc_driver::{driver, CompilerCalls, Compilation};
28 use rustc_codegen_utils::codegen_backend::CodegenBackend;
29 use rustc_metadata::cstore::CStore;
30 use syntax::ast;
31
32 use std::path::PathBuf;
33
34 struct TestCalls<'a> {
35     count: &'a mut u32
36 }
37
38 impl<'a> CompilerCalls<'a> for TestCalls<'a> {
39     fn early_callback(&mut self,
40                       _: &getopts::Matches,
41                       _: &config::Options,
42                       _: &ast::CrateConfig,
43                       _: &errors::registry::Registry,
44                       _: config::ErrorOutputType)
45                       -> Compilation {
46         *self.count *= 2;
47         Compilation::Continue
48     }
49
50     fn late_callback(&mut self,
51                      _: &CodegenBackend,
52                      _: &getopts::Matches,
53                      _: &Session,
54                      _: &CStore,
55                      _: &Input,
56                      _: &Option<PathBuf>,
57                      _: &Option<PathBuf>)
58                      -> Compilation {
59         *self.count *= 3;
60         Compilation::Stop
61     }
62
63     fn some_input(&mut self, input: Input, input_path: Option<PathBuf>)
64                   -> (Input, Option<PathBuf>) {
65         *self.count *= 5;
66         (input, input_path)
67     }
68
69     fn no_input(&mut self,
70                 _: &getopts::Matches,
71                 _: &config::Options,
72                 _: &ast::CrateConfig,
73                 _: &Option<PathBuf>,
74                 _: &Option<PathBuf>,
75                 _: &errors::registry::Registry)
76                 -> Option<(Input, Option<PathBuf>)> {
77         panic!("This shouldn't happen");
78     }
79
80     fn build_controller(self: Box<Self>,
81                         _: &Session,
82                         _: &getopts::Matches)
83                         -> driver::CompileController<'a> {
84         panic!("This shouldn't be called");
85     }
86 }
87
88
89 fn main() {
90     let mut count = 1;
91     {
92         let tc = TestCalls { count: &mut count };
93         // we should never get use this filename, but lets make sure they are valid args.
94         let args = vec!["compiler-calls".to_string(), "foo.rs".to_string()];
95         syntax::with_globals(|| {
96             rustc_driver::run_compiler(&args, Box::new(tc), None, None);
97         });
98     }
99     assert_eq!(count, 30);
100 }