]> git.lizzy.rs Git - rust.git/blob - src/tools/compiletest/src/runtest.rs
140c90aaeacc13dca77768aa7e126665c9678fbf
[rust.git] / src / tools / compiletest / src / runtest.rs
1 // Copyright 2012-2014 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 use common::CompareMode;
12 use common::{expected_output_path, UI_STDERR, UI_STDOUT, UI_FIXED};
13 use common::{output_base_dir, output_base_name, output_testname_unique};
14 use common::{Codegen, CodegenUnits, DebugInfoGdb, DebugInfoLldb, Rustdoc};
15 use common::{CompileFail, ParseFail, Pretty, RunFail, RunPass, RunPassValgrind};
16 use common::{Config, TestPaths};
17 use common::{Incremental, MirOpt, RunMake, Ui};
18 use diff;
19 use errors::{self, Error, ErrorKind};
20 use filetime::FileTime;
21 use header::TestProps;
22 use json;
23 use regex::Regex;
24 use rustfix::{apply_suggestions, get_suggestions_from_json};
25 use util::{logv, PathBufExt};
26
27 use std::collections::{HashMap, HashSet, VecDeque};
28 use std::collections::hash_map::DefaultHasher;
29 use std::env;
30 use std::ffi::OsString;
31 use std::fmt;
32 use std::fs::{self, create_dir_all, File};
33 use std::hash::{Hash, Hasher};
34 use std::io::prelude::*;
35 use std::io::{self, BufReader};
36 use std::path::{Path, PathBuf};
37 use std::process::{Child, Command, ExitStatus, Output, Stdio};
38 use std::str;
39
40 use extract_gdb_version;
41
42 #[cfg(windows)]
43 fn disable_error_reporting<F: FnOnce() -> R, R>(f: F) -> R {
44     use std::sync::Mutex;
45     const SEM_NOGPFAULTERRORBOX: u32 = 0x0002;
46     extern "system" {
47         fn SetErrorMode(mode: u32) -> u32;
48     }
49
50     lazy_static! {
51         static ref LOCK: Mutex<()> = {
52             Mutex::new(())
53         };
54     }
55     // Error mode is a global variable, so lock it so only one thread will change it
56     let _lock = LOCK.lock().unwrap();
57
58     // Tell Windows to not show any UI on errors (such as terminating abnormally).
59     // This is important for running tests, since some of them use abnormal
60     // termination by design. This mode is inherited by all child processes.
61     unsafe {
62         let old_mode = SetErrorMode(SEM_NOGPFAULTERRORBOX); // read inherited flags
63         SetErrorMode(old_mode | SEM_NOGPFAULTERRORBOX);
64         let r = f();
65         SetErrorMode(old_mode);
66         r
67     }
68 }
69
70 #[cfg(not(windows))]
71 fn disable_error_reporting<F: FnOnce() -> R, R>(f: F) -> R {
72     f()
73 }
74
75 /// The name of the environment variable that holds dynamic library locations.
76 pub fn dylib_env_var() -> &'static str {
77     if cfg!(windows) {
78         "PATH"
79     } else if cfg!(target_os = "macos") {
80         "DYLD_LIBRARY_PATH"
81     } else if cfg!(target_os = "haiku") {
82         "LIBRARY_PATH"
83     } else {
84         "LD_LIBRARY_PATH"
85     }
86 }
87
88 #[derive(Debug, PartialEq)]
89 pub enum DiffLine {
90     Context(String),
91     Expected(String),
92     Resulting(String),
93 }
94
95 #[derive(Debug, PartialEq)]
96 pub struct Mismatch {
97     pub line_number: u32,
98     pub lines: Vec<DiffLine>,
99 }
100
101 impl Mismatch {
102     fn new(line_number: u32) -> Mismatch {
103         Mismatch {
104             line_number: line_number,
105             lines: Vec::new(),
106         }
107     }
108 }
109
110 // Produces a diff between the expected output and actual output.
111 pub fn make_diff(expected: &str, actual: &str, context_size: usize) -> Vec<Mismatch> {
112     let mut line_number = 1;
113     let mut context_queue: VecDeque<&str> = VecDeque::with_capacity(context_size);
114     let mut lines_since_mismatch = context_size + 1;
115     let mut results = Vec::new();
116     let mut mismatch = Mismatch::new(0);
117
118     for result in diff::lines(expected, actual) {
119         match result {
120             diff::Result::Left(str) => {
121                 if lines_since_mismatch >= context_size && lines_since_mismatch > 0 {
122                     results.push(mismatch);
123                     mismatch = Mismatch::new(line_number - context_queue.len() as u32);
124                 }
125
126                 while let Some(line) = context_queue.pop_front() {
127                     mismatch.lines.push(DiffLine::Context(line.to_owned()));
128                 }
129
130                 mismatch.lines.push(DiffLine::Expected(str.to_owned()));
131                 line_number += 1;
132                 lines_since_mismatch = 0;
133             }
134             diff::Result::Right(str) => {
135                 if lines_since_mismatch >= context_size && lines_since_mismatch > 0 {
136                     results.push(mismatch);
137                     mismatch = Mismatch::new(line_number - context_queue.len() as u32);
138                 }
139
140                 while let Some(line) = context_queue.pop_front() {
141                     mismatch.lines.push(DiffLine::Context(line.to_owned()));
142                 }
143
144                 mismatch.lines.push(DiffLine::Resulting(str.to_owned()));
145                 lines_since_mismatch = 0;
146             }
147             diff::Result::Both(str, _) => {
148                 if context_queue.len() >= context_size {
149                     let _ = context_queue.pop_front();
150                 }
151
152                 if lines_since_mismatch < context_size {
153                     mismatch.lines.push(DiffLine::Context(str.to_owned()));
154                 } else if context_size > 0 {
155                     context_queue.push_back(str);
156                 }
157
158                 line_number += 1;
159                 lines_since_mismatch += 1;
160             }
161         }
162     }
163
164     results.push(mismatch);
165     results.remove(0);
166
167     results
168 }
169
170 pub fn run(config: Config, testpaths: &TestPaths, revision: Option<&str>) {
171     match &*config.target {
172         "arm-linux-androideabi" | "armv7-linux-androideabi" | "aarch64-linux-android" => {
173             if !config.adb_device_status {
174                 panic!("android device not available");
175             }
176         }
177
178         _ => {
179             // android has its own gdb handling
180             if config.mode == DebugInfoGdb && config.gdb.is_none() {
181                 panic!("gdb not available but debuginfo gdb debuginfo test requested");
182             }
183         }
184     }
185
186     if config.verbose {
187         // We're going to be dumping a lot of info. Start on a new line.
188         print!("\n\n");
189     }
190     debug!("running {:?}", testpaths.file.display());
191     let props = TestProps::from_file(&testpaths.file, revision, &config);
192
193     let cx = TestCx {
194         config: &config,
195         props: &props,
196         testpaths,
197         revision: revision,
198     };
199     create_dir_all(&cx.output_base_dir()).unwrap();
200
201     if config.mode == Incremental {
202         // Incremental tests are special because they cannot be run in
203         // parallel.
204         assert!(
205             !props.revisions.is_empty(),
206             "Incremental tests require revisions."
207         );
208         cx.init_incremental_test();
209         for revision in &props.revisions {
210             let revision_props = TestProps::from_file(&testpaths.file, Some(revision), &config);
211             let rev_cx = TestCx {
212                 config: &config,
213                 props: &revision_props,
214                 testpaths,
215                 revision: Some(revision),
216             };
217             rev_cx.run_revision();
218         }
219     } else {
220         cx.run_revision();
221     }
222
223     cx.create_stamp();
224 }
225
226 pub fn compute_stamp_hash(config: &Config) -> String {
227     let mut hash = DefaultHasher::new();
228     config.stage_id.hash(&mut hash);
229     format!("{:x}", hash.finish())
230 }
231
232 struct TestCx<'test> {
233     config: &'test Config,
234     props: &'test TestProps,
235     testpaths: &'test TestPaths,
236     revision: Option<&'test str>,
237 }
238
239 struct DebuggerCommands {
240     commands: Vec<String>,
241     check_lines: Vec<String>,
242     breakpoint_lines: Vec<usize>,
243 }
244
245 impl<'test> TestCx<'test> {
246     /// Code executed for each revision in turn (or, if there are no
247     /// revisions, exactly once, with revision == None).
248     fn run_revision(&self) {
249         match self.config.mode {
250             CompileFail | ParseFail => self.run_cfail_test(),
251             RunFail => self.run_rfail_test(),
252             RunPass => self.run_rpass_test(),
253             RunPassValgrind => self.run_valgrind_test(),
254             Pretty => self.run_pretty_test(),
255             DebugInfoGdb => self.run_debuginfo_gdb_test(),
256             DebugInfoLldb => self.run_debuginfo_lldb_test(),
257             Codegen => self.run_codegen_test(),
258             Rustdoc => self.run_rustdoc_test(),
259             CodegenUnits => self.run_codegen_units_test(),
260             Incremental => self.run_incremental_test(),
261             RunMake => self.run_rmake_test(),
262             Ui => self.run_ui_test(),
263             MirOpt => self.run_mir_opt_test(),
264         }
265     }
266
267     fn check_if_test_should_compile(&self, proc_res: &ProcRes) {
268         if self.props.compile_pass {
269             if !proc_res.status.success() {
270                 self.fatal_proc_rec("test compilation failed although it shouldn't!", proc_res);
271             }
272         } else {
273             if proc_res.status.success() {
274                 self.fatal_proc_rec(
275                     &format!("{} test compiled successfully!", self.config.mode)[..],
276                     proc_res,
277                 );
278             }
279
280             self.check_correct_failure_status(proc_res);
281         }
282     }
283
284     fn run_cfail_test(&self) {
285         let proc_res = self.compile_test();
286         self.check_if_test_should_compile(&proc_res);
287         self.check_no_compiler_crash(&proc_res);
288
289         let output_to_check = self.get_output(&proc_res);
290         let expected_errors = errors::load_errors(&self.testpaths.file, self.revision);
291         if !expected_errors.is_empty() {
292             if !self.props.error_patterns.is_empty() {
293                 self.fatal("both error pattern and expected errors specified");
294             }
295             self.check_expected_errors(expected_errors, &proc_res);
296         } else {
297             self.check_error_patterns(&output_to_check, &proc_res);
298         }
299
300         self.check_forbid_output(&output_to_check, &proc_res);
301     }
302
303     fn run_rfail_test(&self) {
304         let proc_res = self.compile_test();
305
306         if !proc_res.status.success() {
307             self.fatal_proc_rec("compilation failed!", &proc_res);
308         }
309
310         let proc_res = self.exec_compiled_test();
311
312         // The value our Makefile configures valgrind to return on failure
313         const VALGRIND_ERR: i32 = 100;
314         if proc_res.status.code() == Some(VALGRIND_ERR) {
315             self.fatal_proc_rec("run-fail test isn't valgrind-clean!", &proc_res);
316         }
317
318         let output_to_check = self.get_output(&proc_res);
319         self.check_correct_failure_status(&proc_res);
320         self.check_error_patterns(&output_to_check, &proc_res);
321     }
322
323     fn get_output(&self, proc_res: &ProcRes) -> String {
324         if self.props.check_stdout {
325             format!("{}{}", proc_res.stdout, proc_res.stderr)
326         } else {
327             proc_res.stderr.clone()
328         }
329     }
330
331     fn check_correct_failure_status(&self, proc_res: &ProcRes) {
332         let expected_status = Some(self.props.failure_status);
333         let received_status = proc_res.status.code();
334
335         if expected_status != received_status {
336             self.fatal_proc_rec(
337                 &format!(
338                     "Error: expected failure status ({:?}) but received status {:?}.",
339                     expected_status, received_status
340                 ),
341                 proc_res,
342             );
343         }
344     }
345
346     fn run_rpass_test(&self) {
347         let proc_res = self.compile_test();
348
349         if !proc_res.status.success() {
350             self.fatal_proc_rec("compilation failed!", &proc_res);
351         }
352
353         // FIXME(#41968): Move this check to tidy?
354         let expected_errors = errors::load_errors(&self.testpaths.file, self.revision);
355         assert!(
356             expected_errors.is_empty(),
357             "run-pass tests with expected warnings should be moved to ui/"
358         );
359
360         if !self.props.skip_codegen {
361             let proc_res = self.exec_compiled_test();
362             if !proc_res.status.success() {
363                 self.fatal_proc_rec("test run failed!", &proc_res);
364             }
365         }
366     }
367
368     fn run_valgrind_test(&self) {
369         assert!(self.revision.is_none(), "revisions not relevant here");
370
371         if self.config.valgrind_path.is_none() {
372             assert!(!self.config.force_valgrind);
373             return self.run_rpass_test();
374         }
375
376         let mut proc_res = self.compile_test();
377
378         if !proc_res.status.success() {
379             self.fatal_proc_rec("compilation failed!", &proc_res);
380         }
381
382         let mut new_config = self.config.clone();
383         new_config.runtool = new_config.valgrind_path.clone();
384         let new_cx = TestCx {
385             config: &new_config,
386             ..*self
387         };
388         proc_res = new_cx.exec_compiled_test();
389
390         if !proc_res.status.success() {
391             self.fatal_proc_rec("test run failed!", &proc_res);
392         }
393     }
394
395     fn run_pretty_test(&self) {
396         if self.props.pp_exact.is_some() {
397             logv(self.config, "testing for exact pretty-printing".to_owned());
398         } else {
399             logv(
400                 self.config,
401                 "testing for converging pretty-printing".to_owned(),
402             );
403         }
404
405         let rounds = match self.props.pp_exact {
406             Some(_) => 1,
407             None => 2,
408         };
409
410         let mut src = String::new();
411         File::open(&self.testpaths.file)
412             .unwrap()
413             .read_to_string(&mut src)
414             .unwrap();
415         let mut srcs = vec![src];
416
417         let mut round = 0;
418         while round < rounds {
419             logv(
420                 self.config,
421                 format!(
422                     "pretty-printing round {} revision {:?}",
423                     round, self.revision
424                 ),
425             );
426             let proc_res = self.print_source(srcs[round].to_owned(), &self.props.pretty_mode);
427
428             if !proc_res.status.success() {
429                 self.fatal_proc_rec(
430                     &format!(
431                         "pretty-printing failed in round {} revision {:?}",
432                         round, self.revision
433                     ),
434                     &proc_res,
435                 );
436             }
437
438             let ProcRes { stdout, .. } = proc_res;
439             srcs.push(stdout);
440             round += 1;
441         }
442
443         let mut expected = match self.props.pp_exact {
444             Some(ref file) => {
445                 let filepath = self.testpaths.file.parent().unwrap().join(file);
446                 let mut s = String::new();
447                 File::open(&filepath)
448                     .unwrap()
449                     .read_to_string(&mut s)
450                     .unwrap();
451                 s
452             }
453             None => srcs[srcs.len() - 2].clone(),
454         };
455         let mut actual = srcs[srcs.len() - 1].clone();
456
457         if self.props.pp_exact.is_some() {
458             // Now we have to care about line endings
459             let cr = "\r".to_owned();
460             actual = actual.replace(&cr, "").to_owned();
461             expected = expected.replace(&cr, "").to_owned();
462         }
463
464         self.compare_source(&expected, &actual);
465
466         // If we're only making sure that the output matches then just stop here
467         if self.props.pretty_compare_only {
468             return;
469         }
470
471         // Finally, let's make sure it actually appears to remain valid code
472         let proc_res = self.typecheck_source(actual);
473         if !proc_res.status.success() {
474             self.fatal_proc_rec("pretty-printed source does not typecheck", &proc_res);
475         }
476
477         if !self.props.pretty_expanded {
478             return;
479         }
480
481         // additionally, run `--pretty expanded` and try to build it.
482         let proc_res = self.print_source(srcs[round].clone(), "expanded");
483         if !proc_res.status.success() {
484             self.fatal_proc_rec("pretty-printing (expanded) failed", &proc_res);
485         }
486
487         let ProcRes {
488             stdout: expanded_src,
489             ..
490         } = proc_res;
491         let proc_res = self.typecheck_source(expanded_src);
492         if !proc_res.status.success() {
493             self.fatal_proc_rec(
494                 "pretty-printed source (expanded) does not typecheck",
495                 &proc_res,
496             );
497         }
498     }
499
500     fn print_source(&self, src: String, pretty_type: &str) -> ProcRes {
501         let aux_dir = self.aux_output_dir_name();
502
503         let mut rustc = Command::new(&self.config.rustc_path);
504         rustc
505             .arg("-")
506             .args(&["-Z", &format!("unpretty={}", pretty_type)])
507             .args(&["--target", &self.config.target])
508             .arg("-L")
509             .arg(&aux_dir)
510             .args(self.split_maybe_args(&self.config.target_rustcflags))
511             .args(&self.props.compile_flags)
512             .envs(self.props.exec_env.clone());
513
514         self.compose_and_run(
515             rustc,
516             self.config.compile_lib_path.to_str().unwrap(),
517             Some(aux_dir.to_str().unwrap()),
518             Some(src),
519         )
520     }
521
522     fn compare_source(&self, expected: &str, actual: &str) {
523         if expected != actual {
524             self.error("pretty-printed source does not match expected source");
525             println!(
526                 "\n\
527                  expected:\n\
528                  ------------------------------------------\n\
529                  {}\n\
530                  ------------------------------------------\n\
531                  actual:\n\
532                  ------------------------------------------\n\
533                  {}\n\
534                  ------------------------------------------\n\
535                  \n",
536                 expected, actual
537             );
538         }
539     }
540
541     fn typecheck_source(&self, src: String) -> ProcRes {
542         let mut rustc = Command::new(&self.config.rustc_path);
543
544         let out_dir = self.output_base_name().with_extension("pretty-out");
545         let _ = fs::remove_dir_all(&out_dir);
546         create_dir_all(&out_dir).unwrap();
547
548         let target = if self.props.force_host {
549             &*self.config.host
550         } else {
551             &*self.config.target
552         };
553
554         let aux_dir = self.aux_output_dir_name();
555
556         rustc
557             .arg("-")
558             .arg("-Zno-codegen")
559             .arg("--out-dir")
560             .arg(&out_dir)
561             .arg(&format!("--target={}", target))
562             .arg("-L")
563             .arg(&self.config.build_base)
564             .arg("-L")
565             .arg(aux_dir);
566
567         if let Some(revision) = self.revision {
568             rustc.args(&["--cfg", revision]);
569         }
570
571         rustc.args(self.split_maybe_args(&self.config.target_rustcflags));
572         rustc.args(&self.props.compile_flags);
573
574         self.compose_and_run_compiler(rustc, Some(src))
575     }
576
577     fn run_debuginfo_gdb_test(&self) {
578         assert!(self.revision.is_none(), "revisions not relevant here");
579
580         let config = Config {
581             target_rustcflags: self.cleanup_debug_info_options(&self.config.target_rustcflags),
582             host_rustcflags: self.cleanup_debug_info_options(&self.config.host_rustcflags),
583             ..self.config.clone()
584         };
585
586         let test_cx = TestCx {
587             config: &config,
588             ..*self
589         };
590
591         test_cx.run_debuginfo_gdb_test_no_opt();
592     }
593
594     fn run_debuginfo_gdb_test_no_opt(&self) {
595         let prefixes = if self.config.gdb_native_rust {
596             // GDB with Rust
597             static PREFIXES: &'static [&'static str] = &["gdb", "gdbr"];
598             println!("NOTE: compiletest thinks it is using GDB with native rust support");
599             PREFIXES
600         } else {
601             // Generic GDB
602             static PREFIXES: &'static [&'static str] = &["gdb", "gdbg"];
603             println!("NOTE: compiletest thinks it is using GDB without native rust support");
604             PREFIXES
605         };
606
607         let DebuggerCommands {
608             commands,
609             check_lines,
610             breakpoint_lines,
611         } = self.parse_debugger_commands(prefixes);
612         let mut cmds = commands.join("\n");
613
614         // compile test file (it should have 'compile-flags:-g' in the header)
615         let compiler_run_result = self.compile_test();
616         if !compiler_run_result.status.success() {
617             self.fatal_proc_rec("compilation failed!", &compiler_run_result);
618         }
619
620         let exe_file = self.make_exe_name();
621
622         let debugger_run_result;
623         match &*self.config.target {
624             "arm-linux-androideabi" | "armv7-linux-androideabi" | "aarch64-linux-android" => {
625                 cmds = cmds.replace("run", "continue");
626
627                 let tool_path = match self.config.android_cross_path.to_str() {
628                     Some(x) => x.to_owned(),
629                     None => self.fatal("cannot find android cross path"),
630                 };
631
632                 // write debugger script
633                 let mut script_str = String::with_capacity(2048);
634                 script_str.push_str(&format!("set charset {}\n", Self::charset()));
635                 script_str.push_str(&format!("set sysroot {}\n", tool_path));
636                 script_str.push_str(&format!("file {}\n", exe_file.to_str().unwrap()));
637                 script_str.push_str("target remote :5039\n");
638                 script_str.push_str(&format!(
639                     "set solib-search-path \
640                      ./{}/stage2/lib/rustlib/{}/lib/\n",
641                     self.config.host, self.config.target
642                 ));
643                 for line in &breakpoint_lines {
644                     script_str.push_str(
645                         &format!(
646                             "break {:?}:{}\n",
647                             self.testpaths.file.file_name().unwrap().to_string_lossy(),
648                             *line
649                         )[..],
650                     );
651                 }
652                 script_str.push_str(&cmds);
653                 script_str.push_str("\nquit\n");
654
655                 debug!("script_str = {}", script_str);
656                 self.dump_output_file(&script_str, "debugger.script");
657
658                 let adb_path = &self.config.adb_path;
659
660                 Command::new(adb_path)
661                     .arg("push")
662                     .arg(&exe_file)
663                     .arg(&self.config.adb_test_dir)
664                     .status()
665                     .expect(&format!("failed to exec `{:?}`", adb_path));
666
667                 Command::new(adb_path)
668                     .args(&["forward", "tcp:5039", "tcp:5039"])
669                     .status()
670                     .expect(&format!("failed to exec `{:?}`", adb_path));
671
672                 let adb_arg = format!(
673                     "export LD_LIBRARY_PATH={}; \
674                      gdbserver{} :5039 {}/{}",
675                     self.config.adb_test_dir.clone(),
676                     if self.config.target.contains("aarch64") {
677                         "64"
678                     } else {
679                         ""
680                     },
681                     self.config.adb_test_dir.clone(),
682                     exe_file.file_name().unwrap().to_str().unwrap()
683                 );
684
685                 debug!("adb arg: {}", adb_arg);
686                 let mut adb = Command::new(adb_path)
687                     .args(&["shell", &adb_arg])
688                     .stdout(Stdio::piped())
689                     .stderr(Stdio::inherit())
690                     .spawn()
691                     .expect(&format!("failed to exec `{:?}`", adb_path));
692
693                 // Wait for the gdbserver to print out "Listening on port ..."
694                 // at which point we know that it's started and then we can
695                 // execute the debugger below.
696                 let mut stdout = BufReader::new(adb.stdout.take().unwrap());
697                 let mut line = String::new();
698                 loop {
699                     line.truncate(0);
700                     stdout.read_line(&mut line).unwrap();
701                     if line.starts_with("Listening on port 5039") {
702                         break;
703                     }
704                 }
705                 drop(stdout);
706
707                 let debugger_script = self.make_out_name("debugger.script");
708                 // FIXME (#9639): This needs to handle non-utf8 paths
709                 let debugger_opts = vec![
710                     "-quiet".to_owned(),
711                     "-batch".to_owned(),
712                     "-nx".to_owned(),
713                     format!("-command={}", debugger_script.to_str().unwrap()),
714                 ];
715
716                 let mut gdb_path = tool_path;
717                 gdb_path.push_str("/bin/gdb");
718                 let Output {
719                     status,
720                     stdout,
721                     stderr,
722                 } = Command::new(&gdb_path)
723                     .args(&debugger_opts)
724                     .output()
725                     .expect(&format!("failed to exec `{:?}`", gdb_path));
726                 let cmdline = {
727                     let mut gdb = Command::new(&format!("{}-gdb", self.config.target));
728                     gdb.args(&debugger_opts);
729                     let cmdline = self.make_cmdline(&gdb, "");
730                     logv(self.config, format!("executing {}", cmdline));
731                     cmdline
732                 };
733
734                 debugger_run_result = ProcRes {
735                     status,
736                     stdout: String::from_utf8(stdout).unwrap(),
737                     stderr: String::from_utf8(stderr).unwrap(),
738                     cmdline,
739                 };
740                 if adb.kill().is_err() {
741                     println!("Adb process is already finished.");
742                 }
743             }
744
745             _ => {
746                 let rust_src_root = self.config
747                     .find_rust_src_root()
748                     .expect("Could not find Rust source root");
749                 let rust_pp_module_rel_path = Path::new("./src/etc");
750                 let rust_pp_module_abs_path = rust_src_root
751                     .join(rust_pp_module_rel_path)
752                     .to_str()
753                     .unwrap()
754                     .to_owned();
755                 // write debugger script
756                 let mut script_str = String::with_capacity(2048);
757                 script_str.push_str(&format!("set charset {}\n", Self::charset()));
758                 script_str.push_str("show version\n");
759
760                 match self.config.gdb_version {
761                     Some(version) => {
762                         println!(
763                             "NOTE: compiletest thinks it is using GDB version {}",
764                             version
765                         );
766
767                         if version > extract_gdb_version("7.4").unwrap() {
768                             // Add the directory containing the pretty printers to
769                             // GDB's script auto loading safe path
770                             script_str.push_str(&format!(
771                                 "add-auto-load-safe-path {}\n",
772                                 rust_pp_module_abs_path.replace(r"\", r"\\")
773                             ));
774                         }
775                     }
776                     _ => {
777                         println!(
778                             "NOTE: compiletest does not know which version of \
779                              GDB it is using"
780                         );
781                     }
782                 }
783
784                 // The following line actually doesn't have to do anything with
785                 // pretty printing, it just tells GDB to print values on one line:
786                 script_str.push_str("set print pretty off\n");
787
788                 // Add the pretty printer directory to GDB's source-file search path
789                 script_str.push_str(&format!("directory {}\n", rust_pp_module_abs_path));
790
791                 // Load the target executable
792                 script_str.push_str(&format!(
793                     "file {}\n",
794                     exe_file.to_str().unwrap().replace(r"\", r"\\")
795                 ));
796
797                 // Force GDB to print values in the Rust format.
798                 if self.config.gdb_native_rust {
799                     script_str.push_str("set language rust\n");
800                 }
801
802                 // Add line breakpoints
803                 for line in &breakpoint_lines {
804                     script_str.push_str(&format!(
805                         "break '{}':{}\n",
806                         self.testpaths.file.file_name().unwrap().to_string_lossy(),
807                         *line
808                     ));
809                 }
810
811                 script_str.push_str(&cmds);
812                 script_str.push_str("\nquit\n");
813
814                 debug!("script_str = {}", script_str);
815                 self.dump_output_file(&script_str, "debugger.script");
816
817                 let debugger_script = self.make_out_name("debugger.script");
818
819                 // FIXME (#9639): This needs to handle non-utf8 paths
820                 let debugger_opts = vec![
821                     "-quiet".to_owned(),
822                     "-batch".to_owned(),
823                     "-nx".to_owned(),
824                     format!("-command={}", debugger_script.to_str().unwrap()),
825                 ];
826
827                 let mut gdb = Command::new(self.config.gdb.as_ref().unwrap());
828                 gdb.args(&debugger_opts)
829                     .env("PYTHONPATH", rust_pp_module_abs_path);
830
831                 debugger_run_result = self.compose_and_run(
832                     gdb,
833                     self.config.run_lib_path.to_str().unwrap(),
834                     None,
835                     None,
836                 );
837             }
838         }
839
840         if !debugger_run_result.status.success() {
841             self.fatal_proc_rec("gdb failed to execute", &debugger_run_result);
842         }
843
844         self.check_debugger_output(&debugger_run_result, &check_lines);
845     }
846
847     fn run_debuginfo_lldb_test(&self) {
848         assert!(self.revision.is_none(), "revisions not relevant here");
849
850         if self.config.lldb_python_dir.is_none() {
851             self.fatal("Can't run LLDB test because LLDB's python path is not set.");
852         }
853
854         let config = Config {
855             target_rustcflags: self.cleanup_debug_info_options(&self.config.target_rustcflags),
856             host_rustcflags: self.cleanup_debug_info_options(&self.config.host_rustcflags),
857             ..self.config.clone()
858         };
859
860         let test_cx = TestCx {
861             config: &config,
862             ..*self
863         };
864
865         test_cx.run_debuginfo_lldb_test_no_opt();
866     }
867
868     fn run_debuginfo_lldb_test_no_opt(&self) {
869         // compile test file (it should have 'compile-flags:-g' in the header)
870         let compile_result = self.compile_test();
871         if !compile_result.status.success() {
872             self.fatal_proc_rec("compilation failed!", &compile_result);
873         }
874
875         let exe_file = self.make_exe_name();
876
877         match self.config.lldb_version {
878             Some(ref version) => {
879                 println!(
880                     "NOTE: compiletest thinks it is using LLDB version {}",
881                     version
882                 );
883             }
884             _ => {
885                 println!(
886                     "NOTE: compiletest does not know which version of \
887                      LLDB it is using"
888                 );
889             }
890         }
891
892         // Parse debugger commands etc from test files
893         let DebuggerCommands {
894             commands,
895             check_lines,
896             breakpoint_lines,
897             ..
898         } = self.parse_debugger_commands(&["lldb"]);
899
900         // Write debugger script:
901         // We don't want to hang when calling `quit` while the process is still running
902         let mut script_str = String::from("settings set auto-confirm true\n");
903
904         // Make LLDB emit its version, so we have it documented in the test output
905         script_str.push_str("version\n");
906
907         // Switch LLDB into "Rust mode"
908         let rust_src_root = self.config
909             .find_rust_src_root()
910             .expect("Could not find Rust source root");
911         let rust_pp_module_rel_path = Path::new("./src/etc/lldb_rust_formatters.py");
912         let rust_pp_module_abs_path = rust_src_root
913             .join(rust_pp_module_rel_path)
914             .to_str()
915             .unwrap()
916             .to_owned();
917
918         script_str
919             .push_str(&format!("command script import {}\n", &rust_pp_module_abs_path[..])[..]);
920         script_str.push_str("type summary add --no-value ");
921         script_str.push_str("--python-function lldb_rust_formatters.print_val ");
922         script_str.push_str("-x \".*\" --category Rust\n");
923         script_str.push_str("type category enable Rust\n");
924
925         // Set breakpoints on every line that contains the string "#break"
926         let source_file_name = self.testpaths.file.file_name().unwrap().to_string_lossy();
927         for line in &breakpoint_lines {
928             script_str.push_str(&format!(
929                 "breakpoint set --file '{}' --line {}\n",
930                 source_file_name, line
931             ));
932         }
933
934         // Append the other commands
935         for line in &commands {
936             script_str.push_str(line);
937             script_str.push_str("\n");
938         }
939
940         // Finally, quit the debugger
941         script_str.push_str("\nquit\n");
942
943         // Write the script into a file
944         debug!("script_str = {}", script_str);
945         self.dump_output_file(&script_str, "debugger.script");
946         let debugger_script = self.make_out_name("debugger.script");
947
948         // Let LLDB execute the script via lldb_batchmode.py
949         let debugger_run_result = self.run_lldb(&exe_file, &debugger_script, &rust_src_root);
950
951         if !debugger_run_result.status.success() {
952             self.fatal_proc_rec("Error while running LLDB", &debugger_run_result);
953         }
954
955         self.check_debugger_output(&debugger_run_result, &check_lines);
956     }
957
958     fn run_lldb(
959         &self,
960         test_executable: &Path,
961         debugger_script: &Path,
962         rust_src_root: &Path,
963     ) -> ProcRes {
964         // Prepare the lldb_batchmode which executes the debugger script
965         let lldb_script_path = rust_src_root.join("src/etc/lldb_batchmode.py");
966         self.cmd2procres(
967             Command::new(&self.config.lldb_python)
968                 .arg(&lldb_script_path)
969                 .arg(test_executable)
970                 .arg(debugger_script)
971                 .env("PYTHONPATH", self.config.lldb_python_dir.as_ref().unwrap()),
972         )
973     }
974
975     fn cmd2procres(&self, cmd: &mut Command) -> ProcRes {
976         let (status, out, err) = match cmd.output() {
977             Ok(Output {
978                 status,
979                 stdout,
980                 stderr,
981             }) => (
982                 status,
983                 String::from_utf8(stdout).unwrap(),
984                 String::from_utf8(stderr).unwrap(),
985             ),
986             Err(e) => self.fatal(&format!(
987                 "Failed to setup Python process for \
988                  LLDB script: {}",
989                 e
990             )),
991         };
992
993         self.dump_output(&out, &err);
994         ProcRes {
995             status,
996             stdout: out,
997             stderr: err,
998             cmdline: format!("{:?}", cmd),
999         }
1000     }
1001
1002     fn parse_debugger_commands(&self, debugger_prefixes: &[&str]) -> DebuggerCommands {
1003         let directives = debugger_prefixes
1004             .iter()
1005             .map(|prefix| (format!("{}-command", prefix), format!("{}-check", prefix)))
1006             .collect::<Vec<_>>();
1007
1008         let mut breakpoint_lines = vec![];
1009         let mut commands = vec![];
1010         let mut check_lines = vec![];
1011         let mut counter = 1;
1012         let reader = BufReader::new(File::open(&self.testpaths.file).unwrap());
1013         for line in reader.lines() {
1014             match line {
1015                 Ok(line) => {
1016                     let line = if line.starts_with("//") {
1017                         line[2..].trim_left()
1018                     } else {
1019                         line.as_str()
1020                     };
1021
1022                     if line.contains("#break") {
1023                         breakpoint_lines.push(counter);
1024                     }
1025
1026                     for &(ref command_directive, ref check_directive) in &directives {
1027                         self.config
1028                             .parse_name_value_directive(&line, command_directive)
1029                             .map(|cmd| commands.push(cmd));
1030
1031                         self.config
1032                             .parse_name_value_directive(&line, check_directive)
1033                             .map(|cmd| check_lines.push(cmd));
1034                     }
1035                 }
1036                 Err(e) => self.fatal(&format!("Error while parsing debugger commands: {}", e)),
1037             }
1038             counter += 1;
1039         }
1040
1041         DebuggerCommands {
1042             commands,
1043             check_lines,
1044             breakpoint_lines,
1045         }
1046     }
1047
1048     fn cleanup_debug_info_options(&self, options: &Option<String>) -> Option<String> {
1049         if options.is_none() {
1050             return None;
1051         }
1052
1053         // Remove options that are either unwanted (-O) or may lead to duplicates due to RUSTFLAGS.
1054         let options_to_remove = ["-O".to_owned(), "-g".to_owned(), "--debuginfo".to_owned()];
1055         let new_options = self.split_maybe_args(options)
1056             .into_iter()
1057             .filter(|x| !options_to_remove.contains(x))
1058             .collect::<Vec<String>>();
1059
1060         Some(new_options.join(" "))
1061     }
1062
1063     fn check_debugger_output(&self, debugger_run_result: &ProcRes, check_lines: &[String]) {
1064         let num_check_lines = check_lines.len();
1065
1066         let mut check_line_index = 0;
1067         for line in debugger_run_result.stdout.lines() {
1068             if check_line_index >= num_check_lines {
1069                 break;
1070             }
1071
1072             if check_single_line(line, &(check_lines[check_line_index])[..]) {
1073                 check_line_index += 1;
1074             }
1075         }
1076         if check_line_index != num_check_lines && num_check_lines > 0 {
1077             self.fatal_proc_rec(
1078                 &format!(
1079                     "line not found in debugger output: {}",
1080                     check_lines[check_line_index]
1081                 ),
1082                 debugger_run_result,
1083             );
1084         }
1085
1086         fn check_single_line(line: &str, check_line: &str) -> bool {
1087             // Allow check lines to leave parts unspecified (e.g., uninitialized
1088             // bits in the  wrong case of an enum) with the notation "[...]".
1089             let line = line.trim();
1090             let check_line = check_line.trim();
1091             let can_start_anywhere = check_line.starts_with("[...]");
1092             let can_end_anywhere = check_line.ends_with("[...]");
1093
1094             let check_fragments: Vec<&str> = check_line
1095                 .split("[...]")
1096                 .filter(|frag| !frag.is_empty())
1097                 .collect();
1098             if check_fragments.is_empty() {
1099                 return true;
1100             }
1101
1102             let (mut rest, first_fragment) = if can_start_anywhere {
1103                 match line.find(check_fragments[0]) {
1104                     Some(pos) => (&line[pos + check_fragments[0].len()..], 1),
1105                     None => return false,
1106                 }
1107             } else {
1108                 (line, 0)
1109             };
1110
1111             for current_fragment in &check_fragments[first_fragment..] {
1112                 match rest.find(current_fragment) {
1113                     Some(pos) => {
1114                         rest = &rest[pos + current_fragment.len()..];
1115                     }
1116                     None => return false,
1117                 }
1118             }
1119
1120             if !can_end_anywhere && !rest.is_empty() {
1121                 return false;
1122             }
1123
1124             true
1125         }
1126     }
1127
1128     fn check_error_patterns(&self, output_to_check: &str, proc_res: &ProcRes) {
1129         if self.props.error_patterns.is_empty() {
1130             if self.props.compile_pass {
1131                 return;
1132             } else {
1133                 self.fatal(&format!(
1134                     "no error pattern specified in {:?}",
1135                     self.testpaths.file.display()
1136                 ));
1137             }
1138         }
1139         let mut next_err_idx = 0;
1140         let mut next_err_pat = self.props.error_patterns[next_err_idx].trim();
1141         let mut done = false;
1142         for line in output_to_check.lines() {
1143             if line.contains(next_err_pat) {
1144                 debug!("found error pattern {}", next_err_pat);
1145                 next_err_idx += 1;
1146                 if next_err_idx == self.props.error_patterns.len() {
1147                     debug!("found all error patterns");
1148                     done = true;
1149                     break;
1150                 }
1151                 next_err_pat = self.props.error_patterns[next_err_idx].trim();
1152             }
1153         }
1154         if done {
1155             return;
1156         }
1157
1158         let missing_patterns = &self.props.error_patterns[next_err_idx..];
1159         if missing_patterns.len() == 1 {
1160             self.fatal_proc_rec(
1161                 &format!("error pattern '{}' not found!", missing_patterns[0]),
1162                 proc_res,
1163             );
1164         } else {
1165             for pattern in missing_patterns {
1166                 self.error(&format!("error pattern '{}' not found!", *pattern));
1167             }
1168             self.fatal_proc_rec("multiple error patterns not found", proc_res);
1169         }
1170     }
1171
1172     fn check_no_compiler_crash(&self, proc_res: &ProcRes) {
1173         for line in proc_res.stderr.lines() {
1174             if line.contains("error: internal compiler error") {
1175                 self.fatal_proc_rec("compiler encountered internal error", proc_res);
1176             } else if line.contains(" panicked at ") {
1177                 self.fatal_proc_rec("compiler panicked", proc_res);
1178             }
1179         }
1180     }
1181
1182     fn check_forbid_output(&self, output_to_check: &str, proc_res: &ProcRes) {
1183         for pat in &self.props.forbid_output {
1184             if output_to_check.contains(pat) {
1185                 self.fatal_proc_rec("forbidden pattern found in compiler output", proc_res);
1186             }
1187         }
1188     }
1189
1190     fn check_expected_errors(&self, expected_errors: Vec<errors::Error>, proc_res: &ProcRes) {
1191         if proc_res.status.success()
1192             && expected_errors
1193                 .iter()
1194                 .any(|x| x.kind == Some(ErrorKind::Error))
1195         {
1196             self.fatal_proc_rec("process did not return an error status", proc_res);
1197         }
1198
1199         // on windows, translate all '\' path separators to '/'
1200         let file_name = format!("{}", self.testpaths.file.display()).replace(r"\", "/");
1201
1202         // If the testcase being checked contains at least one expected "help"
1203         // message, then we'll ensure that all "help" messages are expected.
1204         // Otherwise, all "help" messages reported by the compiler will be ignored.
1205         // This logic also applies to "note" messages.
1206         let expect_help = expected_errors
1207             .iter()
1208             .any(|ee| ee.kind == Some(ErrorKind::Help));
1209         let expect_note = expected_errors
1210             .iter()
1211             .any(|ee| ee.kind == Some(ErrorKind::Note));
1212
1213         // Parse the JSON output from the compiler and extract out the messages.
1214         let actual_errors = json::parse_output(&file_name, &proc_res.stderr, proc_res);
1215         let mut unexpected = Vec::new();
1216         let mut found = vec![false; expected_errors.len()];
1217         for actual_error in &actual_errors {
1218             let opt_index = expected_errors.iter().enumerate().position(
1219                 |(index, expected_error)| {
1220                     !found[index] && actual_error.line_num == expected_error.line_num
1221                         && (expected_error.kind.is_none()
1222                             || actual_error.kind == expected_error.kind)
1223                         && actual_error.msg.contains(&expected_error.msg)
1224                 },
1225             );
1226
1227             match opt_index {
1228                 Some(index) => {
1229                     // found a match, everybody is happy
1230                     assert!(!found[index]);
1231                     found[index] = true;
1232                 }
1233
1234                 None => {
1235                     if self.is_unexpected_compiler_message(actual_error, expect_help, expect_note) {
1236                         self.error(&format!(
1237                             "{}:{}: unexpected {}: '{}'",
1238                             file_name,
1239                             actual_error.line_num,
1240                             actual_error
1241                                 .kind
1242                                 .as_ref()
1243                                 .map_or(String::from("message"), |k| k.to_string()),
1244                             actual_error.msg
1245                         ));
1246                         unexpected.push(actual_error);
1247                     }
1248                 }
1249             }
1250         }
1251
1252         let mut not_found = Vec::new();
1253         // anything not yet found is a problem
1254         for (index, expected_error) in expected_errors.iter().enumerate() {
1255             if !found[index] {
1256                 self.error(&format!(
1257                     "{}:{}: expected {} not found: {}",
1258                     file_name,
1259                     expected_error.line_num,
1260                     expected_error
1261                         .kind
1262                         .as_ref()
1263                         .map_or("message".into(), |k| k.to_string()),
1264                     expected_error.msg
1265                 ));
1266                 not_found.push(expected_error);
1267             }
1268         }
1269
1270         if !unexpected.is_empty() || !not_found.is_empty() {
1271             self.error(&format!(
1272                 "{} unexpected errors found, {} expected errors not found",
1273                 unexpected.len(),
1274                 not_found.len()
1275             ));
1276             println!("status: {}\ncommand: {}", proc_res.status, proc_res.cmdline);
1277             if !unexpected.is_empty() {
1278                 println!("unexpected errors (from JSON output): {:#?}\n", unexpected);
1279             }
1280             if !not_found.is_empty() {
1281                 println!("not found errors (from test file): {:#?}\n", not_found);
1282             }
1283             panic!();
1284         }
1285     }
1286
1287     /// Returns true if we should report an error about `actual_error`,
1288     /// which did not match any of the expected error. We always require
1289     /// errors/warnings to be explicitly listed, but only require
1290     /// helps/notes if there are explicit helps/notes given.
1291     fn is_unexpected_compiler_message(
1292         &self,
1293         actual_error: &Error,
1294         expect_help: bool,
1295         expect_note: bool,
1296     ) -> bool {
1297         match actual_error.kind {
1298             Some(ErrorKind::Help) => expect_help,
1299             Some(ErrorKind::Note) => expect_note,
1300             Some(ErrorKind::Error) | Some(ErrorKind::Warning) => true,
1301             Some(ErrorKind::Suggestion) | None => false,
1302         }
1303     }
1304
1305     fn compile_test(&self) -> ProcRes {
1306         let mut rustc = self.make_compile_args(
1307             &self.testpaths.file,
1308             TargetLocation::ThisFile(self.make_exe_name()),
1309         );
1310
1311         rustc.arg("-L").arg(&self.aux_output_dir_name());
1312
1313         match self.config.mode {
1314             CompileFail | Ui => {
1315                 // compile-fail and ui tests tend to have tons of unused code as
1316                 // it's just testing various pieces of the compile, but we don't
1317                 // want to actually assert warnings about all this code. Instead
1318                 // let's just ignore unused code warnings by defaults and tests
1319                 // can turn it back on if needed.
1320                 if !self.config.src_base.ends_with("rustdoc-ui") {
1321                     rustc.args(&["-A", "unused"]);
1322                 }
1323             }
1324             _ => {}
1325         }
1326
1327         self.compose_and_run_compiler(rustc, None)
1328     }
1329
1330     fn document(&self, out_dir: &Path) -> ProcRes {
1331         if self.props.build_aux_docs {
1332             for rel_ab in &self.props.aux_builds {
1333                 let aux_testpaths = self.compute_aux_test_paths(rel_ab);
1334                 let aux_props =
1335                     self.props
1336                         .from_aux_file(&aux_testpaths.file, self.revision, self.config);
1337                 let aux_cx = TestCx {
1338                     config: self.config,
1339                     props: &aux_props,
1340                     testpaths: &aux_testpaths,
1341                     revision: self.revision,
1342                 };
1343                 // Create the directory for the stdout/stderr files.
1344                 create_dir_all(aux_cx.output_base_dir()).unwrap();
1345                 let auxres = aux_cx.document(out_dir);
1346                 if !auxres.status.success() {
1347                     return auxres;
1348                 }
1349             }
1350         }
1351
1352         let aux_dir = self.aux_output_dir_name();
1353
1354         let rustdoc_path = self.config
1355             .rustdoc_path
1356             .as_ref()
1357             .expect("--rustdoc-path passed");
1358         let mut rustdoc = Command::new(rustdoc_path);
1359
1360         rustdoc
1361             .arg("-L")
1362             .arg(self.config.run_lib_path.to_str().unwrap())
1363             .arg("-L")
1364             .arg(aux_dir)
1365             .arg("-o")
1366             .arg(out_dir)
1367             .arg(&self.testpaths.file)
1368             .args(&self.props.compile_flags);
1369         if let Some(ref linker) = self.config.linker {
1370             rustdoc
1371                 .arg("--linker")
1372                 .arg(linker)
1373                 .arg("-Z")
1374                 .arg("unstable-options");
1375         }
1376
1377         self.compose_and_run_compiler(rustdoc, None)
1378     }
1379
1380     fn exec_compiled_test(&self) -> ProcRes {
1381         let env = &self.props.exec_env;
1382
1383         let proc_res = match &*self.config.target {
1384             // This is pretty similar to below, we're transforming:
1385             //
1386             //      program arg1 arg2
1387             //
1388             // into
1389             //
1390             //      remote-test-client run program:support-lib.so arg1 arg2
1391             //
1392             // The test-client program will upload `program` to the emulator
1393             // along with all other support libraries listed (in this case
1394             // `support-lib.so`. It will then execute the program on the
1395             // emulator with the arguments specified (in the environment we give
1396             // the process) and then report back the same result.
1397             _ if self.config.remote_test_client.is_some() => {
1398                 let aux_dir = self.aux_output_dir_name();
1399                 let ProcArgs { mut prog, args } = self.make_run_args();
1400                 if let Ok(entries) = aux_dir.read_dir() {
1401                     for entry in entries {
1402                         let entry = entry.unwrap();
1403                         if !entry.path().is_file() {
1404                             continue;
1405                         }
1406                         prog.push_str(":");
1407                         prog.push_str(entry.path().to_str().unwrap());
1408                     }
1409                 }
1410                 let mut test_client =
1411                     Command::new(self.config.remote_test_client.as_ref().unwrap());
1412                 test_client
1413                     .args(&["run", &prog])
1414                     .args(args)
1415                     .envs(env.clone());
1416                 self.compose_and_run(
1417                     test_client,
1418                     self.config.run_lib_path.to_str().unwrap(),
1419                     Some(aux_dir.to_str().unwrap()),
1420                     None,
1421                 )
1422             }
1423             _ => {
1424                 let aux_dir = self.aux_output_dir_name();
1425                 let ProcArgs { prog, args } = self.make_run_args();
1426                 let mut program = Command::new(&prog);
1427                 program
1428                     .args(args)
1429                     .current_dir(&self.output_base_dir())
1430                     .envs(env.clone());
1431                 self.compose_and_run(
1432                     program,
1433                     self.config.run_lib_path.to_str().unwrap(),
1434                     Some(aux_dir.to_str().unwrap()),
1435                     None,
1436                 )
1437             }
1438         };
1439
1440         if proc_res.status.success() {
1441             // delete the executable after running it to save space.
1442             // it is ok if the deletion failed.
1443             let _ = fs::remove_file(self.make_exe_name());
1444         }
1445
1446         proc_res
1447     }
1448
1449     /// For each `aux-build: foo/bar` annotation, we check to find the
1450     /// file in a `auxiliary` directory relative to the test itself.
1451     fn compute_aux_test_paths(&self, rel_ab: &str) -> TestPaths {
1452         let test_ab = self.testpaths
1453             .file
1454             .parent()
1455             .expect("test file path has no parent")
1456             .join("auxiliary")
1457             .join(rel_ab);
1458         if !test_ab.exists() {
1459             self.fatal(&format!(
1460                 "aux-build `{}` source not found",
1461                 test_ab.display()
1462             ))
1463         }
1464
1465         TestPaths {
1466             file: test_ab,
1467             relative_dir: self.testpaths
1468                 .relative_dir
1469                 .join(self.output_testname_unique())
1470                 .join("auxiliary")
1471                 .join(rel_ab)
1472                 .parent()
1473                 .expect("aux-build path has no parent")
1474                 .to_path_buf(),
1475         }
1476     }
1477
1478     fn compose_and_run_compiler(&self, mut rustc: Command, input: Option<String>) -> ProcRes {
1479         let aux_dir = self.aux_output_dir_name();
1480
1481         if !self.props.aux_builds.is_empty() {
1482             let _ = fs::remove_dir_all(&aux_dir);
1483             create_dir_all(&aux_dir).unwrap();
1484         }
1485
1486         for rel_ab in &self.props.aux_builds {
1487             let aux_testpaths = self.compute_aux_test_paths(rel_ab);
1488             let aux_props =
1489                 self.props
1490                     .from_aux_file(&aux_testpaths.file, self.revision, self.config);
1491             let aux_output = TargetLocation::ThisDirectory(self.aux_output_dir_name());
1492             let aux_cx = TestCx {
1493                 config: self.config,
1494                 props: &aux_props,
1495                 testpaths: &aux_testpaths,
1496                 revision: self.revision,
1497             };
1498             // Create the directory for the stdout/stderr files.
1499             create_dir_all(aux_cx.output_base_dir()).unwrap();
1500             let mut aux_rustc = aux_cx.make_compile_args(&aux_testpaths.file, aux_output);
1501
1502             let crate_type = if aux_props.no_prefer_dynamic {
1503                 None
1504             } else if self.config.target.contains("cloudabi")
1505                 || self.config.target.contains("emscripten")
1506                 || (self.config.target.contains("musl") && !aux_props.force_host)
1507                 || self.config.target.contains("wasm32")
1508             {
1509                 // We primarily compile all auxiliary libraries as dynamic libraries
1510                 // to avoid code size bloat and large binaries as much as possible
1511                 // for the test suite (otherwise including libstd statically in all
1512                 // executables takes up quite a bit of space).
1513                 //
1514                 // For targets like MUSL or Emscripten, however, there is no support for
1515                 // dynamic libraries so we just go back to building a normal library. Note,
1516                 // however, that for MUSL if the library is built with `force_host` then
1517                 // it's ok to be a dylib as the host should always support dylibs.
1518                 Some("lib")
1519             } else {
1520                 Some("dylib")
1521             };
1522
1523             if let Some(crate_type) = crate_type {
1524                 aux_rustc.args(&["--crate-type", crate_type]);
1525             }
1526
1527             aux_rustc.arg("-L").arg(&aux_dir);
1528
1529             let auxres = aux_cx.compose_and_run(
1530                 aux_rustc,
1531                 aux_cx.config.compile_lib_path.to_str().unwrap(),
1532                 Some(aux_dir.to_str().unwrap()),
1533                 None,
1534             );
1535             if !auxres.status.success() {
1536                 self.fatal_proc_rec(
1537                     &format!(
1538                         "auxiliary build of {:?} failed to compile: ",
1539                         aux_testpaths.file.display()
1540                     ),
1541                     &auxres,
1542                 );
1543             }
1544         }
1545
1546         rustc.envs(self.props.rustc_env.clone());
1547         self.compose_and_run(
1548             rustc,
1549             self.config.compile_lib_path.to_str().unwrap(),
1550             Some(aux_dir.to_str().unwrap()),
1551             input,
1552         )
1553     }
1554
1555     fn compose_and_run(
1556         &self,
1557         mut command: Command,
1558         lib_path: &str,
1559         aux_path: Option<&str>,
1560         input: Option<String>,
1561     ) -> ProcRes {
1562         let cmdline = {
1563             let cmdline = self.make_cmdline(&command, lib_path);
1564             logv(self.config, format!("executing {}", cmdline));
1565             cmdline
1566         };
1567
1568         command
1569             .stdout(Stdio::piped())
1570             .stderr(Stdio::piped())
1571             .stdin(Stdio::piped());
1572
1573         // Need to be sure to put both the lib_path and the aux path in the dylib
1574         // search path for the child.
1575         let mut path = env::split_paths(&env::var_os(dylib_env_var()).unwrap_or(OsString::new()))
1576             .collect::<Vec<_>>();
1577         if let Some(p) = aux_path {
1578             path.insert(0, PathBuf::from(p))
1579         }
1580         path.insert(0, PathBuf::from(lib_path));
1581
1582         // Add the new dylib search path var
1583         let newpath = env::join_paths(&path).unwrap();
1584         command.env(dylib_env_var(), newpath);
1585
1586         let mut child = disable_error_reporting(|| command.spawn())
1587             .expect(&format!("failed to exec `{:?}`", &command));
1588         if let Some(input) = input {
1589             child
1590                 .stdin
1591                 .as_mut()
1592                 .unwrap()
1593                 .write_all(input.as_bytes())
1594                 .unwrap();
1595         }
1596
1597         let Output {
1598             status,
1599             stdout,
1600             stderr,
1601         } = read2_abbreviated(child).expect("failed to read output");
1602
1603         let result = ProcRes {
1604             status,
1605             stdout: String::from_utf8_lossy(&stdout).into_owned(),
1606             stderr: String::from_utf8_lossy(&stderr).into_owned(),
1607             cmdline,
1608         };
1609
1610         self.dump_output(&result.stdout, &result.stderr);
1611
1612         result
1613     }
1614
1615     fn make_compile_args(&self, input_file: &Path, output_file: TargetLocation) -> Command {
1616         let is_rustdoc = self.config.src_base.ends_with("rustdoc-ui");
1617         let mut rustc = if !is_rustdoc {
1618             Command::new(&self.config.rustc_path)
1619         } else {
1620             Command::new(&self.config
1621                 .rustdoc_path
1622                 .clone()
1623                 .expect("no rustdoc built yet"))
1624         };
1625         // FIXME Why is -L here?
1626         rustc.arg(input_file);//.arg("-L").arg(&self.config.build_base);
1627
1628         // Optionally prevent default --target if specified in test compile-flags.
1629         let custom_target = self.props
1630             .compile_flags
1631             .iter()
1632             .any(|x| x.starts_with("--target"));
1633
1634         if !custom_target {
1635             let target = if self.props.force_host {
1636                 &*self.config.host
1637             } else {
1638                 &*self.config.target
1639             };
1640
1641             rustc.arg(&format!("--target={}", target));
1642         }
1643
1644         if let Some(revision) = self.revision {
1645             rustc.args(&["--cfg", revision]);
1646         }
1647
1648         if !is_rustdoc {
1649             if let Some(ref incremental_dir) = self.props.incremental_dir {
1650                 rustc.args(&["-C", &format!("incremental={}", incremental_dir.display())]);
1651                 rustc.args(&["-Z", "incremental-verify-ich"]);
1652                 rustc.args(&["-Z", "incremental-queries"]);
1653             }
1654
1655             if self.config.mode == CodegenUnits {
1656                 rustc.args(&["-Z", "human_readable_cgu_names"]);
1657             }
1658         }
1659
1660         match self.config.mode {
1661             CompileFail | ParseFail | Incremental => {
1662                 // If we are extracting and matching errors in the new
1663                 // fashion, then you want JSON mode. Old-skool error
1664                 // patterns still match the raw compiler output.
1665                 if self.props.error_patterns.is_empty() {
1666                     rustc.args(&["--error-format", "json"]);
1667                 }
1668                 if !self.props.disable_ui_testing_normalization {
1669                     rustc.arg("-Zui-testing");
1670                 }
1671             }
1672             Ui => {
1673                 if !self.props
1674                     .compile_flags
1675                     .iter()
1676                     .any(|s| s.starts_with("--error-format"))
1677                 {
1678                     rustc.args(&["--error-format", "json"]);
1679                 }
1680                 if !self.props.disable_ui_testing_normalization {
1681                     rustc.arg("-Zui-testing");
1682                 }
1683             }
1684             MirOpt => {
1685                 rustc.args(&[
1686                     "-Zdump-mir=all",
1687                     "-Zmir-opt-level=3",
1688                     "-Zdump-mir-exclude-pass-number",
1689                 ]);
1690
1691                 let mir_dump_dir = self.get_mir_dump_dir();
1692                 let _ = fs::remove_dir_all(&mir_dump_dir);
1693                 create_dir_all(mir_dump_dir.as_path()).unwrap();
1694                 let mut dir_opt = "-Zdump-mir-dir=".to_string();
1695                 dir_opt.push_str(mir_dump_dir.to_str().unwrap());
1696                 debug!("dir_opt: {:?}", dir_opt);
1697
1698                 rustc.arg(dir_opt);
1699             }
1700             RunPass | RunFail | RunPassValgrind | Pretty | DebugInfoGdb | DebugInfoLldb
1701             | Codegen | Rustdoc | RunMake | CodegenUnits => {
1702                 // do not use JSON output
1703             }
1704         }
1705
1706         if self.props.skip_codegen {
1707             assert!(!self.props.compile_flags.iter().any(|s| s.starts_with("--emit")));
1708             rustc.args(&["--emit", "metadata"]);
1709         }
1710
1711         if !is_rustdoc {
1712             if self.config.target == "wasm32-unknown-unknown" {
1713                 // rustc.arg("-g"); // get any backtrace at all on errors
1714             } else if !self.props.no_prefer_dynamic {
1715                 rustc.args(&["-C", "prefer-dynamic"]);
1716             }
1717         }
1718
1719         match output_file {
1720             TargetLocation::ThisFile(path) => {
1721                 rustc.arg("-o").arg(path);
1722             }
1723             TargetLocation::ThisDirectory(path) => {
1724                 rustc.arg("--out-dir").arg(path);
1725             }
1726         }
1727
1728         match self.config.compare_mode {
1729             Some(CompareMode::Nll) => {
1730                 rustc.args(&["-Zborrowck=mir", "-Ztwo-phase-borrows"]);
1731             }
1732             None => {}
1733         }
1734
1735         if self.props.force_host {
1736             rustc.args(self.split_maybe_args(&self.config.host_rustcflags));
1737         } else {
1738             rustc.args(self.split_maybe_args(&self.config.target_rustcflags));
1739         }
1740         if !is_rustdoc {
1741             if let Some(ref linker) = self.config.linker {
1742                 rustc.arg(format!("-Clinker={}", linker));
1743             }
1744         }
1745
1746         rustc.args(&self.props.compile_flags);
1747
1748         rustc
1749     }
1750
1751     fn make_exe_name(&self) -> PathBuf {
1752         // Using a single letter here to keep the path length down for
1753         // Windows.  Some test names get very long.  rustc creates `rcgu`
1754         // files with the module name appended to it which can more than
1755         // double the length.
1756         let mut f = self.output_base_dir().join("a");
1757         // FIXME: This is using the host architecture exe suffix, not target!
1758         if self.config.target.contains("emscripten") {
1759             f = f.with_extra_extension("js");
1760         } else if self.config.target.contains("wasm32") {
1761             f = f.with_extra_extension("wasm");
1762         } else if !env::consts::EXE_SUFFIX.is_empty() {
1763             f = f.with_extra_extension(env::consts::EXE_SUFFIX);
1764         }
1765         f
1766     }
1767
1768     fn make_run_args(&self) -> ProcArgs {
1769         // If we've got another tool to run under (valgrind),
1770         // then split apart its command
1771         let mut args = self.split_maybe_args(&self.config.runtool);
1772
1773         // If this is emscripten, then run tests under nodejs
1774         if self.config.target.contains("emscripten") {
1775             if let Some(ref p) = self.config.nodejs {
1776                 args.push(p.clone());
1777             } else {
1778                 self.fatal("no NodeJS binary found (--nodejs)");
1779             }
1780         }
1781
1782         // If this is otherwise wasm , then run tests under nodejs with our
1783         // shim
1784         if self.config.target.contains("wasm32") {
1785             if let Some(ref p) = self.config.nodejs {
1786                 args.push(p.clone());
1787             } else {
1788                 self.fatal("no NodeJS binary found (--nodejs)");
1789             }
1790
1791             let src = self.config.src_base
1792                 .parent().unwrap() // chop off `run-pass`
1793                 .parent().unwrap() // chop off `test`
1794                 .parent().unwrap(); // chop off `src`
1795             args.push(src.join("src/etc/wasm32-shim.js").display().to_string());
1796         }
1797
1798         let exe_file = self.make_exe_name();
1799
1800         // FIXME (#9639): This needs to handle non-utf8 paths
1801         args.push(exe_file.to_str().unwrap().to_owned());
1802
1803         // Add the arguments in the run_flags directive
1804         args.extend(self.split_maybe_args(&self.props.run_flags));
1805
1806         let prog = args.remove(0);
1807         ProcArgs { prog, args }
1808     }
1809
1810     fn split_maybe_args(&self, argstr: &Option<String>) -> Vec<String> {
1811         match *argstr {
1812             Some(ref s) => s.split(' ')
1813                 .filter_map(|s| {
1814                     if s.chars().all(|c| c.is_whitespace()) {
1815                         None
1816                     } else {
1817                         Some(s.to_owned())
1818                     }
1819                 })
1820                 .collect(),
1821             None => Vec::new(),
1822         }
1823     }
1824
1825     fn make_cmdline(&self, command: &Command, libpath: &str) -> String {
1826         use util;
1827
1828         // Linux and mac don't require adjusting the library search path
1829         if cfg!(unix) {
1830             format!("{:?}", command)
1831         } else {
1832             // Build the LD_LIBRARY_PATH variable as it would be seen on the command line
1833             // for diagnostic purposes
1834             fn lib_path_cmd_prefix(path: &str) -> String {
1835                 format!(
1836                     "{}=\"{}\"",
1837                     util::lib_path_env_var(),
1838                     util::make_new_path(path)
1839                 )
1840             }
1841
1842             format!("{} {:?}", lib_path_cmd_prefix(libpath), command)
1843         }
1844     }
1845
1846     fn dump_output(&self, out: &str, err: &str) {
1847         let revision = if let Some(r) = self.revision {
1848             format!("{}.", r)
1849         } else {
1850             String::new()
1851         };
1852
1853         self.dump_output_file(out, &format!("{}out", revision));
1854         self.dump_output_file(err, &format!("{}err", revision));
1855         self.maybe_dump_to_stdout(out, err);
1856     }
1857
1858     fn dump_output_file(&self, out: &str, extension: &str) {
1859         let outfile = self.make_out_name(extension);
1860         File::create(&outfile)
1861             .unwrap()
1862             .write_all(out.as_bytes())
1863             .unwrap();
1864     }
1865
1866     /// Create a filename for output with the given extension.  Example:
1867     ///   /.../testname.revision.mode/testname.extension
1868     fn make_out_name(&self, extension: &str) -> PathBuf {
1869         self.output_base_name().with_extension(extension)
1870     }
1871
1872     /// Directory where auxiliary files are written.  Example:
1873     ///   /.../testname.revision.mode/auxiliary/
1874     fn aux_output_dir_name(&self) -> PathBuf {
1875         self.output_base_dir()
1876             .join("auxiliary")
1877             .with_extra_extension(self.config.mode.disambiguator())
1878     }
1879
1880     /// Generates a unique name for the test, such as `testname.revision.mode`.
1881     fn output_testname_unique(&self) -> PathBuf {
1882         output_testname_unique(self.config, self.testpaths, self.safe_revision())
1883     }
1884
1885     /// The revision, ignored for Incremental since it wants all revisions in
1886     /// the same directory.
1887     fn safe_revision(&self) -> Option<&str> {
1888         if self.config.mode == Incremental {
1889             None
1890         } else {
1891             self.revision
1892         }
1893     }
1894
1895     /// Absolute path to the directory where all output for the given
1896     /// test/revision should reside.  Example:
1897     ///   /path/to/build/host-triple/test/ui/relative/testname.revision.mode/
1898     fn output_base_dir(&self) -> PathBuf {
1899         output_base_dir(self.config, self.testpaths, self.safe_revision())
1900     }
1901
1902     /// Absolute path to the base filename used as output for the given
1903     /// test/revision.  Example:
1904     ///   /.../relative/testname.revision.mode/testname
1905     fn output_base_name(&self) -> PathBuf {
1906         output_base_name(self.config, self.testpaths, self.safe_revision())
1907     }
1908
1909     fn maybe_dump_to_stdout(&self, out: &str, err: &str) {
1910         if self.config.verbose {
1911             println!("------{}------------------------------", "stdout");
1912             println!("{}", out);
1913             println!("------{}------------------------------", "stderr");
1914             println!("{}", err);
1915             println!("------------------------------------------");
1916         }
1917     }
1918
1919     fn error(&self, err: &str) {
1920         match self.revision {
1921             Some(rev) => println!("\nerror in revision `{}`: {}", rev, err),
1922             None => println!("\nerror: {}", err),
1923         }
1924     }
1925
1926     fn fatal(&self, err: &str) -> ! {
1927         self.error(err);
1928         panic!();
1929     }
1930
1931     fn fatal_proc_rec(&self, err: &str, proc_res: &ProcRes) -> ! {
1932         self.try_print_open_handles();
1933         self.error(err);
1934         proc_res.fatal(None);
1935     }
1936
1937     // This function is a poor man's attempt to debug rust-lang/rust#38620, if
1938     // that's closed then this should be deleted
1939     //
1940     // This is a very "opportunistic" debugging attempt, so we ignore all
1941     // errors here.
1942     fn try_print_open_handles(&self) {
1943         if !cfg!(windows) {
1944             return;
1945         }
1946         if self.config.mode != Incremental {
1947             return;
1948         }
1949
1950         let filename = match self.testpaths.file.file_stem() {
1951             Some(path) => path,
1952             None => return,
1953         };
1954
1955         let mut cmd = Command::new("handle.exe");
1956         cmd.arg("-a").arg("-u");
1957         cmd.arg(filename);
1958         cmd.arg("-nobanner");
1959         cmd.stdout(Stdio::piped());
1960         cmd.stderr(Stdio::piped());
1961         let output = match cmd.spawn().and_then(read2_abbreviated) {
1962             Ok(output) => output,
1963             Err(_) => return,
1964         };
1965         println!("---------------------------------------------------");
1966         println!("ran extra command to debug rust-lang/rust#38620: ");
1967         println!("{:?}", cmd);
1968         println!("result: {}", output.status);
1969         println!("--- stdout ----------------------------------------");
1970         println!("{}", String::from_utf8_lossy(&output.stdout));
1971         println!("--- stderr ----------------------------------------");
1972         println!("{}", String::from_utf8_lossy(&output.stderr));
1973         println!("---------------------------------------------------");
1974     }
1975
1976     // codegen tests (using FileCheck)
1977
1978     fn compile_test_and_save_ir(&self) -> ProcRes {
1979         let aux_dir = self.aux_output_dir_name();
1980
1981         let output_file = TargetLocation::ThisDirectory(self.output_base_dir());
1982         let mut rustc = self.make_compile_args(&self.testpaths.file, output_file);
1983         rustc.arg("-L").arg(aux_dir).arg("--emit=llvm-ir");
1984
1985         self.compose_and_run_compiler(rustc, None)
1986     }
1987
1988     fn check_ir_with_filecheck(&self) -> ProcRes {
1989         let irfile = self.output_base_name().with_extension("ll");
1990         let mut filecheck = Command::new(self.config.llvm_filecheck.as_ref().unwrap());
1991         filecheck
1992             .arg("--input-file")
1993             .arg(irfile)
1994             .arg(&self.testpaths.file);
1995         self.compose_and_run(filecheck, "", None, None)
1996     }
1997
1998     fn run_codegen_test(&self) {
1999         assert!(self.revision.is_none(), "revisions not relevant here");
2000
2001         if self.config.llvm_filecheck.is_none() {
2002             self.fatal("missing --llvm-filecheck");
2003         }
2004
2005         let mut proc_res = self.compile_test_and_save_ir();
2006         if !proc_res.status.success() {
2007             self.fatal_proc_rec("compilation failed!", &proc_res);
2008         }
2009
2010         proc_res = self.check_ir_with_filecheck();
2011         if !proc_res.status.success() {
2012             self.fatal_proc_rec("verification with 'FileCheck' failed", &proc_res);
2013         }
2014     }
2015
2016     fn charset() -> &'static str {
2017         // FreeBSD 10.1 defaults to GDB 6.1.1 which doesn't support "auto" charset
2018         if cfg!(target_os = "bitrig") {
2019             "auto"
2020         } else if cfg!(target_os = "freebsd") {
2021             "ISO-8859-1"
2022         } else {
2023             "UTF-8"
2024         }
2025     }
2026
2027     fn run_rustdoc_test(&self) {
2028         assert!(self.revision.is_none(), "revisions not relevant here");
2029
2030         let out_dir = self.output_base_dir();
2031         let _ = fs::remove_dir_all(&out_dir);
2032         create_dir_all(&out_dir).unwrap();
2033
2034         let proc_res = self.document(&out_dir);
2035         if !proc_res.status.success() {
2036             self.fatal_proc_rec("rustdoc failed!", &proc_res);
2037         }
2038
2039         if self.props.check_test_line_numbers_match {
2040             self.check_rustdoc_test_option(proc_res);
2041         } else {
2042             let root = self.config.find_rust_src_root().unwrap();
2043             let res = self.cmd2procres(
2044                 Command::new(&self.config.docck_python)
2045                     .arg(root.join("src/etc/htmldocck.py"))
2046                     .arg(out_dir)
2047                     .arg(&self.testpaths.file),
2048             );
2049             if !res.status.success() {
2050                 self.fatal_proc_rec("htmldocck failed!", &res);
2051             }
2052         }
2053     }
2054
2055     fn get_lines<P: AsRef<Path>>(
2056         &self,
2057         path: &P,
2058         mut other_files: Option<&mut Vec<String>>,
2059     ) -> Vec<usize> {
2060         let mut file =
2061             fs::File::open(path).expect("markdown_test_output_check_entry File::open failed");
2062         let mut content = String::new();
2063         file.read_to_string(&mut content)
2064             .expect("markdown_test_output_check_entry read_to_string failed");
2065         let mut ignore = false;
2066         content
2067             .lines()
2068             .enumerate()
2069             .filter_map(|(line_nb, line)| {
2070                 if (line.trim_left().starts_with("pub mod ")
2071                     || line.trim_left().starts_with("mod "))
2072                     && line.ends_with(';')
2073                 {
2074                     if let Some(ref mut other_files) = other_files {
2075                         other_files.push(line.rsplit("mod ").next().unwrap().replace(";", ""));
2076                     }
2077                     None
2078                 } else {
2079                     let sline = line.split("///").last().unwrap_or("");
2080                     let line = sline.trim_left();
2081                     if line.starts_with("```") {
2082                         if ignore {
2083                             ignore = false;
2084                             None
2085                         } else {
2086                             ignore = true;
2087                             Some(line_nb + 1)
2088                         }
2089                     } else {
2090                         None
2091                     }
2092                 }
2093             })
2094             .collect()
2095     }
2096
2097     fn check_rustdoc_test_option(&self, res: ProcRes) {
2098         let mut other_files = Vec::new();
2099         let mut files: HashMap<String, Vec<usize>> = HashMap::new();
2100         let cwd = env::current_dir().unwrap();
2101         files.insert(
2102             self.testpaths
2103                 .file
2104                 .strip_prefix(&cwd)
2105                 .unwrap_or(&self.testpaths.file)
2106                 .to_str()
2107                 .unwrap()
2108                 .replace('\\', "/"),
2109             self.get_lines(&self.testpaths.file, Some(&mut other_files)),
2110         );
2111         for other_file in other_files {
2112             let mut path = self.testpaths.file.clone();
2113             path.set_file_name(&format!("{}.rs", other_file));
2114             files.insert(
2115                 path.strip_prefix(&cwd)
2116                     .unwrap_or(&path)
2117                     .to_str()
2118                     .unwrap()
2119                     .replace('\\', "/"),
2120                 self.get_lines(&path, None),
2121             );
2122         }
2123
2124         let mut tested = 0;
2125         for _ in res.stdout
2126             .split('\n')
2127             .filter(|s| s.starts_with("test "))
2128             .inspect(|s| {
2129                 let tmp: Vec<&str> = s.split(" - ").collect();
2130                 if tmp.len() == 2 {
2131                     let path = tmp[0].rsplit("test ").next().unwrap();
2132                     if let Some(ref mut v) = files.get_mut(&path.replace('\\', "/")) {
2133                         tested += 1;
2134                         let mut iter = tmp[1].split("(line ");
2135                         iter.next();
2136                         let line = iter.next()
2137                             .unwrap_or(")")
2138                             .split(')')
2139                             .next()
2140                             .unwrap_or("0")
2141                             .parse()
2142                             .unwrap_or(0);
2143                         if let Ok(pos) = v.binary_search(&line) {
2144                             v.remove(pos);
2145                         } else {
2146                             self.fatal_proc_rec(
2147                                 &format!("Not found doc test: \"{}\" in \"{}\":{:?}", s, path, v),
2148                                 &res,
2149                             );
2150                         }
2151                     }
2152                 }
2153             }) {}
2154         if tested == 0 {
2155             self.fatal_proc_rec(&format!("No test has been found... {:?}", files), &res);
2156         } else {
2157             for (entry, v) in &files {
2158                 if !v.is_empty() {
2159                     self.fatal_proc_rec(
2160                         &format!(
2161                             "Not found test at line{} \"{}\":{:?}",
2162                             if v.len() > 1 { "s" } else { "" },
2163                             entry,
2164                             v
2165                         ),
2166                         &res,
2167                     );
2168                 }
2169             }
2170         }
2171     }
2172
2173     fn run_codegen_units_test(&self) {
2174         assert!(self.revision.is_none(), "revisions not relevant here");
2175
2176         let proc_res = self.compile_test();
2177
2178         if !proc_res.status.success() {
2179             self.fatal_proc_rec("compilation failed!", &proc_res);
2180         }
2181
2182         self.check_no_compiler_crash(&proc_res);
2183
2184         const PREFIX: &'static str = "MONO_ITEM ";
2185         const CGU_MARKER: &'static str = "@@";
2186
2187         let actual: Vec<MonoItem> = proc_res
2188             .stdout
2189             .lines()
2190             .filter(|line| line.starts_with(PREFIX))
2191             .map(str_to_mono_item)
2192             .collect();
2193
2194         let expected: Vec<MonoItem> = errors::load_errors(&self.testpaths.file, None)
2195             .iter()
2196             .map(|e| str_to_mono_item(&e.msg[..]))
2197             .collect();
2198
2199         let mut missing = Vec::new();
2200         let mut wrong_cgus = Vec::new();
2201
2202         for expected_item in &expected {
2203             let actual_item_with_same_name = actual.iter().find(|ti| ti.name == expected_item.name);
2204
2205             if let Some(actual_item) = actual_item_with_same_name {
2206                 if !expected_item.codegen_units.is_empty() &&
2207                    // Also check for codegen units
2208                    expected_item.codegen_units != actual_item.codegen_units
2209                 {
2210                     wrong_cgus.push((expected_item.clone(), actual_item.clone()));
2211                 }
2212             } else {
2213                 missing.push(expected_item.string.clone());
2214             }
2215         }
2216
2217         let unexpected: Vec<_> = actual
2218             .iter()
2219             .filter(|acgu| !expected.iter().any(|ecgu| acgu.name == ecgu.name))
2220             .map(|acgu| acgu.string.clone())
2221             .collect();
2222
2223         if !missing.is_empty() {
2224             missing.sort();
2225
2226             println!("\nThese items should have been contained but were not:\n");
2227
2228             for item in &missing {
2229                 println!("{}", item);
2230             }
2231
2232             println!("\n");
2233         }
2234
2235         if !unexpected.is_empty() {
2236             let sorted = {
2237                 let mut sorted = unexpected.clone();
2238                 sorted.sort();
2239                 sorted
2240             };
2241
2242             println!("\nThese items were contained but should not have been:\n");
2243
2244             for item in sorted {
2245                 println!("{}", item);
2246             }
2247
2248             println!("\n");
2249         }
2250
2251         if !wrong_cgus.is_empty() {
2252             wrong_cgus.sort_by_key(|pair| pair.0.name.clone());
2253             println!("\nThe following items were assigned to wrong codegen units:\n");
2254
2255             for &(ref expected_item, ref actual_item) in &wrong_cgus {
2256                 println!("{}", expected_item.name);
2257                 println!(
2258                     "  expected: {}",
2259                     codegen_units_to_str(&expected_item.codegen_units)
2260                 );
2261                 println!(
2262                     "  actual:   {}",
2263                     codegen_units_to_str(&actual_item.codegen_units)
2264                 );
2265                 println!("");
2266             }
2267         }
2268
2269         if !(missing.is_empty() && unexpected.is_empty() && wrong_cgus.is_empty()) {
2270             panic!();
2271         }
2272
2273         #[derive(Clone, Eq, PartialEq)]
2274         struct MonoItem {
2275             name: String,
2276             codegen_units: HashSet<String>,
2277             string: String,
2278         }
2279
2280         // [MONO_ITEM] name [@@ (cgu)+]
2281         fn str_to_mono_item(s: &str) -> MonoItem {
2282             let s = if s.starts_with(PREFIX) {
2283                 (&s[PREFIX.len()..]).trim()
2284             } else {
2285                 s.trim()
2286             };
2287
2288             let full_string = format!("{}{}", PREFIX, s.trim().to_owned());
2289
2290             let parts: Vec<&str> = s.split(CGU_MARKER)
2291                 .map(str::trim)
2292                 .filter(|s| !s.is_empty())
2293                 .collect();
2294
2295             let name = parts[0].trim();
2296
2297             let cgus = if parts.len() > 1 {
2298                 let cgus_str = parts[1];
2299
2300                 cgus_str
2301                     .split(' ')
2302                     .map(str::trim)
2303                     .filter(|s| !s.is_empty())
2304                     .map(str::to_owned)
2305                     .collect()
2306             } else {
2307                 HashSet::new()
2308             };
2309
2310             MonoItem {
2311                 name: name.to_owned(),
2312                 codegen_units: cgus,
2313                 string: full_string,
2314             }
2315         }
2316
2317         fn codegen_units_to_str(cgus: &HashSet<String>) -> String {
2318             let mut cgus: Vec<_> = cgus.iter().collect();
2319             cgus.sort();
2320
2321             let mut string = String::new();
2322             for cgu in cgus {
2323                 string.push_str(&cgu[..]);
2324                 string.push_str(" ");
2325             }
2326
2327             string
2328         }
2329     }
2330
2331     fn init_incremental_test(&self) {
2332         // (See `run_incremental_test` for an overview of how incremental tests work.)
2333
2334         // Before any of the revisions have executed, create the
2335         // incremental workproduct directory.  Delete any old
2336         // incremental work products that may be there from prior
2337         // runs.
2338         let incremental_dir = self.incremental_dir();
2339         if incremental_dir.exists() {
2340             // Canonicalizing the path will convert it to the //?/ format
2341             // on Windows, which enables paths longer than 260 character
2342             let canonicalized = incremental_dir.canonicalize().unwrap();
2343             fs::remove_dir_all(canonicalized).unwrap();
2344         }
2345         fs::create_dir_all(&incremental_dir).unwrap();
2346
2347         if self.config.verbose {
2348             print!(
2349                 "init_incremental_test: incremental_dir={}",
2350                 incremental_dir.display()
2351             );
2352         }
2353     }
2354
2355     fn run_incremental_test(&self) {
2356         // Basic plan for a test incremental/foo/bar.rs:
2357         // - load list of revisions rpass1, cfail2, rpass3
2358         //   - each should begin with `rpass`, `cfail`, or `rfail`
2359         //   - if `rpass`, expect compile and execution to succeed
2360         //   - if `cfail`, expect compilation to fail
2361         //   - if `rfail`, expect execution to fail
2362         // - create a directory build/foo/bar.incremental
2363         // - compile foo/bar.rs with -Z incremental=.../foo/bar.incremental and -C rpass1
2364         //   - because name of revision starts with "rpass", expect success
2365         // - compile foo/bar.rs with -Z incremental=.../foo/bar.incremental and -C cfail2
2366         //   - because name of revision starts with "cfail", expect an error
2367         //   - load expected errors as usual, but filter for those that end in `[rfail2]`
2368         // - compile foo/bar.rs with -Z incremental=.../foo/bar.incremental and -C rpass3
2369         //   - because name of revision starts with "rpass", expect success
2370         // - execute build/foo/bar.exe and save output
2371         //
2372         // FIXME -- use non-incremental mode as an oracle? That doesn't apply
2373         // to #[rustc_dirty] and clean tests I guess
2374
2375         let revision = self.revision
2376             .expect("incremental tests require a list of revisions");
2377
2378         // Incremental workproduct directory should have already been created.
2379         let incremental_dir = self.incremental_dir();
2380         assert!(
2381             incremental_dir.exists(),
2382             "init_incremental_test failed to create incremental dir"
2383         );
2384
2385         // Add an extra flag pointing at the incremental directory.
2386         let mut revision_props = self.props.clone();
2387         revision_props.incremental_dir = Some(incremental_dir);
2388
2389         let revision_cx = TestCx {
2390             config: self.config,
2391             props: &revision_props,
2392             testpaths: self.testpaths,
2393             revision: self.revision,
2394         };
2395
2396         if self.config.verbose {
2397             print!(
2398                 "revision={:?} revision_props={:#?}",
2399                 revision, revision_props
2400             );
2401         }
2402
2403         if revision.starts_with("rpass") {
2404             revision_cx.run_rpass_test();
2405         } else if revision.starts_with("rfail") {
2406             revision_cx.run_rfail_test();
2407         } else if revision.starts_with("cfail") {
2408             revision_cx.run_cfail_test();
2409         } else {
2410             revision_cx.fatal("revision name must begin with rpass, rfail, or cfail");
2411         }
2412     }
2413
2414     /// Directory where incremental work products are stored.
2415     fn incremental_dir(&self) -> PathBuf {
2416         self.output_base_name().with_extension("inc")
2417     }
2418
2419     fn run_rmake_test(&self) {
2420         let cwd = env::current_dir().unwrap();
2421         let src_root = self.config
2422             .src_base
2423             .parent()
2424             .unwrap()
2425             .parent()
2426             .unwrap()
2427             .parent()
2428             .unwrap();
2429         let src_root = cwd.join(&src_root);
2430
2431         let tmpdir = cwd.join(self.output_base_name());
2432         if tmpdir.exists() {
2433             self.aggressive_rm_rf(&tmpdir).unwrap();
2434         }
2435         create_dir_all(&tmpdir).unwrap();
2436
2437         let host = &self.config.host;
2438         let make = if host.contains("bitrig") || host.contains("dragonfly")
2439             || host.contains("freebsd") || host.contains("netbsd")
2440             || host.contains("openbsd")
2441         {
2442             "gmake"
2443         } else {
2444             "make"
2445         };
2446
2447         let mut cmd = Command::new(make);
2448         cmd.current_dir(&self.testpaths.file)
2449             .stdout(Stdio::piped())
2450             .stderr(Stdio::piped())
2451             .env("TARGET", &self.config.target)
2452             .env("PYTHON", &self.config.docck_python)
2453             .env("S", src_root)
2454             .env("RUST_BUILD_STAGE", &self.config.stage_id)
2455             .env("RUSTC", cwd.join(&self.config.rustc_path))
2456             .env("TMPDIR", &tmpdir)
2457             .env("LD_LIB_PATH_ENVVAR", dylib_env_var())
2458             .env("HOST_RPATH_DIR", cwd.join(&self.config.compile_lib_path))
2459             .env("TARGET_RPATH_DIR", cwd.join(&self.config.run_lib_path))
2460             .env("LLVM_COMPONENTS", &self.config.llvm_components)
2461             .env("LLVM_CXXFLAGS", &self.config.llvm_cxxflags)
2462
2463             // We for sure don't want these tests to run in parallel, so make
2464             // sure they don't have access to these vars if we we run via `make`
2465             // at the top level
2466             .env_remove("MAKEFLAGS")
2467             .env_remove("MFLAGS")
2468             .env_remove("CARGO_MAKEFLAGS");
2469
2470         if let Some(ref rustdoc) = self.config.rustdoc_path {
2471             cmd.env("RUSTDOC", cwd.join(rustdoc));
2472         }
2473
2474         if let Some(ref node) = self.config.nodejs {
2475             cmd.env("NODE", node);
2476         }
2477
2478         if let Some(ref linker) = self.config.linker {
2479             cmd.env("RUSTC_LINKER", linker);
2480         }
2481
2482         // We don't want RUSTFLAGS set from the outside to interfere with
2483         // compiler flags set in the test cases:
2484         cmd.env_remove("RUSTFLAGS");
2485
2486         if self.config.target.contains("msvc") && self.config.cc != "" {
2487             // We need to pass a path to `lib.exe`, so assume that `cc` is `cl.exe`
2488             // and that `lib.exe` lives next to it.
2489             let lib = Path::new(&self.config.cc).parent().unwrap().join("lib.exe");
2490
2491             // MSYS doesn't like passing flags of the form `/foo` as it thinks it's
2492             // a path and instead passes `C:\msys64\foo`, so convert all
2493             // `/`-arguments to MSVC here to `-` arguments.
2494             let cflags = self.config
2495                 .cflags
2496                 .split(' ')
2497                 .map(|s| s.replace("/", "-"))
2498                 .collect::<Vec<_>>()
2499                 .join(" ");
2500
2501             cmd.env("IS_MSVC", "1")
2502                 .env("IS_WINDOWS", "1")
2503                 .env("MSVC_LIB", format!("'{}' -nologo", lib.display()))
2504                 .env("CC", format!("'{}' {}", self.config.cc, cflags))
2505                 .env("CXX", &self.config.cxx);
2506         } else {
2507             cmd.env("CC", format!("{} {}", self.config.cc, self.config.cflags))
2508                 .env("CXX", format!("{} {}", self.config.cxx, self.config.cflags))
2509                 .env("AR", &self.config.ar);
2510
2511             if self.config.target.contains("windows") {
2512                 cmd.env("IS_WINDOWS", "1");
2513             }
2514         }
2515
2516         let output = cmd.spawn()
2517             .and_then(read2_abbreviated)
2518             .expect("failed to spawn `make`");
2519         if !output.status.success() {
2520             let res = ProcRes {
2521                 status: output.status,
2522                 stdout: String::from_utf8_lossy(&output.stdout).into_owned(),
2523                 stderr: String::from_utf8_lossy(&output.stderr).into_owned(),
2524                 cmdline: format!("{:?}", cmd),
2525             };
2526             self.fatal_proc_rec("make failed", &res);
2527         }
2528     }
2529
2530     fn aggressive_rm_rf(&self, path: &Path) -> io::Result<()> {
2531         for e in path.read_dir()? {
2532             let entry = e?;
2533             let path = entry.path();
2534             if entry.file_type()?.is_dir() {
2535                 self.aggressive_rm_rf(&path)?;
2536             } else {
2537                 // Remove readonly files as well on windows (by default we can't)
2538                 fs::remove_file(&path).or_else(|e| {
2539                     if cfg!(windows) && e.kind() == io::ErrorKind::PermissionDenied {
2540                         let mut meta = entry.metadata()?.permissions();
2541                         meta.set_readonly(false);
2542                         fs::set_permissions(&path, meta)?;
2543                         fs::remove_file(&path)
2544                     } else {
2545                         Err(e)
2546                     }
2547                 })?;
2548             }
2549         }
2550         fs::remove_dir(path)
2551     }
2552
2553     fn run_ui_test(&self) {
2554         // if the user specified a format in the ui test
2555         // print the output to the stderr file, otherwise extract
2556         // the rendered error messages from json and print them
2557         let explicit = self.props
2558             .compile_flags
2559             .iter()
2560             .any(|s| s.contains("--error-format"));
2561         let proc_res = self.compile_test();
2562         self.check_if_test_should_compile(&proc_res);
2563
2564         let expected_stderr = self.load_expected_output(UI_STDERR);
2565         let expected_stdout = self.load_expected_output(UI_STDOUT);
2566         let expected_fixed = self.load_expected_output(UI_FIXED);
2567
2568         let normalized_stdout =
2569             self.normalize_output(&proc_res.stdout, &self.props.normalize_stdout);
2570
2571         let stderr = if explicit {
2572             proc_res.stderr.clone()
2573         } else {
2574             json::extract_rendered(&proc_res.stderr, &proc_res)
2575         };
2576
2577         let normalized_stderr = self.normalize_output(&stderr, &self.props.normalize_stderr);
2578
2579         let mut errors = 0;
2580         errors += self.compare_output("stdout", &normalized_stdout, &expected_stdout);
2581         errors += self.compare_output("stderr", &normalized_stderr, &expected_stderr);
2582
2583         if self.config.compare_mode.is_some() {
2584             // don't test rustfix with nll right now
2585         } else if self.props.run_rustfix {
2586             // Apply suggestions from rustc to the code itself
2587             let unfixed_code = self.load_expected_output_from_path(&self.testpaths.file)
2588                 .unwrap();
2589             let suggestions = get_suggestions_from_json(&proc_res.stderr, &HashSet::new()).unwrap();
2590             let fixed_code = apply_suggestions(&unfixed_code, &suggestions);
2591
2592             errors += self.compare_output("fixed", &fixed_code, &expected_fixed);
2593         } else if !expected_fixed.is_empty() {
2594             panic!("the `// run-rustfix` directive wasn't found but a `*.fixed` \
2595                     file was found");
2596         }
2597
2598         if errors > 0 {
2599             println!("To update references, rerun the tests and pass the `--bless` flag");
2600             let relative_path_to_file = self.testpaths
2601                 .relative_dir
2602                 .join(self.testpaths.file.file_name().unwrap());
2603             println!(
2604                 "To only update this specific test, also pass `--test-args {}`",
2605                 relative_path_to_file.display(),
2606             );
2607             self.fatal_proc_rec(
2608                 &format!("{} errors occurred comparing output.", errors),
2609                 &proc_res,
2610             );
2611         }
2612
2613         let expected_errors = errors::load_errors(&self.testpaths.file, self.revision);
2614
2615         if self.props.run_pass {
2616             let proc_res = self.exec_compiled_test();
2617
2618             if !proc_res.status.success() {
2619                 self.fatal_proc_rec("test run failed!", &proc_res);
2620             }
2621         }
2622         if !explicit && self.config.compare_mode.is_none() {
2623             if !expected_errors.is_empty() || !proc_res.status.success() {
2624                 // "// error-pattern" comments
2625                 self.check_expected_errors(expected_errors, &proc_res);
2626             } else if !self.props.error_patterns.is_empty() || !proc_res.status.success() {
2627                 // "//~ERROR comments"
2628                 self.check_error_patterns(&proc_res.stderr, &proc_res);
2629             }
2630         }
2631
2632         if self.props.run_rustfix && self.config.compare_mode.is_none() {
2633             // And finally, compile the fixed code and make sure it both
2634             // succeeds and has no diagnostics.
2635             let mut rustc = self.make_compile_args(
2636                 &self.testpaths.file.with_extension(UI_FIXED),
2637                 TargetLocation::ThisFile(self.make_exe_name()),
2638             );
2639             rustc.arg("-L").arg(&self.aux_output_dir_name());
2640             let res = self.compose_and_run_compiler(rustc, None);
2641             if !res.status.success() {
2642                 self.fatal_proc_rec("failed to compile fixed code", &res);
2643             }
2644             if !res.stderr.is_empty() {
2645                 self.fatal_proc_rec("fixed code is still producing diagnostics", &res);
2646             }
2647         }
2648     }
2649
2650     fn run_mir_opt_test(&self) {
2651         let proc_res = self.compile_test();
2652
2653         if !proc_res.status.success() {
2654             self.fatal_proc_rec("compilation failed!", &proc_res);
2655         }
2656
2657         let proc_res = self.exec_compiled_test();
2658
2659         if !proc_res.status.success() {
2660             self.fatal_proc_rec("test run failed!", &proc_res);
2661         }
2662         self.check_mir_dump();
2663     }
2664
2665     fn check_mir_dump(&self) {
2666         let mut test_file_contents = String::new();
2667         fs::File::open(self.testpaths.file.clone())
2668             .unwrap()
2669             .read_to_string(&mut test_file_contents)
2670             .unwrap();
2671         if let Some(idx) = test_file_contents.find("// END RUST SOURCE") {
2672             let (_, tests_text) = test_file_contents.split_at(idx + "// END_RUST SOURCE".len());
2673             let tests_text_str = String::from(tests_text);
2674             let mut curr_test: Option<&str> = None;
2675             let mut curr_test_contents = vec![ExpectedLine::Elision];
2676             for l in tests_text_str.lines() {
2677                 debug!("line: {:?}", l);
2678                 if l.starts_with("// START ") {
2679                     let (_, t) = l.split_at("// START ".len());
2680                     curr_test = Some(t);
2681                 } else if l.starts_with("// END") {
2682                     let (_, t) = l.split_at("// END ".len());
2683                     if Some(t) != curr_test {
2684                         panic!("mismatched START END test name");
2685                     }
2686                     self.compare_mir_test_output(curr_test.unwrap(), &curr_test_contents);
2687                     curr_test = None;
2688                     curr_test_contents.clear();
2689                     curr_test_contents.push(ExpectedLine::Elision);
2690                 } else if l.is_empty() {
2691                     // ignore
2692                 } else if l.starts_with("//") && l.split_at("//".len()).1.trim() == "..." {
2693                     curr_test_contents.push(ExpectedLine::Elision)
2694                 } else if l.starts_with("// ") {
2695                     let (_, test_content) = l.split_at("// ".len());
2696                     curr_test_contents.push(ExpectedLine::Text(test_content));
2697                 }
2698             }
2699         }
2700     }
2701
2702     fn check_mir_test_timestamp(&self, test_name: &str, output_file: &Path) {
2703         let t = |file| FileTime::from_last_modification_time(&fs::metadata(file).unwrap());
2704         let source_file = &self.testpaths.file;
2705         let output_time = t(output_file);
2706         let source_time = t(source_file);
2707         if source_time > output_time {
2708             debug!(
2709                 "source file time: {:?} output file time: {:?}",
2710                 source_time, output_time
2711             );
2712             panic!(
2713                 "test source file `{}` is newer than potentially stale output file `{}`.",
2714                 source_file.display(),
2715                 test_name
2716             );
2717         }
2718     }
2719
2720     fn compare_mir_test_output(&self, test_name: &str, expected_content: &[ExpectedLine<&str>]) {
2721         let mut output_file = PathBuf::new();
2722         output_file.push(self.get_mir_dump_dir());
2723         output_file.push(test_name);
2724         debug!("comparing the contests of: {:?}", output_file);
2725         debug!("with: {:?}", expected_content);
2726         if !output_file.exists() {
2727             panic!(
2728                 "Output file `{}` from test does not exist",
2729                 output_file.into_os_string().to_string_lossy()
2730             );
2731         }
2732         self.check_mir_test_timestamp(test_name, &output_file);
2733
2734         let mut dumped_file = fs::File::open(output_file.clone()).unwrap();
2735         let mut dumped_string = String::new();
2736         dumped_file.read_to_string(&mut dumped_string).unwrap();
2737         let mut dumped_lines = dumped_string
2738             .lines()
2739             .map(|l| nocomment_mir_line(l))
2740             .filter(|l| !l.is_empty());
2741         let mut expected_lines = expected_content
2742             .iter()
2743             .filter(|&l| {
2744                 if let &ExpectedLine::Text(l) = l {
2745                     !l.is_empty()
2746                 } else {
2747                     true
2748                 }
2749             })
2750             .peekable();
2751
2752         let compare = |expected_line, dumped_line| {
2753             let e_norm = normalize_mir_line(expected_line);
2754             let d_norm = normalize_mir_line(dumped_line);
2755             debug!("found: {:?}", d_norm);
2756             debug!("expected: {:?}", e_norm);
2757             e_norm == d_norm
2758         };
2759
2760         let error = |expected_line, extra_msg| {
2761             let normalize_all = dumped_string
2762                 .lines()
2763                 .map(nocomment_mir_line)
2764                 .filter(|l| !l.is_empty())
2765                 .collect::<Vec<_>>()
2766                 .join("\n");
2767             let f = |l: &ExpectedLine<_>| match l {
2768                 &ExpectedLine::Elision => "... (elided)".into(),
2769                 &ExpectedLine::Text(t) => t,
2770             };
2771             let expected_content = expected_content
2772                 .iter()
2773                 .map(|l| f(l))
2774                 .collect::<Vec<_>>()
2775                 .join("\n");
2776             panic!(
2777                 "Did not find expected line, error: {}\n\
2778                  Expected Line: {:?}\n\
2779                  Test Name: {}\n\
2780                  Expected:\n{}\n\
2781                  Actual:\n{}",
2782                 extra_msg,
2783                 expected_line,
2784                 test_name,
2785                 expected_content,
2786                 normalize_all
2787             );
2788         };
2789
2790         // We expect each non-empty line to appear consecutively, non-consecutive lines
2791         // must be separated by at least one Elision
2792         let mut start_block_line = None;
2793         while let Some(dumped_line) = dumped_lines.next() {
2794             match expected_lines.next() {
2795                 Some(&ExpectedLine::Text(expected_line)) => {
2796                     let normalized_expected_line = normalize_mir_line(expected_line);
2797                     if normalized_expected_line.contains(":{") {
2798                         start_block_line = Some(expected_line);
2799                     }
2800
2801                     if !compare(expected_line, dumped_line) {
2802                         error!("{:?}", start_block_line);
2803                         error(
2804                             expected_line,
2805                             format!(
2806                                 "Mismatch in lines\n\
2807                                  Current block: {}\n\
2808                                  Actual Line: {:?}",
2809                                 start_block_line.unwrap_or("None"),
2810                                 dumped_line
2811                             ),
2812                         );
2813                     }
2814                 }
2815                 Some(&ExpectedLine::Elision) => {
2816                     // skip any number of elisions in a row.
2817                     while let Some(&&ExpectedLine::Elision) = expected_lines.peek() {
2818                         expected_lines.next();
2819                     }
2820                     if let Some(&ExpectedLine::Text(expected_line)) = expected_lines.next() {
2821                         let mut found = compare(expected_line, dumped_line);
2822                         if found {
2823                             continue;
2824                         }
2825                         while let Some(dumped_line) = dumped_lines.next() {
2826                             found = compare(expected_line, dumped_line);
2827                             if found {
2828                                 break;
2829                             }
2830                         }
2831                         if !found {
2832                             error(expected_line, "ran out of mir dump to match against".into());
2833                         }
2834                     }
2835                 }
2836                 None => {}
2837             }
2838         }
2839     }
2840
2841     fn get_mir_dump_dir(&self) -> PathBuf {
2842         let mut mir_dump_dir = PathBuf::from(self.config.build_base.as_path());
2843         debug!("input_file: {:?}", self.testpaths.file);
2844         mir_dump_dir.push(&self.testpaths.relative_dir);
2845         mir_dump_dir.push(self.testpaths.file.file_stem().unwrap());
2846         mir_dump_dir
2847     }
2848
2849     fn normalize_output(&self, output: &str, custom_rules: &[(String, String)]) -> String {
2850         let parent_dir = self.testpaths.file.parent().unwrap();
2851         let cflags = self.props.compile_flags.join(" ");
2852         let json = cflags.contains("--error-format json")
2853             || cflags.contains("--error-format pretty-json")
2854             || cflags.contains("--error-format=json")
2855             || cflags.contains("--error-format=pretty-json");
2856         let parent_dir_str = if json {
2857             parent_dir.display().to_string().replace("\\", "\\\\")
2858         } else {
2859             parent_dir.display().to_string()
2860         };
2861
2862         let mut normalized = output.replace(&parent_dir_str, "$DIR");
2863
2864         if json {
2865             // escaped newlines in json strings should be readable
2866             // in the stderr files. There's no point int being correct,
2867             // since only humans process the stderr files.
2868             // Thus we just turn escaped newlines back into newlines.
2869             normalized = normalized.replace("\\n", "\n");
2870         }
2871
2872         normalized = normalized.replace("\\\\", "\\") // denormalize for paths on windows
2873               .replace("\\", "/") // normalize for paths on windows
2874               .replace("\r\n", "\n") // normalize for linebreaks on windows
2875               .replace("\t", "\\t"); // makes tabs visible
2876         for rule in custom_rules {
2877             let re = Regex::new(&rule.0).expect("bad regex in custom normalization rule");
2878             normalized = re.replace_all(&normalized, &rule.1[..]).into_owned();
2879         }
2880         normalized
2881     }
2882
2883     fn expected_output_path(&self, kind: &str) -> PathBuf {
2884         let mut path = expected_output_path(
2885             &self.testpaths,
2886             self.revision,
2887             &self.config.compare_mode,
2888             kind,
2889         );
2890         if !path.exists() && self.config.compare_mode.is_some() {
2891             // fallback!
2892             path = expected_output_path(&self.testpaths, self.revision, &None, kind);
2893         }
2894
2895         path
2896     }
2897
2898     fn load_expected_output(&self, kind: &str) -> String {
2899         let path = self.expected_output_path(kind);
2900         if path.exists() {
2901             match self.load_expected_output_from_path(&path) {
2902                 Ok(x) => x,
2903                 Err(x) => self.fatal(&x),
2904             }
2905         } else {
2906             String::new()
2907         }
2908     }
2909
2910     fn load_expected_output_from_path(&self, path: &Path) -> Result<String, String> {
2911         let mut result = String::new();
2912         match File::open(path).and_then(|mut f| f.read_to_string(&mut result)) {
2913             Ok(_) => Ok(result),
2914             Err(e) => Err(format!(
2915                 "failed to load expected output from `{}`: {}",
2916                 path.display(),
2917                 e
2918             )),
2919         }
2920     }
2921
2922     fn compare_output(&self, kind: &str, actual: &str, expected: &str) -> usize {
2923         if actual == expected {
2924             return 0;
2925         }
2926
2927         if !self.config.bless {
2928             if expected.is_empty() {
2929                 println!("normalized {}:\n{}\n", kind, actual);
2930             } else {
2931                 println!("diff of {}:\n", kind);
2932                 let diff_results = make_diff(expected, actual, 3);
2933                 for result in diff_results {
2934                     let mut line_number = result.line_number;
2935                     for line in result.lines {
2936                         match line {
2937                             DiffLine::Expected(e) => {
2938                                 println!("-\t{}", e);
2939                                 line_number += 1;
2940                             }
2941                             DiffLine::Context(c) => {
2942                                 println!("{}\t{}", line_number, c);
2943                                 line_number += 1;
2944                             }
2945                             DiffLine::Resulting(r) => {
2946                                 println!("+\t{}", r);
2947                             }
2948                         }
2949                     }
2950                     println!("");
2951                 }
2952             }
2953         }
2954
2955         let mode = self.config.compare_mode.as_ref().map_or("", |m| m.to_str());
2956         let output_file = self.output_base_name()
2957             .with_extra_extension(self.revision.unwrap_or(""))
2958             .with_extra_extension(mode)
2959             .with_extra_extension(kind);
2960
2961         let mut files = vec![output_file];
2962         if self.config.bless {
2963             files.push(expected_output_path(
2964                 self.testpaths,
2965                 self.revision,
2966                 &self.config.compare_mode,
2967                 kind,
2968             ));
2969         }
2970
2971         for output_file in &files {
2972             if actual.is_empty() {
2973                 if let Err(e) = ::std::fs::remove_file(output_file) {
2974                     self.fatal(&format!(
2975                         "failed to delete `{}`: {}",
2976                         output_file.display(),
2977                         e,
2978                     ));
2979                 }
2980             } else {
2981                 match File::create(&output_file).and_then(|mut f| f.write_all(actual.as_bytes())) {
2982                     Ok(()) => {}
2983                     Err(e) => self.fatal(&format!(
2984                         "failed to write {} to `{}`: {}",
2985                         kind,
2986                         output_file.display(),
2987                         e
2988                     )),
2989                 }
2990             }
2991         }
2992
2993         println!("\nThe actual {0} differed from the expected {0}.", kind);
2994         for output_file in files {
2995             println!("Actual {} saved to {}", kind, output_file.display());
2996         }
2997         if self.config.bless {
2998             0
2999         } else {
3000             1
3001         }
3002     }
3003
3004     fn create_stamp(&self) {
3005         let mut f = File::create(::stamp(&self.config, self.testpaths, self.revision)).unwrap();
3006         f.write_all(compute_stamp_hash(&self.config).as_bytes()).unwrap();
3007     }
3008 }
3009
3010 struct ProcArgs {
3011     prog: String,
3012     args: Vec<String>,
3013 }
3014
3015 pub struct ProcRes {
3016     status: ExitStatus,
3017     stdout: String,
3018     stderr: String,
3019     cmdline: String,
3020 }
3021
3022 impl ProcRes {
3023     pub fn fatal(&self, err: Option<&str>) -> ! {
3024         if let Some(e) = err {
3025             println!("\nerror: {}", e);
3026         }
3027         print!(
3028             "\
3029              status: {}\n\
3030              command: {}\n\
3031              stdout:\n\
3032              ------------------------------------------\n\
3033              {}\n\
3034              ------------------------------------------\n\
3035              stderr:\n\
3036              ------------------------------------------\n\
3037              {}\n\
3038              ------------------------------------------\n\
3039              \n",
3040             self.status, self.cmdline, self.stdout, self.stderr
3041         );
3042         panic!();
3043     }
3044 }
3045
3046 enum TargetLocation {
3047     ThisFile(PathBuf),
3048     ThisDirectory(PathBuf),
3049 }
3050
3051 #[derive(Clone, PartialEq, Eq)]
3052 enum ExpectedLine<T: AsRef<str>> {
3053     Elision,
3054     Text(T),
3055 }
3056
3057 impl<T> fmt::Debug for ExpectedLine<T>
3058 where
3059     T: AsRef<str> + fmt::Debug,
3060 {
3061     fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
3062         if let &ExpectedLine::Text(ref t) = self {
3063             write!(formatter, "{:?}", t)
3064         } else {
3065             write!(formatter, "\"...\" (Elision)")
3066         }
3067     }
3068 }
3069
3070 fn normalize_mir_line(line: &str) -> String {
3071     nocomment_mir_line(line).replace(char::is_whitespace, "")
3072 }
3073
3074 fn nocomment_mir_line(line: &str) -> &str {
3075     if let Some(idx) = line.find("//") {
3076         let (l, _) = line.split_at(idx);
3077         l.trim_right()
3078     } else {
3079         line
3080     }
3081 }
3082
3083 fn read2_abbreviated(mut child: Child) -> io::Result<Output> {
3084     use read2::read2;
3085     use std::mem::replace;
3086
3087     const HEAD_LEN: usize = 160 * 1024;
3088     const TAIL_LEN: usize = 256 * 1024;
3089
3090     enum ProcOutput {
3091         Full(Vec<u8>),
3092         Abbreviated {
3093             head: Vec<u8>,
3094             skipped: usize,
3095             tail: Box<[u8]>,
3096         },
3097     }
3098
3099     impl ProcOutput {
3100         fn extend(&mut self, data: &[u8]) {
3101             let new_self = match *self {
3102                 ProcOutput::Full(ref mut bytes) => {
3103                     bytes.extend_from_slice(data);
3104                     let new_len = bytes.len();
3105                     if new_len <= HEAD_LEN + TAIL_LEN {
3106                         return;
3107                     }
3108                     let tail = bytes.split_off(new_len - TAIL_LEN).into_boxed_slice();
3109                     let head = replace(bytes, Vec::new());
3110                     let skipped = new_len - HEAD_LEN - TAIL_LEN;
3111                     ProcOutput::Abbreviated {
3112                         head,
3113                         skipped,
3114                         tail,
3115                     }
3116                 }
3117                 ProcOutput::Abbreviated {
3118                     ref mut skipped,
3119                     ref mut tail,
3120                     ..
3121                 } => {
3122                     *skipped += data.len();
3123                     if data.len() <= TAIL_LEN {
3124                         tail[..data.len()].copy_from_slice(data);
3125                         tail.rotate_left(data.len());
3126                     } else {
3127                         tail.copy_from_slice(&data[(data.len() - TAIL_LEN)..]);
3128                     }
3129                     return;
3130                 }
3131             };
3132             *self = new_self;
3133         }
3134
3135         fn into_bytes(self) -> Vec<u8> {
3136             match self {
3137                 ProcOutput::Full(bytes) => bytes,
3138                 ProcOutput::Abbreviated {
3139                     mut head,
3140                     skipped,
3141                     tail,
3142                 } => {
3143                     write!(&mut head, "\n\n<<<<<< SKIPPED {} BYTES >>>>>>\n\n", skipped).unwrap();
3144                     head.extend_from_slice(&tail);
3145                     head
3146                 }
3147             }
3148         }
3149     }
3150
3151     let mut stdout = ProcOutput::Full(Vec::new());
3152     let mut stderr = ProcOutput::Full(Vec::new());
3153
3154     drop(child.stdin.take());
3155     read2(
3156         child.stdout.take().unwrap(),
3157         child.stderr.take().unwrap(),
3158         &mut |is_stdout, data, _| {
3159             if is_stdout { &mut stdout } else { &mut stderr }.extend(data);
3160             data.clear();
3161         },
3162     )?;
3163     let status = child.wait()?;
3164
3165     Ok(Output {
3166         status,
3167         stdout: stdout.into_bytes(),
3168         stderr: stderr.into_bytes(),
3169     })
3170 }