diff --git a/cpp2rust/compat/platform_flags.h b/cpp2rust/compat/platform_flags.h index 91e3680b..063449a3 100644 --- a/cpp2rust/compat/platform_flags.h +++ b/cpp2rust/compat/platform_flags.h @@ -10,6 +10,7 @@ static inline std::vector getPlatformClangFlags() { std::vector flags = { "-resource-dir=" CLANG_RESOURCE_DIR, "-I" COMPAT_INCLUDE_DIR, + "-D_FORTIFY_SOURCE=0", }; #ifdef MACOS_SDK_PATH flags.push_back("-isysroot" MACOS_SDK_PATH); diff --git a/cpp2rust/converter/converter.cpp b/cpp2rust/converter/converter.cpp index 1b3ed81e..11867c9d 100644 --- a/cpp2rust/converter/converter.cpp +++ b/cpp2rust/converter/converter.cpp @@ -599,12 +599,12 @@ bool Converter::VisitRecordDecl(clang::RecordDecl *decl) { } Mapper::AddRuleForUserDefinedType(decl); - EmitRustStruct(decl); + EmitRustStructOrUnion(decl); return false; } -void Converter::EmitRustStruct(clang::RecordDecl *decl) { +void Converter::EmitRustStructOrUnion(clang::RecordDecl *decl) { // Enums and static variables. In rust they live outside the record for (auto *d : decl->decls()) { if (auto *enum_decl = llvm::dyn_cast(d)) { @@ -630,6 +630,9 @@ void Converter::EmitRustStruct(clang::RecordDecl *decl) { } // Derived traits + if (EmitsReprCForRecords()) { + StrCat("#[repr(C)]"); + } StrCat("#[derive("); for (auto *attr : GetStructAttributes(decl)) { StrCat(attr, ","); @@ -640,7 +643,8 @@ void Converter::EmitRustStruct(clang::RecordDecl *decl) { auto access = clang::dyn_cast(decl) ? AccessSpecifierAsString(decl->getAccess()) : keyword::kPub; - StrCat(access, keyword::kStruct, GetRecordName(decl)); + StrCat(access, decl->isUnion() ? keyword::kUnion : keyword::kStruct, + GetRecordName(decl)); { PushBrace brace(*this); for (auto *field : decl->fields()) { @@ -681,8 +685,8 @@ void Converter::EmitRustStruct(clang::RecordDecl *decl) { AddOrdTrait(cxx); AddCloneTrait(cxx); AddDropTrait(cxx); - AddDefaultTrait(cxx); } + AddDefaultTrait(decl); AddByteReprTrait(decl); } @@ -728,10 +732,10 @@ bool Converter::VisitCXXRecordDecl(clang::CXXRecordDecl *decl) { } } - EmitRustStruct(decl); + EmitRustStructOrUnion(decl); } else { // FIXME: improve error handling - assert(0 && "unsupported union"); + assert(0 && "unsupported record kind"); } return false; @@ -2883,6 +2887,10 @@ std::string Converter::GetRecordName(const clang::NamedDecl *decl) const { std::vector Converter::GetStructAttributes(const clang::RecordDecl *decl) { + if (decl->isUnion()) { + return {"Copy", "Clone"}; + } + std::vector struct_attrs = {}; if (recordDerivesCopy(decl)) { @@ -3202,7 +3210,21 @@ void Converter::AddCloneTrait(const clang::CXXRecordDecl *decl) {} void Converter::AddDropTrait(const clang::CXXRecordDecl *decl) {} -void Converter::AddDefaultTrait(const clang::CXXRecordDecl *decl) { +void Converter::AddDefaultTraitForUnion(const clang::RecordDecl *decl) { + StrCat(std::format("impl Default for {}", GetRecordName(decl))); + PushBrace impl_brace(*this); + StrCat("fn default() -> Self"); + PushBrace fn_brace(*this); + StrCat("unsafe"); + PushBrace unsafe_brace(*this); + StrCat("std::mem::zeroed()"); +} + +void Converter::AddDefaultTrait(const clang::RecordDecl *decl) { + if (decl->isUnion()) { + AddDefaultTraitForUnion(decl); + return; + } if (RecordDerivesDefault(decl)) { return; } @@ -3211,20 +3233,26 @@ void Converter::AddDefaultTrait(const clang::CXXRecordDecl *decl) { PushBrace impl_brace(*this); StrCat("fn default() -> Self"); PushBrace fn_brace(*this); - if (auto *default_ctor = GetUserDefinedDefaultConstructor(decl)) { - StrCat(keyword_unsafe_); - PushBrace unsafe_brace(*this); - Convert(clang::CXXConstructExpr::Create( - ctx_, ctx_.getCanonicalTagType(decl), clang::SourceLocation(), - default_ctor, - /*Elidable=*/false, llvm::ArrayRef(), - /*HadMultipleCandidates=*/false, - /*ListInitialization=*/false, - /*StdInitListInitialization=*/false, - /*ZeroInitialization=*/false, clang::CXXConstructionKind::Complete, - clang::SourceRange())); - } else { - StrCat(struct_name); + + if (auto *cxx = clang::dyn_cast(decl)) { + if (auto *default_ctor = GetUserDefinedDefaultConstructor(cxx)) { + StrCat(keyword_unsafe_); + PushBrace unsafe_brace(*this); + Convert(clang::CXXConstructExpr::Create( + ctx_, ctx_.getCanonicalTagType(decl), clang::SourceLocation(), + default_ctor, + /*Elidable=*/false, llvm::ArrayRef(), + /*HadMultipleCandidates=*/false, + /*ListInitialization=*/false, + /*StdInitListInitialization=*/false, + /*ZeroInitialization=*/false, clang::CXXConstructionKind::Complete, + clang::SourceRange())); + return; + } + } + + StrCat(struct_name); + { PushBrace struct_brace(*this); for (auto *field : decl->fields()) { StrCat(GetNamedDeclAsString(field), token::kColon, diff --git a/cpp2rust/converter/converter.h b/cpp2rust/converter/converter.h index a948d1a9..4e6140e0 100644 --- a/cpp2rust/converter/converter.h +++ b/cpp2rust/converter/converter.h @@ -99,7 +99,9 @@ class Converter : public clang::RecursiveASTVisitor { virtual bool VisitCXXRecordDecl(clang::CXXRecordDecl *decl); - void EmitRustStruct(clang::RecordDecl *decl); + virtual void EmitRustStructOrUnion(clang::RecordDecl *decl); + + virtual bool EmitsReprCForRecords() const { return true; } virtual bool VisitCXXMethodDecl(clang::CXXMethodDecl *decl); virtual std::string GetSelfMaybeWithMut(const clang::CXXMethodDecl *decl); @@ -441,7 +443,9 @@ class Converter : public clang::RecursiveASTVisitor { virtual void AddDropTrait(const clang::CXXRecordDecl *decl); - virtual void AddDefaultTrait(const clang::CXXRecordDecl *decl); + virtual void AddDefaultTrait(const clang::RecordDecl *decl); + + virtual void AddDefaultTraitForUnion(const clang::RecordDecl *decl); virtual void AddByteReprTrait(const clang::RecordDecl *decl); diff --git a/cpp2rust/converter/lex.h b/cpp2rust/converter/lex.h index db69958d..b7a9e3d2 100644 --- a/cpp2rust/converter/lex.h +++ b/cpp2rust/converter/lex.h @@ -51,6 +51,7 @@ inline constexpr const char *kReturn = "return"; inline constexpr const char *kSelfValue = "self"; inline constexpr const char *kStatic = "static"; inline constexpr const char *kStruct = "struct"; +inline constexpr const char *kUnion = "union"; inline constexpr const char *kTrue = "true"; inline constexpr const char *kWhile = "while"; inline constexpr const char *kFor = "for"; diff --git a/cpp2rust/converter/models/converter_refcount.cpp b/cpp2rust/converter/models/converter_refcount.cpp index 0442ae75..d681d1e3 100644 --- a/cpp2rust/converter/models/converter_refcount.cpp +++ b/cpp2rust/converter/models/converter_refcount.cpp @@ -460,11 +460,14 @@ void ConverterRefCount::AddCloneTrait(const clang::CXXRecordDecl *decl) { StrCat("}"); } -void ConverterRefCount::AddDefaultTrait(const clang::CXXRecordDecl *decl) { +void ConverterRefCount::AddDefaultTrait(const clang::RecordDecl *decl) { PushConversionKind push(*this, ConversionKind::FullRefCount); Converter::AddDefaultTrait(decl); } +void ConverterRefCount::AddDefaultTraitForUnion(const clang::RecordDecl *decl) { +} + void ConverterRefCount::AddDropTrait(const clang::CXXRecordDecl *decl) { if (!decl->hasUserDeclaredDestructor()) { return; diff --git a/cpp2rust/converter/models/converter_refcount.h b/cpp2rust/converter/models/converter_refcount.h index cfb22aa8..0ea89e68 100644 --- a/cpp2rust/converter/models/converter_refcount.h +++ b/cpp2rust/converter/models/converter_refcount.h @@ -28,6 +28,8 @@ class ConverterRefCount final : public Converter { bool VisitCXXRecordDecl(clang::CXXRecordDecl *decl) override; + bool EmitsReprCForRecords() const override { return false; } + void ConvertOrdAndPartialOrdTraits(const clang::CXXRecordDecl *decl, const clang::FunctionDecl *op) override; @@ -37,7 +39,9 @@ class ConverterRefCount final : public Converter { void AddByteReprTrait(const clang::RecordDecl *decl) override; - void AddDefaultTrait(const clang::CXXRecordDecl *decl) override; + void AddDefaultTrait(const clang::RecordDecl *decl) override; + + void AddDefaultTraitForUnion(const clang::RecordDecl *decl) override; std::string GetSelfMaybeWithMut(const clang::CXXMethodDecl *decl) override; diff --git a/tests/benchmarks/out/unsafe/bfs.rs b/tests/benchmarks/out/unsafe/bfs.rs index 6fcf07ca..df11cfcf 100644 --- a/tests/benchmarks/out/unsafe/bfs.rs +++ b/tests/benchmarks/out/unsafe/bfs.rs @@ -6,6 +6,7 @@ use std::collections::BTreeMap; use std::io::{Read, Seek, Write}; use std::os::fd::{AsFd, FromRawFd, IntoRawFd}; use std::rc::Rc; +#[repr(C)] #[derive(Copy, Clone, Default)] pub struct Queue { pub elems: *mut u32, @@ -35,11 +36,13 @@ impl Queue { return ((self.back) == (0_u64)); } } +#[repr(C)] #[derive(Copy, Clone, Default)] pub struct GraphNode { pub vertex: u32, pub next: *mut GraphNode, } +#[repr(C)] #[derive(Copy, Clone, Default)] pub struct Graph { pub V: u32, diff --git a/tests/benchmarks/out/unsafe/bst.rs b/tests/benchmarks/out/unsafe/bst.rs index f3810641..769f040a 100644 --- a/tests/benchmarks/out/unsafe/bst.rs +++ b/tests/benchmarks/out/unsafe/bst.rs @@ -6,6 +6,7 @@ use std::collections::BTreeMap; use std::io::{Read, Seek, Write}; use std::os::fd::{AsFd, FromRawFd, IntoRawFd}; use std::rc::Rc; +#[repr(C)] #[derive(Copy, Clone, Default)] pub struct node_t { pub left: *mut node_t, diff --git a/tests/ub/out/unsafe/ub6.rs b/tests/ub/out/unsafe/ub6.rs index 43b6731d..1d276b7c 100644 --- a/tests/ub/out/unsafe/ub6.rs +++ b/tests/ub/out/unsafe/ub6.rs @@ -6,6 +6,7 @@ use std::collections::BTreeMap; use std::io::{Read, Seek, Write}; use std::os::fd::{AsFd, FromRawFd, IntoRawFd}; use std::rc::Rc; +#[repr(C)] #[derive(Copy, Clone, Default)] pub struct Pair { pub x1: *mut i32, diff --git a/tests/unit/out/refcount/union_addrof_external.rs b/tests/unit/out/refcount/union_addrof_external.rs new file mode 100644 index 00000000..5306b87e --- /dev/null +++ b/tests/unit/out/refcount/union_addrof_external.rs @@ -0,0 +1,118 @@ +extern crate libcc2rs; +use libcc2rs::*; +use std::cell::RefCell; +use std::collections::BTreeMap; +use std::io::prelude::*; +use std::io::{Read, Seek, Write}; +use std::os::fd::AsFd; +use std::rc::{Rc, Weak}; +#[repr(C)] +#[derive()] +pub struct record { + pub code: Value, + pub lo: Value, + pub hi: Value, + pub pad: Value>, +} +impl ByteRepr for record {} +#[repr(C)] +#[derive(Copy, Clone)] +pub union Container_anon_15_3 { + pub h: Value, + pub raw_: Value>, +} +impl Default for Container_anon_15_3 { + fn default() -> Self { + unsafe { std::mem::zeroed() } + } +} +#[repr(C)] +#[derive(Default)] +pub struct Container { + pub view: Value, +} +impl ByteRepr for Container {} +pub fn fill_0(out: AnyPtr, cap: u64) { + let out: Value = Rc::new(RefCell::new(out)); + let cap: Value = Rc::new(RefCell::new(cap)); + let src: Value> = Rc::new(RefCell::new(Box::new([ + 0_u8, + ::default(), + ::default(), + ::default(), + ::default(), + ::default(), + ::default(), + ::default(), + ::default(), + ::default(), + ::default(), + ::default(), + ::default(), + ::default(), + ::default(), + ::default(), + ]))); + (*src.borrow_mut())[(0) as usize] = 2_u8; + (*src.borrow_mut())[(1) as usize] = 0_u8; + (*src.borrow_mut())[(2) as usize] = 0_u8; + (*src.borrow_mut())[(3) as usize] = 80_u8; + (*src.borrow_mut())[(4) as usize] = 127_u8; + (*src.borrow_mut())[(5) as usize] = 0_u8; + (*src.borrow_mut())[(6) as usize] = 0_u8; + (*src.borrow_mut())[(7) as usize] = 1_u8; + let n: Value = Rc::new(RefCell::new( + if ((::std::mem::size_of::<[u8; 16]>() as u64) < (*cap.borrow())) { + ::std::mem::size_of::<[u8; 16]>() as u64 + } else { + (*cap.borrow()) + }, + )); + { + (*out.borrow()).memcpy( + &((src.as_pointer() as Ptr) as Ptr).to_any(), + (*n.borrow()) as usize, + ); + (*out.borrow()).clone() + }; +} +pub fn main() { + std::process::exit(main_0()); +} +fn main_0() -> i32 { + let c: Value = >::default(); + { + ((c.as_pointer()) as Ptr).to_any().memset( + (0) as u8, + ::std::mem::size_of::() as u64 as usize, + ); + ((c.as_pointer()) as Ptr).to_any().clone() + }; + ({ + let _out: AnyPtr = (((*c.borrow()).view.as_pointer()).to_strong().as_pointer() as AnyPtr); + let _cap: u64 = ::std::mem::size_of::() as u64; + fill_0(_out, _cap) + }); + assert!((((*(*(*(*c.borrow()).view.borrow()).h.borrow()).code.borrow()) as i32) == (2))); + assert!( + ((((((*(*(*c.borrow()).view.borrow()).h.borrow()).lo.as_pointer()) + .to_strong() + .as_pointer() as Ptr::) + .offset((0) as isize) + .read()) as i32) + == (0)) + ); + assert!( + ((((((*(*(*c.borrow()).view.borrow()).h.borrow()).lo.as_pointer()) + .to_strong() + .as_pointer() as Ptr::) + .offset((1) as isize) + .read()) as i32) + == (80)) + ); + assert!((((*(*(*c.borrow()).view.borrow()).raw_.borrow())[(0) as usize] as i32) == (2))); + assert!( + ((((*(*(*c.borrow()).view.borrow()).raw_.borrow())[(3) as usize] as u8) as i32) == (80)) + ); + return 0; +} diff --git a/tests/unit/out/refcount/union_basic.rs b/tests/unit/out/refcount/union_basic.rs deleted file mode 100644 index c7255a39..00000000 --- a/tests/unit/out/refcount/union_basic.rs +++ /dev/null @@ -1,25 +0,0 @@ -extern crate libcc2rs; -use libcc2rs::*; -use std::cell::RefCell; -use std::collections::BTreeMap; -use std::io::prelude::*; -use std::io::{Read, Seek, Write}; -use std::os::fd::AsFd; -use std::rc::{Rc, Weak}; -#[derive(Default)] -pub struct basic { - pub i: Value, - pub f: Value, -} -impl ByteRepr for basic {} -pub fn main() { - std::process::exit(main_0()); -} -fn main_0() -> i32 { - let u: Value = >::default(); - (*(*u.borrow()).i.borrow_mut()) = 42; - assert!(((*(*u.borrow()).i.borrow()) == 42)); - (*(*u.borrow()).f.borrow_mut()) = 3.140000105E+0; - assert!(((*(*u.borrow()).f.borrow()) == 3.140000105E+0)); - return 0; -} diff --git a/tests/unit/out/refcount/union_pointer_pun_writethrough.rs b/tests/unit/out/refcount/union_pointer_pun_writethrough.rs deleted file mode 100644 index c6c6960b..00000000 --- a/tests/unit/out/refcount/union_pointer_pun_writethrough.rs +++ /dev/null @@ -1,25 +0,0 @@ -extern crate libcc2rs; -use libcc2rs::*; -use std::cell::RefCell; -use std::collections::BTreeMap; -use std::io::prelude::*; -use std::io::{Read, Seek, Write}; -use std::os::fd::AsFd; -use std::rc::{Rc, Weak}; -pub fn main() { - std::process::exit(main_0()); -} -fn main_0() -> i32 { - let x: Value = Rc::new(RefCell::new((-1_i32 as i64))); - #[derive(Default)] - pub struct anon_0 { - pub as_unsigned: Value>, - pub as_signed: Value>, - } - impl ByteRepr for anon_0 {}; - let pp: Value = >::default(); - (*(*pp.borrow()).as_signed.borrow_mut()) = (x.as_pointer()); - (*(*pp.borrow()).as_unsigned.borrow()).write(42_u64); - assert!(((*x.borrow()) == 42_i64)); - return 0; -} diff --git a/tests/unit/out/refcount/union_tagged_many_arms.rs b/tests/unit/out/refcount/union_tagged_many_arms.rs deleted file mode 100644 index 87d63663..00000000 --- a/tests/unit/out/refcount/union_tagged_many_arms.rs +++ /dev/null @@ -1,69 +0,0 @@ -extern crate libcc2rs; -use libcc2rs::*; -use std::cell::RefCell; -use std::collections::BTreeMap; -use std::io::prelude::*; -use std::io::{Read, Seek, Write}; -use std::os::fd::AsFd; -use std::rc::{Rc, Weak}; -#[derive(Clone, Copy, PartialEq, Debug, Default)] -enum Tag { - #[default] - T_NUM_S = 0, - T_NUM_U = 1, - T_TEXT = 2, - T_FLOAT = 3, - T_REF = 4, -} -#[derive(Default)] -pub struct Slot_anon_0 { - pub text: Value>, - pub handle: Value, - pub signed_n: Value, - pub unsigned_n: Value, - pub f: Value, -} -impl ByteRepr for Slot_anon_0 {} -#[derive(Default)] -pub struct Slot { - pub tag: Value, - pub payload: Value, -} -impl ByteRepr for Slot {} -pub fn main() { - std::process::exit(main_0()); -} -fn main_0() -> i32 { - let a: Value = >::default(); - (*(*a.borrow()).tag.borrow_mut()) = (Tag::T_NUM_S as Tag); - (*(*(*a.borrow()).payload.borrow()).signed_n.borrow_mut()) = (-7_i32 as i64); - assert!(((*(*(*a.borrow()).payload.borrow()).signed_n.borrow()) == (-7_i32 as i64))); - let b: Value = >::default(); - (*(*b.borrow()).tag.borrow_mut()) = (Tag::T_NUM_U as Tag); - (*(*(*b.borrow()).payload.borrow()).unsigned_n.borrow_mut()) = 3735928559_u64; - assert!(((*(*(*b.borrow()).payload.borrow()).unsigned_n.borrow()) == 3735928559_u64)); - let c: Value = >::default(); - (*(*c.borrow()).tag.borrow_mut()) = (Tag::T_TEXT as Tag); - (*(*(*c.borrow()).payload.borrow()).text.borrow_mut()) = - (Ptr::from_string_literal("hello")).clone(); - assert!( - ((((*(*(*c.borrow()).payload.borrow()).text.borrow()) - .offset((0) as isize) - .read()) as i32) - == ('h' as i32)) - ); - let d: Value = >::default(); - (*(*d.borrow()).tag.borrow_mut()) = (Tag::T_FLOAT as Tag); - (*(*(*d.borrow()).payload.borrow()).f.borrow_mut()) = 1.5E+0; - assert!(((*(*(*d.borrow()).payload.borrow()).f.borrow()) == 1.5E+0)); - let x: Value = Rc::new(RefCell::new(0)); - let e: Value = >::default(); - (*(*e.borrow()).tag.borrow_mut()) = (Tag::T_REF as Tag); - (*(*(*e.borrow()).payload.borrow()).handle.borrow_mut()) = - ((x.as_pointer()) as Ptr).to_any(); - assert!({ - let _lhs = (*(*(*e.borrow()).payload.borrow()).handle.borrow()).clone(); - _lhs == ((x.as_pointer()) as Ptr).to_any() - }); - return 0; -} diff --git a/tests/unit/out/refcount/union_tagged_simple.rs b/tests/unit/out/refcount/union_tagged_simple.rs deleted file mode 100644 index fcb54840..00000000 --- a/tests/unit/out/refcount/union_tagged_simple.rs +++ /dev/null @@ -1,49 +0,0 @@ -extern crate libcc2rs; -use libcc2rs::*; -use std::cell::RefCell; -use std::collections::BTreeMap; -use std::io::prelude::*; -use std::io::{Read, Seek, Write}; -use std::os::fd::AsFd; -use std::rc::{Rc, Weak}; -#[derive(Clone, Copy, PartialEq, Debug, Default)] -enum Kind { - #[default] - KIND_NONE = 0, - KIND_DONE = 1, -} -#[derive(Default)] -pub struct Event_anon_0 { - pub obj: Value, - pub code: Value, -} -impl ByteRepr for Event_anon_0 {} -#[derive(Default)] -pub struct Event { - pub kind: Value, - pub handle: Value, - pub payload: Value, -} -impl ByteRepr for Event {} -pub fn main() { - std::process::exit(main_0()); -} -fn main_0() -> i32 { - let dummy: Value = Rc::new(RefCell::new(0)); - let m1: Value = >::default(); - (*(*m1.borrow()).kind.borrow_mut()) = (Kind::KIND_DONE as Kind); - (*(*m1.borrow()).handle.borrow_mut()) = ((dummy.as_pointer()) as Ptr).to_any(); - (*(*(*m1.borrow()).payload.borrow()).code.borrow_mut()) = 42; - assert!((((*(*m1.borrow()).kind.borrow()) as u32) == (Kind::KIND_DONE as u32))); - assert!(((*(*(*m1.borrow()).payload.borrow()).code.borrow()) == 42)); - let m2: Value = >::default(); - (*(*m2.borrow()).kind.borrow_mut()) = (Kind::KIND_NONE as Kind); - (*(*m2.borrow()).handle.borrow_mut()) = ((dummy.as_pointer()) as Ptr).to_any(); - (*(*(*m2.borrow()).payload.borrow()).obj.borrow_mut()) = - ((dummy.as_pointer()) as Ptr).to_any(); - assert!({ - let _lhs = (*(*(*m2.borrow()).payload.borrow()).obj.borrow()).clone(); - _lhs == ((dummy.as_pointer()) as Ptr).to_any() - }); - return 0; -} diff --git a/tests/unit/out/refcount/union_tagged_struct_arms.rs b/tests/unit/out/refcount/union_tagged_struct_arms.rs deleted file mode 100644 index 52dc5c9a..00000000 --- a/tests/unit/out/refcount/union_tagged_struct_arms.rs +++ /dev/null @@ -1,148 +0,0 @@ -extern crate libcc2rs; -use libcc2rs::*; -use std::cell::RefCell; -use std::collections::BTreeMap; -use std::io::prelude::*; -use std::io::{Read, Seek, Write}; -use std::os::fd::AsFd; -use std::rc::{Rc, Weak}; -#[derive(Clone, Copy, PartialEq, Debug, Default)] -enum Choice { - #[default] - C_LIST = 1, - C_LETTERS = 2, - C_INTEGERS = 3, -} -#[derive(Default)] -pub struct Branch_anon_0_anon_0 { - pub items: Value>>, - pub count: Value, - pub cursor: Value, -} -impl ByteRepr for Branch_anon_0_anon_0 {} -#[derive(Default)] -pub struct Branch_anon_0_anon_1 { - pub lo: Value, - pub hi: Value, - pub curr: Value, - pub step: Value, -} -impl ByteRepr for Branch_anon_0_anon_1 {} -#[derive(Default)] -pub struct Branch_anon_0_anon_2 { - pub lo: Value, - pub hi: Value, - pub curr: Value, - pub step: Value, - pub width: Value, -} -impl ByteRepr for Branch_anon_0_anon_2 {} -#[derive(Default)] -pub struct Branch_anon_0 { - pub list: Value, - pub letters: Value, - pub integers: Value, -} -impl ByteRepr for Branch_anon_0 {} -#[derive(Default)] -pub struct Branch { - pub choice: Value, - pub index: Value, - pub v: Value, -} -impl ByteRepr for Branch {} -pub fn main() { - std::process::exit(main_0()); -} -fn main_0() -> i32 { - thread_local!( - static items: Value]>> = Rc::new(RefCell::new(Box::new([ - Ptr::from_string_literal("a"), - Ptr::from_string_literal("b"), - Ptr::from_string_literal("c"), - ]))); - ); - let p_list: Value = >::default(); - (*(*p_list.borrow()).choice.borrow_mut()) = (Choice::C_LIST as Choice); - (*(*p_list.borrow()).index.borrow_mut()) = 0; - (*(*(*(*p_list.borrow()).v.borrow()).list.borrow()) - .items - .borrow_mut()) = (items.with(Value::clone).as_pointer() as Ptr>); - (*(*(*(*p_list.borrow()).v.borrow()).list.borrow()) - .count - .borrow_mut()) = 3_i64; - (*(*(*(*p_list.borrow()).v.borrow()).list.borrow()) - .cursor - .borrow_mut()) = 1_i64; - assert!( - ((*(*(*(*p_list.borrow()).v.borrow()).list.borrow()) - .count - .borrow()) - == 3_i64) - ); - assert!( - (((((*(*(*(*p_list.borrow()).v.borrow()).list.borrow()) - .items - .borrow()) - .offset((1) as isize) - .read()) - .offset((0) as isize) - .read()) as i32) - == ('b' as i32)) - ); - let p_letters: Value = >::default(); - (*(*p_letters.borrow()).choice.borrow_mut()) = (Choice::C_LETTERS as Choice); - (*(*p_letters.borrow()).index.borrow_mut()) = 1; - (*(*(*(*p_letters.borrow()).v.borrow()).letters.borrow()) - .lo - .borrow_mut()) = ('a' as i32); - (*(*(*(*p_letters.borrow()).v.borrow()).letters.borrow()) - .hi - .borrow_mut()) = ('z' as i32); - (*(*(*(*p_letters.borrow()).v.borrow()).letters.borrow()) - .curr - .borrow_mut()) = ('m' as i32); - (*(*(*(*p_letters.borrow()).v.borrow()).letters.borrow()) - .step - .borrow_mut()) = 1_u8; - assert!( - (((*(*(*(*p_letters.borrow()).v.borrow()).letters.borrow()) - .hi - .borrow()) - - (*(*(*(*p_letters.borrow()).v.borrow()).letters.borrow()) - .lo - .borrow())) - == 25) - ); - let p_integers: Value = >::default(); - (*(*p_integers.borrow()).choice.borrow_mut()) = (Choice::C_INTEGERS as Choice); - (*(*p_integers.borrow()).index.borrow_mut()) = 2; - (*(*(*(*p_integers.borrow()).v.borrow()).integers.borrow()) - .lo - .borrow_mut()) = 1_i64; - (*(*(*(*p_integers.borrow()).v.borrow()).integers.borrow()) - .hi - .borrow_mut()) = 100_i64; - (*(*(*(*p_integers.borrow()).v.borrow()).integers.borrow()) - .curr - .borrow_mut()) = 1_i64; - (*(*(*(*p_integers.borrow()).v.borrow()).integers.borrow()) - .step - .borrow_mut()) = 1_i64; - (*(*(*(*p_integers.borrow()).v.borrow()).integers.borrow()) - .width - .borrow_mut()) = 3; - assert!( - ((*(*(*(*p_integers.borrow()).v.borrow()).integers.borrow()) - .hi - .borrow()) - == 100_i64) - ); - assert!( - ((*(*(*(*p_integers.borrow()).v.borrow()).integers.borrow()) - .width - .borrow()) - == 3) - ); - return 0; -} diff --git a/tests/unit/out/refcount/union_void_ptr_sized_deref.rs b/tests/unit/out/refcount/union_void_ptr_sized_deref.rs deleted file mode 100644 index d07cc4a7..00000000 --- a/tests/unit/out/refcount/union_void_ptr_sized_deref.rs +++ /dev/null @@ -1,103 +0,0 @@ -extern crate libcc2rs; -use libcc2rs::*; -use std::cell::RefCell; -use std::collections::BTreeMap; -use std::io::prelude::*; -use std::io::{Read, Seek, Write}; -use std::os::fd::AsFd; -use std::rc::{Rc, Weak}; -#[derive(Clone, Copy, PartialEq, Debug, Default)] -enum Width { - #[default] - W_64 = 0, - W_32 = 1, - W_16 = 2, -} -#[derive(Default)] -pub struct Sink_anon_0 { - pub text: Value>, - pub handle: Value, - pub signed_n: Value, - pub f: Value, -} -impl ByteRepr for Sink_anon_0 {} -#[derive(Default)] -pub struct Sink { - pub width: Value, - pub out: Value, -} -impl ByteRepr for Sink {} -pub fn write_count_0(s: Ptr, count: i64) { - let s: Value> = Rc::new(RefCell::new(s)); - let count: Value = Rc::new(RefCell::new(count)); - 'switch: { - let __match_cond = ((*(*(*s.borrow()).upgrade().deref()).width.borrow()) as u32); - match __match_cond { - v if v == (Width::W_64 as u32) => { - (*(*(*(*s.borrow()).upgrade().deref()).out.borrow()) - .handle - .borrow()) - .cast::() - .expect("ub:wrong type") - .write((*count.borrow())); - break 'switch; - } - v if v == (Width::W_32 as u32) => { - (*(*(*(*s.borrow()).upgrade().deref()).out.borrow()) - .handle - .borrow()) - .cast::() - .expect("ub:wrong type") - .write(((*count.borrow()) as i32)); - break 'switch; - } - v if v == (Width::W_16 as u32) => { - (*(*(*(*s.borrow()).upgrade().deref()).out.borrow()) - .handle - .borrow()) - .cast::() - .expect("ub:wrong type") - .write(((*count.borrow()) as i16)); - break 'switch; - } - _ => {} - } - }; -} -pub fn main() { - std::process::exit(main_0()); -} -fn main_0() -> i32 { - let buf64: Value = Rc::new(RefCell::new(0_i64)); - let buf32: Value = Rc::new(RefCell::new(0)); - let buf16: Value = Rc::new(RefCell::new(0_i16)); - let s: Value = >::default(); - (*(*s.borrow()).width.borrow_mut()) = (Width::W_64 as Width); - (*(*(*s.borrow()).out.borrow()).handle.borrow_mut()) = - ((buf64.as_pointer()) as Ptr).to_any(); - ({ - let _s: Ptr = (s.as_pointer()); - let _count: i64 = 1234605616436508552_i64; - write_count_0(_s, _count) - }); - assert!(((*buf64.borrow()) == 1234605616436508552_i64)); - (*(*s.borrow()).width.borrow_mut()) = (Width::W_32 as Width); - (*(*(*s.borrow()).out.borrow()).handle.borrow_mut()) = - ((buf32.as_pointer()) as Ptr).to_any(); - ({ - let _s: Ptr = (s.as_pointer()); - let _count: i64 = 305419896_i64; - write_count_0(_s, _count) - }); - assert!(((*buf32.borrow()) == 305419896)); - (*(*s.borrow()).width.borrow_mut()) = (Width::W_16 as Width); - (*(*(*s.borrow()).out.borrow()).handle.borrow_mut()) = - ((buf16.as_pointer()) as Ptr).to_any(); - ({ - let _s: Ptr = (s.as_pointer()); - let _count: i64 = 4660_i64; - write_count_0(_s, _count) - }); - assert!((((*buf16.borrow()) as i32) == 4660)); - return 0; -} diff --git a/tests/unit/out/unsafe/10_struct.rs b/tests/unit/out/unsafe/10_struct.rs index afb5f3bf..717ba9f5 100644 --- a/tests/unit/out/unsafe/10_struct.rs +++ b/tests/unit/out/unsafe/10_struct.rs @@ -6,11 +6,13 @@ use std::collections::BTreeMap; use std::io::{Read, Seek, Write}; use std::os::fd::{AsFd, FromRawFd, IntoRawFd}; use std::rc::Rc; +#[repr(C)] #[derive(Copy, Clone, Default)] pub struct GraphNode { pub dst: u32, pub next: *mut GraphNode, } +#[repr(C)] #[derive(Copy, Clone, Default)] pub struct Graph { pub V: u32, diff --git a/tests/unit/out/unsafe/anonymous-struct.rs b/tests/unit/out/unsafe/anonymous-struct.rs index 6b95a9f3..8940f0ac 100644 --- a/tests/unit/out/unsafe/anonymous-struct.rs +++ b/tests/unit/out/unsafe/anonymous-struct.rs @@ -6,40 +6,48 @@ use std::collections::BTreeMap; use std::io::{Read, Seek, Write}; use std::os::fd::{AsFd, FromRawFd, IntoRawFd}; use std::rc::Rc; +#[repr(C)] #[derive(Copy, Clone, Default)] pub struct Outer_Named { pub a: i32, pub b: i32, } +#[repr(C)] #[derive(Copy, Clone, Default)] pub struct Outer_anon_0 { pub c: i32, pub d: i32, } +#[repr(C)] #[derive(Copy, Clone, Default)] pub struct Outer_anon_1 { pub g: i32, pub h: i32, } +#[repr(C)] #[derive(Copy, Clone, Default)] pub struct Outer_anon_2 { pub e: i32, pub f: i32, } +#[repr(C)] #[derive(Copy, Clone, Default)] pub struct Outer_anon_3_anon_0 { pub j: i32, } +#[repr(C)] #[derive(Copy, Clone, Default)] pub struct Outer_anon_3_anon_1 { pub k: i32, } +#[repr(C)] #[derive(Copy, Clone, Default)] pub struct Outer_anon_3 { pub i: i32, pub inner_named: Outer_anon_3_anon_0, pub anon_1: Outer_anon_3_anon_1, } +#[repr(C)] #[derive(Copy, Clone, Default)] pub struct Outer { pub named: Outer_Named, @@ -87,6 +95,7 @@ unsafe fn main_0() -> i32 { assert!(((o.anon_3.i) == (9))); assert!(((o.anon_3.inner_named.j) == (10))); assert!(((o.anon_3.anon_1.k) == (11))); + #[repr(C)] #[derive(Copy, Clone, Default)] pub struct anon_0 { pub x: i32, diff --git a/tests/unit/out/unsafe/anonymous-struct_c.rs b/tests/unit/out/unsafe/anonymous-struct_c.rs index 578d1f81..1da705e1 100644 --- a/tests/unit/out/unsafe/anonymous-struct_c.rs +++ b/tests/unit/out/unsafe/anonymous-struct_c.rs @@ -6,40 +6,48 @@ use std::collections::BTreeMap; use std::io::{Read, Seek, Write}; use std::os::fd::{AsFd, FromRawFd, IntoRawFd}; use std::rc::Rc; +#[repr(C)] #[derive(Copy, Clone, Default)] pub struct Named { pub a: i32, pub b: i32, } +#[repr(C)] #[derive(Copy, Clone, Default)] pub struct Outer_anon_0 { pub c: i32, pub d: i32, } +#[repr(C)] #[derive(Copy, Clone, Default)] pub struct Outer_anon_1 { pub g: i32, pub h: i32, } +#[repr(C)] #[derive(Copy, Clone, Default)] pub struct Outer_anon_2 { pub e: i32, pub f: i32, } +#[repr(C)] #[derive(Copy, Clone, Default)] pub struct Outer_anon_3_anon_0 { pub j: i32, } +#[repr(C)] #[derive(Copy, Clone, Default)] pub struct Outer_anon_3_anon_1 { pub k: i32, } +#[repr(C)] #[derive(Copy, Clone, Default)] pub struct Outer_anon_3 { pub i: i32, pub inner_named: Outer_anon_3_anon_0, pub anon_1: Outer_anon_3_anon_1, } +#[repr(C)] #[derive(Copy, Clone, Default)] pub struct Outer { pub named: Named, @@ -83,6 +91,7 @@ unsafe fn main_0() -> i32 { assert!(((o.anon_3.i) == (9))); assert!(((o.anon_3.inner_named.j) == (10))); assert!(((o.anon_3.anon_1.k) == (11))); + #[repr(C)] #[derive(Copy, Clone, Default)] pub struct anon_0 { pub x: i32, diff --git a/tests/unit/out/unsafe/bounded_struct_ptr.rs b/tests/unit/out/unsafe/bounded_struct_ptr.rs index 10f11398..e5acc6a7 100644 --- a/tests/unit/out/unsafe/bounded_struct_ptr.rs +++ b/tests/unit/out/unsafe/bounded_struct_ptr.rs @@ -6,6 +6,7 @@ use std::collections::BTreeMap; use std::io::{Read, Seek, Write}; use std::os::fd::{AsFd, FromRawFd, IntoRawFd}; use std::rc::Rc; +#[repr(C)] #[derive(Copy, Clone, Default)] pub struct Foo { pub x1: i32, diff --git a/tests/unit/out/unsafe/bst.rs b/tests/unit/out/unsafe/bst.rs index 1d9406ff..0067a0fe 100644 --- a/tests/unit/out/unsafe/bst.rs +++ b/tests/unit/out/unsafe/bst.rs @@ -6,6 +6,7 @@ use std::collections::BTreeMap; use std::io::{Read, Seek, Write}; use std::os::fd::{AsFd, FromRawFd, IntoRawFd}; use std::rc::Rc; +#[repr(C)] #[derive(Copy, Clone, Default)] pub struct node_t { pub left: *mut node_t, diff --git a/tests/unit/out/unsafe/c_struct.rs b/tests/unit/out/unsafe/c_struct.rs index 3d12a8fd..1d2fcd32 100644 --- a/tests/unit/out/unsafe/c_struct.rs +++ b/tests/unit/out/unsafe/c_struct.rs @@ -6,16 +6,19 @@ use std::collections::BTreeMap; use std::io::{Read, Seek, Write}; use std::os::fd::{AsFd, FromRawFd, IntoRawFd}; use std::rc::Rc; +#[repr(C)] #[derive(Copy, Clone, Default)] pub struct Point { pub x: i32, pub y: i32, } +#[repr(C)] #[derive(Copy, Clone, Default)] pub struct Line { pub start: Point, pub end: Point, } +#[repr(C)] #[derive(Copy, Clone, Default)] pub struct Node { pub value: i32, @@ -28,11 +31,13 @@ enum Color { GREEN = 1, BLUE = 2, } +#[repr(C)] #[derive(Copy, Clone, Default)] pub struct Inner { pub a: i32, pub b: i32, } +#[repr(C)] #[derive(Copy, Clone, Default)] pub struct Container { pub inner: Inner, diff --git a/tests/unit/out/unsafe/class.rs b/tests/unit/out/unsafe/class.rs index aacf2489..2e098d4f 100644 --- a/tests/unit/out/unsafe/class.rs +++ b/tests/unit/out/unsafe/class.rs @@ -6,6 +6,7 @@ use std::collections::BTreeMap; use std::io::{Read, Seek, Write}; use std::os::fd::{AsFd, FromRawFd, IntoRawFd}; use std::rc::Rc; +#[repr(C)] #[derive(Copy, Clone, Default)] pub struct Pair { pub first: i32, @@ -42,6 +43,7 @@ impl Pair { })); } } +#[repr(C)] #[derive(Copy, Clone, Default)] pub struct Route { pub path: Pair, diff --git a/tests/unit/out/unsafe/clone_vs_move.rs b/tests/unit/out/unsafe/clone_vs_move.rs index 5f782e3e..7dcb0938 100644 --- a/tests/unit/out/unsafe/clone_vs_move.rs +++ b/tests/unit/out/unsafe/clone_vs_move.rs @@ -6,10 +6,12 @@ use std::collections::BTreeMap; use std::io::{Read, Seek, Write}; use std::os::fd::{AsFd, FromRawFd, IntoRawFd}; use std::rc::Rc; +#[repr(C)] #[derive(Copy, Clone, Default)] pub struct Bar { pub w: i32, } +#[repr(C)] #[derive(Copy, Clone)] pub struct Foo { pub x: i32, diff --git a/tests/unit/out/unsafe/complex_function.rs b/tests/unit/out/unsafe/complex_function.rs index fe27d543..62b15075 100644 --- a/tests/unit/out/unsafe/complex_function.rs +++ b/tests/unit/out/unsafe/complex_function.rs @@ -15,10 +15,12 @@ pub unsafe fn ptr_1(mut x: *mut i32) -> *mut i32 { pub unsafe fn bar_2(x: *mut i32) -> *mut i32 { return x; } +#[repr(C)] #[derive(Copy, Clone, Default)] pub struct X1 { pub v: i32, } +#[repr(C)] #[derive(Copy, Clone, Default)] pub struct X2 { pub v: *mut X1, @@ -28,6 +30,7 @@ impl X2 { return self.v; } } +#[repr(C)] #[derive(Copy, Clone, Default)] pub struct X3 { pub v: *mut X2, @@ -37,6 +40,7 @@ impl X3 { return self.v; } } +#[repr(C)] #[derive(Copy, Clone, Default)] pub struct X4 { pub v: X3, diff --git a/tests/unit/out/unsafe/default.rs b/tests/unit/out/unsafe/default.rs index aa31c35c..6ccbb6af 100644 --- a/tests/unit/out/unsafe/default.rs +++ b/tests/unit/out/unsafe/default.rs @@ -6,6 +6,7 @@ use std::collections::BTreeMap; use std::io::{Read, Seek, Write}; use std::os::fd::{AsFd, FromRawFd, IntoRawFd}; use std::rc::Rc; +#[repr(C)] #[derive(Copy, Clone)] pub struct Pointers { pub x1: *mut i32, diff --git a/tests/unit/out/unsafe/exprs.rs b/tests/unit/out/unsafe/exprs.rs index c6643731..c8161b85 100644 --- a/tests/unit/out/unsafe/exprs.rs +++ b/tests/unit/out/unsafe/exprs.rs @@ -6,10 +6,12 @@ use std::collections::BTreeMap; use std::io::{Read, Seek, Write}; use std::os::fd::{AsFd, FromRawFd, IntoRawFd}; use std::rc::Rc; +#[repr(C)] #[derive(Copy, Clone, Default)] pub struct X { pub x: i32, } +#[repr(C)] #[derive(Copy, Clone, Default)] pub struct Y { pub x: X, diff --git a/tests/unit/out/unsafe/fft.rs b/tests/unit/out/unsafe/fft.rs index 564131af..3235a4bd 100644 --- a/tests/unit/out/unsafe/fft.rs +++ b/tests/unit/out/unsafe/fft.rs @@ -6,6 +6,7 @@ use std::collections::BTreeMap; use std::io::{Read, Seek, Write}; use std::os::fd::{AsFd, FromRawFd, IntoRawFd}; use std::rc::Rc; +#[repr(C)] #[derive(Copy, Clone, Default)] pub struct Complex { pub re: f64, diff --git a/tests/unit/out/unsafe/fn_ptr_cast.rs b/tests/unit/out/unsafe/fn_ptr_cast.rs index f9178b9c..9951cc7b 100644 --- a/tests/unit/out/unsafe/fn_ptr_cast.rs +++ b/tests/unit/out/unsafe/fn_ptr_cast.rs @@ -44,6 +44,7 @@ pub unsafe fn test_double_cast_2() { ); assert!(((fn2) == (fn_))); } +#[repr(C)] #[derive(Copy, Clone, Default)] pub struct Command { pub data: *mut ::libc::c_void, diff --git a/tests/unit/out/unsafe/fn_ptr_stable_sort.rs b/tests/unit/out/unsafe/fn_ptr_stable_sort.rs index 4f8cb21e..f1fb136d 100644 --- a/tests/unit/out/unsafe/fn_ptr_stable_sort.rs +++ b/tests/unit/out/unsafe/fn_ptr_stable_sort.rs @@ -6,6 +6,7 @@ use std::collections::BTreeMap; use std::io::{Read, Seek, Write}; use std::os::fd::{AsFd, FromRawFd, IntoRawFd}; use std::rc::Rc; +#[repr(C)] #[derive(Copy, Clone, Default)] pub struct Item { pub key: i32, diff --git a/tests/unit/out/unsafe/fn_ptr_struct.rs b/tests/unit/out/unsafe/fn_ptr_struct.rs index 4760d91c..6e4a0466 100644 --- a/tests/unit/out/unsafe/fn_ptr_struct.rs +++ b/tests/unit/out/unsafe/fn_ptr_struct.rs @@ -6,6 +6,7 @@ use std::collections::BTreeMap; use std::io::{Read, Seek, Write}; use std::os::fd::{AsFd, FromRawFd, IntoRawFd}; use std::rc::Rc; +#[repr(C)] #[derive(Clone)] pub struct Handler { pub tag: i32, diff --git a/tests/unit/out/unsafe/fn_ptr_vtable.rs b/tests/unit/out/unsafe/fn_ptr_vtable.rs index 1beb5c33..25b91dca 100644 --- a/tests/unit/out/unsafe/fn_ptr_vtable.rs +++ b/tests/unit/out/unsafe/fn_ptr_vtable.rs @@ -6,6 +6,7 @@ use std::collections::BTreeMap; use std::io::{Read, Seek, Write}; use std::os::fd::{AsFd, FromRawFd, IntoRawFd}; use std::rc::Rc; +#[repr(C)] #[derive(Clone)] pub struct Vtable { pub create: Option *mut ::libc::c_void>, diff --git a/tests/unit/out/unsafe/foreach_disjoint_field_borrow.rs b/tests/unit/out/unsafe/foreach_disjoint_field_borrow.rs index dd8f8325..93399ada 100644 --- a/tests/unit/out/unsafe/foreach_disjoint_field_borrow.rs +++ b/tests/unit/out/unsafe/foreach_disjoint_field_borrow.rs @@ -6,6 +6,7 @@ use std::collections::BTreeMap; use std::io::{Read, Seek, Write}; use std::os::fd::{AsFd, FromRawFd, IntoRawFd}; use std::rc::Rc; +#[repr(C)] #[derive(Clone, Default)] pub struct S { pub v: Vec, diff --git a/tests/unit/out/unsafe/function_overloading.rs b/tests/unit/out/unsafe/function_overloading.rs index 4b0cb1fa..effb483d 100644 --- a/tests/unit/out/unsafe/function_overloading.rs +++ b/tests/unit/out/unsafe/function_overloading.rs @@ -21,6 +21,7 @@ pub unsafe fn foo_3(mut x: *mut i32, mut y: *mut i32, z: *mut i32) -> i32 { pub unsafe fn bar_4(x: *mut i32) -> i32 { return (*x); } +#[repr(C)] #[derive(Copy, Clone, Default)] pub struct Foo {} impl Foo { diff --git a/tests/unit/out/unsafe/huffman.rs b/tests/unit/out/unsafe/huffman.rs index 962d5690..d9da3862 100644 --- a/tests/unit/out/unsafe/huffman.rs +++ b/tests/unit/out/unsafe/huffman.rs @@ -6,6 +6,7 @@ use std::collections::BTreeMap; use std::io::{Read, Seek, Write}; use std::os::fd::{AsFd, FromRawFd, IntoRawFd}; use std::rc::Rc; +#[repr(C)] #[derive(Copy, Clone, Default)] pub struct MinHeapNode { pub data: u8, @@ -40,6 +41,7 @@ pub unsafe fn Swap_0(a: *mut MinHeapNode, b: *mut MinHeapNode) { }) .clone(); } +#[repr(C)] #[derive(Default)] pub struct MinHeap { pub size: i32, diff --git a/tests/unit/out/unsafe/immutable-deref-on-func-call.rs b/tests/unit/out/unsafe/immutable-deref-on-func-call.rs index 85a4fcfd..3a917620 100644 --- a/tests/unit/out/unsafe/immutable-deref-on-func-call.rs +++ b/tests/unit/out/unsafe/immutable-deref-on-func-call.rs @@ -6,6 +6,7 @@ use std::collections::BTreeMap; use std::io::{Read, Seek, Write}; use std::os::fd::{AsFd, FromRawFd, IntoRawFd}; use std::rc::Rc; +#[repr(C)] #[derive(Copy, Clone, Default)] pub struct Item { pub value: i32, diff --git a/tests/unit/out/unsafe/init.rs b/tests/unit/out/unsafe/init.rs index fe3cc58a..c43dbde4 100644 --- a/tests/unit/out/unsafe/init.rs +++ b/tests/unit/out/unsafe/init.rs @@ -6,6 +6,7 @@ use std::collections::BTreeMap; use std::io::{Read, Seek, Write}; use std::os::fd::{AsFd, FromRawFd, IntoRawFd}; use std::rc::Rc; +#[repr(C)] #[derive(Copy, Clone, Default)] pub struct X { pub x: i32, diff --git a/tests/unit/out/unsafe/kruskal.rs b/tests/unit/out/unsafe/kruskal.rs index d6b5f346..4ae16259 100644 --- a/tests/unit/out/unsafe/kruskal.rs +++ b/tests/unit/out/unsafe/kruskal.rs @@ -6,6 +6,7 @@ use std::collections::BTreeMap; use std::io::{Read, Seek, Write}; use std::os::fd::{AsFd, FromRawFd, IntoRawFd}; use std::rc::Rc; +#[repr(C)] #[derive(Copy, Clone, Default)] pub struct Edge { pub u: i32, @@ -93,6 +94,7 @@ pub unsafe fn quicksort_1(arr: *mut Option>, mut start: i32, mut end quicksort_1(_arr, _start, _end) }); } +#[repr(C)] #[derive(Default)] pub struct DisjointSet { pub rank: Option>, @@ -144,6 +146,7 @@ impl DisjointSet { } } } +#[repr(C)] #[derive(Default)] pub struct Graph { pub edges: Option>, diff --git a/tests/unit/out/unsafe/linked_list.rs b/tests/unit/out/unsafe/linked_list.rs index 3c46970c..b9141c36 100644 --- a/tests/unit/out/unsafe/linked_list.rs +++ b/tests/unit/out/unsafe/linked_list.rs @@ -6,6 +6,7 @@ use std::collections::BTreeMap; use std::io::{Read, Seek, Write}; use std::os::fd::{AsFd, FromRawFd, IntoRawFd}; use std::rc::Rc; +#[repr(C)] #[derive(Copy, Clone, Default)] pub struct Node { pub val: i32, diff --git a/tests/unit/out/unsafe/nested_structs.rs b/tests/unit/out/unsafe/nested_structs.rs index 6d018fd7..b93fed2d 100644 --- a/tests/unit/out/unsafe/nested_structs.rs +++ b/tests/unit/out/unsafe/nested_structs.rs @@ -6,28 +6,34 @@ use std::collections::BTreeMap; use std::io::{Read, Seek, Write}; use std::os::fd::{AsFd, FromRawFd, IntoRawFd}; use std::rc::Rc; +#[repr(C)] #[derive(Copy, Clone, Default)] pub struct Level0_Level1_1_Level2_1_Level3_1 { pub x1: i32, } +#[repr(C)] #[derive(Copy, Clone, Default)] pub struct Level0_Level1_1_Level2_1_Level3_2 { pub x1: i32, pub x2: i32, } +#[repr(C)] #[derive(Copy, Clone, Default)] pub struct Level0_Level1_1_Level2_1 { pub x1: i32, } +#[repr(C)] #[derive(Copy, Clone, Default)] pub struct Level0_Level1_1 { pub x1: i32, } +#[repr(C)] #[derive(Copy, Clone, Default)] pub struct Level0_Level1_2 { pub x1: i32, pub x2: i32, } +#[repr(C)] #[derive(Copy, Clone, Default)] pub struct Level0 {} pub fn main() { diff --git a/tests/unit/out/unsafe/new_bst.rs b/tests/unit/out/unsafe/new_bst.rs index 441bd7a7..cb5a75cd 100644 --- a/tests/unit/out/unsafe/new_bst.rs +++ b/tests/unit/out/unsafe/new_bst.rs @@ -6,6 +6,7 @@ use std::collections::BTreeMap; use std::io::{Read, Seek, Write}; use std::os::fd::{AsFd, FromRawFd, IntoRawFd}; use std::rc::Rc; +#[repr(C)] #[derive(Copy, Clone, Default)] pub struct node_t { pub left: *mut node_t, diff --git a/tests/unit/out/unsafe/new_struct.rs b/tests/unit/out/unsafe/new_struct.rs index 5adf5151..ed91969b 100644 --- a/tests/unit/out/unsafe/new_struct.rs +++ b/tests/unit/out/unsafe/new_struct.rs @@ -6,6 +6,7 @@ use std::collections::BTreeMap; use std::io::{Read, Seek, Write}; use std::os::fd::{AsFd, FromRawFd, IntoRawFd}; use std::rc::Rc; +#[repr(C)] #[derive(Copy, Clone, Default)] pub struct Pair { pub x: i32, diff --git a/tests/unit/out/unsafe/operator_less_than.rs b/tests/unit/out/unsafe/operator_less_than.rs index 0665ca14..d183314f 100644 --- a/tests/unit/out/unsafe/operator_less_than.rs +++ b/tests/unit/out/unsafe/operator_less_than.rs @@ -6,6 +6,7 @@ use std::collections::BTreeMap; use std::io::{Read, Seek, Write}; use std::os::fd::{AsFd, FromRawFd, IntoRawFd}; use std::rc::Rc; +#[repr(C)] #[derive(Copy, Clone, Default)] pub struct Pair { pub x: i32, diff --git a/tests/unit/out/unsafe/pod.rs b/tests/unit/out/unsafe/pod.rs index 0039489f..337aadab 100644 --- a/tests/unit/out/unsafe/pod.rs +++ b/tests/unit/out/unsafe/pod.rs @@ -6,6 +6,7 @@ use std::collections::BTreeMap; use std::io::{Read, Seek, Write}; use std::os::fd::{AsFd, FromRawFd, IntoRawFd}; use std::rc::Rc; +#[repr(C)] #[derive(Copy, Clone, Default)] pub struct POD { pub x1: i32, diff --git a/tests/unit/out/unsafe/pointer_array.rs b/tests/unit/out/unsafe/pointer_array.rs index 2dd1090c..3d3d82e7 100644 --- a/tests/unit/out/unsafe/pointer_array.rs +++ b/tests/unit/out/unsafe/pointer_array.rs @@ -6,6 +6,7 @@ use std::collections::BTreeMap; use std::io::{Read, Seek, Write}; use std::os::fd::{AsFd, FromRawFd, IntoRawFd}; use std::rc::Rc; +#[repr(C)] #[derive(Copy, Clone)] pub struct StackArray { pub arr: [*mut i32; 3], diff --git a/tests/unit/out/unsafe/pointers.rs b/tests/unit/out/unsafe/pointers.rs index 7bbc2eb5..0b5e1a45 100644 --- a/tests/unit/out/unsafe/pointers.rs +++ b/tests/unit/out/unsafe/pointers.rs @@ -6,6 +6,7 @@ use std::collections::BTreeMap; use std::io::{Read, Seek, Write}; use std::os::fd::{AsFd, FromRawFd, IntoRawFd}; use std::rc::Rc; +#[repr(C)] #[derive(Copy, Clone, Default)] pub struct Test { pub x: i32, diff --git a/tests/unit/out/unsafe/polymorphism.rs b/tests/unit/out/unsafe/polymorphism.rs index ee123ce9..3152dceb 100644 --- a/tests/unit/out/unsafe/polymorphism.rs +++ b/tests/unit/out/unsafe/polymorphism.rs @@ -9,6 +9,7 @@ use std::rc::Rc; pub unsafe trait Animal { unsafe fn bark(&self) -> bool; } +#[repr(C)] #[derive(Copy, Clone, Default)] pub struct Dog {} unsafe impl Animal for Dog { @@ -16,6 +17,7 @@ unsafe impl Animal for Dog { return true; } } +#[repr(C)] #[derive(Copy, Clone, Default)] pub struct Cat {} impl Cat { diff --git a/tests/unit/out/unsafe/push_emplace_back.rs b/tests/unit/out/unsafe/push_emplace_back.rs index afa6f328..35c60983 100644 --- a/tests/unit/out/unsafe/push_emplace_back.rs +++ b/tests/unit/out/unsafe/push_emplace_back.rs @@ -6,15 +6,18 @@ use std::collections::BTreeMap; use std::io::{Read, Seek, Write}; use std::os::fd::{AsFd, FromRawFd, IntoRawFd}; use std::rc::Rc; +#[repr(C)] #[derive(Copy, Clone, Default)] pub struct Chunk { pub data: i32, } +#[repr(C)] #[derive(Copy, Clone, Default)] pub struct Writer { pub output: *mut Vec, pub chunk: Chunk, } +#[repr(C)] #[derive(Clone, Default)] pub struct JPEGData { pub com_data: Vec>, diff --git a/tests/unit/out/unsafe/random.rs b/tests/unit/out/unsafe/random.rs index 773659dd..2ff83a5c 100644 --- a/tests/unit/out/unsafe/random.rs +++ b/tests/unit/out/unsafe/random.rs @@ -6,6 +6,7 @@ use std::collections::BTreeMap; use std::io::{Read, Seek, Write}; use std::os::fd::{AsFd, FromRawFd, IntoRawFd}; use std::rc::Rc; +#[repr(C)] #[derive(Copy, Clone)] pub struct Pair { pub x: i32, @@ -52,6 +53,7 @@ impl Default for Pair { pub unsafe fn zero_0() -> i32 { return 0; } +#[repr(C)] #[derive(Copy, Clone, Default)] pub struct X1 {} pub unsafe fn foo_1(mut x1: i32, x2: *mut i32, mut x3: *mut i32, p2: *mut Pair, mut p3: *mut Pair) { diff --git a/tests/unit/out/unsafe/reinterpret_cast_struct.rs b/tests/unit/out/unsafe/reinterpret_cast_struct.rs index 04b9f91e..76cdd709 100644 --- a/tests/unit/out/unsafe/reinterpret_cast_struct.rs +++ b/tests/unit/out/unsafe/reinterpret_cast_struct.rs @@ -6,6 +6,7 @@ use std::collections::BTreeMap; use std::io::{Read, Seek, Write}; use std::os::fd::{AsFd, FromRawFd, IntoRawFd}; use std::rc::Rc; +#[repr(C)] #[derive(Copy, Clone, Default)] pub struct Point { pub x: i32, diff --git a/tests/unit/out/unsafe/reinterpret_cast_struct_to_struct.rs b/tests/unit/out/unsafe/reinterpret_cast_struct_to_struct.rs index 5209888f..21852fbe 100644 --- a/tests/unit/out/unsafe/reinterpret_cast_struct_to_struct.rs +++ b/tests/unit/out/unsafe/reinterpret_cast_struct_to_struct.rs @@ -6,11 +6,13 @@ use std::collections::BTreeMap; use std::io::{Read, Seek, Write}; use std::os::fd::{AsFd, FromRawFd, IntoRawFd}; use std::rc::Rc; +#[repr(C)] #[derive(Copy, Clone, Default)] pub struct Point { pub x: u32, pub y: u32, } +#[repr(C)] #[derive(Copy, Clone, Default)] pub struct Pair { pub first: u32, diff --git a/tests/unit/out/unsafe/reserved_keywords.rs b/tests/unit/out/unsafe/reserved_keywords.rs index 998bb849..d90eabda 100644 --- a/tests/unit/out/unsafe/reserved_keywords.rs +++ b/tests/unit/out/unsafe/reserved_keywords.rs @@ -6,6 +6,7 @@ use std::collections::BTreeMap; use std::io::{Read, Seek, Write}; use std::os::fd::{AsFd, FromRawFd, IntoRawFd}; use std::rc::Rc; +#[repr(C)] #[derive(Copy, Clone, Default)] pub struct S { pub as_: i32, diff --git a/tests/unit/out/unsafe/struct_ctor.rs b/tests/unit/out/unsafe/struct_ctor.rs index a6e534cd..ffcf6f82 100644 --- a/tests/unit/out/unsafe/struct_ctor.rs +++ b/tests/unit/out/unsafe/struct_ctor.rs @@ -6,6 +6,7 @@ use std::collections::BTreeMap; use std::io::{Read, Seek, Write}; use std::os::fd::{AsFd, FromRawFd, IntoRawFd}; use std::rc::Rc; +#[repr(C)] #[derive(Copy, Clone, Default)] pub struct StructWithCtor { x1_: i32, diff --git a/tests/unit/out/unsafe/struct_default_ctor.rs b/tests/unit/out/unsafe/struct_default_ctor.rs index accfa3af..bd1ccbf2 100644 --- a/tests/unit/out/unsafe/struct_default_ctor.rs +++ b/tests/unit/out/unsafe/struct_default_ctor.rs @@ -6,6 +6,7 @@ use std::collections::BTreeMap; use std::io::{Read, Seek, Write}; use std::os::fd::{AsFd, FromRawFd, IntoRawFd}; use std::rc::Rc; +#[repr(C)] #[derive(Clone)] pub struct WOFF2Params { pub extended_metadata: Vec, diff --git a/tests/unit/out/unsafe/struct_ptr.rs b/tests/unit/out/unsafe/struct_ptr.rs index ee77901c..c0d49d77 100644 --- a/tests/unit/out/unsafe/struct_ptr.rs +++ b/tests/unit/out/unsafe/struct_ptr.rs @@ -6,6 +6,7 @@ use std::collections::BTreeMap; use std::io::{Read, Seek, Write}; use std::os::fd::{AsFd, FromRawFd, IntoRawFd}; use std::rc::Rc; +#[repr(C)] #[derive(Copy, Clone, Default)] pub struct XX { pub x: i32, diff --git a/tests/unit/out/unsafe/union_addrof_external.rs b/tests/unit/out/unsafe/union_addrof_external.rs new file mode 100644 index 00000000..c53e5a37 --- /dev/null +++ b/tests/unit/out/unsafe/union_addrof_external.rs @@ -0,0 +1,99 @@ +extern crate libc; +use libc::*; +extern crate libcc2rs; +use libcc2rs::*; +use std::collections::BTreeMap; +use std::io::{Read, Seek, Write}; +use std::os::fd::{AsFd, FromRawFd, IntoRawFd}; +use std::rc::Rc; +#[repr(C)] +#[derive(Copy, Clone)] +pub struct record { + pub code: u16, + pub lo: u16, + pub hi: u32, + pub pad: [u8; 8], +} +impl Default for record { + fn default() -> Self { + record { + code: 0_u16, + lo: 0_u16, + hi: 0_u32, + pad: [0_u8; 8], + } + } +} +#[repr(C)] +#[derive(Copy, Clone)] +pub union Container_anon_0 { + pub h: record, + pub raw_: [u8; 128], +} +impl Default for Container_anon_0 { + fn default() -> Self { + unsafe { std::mem::zeroed() } + } +} +#[repr(C)] +#[derive(Copy, Clone, Default)] +pub struct Container { + pub view: Container_anon_0, +} +pub unsafe fn fill_0(mut out: *mut ::libc::c_void, mut cap: u64) { + let mut src: [u8; 16] = [ + 0_u8, 0_u8, 0_u8, 0_u8, 0_u8, 0_u8, 0_u8, 0_u8, 0_u8, 0_u8, 0_u8, 0_u8, 0_u8, 0_u8, 0_u8, + 0_u8, + ]; + src[(0) as usize] = 2_u8; + src[(1) as usize] = 0_u8; + src[(2) as usize] = 0_u8; + src[(3) as usize] = 80_u8; + src[(4) as usize] = 127_u8; + src[(5) as usize] = 0_u8; + src[(6) as usize] = 0_u8; + src[(7) as usize] = 1_u8; + let mut n: u64 = if ((::std::mem::size_of::<[u8; 16]>() as u64) < (cap)) { + ::std::mem::size_of::<[u8; 16]>() as u64 + } else { + cap + }; + { + if n != 0 { + ::std::ptr::copy_nonoverlapping( + (src.as_mut_ptr() as *const u8 as *const ::libc::c_void), + out, + n as usize, + ) + } + out + }; +} +pub fn main() { + unsafe { + std::process::exit(main_0() as i32); + } +} +unsafe fn main_0() -> i32 { + let mut c: Container = ::default(); + { + let byte_0 = + ((&mut c as *mut Container) as *mut Container as *mut ::libc::c_void) as *mut u8; + for offset in 0..::std::mem::size_of::() as u64 { + *byte_0.offset(offset as isize) = 0 as u8; + } + ((&mut c as *mut Container) as *mut Container as *mut ::libc::c_void) + }; + (unsafe { + let _out: *mut ::libc::c_void = + ((&mut c.view as *mut Container_anon_0) as *mut ::libc::c_void); + let _cap: u64 = ::std::mem::size_of::() as u64; + fill_0(_out, _cap) + }); + assert!(((c.view.h.code as i32) == (2))); + assert!((((*((&mut c.view.h.lo as *mut u16) as *mut u8).offset((0) as isize)) as i32) == (0))); + assert!((((*((&mut c.view.h.lo as *mut u16) as *mut u8).offset((1) as isize)) as i32) == (80))); + assert!(((c.view.raw_[(0) as usize] as i32) == (2))); + assert!((((c.view.raw_[(3) as usize] as u8) as i32) == (80))); + return 0; +} diff --git a/tests/unit/out/unsafe/union_basic.rs b/tests/unit/out/unsafe/union_basic.rs index 1ddfcb9c..81e82df3 100644 --- a/tests/unit/out/unsafe/union_basic.rs +++ b/tests/unit/out/unsafe/union_basic.rs @@ -6,11 +6,17 @@ use std::collections::BTreeMap; use std::io::{Read, Seek, Write}; use std::os::fd::{AsFd, FromRawFd, IntoRawFd}; use std::rc::Rc; -#[derive(Copy, Clone, Default)] -pub struct basic { +#[repr(C)] +#[derive(Copy, Clone)] +pub union basic { pub i: i32, pub f: f32, } +impl Default for basic { + fn default() -> Self { + unsafe { std::mem::zeroed() } + } +} pub fn main() { unsafe { std::process::exit(main_0() as i32); diff --git a/tests/unit/out/unsafe/union_cross_arm_cast.rs b/tests/unit/out/unsafe/union_cross_arm_cast.rs new file mode 100644 index 00000000..3a40ae36 --- /dev/null +++ b/tests/unit/out/unsafe/union_cross_arm_cast.rs @@ -0,0 +1,90 @@ +extern crate libc; +use libc::*; +extern crate libcc2rs; +use libcc2rs::*; +use std::collections::BTreeMap; +use std::io::{Read, Seek, Write}; +use std::os::fd::{AsFd, FromRawFd, IntoRawFd}; +use std::rc::Rc; +#[repr(C)] +#[derive(Copy, Clone)] +pub struct shape_a { + pub code: u16, + pub pad: [u8; 14], +} +impl Default for shape_a { + fn default() -> Self { + shape_a { + code: 0_u16, + pad: [0_u8; 14], + } + } +} +#[repr(C)] +#[derive(Copy, Clone)] +pub struct shape_b { + pub code: u16, + pub lo: u16, + pub mid: u32, + pub fill: [u8; 16], + pub tail: u32, +} +impl Default for shape_b { + fn default() -> Self { + shape_b { + code: 0_u16, + lo: 0_u16, + mid: 0_u32, + fill: [0_u8; 16], + tail: 0_u32, + } + } +} +#[repr(C)] +#[derive(Copy, Clone)] +pub union Container_anon_0 { + pub a: shape_a, + pub b: shape_b, + pub raw_: [u8; 64], +} +impl Default for Container_anon_0 { + fn default() -> Self { + unsafe { std::mem::zeroed() } + } +} +#[repr(C)] +#[derive(Copy, Clone, Default)] +pub struct Container { + pub len: u32, + pub u: Container_anon_0, +} +pub fn main() { + unsafe { + std::process::exit(main_0() as i32); + } +} +unsafe fn main_0() -> i32 { + let mut c: Container = ::default(); + { + let byte_0 = + ((&mut c as *mut Container) as *mut Container as *mut ::libc::c_void) as *mut u8; + for offset in 0..::std::mem::size_of::() as u64 { + *byte_0.offset(offset as isize) = 0 as u8; + } + ((&mut c as *mut Container) as *mut Container as *mut ::libc::c_void) + }; + c.u.a.code = 10_u16; + c.len = (::std::mem::size_of::() as u64 as u32); + (*(((&mut c.u.a as *mut shape_a) as *mut ::libc::c_void) as *mut shape_b)).tail = + 3735928559_u32; + assert!(((c.u.b.tail) == (3735928559_u32))); + assert!(((c.u.b.code as i32) == (10))); + c.u.b.lo = 8080_u16; + assert!( + (((*((&mut c.u.raw_ as *mut [u8; 64]) as *mut u8).offset((2) as isize)) as i32) == (144)) + ); + assert!( + (((*((&mut c.u.raw_ as *mut [u8; 64]) as *mut u8).offset((3) as isize)) as i32) == (31)) + ); + return 0; +} diff --git a/tests/unit/out/unsafe/union_field_alignment.rs b/tests/unit/out/unsafe/union_field_alignment.rs new file mode 100644 index 00000000..623efb1f --- /dev/null +++ b/tests/unit/out/unsafe/union_field_alignment.rs @@ -0,0 +1,37 @@ +extern crate libc; +use libc::*; +extern crate libcc2rs; +use libcc2rs::*; +use std::collections::BTreeMap; +use std::io::{Read, Seek, Write}; +use std::os::fd::{AsFd, FromRawFd, IntoRawFd}; +use std::rc::Rc; +#[repr(C)] +#[derive(Copy, Clone)] +pub union node_anon_0 { + pub bytes: [u8; 1], + pub aligner: *mut ::libc::c_void, +} +impl Default for node_anon_0 { + fn default() -> Self { + unsafe { std::mem::zeroed() } + } +} +#[repr(C)] +#[derive(Copy, Clone, Default)] +pub struct node { + pub next: *mut node, + pub x: node_anon_0, +} +pub fn main() { + unsafe { + std::process::exit(main_0() as i32); + } +} +unsafe fn main_0() -> i32 { + let mut n: node = ::default(); + n.next = Default::default(); + n.x.bytes[(0) as usize] = 171_u8; + assert!(((n.x.bytes[(0) as usize] as i32) == (171))); + return 0; +} diff --git a/tests/unit/out/unsafe/union_memset_memcpy.rs b/tests/unit/out/unsafe/union_memset_memcpy.rs new file mode 100644 index 00000000..1a54f254 --- /dev/null +++ b/tests/unit/out/unsafe/union_memset_memcpy.rs @@ -0,0 +1,112 @@ +extern crate libc; +use libc::*; +extern crate libcc2rs; +use libcc2rs::*; +use std::collections::BTreeMap; +use std::io::{Read, Seek, Write}; +use std::os::fd::{AsFd, FromRawFd, IntoRawFd}; +use std::rc::Rc; +#[repr(C)] +#[derive(Copy, Clone)] +pub struct shape_a { + pub code: u16, + pub pad: [u8; 14], +} +impl Default for shape_a { + fn default() -> Self { + shape_a { + code: 0_u16, + pad: [0_u8; 14], + } + } +} +#[repr(C)] +#[derive(Copy, Clone)] +pub struct shape_b { + pub code: u16, + pub lo: u16, + pub hi: u32, + pub fill: [u8; 8], +} +impl Default for shape_b { + fn default() -> Self { + shape_b { + code: 0_u16, + lo: 0_u16, + hi: 0_u32, + fill: [0_u8; 8], + } + } +} +#[repr(C)] +#[derive(Copy, Clone)] +pub union Container_anon_0 { + pub a: shape_a, + pub b: shape_b, + pub raw_: [u8; 256], +} +impl Default for Container_anon_0 { + fn default() -> Self { + unsafe { std::mem::zeroed() } + } +} +#[repr(C)] +#[derive(Copy, Clone, Default)] +pub struct Container { + pub view: Container_anon_0, +} +pub fn main() { + unsafe { + std::process::exit(main_0() as i32); + } +} +unsafe fn main_0() -> i32 { + let mut c: Container = ::default(); + { + let byte_0 = + ((&mut c as *mut Container) as *mut Container as *mut ::libc::c_void) as *mut u8; + for offset in 0..::std::mem::size_of::() as u64 { + *byte_0.offset(offset as isize) = 0 as u8; + } + ((&mut c as *mut Container) as *mut Container as *mut ::libc::c_void) + }; + assert!(((c.view.a.code as i32) == (0))); + assert!(((c.view.b.lo as i32) == (0))); + assert!(((c.view.raw_[(0) as usize] as i32) == (0))); + assert!(((c.view.raw_[(255) as usize] as i32) == (0))); + let mut src: [u8; 16] = [ + 0_u8, 0_u8, 0_u8, 0_u8, 0_u8, 0_u8, 0_u8, 0_u8, 0_u8, 0_u8, 0_u8, 0_u8, 0_u8, 0_u8, 0_u8, + 0_u8, + ]; + src[(0) as usize] = 2_u8; + src[(2) as usize] = 80_u8; + src[(3) as usize] = 0_u8; + src[(4) as usize] = 127_u8; + src[(5) as usize] = 0_u8; + src[(6) as usize] = 0_u8; + src[(7) as usize] = 1_u8; + let mut len: u64 = 16_u64; + assert!(((len) <= (::std::mem::size_of::<[u8; 256]>() as u64))); + { + if len != 0 { + ::std::ptr::copy_nonoverlapping( + (src.as_mut_ptr() as *const u8 as *const ::libc::c_void), + ((&mut c.view.raw_ as *mut [u8; 256]) as *mut [u8; 256] as *mut ::libc::c_void), + len as usize, + ) + } + ((&mut c.view.raw_ as *mut [u8; 256]) as *mut [u8; 256] as *mut ::libc::c_void) + }; + assert!(((c.view.b.code as i32) == (2))); + assert!((((*((&mut c.view.b.lo as *mut u16) as *mut u8).offset((0) as isize)) as i32) == (80))); + { + let byte_0 = + ((&mut c as *mut Container) as *mut Container as *mut ::libc::c_void) as *mut u8; + for offset in 0..::std::mem::size_of::() as u64 { + *byte_0.offset(offset as isize) = 0 as u8; + } + ((&mut c as *mut Container) as *mut Container as *mut ::libc::c_void) + }; + assert!(((c.view.b.code as i32) == (0))); + return 0; +} diff --git a/tests/unit/out/unsafe/union_nested.rs b/tests/unit/out/unsafe/union_nested.rs new file mode 100644 index 00000000..d50dac02 --- /dev/null +++ b/tests/unit/out/unsafe/union_nested.rs @@ -0,0 +1,83 @@ +extern crate libc; +use libc::*; +extern crate libcc2rs; +use libcc2rs::*; +use std::collections::BTreeMap; +use std::io::{Read, Seek, Write}; +use std::os::fd::{AsFd, FromRawFd, IntoRawFd}; +use std::rc::Rc; +#[repr(C)] +#[derive(Copy, Clone)] +pub struct record { + pub code: u16, + pub pad: [u8; 14], +} +impl Default for record { + fn default() -> Self { + record { + code: 0_u16, + pad: [0_u8; 14], + } + } +} +#[repr(C)] +#[derive(Copy, Clone)] +pub union inner_anon_0 { + pub h: record, + pub raw_: [u8; 128], +} +impl Default for inner_anon_0 { + fn default() -> Self { + unsafe { std::mem::zeroed() } + } +} +#[repr(C)] +#[derive(Copy, Clone, Default)] +pub struct inner { + pub view: inner_anon_0, +} +#[repr(C)] +#[derive(Copy, Clone)] +pub union Outer_anon_0 { + pub h: record, + pub nested: inner, +} +impl Default for Outer_anon_0 { + fn default() -> Self { + unsafe { std::mem::zeroed() } + } +} +#[repr(C)] +#[derive(Copy, Clone, Default)] +pub struct Outer { + pub kind: i32, + pub level: i32, + pub variant: i32, + pub len: u32, + pub body: Outer_anon_0, +} +pub fn main() { + unsafe { + std::process::exit(main_0() as i32); + } +} +unsafe fn main_0() -> i32 { + let mut ex: Outer = ::default(); + { + let byte_0 = ((&mut ex as *mut Outer) as *mut Outer as *mut ::libc::c_void) as *mut u8; + for offset in 0..::std::mem::size_of::() as u64 { + *byte_0.offset(offset as isize) = 0 as u8; + } + ((&mut ex as *mut Outer) as *mut Outer as *mut ::libc::c_void) + }; + ex.kind = 2; + ex.level = 1; + ex.variant = 6; + ex.len = (::std::mem::size_of::() as u64 as u32); + ex.body.h.code = 2_u16; + ex.body.h.pad[(0) as usize] = (('X' as i32) as u8); + assert!(((ex.body.h.code as i32) == (2))); + assert!(((ex.body.h.pad[(0) as usize] as i32) == ('X' as i32))); + assert!(((ex.body.nested.view.h.code as i32) == (2))); + return 0; +} diff --git a/tests/unit/out/unsafe/union_pointer_pun_address.rs b/tests/unit/out/unsafe/union_pointer_pun_address.rs index ad93126d..3115e621 100644 --- a/tests/unit/out/unsafe/union_pointer_pun_address.rs +++ b/tests/unit/out/unsafe/union_pointer_pun_address.rs @@ -6,10 +6,12 @@ use std::collections::BTreeMap; use std::io::{Read, Seek, Write}; use std::os::fd::{AsFd, FromRawFd, IntoRawFd}; use std::rc::Rc; +#[repr(C)] #[derive(Copy, Clone, Default)] pub struct node_a { pub n: i32, } +#[repr(C)] #[derive(Copy, Clone, Default)] pub struct node_b { pub data: *mut ::libc::c_void, @@ -22,10 +24,16 @@ pub fn main() { } unsafe fn main_0() -> i32 { let mut a: node_a = node_a { n: 123 }; - #[derive(Copy, Clone, Default)] - pub struct anon_0 { + #[repr(C)] + #[derive(Copy, Clone)] + pub union anon_0 { pub to_a: *mut node_a, pub to_b: *mut node_b, + } + impl Default for anon_0 { + fn default() -> Self { + unsafe { std::mem::zeroed() } + } }; let mut ptr: anon_0 = ::default(); ptr.to_a = (&mut a as *mut node_a); diff --git a/tests/unit/out/unsafe/union_pointer_pun_writethrough.rs b/tests/unit/out/unsafe/union_pointer_pun_writethrough.rs index 40f7088e..908bde23 100644 --- a/tests/unit/out/unsafe/union_pointer_pun_writethrough.rs +++ b/tests/unit/out/unsafe/union_pointer_pun_writethrough.rs @@ -13,10 +13,16 @@ pub fn main() { } unsafe fn main_0() -> i32 { let mut x: i64 = (-1_i32 as i64); - #[derive(Copy, Clone, Default)] - pub struct anon_0 { + #[repr(C)] + #[derive(Copy, Clone)] + pub union anon_0 { pub as_unsigned: *mut u64, pub as_signed: *mut i64, + } + impl Default for anon_0 { + fn default() -> Self { + unsafe { std::mem::zeroed() } + } }; let mut pp: anon_0 = ::default(); pp.as_signed = (&mut x as *mut i64); diff --git a/tests/unit/out/unsafe/union_struct_dual_use.rs b/tests/unit/out/unsafe/union_struct_dual_use.rs new file mode 100644 index 00000000..c5761448 --- /dev/null +++ b/tests/unit/out/unsafe/union_struct_dual_use.rs @@ -0,0 +1,68 @@ +extern crate libc; +use libc::*; +extern crate libcc2rs; +use libcc2rs::*; +use std::collections::BTreeMap; +use std::io::{Read, Seek, Write}; +use std::os::fd::{AsFd, FromRawFd, IntoRawFd}; +use std::rc::Rc; +#[repr(C)] +#[derive(Copy, Clone, Default)] +pub struct Inner { + pub a: i32, + pub b: i32, +} +pub unsafe fn sum_inner_0(mut i: *mut Inner) -> i32 { + return (((*i).a) + ((*i).b)); +} +#[repr(C)] +#[derive(Copy, Clone)] +pub union Outer_anon_0 { + pub inner: Inner, + pub raw_: [u8; 16], +} +impl Default for Outer_anon_0 { + fn default() -> Self { + unsafe { std::mem::zeroed() } + } +} +#[repr(C)] +#[derive(Copy, Clone, Default)] +pub struct Outer { + pub u: Outer_anon_0, +} +pub fn main() { + unsafe { + std::process::exit(main_0() as i32); + } +} +unsafe fn main_0() -> i32 { + let mut standalone: Inner = ::default(); + standalone.a = 3; + standalone.b = 4; + assert!( + ((unsafe { + let _i: *mut Inner = (&mut standalone as *mut Inner); + sum_inner_0(_i) + }) == (7)) + ); + let mut outer: Outer = ::default(); + { + let byte_0 = ((&mut outer as *mut Outer) as *mut Outer as *mut ::libc::c_void) as *mut u8; + for offset in 0..::std::mem::size_of::() as u64 { + *byte_0.offset(offset as isize) = 0 as u8; + } + ((&mut outer as *mut Outer) as *mut Outer as *mut ::libc::c_void) + }; + outer.u.inner.a = 3; + outer.u.inner.b = 4; + assert!( + ((unsafe { + let _i: *mut Inner = (&mut outer.u.inner as *mut Inner); + sum_inner_0(_i) + }) == (7)) + ); + assert!((((outer.u.raw_[(0) as usize] as u8) as i32) == (3))); + assert!((((outer.u.raw_[(4) as usize] as u8) as i32) == (4))); + return 0; +} diff --git a/tests/unit/out/unsafe/union_tagged_many_arms.rs b/tests/unit/out/unsafe/union_tagged_many_arms.rs index 81817191..1f9f7972 100644 --- a/tests/unit/out/unsafe/union_tagged_many_arms.rs +++ b/tests/unit/out/unsafe/union_tagged_many_arms.rs @@ -15,14 +15,21 @@ enum Tag { T_FLOAT = 3, T_REF = 4, } -#[derive(Copy, Clone, Default)] -pub struct Slot_anon_0 { +#[repr(C)] +#[derive(Copy, Clone)] +pub union Slot_anon_0 { pub text: *const u8, pub handle: *mut ::libc::c_void, pub signed_n: i64, pub unsigned_n: u64, pub f: f64, } +impl Default for Slot_anon_0 { + fn default() -> Self { + unsafe { std::mem::zeroed() } + } +} +#[repr(C)] #[derive(Copy, Clone, Default)] pub struct Slot { pub tag: Tag, diff --git a/tests/unit/out/unsafe/union_tagged_simple.rs b/tests/unit/out/unsafe/union_tagged_simple.rs index 0468df97..ea3282b6 100644 --- a/tests/unit/out/unsafe/union_tagged_simple.rs +++ b/tests/unit/out/unsafe/union_tagged_simple.rs @@ -12,11 +12,18 @@ enum Kind { KIND_NONE = 0, KIND_DONE = 1, } -#[derive(Copy, Clone, Default)] -pub struct Event_anon_0 { +#[repr(C)] +#[derive(Copy, Clone)] +pub union Event_anon_0 { pub obj: *mut ::libc::c_void, pub code: i32, } +impl Default for Event_anon_0 { + fn default() -> Self { + unsafe { std::mem::zeroed() } + } +} +#[repr(C)] #[derive(Copy, Clone, Default)] pub struct Event { pub kind: Kind, diff --git a/tests/unit/out/unsafe/union_void_ptr_sized_deref.rs b/tests/unit/out/unsafe/union_void_ptr_sized_deref.rs index 6b542afb..785dec47 100644 --- a/tests/unit/out/unsafe/union_void_ptr_sized_deref.rs +++ b/tests/unit/out/unsafe/union_void_ptr_sized_deref.rs @@ -13,13 +13,20 @@ enum Width { W_32 = 1, W_16 = 2, } -#[derive(Copy, Clone, Default)] -pub struct Sink_anon_0 { +#[repr(C)] +#[derive(Copy, Clone)] +pub union Sink_anon_0 { pub text: *const u8, pub handle: *mut ::libc::c_void, pub signed_n: i64, pub f: f64, } +impl Default for Sink_anon_0 { + fn default() -> Self { + unsafe { std::mem::zeroed() } + } +} +#[repr(C)] #[derive(Copy, Clone, Default)] pub struct Sink { pub width: Width, diff --git a/tests/unit/out/unsafe/unique_ptr.rs b/tests/unit/out/unsafe/unique_ptr.rs index 1582c2c8..dba60cdd 100644 --- a/tests/unit/out/unsafe/unique_ptr.rs +++ b/tests/unit/out/unsafe/unique_ptr.rs @@ -6,6 +6,7 @@ use std::collections::BTreeMap; use std::io::{Read, Seek, Write}; use std::os::fd::{AsFd, FromRawFd, IntoRawFd}; use std::rc::Rc; +#[repr(C)] #[derive(Default)] pub struct SafePointer { pub ptr: Option>, @@ -15,6 +16,7 @@ impl SafePointer { (*self.ptr.as_deref_mut().unwrap()).prefix_inc(); } } +#[repr(C)] #[derive(Copy, Clone, Default)] pub struct Pair { pub x: i32, diff --git a/tests/unit/out/unsafe/unique_ptr_const_deref.rs b/tests/unit/out/unsafe/unique_ptr_const_deref.rs index 4c4c64a9..b7a96774 100644 --- a/tests/unit/out/unsafe/unique_ptr_const_deref.rs +++ b/tests/unit/out/unsafe/unique_ptr_const_deref.rs @@ -6,6 +6,7 @@ use std::collections::BTreeMap; use std::io::{Read, Seek, Write}; use std::os::fd::{AsFd, FromRawFd, IntoRawFd}; use std::rc::Rc; +#[repr(C)] #[derive(Default)] pub struct Holder { pub val: Option>, diff --git a/tests/unit/out/unsafe/unique_ptr_nested.rs b/tests/unit/out/unsafe/unique_ptr_nested.rs index 44448ae3..e316f9f8 100644 --- a/tests/unit/out/unsafe/unique_ptr_nested.rs +++ b/tests/unit/out/unsafe/unique_ptr_nested.rs @@ -6,11 +6,13 @@ use std::collections::BTreeMap; use std::io::{Read, Seek, Write}; use std::os::fd::{AsFd, FromRawFd, IntoRawFd}; use std::rc::Rc; +#[repr(C)] #[derive(Copy, Clone, Default)] pub struct Inner { pub x: i32, pub y: i32, } +#[repr(C)] #[derive(Default)] pub struct Outer { pub inner: Option>, diff --git a/tests/unit/out/unsafe/unique_ptr_struct.rs b/tests/unit/out/unsafe/unique_ptr_struct.rs index e9da7738..726d5da2 100644 --- a/tests/unit/out/unsafe/unique_ptr_struct.rs +++ b/tests/unit/out/unsafe/unique_ptr_struct.rs @@ -6,6 +6,7 @@ use std::collections::BTreeMap; use std::io::{Read, Seek, Write}; use std::os::fd::{AsFd, FromRawFd, IntoRawFd}; use std::rc::Rc; +#[repr(C)] #[derive(Copy, Clone, Default)] pub struct Point { pub x: i32, diff --git a/tests/unit/out/unsafe/va_arg_struct_ctx.rs b/tests/unit/out/unsafe/va_arg_struct_ctx.rs index edc3c758..1b7030b1 100644 --- a/tests/unit/out/unsafe/va_arg_struct_ctx.rs +++ b/tests/unit/out/unsafe/va_arg_struct_ctx.rs @@ -6,6 +6,7 @@ use std::collections::BTreeMap; use std::io::{Read, Seek, Write}; use std::os::fd::{AsFd, FromRawFd, IntoRawFd}; use std::rc::Rc; +#[repr(C)] #[derive(Copy, Clone, Default)] pub struct context { pub verbose: i32, diff --git a/tests/unit/union_addrof_external.c b/tests/unit/union_addrof_external.c index 87a58268..9669e853 100644 --- a/tests/unit/union_addrof_external.c +++ b/tests/unit/union_addrof_external.c @@ -1,5 +1,4 @@ // translation-fail: refcount -// no-compile: unsafe #include #include #include diff --git a/tests/unit/union_basic.c b/tests/unit/union_basic.c index 5d26a336..86731cc2 100644 --- a/tests/unit/union_basic.c +++ b/tests/unit/union_basic.c @@ -1,3 +1,4 @@ +// no-compile: refcount #include union basic { diff --git a/tests/unit/union_cross_arm_cast.c b/tests/unit/union_cross_arm_cast.c index d9af701d..598de31e 100644 --- a/tests/unit/union_cross_arm_cast.c +++ b/tests/unit/union_cross_arm_cast.c @@ -1,4 +1,4 @@ -// no-compile +// no-compile: refcount #include #include #include diff --git a/tests/unit/union_field_alignment.c b/tests/unit/union_field_alignment.c index 4051ff9d..63c957d1 100644 --- a/tests/unit/union_field_alignment.c +++ b/tests/unit/union_field_alignment.c @@ -1,4 +1,4 @@ -// no-compile +// no-compile: refcount #include #include #include diff --git a/tests/unit/union_memset_memcpy.c b/tests/unit/union_memset_memcpy.c index 6311739b..b4a270b6 100644 --- a/tests/unit/union_memset_memcpy.c +++ b/tests/unit/union_memset_memcpy.c @@ -1,4 +1,4 @@ -// no-compile +// no-compile: refcount #include #include #include diff --git a/tests/unit/union_nested.c b/tests/unit/union_nested.c index d046dcc2..b056725e 100644 --- a/tests/unit/union_nested.c +++ b/tests/unit/union_nested.c @@ -1,4 +1,4 @@ -// no-compile +// no-compile: refcount #include #include #include diff --git a/tests/unit/union_pointer_pun_address.c b/tests/unit/union_pointer_pun_address.c index 50dfd767..421bb1f0 100644 --- a/tests/unit/union_pointer_pun_address.c +++ b/tests/unit/union_pointer_pun_address.c @@ -1,5 +1,4 @@ // no-compile: refcount -// panic: unsafe #include struct node_a { diff --git a/tests/unit/union_pointer_pun_writethrough.c b/tests/unit/union_pointer_pun_writethrough.c index 03fbc47f..d29cbc50 100644 --- a/tests/unit/union_pointer_pun_writethrough.c +++ b/tests/unit/union_pointer_pun_writethrough.c @@ -1,5 +1,4 @@ -// panic: refcount -// XFAIL: unsafe +// no-compile: refcount #include int main(void) { diff --git a/tests/unit/union_struct_dual_use.c b/tests/unit/union_struct_dual_use.c index a160e07b..2ddb14f4 100644 --- a/tests/unit/union_struct_dual_use.c +++ b/tests/unit/union_struct_dual_use.c @@ -1,4 +1,4 @@ -// no-compile +// no-compile: refcount #include #include diff --git a/tests/unit/union_tagged_many_arms.c b/tests/unit/union_tagged_many_arms.c index e3677004..c492c67d 100644 --- a/tests/unit/union_tagged_many_arms.c +++ b/tests/unit/union_tagged_many_arms.c @@ -1,3 +1,4 @@ +// no-compile: refcount #include #include diff --git a/tests/unit/union_tagged_simple.c b/tests/unit/union_tagged_simple.c index 6efa44de..590f34bb 100644 --- a/tests/unit/union_tagged_simple.c +++ b/tests/unit/union_tagged_simple.c @@ -1,3 +1,4 @@ +// no-compile: refcount #include typedef enum { diff --git a/tests/unit/union_tagged_struct_arms.c b/tests/unit/union_tagged_struct_arms.c index 52a4de99..3cd71f8a 100644 --- a/tests/unit/union_tagged_struct_arms.c +++ b/tests/unit/union_tagged_struct_arms.c @@ -1,4 +1,4 @@ -// no-compile: unsafe +// no-compile #include #include diff --git a/tests/unit/union_void_ptr_sized_deref.c b/tests/unit/union_void_ptr_sized_deref.c index 586a608e..3d957cfe 100644 --- a/tests/unit/union_void_ptr_sized_deref.c +++ b/tests/unit/union_void_ptr_sized_deref.c @@ -1,3 +1,4 @@ +// no-compile: refcount #include #include #include