Alex Crichton | 8e5f0cd | 2015-09-09 22:46:19 -0700 | [diff] [blame] | 1 | extern crate gcc; |
| 2 | extern crate syntex_syntax as syntax; |
| 3 | |
Alex Crichton | 310d623 | 2015-09-10 10:56:31 -0700 | [diff] [blame] | 4 | use std::collections::HashSet; |
Alex Crichton | 8e5f0cd | 2015-09-09 22:46:19 -0700 | [diff] [blame] | 5 | use std::env; |
| 6 | use std::fs::File; |
| 7 | use std::io::BufWriter; |
| 8 | use std::io::prelude::*; |
| 9 | use std::path::{Path, PathBuf}; |
| 10 | |
Alex Crichton | 3150484 | 2015-09-10 23:43:41 -0700 | [diff] [blame] | 11 | use syntax::abi::Abi; |
Alex Crichton | 8e5f0cd | 2015-09-09 22:46:19 -0700 | [diff] [blame] | 12 | use syntax::ast; |
Alex Crichton | a9adfbf | 2015-09-09 23:21:27 -0700 | [diff] [blame] | 13 | use syntax::attr::{self, ReprAttr}; |
Alex Crichton | 3150484 | 2015-09-10 23:43:41 -0700 | [diff] [blame] | 14 | use syntax::diagnostic::SpanHandler; |
| 15 | use syntax::ext::base::SyntaxExtension; |
| 16 | use syntax::ext::expand; |
| 17 | use syntax::parse::token::{intern, InternedString}; |
Alex Crichton | 8e5f0cd | 2015-09-09 22:46:19 -0700 | [diff] [blame] | 18 | use syntax::parse::{self, ParseSess}; |
| 19 | use syntax::visit::{self, Visitor}; |
| 20 | |
Alex Crichton | 310d623 | 2015-09-10 10:56:31 -0700 | [diff] [blame] | 21 | macro_rules! t { |
| 22 | ($e:expr) => (match $e { |
| 23 | Ok(e) => e, |
| 24 | Err(e) => panic!("{} failed with {}", stringify!($e), e), |
| 25 | }) |
| 26 | } |
| 27 | |
Alex Crichton | a9adfbf | 2015-09-09 23:21:27 -0700 | [diff] [blame] | 28 | struct TestGenerator<'a> { |
Alex Crichton | 310d623 | 2015-09-10 10:56:31 -0700 | [diff] [blame] | 29 | target: String, |
Alex Crichton | 8e5f0cd | 2015-09-09 22:46:19 -0700 | [diff] [blame] | 30 | rust: Box<Write>, |
| 31 | c: Box<Write>, |
Alex Crichton | a9adfbf | 2015-09-09 23:21:27 -0700 | [diff] [blame] | 32 | sh: &'a SpanHandler, |
Alex Crichton | 310d623 | 2015-09-10 10:56:31 -0700 | [diff] [blame] | 33 | structs: HashSet<String>, |
Alex Crichton | 3150484 | 2015-09-10 23:43:41 -0700 | [diff] [blame] | 34 | abi: Abi, |
Alex Crichton | 5a28433 | 2015-09-13 23:33:33 -0700 | [diff] [blame] | 35 | tests: Vec<String>, |
Alex Crichton | 310d623 | 2015-09-10 10:56:31 -0700 | [diff] [blame] | 36 | } |
| 37 | |
| 38 | struct StructFinder { |
| 39 | structs: HashSet<String>, |
| 40 | } |
| 41 | |
| 42 | impl<'a> TestGenerator<'a> { |
Alex Crichton | 0df7c10 | 2015-09-10 16:35:37 -0700 | [diff] [blame] | 43 | fn defines(&self) -> Vec<&'static str> { |
| 44 | let mut ret = Vec::new(); |
Alex Crichton | 3150484 | 2015-09-10 23:43:41 -0700 | [diff] [blame] | 45 | |
| 46 | // Pull in extra goodies on linux |
Alex Crichton | 1cf98ae | 2015-09-13 11:19:02 -0700 | [diff] [blame] | 47 | if self.target.contains("unknown-linux-gnu") { |
Alex Crichton | 0df7c10 | 2015-09-10 16:35:37 -0700 | [diff] [blame] | 48 | ret.push("_GNU_SOURCE"); |
| 49 | } |
Alex Crichton | 3150484 | 2015-09-10 23:43:41 -0700 | [diff] [blame] | 50 | |
| 51 | // MSVC doesn't have stdalign.h so get alignof ourselves |
Alex Crichton | ac2bd85 | 2015-09-10 17:21:20 -0700 | [diff] [blame] | 52 | if self.target.contains("msvc") { |
| 53 | ret.push("alignof __alignof"); |
| 54 | } |
Alex Crichton | 3150484 | 2015-09-10 23:43:41 -0700 | [diff] [blame] | 55 | |
Alex Crichton | d3d7792 | 2015-09-11 17:03:39 -0700 | [diff] [blame] | 56 | // android also doesn't have stdalign.h so get alignof ourselves |
Alex Crichton | 8fc95d2 | 2015-09-14 11:06:20 -0700 | [diff] [blame] | 57 | if self.target.contains("android") || self.target.contains("mips") { |
Alex Crichton | d3d7792 | 2015-09-11 17:03:39 -0700 | [diff] [blame] | 58 | ret.push("alignof __alignof__"); |
| 59 | } |
| 60 | |
Alex Crichton | 3150484 | 2015-09-10 23:43:41 -0700 | [diff] [blame] | 61 | // Pull in extra goodies on mingw |
| 62 | if self.target.contains("windows") { |
| 63 | ret.push("_WIN32_WINNT 0x8000"); |
| 64 | } |
Alex Crichton | 0df7c10 | 2015-09-10 16:35:37 -0700 | [diff] [blame] | 65 | return ret |
| 66 | } |
| 67 | |
Alex Crichton | 310d623 | 2015-09-10 10:56:31 -0700 | [diff] [blame] | 68 | fn headers(&self) -> Vec<&'static str> { |
Alex Crichton | ac2bd85 | 2015-09-10 17:21:20 -0700 | [diff] [blame] | 69 | let mut base = Vec::new(); |
| 70 | |
Alex Crichton | f81e3d3 | 2015-09-11 15:27:09 -0700 | [diff] [blame] | 71 | base.extend([ |
Alex Crichton | 0df7c10 | 2015-09-10 16:35:37 -0700 | [diff] [blame] | 72 | "errno.h", |
| 73 | "fcntl.h", |
Alex Crichton | 0df7c10 | 2015-09-10 16:35:37 -0700 | [diff] [blame] | 74 | "limits.h", |
Alex Crichton | 310d623 | 2015-09-10 10:56:31 -0700 | [diff] [blame] | 75 | "stddef.h", |
| 76 | "stdint.h", |
Alex Crichton | 0df7c10 | 2015-09-10 16:35:37 -0700 | [diff] [blame] | 77 | "stdio.h", |
| 78 | "stdlib.h", |
Alex Crichton | 310d623 | 2015-09-10 10:56:31 -0700 | [diff] [blame] | 79 | "sys/stat.h", |
Alex Crichton | 310d623 | 2015-09-10 10:56:31 -0700 | [diff] [blame] | 80 | "sys/types.h", |
Alex Crichton | 310d623 | 2015-09-10 10:56:31 -0700 | [diff] [blame] | 81 | "time.h", |
Alex Crichton | 310d623 | 2015-09-10 10:56:31 -0700 | [diff] [blame] | 82 | "wchar.h", |
Alex Crichton | f81e3d3 | 2015-09-11 15:27:09 -0700 | [diff] [blame] | 83 | ].iter().cloned()); |
Alex Crichton | 310d623 | 2015-09-10 10:56:31 -0700 | [diff] [blame] | 84 | |
| 85 | if self.target.contains("apple-darwin") { |
Alex Crichton | e860619 | 2015-09-10 20:19:44 -0700 | [diff] [blame] | 86 | base.push("mach-o/dyld.h"); |
Alex Crichton | 310d623 | 2015-09-10 10:56:31 -0700 | [diff] [blame] | 87 | base.push("mach/mach_time.h"); |
| 88 | } |
Alex Crichton | ac2bd85 | 2015-09-10 17:21:20 -0700 | [diff] [blame] | 89 | |
Alex Crichton | d3d7792 | 2015-09-11 17:03:39 -0700 | [diff] [blame] | 90 | if self.target.contains("unknown-linux") || |
| 91 | self.target.contains("android") { |
Alex Crichton | 310d623 | 2015-09-10 10:56:31 -0700 | [diff] [blame] | 92 | base.push("linux/if_packet.h"); |
| 93 | base.push("net/ethernet.h"); |
| 94 | } |
| 95 | |
Alex Crichton | ac2bd85 | 2015-09-10 17:21:20 -0700 | [diff] [blame] | 96 | if self.target.contains("windows") { |
Alex Crichton | 7da9b10 | 2015-09-10 20:57:14 -0700 | [diff] [blame] | 97 | base.push("winsock2.h"); // must be before windows.h |
| 98 | |
| 99 | base.push("direct.h"); |
| 100 | base.push("io.h"); |
Alex Crichton | 13a6f2d | 2015-09-10 18:10:58 -0700 | [diff] [blame] | 101 | base.push("sys/utime.h"); |
Alex Crichton | 7da9b10 | 2015-09-10 20:57:14 -0700 | [diff] [blame] | 102 | base.push("windows.h"); |
| 103 | base.push("process.h"); |
| 104 | base.push("ws2ipdef.h"); |
Alex Crichton | 3150484 | 2015-09-10 23:43:41 -0700 | [diff] [blame] | 105 | |
| 106 | if self.target.contains("gnu") { |
| 107 | base.push("stdalign.h"); |
| 108 | base.push("ws2tcpip.h"); |
| 109 | } |
Alex Crichton | ac2bd85 | 2015-09-10 17:21:20 -0700 | [diff] [blame] | 110 | } else { |
Alex Crichton | 2237882 | 2015-09-10 19:59:23 -0700 | [diff] [blame] | 111 | base.push("ctype.h"); |
| 112 | base.push("dirent.h"); |
Alex Crichton | ac2bd85 | 2015-09-10 17:21:20 -0700 | [diff] [blame] | 113 | base.push("net/if.h"); |
| 114 | base.push("netdb.h"); |
| 115 | base.push("netinet/in.h"); |
| 116 | base.push("netinet/ip.h"); |
| 117 | base.push("netinet/tcp.h"); |
| 118 | base.push("pthread.h"); |
| 119 | base.push("signal.h"); |
Alex Crichton | e860619 | 2015-09-10 20:19:44 -0700 | [diff] [blame] | 120 | base.push("string.h"); |
Alex Crichton | 2237882 | 2015-09-10 19:59:23 -0700 | [diff] [blame] | 121 | base.push("sys/file.h"); |
| 122 | base.push("sys/ioctl.h"); |
Alex Crichton | ac2bd85 | 2015-09-10 17:21:20 -0700 | [diff] [blame] | 123 | base.push("sys/mman.h"); |
| 124 | base.push("sys/resource.h"); |
| 125 | base.push("sys/socket.h"); |
| 126 | base.push("sys/time.h"); |
| 127 | base.push("sys/un.h"); |
Alex Crichton | 2237882 | 2015-09-10 19:59:23 -0700 | [diff] [blame] | 128 | base.push("sys/wait.h"); |
Alex Crichton | ac2bd85 | 2015-09-10 17:21:20 -0700 | [diff] [blame] | 129 | base.push("unistd.h"); |
Alex Crichton | 2237882 | 2015-09-10 19:59:23 -0700 | [diff] [blame] | 130 | base.push("utime.h"); |
Alex Crichton | d3d7792 | 2015-09-11 17:03:39 -0700 | [diff] [blame] | 131 | |
| 132 | if self.target.contains("android") { |
| 133 | base.push("arpa/inet.h"); |
| 134 | } else { |
| 135 | base.push("glob.h"); |
| 136 | base.push("ifaddrs.h"); |
Alex Crichton | 8fc95d2 | 2015-09-14 11:06:20 -0700 | [diff] [blame] | 137 | if !self.target.contains("mips") { |
| 138 | base.push("stdalign.h"); |
| 139 | } |
Alex Crichton | d3d7792 | 2015-09-11 17:03:39 -0700 | [diff] [blame] | 140 | base.push("sys/sysctl.h"); |
| 141 | } |
Alex Crichton | ac2bd85 | 2015-09-10 17:21:20 -0700 | [diff] [blame] | 142 | } |
| 143 | |
Alex Crichton | 310d623 | 2015-09-10 10:56:31 -0700 | [diff] [blame] | 144 | return base |
| 145 | } |
| 146 | |
| 147 | fn rust2c(&self, ty: &str) -> String { |
Alex Crichton | 13a6f2d | 2015-09-10 18:10:58 -0700 | [diff] [blame] | 148 | let windows = self.target.contains("windows"); |
Alex Crichton | 310d623 | 2015-09-10 10:56:31 -0700 | [diff] [blame] | 149 | match ty { |
| 150 | t if t.starts_with("c_") => { |
| 151 | match &ty[2..].replace("long", " long")[..] { |
| 152 | s if s.starts_with("u") => format!("unsigned {}", &s[1..]), |
| 153 | "short" => format!("short"), |
| 154 | s if s.starts_with("s") => format!("signed {}", &s[1..]), |
| 155 | s => s.to_string(), |
| 156 | } |
| 157 | } |
Alex Crichton | 3150484 | 2015-09-10 23:43:41 -0700 | [diff] [blame] | 158 | |
| 159 | // Just pass all these through, no need for a "struct" prefix |
Alex Crichton | 2237882 | 2015-09-10 19:59:23 -0700 | [diff] [blame] | 160 | "glob_t" | |
| 161 | "FILE" | |
| 162 | "DIR" | |
| 163 | "fpos_t" => ty.to_string(), |
Alex Crichton | 310d623 | 2015-09-10 10:56:31 -0700 | [diff] [blame] | 164 | t if t.starts_with("pthread") => t.to_string(), |
| 165 | |
Alex Crichton | 3150484 | 2015-09-10 23:43:41 -0700 | [diff] [blame] | 166 | // Windows uppercase structs don't have `struct` in front, there's a |
| 167 | // few special cases for windows, and then otherwise put `struct` in |
| 168 | // front of everything. |
Alex Crichton | 13a6f2d | 2015-09-10 18:10:58 -0700 | [diff] [blame] | 169 | t if self.structs.contains(t) => { |
| 170 | if windows && ty.chars().next().unwrap().is_uppercase() { |
| 171 | t.to_string() |
| 172 | } else if windows && t == "stat" { |
| 173 | "struct __stat64".to_string() |
Alex Crichton | 7da9b10 | 2015-09-10 20:57:14 -0700 | [diff] [blame] | 174 | } else if windows && t == "utimbuf" { |
| 175 | "struct __utimbuf64".to_string() |
Alex Crichton | 13a6f2d | 2015-09-10 18:10:58 -0700 | [diff] [blame] | 176 | } else { |
| 177 | format!("struct {}", t) |
| 178 | } |
| 179 | } |
Alex Crichton | 310d623 | 2015-09-10 10:56:31 -0700 | [diff] [blame] | 180 | |
Alex Crichton | 3150484 | 2015-09-10 23:43:41 -0700 | [diff] [blame] | 181 | // Fixup a few types on windows that don't actually exist. |
Alex Crichton | 13a6f2d | 2015-09-10 18:10:58 -0700 | [diff] [blame] | 182 | "time64_t" if windows => "__time64_t".to_string(), |
| 183 | "ssize_t" if windows => "SSIZE_T".to_string(), |
Alex Crichton | 3150484 | 2015-09-10 23:43:41 -0700 | [diff] [blame] | 184 | |
Alex Crichton | 310d623 | 2015-09-10 10:56:31 -0700 | [diff] [blame] | 185 | t => t.to_string(), |
| 186 | } |
| 187 | } |
| 188 | |
| 189 | fn rust2cfield(&self, struct_: &str, field: &str) -> String { |
| 190 | match field { |
Alex Crichton | 3150484 | 2015-09-10 23:43:41 -0700 | [diff] [blame] | 191 | // Our stat *_nsec fields normally don't actually exist but are part |
| 192 | // of a timeval struct |
Alex Crichton | 310d623 | 2015-09-10 10:56:31 -0700 | [diff] [blame] | 193 | s if s.ends_with("_nsec") && struct_ == "stat" => { |
| 194 | if self.target.contains("apple-darwin") { |
| 195 | s.replace("_nsec", "spec.tv_nsec") |
Alex Crichton | d3d7792 | 2015-09-11 17:03:39 -0700 | [diff] [blame] | 196 | } else if self.target.contains("android") { |
| 197 | s.to_string() |
Alex Crichton | 310d623 | 2015-09-10 10:56:31 -0700 | [diff] [blame] | 198 | } else { |
| 199 | s.replace("e_nsec", ".tv_nsec") |
| 200 | } |
| 201 | } |
| 202 | s => s.to_string(), |
| 203 | } |
| 204 | } |
| 205 | |
| 206 | fn cfg_list(&self) -> Vec<(&'static str, Option<&'static str>)> { |
| 207 | let mut ret = Vec::new(); |
| 208 | let (arch, target_pointer_width) = if self.target.starts_with("x86_64") { |
| 209 | ("x86_64", "64") |
| 210 | } else if self.target.starts_with("i686") { |
| 211 | ("x86", "32") |
Alex Crichton | d3d7792 | 2015-09-11 17:03:39 -0700 | [diff] [blame] | 212 | } else if self.target.starts_with("arm") { |
| 213 | ("arm", "32") |
Alex Crichton | 8fc95d2 | 2015-09-14 11:06:20 -0700 | [diff] [blame] | 214 | } else if self.target.starts_with("mips") { |
| 215 | ("mips", "32") |
Alex Crichton | 310d623 | 2015-09-10 10:56:31 -0700 | [diff] [blame] | 216 | } else { |
| 217 | panic!("unknown arch/pointer width: {}", self.target) |
| 218 | }; |
Alex Crichton | 1cf98ae | 2015-09-13 11:19:02 -0700 | [diff] [blame] | 219 | let (os, family, env) = if self.target.contains("unknown-linux-gnu") { |
Alex Crichton | 310d623 | 2015-09-10 10:56:31 -0700 | [diff] [blame] | 220 | ("linux", "unix", "gnu") |
Alex Crichton | 1cf98ae | 2015-09-13 11:19:02 -0700 | [diff] [blame] | 221 | } else if self.target.contains("unknown-linux-musl") { |
| 222 | ("linux", "unix", "musl") |
Alex Crichton | 310d623 | 2015-09-10 10:56:31 -0700 | [diff] [blame] | 223 | } else if self.target.contains("apple-darwin") { |
| 224 | ("macos", "unix", "") |
Alex Crichton | ac2bd85 | 2015-09-10 17:21:20 -0700 | [diff] [blame] | 225 | } else if self.target.contains("windows-msvc") { |
| 226 | ("windows", "windows", "msvc") |
| 227 | } else if self.target.contains("windows-gnu") { |
| 228 | ("windows", "windows", "gnu") |
Alex Crichton | d3d7792 | 2015-09-11 17:03:39 -0700 | [diff] [blame] | 229 | } else if self.target.contains("android") { |
| 230 | ("android", "unix", "") |
Alex Crichton | 8293ced | 2015-09-15 17:05:19 -0700 | [diff] [blame^] | 231 | } else if self.target.contains("unknown-freebsd") { |
| 232 | ("freebsd", "unix", "") |
Alex Crichton | 310d623 | 2015-09-10 10:56:31 -0700 | [diff] [blame] | 233 | } else { |
| 234 | panic!("unknown os/family width: {}", self.target) |
| 235 | }; |
| 236 | |
| 237 | ret.push((family, None)); |
| 238 | ret.push(("target_os", Some(os))); |
| 239 | ret.push(("target_family", Some(family))); |
| 240 | ret.push(("target_arch", Some(arch))); |
| 241 | // skip endianness |
| 242 | ret.push(("target_pointer_width", Some(target_pointer_width))); |
| 243 | ret.push(("target_env", Some(env))); |
| 244 | |
| 245 | return ret |
| 246 | } |
Alex Crichton | 8e5f0cd | 2015-09-09 22:46:19 -0700 | [diff] [blame] | 247 | } |
| 248 | |
| 249 | fn main() { |
Alex Crichton | 310d623 | 2015-09-10 10:56:31 -0700 | [diff] [blame] | 250 | // Prep the test generator |
| 251 | let target = t!(env::var("TARGET")); |
| 252 | let out = PathBuf::from(env::var_os("OUT_DIR").unwrap()); |
| 253 | let rust_out = BufWriter::new(t!(File::create(out.join("all.rs")))); |
| 254 | let c_out = BufWriter::new(t!(File::create(out.join("all.c")))); |
Alex Crichton | 1608306 | 2015-09-09 22:59:24 -0700 | [diff] [blame] | 255 | let sess = ParseSess::new(); |
Alex Crichton | 310d623 | 2015-09-10 10:56:31 -0700 | [diff] [blame] | 256 | let mut tg = TestGenerator { |
| 257 | target: target, |
| 258 | rust: Box::new(rust_out), |
| 259 | c: Box::new(c_out), |
| 260 | sh: &sess.span_diagnostic, |
| 261 | structs: HashSet::new(), |
Alex Crichton | 3150484 | 2015-09-10 23:43:41 -0700 | [diff] [blame] | 262 | abi: Abi::C, |
Alex Crichton | 5a28433 | 2015-09-13 23:33:33 -0700 | [diff] [blame] | 263 | tests: Vec::new(), |
Alex Crichton | 310d623 | 2015-09-10 10:56:31 -0700 | [diff] [blame] | 264 | }; |
| 265 | |
| 266 | // Parse the libc crate |
Alex Crichton | 8e5f0cd | 2015-09-09 22:46:19 -0700 | [diff] [blame] | 267 | let src = Path::new("../src/lib.rs"); |
| 268 | let cfg = Vec::new(); |
Alex Crichton | 3150484 | 2015-09-10 23:43:41 -0700 | [diff] [blame] | 269 | let krate = parse::parse_crate_from_file(src, cfg, &sess); |
| 270 | |
| 271 | // expand macros |
| 272 | let ecfg = expand::ExpansionConfig::default("libc".to_string()); |
| 273 | let exts = vec![ |
| 274 | (intern("macro_rules"), SyntaxExtension::MacroRulesTT), |
| 275 | ]; |
| 276 | let mut krate = expand::expand_crate(&sess, ecfg, Vec::new(), |
| 277 | exts, &mut Vec::new(), krate); |
Alex Crichton | 8e5f0cd | 2015-09-09 22:46:19 -0700 | [diff] [blame] | 278 | |
Alex Crichton | 310d623 | 2015-09-10 10:56:31 -0700 | [diff] [blame] | 279 | // Strip the crate down to just what's configured for our target |
| 280 | for (k, v) in tg.cfg_list() { |
| 281 | let s = InternedString::new; |
| 282 | krate.config.push(match v { |
| 283 | Some(v) => attr::mk_name_value_item_str(s(k), s(v)), |
| 284 | None => attr::mk_word_item(s(k)), |
| 285 | }); |
| 286 | } |
Alex Crichton | 8e5f0cd | 2015-09-09 22:46:19 -0700 | [diff] [blame] | 287 | let mut gated_cfgs = Vec::new(); |
| 288 | let krate = syntax::config::strip_unconfigured_items(&sess.span_diagnostic, |
| 289 | krate, |
| 290 | &mut gated_cfgs); |
| 291 | |
Alex Crichton | 310d623 | 2015-09-10 10:56:31 -0700 | [diff] [blame] | 292 | // Probe the crate to find all structs (used to convert type names to names |
| 293 | // in C). |
| 294 | let mut structs = StructFinder { |
| 295 | structs: HashSet::new(), |
| 296 | }; |
| 297 | visit::walk_crate(&mut structs, &krate); |
| 298 | tg.structs = structs.structs; |
Alex Crichton | 8e5f0cd | 2015-09-09 22:46:19 -0700 | [diff] [blame] | 299 | |
Alex Crichton | 0df7c10 | 2015-09-10 16:35:37 -0700 | [diff] [blame] | 300 | // Prep the C file by emitting header stuff |
| 301 | for define in tg.defines() { |
| 302 | t!(writeln!(tg.c, "#define {}", define)); |
| 303 | } |
Alex Crichton | 310d623 | 2015-09-10 10:56:31 -0700 | [diff] [blame] | 304 | for header in tg.headers() { |
| 305 | t!(writeln!(tg.c, "#include <{}>", header)); |
Alex Crichton | 1608306 | 2015-09-09 22:59:24 -0700 | [diff] [blame] | 306 | } |
Alex Crichton | 0df7c10 | 2015-09-10 16:35:37 -0700 | [diff] [blame] | 307 | |
| 308 | // Walk the crate, emitting test cases for everything found |
Alex Crichton | 310d623 | 2015-09-10 10:56:31 -0700 | [diff] [blame] | 309 | visit::walk_crate(&mut tg, &krate); |
Alex Crichton | 5a28433 | 2015-09-13 23:33:33 -0700 | [diff] [blame] | 310 | tg.emit_run_all(); |
Alex Crichton | 1608306 | 2015-09-09 22:59:24 -0700 | [diff] [blame] | 311 | |
Alex Crichton | 310d623 | 2015-09-10 10:56:31 -0700 | [diff] [blame] | 312 | // Compile our C shim to be linked into tests |
Alex Crichton | ac2bd85 | 2015-09-10 17:21:20 -0700 | [diff] [blame] | 313 | let mut cfg = gcc::Config::new(); |
| 314 | cfg.file(out.join("all.c")); |
| 315 | |
| 316 | if tg.target.contains("msvc") { |
Alex Crichton | 13a6f2d | 2015-09-10 18:10:58 -0700 | [diff] [blame] | 317 | cfg.flag("/W3").flag("/Wall").flag("/WX") |
| 318 | .flag("/wd4820") // weird warning about adding padding? |
Alex Crichton | 7da9b10 | 2015-09-10 20:57:14 -0700 | [diff] [blame] | 319 | .flag("/wd4100") // don't warn about unused parameters |
| 320 | .flag("/wd4996"); // don't warn about deprecated functions |
Alex Crichton | ac2bd85 | 2015-09-10 17:21:20 -0700 | [diff] [blame] | 321 | } else { |
Alex Crichton | b01a8cc | 2015-09-10 17:37:22 -0700 | [diff] [blame] | 322 | cfg.flag("-Wall").flag("-Wextra").flag("-Werror") |
Alex Crichton | 6d3cfdb | 2015-09-15 14:53:01 -0700 | [diff] [blame] | 323 | .flag("-Wno-unused-parameter") |
| 324 | .flag("-Wno-type-limits"); |
Alex Crichton | ac2bd85 | 2015-09-10 17:21:20 -0700 | [diff] [blame] | 325 | } |
| 326 | |
| 327 | drop(tg); |
| 328 | cfg.compile("liball.a"); |
Alex Crichton | 8e5f0cd | 2015-09-09 22:46:19 -0700 | [diff] [blame] | 329 | } |
| 330 | |
Alex Crichton | a9adfbf | 2015-09-09 23:21:27 -0700 | [diff] [blame] | 331 | impl<'a> TestGenerator<'a> { |
| 332 | fn test_type(&mut self, ty: &str) { |
Alex Crichton | 310d623 | 2015-09-10 10:56:31 -0700 | [diff] [blame] | 333 | match ty { |
Alex Crichton | d3d7792 | 2015-09-11 17:03:39 -0700 | [diff] [blame] | 334 | // sighandler_t is crazy across platforms |
Alex Crichton | 310d623 | 2015-09-10 10:56:31 -0700 | [diff] [blame] | 335 | "sighandler_t" => return, |
Alex Crichton | d3d7792 | 2015-09-11 17:03:39 -0700 | [diff] [blame] | 336 | |
| 337 | // Not actually defined on android, but it's not hurting anyone |
| 338 | "in_port_t" if self.target.contains("android") => return, |
Alex Crichton | 310d623 | 2015-09-10 10:56:31 -0700 | [diff] [blame] | 339 | _ => {} |
| 340 | } |
Alex Crichton | 2237882 | 2015-09-10 19:59:23 -0700 | [diff] [blame] | 341 | let c = self.rust_ty_to_c_ty(ty); |
Alex Crichton | 310d623 | 2015-09-10 10:56:31 -0700 | [diff] [blame] | 342 | self.test_size_align(ty, &c); |
Alex Crichton | 6d3cfdb | 2015-09-15 14:53:01 -0700 | [diff] [blame] | 343 | self.test_sign(ty, &c); |
Alex Crichton | a9adfbf | 2015-09-09 23:21:27 -0700 | [diff] [blame] | 344 | } |
Alex Crichton | 1608306 | 2015-09-09 22:59:24 -0700 | [diff] [blame] | 345 | |
Alex Crichton | 3e5155b | 2015-09-09 23:46:19 -0700 | [diff] [blame] | 346 | fn test_struct(&mut self, ty: &str, s: &ast::StructDef) { |
Alex Crichton | 2237882 | 2015-09-10 19:59:23 -0700 | [diff] [blame] | 347 | let cty = self.rust_ty_to_c_ty(ty); |
Alex Crichton | a9adfbf | 2015-09-09 23:21:27 -0700 | [diff] [blame] | 348 | self.test_size_align(ty, &cty); |
Alex Crichton | 3e5155b | 2015-09-09 23:46:19 -0700 | [diff] [blame] | 349 | |
Alex Crichton | 5a28433 | 2015-09-13 23:33:33 -0700 | [diff] [blame] | 350 | self.tests.push(format!("field_offset_size_{}", ty)); |
Alex Crichton | 310d623 | 2015-09-10 10:56:31 -0700 | [diff] [blame] | 351 | t!(writeln!(self.rust, r#" |
Alex Crichton | 3e5155b | 2015-09-09 23:46:19 -0700 | [diff] [blame] | 352 | fn field_offset_size_{ty}() {{ |
Alex Crichton | 5a28433 | 2015-09-13 23:33:33 -0700 | [diff] [blame] | 353 | println!("verifying struct {ty}"); |
Alex Crichton | 310d623 | 2015-09-10 10:56:31 -0700 | [diff] [blame] | 354 | "#, ty = ty)); |
Alex Crichton | 3e5155b | 2015-09-09 23:46:19 -0700 | [diff] [blame] | 355 | for field in s.fields.iter() { |
| 356 | let name = match field.node.kind { |
| 357 | ast::NamedField(name, ast::Public) => name, |
| 358 | ast::NamedField(_, ast::Inherited) => continue, |
| 359 | ast::UnnamedField(..) => panic!("no tuple structs in FFI"), |
| 360 | }; |
| 361 | |
Alex Crichton | 310d623 | 2015-09-10 10:56:31 -0700 | [diff] [blame] | 362 | let cfield = self.rust2cfield(ty, &name.to_string()); |
Alex Crichton | 3e5155b | 2015-09-09 23:46:19 -0700 | [diff] [blame] | 363 | |
Alex Crichton | 310d623 | 2015-09-10 10:56:31 -0700 | [diff] [blame] | 364 | t!(writeln!(self.c, r#" |
Alex Crichton | 671376b | 2015-09-10 17:39:03 -0700 | [diff] [blame] | 365 | uint64_t __test_offset_{ty}_{rust_field}(void) {{ |
Alex Crichton | 3e5155b | 2015-09-09 23:46:19 -0700 | [diff] [blame] | 366 | return offsetof({cty}, {c_field}); |
| 367 | }} |
Alex Crichton | 671376b | 2015-09-10 17:39:03 -0700 | [diff] [blame] | 368 | uint64_t __test_size_{ty}_{rust_field}(void) {{ |
Alex Crichton | 3e5155b | 2015-09-09 23:46:19 -0700 | [diff] [blame] | 369 | {cty}* foo = NULL; |
| 370 | return sizeof(foo->{c_field}); |
| 371 | }} |
Alex Crichton | 310d623 | 2015-09-10 10:56:31 -0700 | [diff] [blame] | 372 | "#, ty = ty, cty = cty, rust_field = name, c_field = cfield)); |
| 373 | t!(writeln!(self.rust, r#" |
Alex Crichton | 3e5155b | 2015-09-09 23:46:19 -0700 | [diff] [blame] | 374 | extern {{ |
| 375 | fn __test_offset_{ty}_{field}() -> u64; |
| 376 | fn __test_size_{ty}_{field}() -> u64; |
| 377 | }} |
| 378 | unsafe {{ |
| 379 | let foo = 0 as *const {ty}; |
| 380 | same(offset_of!({ty}, {field}), |
| 381 | __test_offset_{ty}_{field}(), |
| 382 | "field offset {field} of {ty}"); |
| 383 | same(mem::size_of_val(&(*foo).{field}) as u64, |
| 384 | __test_size_{ty}_{field}(), |
| 385 | "field size {field} of {ty}"); |
| 386 | }} |
Alex Crichton | 310d623 | 2015-09-10 10:56:31 -0700 | [diff] [blame] | 387 | "#, ty = ty, field = name)); |
Alex Crichton | 3e5155b | 2015-09-09 23:46:19 -0700 | [diff] [blame] | 388 | } |
Alex Crichton | 310d623 | 2015-09-10 10:56:31 -0700 | [diff] [blame] | 389 | t!(writeln!(self.rust, r#" |
Alex Crichton | 3e5155b | 2015-09-09 23:46:19 -0700 | [diff] [blame] | 390 | }} |
Alex Crichton | 310d623 | 2015-09-10 10:56:31 -0700 | [diff] [blame] | 391 | "#)); |
Alex Crichton | a9adfbf | 2015-09-09 23:21:27 -0700 | [diff] [blame] | 392 | } |
| 393 | |
| 394 | fn test_size_align(&mut self, rust: &str, c: &str) { |
Alex Crichton | 310d623 | 2015-09-10 10:56:31 -0700 | [diff] [blame] | 395 | t!(writeln!(self.c, r#" |
Alex Crichton | 671376b | 2015-09-10 17:39:03 -0700 | [diff] [blame] | 396 | uint64_t __test_size_{ty}(void) {{ return sizeof({cty}); }} |
| 397 | uint64_t __test_align_{ty}(void) {{ return alignof({cty}); }} |
Alex Crichton | 310d623 | 2015-09-10 10:56:31 -0700 | [diff] [blame] | 398 | "#, ty = rust, cty = c)); |
| 399 | t!(writeln!(self.rust, r#" |
Alex Crichton | a9adfbf | 2015-09-09 23:21:27 -0700 | [diff] [blame] | 400 | fn size_align_{ty}() {{ |
Alex Crichton | 1608306 | 2015-09-09 22:59:24 -0700 | [diff] [blame] | 401 | extern {{ |
Alex Crichton | a9adfbf | 2015-09-09 23:21:27 -0700 | [diff] [blame] | 402 | fn __test_size_{ty}() -> u64; |
| 403 | fn __test_align_{ty}() -> u64; |
Alex Crichton | 1608306 | 2015-09-09 22:59:24 -0700 | [diff] [blame] | 404 | }} |
Alex Crichton | 5a28433 | 2015-09-13 23:33:33 -0700 | [diff] [blame] | 405 | println!("verifying type {ty} align/size"); |
Alex Crichton | 1608306 | 2015-09-09 22:59:24 -0700 | [diff] [blame] | 406 | unsafe {{ |
Alex Crichton | 3e5155b | 2015-09-09 23:46:19 -0700 | [diff] [blame] | 407 | same(mem::size_of::<{ty}>() as u64, |
Alex Crichton | 5a28433 | 2015-09-13 23:33:33 -0700 | [diff] [blame] | 408 | __test_size_{ty}(), "{ty} size"); |
Alex Crichton | c8b895c | 2015-09-10 13:24:15 -0700 | [diff] [blame] | 409 | same(align::<{ty}>() as u64, |
Alex Crichton | 5a28433 | 2015-09-13 23:33:33 -0700 | [diff] [blame] | 410 | __test_align_{ty}(), "{ty} align"); |
Alex Crichton | 1608306 | 2015-09-09 22:59:24 -0700 | [diff] [blame] | 411 | }} |
Alex Crichton | 8e5f0cd | 2015-09-09 22:46:19 -0700 | [diff] [blame] | 412 | }} |
Alex Crichton | 310d623 | 2015-09-10 10:56:31 -0700 | [diff] [blame] | 413 | "#, ty = rust)); |
Alex Crichton | 5a28433 | 2015-09-13 23:33:33 -0700 | [diff] [blame] | 414 | self.tests.push(format!("size_align_{}", rust)); |
Alex Crichton | a9adfbf | 2015-09-09 23:21:27 -0700 | [diff] [blame] | 415 | } |
| 416 | |
Alex Crichton | 6d3cfdb | 2015-09-15 14:53:01 -0700 | [diff] [blame] | 417 | fn test_sign(&mut self, rust: &str, c: &str) { |
| 418 | match c { |
| 419 | "float" | |
Alex Crichton | eef03da | 2015-09-15 16:57:06 -0700 | [diff] [blame] | 420 | "mach_timebase_info_data_t" | |
Alex Crichton | 6d3cfdb | 2015-09-15 14:53:01 -0700 | [diff] [blame] | 421 | "double" => return, |
Alex Crichton | 8293ced | 2015-09-15 17:05:19 -0700 | [diff] [blame^] | 422 | n if n.starts_with("pthread") => return, |
Alex Crichton | 6d3cfdb | 2015-09-15 14:53:01 -0700 | [diff] [blame] | 423 | _ => {} |
| 424 | } |
| 425 | t!(writeln!(self.c, r#" |
| 426 | uint32_t __test_signed_{ty}(void) {{ |
| 427 | return ((({cty}) -1) < 0); |
| 428 | }} |
| 429 | "#, ty = rust, cty = c)); |
| 430 | t!(writeln!(self.rust, r#" |
| 431 | fn sign_{ty}() {{ |
| 432 | extern {{ |
| 433 | fn __test_signed_{ty}() -> u32; |
| 434 | }} |
| 435 | println!("verifying type {ty} sign"); |
| 436 | unsafe {{ |
| 437 | same(((!(0 as {ty})) < (0 as {ty})) as u32, |
| 438 | __test_signed_{ty}(), "{ty} signed"); |
| 439 | }} |
| 440 | }} |
| 441 | "#, ty = rust)); |
| 442 | self.tests.push(format!("sign_{}", rust)); |
| 443 | } |
| 444 | |
Alex Crichton | 2237882 | 2015-09-10 19:59:23 -0700 | [diff] [blame] | 445 | fn rust_ty_to_c_ty(&self, mut rust_ty: &str) -> String { |
| 446 | let mut cty = self.rust2c(&rust_ty.replace("*mut ", "") |
| 447 | .replace("*const ", "")); |
| 448 | while rust_ty.starts_with("*") { |
| 449 | if rust_ty.starts_with("*const") { |
| 450 | cty = format!("const {}*", cty); |
| 451 | rust_ty = &rust_ty[7..]; |
| 452 | } else { |
| 453 | cty = format!("{}*", cty); |
| 454 | rust_ty = &rust_ty[5..]; |
| 455 | } |
| 456 | } |
| 457 | return cty |
| 458 | } |
| 459 | |
Alex Crichton | 0df7c10 | 2015-09-10 16:35:37 -0700 | [diff] [blame] | 460 | fn test_const(&mut self, name: &str, rust_ty: &str) { |
Alex Crichton | 3150484 | 2015-09-10 23:43:41 -0700 | [diff] [blame] | 461 | let mingw = self.target.contains("windows-gnu"); |
| 462 | |
| 463 | // Apparently these don't exist in mingw headers? |
| 464 | match name { |
| 465 | "MEM_RESET_UNDO" | |
| 466 | "FILE_ATTRIBUTE_NO_SCRUB_DATA" | |
| 467 | "FILE_ATTRIBUTE_INTEGRITY_STREAM" | |
| 468 | "ERROR_NOTHING_TO_TERMINATE" if mingw => return, |
| 469 | _ => {} |
| 470 | } |
| 471 | |
Alex Crichton | 2237882 | 2015-09-10 19:59:23 -0700 | [diff] [blame] | 472 | let cty = self.rust_ty_to_c_ty(rust_ty); |
Alex Crichton | 3150484 | 2015-09-10 23:43:41 -0700 | [diff] [blame] | 473 | |
| 474 | // SIG_IGN has weird types on platforms, just worry about it as a size_t |
Alex Crichton | 0df7c10 | 2015-09-10 16:35:37 -0700 | [diff] [blame] | 475 | let cast = if name == "SIG_IGN" {"(size_t)"} else {""}; |
Alex Crichton | 3150484 | 2015-09-10 23:43:41 -0700 | [diff] [blame] | 476 | |
Alex Crichton | 0df7c10 | 2015-09-10 16:35:37 -0700 | [diff] [blame] | 477 | t!(writeln!(self.c, r#" |
Alex Crichton | 13a6f2d | 2015-09-10 18:10:58 -0700 | [diff] [blame] | 478 | int __test_const_{name}({cty} *outptr) {{ |
| 479 | *outptr = {cast}({name}); |
| 480 | return 1; |
Alex Crichton | e7afdd8 | 2015-09-10 17:15:20 -0700 | [diff] [blame] | 481 | }} |
Alex Crichton | 0df7c10 | 2015-09-10 16:35:37 -0700 | [diff] [blame] | 482 | "#, name = name, cast = cast, cty = cty)); |
| 483 | t!(writeln!(self.rust, r#" |
Alex Crichton | 0df7c10 | 2015-09-10 16:35:37 -0700 | [diff] [blame] | 484 | fn const_{name}() {{ |
| 485 | extern {{ |
Alex Crichton | e7afdd8 | 2015-09-10 17:15:20 -0700 | [diff] [blame] | 486 | fn __test_const_{name}(out: *mut {ty}) -> c_int; |
Alex Crichton | 0df7c10 | 2015-09-10 16:35:37 -0700 | [diff] [blame] | 487 | }} |
Alex Crichton | 5a28433 | 2015-09-13 23:33:33 -0700 | [diff] [blame] | 488 | println!("verifying const {name} value"); |
Alex Crichton | 0df7c10 | 2015-09-10 16:35:37 -0700 | [diff] [blame] | 489 | unsafe {{ |
Alex Crichton | e7afdd8 | 2015-09-10 17:15:20 -0700 | [diff] [blame] | 490 | let mut o = mem::zeroed(); |
| 491 | if __test_const_{name}(&mut o) == 0 {{ |
Alex Crichton | 5a28433 | 2015-09-13 23:33:33 -0700 | [diff] [blame] | 492 | panic!("{name} not defined"); |
Alex Crichton | e7afdd8 | 2015-09-10 17:15:20 -0700 | [diff] [blame] | 493 | }} else {{ |
Alex Crichton | 5a28433 | 2015-09-13 23:33:33 -0700 | [diff] [blame] | 494 | same({name}, o, "{name} value"); |
Alex Crichton | e7afdd8 | 2015-09-10 17:15:20 -0700 | [diff] [blame] | 495 | }} |
Alex Crichton | 0df7c10 | 2015-09-10 16:35:37 -0700 | [diff] [blame] | 496 | }} |
| 497 | }} |
| 498 | "#, ty = rust_ty, name = name)); |
Alex Crichton | 5a28433 | 2015-09-13 23:33:33 -0700 | [diff] [blame] | 499 | self.tests.push(format!("const_{}", name)); |
Alex Crichton | 0df7c10 | 2015-09-10 16:35:37 -0700 | [diff] [blame] | 500 | } |
| 501 | |
Alex Crichton | 7da9b10 | 2015-09-10 20:57:14 -0700 | [diff] [blame] | 502 | fn test_extern_fn(&mut self, name: &str, cname: &str, |
| 503 | args: &[String], ret: &str, |
Alex Crichton | 3150484 | 2015-09-10 23:43:41 -0700 | [diff] [blame] | 504 | variadic: bool, abi: Abi) { |
Alex Crichton | 2237882 | 2015-09-10 19:59:23 -0700 | [diff] [blame] | 505 | match name { |
| 506 | // manually verified |
| 507 | "execv" | |
| 508 | "execve" | |
| 509 | "execvp" | |
Alex Crichton | 7da9b10 | 2015-09-10 20:57:14 -0700 | [diff] [blame] | 510 | "execvpe" | |
Alex Crichton | 2237882 | 2015-09-10 19:59:23 -0700 | [diff] [blame] | 511 | "glob" | |
| 512 | "getrlimit" | |
| 513 | "setrlimit" | |
Alex Crichton | e860619 | 2015-09-10 20:19:44 -0700 | [diff] [blame] | 514 | "signal" | |
Alex Crichton | 2237882 | 2015-09-10 19:59:23 -0700 | [diff] [blame] | 515 | "getopt" => return, |
| 516 | _ => {} |
| 517 | } |
| 518 | let args = if args.len() == 0 && !variadic { |
| 519 | "void".to_string() |
| 520 | } else { |
| 521 | args.iter().map(|a| self.rust_ty_to_c_ty(a)).collect::<Vec<_>>() |
| 522 | .connect(", ") + if variadic {", ..."} else {""} |
| 523 | }; |
| 524 | let cret = self.rust_ty_to_c_ty(ret); |
Alex Crichton | 3150484 | 2015-09-10 23:43:41 -0700 | [diff] [blame] | 525 | let abi = match abi { |
| 526 | Abi::C => "", |
| 527 | Abi::Stdcall => "__stdcall ", |
| 528 | Abi::System if self.target.contains("i686-pc-windows") => { |
| 529 | "__stdcall " |
| 530 | } |
| 531 | Abi::System => "", |
| 532 | a => panic!("unknown ABI: {}", a), |
| 533 | }; |
Alex Crichton | 2237882 | 2015-09-10 19:59:23 -0700 | [diff] [blame] | 534 | t!(writeln!(self.c, r#" |
Alex Crichton | 3150484 | 2015-09-10 23:43:41 -0700 | [diff] [blame] | 535 | {ret} ({abi}*__test_fn_{name}(void))({args}) {{ |
Alex Crichton | 7da9b10 | 2015-09-10 20:57:14 -0700 | [diff] [blame] | 536 | return {cname}; |
Alex Crichton | 2237882 | 2015-09-10 19:59:23 -0700 | [diff] [blame] | 537 | }} |
Alex Crichton | 3150484 | 2015-09-10 23:43:41 -0700 | [diff] [blame] | 538 | "#, name = name, cname = cname, args = args, ret = cret, abi = abi)); |
Alex Crichton | 2237882 | 2015-09-10 19:59:23 -0700 | [diff] [blame] | 539 | t!(writeln!(self.rust, r#" |
Alex Crichton | 2237882 | 2015-09-10 19:59:23 -0700 | [diff] [blame] | 540 | fn fn_{name}() {{ |
| 541 | extern {{ |
| 542 | fn __test_fn_{name}() -> size_t; |
| 543 | }} |
Alex Crichton | 5a28433 | 2015-09-13 23:33:33 -0700 | [diff] [blame] | 544 | println!("verifying function {name} pointer"); |
Alex Crichton | 2237882 | 2015-09-10 19:59:23 -0700 | [diff] [blame] | 545 | unsafe {{ |
| 546 | same({name} as usize, |
Alex Crichton | 5a28433 | 2015-09-13 23:33:33 -0700 | [diff] [blame] | 547 | __test_fn_{name}() as usize, |
| 548 | "{name} function pointer"); |
Alex Crichton | 2237882 | 2015-09-10 19:59:23 -0700 | [diff] [blame] | 549 | }} |
| 550 | }} |
| 551 | "#, name = name)); |
Alex Crichton | 5a28433 | 2015-09-13 23:33:33 -0700 | [diff] [blame] | 552 | self.tests.push(format!("fn_{}", name)); |
Alex Crichton | 2237882 | 2015-09-10 19:59:23 -0700 | [diff] [blame] | 553 | } |
| 554 | |
| 555 | fn assert_no_generics(&self, _i: ast::Ident, generics: &ast::Generics) { |
| 556 | assert!(generics.lifetimes.len() == 0); |
| 557 | assert!(generics.ty_params.len() == 0); |
| 558 | assert!(generics.where_clause.predicates.len() == 0); |
Alex Crichton | 8e5f0cd | 2015-09-09 22:46:19 -0700 | [diff] [blame] | 559 | } |
Alex Crichton | 0df7c10 | 2015-09-10 16:35:37 -0700 | [diff] [blame] | 560 | |
| 561 | fn ty2name(&self, ty: &ast::Ty) -> String { |
| 562 | match ty.node { |
| 563 | ast::TyPath(_, ref path) => { |
| 564 | path.segments.last().unwrap().identifier.to_string() |
| 565 | } |
| 566 | ast::TyPtr(ref t) => { |
| 567 | format!("*{} {}", match t.mutbl { |
| 568 | ast::MutImmutable => "const", |
| 569 | ast::MutMutable => "mut", |
| 570 | }, self.ty2name(&t.ty)) |
| 571 | } |
Alex Crichton | 2237882 | 2015-09-10 19:59:23 -0700 | [diff] [blame] | 572 | ast::TyBareFn(ref t) => { |
| 573 | assert!(t.lifetimes.len() == 0); |
| 574 | let (ret, mut args, variadic) = self.decl2rust(&t.decl); |
| 575 | assert!(!variadic); |
| 576 | if args.len() == 0 { |
| 577 | args.push("void".to_string()); |
| 578 | } |
| 579 | format!("{}(*)({})", ret, args.connect(", ")) |
| 580 | } |
Alex Crichton | 0df7c10 | 2015-09-10 16:35:37 -0700 | [diff] [blame] | 581 | _ => panic!("unknown ty {:?}", ty), |
| 582 | } |
| 583 | } |
Alex Crichton | 2237882 | 2015-09-10 19:59:23 -0700 | [diff] [blame] | 584 | |
| 585 | fn decl2rust(&self, decl: &ast::FnDecl) -> (String, Vec<String>, bool) { |
| 586 | let args = decl.inputs.iter().map(|arg| { |
| 587 | self.ty2name(&arg.ty) |
| 588 | }).collect::<Vec<_>>(); |
| 589 | let ret = match decl.output { |
| 590 | ast::NoReturn(..) | |
| 591 | ast::DefaultReturn(..) => "void".to_string(), |
| 592 | ast::Return(ref t) => self.ty2name(t), |
| 593 | }; |
| 594 | (ret, args, decl.variadic) |
| 595 | } |
Alex Crichton | 5a28433 | 2015-09-13 23:33:33 -0700 | [diff] [blame] | 596 | |
| 597 | fn emit_run_all(&mut self) { |
| 598 | t!(writeln!(self.rust, " |
| 599 | fn run_all() {{ |
| 600 | ")); |
| 601 | for test in self.tests.iter() { |
| 602 | if test.starts_with("fn_") { |
| 603 | // FIXME: weird dllimport issues with windows? |
| 604 | t!(writeln!(self.rust, "if cfg!(not(windows)) {{ {}(); }}", |
| 605 | test)); |
| 606 | } else { |
| 607 | t!(writeln!(self.rust, "{}();", test)); |
| 608 | } |
| 609 | } |
| 610 | t!(writeln!(self.rust, " |
| 611 | }} |
| 612 | ")); |
| 613 | } |
Alex Crichton | 8e5f0cd | 2015-09-09 22:46:19 -0700 | [diff] [blame] | 614 | } |
| 615 | |
Alex Crichton | a9adfbf | 2015-09-09 23:21:27 -0700 | [diff] [blame] | 616 | impl<'a, 'v> Visitor<'v> for TestGenerator<'a> { |
Alex Crichton | 2237882 | 2015-09-10 19:59:23 -0700 | [diff] [blame] | 617 | fn visit_item(&mut self, i: &'v ast::Item) { |
Alex Crichton | 3150484 | 2015-09-10 23:43:41 -0700 | [diff] [blame] | 618 | let prev_abi = self.abi; |
Alex Crichton | 2237882 | 2015-09-10 19:59:23 -0700 | [diff] [blame] | 619 | match i.node { |
| 620 | ast::ItemTy(_, ref generics) => { |
| 621 | self.assert_no_generics(i.ident, generics); |
| 622 | self.test_type(&i.ident.to_string()); |
| 623 | } |
Alex Crichton | a9adfbf | 2015-09-09 23:21:27 -0700 | [diff] [blame] | 624 | |
Alex Crichton | 2237882 | 2015-09-10 19:59:23 -0700 | [diff] [blame] | 625 | ast::ItemStruct(ref s, ref generics) => { |
| 626 | self.assert_no_generics(i.ident, generics); |
| 627 | let is_c = i.attrs.iter().any(|a| { |
Alex Crichton | a9adfbf | 2015-09-09 23:21:27 -0700 | [diff] [blame] | 628 | attr::find_repr_attrs(self.sh, a).iter().any(|a| { |
| 629 | *a == ReprAttr::ReprExtern |
| 630 | }) |
Alex Crichton | 2237882 | 2015-09-10 19:59:23 -0700 | [diff] [blame] | 631 | }); |
| 632 | if !is_c { |
| 633 | panic!("{} is not marked #[repr(C)]", i.ident); |
| 634 | } |
| 635 | self.test_struct(&i.ident.to_string(), s); |
| 636 | } |
Alex Crichton | 8e5f0cd | 2015-09-09 22:46:19 -0700 | [diff] [blame] | 637 | |
Alex Crichton | 2237882 | 2015-09-10 19:59:23 -0700 | [diff] [blame] | 638 | ast::ItemConst(ref ty, _) => { |
| 639 | let ty = self.ty2name(ty); |
| 640 | self.test_const(&i.ident.to_string(), &ty); |
| 641 | } |
Alex Crichton | 0df7c10 | 2015-09-10 16:35:37 -0700 | [diff] [blame] | 642 | |
Alex Crichton | 3150484 | 2015-09-10 23:43:41 -0700 | [diff] [blame] | 643 | ast::ItemForeignMod(ref fm) => { |
| 644 | self.abi = fm.abi; |
| 645 | } |
| 646 | |
Alex Crichton | 2237882 | 2015-09-10 19:59:23 -0700 | [diff] [blame] | 647 | _ => {} |
| 648 | } |
Alex Crichton | 3150484 | 2015-09-10 23:43:41 -0700 | [diff] [blame] | 649 | visit::walk_item(self, i); |
| 650 | self.abi = prev_abi; |
Alex Crichton | 2237882 | 2015-09-10 19:59:23 -0700 | [diff] [blame] | 651 | } |
| 652 | |
| 653 | fn visit_foreign_item(&mut self, i: &'v ast::ForeignItem) { |
| 654 | match i.node { |
| 655 | ast::ForeignItemFn(ref decl, ref generics) => { |
| 656 | self.assert_no_generics(i.ident, generics); |
| 657 | let (ret, args, variadic) = self.decl2rust(decl); |
Alex Crichton | 7da9b10 | 2015-09-10 20:57:14 -0700 | [diff] [blame] | 658 | let cname = match attr::first_attr_value_str_by_name(&i.attrs, |
| 659 | "link_name") { |
| 660 | Some(ref i) if !i.to_string().contains("$") => { |
| 661 | i.to_string() |
| 662 | } |
| 663 | _ => i.ident.to_string(), |
| 664 | }; |
Alex Crichton | 3150484 | 2015-09-10 23:43:41 -0700 | [diff] [blame] | 665 | let abi = self.abi; |
Alex Crichton | 7da9b10 | 2015-09-10 20:57:14 -0700 | [diff] [blame] | 666 | self.test_extern_fn(&i.ident.to_string(), &cname, &args, &ret, |
Alex Crichton | 3150484 | 2015-09-10 23:43:41 -0700 | [diff] [blame] | 667 | variadic, abi); |
Alex Crichton | 2237882 | 2015-09-10 19:59:23 -0700 | [diff] [blame] | 668 | } |
| 669 | ast::ForeignItemStatic(_, _) => { |
| 670 | } |
| 671 | } |
| 672 | visit::walk_foreign_item(self, i) |
| 673 | } |
Alex Crichton | 8e5f0cd | 2015-09-09 22:46:19 -0700 | [diff] [blame] | 674 | } |
Alex Crichton | 310d623 | 2015-09-10 10:56:31 -0700 | [diff] [blame] | 675 | |
| 676 | impl<'v> Visitor<'v> for StructFinder { |
Alex Crichton | 2237882 | 2015-09-10 19:59:23 -0700 | [diff] [blame] | 677 | fn visit_item(&mut self, i: &'v ast::Item) { |
| 678 | match i.node { |
| 679 | ast::ItemStruct(..) => { |
| 680 | self.structs.insert(i.ident.to_string()); |
| 681 | } |
| 682 | ast::ItemEnum(..) => { |
| 683 | self.structs.insert(i.ident.to_string()); |
| 684 | } |
Alex Crichton | 310d623 | 2015-09-10 10:56:31 -0700 | [diff] [blame] | 685 | |
Alex Crichton | 2237882 | 2015-09-10 19:59:23 -0700 | [diff] [blame] | 686 | _ => {} |
| 687 | } |
| 688 | visit::walk_item(self, i) |
| 689 | } |
Alex Crichton | 310d623 | 2015-09-10 10:56:31 -0700 | [diff] [blame] | 690 | } |