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