Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
174 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
// bindgen-flags: --dynamic-loading TestLib --unsafe-blocks | ||
|
||
int foo(int x, int y); | ||
int bar(void *x); | ||
int baz(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
use syn::{ | ||
visit_mut::{ | ||
visit_impl_item_method_mut, visit_item_fn_mut, | ||
visit_trait_item_method_mut, VisitMut, | ||
}, | ||
ExprUnsafe, ImplItemMethod, Item, ItemFn, Stmt, TraitItemMethod, | ||
}; | ||
|
||
pub(crate) fn unsafe_blocks(items: &mut Vec<Item>) { | ||
for item in items { | ||
syn::visit_mut::visit_item_mut(&mut Visitor, item); | ||
} | ||
} | ||
|
||
struct Visitor; | ||
|
||
impl Visitor { | ||
fn add_unsafe_block(&self, sig: &syn::Signature, block: &mut syn::Block) { | ||
if sig.unsafety.is_none() { | ||
return; | ||
} | ||
|
||
let inner_block = std::mem::replace( | ||
block, | ||
syn::Block { | ||
brace_token: Default::default(), | ||
stmts: Default::default(), | ||
}, | ||
); | ||
|
||
block.stmts.push(Stmt::Expr( | ||
ExprUnsafe { | ||
attrs: Default::default(), | ||
block: inner_block, | ||
unsafe_token: Default::default(), | ||
} | ||
.into(), | ||
)); | ||
} | ||
} | ||
|
||
impl VisitMut for Visitor { | ||
fn visit_impl_item_method_mut(&mut self, item: &mut ImplItemMethod) { | ||
self.add_unsafe_block(&item.sig, &mut item.block); | ||
|
||
visit_impl_item_method_mut(self, item) | ||
} | ||
|
||
fn visit_item_fn_mut(&mut self, item: &mut ItemFn) { | ||
self.add_unsafe_block(&item.sig, &mut item.block); | ||
|
||
visit_item_fn_mut(self, item) | ||
} | ||
|
||
fn visit_trait_item_method_mut(&mut self, item: &mut TraitItemMethod) { | ||
if let Some(ref mut block) = item.default { | ||
self.add_unsafe_block(&item.sig, block); | ||
} | ||
|
||
visit_trait_item_method_mut(self, item) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters