]> git.lizzy.rs Git - rust.git/blob - src/compiletest/header.rs
Do not use entropy during gen_weighted_bool(1)
[rust.git] / src / compiletest / 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 common::Config;
12 use common;
13 use util;
14
15 pub struct TestProps {
16     // Lines that should be expected, in order, on standard out
17     pub error_patterns: Vec<String> ,
18     // Extra flags to pass to the compiler
19     pub compile_flags: Option<String>,
20     // Extra flags to pass when the compiled code is run (such as --bench)
21     pub run_flags: Option<String>,
22     // If present, the name of a file that this test should match when
23     // pretty-printed
24     pub pp_exact: Option<Path>,
25     // Modules from aux directory that should be compiled
26     pub aux_builds: Vec<String> ,
27     // Environment settings to use during execution
28     pub exec_env: Vec<(String,String)> ,
29     // Lines to check if they appear in the expected debugger output
30     pub check_lines: Vec<String> ,
31     // Flag to force a crate to be built with the host architecture
32     pub force_host: bool,
33     // Check stdout for error-pattern output as well as stderr
34     pub check_stdout: bool,
35     // Don't force a --crate-type=dylib flag on the command line
36     pub no_prefer_dynamic: bool,
37     // Don't run --pretty expanded when running pretty printing tests
38     pub no_pretty_expanded: bool,
39     // Which pretty mode are we testing with, default to 'normal'
40     pub pretty_mode: String,
41     // Only compare pretty output and don't try compiling
42     pub pretty_compare_only: bool,
43     // Patterns which must not appear in the output of a cfail test.
44     pub forbid_output: Vec<String>,
45 }
46
47 // Load any test directives embedded in the file
48 pub fn load_props(testfile: &Path) -> TestProps {
49     let mut error_patterns = Vec::new();
50     let mut aux_builds = Vec::new();
51     let mut exec_env = Vec::new();
52     let mut compile_flags = None;
53     let mut run_flags = None;
54     let mut pp_exact = None;
55     let mut check_lines = Vec::new();
56     let mut force_host = false;
57     let mut check_stdout = false;
58     let mut no_prefer_dynamic = false;
59     let mut no_pretty_expanded = false;
60     let mut pretty_mode = None;
61     let mut pretty_compare_only = false;
62     let mut forbid_output = Vec::new();
63     iter_header(testfile, |ln| {
64         match parse_error_pattern(ln) {
65           Some(ep) => error_patterns.push(ep),
66           None => ()
67         };
68
69         if compile_flags.is_none() {
70             compile_flags = parse_compile_flags(ln);
71         }
72
73         if run_flags.is_none() {
74             run_flags = parse_run_flags(ln);
75         }
76
77         if pp_exact.is_none() {
78             pp_exact = parse_pp_exact(ln, testfile);
79         }
80
81         if !force_host {
82             force_host = parse_force_host(ln);
83         }
84
85         if !check_stdout {
86             check_stdout = parse_check_stdout(ln);
87         }
88
89         if !no_prefer_dynamic {
90             no_prefer_dynamic = parse_no_prefer_dynamic(ln);
91         }
92
93         if !no_pretty_expanded {
94             no_pretty_expanded = parse_no_pretty_expanded(ln);
95         }
96
97         if pretty_mode.is_none() {
98             pretty_mode = parse_pretty_mode(ln);
99         }
100
101         if !pretty_compare_only {
102             pretty_compare_only = parse_pretty_compare_only(ln);
103         }
104
105         match parse_aux_build(ln) {
106             Some(ab) => { aux_builds.push(ab); }
107             None => {}
108         }
109
110         match parse_exec_env(ln) {
111             Some(ee) => { exec_env.push(ee); }
112             None => {}
113         }
114
115         match parse_check_line(ln) {
116             Some(cl) => check_lines.push(cl),
117             None => ()
118         };
119
120         match parse_forbid_output(ln) {
121             Some(of) => forbid_output.push(of),
122             None => (),
123         }
124
125         true
126     });
127
128     TestProps {
129         error_patterns: error_patterns,
130         compile_flags: compile_flags,
131         run_flags: run_flags,
132         pp_exact: pp_exact,
133         aux_builds: aux_builds,
134         exec_env: exec_env,
135         check_lines: check_lines,
136         force_host: force_host,
137         check_stdout: check_stdout,
138         no_prefer_dynamic: no_prefer_dynamic,
139         no_pretty_expanded: no_pretty_expanded,
140         pretty_mode: pretty_mode.unwrap_or("normal".to_string()),
141         pretty_compare_only: pretty_compare_only,
142         forbid_output: forbid_output,
143     }
144 }
145
146 pub fn is_test_ignored(config: &Config, testfile: &Path) -> bool {
147     fn ignore_target(config: &Config) -> String {
148         format!("ignore-{}", util::get_os(config.target.as_slice()))
149     }
150     fn ignore_stage(config: &Config) -> String {
151         format!("ignore-{}",
152                 config.stage_id.as_slice().split('-').next().unwrap())
153     }
154     fn ignore_gdb(config: &Config, line: &str) -> bool {
155         if config.mode != common::DebugInfoGdb {
156             return false;
157         }
158
159         if parse_name_directive(line, "ignore-gdb") {
160             return true;
161         }
162
163         match config.gdb_version {
164             Some(ref actual_version) => {
165                 if line.contains("min-gdb-version") {
166                     let min_version = line.trim()
167                                           .split(' ')
168                                           .last()
169                                           .expect("Malformed GDB version directive");
170                     // Ignore if actual version is smaller the minimum required
171                     // version
172                     gdb_version_to_int(actual_version.as_slice()) <
173                         gdb_version_to_int(min_version.as_slice())
174                 } else {
175                     false
176                 }
177             }
178             None => false
179         }
180     }
181
182     fn ignore_lldb(config: &Config, line: &str) -> bool {
183         if config.mode != common::DebugInfoLldb {
184             return false;
185         }
186
187         if parse_name_directive(line, "ignore-lldb") {
188             return true;
189         }
190
191         match config.lldb_version {
192             Some(ref actual_version) => {
193                 if line.contains("min-lldb-version") {
194                     let min_version = line.trim()
195                                           .split(' ')
196                                           .last()
197                                           .expect("Malformed lldb version directive");
198                     // Ignore if actual version is smaller the minimum required
199                     // version
200                     lldb_version_to_int(actual_version.as_slice()) <
201                         lldb_version_to_int(min_version.as_slice())
202                 } else {
203                     false
204                 }
205             }
206             None => false
207         }
208     }
209
210     let val = iter_header(testfile, |ln| {
211         !parse_name_directive(ln, "ignore-test") &&
212         !parse_name_directive(ln, ignore_target(config).as_slice()) &&
213         !parse_name_directive(ln, ignore_stage(config).as_slice()) &&
214         !(config.mode == common::Pretty && parse_name_directive(ln, "ignore-pretty")) &&
215         !(config.target != config.host && parse_name_directive(ln, "ignore-cross-compile")) &&
216         !ignore_gdb(config, ln) &&
217         !ignore_lldb(config, ln)
218     });
219
220     !val
221 }
222
223 fn iter_header(testfile: &Path, it: |&str| -> bool) -> bool {
224     use std::io::{BufferedReader, File};
225
226     let mut rdr = BufferedReader::new(File::open(testfile).unwrap());
227     for ln in rdr.lines() {
228         // Assume that any directives will be found before the first
229         // module or function. This doesn't seem to be an optimization
230         // with a warm page cache. Maybe with a cold one.
231         let ln = ln.unwrap();
232         if ln.as_slice().starts_with("fn") ||
233                 ln.as_slice().starts_with("mod") {
234             return true;
235         } else {
236             if !(it(ln.as_slice().trim())) {
237                 return false;
238             }
239         }
240     }
241     return true;
242 }
243
244 fn parse_error_pattern(line: &str) -> Option<String> {
245     parse_name_value_directive(line, "error-pattern")
246 }
247
248 fn parse_forbid_output(line: &str) -> Option<String> {
249     parse_name_value_directive(line, "forbid-output")
250 }
251
252 fn parse_aux_build(line: &str) -> Option<String> {
253     parse_name_value_directive(line, "aux-build")
254 }
255
256 fn parse_compile_flags(line: &str) -> Option<String> {
257     parse_name_value_directive(line, "compile-flags")
258 }
259
260 fn parse_run_flags(line: &str) -> Option<String> {
261     parse_name_value_directive(line, "run-flags")
262 }
263
264 fn parse_check_line(line: &str) -> Option<String> {
265     parse_name_value_directive(line, "check")
266 }
267
268 fn parse_force_host(line: &str) -> bool {
269     parse_name_directive(line, "force-host")
270 }
271
272 fn parse_check_stdout(line: &str) -> bool {
273     parse_name_directive(line, "check-stdout")
274 }
275
276 fn parse_no_prefer_dynamic(line: &str) -> bool {
277     parse_name_directive(line, "no-prefer-dynamic")
278 }
279
280 fn parse_no_pretty_expanded(line: &str) -> bool {
281     parse_name_directive(line, "no-pretty-expanded")
282 }
283
284 fn parse_pretty_mode(line: &str) -> Option<String> {
285     parse_name_value_directive(line, "pretty-mode")
286 }
287
288 fn parse_pretty_compare_only(line: &str) -> bool {
289     parse_name_directive(line, "pretty-compare-only")
290 }
291
292 fn parse_exec_env(line: &str) -> Option<(String, String)> {
293     parse_name_value_directive(line, "exec-env").map(|nv| {
294         // nv is either FOO or FOO=BAR
295         let mut strs: Vec<String> = nv.as_slice()
296                                       .splitn(1, '=')
297                                       .map(|s| s.to_string())
298                                       .collect();
299
300         match strs.len() {
301           1u => (strs.pop().unwrap(), "".to_string()),
302           2u => {
303               let end = strs.pop().unwrap();
304               (strs.pop().unwrap(), end)
305           }
306           n => panic!("Expected 1 or 2 strings, not {}", n)
307         }
308     })
309 }
310
311 fn parse_pp_exact(line: &str, testfile: &Path) -> Option<Path> {
312     match parse_name_value_directive(line, "pp-exact") {
313       Some(s) => Some(Path::new(s)),
314       None => {
315         if parse_name_directive(line, "pp-exact") {
316             testfile.filename().map(|s| Path::new(s))
317         } else {
318             None
319         }
320       }
321     }
322 }
323
324 fn parse_name_directive(line: &str, directive: &str) -> bool {
325     line.contains(directive)
326 }
327
328 pub fn parse_name_value_directive(line: &str, directive: &str)
329                                   -> Option<String> {
330     let keycolon = format!("{}:", directive);
331     match line.find_str(keycolon.as_slice()) {
332         Some(colon) => {
333             let value = line.slice(colon + keycolon.len(),
334                                    line.len()).to_string();
335             debug!("{}: {}", directive, value);
336             Some(value)
337         }
338         None => None
339     }
340 }
341
342 pub fn gdb_version_to_int(version_string: &str) -> int {
343     let error_string = format!(
344         "Encountered GDB version string with unexpected format: {}",
345         version_string);
346     let error_string = error_string.as_slice();
347
348     let components: Vec<&str> = version_string.trim().split('.').collect();
349
350     if components.len() != 2 {
351         panic!("{}", error_string);
352     }
353
354     let major: int = components[0].parse().expect(error_string);
355     let minor: int = components[1].parse().expect(error_string);
356
357     return major * 1000 + minor;
358 }
359
360 pub fn lldb_version_to_int(version_string: &str) -> int {
361     let error_string = format!(
362         "Encountered LLDB version string with unexpected format: {}",
363         version_string);
364     let error_string = error_string.as_slice();
365     let major: int = version_string.parse().expect(error_string);
366     return major;
367 }