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