]> git.proxmox.com Git - rustc.git/blame - src/tools/compiletest/src/header.rs
New upstream version 1.40.0+dfsg1
[rustc.git] / src / tools / compiletest / src / header.rs
CommitLineData
a7813a04
XL
1use std::env;
2use std::fs::File;
a7813a04 3use std::io::prelude::*;
94b46f34 4use std::io::BufReader;
a7813a04
XL
5use std::path::{Path, PathBuf};
6
48663c56
XL
7use log::*;
8
dc9dc135 9use crate::common::{self, CompareMode, Config, Mode, PassMode};
9fa01778 10use crate::util;
9fa01778 11use crate::extract_gdb_version;
c30ab7b3 12
416331ca
XL
13#[cfg(test)]
14mod tests;
15
0bf4aa26
XL
16/// Whether to ignore the test.
17#[derive(Clone, Copy, PartialEq, Debug)]
18pub enum Ignore {
9fa01778 19 /// Runs it.
0bf4aa26
XL
20 Run,
21 /// Ignore it totally.
22 Ignore,
23 /// Ignore only the gdb test, but run the lldb test.
24 IgnoreGdb,
25 /// Ignore only the lldb test, but run the gdb test.
26 IgnoreLldb,
27}
28
29impl Ignore {
30 pub fn can_run_gdb(&self) -> bool {
31 *self == Ignore::Run || *self == Ignore::IgnoreLldb
32 }
33
34 pub fn can_run_lldb(&self) -> bool {
35 *self == Ignore::Run || *self == Ignore::IgnoreGdb
36 }
37
38 pub fn no_gdb(&self) -> Ignore {
39 match *self {
40 Ignore::Run => Ignore::IgnoreGdb,
41 Ignore::IgnoreGdb => Ignore::IgnoreGdb,
42 _ => Ignore::Ignore,
43 }
44 }
45
46 pub fn no_lldb(&self) -> Ignore {
47 match *self {
48 Ignore::Run => Ignore::IgnoreLldb,
49 Ignore::IgnoreLldb => Ignore::IgnoreLldb,
50 _ => Ignore::Ignore,
51 }
52 }
53}
54
55/// The result of parse_cfg_name_directive.
56#[derive(Clone, Copy, PartialEq, Debug)]
57enum ParsedNameDirective {
58 /// No match.
59 NoMatch,
60 /// Match.
61 Match,
dc9dc135 62 /// Mode was DebugInfoGdbLldb and this matched gdb.
0bf4aa26 63 MatchGdb,
dc9dc135 64 /// Mode was DebugInfoGdbLldb and this matched lldb.
0bf4aa26
XL
65 MatchLldb,
66}
67
a7813a04
XL
68/// Properties which must be known very early, before actually running
69/// the test.
70pub struct EarlyProps {
0bf4aa26 71 pub ignore: Ignore,
a7813a04 72 pub should_fail: bool,
8bb4bdeb 73 pub aux: Vec<String>,
ff7c6d11 74 pub revisions: Vec<String>,
a7813a04
XL
75}
76
77impl EarlyProps {
78 pub fn from_file(config: &Config, testfile: &Path) -> Self {
79 let mut props = EarlyProps {
0bf4aa26 80 ignore: Ignore::Run,
a7813a04 81 should_fail: false,
8bb4bdeb 82 aux: Vec::new(),
ff7c6d11 83 revisions: vec![],
a7813a04
XL
84 };
85
dc9dc135 86 if config.mode == common::DebugInfoGdbLldb {
0bf4aa26
XL
87 if config.lldb_python_dir.is_none() {
88 props.ignore = props.ignore.no_lldb();
89 }
90 if config.gdb_version.is_none() {
91 props.ignore = props.ignore.no_gdb();
92 }
dc9dc135
XL
93 } else if config.mode == common::DebugInfoCdb {
94 if config.cdb.is_none() {
95 props.ignore = Ignore::Ignore;
96 }
0bf4aa26
XL
97 }
98
48663c56
XL
99 let rustc_has_profiler_support = env::var_os("RUSTC_PROFILER_SUPPORT").is_some();
100 let rustc_has_sanitizer_support = env::var_os("RUSTC_SANITIZER_SUPPORT").is_some();
101
94b46f34 102 iter_header(testfile, None, &mut |ln| {
2c00a5a8
XL
103 // we should check if any only-<platform> exists and if it exists
104 // and does not matches the current platform, skip the test
0bf4aa26
XL
105 if props.ignore != Ignore::Ignore {
106 props.ignore = match config.parse_cfg_name_directive(ln, "ignore") {
107 ParsedNameDirective::Match => Ignore::Ignore,
108 ParsedNameDirective::NoMatch => props.ignore,
109 ParsedNameDirective::MatchGdb => props.ignore.no_gdb(),
110 ParsedNameDirective::MatchLldb => props.ignore.no_lldb(),
111 };
112
113 if config.has_cfg_prefix(ln, "only") {
114 props.ignore = match config.parse_cfg_name_directive(ln, "only") {
115 ParsedNameDirective::Match => props.ignore,
116 ParsedNameDirective::NoMatch => Ignore::Ignore,
117 ParsedNameDirective::MatchLldb => props.ignore.no_gdb(),
118 ParsedNameDirective::MatchGdb => props.ignore.no_lldb(),
119 };
120 }
121
122 if ignore_llvm(config, ln) {
123 props.ignore = Ignore::Ignore;
124 }
9fa01778
XL
125
126 if config.run_clang_based_tests_with.is_none() &&
127 config.parse_needs_matching_clang(ln) {
128 props.ignore = Ignore::Ignore;
129 }
48663c56
XL
130
131 if !rustc_has_profiler_support &&
132 config.parse_needs_profiler_support(ln) {
133 props.ignore = Ignore::Ignore;
134 }
135
136 if !rustc_has_sanitizer_support &&
137 config.parse_needs_sanitizer_support(ln) {
138 props.ignore = Ignore::Ignore;
139 }
e1599b0c
XL
140
141 if config.target == "wasm32-unknown-unknown" && config.parse_check_run_results(ln) {
142 props.ignore = Ignore::Ignore;
143 }
0bf4aa26
XL
144 }
145
dc9dc135 146 if (config.mode == common::DebugInfoGdb || config.mode == common::DebugInfoGdbLldb) &&
0bf4aa26
XL
147 props.ignore.can_run_gdb() && ignore_gdb(config, ln) {
148 props.ignore = props.ignore.no_gdb();
149 }
150
dc9dc135 151 if (config.mode == common::DebugInfoLldb || config.mode == common::DebugInfoGdbLldb) &&
0bf4aa26
XL
152 props.ignore.can_run_lldb() && ignore_lldb(config, ln) {
153 props.ignore = props.ignore.no_lldb();
154 }
a7813a04 155
7cac9316 156 if let Some(s) = config.parse_aux_build(ln) {
8bb4bdeb
XL
157 props.aux.push(s);
158 }
159
ff7c6d11
XL
160 if let Some(r) = config.parse_revisions(ln) {
161 props.revisions.extend(r);
162 }
163
7cac9316 164 props.should_fail = props.should_fail || config.parse_name_directive(ln, "should-fail");
a7813a04
XL
165 });
166
167 return props;
168
a7813a04 169 fn ignore_gdb(config: &Config, line: &str) -> bool {
c30ab7b3 170 if let Some(actual_version) = config.gdb_version {
7cac9316 171 if line.starts_with("min-gdb-version") {
32a655c1
SL
172 let (start_ver, end_ver) = extract_gdb_version_range(line);
173
174 if start_ver != end_ver {
175 panic!("Expected single GDB version")
176 }
a7813a04
XL
177 // Ignore if actual version is smaller the minimum required
178 // version
32a655c1 179 actual_version < start_ver
7cac9316 180 } else if line.starts_with("ignore-gdb-version") {
32a655c1
SL
181 let (min_version, max_version) = extract_gdb_version_range(line);
182
183 if max_version < min_version {
184 panic!("Malformed GDB version range: max < min")
185 }
186
187 actual_version >= min_version && actual_version <= max_version
a7813a04
XL
188 } else {
189 false
190 }
191 } else {
192 false
193 }
194 }
195
32a655c1
SL
196 // Takes a directive of the form "ignore-gdb-version <version1> [- <version2>]",
197 // returns the numeric representation of <version1> and <version2> as
198 // tuple: (<version1> as u32, <version2> as u32)
199 // If the <version2> part is omitted, the second component of the tuple
200 // is the same as <version1>.
201 fn extract_gdb_version_range(line: &str) -> (u32, u32) {
202 const ERROR_MESSAGE: &'static str = "Malformed GDB version directive";
203
7cac9316
XL
204 let range_components = line.split(&[' ', '-'][..])
205 .filter(|word| !word.is_empty())
206 .map(extract_gdb_version)
207 .skip_while(Option::is_none)
208 .take(3) // 3 or more = invalid, so take at most 3.
209 .collect::<Vec<Option<u32>>>();
32a655c1
SL
210
211 match range_components.len() {
212 1 => {
7cac9316 213 let v = range_components[0].unwrap();
32a655c1
SL
214 (v, v)
215 }
216 2 => {
7cac9316
XL
217 let v_min = range_components[0].unwrap();
218 let v_max = range_components[1].expect(ERROR_MESSAGE);
32a655c1
SL
219 (v_min, v_max)
220 }
221 _ => panic!(ERROR_MESSAGE),
222 }
223 }
224
a7813a04 225 fn ignore_lldb(config: &Config, line: &str) -> bool {
a7813a04 226 if let Some(ref actual_version) = config.lldb_version {
7cac9316 227 if line.starts_with("min-lldb-version") {
0731742a 228 let min_version = line.trim_end()
7cac9316
XL
229 .rsplit(' ')
230 .next()
5bcae85e 231 .expect("Malformed lldb version directive");
a7813a04
XL
232 // Ignore if actual version is smaller the minimum required
233 // version
5bcae85e 234 lldb_version_to_int(actual_version) < lldb_version_to_int(min_version)
0bf4aa26
XL
235 } else if line.starts_with("rust-lldb") && !config.lldb_native_rust {
236 true
a7813a04
XL
237 } else {
238 false
239 }
240 } else {
241 false
242 }
243 }
9e0c209e
SL
244
245 fn ignore_llvm(config: &Config, line: &str) -> bool {
041b39d2 246 if config.system_llvm && line.starts_with("no-system-llvm") {
94b46f34 247 return true;
041b39d2 248 }
9e0c209e 249 if let Some(ref actual_version) = config.llvm_version {
7cac9316 250 if line.starts_with("min-llvm-version") {
0731742a 251 let min_version = line.trim_end()
7cac9316
XL
252 .rsplit(' ')
253 .next()
9e0c209e
SL
254 .expect("Malformed llvm version directive");
255 // Ignore if actual version is smaller the minimum required
256 // version
257 &actual_version[..] < min_version
abe05a73 258 } else if line.starts_with("min-system-llvm-version") {
0731742a 259 let min_version = line.trim_end()
abe05a73
XL
260 .rsplit(' ')
261 .next()
262 .expect("Malformed llvm version directive");
263 // Ignore if using system LLVM and actual version
264 // is smaller the minimum required version
2c00a5a8 265 config.system_llvm && &actual_version[..] < min_version
a1dfa0c6
XL
266 } else if line.starts_with("ignore-llvm-version") {
267 // Syntax is: "ignore-llvm-version <version1> [- <version2>]"
268 let range_components = line.split(' ')
269 .skip(1) // Skip the directive.
270 .map(|s| s.trim())
271 .filter(|word| !word.is_empty() && word != &"-")
272 .take(3) // 3 or more = invalid, so take at most 3.
273 .collect::<Vec<&str>>();
274 match range_components.len() {
275 1 => {
276 &actual_version[..] == range_components[0]
277 }
278 2 => {
279 let v_min = range_components[0];
280 let v_max = range_components[1];
281 if v_max < v_min {
282 panic!("Malformed LLVM version range: max < min")
283 }
284 // Ignore if version lies inside of range.
285 &actual_version[..] >= v_min && &actual_version[..] <= v_max
286 }
287 _ => panic!("Malformed LLVM version directive"),
288 }
9e0c209e
SL
289 } else {
290 false
291 }
292 } else {
293 false
294 }
295 }
a7813a04
XL
296 }
297}
298
299#[derive(Clone, Debug)]
300pub struct TestProps {
301 // Lines that should be expected, in order, on standard out
5bcae85e 302 pub error_patterns: Vec<String>,
a7813a04
XL
303 // Extra flags to pass to the compiler
304 pub compile_flags: Vec<String>,
305 // Extra flags to pass when the compiled code is run (such as --bench)
306 pub run_flags: Option<String>,
307 // If present, the name of a file that this test should match when
308 // pretty-printed
309 pub pp_exact: Option<PathBuf>,
310 // Other crates that should be compiled (typically from the same
311 // directory as the test, but for backwards compatibility reasons
312 // we also check the auxiliary directory)
5bcae85e 313 pub aux_builds: Vec<String>,
48663c56
XL
314 // A list of crates to pass '--extern-private name:PATH' flags for
315 // This should be a subset of 'aux_build'
316 // FIXME: Replace this with a better solution: https://github.com/rust-lang/rust/pull/54020
317 pub extern_private: Vec<String>,
a7813a04 318 // Environment settings to use for compiling
5bcae85e 319 pub rustc_env: Vec<(String, String)>,
48663c56
XL
320 // Environment variables to unset prior to compiling.
321 // Variables are unset before applying 'rustc_env'.
322 pub unset_rustc_env: Vec<String>,
a7813a04 323 // Environment settings to use during execution
5bcae85e 324 pub exec_env: Vec<(String, String)>,
a7813a04 325 // Lines to check if they appear in the expected debugger output
5bcae85e 326 pub check_lines: Vec<String>,
a7813a04
XL
327 // Build documentation for all specified aux-builds as well
328 pub build_aux_docs: bool,
329 // Flag to force a crate to be built with the host architecture
330 pub force_host: bool,
331 // Check stdout for error-pattern output as well as stderr
332 pub check_stdout: bool,
e1599b0c
XL
333 // Check stdout & stderr for output of run-pass test
334 pub check_run_results: bool,
0bf4aa26
XL
335 // For UI tests, allows compiler to generate arbitrary output to stdout
336 pub dont_check_compiler_stdout: bool,
337 // For UI tests, allows compiler to generate arbitrary output to stderr
338 pub dont_check_compiler_stderr: bool,
a7813a04 339 // Don't force a --crate-type=dylib flag on the command line
48663c56
XL
340 //
341 // Set this for example if you have an auxiliary test file that contains
342 // a proc-macro and needs `#![crate_type = "proc-macro"]`. This ensures
343 // that the aux file is compiled as a `proc-macro` and not as a `dylib`.
a7813a04
XL
344 pub no_prefer_dynamic: bool,
345 // Run --pretty expanded when running pretty printing tests
346 pub pretty_expanded: bool,
347 // Which pretty mode are we testing with, default to 'normal'
348 pub pretty_mode: String,
349 // Only compare pretty output and don't try compiling
350 pub pretty_compare_only: bool,
351 // Patterns which must not appear in the output of a cfail test.
352 pub forbid_output: Vec<String>,
353 // Revisions to test for incremental compilation.
354 pub revisions: Vec<String>,
355 // Directory (if any) to use for incremental compilation. This is
356 // not set by end-users; rather it is set by the incremental
357 // testing harness and used when generating compilation
358 // arguments. (In particular, it propagates to the aux-builds.)
359 pub incremental_dir: Option<PathBuf>,
dc9dc135
XL
360 // How far should the test proceed while still passing.
361 pass_mode: Option<PassMode>,
362 // Ignore `--pass` overrides from the command line for this test.
363 ignore_pass: bool,
8bb4bdeb
XL
364 // rustdoc will test the output of the `--test` option
365 pub check_test_line_numbers_match: bool,
0531ce1d
XL
366 // Do not pass `-Z ui-testing` to UI tests
367 pub disable_ui_testing_normalization: bool,
041b39d2
XL
368 // customized normalization rules
369 pub normalize_stdout: Vec<(String, String)>,
370 pub normalize_stderr: Vec<(String, String)>,
0531ce1d 371 pub failure_status: i32,
532ac7d7
XL
372 // Whether or not `rustfix` should apply the `CodeSuggestion`s of this test and compile the
373 // resulting Rust code.
83c7162d 374 pub run_rustfix: bool,
532ac7d7 375 // If true, `rustfix` will only apply `MachineApplicable` suggestions.
b7449926 376 pub rustfix_only_machine_applicable: bool,
532ac7d7 377 pub assembly_output: Option<String>,
a7813a04
XL
378}
379
380impl TestProps {
381 pub fn new() -> Self {
a7813a04 382 TestProps {
9e0c209e 383 error_patterns: vec![],
a7813a04 384 compile_flags: vec![],
9e0c209e
SL
385 run_flags: None,
386 pp_exact: None,
387 aux_builds: vec![],
48663c56 388 extern_private: vec![],
a7813a04
XL
389 revisions: vec![],
390 rustc_env: vec![],
48663c56 391 unset_rustc_env: vec![],
9e0c209e
SL
392 exec_env: vec![],
393 check_lines: vec![],
394 build_aux_docs: false,
395 force_host: false,
396 check_stdout: false,
e1599b0c 397 check_run_results: false,
0bf4aa26
XL
398 dont_check_compiler_stdout: false,
399 dont_check_compiler_stderr: false,
9e0c209e
SL
400 no_prefer_dynamic: false,
401 pretty_expanded: false,
041b39d2 402 pretty_mode: "normal".to_string(),
9e0c209e
SL
403 pretty_compare_only: false,
404 forbid_output: vec![],
a7813a04 405 incremental_dir: None,
dc9dc135
XL
406 pass_mode: None,
407 ignore_pass: false,
8bb4bdeb 408 check_test_line_numbers_match: false,
0531ce1d 409 disable_ui_testing_normalization: false,
041b39d2
XL
410 normalize_stdout: vec![],
411 normalize_stderr: vec![],
8faf50e0 412 failure_status: -1,
83c7162d 413 run_rustfix: false,
b7449926 414 rustfix_only_machine_applicable: false,
532ac7d7 415 assembly_output: None,
a7813a04
XL
416 }
417 }
418
94b46f34 419 pub fn from_aux_file(&self, testfile: &Path, cfg: Option<&str>, config: &Config) -> Self {
a7813a04
XL
420 let mut props = TestProps::new();
421
422 // copy over select properties to the aux build:
423 props.incremental_dir = self.incremental_dir.clone();
7cac9316 424 props.load_from(testfile, cfg, config);
a7813a04
XL
425
426 props
427 }
428
abe05a73 429 pub fn from_file(testfile: &Path, cfg: Option<&str>, config: &Config) -> Self {
a7813a04 430 let mut props = TestProps::new();
abe05a73 431 props.load_from(testfile, cfg, config);
a7813a04
XL
432 props
433 }
434
9fa01778 435 /// Loads properties from `testfile` into `props`. If a property is
a7813a04
XL
436 /// tied to a particular revision `foo` (indicated by writing
437 /// `//[foo]`), then the property is ignored unless `cfg` is
438 /// `Some("foo")`.
94b46f34
XL
439 fn load_from(&mut self, testfile: &Path, cfg: Option<&str>, config: &Config) {
440 iter_header(testfile, cfg, &mut |ln| {
7cac9316 441 if let Some(ep) = config.parse_error_pattern(ln) {
a7813a04
XL
442 self.error_patterns.push(ep);
443 }
444
7cac9316 445 if let Some(flags) = config.parse_compile_flags(ln) {
94b46f34
XL
446 self.compile_flags
447 .extend(flags.split_whitespace().map(|s| s.to_owned()));
a7813a04
XL
448 }
449
8faf50e0
XL
450 if let Some(edition) = config.parse_edition(ln) {
451 self.compile_flags.push(format!("--edition={}", edition));
452 }
453
7cac9316 454 if let Some(r) = config.parse_revisions(ln) {
a7813a04
XL
455 self.revisions.extend(r);
456 }
457
458 if self.run_flags.is_none() {
7cac9316 459 self.run_flags = config.parse_run_flags(ln);
a7813a04
XL
460 }
461
462 if self.pp_exact.is_none() {
7cac9316 463 self.pp_exact = config.parse_pp_exact(ln, testfile);
a7813a04
XL
464 }
465
466 if !self.build_aux_docs {
7cac9316 467 self.build_aux_docs = config.parse_build_aux_docs(ln);
a7813a04
XL
468 }
469
470 if !self.force_host {
7cac9316 471 self.force_host = config.parse_force_host(ln);
a7813a04
XL
472 }
473
474 if !self.check_stdout {
7cac9316 475 self.check_stdout = config.parse_check_stdout(ln);
a7813a04
XL
476 }
477
e1599b0c
XL
478 if !self.check_run_results {
479 self.check_run_results = config.parse_check_run_results(ln);
480 }
481
0bf4aa26
XL
482 if !self.dont_check_compiler_stdout {
483 self.dont_check_compiler_stdout = config.parse_dont_check_compiler_stdout(ln);
484 }
485
486 if !self.dont_check_compiler_stderr {
487 self.dont_check_compiler_stderr = config.parse_dont_check_compiler_stderr(ln);
488 }
489
a7813a04 490 if !self.no_prefer_dynamic {
7cac9316 491 self.no_prefer_dynamic = config.parse_no_prefer_dynamic(ln);
a7813a04
XL
492 }
493
494 if !self.pretty_expanded {
7cac9316 495 self.pretty_expanded = config.parse_pretty_expanded(ln);
a7813a04
XL
496 }
497
7cac9316 498 if let Some(m) = config.parse_pretty_mode(ln) {
a7813a04
XL
499 self.pretty_mode = m;
500 }
501
502 if !self.pretty_compare_only {
7cac9316 503 self.pretty_compare_only = config.parse_pretty_compare_only(ln);
a7813a04
XL
504 }
505
7cac9316 506 if let Some(ab) = config.parse_aux_build(ln) {
a7813a04
XL
507 self.aux_builds.push(ab);
508 }
509
48663c56
XL
510 if let Some(ep) = config.parse_extern_private(ln) {
511 self.extern_private.push(ep);
512 }
513
7cac9316 514 if let Some(ee) = config.parse_env(ln, "exec-env") {
a7813a04
XL
515 self.exec_env.push(ee);
516 }
517
7cac9316 518 if let Some(ee) = config.parse_env(ln, "rustc-env") {
a7813a04
XL
519 self.rustc_env.push(ee);
520 }
521
48663c56
XL
522 if let Some(ev) = config.parse_name_value_directive(ln, "unset-rustc-env") {
523 self.unset_rustc_env.push(ev);
524 }
525
7cac9316 526 if let Some(cl) = config.parse_check_line(ln) {
a7813a04
XL
527 self.check_lines.push(cl);
528 }
529
7cac9316 530 if let Some(of) = config.parse_forbid_output(ln) {
a7813a04
XL
531 self.forbid_output.push(of);
532 }
9e0c209e 533
8bb4bdeb 534 if !self.check_test_line_numbers_match {
7cac9316
XL
535 self.check_test_line_numbers_match = config.parse_check_test_line_numbers_match(ln);
536 }
537
dc9dc135 538 self.update_pass_mode(ln, cfg, config);
ff7c6d11 539
dc9dc135
XL
540 if !self.ignore_pass {
541 self.ignore_pass = config.parse_ignore_pass(ln);
8faf50e0 542 }
83c7162d 543
0531ce1d
XL
544 if !self.disable_ui_testing_normalization {
545 self.disable_ui_testing_normalization =
546 config.parse_disable_ui_testing_normalization(ln);
547 }
548
041b39d2
XL
549 if let Some(rule) = config.parse_custom_normalization(ln, "normalize-stdout") {
550 self.normalize_stdout.push(rule);
551 }
552 if let Some(rule) = config.parse_custom_normalization(ln, "normalize-stderr") {
553 self.normalize_stderr.push(rule);
554 }
0531ce1d
XL
555
556 if let Some(code) = config.parse_failure_status(ln) {
557 self.failure_status = code;
558 }
83c7162d
XL
559
560 if !self.run_rustfix {
561 self.run_rustfix = config.parse_run_rustfix(ln);
562 }
b7449926
XL
563
564 if !self.rustfix_only_machine_applicable {
565 self.rustfix_only_machine_applicable =
566 config.parse_rustfix_only_machine_applicable(ln);
567 }
532ac7d7
XL
568
569 if self.assembly_output.is_none() {
570 self.assembly_output = config.parse_assembly_output(ln);
571 }
a7813a04
XL
572 });
573
74d20737
XL
574 if self.failure_status == -1 {
575 self.failure_status = match config.mode {
576 Mode::RunFail => 101,
577 _ => 1,
578 };
579 }
580
041b39d2
XL
581 for key in &["RUST_TEST_NOCAPTURE", "RUST_TEST_THREADS"] {
582 if let Ok(val) = env::var(key) {
583 if self.exec_env.iter().find(|&&(ref x, _)| x == key).is_none() {
584 self.exec_env.push(((*key).to_owned(), val))
5bcae85e 585 }
a7813a04
XL
586 }
587 }
588 }
dc9dc135
XL
589
590 fn update_pass_mode(&mut self, ln: &str, revision: Option<&str>, config: &Config) {
591 let check_no_run = |s| {
592 if config.mode != Mode::Ui && config.mode != Mode::Incremental {
593 panic!("`{}` header is only supported in UI and incremental tests", s);
594 }
595 if config.mode == Mode::Incremental &&
596 !revision.map_or(false, |r| r.starts_with("cfail")) &&
597 !self.revisions.iter().all(|r| r.starts_with("cfail")) {
598 panic!("`{}` header is only supported in `cfail` incremental tests", s);
599 }
600 };
601 let pass_mode = if config.parse_name_directive(ln, "check-pass") {
602 check_no_run("check-pass");
603 Some(PassMode::Check)
604 } else if config.parse_name_directive(ln, "build-pass") {
605 check_no_run("build-pass");
606 Some(PassMode::Build)
dc9dc135 607 } else if config.parse_name_directive(ln, "run-pass") {
416331ca 608 if config.mode != Mode::Ui {
dc9dc135
XL
609 panic!("`run-pass` header is only supported in UI tests")
610 }
611 Some(PassMode::Run)
e74abb32
XL
612 } else if config.parse_name_directive(ln, "run-fail") {
613 if config.mode != Mode::Ui {
614 panic!("`run-fail` header is only supported in UI tests")
615 }
616 Some(PassMode::RunFail)
dc9dc135
XL
617 } else {
618 None
619 };
620 match (self.pass_mode, pass_mode) {
621 (None, Some(_)) => self.pass_mode = pass_mode,
622 (Some(_), Some(_)) => panic!("multiple `*-pass` headers in a single test"),
623 (_, None) => {}
624 }
625 }
626
627 pub fn pass_mode(&self, config: &Config) -> Option<PassMode> {
628 if !self.ignore_pass {
629 if let (mode @ Some(_), Some(_)) = (config.force_pass_mode, self.pass_mode) {
630 return mode;
631 }
632 }
633 self.pass_mode
634 }
e1599b0c
XL
635
636 // does not consider CLI override for pass mode
637 pub fn local_pass_mode(&self) -> Option<PassMode> {
638 self.pass_mode
639 }
a7813a04
XL
640}
641
8faf50e0 642fn iter_header(testfile: &Path, cfg: Option<&str>, it: &mut dyn FnMut(&str)) {
a7813a04 643 if testfile.is_dir() {
5bcae85e 644 return;
a7813a04 645 }
83c7162d
XL
646
647 let comment = if testfile.to_string_lossy().ends_with(".rs") {
648 "//"
649 } else {
650 "#"
651 };
652
9fa01778
XL
653 // FIXME: would be nice to allow some whitespace between comment and brace :)
654 // It took me like 2 days to debug why compile-flags weren’t taken into account for my test :)
83c7162d
XL
655 let comment_with_brace = comment.to_string() + "[";
656
a7813a04
XL
657 let rdr = BufReader::new(File::open(testfile).unwrap());
658 for ln in rdr.lines() {
659 // Assume that any directives will be found before the first
660 // module or function. This doesn't seem to be an optimization
661 // with a warm page cache. Maybe with a cold one.
662 let ln = ln.unwrap();
663 let ln = ln.trim();
664 if ln.starts_with("fn") || ln.starts_with("mod") {
665 return;
83c7162d 666 } else if ln.starts_with(&comment_with_brace) {
a7813a04 667 // A comment like `//[foo]` is specific to revision `foo`
041b39d2 668 if let Some(close_brace) = ln.find(']') {
83c7162d
XL
669 let open_brace = ln.find('[').unwrap();
670 let lncfg = &ln[open_brace + 1 .. close_brace];
a7813a04
XL
671 let matches = match cfg {
672 Some(s) => s == &lncfg[..],
673 None => false,
674 };
675 if matches {
0731742a 676 it(ln[(close_brace + 1)..].trim_start());
a7813a04
XL
677 }
678 } else {
83c7162d
XL
679 panic!("malformed condition directive: expected `{}foo]`, found `{}`",
680 comment_with_brace, ln)
a7813a04 681 }
83c7162d 682 } else if ln.starts_with(comment) {
0731742a 683 it(ln[comment.len() ..].trim_start());
a7813a04
XL
684 }
685 }
686 return;
687}
688
7cac9316 689impl Config {
7cac9316
XL
690 fn parse_error_pattern(&self, line: &str) -> Option<String> {
691 self.parse_name_value_directive(line, "error-pattern")
692 }
a7813a04 693
7cac9316
XL
694 fn parse_forbid_output(&self, line: &str) -> Option<String> {
695 self.parse_name_value_directive(line, "forbid-output")
696 }
a7813a04 697
7cac9316
XL
698 fn parse_aux_build(&self, line: &str) -> Option<String> {
699 self.parse_name_value_directive(line, "aux-build")
532ac7d7 700 .map(|r| r.trim().to_string())
7cac9316 701 }
a7813a04 702
48663c56
XL
703 fn parse_extern_private(&self, line: &str) -> Option<String> {
704 self.parse_name_value_directive(line, "extern-private")
705 }
706
7cac9316
XL
707 fn parse_compile_flags(&self, line: &str) -> Option<String> {
708 self.parse_name_value_directive(line, "compile-flags")
709 }
a7813a04 710
7cac9316
XL
711 fn parse_revisions(&self, line: &str) -> Option<Vec<String>> {
712 self.parse_name_value_directive(line, "revisions")
713 .map(|r| r.split_whitespace().map(|t| t.to_string()).collect())
714 }
a7813a04 715
7cac9316
XL
716 fn parse_run_flags(&self, line: &str) -> Option<String> {
717 self.parse_name_value_directive(line, "run-flags")
718 }
a7813a04 719
7cac9316
XL
720 fn parse_check_line(&self, line: &str) -> Option<String> {
721 self.parse_name_value_directive(line, "check")
722 }
a7813a04 723
7cac9316
XL
724 fn parse_force_host(&self, line: &str) -> bool {
725 self.parse_name_directive(line, "force-host")
726 }
a7813a04 727
7cac9316
XL
728 fn parse_build_aux_docs(&self, line: &str) -> bool {
729 self.parse_name_directive(line, "build-aux-docs")
730 }
a7813a04 731
7cac9316
XL
732 fn parse_check_stdout(&self, line: &str) -> bool {
733 self.parse_name_directive(line, "check-stdout")
734 }
a7813a04 735
e1599b0c
XL
736 fn parse_check_run_results(&self, line: &str) -> bool {
737 self.parse_name_directive(line, "check-run-results")
738 }
739
0bf4aa26
XL
740 fn parse_dont_check_compiler_stdout(&self, line: &str) -> bool {
741 self.parse_name_directive(line, "dont-check-compiler-stdout")
742 }
743
744 fn parse_dont_check_compiler_stderr(&self, line: &str) -> bool {
745 self.parse_name_directive(line, "dont-check-compiler-stderr")
746 }
747
7cac9316
XL
748 fn parse_no_prefer_dynamic(&self, line: &str) -> bool {
749 self.parse_name_directive(line, "no-prefer-dynamic")
750 }
a7813a04 751
7cac9316
XL
752 fn parse_pretty_expanded(&self, line: &str) -> bool {
753 self.parse_name_directive(line, "pretty-expanded")
754 }
a7813a04 755
7cac9316
XL
756 fn parse_pretty_mode(&self, line: &str) -> Option<String> {
757 self.parse_name_value_directive(line, "pretty-mode")
758 }
a7813a04 759
7cac9316
XL
760 fn parse_pretty_compare_only(&self, line: &str) -> bool {
761 self.parse_name_directive(line, "pretty-compare-only")
762 }
9e0c209e 763
0531ce1d
XL
764 fn parse_failure_status(&self, line: &str) -> Option<i32> {
765 match self.parse_name_value_directive(line, "failure-status") {
766 Some(code) => code.trim().parse::<i32>().ok(),
767 _ => None,
768 }
769 }
770
0531ce1d
XL
771 fn parse_disable_ui_testing_normalization(&self, line: &str) -> bool {
772 self.parse_name_directive(line, "disable-ui-testing-normalization")
773 }
774
7cac9316
XL
775 fn parse_check_test_line_numbers_match(&self, line: &str) -> bool {
776 self.parse_name_directive(line, "check-test-line-numbers-match")
777 }
8bb4bdeb 778
dc9dc135
XL
779 fn parse_ignore_pass(&self, line: &str) -> bool {
780 self.parse_name_directive(line, "ignore-pass")
83c7162d
XL
781 }
782
532ac7d7
XL
783 fn parse_assembly_output(&self, line: &str) -> Option<String> {
784 self.parse_name_value_directive(line, "assembly-output")
785 .map(|r| r.trim().to_string())
786 }
787
7cac9316
XL
788 fn parse_env(&self, line: &str, name: &str) -> Option<(String, String)> {
789 self.parse_name_value_directive(line, name).map(|nv| {
790 // nv is either FOO or FOO=BAR
94b46f34 791 let mut strs: Vec<String> = nv.splitn(2, '=').map(str::to_owned).collect();
a7813a04 792
7cac9316 793 match strs.len() {
b7449926 794 1 => (strs.pop().unwrap(), String::new()),
7cac9316
XL
795 2 => {
796 let end = strs.pop().unwrap();
797 (strs.pop().unwrap(), end)
798 }
799 n => panic!("Expected 1 or 2 strings, not {}", n),
5bcae85e 800 }
7cac9316
XL
801 })
802 }
a7813a04 803
7cac9316
XL
804 fn parse_pp_exact(&self, line: &str, testfile: &Path) -> Option<PathBuf> {
805 if let Some(s) = self.parse_name_value_directive(line, "pp-exact") {
806 Some(PathBuf::from(&s))
041b39d2
XL
807 } else if self.parse_name_directive(line, "pp-exact") {
808 testfile.file_name().map(PathBuf::from)
a7813a04 809 } else {
041b39d2
XL
810 None
811 }
812 }
813
814 fn parse_custom_normalization(&self, mut line: &str, prefix: &str) -> Option<(String, String)> {
0bf4aa26 815 if self.parse_cfg_name_directive(line, prefix) == ParsedNameDirective::Match {
0731742a
XL
816 let from = parse_normalization_string(&mut line)?;
817 let to = parse_normalization_string(&mut line)?;
041b39d2
XL
818 Some((from, to))
819 } else {
820 None
821 }
822 }
823
9fa01778
XL
824 fn parse_needs_matching_clang(&self, line: &str) -> bool {
825 self.parse_name_directive(line, "needs-matching-clang")
826 }
827
48663c56
XL
828 fn parse_needs_profiler_support(&self, line: &str) -> bool {
829 self.parse_name_directive(line, "needs-profiler-support")
830 }
831
832 fn parse_needs_sanitizer_support(&self, line: &str) -> bool {
833 self.parse_name_directive(line, "needs-sanitizer-support")
834 }
835
0731742a 836 /// Parses a name-value directive which contains config-specific information, e.g., `ignore-x86`
0bf4aa26
XL
837 /// or `normalize-stderr-32bit`.
838 fn parse_cfg_name_directive(&self, line: &str, prefix: &str) -> ParsedNameDirective {
041b39d2 839 if line.starts_with(prefix) && line.as_bytes().get(prefix.len()) == Some(&b'-') {
94b46f34
XL
840 let name = line[prefix.len() + 1..]
841 .split(&[':', ' '][..])
842 .next()
843 .unwrap();
041b39d2 844
0bf4aa26 845 if name == "test" ||
abe05a73 846 util::matches_os(&self.target, name) || // target
e1599b0c 847 util::matches_env(&self.target, name) || // env
041b39d2
XL
848 name == util::get_arch(&self.target) || // architecture
849 name == util::get_pointer_width(&self.target) || // pointer width
850 name == self.stage_id.split('-').next().unwrap() || // stage
b7449926 851 (self.target != self.host && name == "cross-compile") ||
8faf50e0
XL
852 match self.compare_mode {
853 Some(CompareMode::Nll) => name == "compare-mode-nll",
854 Some(CompareMode::Polonius) => name == "compare-mode-polonius",
855 None => false,
b7449926 856 } ||
0bf4aa26
XL
857 (cfg!(debug_assertions) && name == "debug") {
858 ParsedNameDirective::Match
859 } else {
860 match self.mode {
dc9dc135 861 common::DebugInfoGdbLldb => {
0bf4aa26
XL
862 if name == "gdb" {
863 ParsedNameDirective::MatchGdb
864 } else if name == "lldb" {
865 ParsedNameDirective::MatchLldb
866 } else {
867 ParsedNameDirective::NoMatch
868 }
869 },
dc9dc135
XL
870 common::DebugInfoCdb => if name == "cdb" {
871 ParsedNameDirective::Match
872 } else {
873 ParsedNameDirective::NoMatch
874 },
0bf4aa26
XL
875 common::DebugInfoGdb => if name == "gdb" {
876 ParsedNameDirective::Match
877 } else {
878 ParsedNameDirective::NoMatch
879 },
880 common::DebugInfoLldb => if name == "lldb" {
881 ParsedNameDirective::Match
882 } else {
883 ParsedNameDirective::NoMatch
884 },
885 common::Pretty => if name == "pretty" {
886 ParsedNameDirective::Match
887 } else {
888 ParsedNameDirective::NoMatch
889 },
890 _ => ParsedNameDirective::NoMatch,
891 }
892 }
041b39d2 893 } else {
0bf4aa26 894 ParsedNameDirective::NoMatch
a7813a04
XL
895 }
896 }
a7813a04 897
2c00a5a8
XL
898 fn has_cfg_prefix(&self, line: &str, prefix: &str) -> bool {
899 // returns whether this line contains this prefix or not. For prefix
900 // "ignore", returns true if line says "ignore-x86_64", "ignore-arch",
83c7162d 901 // "ignore-android" etc.
2c00a5a8
XL
902 line.starts_with(prefix) && line.as_bytes().get(prefix.len()) == Some(&b'-')
903 }
904
7cac9316
XL
905 fn parse_name_directive(&self, line: &str, directive: &str) -> bool {
906 // Ensure the directive is a whole word. Do not match "ignore-x86" when
907 // the line says "ignore-x86_64".
908 line.starts_with(directive) && match line.as_bytes().get(directive.len()) {
909 None | Some(&b' ') | Some(&b':') => true,
94b46f34 910 _ => false,
7cac9316
XL
911 }
912 }
a7813a04 913
7cac9316
XL
914 pub fn parse_name_value_directive(&self, line: &str, directive: &str) -> Option<String> {
915 let colon = directive.len();
916 if line.starts_with(directive) && line.as_bytes().get(colon) == Some(&b':') {
94b46f34 917 let value = line[(colon + 1)..].to_owned();
7cac9316
XL
918 debug!("{}: {}", directive, value);
919 Some(expand_variables(value, self))
920 } else {
921 None
922 }
a7813a04 923 }
abe05a73
XL
924
925 pub fn find_rust_src_root(&self) -> Option<PathBuf> {
926 let mut path = self.src_base.clone();
927 let path_postfix = Path::new("src/etc/lldb_batchmode.py");
928
929 while path.pop() {
930 if path.join(&path_postfix).is_file() {
931 return Some(path);
932 }
933 }
934
935 None
936 }
83c7162d
XL
937
938 fn parse_run_rustfix(&self, line: &str) -> bool {
939 self.parse_name_directive(line, "run-rustfix")
940 }
8faf50e0 941
b7449926
XL
942 fn parse_rustfix_only_machine_applicable(&self, line: &str) -> bool {
943 self.parse_name_directive(line, "rustfix-only-machine-applicable")
944 }
945
8faf50e0
XL
946 fn parse_edition(&self, line: &str) -> Option<String> {
947 self.parse_name_value_directive(line, "edition")
948 }
a7813a04
XL
949}
950
a7813a04 951pub fn lldb_version_to_int(version_string: &str) -> isize {
94b46f34
XL
952 let error_string = format!(
953 "Encountered LLDB version string with unexpected format: {}",
954 version_string
955 );
041b39d2 956 version_string.parse().expect(&error_string)
a7813a04 957}
7cac9316
XL
958
959fn expand_variables(mut value: String, config: &Config) -> String {
960 const CWD: &'static str = "{{cwd}}";
961 const SRC_BASE: &'static str = "{{src-base}}";
962 const BUILD_BASE: &'static str = "{{build-base}}";
963
964 if value.contains(CWD) {
965 let cwd = env::current_dir().unwrap();
966 value = value.replace(CWD, &cwd.to_string_lossy());
967 }
968
969 if value.contains(SRC_BASE) {
970 value = value.replace(SRC_BASE, &config.src_base.to_string_lossy());
971 }
972
973 if value.contains(BUILD_BASE) {
974 value = value.replace(BUILD_BASE, &config.build_base.to_string_lossy());
975 }
976
977 value
978}
041b39d2
XL
979
980/// Finds the next quoted string `"..."` in `line`, and extract the content from it. Move the `line`
981/// variable after the end of the quoted string.
982///
983/// # Examples
984///
985/// ```
986/// let mut s = "normalize-stderr-32bit: \"something (32 bits)\" -> \"something ($WORD bits)\".";
987/// let first = parse_normalization_string(&mut s);
988/// assert_eq!(first, Some("something (32 bits)".to_owned()));
989/// assert_eq!(s, " -> \"something ($WORD bits)\".");
990/// ```
991fn parse_normalization_string(line: &mut &str) -> Option<String> {
992 // FIXME support escapes in strings.
0731742a
XL
993 let begin = line.find('"')? + 1;
994 let end = line[begin..].find('"')? + begin;
041b39d2 995 let result = line[begin..end].to_owned();
94b46f34 996 *line = &line[end + 1..];
041b39d2
XL
997 Some(result)
998}