Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions cpp2rust/converter/converter_lib.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -968,6 +968,10 @@ std::string GetNamedDeclAsString(const clang::NamedDecl *decl) {
// keyword -> keyword_
// keyword_ -> keyword__
// etc
if (!name.empty() &&
std::ranges::all_of(name, [](char c) { return c == '_'; })) {
name += '_';
}
for (auto &keyword : rust_keywords) {
if (!name.starts_with(keyword))
continue;
Expand Down
30 changes: 30 additions & 0 deletions tests/unit/out/refcount/union_basic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,28 @@ impl Default for empty {
}
}
}
#[derive(VaArg, FnPtrArg, ByteRepr, DeepClone)]
#[byte_size(8)]
pub struct underscores {
#[offset(0)]
#[byte_size(8)]
__bytes: Value<Box<[u8]>>,
}
impl underscores {
pub fn __(this: Ptr<Self>) -> Ptr<i32> {
this.reinterpret_cast()
}
pub fn ___(this: Ptr<Self>) -> Ptr<f64> {
this.reinterpret_cast()
}
}
impl Default for underscores {
fn default() -> Self {
underscores {
__bytes: Rc::new(RefCell::new(Box::from([0u8; 8]))),
}
}
}
pub fn main() {
__cpp2rust_init_globals();
std::process::exit(main_0());
Expand Down Expand Up @@ -71,6 +93,14 @@ fn main_0() -> i32 {
assert!(((basic::i(ru.clone()).read()) == 1065353216));
assert!(((pf.read()) == 1_f32));
assert!((((*buf.borrow())[(3) as usize] as i32) == 63));
let us: Value<underscores> = Rc::new(RefCell::new(<underscores>::default()));
underscores::__(us.as_pointer()).write(5);
assert!(((underscores::__(us.as_pointer()).read()) == 5));
underscores::___(us.as_pointer()).write(2.5_f64);
assert!(((underscores::___(us.as_pointer()).read()) == 2.5_f64));
let mut __: i32 = 1;
let mut ___: i32 = 2;
assert!(((__ + ___) == 3));
return 0;
}
pub fn __cpp2rust_init_globals() {}
19 changes: 19 additions & 0 deletions tests/unit/out/unsafe/union_basic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,17 @@ impl Default for empty {
unsafe { std::mem::zeroed() }
}
}
#[repr(C)]
#[derive(Copy, Clone, FnPtrArg, VaArg)]
pub union underscores {
pub __: i32,
pub ___: f64,
}
impl Default for underscores {
fn default() -> Self {
unsafe { std::mem::zeroed() }
}
}
pub fn main() {
unsafe {
__cpp2rust_init_globals();
Expand Down Expand Up @@ -58,6 +69,14 @@ unsafe fn main_0() -> i32 {
assert!((((*ru).i) == (1065353216)));
assert!(((*pf) == (1_f32)));
assert!(((buf[(3) as usize] as i32) == (63)));
let mut us: underscores = <underscores>::default();
us.__ = 5;
assert!(((us.__) == (5)));
us.___ = 2.5_f64;
assert!(((us.___) == (2.5_f64)));
let mut __: i32 = 1;
let mut ___: i32 = 2;
assert!((((__) + (___)) == (3)));
return 0;
}
pub unsafe fn __cpp2rust_init_globals() {}
15 changes: 15 additions & 0 deletions tests/unit/union_basic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ union basic {

union empty {};

union underscores {
int _;
double __;
};

int main(void) {
union basic u;
union empty e;
Expand All @@ -33,5 +38,15 @@ int main(void) {
assert(*pf == 1.0f);
assert(buf[3] == 0x3F);

union underscores us;
us._ = 5;
assert(us._ == 5);
us.__ = 2.5;
assert(us.__ == 2.5);

int _ = 1;
int __ = 2;
assert(_ + __ == 3);

return 0;
}
Loading