]> git.lizzy.rs Git - rust.git/blob - src/tools/compiletest/src/header.rs
Unignore u128 test for stage 0,1
[rust.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 }
29
30 impl EarlyProps {
31     pub fn from_file(config: &Config, testfile: &Path) -> Self {
32         let mut props = EarlyProps {
33             ignore: false,
34             should_fail: false,
35         };
36
37         iter_header(testfile,
38                     None,
39                     &mut |ln| {
40             props.ignore =
41                 props.ignore || parse_name_directive(ln, "ignore-test") ||
42                 parse_name_directive(ln, &ignore_target(config)) ||
43                 parse_name_directive(ln, &ignore_architecture(config)) ||
44                 parse_name_directive(ln, &ignore_stage(config)) ||
45                 parse_name_directive(ln, &ignore_env(config)) ||
46                 (config.mode == common::Pretty && parse_name_directive(ln, "ignore-pretty")) ||
47                 (config.target != config.host &&
48                  parse_name_directive(ln, "ignore-cross-compile")) ||
49                 ignore_gdb(config, ln) ||
50                 ignore_lldb(config, ln) ||
51                 ignore_llvm(config, ln);
52
53             props.should_fail = props.should_fail || parse_name_directive(ln, "should-fail");
54         });
55
56         return props;
57
58         fn ignore_target(config: &Config) -> String {
59             format!("ignore-{}", util::get_os(&config.target))
60         }
61         fn ignore_architecture(config: &Config) -> String {
62             format!("ignore-{}", util::get_arch(&config.target))
63         }
64         fn ignore_stage(config: &Config) -> String {
65             format!("ignore-{}", config.stage_id.split('-').next().unwrap())
66         }
67         fn ignore_env(config: &Config) -> String {
68             format!("ignore-{}",
69                     util::get_env(&config.target).unwrap_or("<unknown>"))
70         }
71         fn ignore_gdb(config: &Config, line: &str) -> bool {
72             if config.mode != common::DebugInfoGdb {
73                 return false;
74             }
75
76             if !line.contains("ignore-gdb-version") &&
77                parse_name_directive(line, "ignore-gdb") {
78                 return true;
79             }
80
81             if let Some(actual_version) = config.gdb_version {
82                 if line.contains("min-gdb-version") {
83                     let (start_ver, end_ver) = extract_gdb_version_range(line);
84
85                     if start_ver != end_ver {
86                         panic!("Expected single GDB version")
87                     }
88                     // Ignore if actual version is smaller the minimum required
89                     // version
90                     actual_version < start_ver
91                 } else if line.contains("ignore-gdb-version") {
92                     let (min_version, max_version) = extract_gdb_version_range(line);
93
94                     if max_version < min_version {
95                         panic!("Malformed GDB version range: max < min")
96                     }
97
98                     actual_version >= min_version && actual_version <= max_version
99                 } else {
100                     false
101                 }
102             } else {
103                 false
104             }
105         }
106
107         // Takes a directive of the form "ignore-gdb-version <version1> [- <version2>]",
108         // returns the numeric representation of <version1> and <version2> as
109         // tuple: (<version1> as u32, <version2> as u32)
110         // If the <version2> part is omitted, the second component of the tuple
111         // is the same as <version1>.
112         fn extract_gdb_version_range(line: &str) -> (u32, u32) {
113             const ERROR_MESSAGE: &'static str = "Malformed GDB version directive";
114
115             let range_components = line.split(' ')
116                                        .flat_map(|word| word.split('-'))
117                                        .filter(|word| word.len() > 0)
118                                        .skip_while(|word| extract_gdb_version(word).is_none())
119                                        .collect::<Vec<&str>>();
120
121             match range_components.len() {
122                 1 => {
123                     let v = extract_gdb_version(range_components[0]).unwrap();
124                     (v, v)
125                 }
126                 2 => {
127                     let v_min = extract_gdb_version(range_components[0]).unwrap();
128                     let v_max = extract_gdb_version(range_components[1]).expect(ERROR_MESSAGE);
129                     (v_min, v_max)
130                 }
131                 _ => panic!(ERROR_MESSAGE),
132             }
133         }
134
135         fn ignore_lldb(config: &Config, line: &str) -> bool {
136             if config.mode != common::DebugInfoLldb {
137                 return false;
138             }
139
140             if parse_name_directive(line, "ignore-lldb") {
141                 return true;
142             }
143
144             if let Some(ref actual_version) = config.lldb_version {
145                 if line.contains("min-lldb-version") {
146                     let min_version = line.trim()
147                         .split(' ')
148                         .last()
149                         .expect("Malformed lldb version directive");
150                     // Ignore if actual version is smaller the minimum required
151                     // version
152                     lldb_version_to_int(actual_version) < lldb_version_to_int(min_version)
153                 } else {
154                     false
155                 }
156             } else {
157                 false
158             }
159         }
160
161         fn ignore_llvm(config: &Config, line: &str) -> bool {
162             if let Some(ref actual_version) = config.llvm_version {
163                 if line.contains("min-llvm-version") {
164                     let min_version = line.trim()
165                         .split(' ')
166                         .last()
167                         .expect("Malformed llvm version directive");
168                     // Ignore if actual version is smaller the minimum required
169                     // version
170                     &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 cfail 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 }
230
231 impl TestProps {
232     pub fn new() -> Self {
233         TestProps {
234             error_patterns: vec![],
235             compile_flags: vec![],
236             run_flags: None,
237             pp_exact: None,
238             aux_builds: vec![],
239             revisions: vec![],
240             rustc_env: vec![],
241             exec_env: vec![],
242             check_lines: vec![],
243             build_aux_docs: false,
244             force_host: false,
245             check_stdout: false,
246             no_prefer_dynamic: false,
247             pretty_expanded: false,
248             pretty_mode: format!("normal"),
249             pretty_compare_only: false,
250             forbid_output: vec![],
251             incremental_dir: None,
252             must_compile_successfully: false,
253             check_test_line_numbers_match: false,
254         }
255     }
256
257     pub fn from_aux_file(&self, testfile: &Path, cfg: Option<&str>) -> Self {
258         let mut props = TestProps::new();
259
260         // copy over select properties to the aux build:
261         props.incremental_dir = self.incremental_dir.clone();
262         props.load_from(testfile, cfg);
263
264         props
265     }
266
267     pub fn from_file(testfile: &Path) -> Self {
268         let mut props = TestProps::new();
269         props.load_from(testfile, None);
270         props
271     }
272
273     /// Load properties from `testfile` into `props`. If a property is
274     /// tied to a particular revision `foo` (indicated by writing
275     /// `//[foo]`), then the property is ignored unless `cfg` is
276     /// `Some("foo")`.
277     pub fn load_from(&mut self, testfile: &Path, cfg: Option<&str>) {
278         iter_header(testfile,
279                     cfg,
280                     &mut |ln| {
281             if let Some(ep) = parse_error_pattern(ln) {
282                 self.error_patterns.push(ep);
283             }
284
285             if let Some(flags) = parse_compile_flags(ln) {
286                 self.compile_flags.extend(flags.split_whitespace()
287                     .map(|s| s.to_owned()));
288             }
289
290             if let Some(r) = parse_revisions(ln) {
291                 self.revisions.extend(r);
292             }
293
294             if self.run_flags.is_none() {
295                 self.run_flags = parse_run_flags(ln);
296             }
297
298             if self.pp_exact.is_none() {
299                 self.pp_exact = parse_pp_exact(ln, testfile);
300             }
301
302             if !self.build_aux_docs {
303                 self.build_aux_docs = parse_build_aux_docs(ln);
304             }
305
306             if !self.force_host {
307                 self.force_host = parse_force_host(ln);
308             }
309
310             if !self.check_stdout {
311                 self.check_stdout = parse_check_stdout(ln);
312             }
313
314             if !self.no_prefer_dynamic {
315                 self.no_prefer_dynamic = parse_no_prefer_dynamic(ln);
316             }
317
318             if !self.pretty_expanded {
319                 self.pretty_expanded = parse_pretty_expanded(ln);
320             }
321
322             if let Some(m) = parse_pretty_mode(ln) {
323                 self.pretty_mode = m;
324             }
325
326             if !self.pretty_compare_only {
327                 self.pretty_compare_only = parse_pretty_compare_only(ln);
328             }
329
330             if let Some(ab) = parse_aux_build(ln) {
331                 self.aux_builds.push(ab);
332             }
333
334             if let Some(ee) = parse_env(ln, "exec-env") {
335                 self.exec_env.push(ee);
336             }
337
338             if let Some(ee) = parse_env(ln, "rustc-env") {
339                 self.rustc_env.push(ee);
340             }
341
342             if let Some(cl) = parse_check_line(ln) {
343                 self.check_lines.push(cl);
344             }
345
346             if let Some(of) = parse_forbid_output(ln) {
347                 self.forbid_output.push(of);
348             }
349
350             if !self.must_compile_successfully {
351                 self.must_compile_successfully = parse_must_compile_successfully(ln);
352             }
353
354             if !self.check_test_line_numbers_match {
355                 self.check_test_line_numbers_match = parse_check_test_line_numbers_match(ln);
356             }
357         });
358
359         for key in vec!["RUST_TEST_NOCAPTURE", "RUST_TEST_THREADS"] {
360             match env::var(key) {
361                 Ok(val) => {
362                     if self.exec_env.iter().find(|&&(ref x, _)| *x == key).is_none() {
363                         self.exec_env.push((key.to_owned(), val))
364                     }
365                 }
366                 Err(..) => {}
367             }
368         }
369     }
370 }
371
372 fn iter_header(testfile: &Path, cfg: Option<&str>, it: &mut FnMut(&str)) {
373     if testfile.is_dir() {
374         return;
375     }
376     let rdr = BufReader::new(File::open(testfile).unwrap());
377     for ln in rdr.lines() {
378         // Assume that any directives will be found before the first
379         // module or function. This doesn't seem to be an optimization
380         // with a warm page cache. Maybe with a cold one.
381         let ln = ln.unwrap();
382         let ln = ln.trim();
383         if ln.starts_with("fn") || ln.starts_with("mod") {
384             return;
385         } else if ln.starts_with("//[") {
386             // A comment like `//[foo]` is specific to revision `foo`
387             if let Some(close_brace) = ln.find("]") {
388                 let lncfg = &ln[3..close_brace];
389                 let matches = match cfg {
390                     Some(s) => s == &lncfg[..],
391                     None => false,
392                 };
393                 if matches {
394                     it(&ln[close_brace + 1..]);
395                 }
396             } else {
397                 panic!("malformed condition directive: expected `//[foo]`, found `{}`",
398                        ln)
399             }
400         } else if ln.starts_with("//") {
401             it(&ln[2..]);
402         }
403     }
404     return;
405 }
406
407 fn parse_error_pattern(line: &str) -> Option<String> {
408     parse_name_value_directive(line, "error-pattern")
409 }
410
411 fn parse_forbid_output(line: &str) -> Option<String> {
412     parse_name_value_directive(line, "forbid-output")
413 }
414
415 fn parse_aux_build(line: &str) -> Option<String> {
416     parse_name_value_directive(line, "aux-build")
417 }
418
419 fn parse_compile_flags(line: &str) -> Option<String> {
420     parse_name_value_directive(line, "compile-flags")
421 }
422
423 fn parse_revisions(line: &str) -> Option<Vec<String>> {
424     parse_name_value_directive(line, "revisions")
425         .map(|r| r.split_whitespace().map(|t| t.to_string()).collect())
426 }
427
428 fn parse_run_flags(line: &str) -> Option<String> {
429     parse_name_value_directive(line, "run-flags")
430 }
431
432 fn parse_check_line(line: &str) -> Option<String> {
433     parse_name_value_directive(line, "check")
434 }
435
436 fn parse_force_host(line: &str) -> bool {
437     parse_name_directive(line, "force-host")
438 }
439
440 fn parse_build_aux_docs(line: &str) -> bool {
441     parse_name_directive(line, "build-aux-docs")
442 }
443
444 fn parse_check_stdout(line: &str) -> bool {
445     parse_name_directive(line, "check-stdout")
446 }
447
448 fn parse_no_prefer_dynamic(line: &str) -> bool {
449     parse_name_directive(line, "no-prefer-dynamic")
450 }
451
452 fn parse_pretty_expanded(line: &str) -> bool {
453     parse_name_directive(line, "pretty-expanded")
454 }
455
456 fn parse_pretty_mode(line: &str) -> Option<String> {
457     parse_name_value_directive(line, "pretty-mode")
458 }
459
460 fn parse_pretty_compare_only(line: &str) -> bool {
461     parse_name_directive(line, "pretty-compare-only")
462 }
463
464 fn parse_must_compile_successfully(line: &str) -> bool {
465     parse_name_directive(line, "must-compile-successfully")
466 }
467
468 fn parse_check_test_line_numbers_match(line: &str) -> bool {
469     parse_name_directive(line, "check-test-line-numbers-match")
470 }
471
472 fn parse_env(line: &str, name: &str) -> Option<(String, String)> {
473     parse_name_value_directive(line, name).map(|nv| {
474         // nv is either FOO or FOO=BAR
475         let mut strs: Vec<String> = nv.splitn(2, '=')
476             .map(str::to_owned)
477             .collect();
478
479         match strs.len() {
480             1 => (strs.pop().unwrap(), "".to_owned()),
481             2 => {
482                 let end = strs.pop().unwrap();
483                 (strs.pop().unwrap(), end)
484             }
485             n => panic!("Expected 1 or 2 strings, not {}", n),
486         }
487     })
488 }
489
490 fn parse_pp_exact(line: &str, testfile: &Path) -> Option<PathBuf> {
491     if let Some(s) = parse_name_value_directive(line, "pp-exact") {
492         Some(PathBuf::from(&s))
493     } else {
494         if parse_name_directive(line, "pp-exact") {
495             testfile.file_name().map(PathBuf::from)
496         } else {
497             None
498         }
499     }
500 }
501
502 fn parse_name_directive(line: &str, directive: &str) -> bool {
503     // This 'no-' rule is a quick hack to allow pretty-expanded and no-pretty-expanded to coexist
504     line.contains(directive) && !line.contains(&("no-".to_owned() + directive))
505 }
506
507 pub fn parse_name_value_directive(line: &str, directive: &str) -> Option<String> {
508     let keycolon = format!("{}:", directive);
509     if let Some(colon) = line.find(&keycolon) {
510         let value = line[(colon + keycolon.len())..line.len()].to_owned();
511         debug!("{}: {}", directive, value);
512         Some(value)
513     } else {
514         None
515     }
516 }
517
518 pub fn lldb_version_to_int(version_string: &str) -> isize {
519     let error_string = format!("Encountered LLDB version string with unexpected format: {}",
520                                version_string);
521     let error_string = error_string;
522     let major: isize = version_string.parse().ok().expect(&error_string);
523     return major;
524 }