blob: 4b64473b92b8170c52d4bdd4b20b20dc8bfcd9a4 [file] [log] [blame]
Alex Crichton8e5f0cd2015-09-09 22:46:19 -07001extern crate gcc;
2extern crate syntex_syntax as syntax;
3
Alex Crichton310d6232015-09-10 10:56:31 -07004use std::collections::HashSet;
Alex Crichton8e5f0cd2015-09-09 22:46:19 -07005use std::env;
6use std::fs::File;
7use std::io::BufWriter;
8use std::io::prelude::*;
9use std::path::{Path, PathBuf};
10
11use syntax::ast;
Alex Crichtona9adfbf2015-09-09 23:21:27 -070012use syntax::diagnostic::SpanHandler;
Alex Crichton8e5f0cd2015-09-09 22:46:19 -070013use syntax::parse::token::InternedString;
Alex Crichtona9adfbf2015-09-09 23:21:27 -070014use syntax::attr::{self, ReprAttr};
Alex Crichton8e5f0cd2015-09-09 22:46:19 -070015use syntax::parse::{self, ParseSess};
16use syntax::visit::{self, Visitor};
17
Alex Crichton310d6232015-09-10 10:56:31 -070018macro_rules! t {
19 ($e:expr) => (match $e {
20 Ok(e) => e,
21 Err(e) => panic!("{} failed with {}", stringify!($e), e),
22 })
23}
24
Alex Crichtona9adfbf2015-09-09 23:21:27 -070025struct TestGenerator<'a> {
Alex Crichton310d6232015-09-10 10:56:31 -070026 target: String,
Alex Crichton8e5f0cd2015-09-09 22:46:19 -070027 rust: Box<Write>,
28 c: Box<Write>,
Alex Crichtona9adfbf2015-09-09 23:21:27 -070029 sh: &'a SpanHandler,
Alex Crichton310d6232015-09-10 10:56:31 -070030 structs: HashSet<String>,
31}
32
33struct StructFinder {
34 structs: HashSet<String>,
35}
36
37impl<'a> TestGenerator<'a> {
Alex Crichton0df7c102015-09-10 16:35:37 -070038 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 Crichtonac2bd852015-09-10 17:21:20 -070043 if self.target.contains("msvc") {
44 ret.push("alignof __alignof");
45 }
Alex Crichton0df7c102015-09-10 16:35:37 -070046 return ret
47 }
48
Alex Crichton310d6232015-09-10 10:56:31 -070049 fn headers(&self) -> Vec<&'static str> {
Alex Crichtonac2bd852015-09-10 17:21:20 -070050 let mut base = Vec::new();
51
Alex Crichtonac2bd852015-09-10 17:21:20 -070052 base.extend(&[
Alex Crichton0df7c102015-09-10 16:35:37 -070053 "errno.h",
54 "fcntl.h",
Alex Crichton0df7c102015-09-10 16:35:37 -070055 "limits.h",
Alex Crichton310d6232015-09-10 10:56:31 -070056 "stddef.h",
57 "stdint.h",
Alex Crichton0df7c102015-09-10 16:35:37 -070058 "stdio.h",
59 "stdlib.h",
Alex Crichton310d6232015-09-10 10:56:31 -070060 "sys/stat.h",
Alex Crichton310d6232015-09-10 10:56:31 -070061 "sys/types.h",
Alex Crichton310d6232015-09-10 10:56:31 -070062 "time.h",
Alex Crichton310d6232015-09-10 10:56:31 -070063 "wchar.h",
Alex Crichtonac2bd852015-09-10 17:21:20 -070064 ]);
Alex Crichton310d6232015-09-10 10:56:31 -070065
66 if self.target.contains("apple-darwin") {
67 base.push("mach/mach_time.h");
68 }
Alex Crichtonac2bd852015-09-10 17:21:20 -070069
Alex Crichton310d6232015-09-10 10:56:31 -070070 if self.target.contains("unknown-linux") {
71 base.push("linux/if_packet.h");
72 base.push("net/ethernet.h");
73 }
74
Alex Crichtonac2bd852015-09-10 17:21:20 -070075 if self.target.contains("windows") {
Alex Crichton13a6f2d2015-09-10 18:10:58 -070076 base.push("winsock2.h");
77 base.push("ws2ipdef.h");
78 base.push("windows.h");
79 base.push("sys/utime.h");
Alex Crichtonac2bd852015-09-10 17:21:20 -070080 } else {
Alex Crichton22378822015-09-10 19:59:23 -070081 base.push("ctype.h");
82 base.push("dirent.h");
Alex Crichtonac2bd852015-09-10 17:21:20 -070083 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 Crichton22378822015-09-10 19:59:23 -070093 base.push("sys/file.h");
94 base.push("sys/ioctl.h");
Alex Crichtonac2bd852015-09-10 17:21:20 -070095 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 Crichton22378822015-09-10 19:59:23 -0700100 base.push("sys/wait.h");
Alex Crichtonac2bd852015-09-10 17:21:20 -0700101 base.push("unistd.h");
Alex Crichton22378822015-09-10 19:59:23 -0700102 base.push("utime.h");
Alex Crichtonac2bd852015-09-10 17:21:20 -0700103 }
104
Alex Crichton310d6232015-09-10 10:56:31 -0700105 return base
106 }
107
108 fn rust2c(&self, ty: &str) -> String {
Alex Crichton13a6f2d2015-09-10 18:10:58 -0700109 let windows = self.target.contains("windows");
Alex Crichton310d6232015-09-10 10:56:31 -0700110 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 Crichton22378822015-09-10 19:59:23 -0700120 "glob_t" |
121 "FILE" |
122 "DIR" |
123 "fpos_t" => ty.to_string(),
Alex Crichton310d6232015-09-10 10:56:31 -0700124 t if t.starts_with("pthread") => t.to_string(),
125
Alex Crichton13a6f2d2015-09-10 18:10:58 -0700126 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 Crichton310d6232015-09-10 10:56:31 -0700135
Alex Crichton13a6f2d2015-09-10 18:10:58 -0700136 "time64_t" if windows => "__time64_t".to_string(),
137 "ssize_t" if windows => "SSIZE_T".to_string(),
Alex Crichton310d6232015-09-10 10:56:31 -0700138 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 Crichtonac2bd852015-09-10 17:21:20 -0700168 } else if self.target.contains("windows-msvc") {
169 ("windows", "windows", "msvc")
170 } else if self.target.contains("windows-gnu") {
171 ("windows", "windows", "gnu")
Alex Crichton310d6232015-09-10 10:56:31 -0700172 } 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 Crichton8e5f0cd2015-09-09 22:46:19 -0700186}
187
188fn main() {
Alex Crichton310d6232015-09-10 10:56:31 -0700189 // 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 Crichton16083062015-09-09 22:59:24 -0700194 let sess = ParseSess::new();
Alex Crichton310d6232015-09-10 10:56:31 -0700195 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 Crichton8e5f0cd2015-09-09 22:46:19 -0700204 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 Crichton8e5f0cd2015-09-09 22:46:19 -0700207
Alex Crichton310d6232015-09-10 10:56:31 -0700208 // 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 Crichton8e5f0cd2015-09-09 22:46:19 -0700216 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 Crichton310d6232015-09-10 10:56:31 -0700221 // 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 Crichton8e5f0cd2015-09-09 22:46:19 -0700228
Alex Crichton0df7c102015-09-10 16:35:37 -0700229 // Prep the C file by emitting header stuff
230 for define in tg.defines() {
231 t!(writeln!(tg.c, "#define {}", define));
232 }
Alex Crichton310d6232015-09-10 10:56:31 -0700233 for header in tg.headers() {
234 t!(writeln!(tg.c, "#include <{}>", header));
Alex Crichton16083062015-09-09 22:59:24 -0700235 }
Alex Crichton0df7c102015-09-10 16:35:37 -0700236
237 // Walk the crate, emitting test cases for everything found
Alex Crichton310d6232015-09-10 10:56:31 -0700238 visit::walk_crate(&mut tg, &krate);
Alex Crichton16083062015-09-09 22:59:24 -0700239
Alex Crichton310d6232015-09-10 10:56:31 -0700240 // Compile our C shim to be linked into tests
Alex Crichtonac2bd852015-09-10 17:21:20 -0700241 let mut cfg = gcc::Config::new();
242 cfg.file(out.join("all.c"));
243
244 if tg.target.contains("msvc") {
Alex Crichton13a6f2d2015-09-10 18:10:58 -0700245 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 Crichtonac2bd852015-09-10 17:21:20 -0700248 } else {
Alex Crichtonb01a8cc2015-09-10 17:37:22 -0700249 cfg.flag("-Wall").flag("-Wextra").flag("-Werror")
Alex Crichton5f624a52015-09-10 17:39:21 -0700250 .flag("-Wno-unused-parameter");
Alex Crichtonac2bd852015-09-10 17:21:20 -0700251 }
252
253 drop(tg);
254 cfg.compile("liball.a");
Alex Crichton8e5f0cd2015-09-09 22:46:19 -0700255}
256
Alex Crichtona9adfbf2015-09-09 23:21:27 -0700257impl<'a> TestGenerator<'a> {
258 fn test_type(&mut self, ty: &str) {
Alex Crichton310d6232015-09-10 10:56:31 -0700259 match ty {
260 "sighandler_t" => return,
261 _ => {}
262 }
Alex Crichton22378822015-09-10 19:59:23 -0700263 let c = self.rust_ty_to_c_ty(ty);
Alex Crichton310d6232015-09-10 10:56:31 -0700264 self.test_size_align(ty, &c);
Alex Crichtona9adfbf2015-09-09 23:21:27 -0700265 }
Alex Crichton16083062015-09-09 22:59:24 -0700266
Alex Crichton3e5155b2015-09-09 23:46:19 -0700267 fn test_struct(&mut self, ty: &str, s: &ast::StructDef) {
Alex Crichton22378822015-09-10 19:59:23 -0700268 let cty = self.rust_ty_to_c_ty(ty);
Alex Crichtona9adfbf2015-09-09 23:21:27 -0700269 self.test_size_align(ty, &cty);
Alex Crichton3e5155b2015-09-09 23:46:19 -0700270
Alex Crichton310d6232015-09-10 10:56:31 -0700271 t!(writeln!(self.rust, r#"
Alex Crichton3e5155b2015-09-09 23:46:19 -0700272 #[test]
273 fn field_offset_size_{ty}() {{
Alex Crichton310d6232015-09-10 10:56:31 -0700274 "#, ty = ty));
Alex Crichton3e5155b2015-09-09 23:46:19 -0700275 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 Crichton310d6232015-09-10 10:56:31 -0700282 let cfield = self.rust2cfield(ty, &name.to_string());
Alex Crichton3e5155b2015-09-09 23:46:19 -0700283
Alex Crichton310d6232015-09-10 10:56:31 -0700284 t!(writeln!(self.c, r#"
Alex Crichton671376b2015-09-10 17:39:03 -0700285 uint64_t __test_offset_{ty}_{rust_field}(void) {{
Alex Crichton3e5155b2015-09-09 23:46:19 -0700286 return offsetof({cty}, {c_field});
287 }}
Alex Crichton671376b2015-09-10 17:39:03 -0700288 uint64_t __test_size_{ty}_{rust_field}(void) {{
Alex Crichton3e5155b2015-09-09 23:46:19 -0700289 {cty}* foo = NULL;
290 return sizeof(foo->{c_field});
291 }}
Alex Crichton310d6232015-09-10 10:56:31 -0700292 "#, ty = ty, cty = cty, rust_field = name, c_field = cfield));
293 t!(writeln!(self.rust, r#"
Alex Crichton3e5155b2015-09-09 23:46:19 -0700294 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 Crichton310d6232015-09-10 10:56:31 -0700307 "#, ty = ty, field = name));
Alex Crichton3e5155b2015-09-09 23:46:19 -0700308 }
Alex Crichton310d6232015-09-10 10:56:31 -0700309 t!(writeln!(self.rust, r#"
Alex Crichton3e5155b2015-09-09 23:46:19 -0700310 }}
Alex Crichton310d6232015-09-10 10:56:31 -0700311 "#));
Alex Crichtona9adfbf2015-09-09 23:21:27 -0700312 }
313
314 fn test_size_align(&mut self, rust: &str, c: &str) {
Alex Crichton310d6232015-09-10 10:56:31 -0700315 t!(writeln!(self.c, r#"
Alex Crichton671376b2015-09-10 17:39:03 -0700316 uint64_t __test_size_{ty}(void) {{ return sizeof({cty}); }}
317 uint64_t __test_align_{ty}(void) {{ return alignof({cty}); }}
Alex Crichton310d6232015-09-10 10:56:31 -0700318 "#, ty = rust, cty = c));
319 t!(writeln!(self.rust, r#"
Alex Crichton8e5f0cd2015-09-09 22:46:19 -0700320 #[test]
Alex Crichtona9adfbf2015-09-09 23:21:27 -0700321 fn size_align_{ty}() {{
Alex Crichton16083062015-09-09 22:59:24 -0700322 extern {{
Alex Crichtona9adfbf2015-09-09 23:21:27 -0700323 fn __test_size_{ty}() -> u64;
324 fn __test_align_{ty}() -> u64;
Alex Crichton16083062015-09-09 22:59:24 -0700325 }}
326 unsafe {{
Alex Crichton3e5155b2015-09-09 23:46:19 -0700327 same(mem::size_of::<{ty}>() as u64,
328 __test_size_{ty}(), "size");
Alex Crichtonc8b895c2015-09-10 13:24:15 -0700329 same(align::<{ty}>() as u64,
Alex Crichton3e5155b2015-09-09 23:46:19 -0700330 __test_align_{ty}(), "align");
Alex Crichton16083062015-09-09 22:59:24 -0700331 }}
Alex Crichton8e5f0cd2015-09-09 22:46:19 -0700332 }}
Alex Crichton310d6232015-09-10 10:56:31 -0700333 "#, ty = rust));
Alex Crichtona9adfbf2015-09-09 23:21:27 -0700334 }
335
Alex Crichton22378822015-09-10 19:59:23 -0700336 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 Crichton0df7c102015-09-10 16:35:37 -0700351 fn test_const(&mut self, name: &str, rust_ty: &str) {
Alex Crichton22378822015-09-10 19:59:23 -0700352 let cty = self.rust_ty_to_c_ty(rust_ty);
Alex Crichton0df7c102015-09-10 16:35:37 -0700353 let cast = if name == "SIG_IGN" {"(size_t)"} else {""};
354 t!(writeln!(self.c, r#"
Alex Crichton13a6f2d2015-09-10 18:10:58 -0700355 int __test_const_{name}({cty} *outptr) {{
356 *outptr = {cast}({name});
357 return 1;
Alex Crichtone7afdd82015-09-10 17:15:20 -0700358 }}
Alex Crichton0df7c102015-09-10 16:35:37 -0700359 "#, name = name, cast = cast, cty = cty));
360 t!(writeln!(self.rust, r#"
361 #[test]
362 fn const_{name}() {{
363 extern {{
Alex Crichtone7afdd82015-09-10 17:15:20 -0700364 fn __test_const_{name}(out: *mut {ty}) -> c_int;
Alex Crichton0df7c102015-09-10 16:35:37 -0700365 }}
366 unsafe {{
Alex Crichtone7afdd82015-09-10 17:15:20 -0700367 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 Crichton0df7c102015-09-10 16:35:37 -0700373 }}
374 }}
375 "#, ty = rust_ty, name = name));
376 }
377
Alex Crichton22378822015-09-10 19:59:23 -0700378 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 Crichton8e5f0cd2015-09-09 22:46:19 -0700421 }
Alex Crichton0df7c102015-09-10 16:35:37 -0700422
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 Crichton22378822015-09-10 19:59:23 -0700434 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 Crichton0df7c102015-09-10 16:35:37 -0700443 _ => panic!("unknown ty {:?}", ty),
444 }
445 }
Alex Crichton22378822015-09-10 19:59:23 -0700446
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 Crichton8e5f0cd2015-09-09 22:46:19 -0700458}
459
Alex Crichtona9adfbf2015-09-09 23:21:27 -0700460impl<'a, 'v> Visitor<'v> for TestGenerator<'a> {
Alex Crichton22378822015-09-10 19:59:23 -0700461 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 Crichtona9adfbf2015-09-09 23:21:27 -0700467
Alex Crichton22378822015-09-10 19:59:23 -0700468 ast::ItemStruct(ref s, ref generics) => {
469 self.assert_no_generics(i.ident, generics);
470 let is_c = i.attrs.iter().any(|a| {
Alex Crichtona9adfbf2015-09-09 23:21:27 -0700471 attr::find_repr_attrs(self.sh, a).iter().any(|a| {
472 *a == ReprAttr::ReprExtern
473 })
Alex Crichton22378822015-09-10 19:59:23 -0700474 });
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 Crichton8e5f0cd2015-09-09 22:46:19 -0700480
Alex Crichton22378822015-09-10 19:59:23 -0700481 ast::ItemConst(ref ty, _) => {
482 let ty = self.ty2name(ty);
483 self.test_const(&i.ident.to_string(), &ty);
484 }
Alex Crichton0df7c102015-09-10 16:35:37 -0700485
Alex Crichton22378822015-09-10 19:59:23 -0700486 _ => {}
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 Crichton8e5f0cd2015-09-09 22:46:19 -0700504}
Alex Crichton310d6232015-09-10 10:56:31 -0700505
506impl<'v> Visitor<'v> for StructFinder {
Alex Crichton22378822015-09-10 19:59:23 -0700507 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 Crichton310d6232015-09-10 10:56:31 -0700515
Alex Crichton22378822015-09-10 19:59:23 -0700516 _ => {}
517 }
518 visit::walk_item(self, i)
519 }
Alex Crichton310d6232015-09-10 10:56:31 -0700520}