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