]> git.proxmox.com Git - rustc.git/blame - src/tools/compiletest/src/header.rs
New upstream version 1.28.0+dfsg1
[rustc.git] / src / tools / compiletest / src / header.rs
CommitLineData
a7813a04
XL
1// Copyright 2012-2013 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
11use std::env;
12use std::fs::File;
a7813a04 13use std::io::prelude::*;
94b46f34 14use std::io::BufReader;
a7813a04
XL
15use std::path::{Path, PathBuf};
16
a7813a04 17use common;
74d20737 18use common::{Config, Mode};
a7813a04
XL
19use util;
20
c30ab7b3
SL
21use extract_gdb_version;
22
a7813a04
XL
23/// Properties which must be known very early, before actually running
24/// the test.
25pub struct EarlyProps {
26 pub ignore: bool,
27 pub should_fail: bool,
8bb4bdeb 28 pub aux: Vec<String>,
ff7c6d11 29 pub revisions: Vec<String>,
a7813a04
XL
30}
31
32impl EarlyProps {
33 pub fn from_file(config: &Config, testfile: &Path) -> Self {
34 let mut props = EarlyProps {
35 ignore: false,
36 should_fail: false,
8bb4bdeb 37 aux: Vec::new(),
ff7c6d11 38 revisions: vec![],
a7813a04
XL
39 };
40
94b46f34 41 iter_header(testfile, None, &mut |ln| {
2c00a5a8
XL
42 // we should check if any only-<platform> exists and if it exists
43 // and does not matches the current platform, skip the test
94b46f34
XL
44 props.ignore = props.ignore || config.parse_cfg_name_directive(ln, "ignore")
45 || (config.has_cfg_prefix(ln, "only")
46 && !config.parse_cfg_name_directive(ln, "only"))
47 || ignore_gdb(config, ln) || ignore_lldb(config, ln)
48 || ignore_llvm(config, ln);
a7813a04 49
7cac9316 50 if let Some(s) = config.parse_aux_build(ln) {
8bb4bdeb
XL
51 props.aux.push(s);
52 }
53
ff7c6d11
XL
54 if let Some(r) = config.parse_revisions(ln) {
55 props.revisions.extend(r);
56 }
57
7cac9316 58 props.should_fail = props.should_fail || config.parse_name_directive(ln, "should-fail");
a7813a04
XL
59 });
60
61 return props;
62
a7813a04
XL
63 fn ignore_gdb(config: &Config, line: &str) -> bool {
64 if config.mode != common::DebugInfoGdb {
65 return false;
66 }
67
c30ab7b3 68 if let Some(actual_version) = config.gdb_version {
7cac9316 69 if line.starts_with("min-gdb-version") {
32a655c1
SL
70 let (start_ver, end_ver) = extract_gdb_version_range(line);
71
72 if start_ver != end_ver {
73 panic!("Expected single GDB version")
74 }
a7813a04
XL
75 // Ignore if actual version is smaller the minimum required
76 // version
32a655c1 77 actual_version < start_ver
7cac9316 78 } else if line.starts_with("ignore-gdb-version") {
32a655c1
SL
79 let (min_version, max_version) = extract_gdb_version_range(line);
80
81 if max_version < min_version {
82 panic!("Malformed GDB version range: max < min")
83 }
84
85 actual_version >= min_version && actual_version <= max_version
a7813a04
XL
86 } else {
87 false
88 }
89 } else {
90 false
91 }
92 }
93
32a655c1
SL
94 // Takes a directive of the form "ignore-gdb-version <version1> [- <version2>]",
95 // returns the numeric representation of <version1> and <version2> as
96 // tuple: (<version1> as u32, <version2> as u32)
97 // If the <version2> part is omitted, the second component of the tuple
98 // is the same as <version1>.
99 fn extract_gdb_version_range(line: &str) -> (u32, u32) {
100 const ERROR_MESSAGE: &'static str = "Malformed GDB version directive";
101
7cac9316
XL
102 let range_components = line.split(&[' ', '-'][..])
103 .filter(|word| !word.is_empty())
104 .map(extract_gdb_version)
105 .skip_while(Option::is_none)
106 .take(3) // 3 or more = invalid, so take at most 3.
107 .collect::<Vec<Option<u32>>>();
32a655c1
SL
108
109 match range_components.len() {
110 1 => {
7cac9316 111 let v = range_components[0].unwrap();
32a655c1
SL
112 (v, v)
113 }
114 2 => {
7cac9316
XL
115 let v_min = range_components[0].unwrap();
116 let v_max = range_components[1].expect(ERROR_MESSAGE);
32a655c1
SL
117 (v_min, v_max)
118 }
119 _ => panic!(ERROR_MESSAGE),
120 }
121 }
122
a7813a04
XL
123 fn ignore_lldb(config: &Config, line: &str) -> bool {
124 if config.mode != common::DebugInfoLldb {
125 return false;
126 }
127
a7813a04 128 if let Some(ref actual_version) = config.lldb_version {
7cac9316
XL
129 if line.starts_with("min-lldb-version") {
130 let min_version = line.trim_right()
131 .rsplit(' ')
132 .next()
5bcae85e 133 .expect("Malformed lldb version directive");
a7813a04
XL
134 // Ignore if actual version is smaller the minimum required
135 // version
5bcae85e 136 lldb_version_to_int(actual_version) < lldb_version_to_int(min_version)
a7813a04
XL
137 } else {
138 false
139 }
140 } else {
141 false
142 }
143 }
9e0c209e
SL
144
145 fn ignore_llvm(config: &Config, line: &str) -> bool {
041b39d2 146 if config.system_llvm && line.starts_with("no-system-llvm") {
94b46f34 147 return true;
041b39d2 148 }
9e0c209e 149 if let Some(ref actual_version) = config.llvm_version {
7cac9316
XL
150 if line.starts_with("min-llvm-version") {
151 let min_version = line.trim_right()
152 .rsplit(' ')
153 .next()
9e0c209e
SL
154 .expect("Malformed llvm version directive");
155 // Ignore if actual version is smaller the minimum required
156 // version
157 &actual_version[..] < min_version
abe05a73
XL
158 } else if line.starts_with("min-system-llvm-version") {
159 let min_version = line.trim_right()
160 .rsplit(' ')
161 .next()
162 .expect("Malformed llvm version directive");
163 // Ignore if using system LLVM and actual version
164 // is smaller the minimum required version
2c00a5a8 165 config.system_llvm && &actual_version[..] < min_version
9e0c209e
SL
166 } else {
167 false
168 }
169 } else {
170 false
171 }
172 }
a7813a04
XL
173 }
174}
175
176#[derive(Clone, Debug)]
177pub struct TestProps {
178 // Lines that should be expected, in order, on standard out
5bcae85e 179 pub error_patterns: Vec<String>,
a7813a04
XL
180 // Extra flags to pass to the compiler
181 pub compile_flags: Vec<String>,
182 // Extra flags to pass when the compiled code is run (such as --bench)
183 pub run_flags: Option<String>,
184 // If present, the name of a file that this test should match when
185 // pretty-printed
186 pub pp_exact: Option<PathBuf>,
187 // Other crates that should be compiled (typically from the same
188 // directory as the test, but for backwards compatibility reasons
189 // we also check the auxiliary directory)
5bcae85e 190 pub aux_builds: Vec<String>,
a7813a04 191 // Environment settings to use for compiling
5bcae85e 192 pub rustc_env: Vec<(String, String)>,
a7813a04 193 // Environment settings to use during execution
5bcae85e 194 pub exec_env: Vec<(String, String)>,
a7813a04 195 // Lines to check if they appear in the expected debugger output
5bcae85e 196 pub check_lines: Vec<String>,
a7813a04
XL
197 // Build documentation for all specified aux-builds as well
198 pub build_aux_docs: bool,
199 // Flag to force a crate to be built with the host architecture
200 pub force_host: bool,
201 // Check stdout for error-pattern output as well as stderr
202 pub check_stdout: bool,
203 // Don't force a --crate-type=dylib flag on the command line
204 pub no_prefer_dynamic: bool,
205 // Run --pretty expanded when running pretty printing tests
206 pub pretty_expanded: bool,
207 // Which pretty mode are we testing with, default to 'normal'
208 pub pretty_mode: String,
209 // Only compare pretty output and don't try compiling
210 pub pretty_compare_only: bool,
211 // Patterns which must not appear in the output of a cfail test.
212 pub forbid_output: Vec<String>,
213 // Revisions to test for incremental compilation.
214 pub revisions: Vec<String>,
215 // Directory (if any) to use for incremental compilation. This is
216 // not set by end-users; rather it is set by the incremental
217 // testing harness and used when generating compilation
218 // arguments. (In particular, it propagates to the aux-builds.)
219 pub incremental_dir: Option<PathBuf>,
ff7c6d11 220 // Specifies that a test must actually compile without errors.
83c7162d 221 pub compile_pass: bool,
8bb4bdeb
XL
222 // rustdoc will test the output of the `--test` option
223 pub check_test_line_numbers_match: bool,
0531ce1d 224 // The test must be compiled and run successfully. Only used in UI tests for now.
7cac9316 225 pub run_pass: bool,
83c7162d 226 // Skip any codegen step and running the executable. Only for run-pass.
94b46f34 227 pub skip_codegen: bool,
0531ce1d
XL
228 // Do not pass `-Z ui-testing` to UI tests
229 pub disable_ui_testing_normalization: bool,
041b39d2
XL
230 // customized normalization rules
231 pub normalize_stdout: Vec<(String, String)>,
232 pub normalize_stderr: Vec<(String, String)>,
0531ce1d 233 pub failure_status: i32,
83c7162d 234 pub run_rustfix: bool,
a7813a04
XL
235}
236
237impl TestProps {
238 pub fn new() -> Self {
a7813a04 239 TestProps {
9e0c209e 240 error_patterns: vec![],
a7813a04 241 compile_flags: vec![],
9e0c209e
SL
242 run_flags: None,
243 pp_exact: None,
244 aux_builds: vec![],
a7813a04
XL
245 revisions: vec![],
246 rustc_env: vec![],
9e0c209e
SL
247 exec_env: vec![],
248 check_lines: vec![],
249 build_aux_docs: false,
250 force_host: false,
251 check_stdout: false,
252 no_prefer_dynamic: false,
253 pretty_expanded: false,
041b39d2 254 pretty_mode: "normal".to_string(),
9e0c209e
SL
255 pretty_compare_only: false,
256 forbid_output: vec![],
a7813a04 257 incremental_dir: None,
83c7162d 258 compile_pass: false,
8bb4bdeb 259 check_test_line_numbers_match: false,
7cac9316 260 run_pass: false,
94b46f34 261 skip_codegen: false,
0531ce1d 262 disable_ui_testing_normalization: false,
041b39d2
XL
263 normalize_stdout: vec![],
264 normalize_stderr: vec![],
0531ce1d 265 failure_status: 101,
83c7162d 266 run_rustfix: false,
a7813a04
XL
267 }
268 }
269
94b46f34 270 pub fn from_aux_file(&self, testfile: &Path, cfg: Option<&str>, config: &Config) -> Self {
a7813a04
XL
271 let mut props = TestProps::new();
272
273 // copy over select properties to the aux build:
274 props.incremental_dir = self.incremental_dir.clone();
7cac9316 275 props.load_from(testfile, cfg, config);
a7813a04
XL
276
277 props
278 }
279
abe05a73 280 pub fn from_file(testfile: &Path, cfg: Option<&str>, config: &Config) -> Self {
a7813a04 281 let mut props = TestProps::new();
abe05a73 282 props.load_from(testfile, cfg, config);
a7813a04
XL
283 props
284 }
285
286 /// Load properties from `testfile` into `props`. If a property is
287 /// tied to a particular revision `foo` (indicated by writing
288 /// `//[foo]`), then the property is ignored unless `cfg` is
289 /// `Some("foo")`.
94b46f34
XL
290 fn load_from(&mut self, testfile: &Path, cfg: Option<&str>, config: &Config) {
291 iter_header(testfile, cfg, &mut |ln| {
7cac9316 292 if let Some(ep) = config.parse_error_pattern(ln) {
a7813a04
XL
293 self.error_patterns.push(ep);
294 }
295
7cac9316 296 if let Some(flags) = config.parse_compile_flags(ln) {
94b46f34
XL
297 self.compile_flags
298 .extend(flags.split_whitespace().map(|s| s.to_owned()));
a7813a04
XL
299 }
300
7cac9316 301 if let Some(r) = config.parse_revisions(ln) {
a7813a04
XL
302 self.revisions.extend(r);
303 }
304
305 if self.run_flags.is_none() {
7cac9316 306 self.run_flags = config.parse_run_flags(ln);
a7813a04
XL
307 }
308
309 if self.pp_exact.is_none() {
7cac9316 310 self.pp_exact = config.parse_pp_exact(ln, testfile);
a7813a04
XL
311 }
312
313 if !self.build_aux_docs {
7cac9316 314 self.build_aux_docs = config.parse_build_aux_docs(ln);
a7813a04
XL
315 }
316
317 if !self.force_host {
7cac9316 318 self.force_host = config.parse_force_host(ln);
a7813a04
XL
319 }
320
321 if !self.check_stdout {
7cac9316 322 self.check_stdout = config.parse_check_stdout(ln);
a7813a04
XL
323 }
324
325 if !self.no_prefer_dynamic {
7cac9316 326 self.no_prefer_dynamic = config.parse_no_prefer_dynamic(ln);
a7813a04
XL
327 }
328
329 if !self.pretty_expanded {
7cac9316 330 self.pretty_expanded = config.parse_pretty_expanded(ln);
a7813a04
XL
331 }
332
7cac9316 333 if let Some(m) = config.parse_pretty_mode(ln) {
a7813a04
XL
334 self.pretty_mode = m;
335 }
336
337 if !self.pretty_compare_only {
7cac9316 338 self.pretty_compare_only = config.parse_pretty_compare_only(ln);
a7813a04
XL
339 }
340
7cac9316 341 if let Some(ab) = config.parse_aux_build(ln) {
a7813a04
XL
342 self.aux_builds.push(ab);
343 }
344
7cac9316 345 if let Some(ee) = config.parse_env(ln, "exec-env") {
a7813a04
XL
346 self.exec_env.push(ee);
347 }
348
7cac9316 349 if let Some(ee) = config.parse_env(ln, "rustc-env") {
a7813a04
XL
350 self.rustc_env.push(ee);
351 }
352
7cac9316 353 if let Some(cl) = config.parse_check_line(ln) {
a7813a04
XL
354 self.check_lines.push(cl);
355 }
356
7cac9316 357 if let Some(of) = config.parse_forbid_output(ln) {
a7813a04
XL
358 self.forbid_output.push(of);
359 }
9e0c209e 360
8bb4bdeb 361 if !self.check_test_line_numbers_match {
7cac9316
XL
362 self.check_test_line_numbers_match = config.parse_check_test_line_numbers_match(ln);
363 }
364
365 if !self.run_pass {
366 self.run_pass = config.parse_run_pass(ln);
8bb4bdeb 367 }
041b39d2 368
83c7162d 369 if !self.compile_pass {
ff7c6d11 370 // run-pass implies must_compile_sucessfully
94b46f34 371 self.compile_pass = config.parse_compile_pass(ln) || self.run_pass;
ff7c6d11
XL
372 }
373
94b46f34
XL
374 if !self.skip_codegen {
375 self.skip_codegen = config.parse_skip_codegen(ln);
83c7162d
XL
376 }
377
0531ce1d
XL
378 if !self.disable_ui_testing_normalization {
379 self.disable_ui_testing_normalization =
380 config.parse_disable_ui_testing_normalization(ln);
381 }
382
041b39d2
XL
383 if let Some(rule) = config.parse_custom_normalization(ln, "normalize-stdout") {
384 self.normalize_stdout.push(rule);
385 }
386 if let Some(rule) = config.parse_custom_normalization(ln, "normalize-stderr") {
387 self.normalize_stderr.push(rule);
388 }
0531ce1d
XL
389
390 if let Some(code) = config.parse_failure_status(ln) {
391 self.failure_status = code;
392 }
83c7162d
XL
393
394 if !self.run_rustfix {
395 self.run_rustfix = config.parse_run_rustfix(ln);
396 }
a7813a04
XL
397 });
398
74d20737
XL
399 if self.failure_status == -1 {
400 self.failure_status = match config.mode {
401 Mode::RunFail => 101,
402 _ => 1,
403 };
404 }
405
041b39d2
XL
406 for key in &["RUST_TEST_NOCAPTURE", "RUST_TEST_THREADS"] {
407 if let Ok(val) = env::var(key) {
408 if self.exec_env.iter().find(|&&(ref x, _)| x == key).is_none() {
409 self.exec_env.push(((*key).to_owned(), val))
5bcae85e 410 }
a7813a04
XL
411 }
412 }
413 }
414}
415
5bcae85e 416fn iter_header(testfile: &Path, cfg: Option<&str>, it: &mut FnMut(&str)) {
a7813a04 417 if testfile.is_dir() {
5bcae85e 418 return;
a7813a04 419 }
83c7162d
XL
420
421 let comment = if testfile.to_string_lossy().ends_with(".rs") {
422 "//"
423 } else {
424 "#"
425 };
426
427 let comment_with_brace = comment.to_string() + "[";
428
a7813a04
XL
429 let rdr = BufReader::new(File::open(testfile).unwrap());
430 for ln in rdr.lines() {
431 // Assume that any directives will be found before the first
432 // module or function. This doesn't seem to be an optimization
433 // with a warm page cache. Maybe with a cold one.
434 let ln = ln.unwrap();
435 let ln = ln.trim();
436 if ln.starts_with("fn") || ln.starts_with("mod") {
437 return;
83c7162d 438 } else if ln.starts_with(&comment_with_brace) {
a7813a04 439 // A comment like `//[foo]` is specific to revision `foo`
041b39d2 440 if let Some(close_brace) = ln.find(']') {
83c7162d
XL
441 let open_brace = ln.find('[').unwrap();
442 let lncfg = &ln[open_brace + 1 .. close_brace];
a7813a04
XL
443 let matches = match cfg {
444 Some(s) => s == &lncfg[..],
445 None => false,
446 };
447 if matches {
94b46f34 448 it(ln[(close_brace + 1)..].trim_left());
a7813a04
XL
449 }
450 } else {
83c7162d
XL
451 panic!("malformed condition directive: expected `{}foo]`, found `{}`",
452 comment_with_brace, ln)
a7813a04 453 }
83c7162d
XL
454 } else if ln.starts_with(comment) {
455 it(ln[comment.len() ..].trim_left());
a7813a04
XL
456 }
457 }
458 return;
459}
460
7cac9316 461impl Config {
7cac9316
XL
462 fn parse_error_pattern(&self, line: &str) -> Option<String> {
463 self.parse_name_value_directive(line, "error-pattern")
464 }
a7813a04 465
7cac9316
XL
466 fn parse_forbid_output(&self, line: &str) -> Option<String> {
467 self.parse_name_value_directive(line, "forbid-output")
468 }
a7813a04 469
7cac9316
XL
470 fn parse_aux_build(&self, line: &str) -> Option<String> {
471 self.parse_name_value_directive(line, "aux-build")
472 }
a7813a04 473
7cac9316
XL
474 fn parse_compile_flags(&self, line: &str) -> Option<String> {
475 self.parse_name_value_directive(line, "compile-flags")
476 }
a7813a04 477
7cac9316
XL
478 fn parse_revisions(&self, line: &str) -> Option<Vec<String>> {
479 self.parse_name_value_directive(line, "revisions")
480 .map(|r| r.split_whitespace().map(|t| t.to_string()).collect())
481 }
a7813a04 482
7cac9316
XL
483 fn parse_run_flags(&self, line: &str) -> Option<String> {
484 self.parse_name_value_directive(line, "run-flags")
485 }
a7813a04 486
7cac9316
XL
487 fn parse_check_line(&self, line: &str) -> Option<String> {
488 self.parse_name_value_directive(line, "check")
489 }
a7813a04 490
7cac9316
XL
491 fn parse_force_host(&self, line: &str) -> bool {
492 self.parse_name_directive(line, "force-host")
493 }
a7813a04 494
7cac9316
XL
495 fn parse_build_aux_docs(&self, line: &str) -> bool {
496 self.parse_name_directive(line, "build-aux-docs")
497 }
a7813a04 498
7cac9316
XL
499 fn parse_check_stdout(&self, line: &str) -> bool {
500 self.parse_name_directive(line, "check-stdout")
501 }
a7813a04 502
7cac9316
XL
503 fn parse_no_prefer_dynamic(&self, line: &str) -> bool {
504 self.parse_name_directive(line, "no-prefer-dynamic")
505 }
a7813a04 506
7cac9316
XL
507 fn parse_pretty_expanded(&self, line: &str) -> bool {
508 self.parse_name_directive(line, "pretty-expanded")
509 }
a7813a04 510
7cac9316
XL
511 fn parse_pretty_mode(&self, line: &str) -> Option<String> {
512 self.parse_name_value_directive(line, "pretty-mode")
513 }
a7813a04 514
7cac9316
XL
515 fn parse_pretty_compare_only(&self, line: &str) -> bool {
516 self.parse_name_directive(line, "pretty-compare-only")
517 }
9e0c209e 518
0531ce1d
XL
519 fn parse_failure_status(&self, line: &str) -> Option<i32> {
520 match self.parse_name_value_directive(line, "failure-status") {
521 Some(code) => code.trim().parse::<i32>().ok(),
522 _ => None,
523 }
524 }
525
83c7162d
XL
526 fn parse_compile_pass(&self, line: &str) -> bool {
527 self.parse_name_directive(line, "compile-pass")
7cac9316
XL
528 }
529
0531ce1d
XL
530 fn parse_disable_ui_testing_normalization(&self, line: &str) -> bool {
531 self.parse_name_directive(line, "disable-ui-testing-normalization")
532 }
533
7cac9316
XL
534 fn parse_check_test_line_numbers_match(&self, line: &str) -> bool {
535 self.parse_name_directive(line, "check-test-line-numbers-match")
536 }
8bb4bdeb 537
7cac9316
XL
538 fn parse_run_pass(&self, line: &str) -> bool {
539 self.parse_name_directive(line, "run-pass")
540 }
541
94b46f34
XL
542 fn parse_skip_codegen(&self, line: &str) -> bool {
543 self.parse_name_directive(line, "skip-codegen")
83c7162d
XL
544 }
545
7cac9316
XL
546 fn parse_env(&self, line: &str, name: &str) -> Option<(String, String)> {
547 self.parse_name_value_directive(line, name).map(|nv| {
548 // nv is either FOO or FOO=BAR
94b46f34 549 let mut strs: Vec<String> = nv.splitn(2, '=').map(str::to_owned).collect();
a7813a04 550
7cac9316
XL
551 match strs.len() {
552 1 => (strs.pop().unwrap(), "".to_owned()),
553 2 => {
554 let end = strs.pop().unwrap();
555 (strs.pop().unwrap(), end)
556 }
557 n => panic!("Expected 1 or 2 strings, not {}", n),
5bcae85e 558 }
7cac9316
XL
559 })
560 }
a7813a04 561
7cac9316
XL
562 fn parse_pp_exact(&self, line: &str, testfile: &Path) -> Option<PathBuf> {
563 if let Some(s) = self.parse_name_value_directive(line, "pp-exact") {
564 Some(PathBuf::from(&s))
041b39d2
XL
565 } else if self.parse_name_directive(line, "pp-exact") {
566 testfile.file_name().map(PathBuf::from)
a7813a04 567 } else {
041b39d2
XL
568 None
569 }
570 }
571
572 fn parse_custom_normalization(&self, mut line: &str, prefix: &str) -> Option<(String, String)> {
573 if self.parse_cfg_name_directive(line, prefix) {
574 let from = match parse_normalization_string(&mut line) {
575 Some(s) => s,
576 None => return None,
577 };
578 let to = match parse_normalization_string(&mut line) {
579 Some(s) => s,
580 None => return None,
581 };
582 Some((from, to))
583 } else {
584 None
585 }
586 }
587
588 /// Parses a name-value directive which contains config-specific information, e.g. `ignore-x86`
589 /// or `normalize-stderr-32bit`. Returns `true` if the line matches it.
590 fn parse_cfg_name_directive(&self, line: &str, prefix: &str) -> bool {
591 if line.starts_with(prefix) && line.as_bytes().get(prefix.len()) == Some(&b'-') {
94b46f34
XL
592 let name = line[prefix.len() + 1..]
593 .split(&[':', ' '][..])
594 .next()
595 .unwrap();
041b39d2
XL
596
597 name == "test" ||
abe05a73 598 util::matches_os(&self.target, name) || // target
041b39d2
XL
599 name == util::get_arch(&self.target) || // architecture
600 name == util::get_pointer_width(&self.target) || // pointer width
601 name == self.stage_id.split('-').next().unwrap() || // stage
602 Some(name) == util::get_env(&self.target) || // env
603 match self.mode {
604 common::DebugInfoGdb => name == "gdb",
605 common::DebugInfoLldb => name == "lldb",
606 common::Pretty => name == "pretty",
607 _ => false,
94b46f34 608 } || (self.target != self.host && name == "cross-compile")
041b39d2
XL
609 } else {
610 false
a7813a04
XL
611 }
612 }
a7813a04 613
2c00a5a8
XL
614 fn has_cfg_prefix(&self, line: &str, prefix: &str) -> bool {
615 // returns whether this line contains this prefix or not. For prefix
616 // "ignore", returns true if line says "ignore-x86_64", "ignore-arch",
83c7162d 617 // "ignore-android" etc.
2c00a5a8
XL
618 line.starts_with(prefix) && line.as_bytes().get(prefix.len()) == Some(&b'-')
619 }
620
7cac9316
XL
621 fn parse_name_directive(&self, line: &str, directive: &str) -> bool {
622 // Ensure the directive is a whole word. Do not match "ignore-x86" when
623 // the line says "ignore-x86_64".
624 line.starts_with(directive) && match line.as_bytes().get(directive.len()) {
625 None | Some(&b' ') | Some(&b':') => true,
94b46f34 626 _ => false,
7cac9316
XL
627 }
628 }
a7813a04 629
7cac9316
XL
630 pub fn parse_name_value_directive(&self, line: &str, directive: &str) -> Option<String> {
631 let colon = directive.len();
632 if line.starts_with(directive) && line.as_bytes().get(colon) == Some(&b':') {
94b46f34 633 let value = line[(colon + 1)..].to_owned();
7cac9316
XL
634 debug!("{}: {}", directive, value);
635 Some(expand_variables(value, self))
636 } else {
637 None
638 }
a7813a04 639 }
abe05a73
XL
640
641 pub fn find_rust_src_root(&self) -> Option<PathBuf> {
642 let mut path = self.src_base.clone();
643 let path_postfix = Path::new("src/etc/lldb_batchmode.py");
644
645 while path.pop() {
646 if path.join(&path_postfix).is_file() {
647 return Some(path);
648 }
649 }
650
651 None
652 }
83c7162d
XL
653
654 fn parse_run_rustfix(&self, line: &str) -> bool {
655 self.parse_name_directive(line, "run-rustfix")
656 }
a7813a04
XL
657}
658
a7813a04 659pub fn lldb_version_to_int(version_string: &str) -> isize {
94b46f34
XL
660 let error_string = format!(
661 "Encountered LLDB version string with unexpected format: {}",
662 version_string
663 );
041b39d2 664 version_string.parse().expect(&error_string)
a7813a04 665}
7cac9316
XL
666
667fn expand_variables(mut value: String, config: &Config) -> String {
668 const CWD: &'static str = "{{cwd}}";
669 const SRC_BASE: &'static str = "{{src-base}}";
670 const BUILD_BASE: &'static str = "{{build-base}}";
671
672 if value.contains(CWD) {
673 let cwd = env::current_dir().unwrap();
674 value = value.replace(CWD, &cwd.to_string_lossy());
675 }
676
677 if value.contains(SRC_BASE) {
678 value = value.replace(SRC_BASE, &config.src_base.to_string_lossy());
679 }
680
681 if value.contains(BUILD_BASE) {
682 value = value.replace(BUILD_BASE, &config.build_base.to_string_lossy());
683 }
684
685 value
686}
041b39d2
XL
687
688/// Finds the next quoted string `"..."` in `line`, and extract the content from it. Move the `line`
689/// variable after the end of the quoted string.
690///
691/// # Examples
692///
693/// ```
694/// let mut s = "normalize-stderr-32bit: \"something (32 bits)\" -> \"something ($WORD bits)\".";
695/// let first = parse_normalization_string(&mut s);
696/// assert_eq!(first, Some("something (32 bits)".to_owned()));
697/// assert_eq!(s, " -> \"something ($WORD bits)\".");
698/// ```
699fn parse_normalization_string(line: &mut &str) -> Option<String> {
700 // FIXME support escapes in strings.
701 let begin = match line.find('"') {
702 Some(i) => i + 1,
703 None => return None,
704 };
705 let end = match line[begin..].find('"') {
706 Some(i) => i + begin,
707 None => return None,
708 };
709 let result = line[begin..end].to_owned();
94b46f34 710 *line = &line[end + 1..];
041b39d2
XL
711 Some(result)
712}