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