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