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
10 changes: 8 additions & 2 deletions cpp2rust/converter/converter_lib.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -744,8 +744,14 @@ unsigned GetArraySize(clang::QualType array_type) {
}

static std::string GetLocationID(const clang::Decl *decl) {
return GetFileName(decl) + std::to_string(GetLineNumber(decl)) +
std::to_string(GetColumnNumber(decl));
auto id = GetFileName(decl) + std::to_string(GetLineNumber(decl)) +
std::to_string(GetColumnNumber(decl));
if (auto loc = decl->getLocation(); loc.isMacroID()) {
const auto &src_mgr = decl->getASTContext().getSourceManager();
id += std::to_string(src_mgr.getSpellingLineNumber(loc)) +
std::to_string(src_mgr.getSpellingColumnNumber(loc));
}
return id;
}

static std::string GetParamSignature(const clang::Decl *decl) {
Expand Down
27 changes: 27 additions & 0 deletions tests/unit/out/refcount/template_static_member.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,24 @@ thread_local!(
#[derive(Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)]
#[byte_size(1)]
pub struct Static_long_ {}
thread_local!(
pub static lo_3: Value<i32> = Rc::new(RefCell::new(-1_i32));
);
thread_local!(
pub static hi_4: Value<i32> = Rc::new(RefCell::new(1));
);
#[derive(Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)]
#[byte_size(1)]
pub struct Range_int_ {}
thread_local!(
pub static lo_5: Value<i64> = Rc::new(RefCell::new((-2_i32 as i64)));
);
thread_local!(
pub static hi_6: Value<i64> = Rc::new(RefCell::new(2_i64));
);
#[derive(Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)]
#[byte_size(1)]
pub struct Range_long_ {}
pub fn main() {
__cpp2rust_init_globals();
std::process::exit(main_0());
Expand All @@ -34,10 +52,19 @@ fn main_0() -> i32 {
assert!((s_0.with(|rc| *rc.borrow()) == 22));
assert!(((s_1.with(|rc| *rc.borrow()) as i32) == 33));
assert!((s_2.with(|rc| *rc.borrow()) == 55_i64));
assert!((lo_3.with(|rc| *rc.borrow()) == -1_i32) && (hi_4.with(|rc| *rc.borrow()) == 1));
assert!(
(lo_5.with(|rc| *rc.borrow()) == (-2_i32 as i64))
&& (hi_6.with(|rc| *rc.borrow()) == 2_i64)
);
return 0;
}
pub fn __cpp2rust_init_globals() {
let _ = s_0.with(|_| ());
let _ = s_1.with(|_| ());
let _ = s_2.with(|_| ());
let _ = lo_3.with(|_| ());
let _ = hi_4.with(|_| ());
let _ = lo_5.with(|_| ());
let _ = hi_6.with(|_| ());
}
23 changes: 23 additions & 0 deletions tests/unit/out/unsafe/template_static_member.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,17 @@ pub static mut s_2: std::cell::LazyCell<i64> = std::cell::LazyCell::new(|| unsaf
#[repr(C)]
#[derive(Copy, Clone, VaArg, FnPtrArg, Default)]
pub struct Static_long_ {}
pub static mut lo_3: std::cell::LazyCell<i32> = std::cell::LazyCell::new(|| unsafe { -1_i32 });
pub static mut hi_4: std::cell::LazyCell<i32> = std::cell::LazyCell::new(|| unsafe { 1 });
#[repr(C)]
#[derive(Copy, Clone, VaArg, FnPtrArg, Default)]
pub struct Range_int_ {}
pub static mut lo_5: std::cell::LazyCell<i64> =
std::cell::LazyCell::new(|| unsafe { (-2_i32 as i64) });
pub static mut hi_6: std::cell::LazyCell<i64> = std::cell::LazyCell::new(|| unsafe { 2_i64 });
#[repr(C)]
#[derive(Copy, Clone, VaArg, FnPtrArg, Default)]
pub struct Range_long_ {}
pub fn main() {
unsafe {
__cpp2rust_init_globals();
Expand All @@ -31,10 +42,22 @@ unsafe fn main_0() -> i32 {
assert!(((*std::cell::LazyCell::force_mut(&mut *&raw mut s_0)) == (22)));
assert!((((*std::cell::LazyCell::force_mut(&mut *&raw mut s_1)) as i32) == (33)));
assert!(((*std::cell::LazyCell::force_mut(&mut *&raw mut s_2)) == (55_i64)));
assert!(
((*std::cell::LazyCell::force_mut(&mut *&raw mut lo_3)) == (-1_i32))
&& ((*std::cell::LazyCell::force_mut(&mut *&raw mut hi_4)) == (1))
);
assert!(
((*std::cell::LazyCell::force_mut(&mut *&raw mut lo_5)) == (-2_i32 as i64))
&& ((*std::cell::LazyCell::force_mut(&mut *&raw mut hi_6)) == (2_i64))
);
return 0;
}
pub unsafe fn __cpp2rust_init_globals() {
std::cell::LazyCell::force(&*&raw const s_0);
std::cell::LazyCell::force(&*&raw const s_1);
std::cell::LazyCell::force(&*&raw const s_2);
std::cell::LazyCell::force(&*&raw const lo_3);
std::cell::LazyCell::force(&*&raw const hi_4);
std::cell::LazyCell::force(&*&raw const lo_5);
std::cell::LazyCell::force(&*&raw const hi_6);
}
14 changes: 14 additions & 0 deletions tests/unit/template_static_member.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,27 @@ template <class T> struct Static {

template <class T> T Static<T>::s = 55;

template <class T> struct Range {
static T lo;
static T hi;
};

#define DEFINE_RANGE(T, a, b) \
template <> T Range<T>::lo = a; \
template <> T Range<T>::hi = b

DEFINE_RANGE(int, -1, 1);
DEFINE_RANGE(long, -2, 2);

int main() {
Static<int>::s = 22;
Static<char>::s = 33;

assert(Static<int>::s == 22);
assert(Static<char>::s == 33);
assert(Static<long>::s == 55);
assert(Range<int>::lo == -1 && Range<int>::hi == 1);
assert(Range<long>::lo == -2 && Range<long>::hi == 2);

return 0;
}
Loading