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