| 1 |
/* This Source Code Form is subject to the terms of the Mozilla Public |
| 2 |
* License, v. 2.0. If a copy of the MPL was not distributed with this |
| 3 |
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
| 4 |
|
| 5 |
#![allow(clippy::redundant_closure_call)] |
| 6 |
|
| 7 |
use syn::ext::IdentExt; |
| 8 |
|
| 9 |
pub trait IterHelpers: Iterator { |
| 10 |
fn try_skip_map<F, T, E>(&mut self, f: F) -> Result<Vec<T>, E> |
| 11 |
where |
| 12 |
F: FnMut(&Self::Item) -> Result<Option<T>, E>; |
| 13 |
} |
| 14 |
|
| 15 |
impl<I> IterHelpers for I |
| 16 |
where |
| 17 |
I: Iterator, |
| 18 |
{ |
| 19 |
fn try_skip_map<F, T, E>(&mut self, mut f: F) -> Result<Vec<T>, E> |
| 20 |
where |
| 21 |
F: FnMut(&Self::Item) -> Result<Option<T>, E>, |
| 22 |
{ |
| 23 |
let mut out = Vec::new(); |
| 24 |
for item in self { |
| 25 |
if let Some(x) = f(&item)? { |
| 26 |
out.push(x); |
| 27 |
} |
| 28 |
} |
| 29 |
Ok(out) |
| 30 |
} |
| 31 |
} |
| 32 |
|
| 33 |
pub trait SynItemFnHelpers: SynAttributeHelpers { |
| 34 |
fn exported_name(&self) -> Option<String>; |
| 35 |
} |
| 36 |
|
| 37 |
impl SynItemFnHelpers for syn::ItemFn { |
| 38 |
fn exported_name(&self) -> Option<String> { |
| 39 |
self.attrs |
| 40 |
.attr_name_value_lookup("export_name") |
| 41 |
.or_else(|| { |
| 42 |
if self.is_no_mangle() { |
| 43 |
Some(self.sig.ident.unraw().to_string()) |
| 44 |
} else { |
| 45 |
None |
| 46 |
} |
| 47 |
}) |
| 48 |
} |
| 49 |
} |
| 50 |
|
| 51 |
impl SynItemFnHelpers for syn::ImplItemMethod { |
| 52 |
fn exported_name(&self) -> Option<String> { |
| 53 |
self.attrs |
| 54 |
.attr_name_value_lookup("export_name") |
| 55 |
.or_else(|| { |
| 56 |
if self.is_no_mangle() { |
| 57 |
Some(self.sig.ident.unraw().to_string()) |
| 58 |
} else { |
| 59 |
None |
| 60 |
} |
| 61 |
}) |
| 62 |
} |
| 63 |
} |
| 64 |
|
| 65 |
/// Returns whether this attribute causes us to skip at item. This basically |
| 66 |
/// checks for `#[cfg(test)]`, `#[test]`, `/// cbindgen::ignore` and |
| 67 |
/// variations thereof. |
| 68 |
fn is_skip_item_attr(attr: &syn::Meta) -> bool { |
| 69 |
match *attr { |
| 70 |
syn::Meta::Path(ref path) => { |
| 71 |
// TODO(emilio): It'd be great if rustc allowed us to use a syntax |
| 72 |
// like `#[cbindgen::ignore]` or such. |
| 73 |
path.is_ident("test") |
| 74 |
} |
| 75 |
syn::Meta::List(ref list) => { |
| 76 |
if !list.path.is_ident("cfg") { |
| 77 |
return false; |
| 78 |
} |
| 79 |
list.nested.iter().any(|nested| match *nested { |
| 80 |
syn::NestedMeta::Meta(ref meta) => is_skip_item_attr(meta), |
| 81 |
syn::NestedMeta::Lit(..) => false, |
| 82 |
}) |
| 83 |
} |
| 84 |
syn::Meta::NameValue(ref name_value) => { |
| 85 |
if name_value.path.is_ident("doc") { |
| 86 |
if let syn::Lit::Str(ref content) = name_value.lit { |
| 87 |
// FIXME(emilio): Maybe should use the general annotation |
| 88 |
// mechanism, but it seems overkill for this. |
| 89 |
if content.value().trim() == "cbindgen:ignore" { |
| 90 |
return true; |
| 91 |
} |
| 92 |
} |
| 93 |
} |
| 94 |
false |
| 95 |
} |
| 96 |
} |
| 97 |
} |
| 98 |
|
| 99 |
pub trait SynAttributeHelpers { |
| 100 |
/// Returns the list of attributes for an item. |
| 101 |
fn attrs(&self) -> &[syn::Attribute]; |
| 102 |
|
| 103 |
/// Searches for attributes like `#[test]`. |
| 104 |
/// Example: |
| 105 |
/// - `item.has_attr_word("test")` => `#[test]` |
| 106 |
fn has_attr_word(&self, name: &str) -> bool { |
| 107 |
self.attrs() |
| 108 |
.iter() |
| 109 |
.filter_map(|x| x.parse_meta().ok()) |
| 110 |
.any(|attr| { |
| 111 |
if let syn::Meta::Path(ref path) = attr { |
| 112 |
path.is_ident(name) |
| 113 |
} else { |
| 114 |
false |
| 115 |
} |
| 116 |
}) |
| 117 |
} |
| 118 |
|
| 119 |
fn is_no_mangle(&self) -> bool { |
| 120 |
self.has_attr_word("no_mangle") |
| 121 |
} |
| 122 |
|
| 123 |
/// Sees whether we should skip parsing a given item. |
| 124 |
fn should_skip_parsing(&self) -> bool { |
| 125 |
for attr in self.attrs() { |
| 126 |
let meta = match attr.parse_meta() { |
| 127 |
Ok(attr) => attr, |
| 128 |
Err(..) => return false, |
| 129 |
}; |
| 130 |
if is_skip_item_attr(&meta) { |
| 131 |
return true; |
| 132 |
} |
| 133 |
} |
| 134 |
|
| 135 |
false |
| 136 |
} |
| 137 |
|
| 138 |
fn attr_name_value_lookup(&self, name: &str) -> Option<String> { |
| 139 |
self.attrs() |
| 140 |
.iter() |
| 141 |
.filter_map(|attr| { |
| 142 |
let attr = attr.parse_meta().ok()?; |
| 143 |
if let syn::Meta::NameValue(syn::MetaNameValue { |
| 144 |
path, |
| 145 |
lit: syn::Lit::Str(lit), |
| 146 |
.. |
| 147 |
}) = attr |
| 148 |
{ |
| 149 |
if path.is_ident(name) { |
| 150 |
return Some(lit.value()); |
| 151 |
} |
| 152 |
} |
| 153 |
None |
| 154 |
}) |
| 155 |
.next() |
| 156 |
} |
| 157 |
|
| 158 |
fn get_comment_lines(&self) -> Vec<String> { |
| 159 |
let mut comment = Vec::new(); |
| 160 |
|
| 161 |
for attr in self.attrs() { |
| 162 |
if attr.style == syn::AttrStyle::Outer { |
| 163 |
if let Ok(syn::Meta::NameValue(syn::MetaNameValue { |
| 164 |
path, |
| 165 |
lit: syn::Lit::Str(content), |
| 166 |
.. |
| 167 |
})) = attr.parse_meta() |
| 168 |
{ |
| 169 |
if path.is_ident("doc") { |
| 170 |
comment.extend(split_doc_attr(&content.value())); |
| 171 |
} |
| 172 |
} |
| 173 |
} |
| 174 |
} |
| 175 |
|
| 176 |
comment |
| 177 |
} |
| 178 |
} |
| 179 |
|
| 180 |
macro_rules! syn_item_match_helper { |
| 181 |
($s:ident => has_attrs: |$i:ident| $a:block, otherwise: || $b:block) => { |
| 182 |
match *$s { |
| 183 |
syn::Item::Const(ref $i) => $a, |
| 184 |
syn::Item::Enum(ref $i) => $a, |
| 185 |
syn::Item::ExternCrate(ref $i) => $a, |
| 186 |
syn::Item::Fn(ref $i) => $a, |
| 187 |
syn::Item::ForeignMod(ref $i) => $a, |
| 188 |
syn::Item::Impl(ref $i) => $a, |
| 189 |
syn::Item::Macro(ref $i) => $a, |
| 190 |
syn::Item::Macro2(ref $i) => $a, |
| 191 |
syn::Item::Mod(ref $i) => $a, |
| 192 |
syn::Item::Static(ref $i) => $a, |
| 193 |
syn::Item::Struct(ref $i) => $a, |
| 194 |
syn::Item::Trait(ref $i) => $a, |
| 195 |
syn::Item::Type(ref $i) => $a, |
| 196 |
syn::Item::Union(ref $i) => $a, |
| 197 |
syn::Item::Use(ref $i) => $a, |
| 198 |
syn::Item::TraitAlias(ref $i) => $a, |
| 199 |
syn::Item::Verbatim(_) => $b, |
| 200 |
_ => panic!("Unhandled syn::Item: {:?}", $s), |
| 201 |
} |
| 202 |
}; |
| 203 |
} |
| 204 |
|
| 205 |
impl SynAttributeHelpers for syn::Item { |
| 206 |
fn attrs(&self) -> &[syn::Attribute] { |
| 207 |
syn_item_match_helper!(self => |
| 208 |
has_attrs: |item| { &item.attrs }, |
| 209 |
otherwise: || { &[] } |
| 210 |
) |
| 211 |
} |
| 212 |
} |
| 213 |
|
| 214 |
macro_rules! impl_syn_item_helper { |
| 215 |
($t:ty) => { |
| 216 |
impl SynAttributeHelpers for $t { |
| 217 |
fn attrs(&self) -> &[syn::Attribute] { |
| 218 |
&self.attrs |
| 219 |
} |
| 220 |
} |
| 221 |
}; |
| 222 |
} |
| 223 |
|
| 224 |
impl_syn_item_helper!(syn::ItemExternCrate); |
| 225 |
impl_syn_item_helper!(syn::ItemUse); |
| 226 |
impl_syn_item_helper!(syn::ItemStatic); |
| 227 |
impl_syn_item_helper!(syn::ItemConst); |
| 228 |
impl_syn_item_helper!(syn::ItemFn); |
| 229 |
impl_syn_item_helper!(syn::ImplItemMethod); |
| 230 |
impl_syn_item_helper!(syn::ItemMod); |
| 231 |
impl_syn_item_helper!(syn::ItemForeignMod); |
| 232 |
impl_syn_item_helper!(syn::ItemType); |
| 233 |
impl_syn_item_helper!(syn::ItemStruct); |
| 234 |
impl_syn_item_helper!(syn::ItemEnum); |
| 235 |
impl_syn_item_helper!(syn::ItemUnion); |
| 236 |
impl_syn_item_helper!(syn::ItemTrait); |
| 237 |
impl_syn_item_helper!(syn::ItemImpl); |
| 238 |
impl_syn_item_helper!(syn::ItemMacro); |
| 239 |
impl_syn_item_helper!(syn::ItemMacro2); |
| 240 |
impl_syn_item_helper!(syn::ItemTraitAlias); |
| 241 |
|
| 242 |
/// Helper function for accessing Abi information |
| 243 |
pub trait SynAbiHelpers { |
| 244 |
fn is_c(&self) -> bool; |
| 245 |
fn is_omitted(&self) -> bool; |
| 246 |
} |
| 247 |
|
| 248 |
impl SynAbiHelpers for Option<syn::Abi> { |
| 249 |
fn is_c(&self) -> bool { |
| 250 |
if let Some(ref abi) = *self { |
| 251 |
if let Some(ref lit_string) = abi.name { |
| 252 |
return lit_string.value() == "C"; |
| 253 |
} |
| 254 |
} |
| 255 |
false |
| 256 |
} |
| 257 |
fn is_omitted(&self) -> bool { |
| 258 |
if let Some(ref abi) = *self { |
| 259 |
abi.name.is_none() |
| 260 |
} else { |
| 261 |
false |
| 262 |
} |
| 263 |
} |
| 264 |
} |
| 265 |
|
| 266 |
impl SynAbiHelpers for syn::Abi { |
| 267 |
fn is_c(&self) -> bool { |
| 268 |
if let Some(ref lit_string) = self.name { |
| 269 |
lit_string.value() == "C" |
| 270 |
} else { |
| 271 |
false |
| 272 |
} |
| 273 |
} |
| 274 |
fn is_omitted(&self) -> bool { |
| 275 |
self.name.is_none() |
| 276 |
} |
| 277 |
} |
| 278 |
|
| 279 |
impl SynAttributeHelpers for [syn::Attribute] { |
| 280 |
fn attrs(&self) -> &[syn::Attribute] { |
| 281 |
self |
| 282 |
} |
| 283 |
} |
| 284 |
|
| 285 |
fn split_doc_attr(input: &str) -> Vec<String> { |
| 286 |
input |
| 287 |
// Convert two newline (indicate "new paragraph") into two line break. |
| 288 |
.replace("\n\n", " \n \n") |
| 289 |
// Convert newline after two spaces (indicate "line break") into line break. |
| 290 |
.split(" \n") |
| 291 |
// Convert single newline (indicate hard-wrapped) into space. |
| 292 |
.map(|s| s.replace('\n', " ")) |
| 293 |
.map(|s| s.trim_end().to_string()) |
| 294 |
.collect() |
| 295 |
} |