From 7bddcb274e5c4851e9e02b7973ea74b0b9d48eed Mon Sep 17 00:00:00 2001 From: Emilio Cobos Álvarez Date: Tue, 24 May 2016 20:55:23 +0200 Subject: parser: Implement nocopy annotation. --- src/parser.rs | 8 ++++++++ src/types.rs | 6 ++++++ tests/expectations/no_copy.rs | 14 ++++++++++++++ tests/headers/no_copy.hpp | 6 ++++++ 4 files changed, 34 insertions(+) create mode 100644 tests/expectations/no_copy.rs create mode 100644 tests/headers/no_copy.hpp diff --git a/src/parser.rs b/src/parser.rs index aa6fcbdb..b104c2a3 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -517,6 +517,8 @@ struct Annotations { opaque: bool, hide: bool, use_as: Option, + /// Disable deriving copy/clone on this struct. + no_copy: bool, } impl Annotations { @@ -525,6 +527,7 @@ impl Annotations { opaque: false, hide: false, use_as: None, + no_copy: false, }; anno.parse(&cursor.comment()); @@ -542,6 +545,7 @@ impl Annotations { "opaque" => self.opaque = true, "hide" => self.hide = true, "replaces" => self.use_as = Some(comment.get_tag_attr_value(i)), + "nocopy" => self.no_copy = true, _ => (), } } @@ -1089,6 +1093,10 @@ fn visit_top(cursor: &Cursor, ci.borrow_mut().hide = true; } + if anno.no_copy { + ci.borrow_mut().no_copy = true; + } + // If we find a previous translation, we take it now and carry // on. // diff --git a/src/types.rs b/src/types.rs index 7d65c7d0..4527d710 100644 --- a/src/types.rs +++ b/src/types.rs @@ -429,6 +429,8 @@ pub struct CompInfo { pub opaque: bool, pub base_members: usize, pub layout: Layout, + /// If this struct is explicitely marked as non-copiable. + pub no_copy: bool, /// Typedef'd types names, that we'll resolve early to avoid name conflicts pub typedefs: Vec, /// If this type has a template parameter which is not a type (e.g.: a size_t) @@ -479,6 +481,7 @@ impl CompInfo { hide: false, parser_cursor: None, opaque: false, + no_copy: false, base_members: 0, layout: layout, typedefs: vec![], @@ -570,6 +573,9 @@ impl CompInfo { } pub fn can_derive_copy(&self) -> bool { + if self.no_copy { + return false; + } match self.kind { CompKind::Union => true, CompKind::Struct => { diff --git a/tests/expectations/no_copy.rs b/tests/expectations/no_copy.rs new file mode 100644 index 00000000..95a733b6 --- /dev/null +++ b/tests/expectations/no_copy.rs @@ -0,0 +1,14 @@ +/* automatically generated by rust-bindgen */ + + +#![feature(const_fn)] +#![allow(non_snake_case)] + + +/**
*/ +#[repr(C)] +#[derive(Debug)] +pub struct Struct_CopiableButWait { + pub whatever: ::std::os::raw::c_int, + pub _phantom0: ::std::marker::PhantomData, +} diff --git a/tests/headers/no_copy.hpp b/tests/headers/no_copy.hpp new file mode 100644 index 00000000..349e428e --- /dev/null +++ b/tests/headers/no_copy.hpp @@ -0,0 +1,6 @@ + +/**
*/ +template +class CopiableButWait { + int whatever; +}; -- cgit v1.2.3