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