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