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