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::*; |
Alex Crichton | 0df7c10 | 2015-09-10 16:35:37 -0700 | [diff] [blame] | 9 | use std::iter; |
Alex Crichton | 8e5f0cd | 2015-09-09 22:46:19 -0700 | [diff] [blame] | 10 | use std::path::{Path, PathBuf}; |
| 11 | |
| 12 | use syntax::ast; |
Alex Crichton | a9adfbf | 2015-09-09 23:21:27 -0700 | [diff] [blame] | 13 | use syntax::diagnostic::SpanHandler; |
Alex Crichton | 8e5f0cd | 2015-09-09 22:46:19 -0700 | [diff] [blame] | 14 | use syntax::parse::token::InternedString; |
Alex Crichton | a9adfbf | 2015-09-09 23:21:27 -0700 | [diff] [blame] | 15 | use syntax::attr::{self, ReprAttr}; |
Alex Crichton | 8e5f0cd | 2015-09-09 22:46:19 -0700 | [diff] [blame] | 16 | use syntax::parse::{self, ParseSess}; |
| 17 | use syntax::visit::{self, Visitor}; |
| 18 | |
Alex Crichton | 310d623 | 2015-09-10 10:56:31 -0700 | [diff] [blame] | 19 | macro_rules! t { |
| 20 | ($e:expr) => (match $e { |
| 21 | Ok(e) => e, |
| 22 | Err(e) => panic!("{} failed with {}", stringify!($e), e), |
| 23 | }) |
| 24 | } |
| 25 | |
Alex Crichton | a9adfbf | 2015-09-09 23:21:27 -0700 | [diff] [blame] | 26 | struct TestGenerator<'a> { |
Alex Crichton | 310d623 | 2015-09-10 10:56:31 -0700 | [diff] [blame] | 27 | target: String, |
Alex Crichton | 8e5f0cd | 2015-09-09 22:46:19 -0700 | [diff] [blame] | 28 | rust: Box<Write>, |
| 29 | c: Box<Write>, |
Alex Crichton | a9adfbf | 2015-09-09 23:21:27 -0700 | [diff] [blame] | 30 | sh: &'a SpanHandler, |
Alex Crichton | 310d623 | 2015-09-10 10:56:31 -0700 | [diff] [blame] | 31 | structs: HashSet<String>, |
| 32 | } |
| 33 | |
| 34 | struct StructFinder { |
| 35 | structs: HashSet<String>, |
| 36 | } |
| 37 | |
| 38 | impl<'a> TestGenerator<'a> { |
Alex Crichton | 0df7c10 | 2015-09-10 16:35:37 -0700 | [diff] [blame] | 39 | fn defines(&self) -> Vec<&'static str> { |
| 40 | let mut ret = Vec::new(); |
| 41 | if self.target.contains("unknown-linux") { |
| 42 | ret.push("_GNU_SOURCE"); |
| 43 | } |
Alex Crichton | ac2bd85 | 2015-09-10 17:21:20 -0700 | [diff] [blame] | 44 | if self.target.contains("msvc") { |
| 45 | ret.push("alignof __alignof"); |
| 46 | } |
Alex Crichton | 0df7c10 | 2015-09-10 16:35:37 -0700 | [diff] [blame] | 47 | return ret |
| 48 | } |
| 49 | |
Alex Crichton | 310d623 | 2015-09-10 10:56:31 -0700 | [diff] [blame] | 50 | fn headers(&self) -> Vec<&'static str> { |
Alex Crichton | ac2bd85 | 2015-09-10 17:21:20 -0700 | [diff] [blame] | 51 | let mut base = Vec::new(); |
| 52 | |
| 53 | if self.target.contains("windows") { |
| 54 | base.push("windows.h"); |
| 55 | } |
| 56 | |
| 57 | base.extend(&[ |
Alex Crichton | 0df7c10 | 2015-09-10 16:35:37 -0700 | [diff] [blame] | 58 | "errno.h", |
| 59 | "fcntl.h", |
Alex Crichton | 0df7c10 | 2015-09-10 16:35:37 -0700 | [diff] [blame] | 60 | "limits.h", |
Alex Crichton | 310d623 | 2015-09-10 10:56:31 -0700 | [diff] [blame] | 61 | "stddef.h", |
| 62 | "stdint.h", |
Alex Crichton | 0df7c10 | 2015-09-10 16:35:37 -0700 | [diff] [blame] | 63 | "stdio.h", |
| 64 | "stdlib.h", |
Alex Crichton | 310d623 | 2015-09-10 10:56:31 -0700 | [diff] [blame] | 65 | "sys/stat.h", |
Alex Crichton | 310d623 | 2015-09-10 10:56:31 -0700 | [diff] [blame] | 66 | "sys/types.h", |
Alex Crichton | 310d623 | 2015-09-10 10:56:31 -0700 | [diff] [blame] | 67 | "time.h", |
Alex Crichton | 310d623 | 2015-09-10 10:56:31 -0700 | [diff] [blame] | 68 | "wchar.h", |
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 | |
| 71 | if self.target.contains("apple-darwin") { |
| 72 | base.push("mach/mach_time.h"); |
| 73 | } |
Alex Crichton | ac2bd85 | 2015-09-10 17:21:20 -0700 | [diff] [blame] | 74 | |
Alex Crichton | 310d623 | 2015-09-10 10:56:31 -0700 | [diff] [blame] | 75 | if self.target.contains("unknown-linux") { |
| 76 | base.push("linux/if_packet.h"); |
| 77 | base.push("net/ethernet.h"); |
| 78 | } |
| 79 | |
Alex Crichton | ac2bd85 | 2015-09-10 17:21:20 -0700 | [diff] [blame] | 80 | if self.target.contains("windows") { |
| 81 | base.push("ws2tcpip.h"); |
| 82 | } else { |
| 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"); |
| 93 | base.push("sys/mman.h"); |
| 94 | base.push("sys/resource.h"); |
| 95 | base.push("sys/socket.h"); |
| 96 | base.push("sys/time.h"); |
| 97 | base.push("sys/un.h"); |
| 98 | base.push("utime.h"); |
| 99 | base.push("unistd.h"); |
| 100 | } |
| 101 | |
Alex Crichton | 310d623 | 2015-09-10 10:56:31 -0700 | [diff] [blame] | 102 | return base |
| 103 | } |
| 104 | |
| 105 | fn rust2c(&self, ty: &str) -> String { |
| 106 | match ty { |
| 107 | t if t.starts_with("c_") => { |
| 108 | match &ty[2..].replace("long", " long")[..] { |
| 109 | s if s.starts_with("u") => format!("unsigned {}", &s[1..]), |
| 110 | "short" => format!("short"), |
| 111 | s if s.starts_with("s") => format!("signed {}", &s[1..]), |
| 112 | s => s.to_string(), |
| 113 | } |
| 114 | } |
| 115 | "ip6_mreq" => "struct ipv6_mreq".to_string(), |
| 116 | "glob_t" => "glob_t".to_string(), |
| 117 | t if t.starts_with("pthread") => t.to_string(), |
| 118 | |
| 119 | t if self.structs.contains(t) => format!("struct {}", t), |
| 120 | |
| 121 | t => t.to_string(), |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | fn rust2cfield(&self, struct_: &str, field: &str) -> String { |
| 126 | match field { |
| 127 | s if s.ends_with("_nsec") && struct_ == "stat" => { |
| 128 | if self.target.contains("apple-darwin") { |
| 129 | s.replace("_nsec", "spec.tv_nsec") |
| 130 | } else { |
| 131 | s.replace("e_nsec", ".tv_nsec") |
| 132 | } |
| 133 | } |
| 134 | s => s.to_string(), |
| 135 | } |
| 136 | } |
| 137 | |
| 138 | fn cfg_list(&self) -> Vec<(&'static str, Option<&'static str>)> { |
| 139 | let mut ret = Vec::new(); |
| 140 | let (arch, target_pointer_width) = if self.target.starts_with("x86_64") { |
| 141 | ("x86_64", "64") |
| 142 | } else if self.target.starts_with("i686") { |
| 143 | ("x86", "32") |
| 144 | } else { |
| 145 | panic!("unknown arch/pointer width: {}", self.target) |
| 146 | }; |
| 147 | let (os, family, env) = if self.target.contains("unknown-linux") { |
| 148 | ("linux", "unix", "gnu") |
| 149 | } else if self.target.contains("apple-darwin") { |
| 150 | ("macos", "unix", "") |
Alex Crichton | ac2bd85 | 2015-09-10 17:21:20 -0700 | [diff] [blame] | 151 | } else if self.target.contains("windows-msvc") { |
| 152 | ("windows", "windows", "msvc") |
| 153 | } else if self.target.contains("windows-gnu") { |
| 154 | ("windows", "windows", "gnu") |
Alex Crichton | 310d623 | 2015-09-10 10:56:31 -0700 | [diff] [blame] | 155 | } else { |
| 156 | panic!("unknown os/family width: {}", self.target) |
| 157 | }; |
| 158 | |
| 159 | ret.push((family, None)); |
| 160 | ret.push(("target_os", Some(os))); |
| 161 | ret.push(("target_family", Some(family))); |
| 162 | ret.push(("target_arch", Some(arch))); |
| 163 | // skip endianness |
| 164 | ret.push(("target_pointer_width", Some(target_pointer_width))); |
| 165 | ret.push(("target_env", Some(env))); |
| 166 | |
| 167 | return ret |
| 168 | } |
Alex Crichton | 8e5f0cd | 2015-09-09 22:46:19 -0700 | [diff] [blame] | 169 | } |
| 170 | |
| 171 | fn main() { |
Alex Crichton | 310d623 | 2015-09-10 10:56:31 -0700 | [diff] [blame] | 172 | // Prep the test generator |
| 173 | let target = t!(env::var("TARGET")); |
| 174 | let out = PathBuf::from(env::var_os("OUT_DIR").unwrap()); |
| 175 | let rust_out = BufWriter::new(t!(File::create(out.join("all.rs")))); |
| 176 | let c_out = BufWriter::new(t!(File::create(out.join("all.c")))); |
Alex Crichton | 1608306 | 2015-09-09 22:59:24 -0700 | [diff] [blame] | 177 | let sess = ParseSess::new(); |
Alex Crichton | 310d623 | 2015-09-10 10:56:31 -0700 | [diff] [blame] | 178 | let mut tg = TestGenerator { |
| 179 | target: target, |
| 180 | rust: Box::new(rust_out), |
| 181 | c: Box::new(c_out), |
| 182 | sh: &sess.span_diagnostic, |
| 183 | structs: HashSet::new(), |
| 184 | }; |
| 185 | |
| 186 | // Parse the libc crate |
Alex Crichton | 8e5f0cd | 2015-09-09 22:46:19 -0700 | [diff] [blame] | 187 | let src = Path::new("../src/lib.rs"); |
| 188 | let cfg = Vec::new(); |
| 189 | let mut krate = parse::parse_crate_from_file(src, cfg, &sess); |
Alex Crichton | 8e5f0cd | 2015-09-09 22:46:19 -0700 | [diff] [blame] | 190 | |
Alex Crichton | 310d623 | 2015-09-10 10:56:31 -0700 | [diff] [blame] | 191 | // Strip the crate down to just what's configured for our target |
| 192 | for (k, v) in tg.cfg_list() { |
| 193 | let s = InternedString::new; |
| 194 | krate.config.push(match v { |
| 195 | Some(v) => attr::mk_name_value_item_str(s(k), s(v)), |
| 196 | None => attr::mk_word_item(s(k)), |
| 197 | }); |
| 198 | } |
Alex Crichton | 8e5f0cd | 2015-09-09 22:46:19 -0700 | [diff] [blame] | 199 | let mut gated_cfgs = Vec::new(); |
| 200 | let krate = syntax::config::strip_unconfigured_items(&sess.span_diagnostic, |
| 201 | krate, |
| 202 | &mut gated_cfgs); |
| 203 | |
Alex Crichton | 310d623 | 2015-09-10 10:56:31 -0700 | [diff] [blame] | 204 | // Probe the crate to find all structs (used to convert type names to names |
| 205 | // in C). |
| 206 | let mut structs = StructFinder { |
| 207 | structs: HashSet::new(), |
| 208 | }; |
| 209 | visit::walk_crate(&mut structs, &krate); |
| 210 | tg.structs = structs.structs; |
Alex Crichton | 8e5f0cd | 2015-09-09 22:46:19 -0700 | [diff] [blame] | 211 | |
Alex Crichton | 0df7c10 | 2015-09-10 16:35:37 -0700 | [diff] [blame] | 212 | // Prep the C file by emitting header stuff |
| 213 | for define in tg.defines() { |
| 214 | t!(writeln!(tg.c, "#define {}", define)); |
| 215 | } |
Alex Crichton | 310d623 | 2015-09-10 10:56:31 -0700 | [diff] [blame] | 216 | for header in tg.headers() { |
| 217 | t!(writeln!(tg.c, "#include <{}>", header)); |
Alex Crichton | 1608306 | 2015-09-09 22:59:24 -0700 | [diff] [blame] | 218 | } |
Alex Crichton | 0df7c10 | 2015-09-10 16:35:37 -0700 | [diff] [blame] | 219 | |
| 220 | // Walk the crate, emitting test cases for everything found |
Alex Crichton | 310d623 | 2015-09-10 10:56:31 -0700 | [diff] [blame] | 221 | visit::walk_crate(&mut tg, &krate); |
Alex Crichton | 1608306 | 2015-09-09 22:59:24 -0700 | [diff] [blame] | 222 | |
Alex Crichton | 310d623 | 2015-09-10 10:56:31 -0700 | [diff] [blame] | 223 | // Compile our C shim to be linked into tests |
Alex Crichton | ac2bd85 | 2015-09-10 17:21:20 -0700 | [diff] [blame] | 224 | let mut cfg = gcc::Config::new(); |
| 225 | cfg.file(out.join("all.c")); |
| 226 | |
| 227 | if tg.target.contains("msvc") { |
| 228 | cfg.flag("/W3").flag("/Wall").flag("/WX"); |
| 229 | } else { |
Alex Crichton | b01a8cc | 2015-09-10 17:37:22 -0700 | [diff] [blame] | 230 | cfg.flag("-Wall").flag("-Wextra").flag("-Werror") |
| 231 | .arg("-Wno-unused-parameter"); |
Alex Crichton | ac2bd85 | 2015-09-10 17:21:20 -0700 | [diff] [blame] | 232 | } |
| 233 | |
| 234 | drop(tg); |
| 235 | cfg.compile("liball.a"); |
Alex Crichton | 8e5f0cd | 2015-09-09 22:46:19 -0700 | [diff] [blame] | 236 | } |
| 237 | |
Alex Crichton | a9adfbf | 2015-09-09 23:21:27 -0700 | [diff] [blame] | 238 | impl<'a> TestGenerator<'a> { |
| 239 | fn test_type(&mut self, ty: &str) { |
Alex Crichton | 310d623 | 2015-09-10 10:56:31 -0700 | [diff] [blame] | 240 | match ty { |
| 241 | "sighandler_t" => return, |
| 242 | _ => {} |
| 243 | } |
| 244 | let c = self.rust2c(ty); |
| 245 | self.test_size_align(ty, &c); |
Alex Crichton | a9adfbf | 2015-09-09 23:21:27 -0700 | [diff] [blame] | 246 | } |
Alex Crichton | 1608306 | 2015-09-09 22:59:24 -0700 | [diff] [blame] | 247 | |
Alex Crichton | 3e5155b | 2015-09-09 23:46:19 -0700 | [diff] [blame] | 248 | fn test_struct(&mut self, ty: &str, s: &ast::StructDef) { |
Alex Crichton | 310d623 | 2015-09-10 10:56:31 -0700 | [diff] [blame] | 249 | let cty = self.rust2c(ty); |
Alex Crichton | a9adfbf | 2015-09-09 23:21:27 -0700 | [diff] [blame] | 250 | self.test_size_align(ty, &cty); |
Alex Crichton | 3e5155b | 2015-09-09 23:46:19 -0700 | [diff] [blame] | 251 | |
Alex Crichton | 310d623 | 2015-09-10 10:56:31 -0700 | [diff] [blame] | 252 | t!(writeln!(self.rust, r#" |
Alex Crichton | 3e5155b | 2015-09-09 23:46:19 -0700 | [diff] [blame] | 253 | #[test] |
| 254 | fn field_offset_size_{ty}() {{ |
Alex Crichton | 310d623 | 2015-09-10 10:56:31 -0700 | [diff] [blame] | 255 | "#, ty = ty)); |
Alex Crichton | 3e5155b | 2015-09-09 23:46:19 -0700 | [diff] [blame] | 256 | for field in s.fields.iter() { |
| 257 | let name = match field.node.kind { |
| 258 | ast::NamedField(name, ast::Public) => name, |
| 259 | ast::NamedField(_, ast::Inherited) => continue, |
| 260 | ast::UnnamedField(..) => panic!("no tuple structs in FFI"), |
| 261 | }; |
| 262 | |
Alex Crichton | 310d623 | 2015-09-10 10:56:31 -0700 | [diff] [blame] | 263 | let cfield = self.rust2cfield(ty, &name.to_string()); |
Alex Crichton | 3e5155b | 2015-09-09 23:46:19 -0700 | [diff] [blame] | 264 | |
Alex Crichton | 310d623 | 2015-09-10 10:56:31 -0700 | [diff] [blame] | 265 | t!(writeln!(self.c, r#" |
Alex Crichton | 671376b | 2015-09-10 17:39:03 -0700 | [diff] [blame^] | 266 | uint64_t __test_offset_{ty}_{rust_field}(void) {{ |
Alex Crichton | 3e5155b | 2015-09-09 23:46:19 -0700 | [diff] [blame] | 267 | return offsetof({cty}, {c_field}); |
| 268 | }} |
Alex Crichton | 671376b | 2015-09-10 17:39:03 -0700 | [diff] [blame^] | 269 | uint64_t __test_size_{ty}_{rust_field}(void) {{ |
Alex Crichton | 3e5155b | 2015-09-09 23:46:19 -0700 | [diff] [blame] | 270 | {cty}* foo = NULL; |
| 271 | return sizeof(foo->{c_field}); |
| 272 | }} |
Alex Crichton | 310d623 | 2015-09-10 10:56:31 -0700 | [diff] [blame] | 273 | "#, ty = ty, cty = cty, rust_field = name, c_field = cfield)); |
| 274 | t!(writeln!(self.rust, r#" |
Alex Crichton | 3e5155b | 2015-09-09 23:46:19 -0700 | [diff] [blame] | 275 | extern {{ |
| 276 | fn __test_offset_{ty}_{field}() -> u64; |
| 277 | fn __test_size_{ty}_{field}() -> u64; |
| 278 | }} |
| 279 | unsafe {{ |
| 280 | let foo = 0 as *const {ty}; |
| 281 | same(offset_of!({ty}, {field}), |
| 282 | __test_offset_{ty}_{field}(), |
| 283 | "field offset {field} of {ty}"); |
| 284 | same(mem::size_of_val(&(*foo).{field}) as u64, |
| 285 | __test_size_{ty}_{field}(), |
| 286 | "field size {field} of {ty}"); |
| 287 | }} |
Alex Crichton | 310d623 | 2015-09-10 10:56:31 -0700 | [diff] [blame] | 288 | "#, ty = ty, field = name)); |
Alex Crichton | 3e5155b | 2015-09-09 23:46:19 -0700 | [diff] [blame] | 289 | } |
Alex Crichton | 310d623 | 2015-09-10 10:56:31 -0700 | [diff] [blame] | 290 | t!(writeln!(self.rust, r#" |
Alex Crichton | 3e5155b | 2015-09-09 23:46:19 -0700 | [diff] [blame] | 291 | }} |
Alex Crichton | 310d623 | 2015-09-10 10:56:31 -0700 | [diff] [blame] | 292 | "#)); |
Alex Crichton | a9adfbf | 2015-09-09 23:21:27 -0700 | [diff] [blame] | 293 | } |
| 294 | |
| 295 | fn test_size_align(&mut self, rust: &str, c: &str) { |
Alex Crichton | 310d623 | 2015-09-10 10:56:31 -0700 | [diff] [blame] | 296 | t!(writeln!(self.c, r#" |
Alex Crichton | 671376b | 2015-09-10 17:39:03 -0700 | [diff] [blame^] | 297 | uint64_t __test_size_{ty}(void) {{ return sizeof({cty}); }} |
| 298 | uint64_t __test_align_{ty}(void) {{ return alignof({cty}); }} |
Alex Crichton | 310d623 | 2015-09-10 10:56:31 -0700 | [diff] [blame] | 299 | "#, ty = rust, cty = c)); |
| 300 | t!(writeln!(self.rust, r#" |
Alex Crichton | 8e5f0cd | 2015-09-09 22:46:19 -0700 | [diff] [blame] | 301 | #[test] |
Alex Crichton | a9adfbf | 2015-09-09 23:21:27 -0700 | [diff] [blame] | 302 | fn size_align_{ty}() {{ |
Alex Crichton | 1608306 | 2015-09-09 22:59:24 -0700 | [diff] [blame] | 303 | extern {{ |
Alex Crichton | a9adfbf | 2015-09-09 23:21:27 -0700 | [diff] [blame] | 304 | fn __test_size_{ty}() -> u64; |
| 305 | fn __test_align_{ty}() -> u64; |
Alex Crichton | 1608306 | 2015-09-09 22:59:24 -0700 | [diff] [blame] | 306 | }} |
| 307 | unsafe {{ |
Alex Crichton | 3e5155b | 2015-09-09 23:46:19 -0700 | [diff] [blame] | 308 | same(mem::size_of::<{ty}>() as u64, |
| 309 | __test_size_{ty}(), "size"); |
Alex Crichton | c8b895c | 2015-09-10 13:24:15 -0700 | [diff] [blame] | 310 | same(align::<{ty}>() as u64, |
Alex Crichton | 3e5155b | 2015-09-09 23:46:19 -0700 | [diff] [blame] | 311 | __test_align_{ty}(), "align"); |
Alex Crichton | 1608306 | 2015-09-09 22:59:24 -0700 | [diff] [blame] | 312 | }} |
Alex Crichton | 8e5f0cd | 2015-09-09 22:46:19 -0700 | [diff] [blame] | 313 | }} |
Alex Crichton | 310d623 | 2015-09-10 10:56:31 -0700 | [diff] [blame] | 314 | "#, ty = rust)); |
Alex Crichton | a9adfbf | 2015-09-09 23:21:27 -0700 | [diff] [blame] | 315 | } |
| 316 | |
Alex Crichton | 0df7c10 | 2015-09-10 16:35:37 -0700 | [diff] [blame] | 317 | fn test_const(&mut self, name: &str, rust_ty: &str) { |
| 318 | let cty = self.rust2c(&rust_ty.replace("*mut ", "") |
| 319 | .replace("*const ", "")); |
| 320 | let ptrs = rust_ty.matches("*").count(); |
| 321 | let cty = format!("{}{}", cty, |
| 322 | iter::repeat("*").take(ptrs).collect::<String>()); |
| 323 | let cast = if name == "SIG_IGN" {"(size_t)"} else {""}; |
| 324 | t!(writeln!(self.c, r#" |
Alex Crichton | e7afdd8 | 2015-09-10 17:15:20 -0700 | [diff] [blame] | 325 | int __test_const_{name}({cty} *out) {{ |
| 326 | int ret = 0; |
| 327 | #if defined({name}) |
| 328 | *out = {cast}({name}); |
| 329 | ret = 1; |
| 330 | #endif |
| 331 | return ret; |
| 332 | }} |
Alex Crichton | 0df7c10 | 2015-09-10 16:35:37 -0700 | [diff] [blame] | 333 | "#, name = name, cast = cast, cty = cty)); |
| 334 | t!(writeln!(self.rust, r#" |
| 335 | #[test] |
| 336 | fn const_{name}() {{ |
| 337 | extern {{ |
Alex Crichton | e7afdd8 | 2015-09-10 17:15:20 -0700 | [diff] [blame] | 338 | fn __test_const_{name}(out: *mut {ty}) -> c_int; |
Alex Crichton | 0df7c10 | 2015-09-10 16:35:37 -0700 | [diff] [blame] | 339 | }} |
| 340 | unsafe {{ |
Alex Crichton | e7afdd8 | 2015-09-10 17:15:20 -0700 | [diff] [blame] | 341 | let mut o = mem::zeroed(); |
| 342 | if __test_const_{name}(&mut o) == 0 {{ |
| 343 | panic!("not defined"); |
| 344 | }} else {{ |
| 345 | same({name}, o, "value"); |
| 346 | }} |
Alex Crichton | 0df7c10 | 2015-09-10 16:35:37 -0700 | [diff] [blame] | 347 | }} |
| 348 | }} |
| 349 | "#, ty = rust_ty, name = name)); |
| 350 | } |
| 351 | |
Alex Crichton | a9adfbf | 2015-09-09 23:21:27 -0700 | [diff] [blame] | 352 | fn assert_no_generics(&self, _i: &ast::Item, generics: &ast::Generics) { |
| 353 | assert!(generics.lifetimes.len() == 0); |
| 354 | assert!(generics.ty_params.len() == 0); |
| 355 | assert!(generics.where_clause.predicates.len() == 0); |
Alex Crichton | 8e5f0cd | 2015-09-09 22:46:19 -0700 | [diff] [blame] | 356 | } |
Alex Crichton | 0df7c10 | 2015-09-10 16:35:37 -0700 | [diff] [blame] | 357 | |
| 358 | fn ty2name(&self, ty: &ast::Ty) -> String { |
| 359 | match ty.node { |
| 360 | ast::TyPath(_, ref path) => { |
| 361 | path.segments.last().unwrap().identifier.to_string() |
| 362 | } |
| 363 | ast::TyPtr(ref t) => { |
| 364 | format!("*{} {}", match t.mutbl { |
| 365 | ast::MutImmutable => "const", |
| 366 | ast::MutMutable => "mut", |
| 367 | }, self.ty2name(&t.ty)) |
| 368 | } |
| 369 | _ => panic!("unknown ty {:?}", ty), |
| 370 | } |
| 371 | } |
Alex Crichton | 8e5f0cd | 2015-09-09 22:46:19 -0700 | [diff] [blame] | 372 | } |
| 373 | |
Alex Crichton | a9adfbf | 2015-09-09 23:21:27 -0700 | [diff] [blame] | 374 | impl<'a, 'v> Visitor<'v> for TestGenerator<'a> { |
Alex Crichton | 8e5f0cd | 2015-09-09 22:46:19 -0700 | [diff] [blame] | 375 | fn visit_item(&mut self, i: &'v ast::Item) { |
| 376 | match i.node { |
| 377 | ast::ItemTy(_, ref generics) => { |
Alex Crichton | a9adfbf | 2015-09-09 23:21:27 -0700 | [diff] [blame] | 378 | self.assert_no_generics(i, generics); |
| 379 | self.test_type(&i.ident.to_string()); |
| 380 | } |
| 381 | |
| 382 | ast::ItemStruct(ref s, ref generics) => { |
| 383 | self.assert_no_generics(i, generics); |
| 384 | let is_c = i.attrs.iter().any(|a| { |
| 385 | attr::find_repr_attrs(self.sh, a).iter().any(|a| { |
| 386 | *a == ReprAttr::ReprExtern |
| 387 | }) |
| 388 | }); |
| 389 | if !is_c { |
| 390 | panic!("{} is not marked #[repr(C)]", i.ident); |
| 391 | } |
| 392 | self.test_struct(&i.ident.to_string(), s); |
Alex Crichton | 8e5f0cd | 2015-09-09 22:46:19 -0700 | [diff] [blame] | 393 | } |
| 394 | |
Alex Crichton | 0df7c10 | 2015-09-10 16:35:37 -0700 | [diff] [blame] | 395 | ast::ItemConst(ref ty, _) => { |
| 396 | let ty = self.ty2name(ty); |
| 397 | self.test_const(&i.ident.to_string(), &ty); |
| 398 | } |
| 399 | |
Alex Crichton | 8e5f0cd | 2015-09-09 22:46:19 -0700 | [diff] [blame] | 400 | _ => {} |
| 401 | } |
| 402 | visit::walk_item(self, i) |
| 403 | } |
| 404 | } |
Alex Crichton | 310d623 | 2015-09-10 10:56:31 -0700 | [diff] [blame] | 405 | |
| 406 | impl<'v> Visitor<'v> for StructFinder { |
| 407 | fn visit_item(&mut self, i: &'v ast::Item) { |
| 408 | match i.node { |
| 409 | ast::ItemStruct(..) => { |
| 410 | self.structs.insert(i.ident.to_string()); |
| 411 | } |
| 412 | |
| 413 | _ => {} |
| 414 | } |
| 415 | visit::walk_item(self, i) |
| 416 | } |
| 417 | } |