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