Skip to content

Commit 8a23240

Browse files
authored
Distinguish declarations from one macro expansion in decl IDs (#485)
1 parent ea550fb commit 8a23240

4 files changed

Lines changed: 72 additions & 2 deletions

File tree

‎cpp2rust/converter/converter_lib.cpp‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -744,8 +744,14 @@ unsigned GetArraySize(clang::QualType array_type) {
744744
}
745745

746746
static std::string GetLocationID(const clang::Decl *decl) {
747-
return GetFileName(decl) + std::to_string(GetLineNumber(decl)) +
748-
std::to_string(GetColumnNumber(decl));
747+
auto id = GetFileName(decl) + std::to_string(GetLineNumber(decl)) +
748+
std::to_string(GetColumnNumber(decl));
749+
if (auto loc = decl->getLocation(); loc.isMacroID()) {
750+
const auto &src_mgr = decl->getASTContext().getSourceManager();
751+
id += std::to_string(src_mgr.getSpellingLineNumber(loc)) +
752+
std::to_string(src_mgr.getSpellingColumnNumber(loc));
753+
}
754+
return id;
749755
}
750756

751757
static std::string GetParamSignature(const clang::Decl *decl) {

‎tests/unit/out/refcount/template_static_member.rs‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,24 @@ thread_local!(
2424
#[derive(Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)]
2525
#[byte_size(1)]
2626
pub struct Static_long_ {}
27+
thread_local!(
28+
pub static lo_3: Value<i32> = Rc::new(RefCell::new(-1_i32));
29+
);
30+
thread_local!(
31+
pub static hi_4: Value<i32> = Rc::new(RefCell::new(1));
32+
);
33+
#[derive(Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)]
34+
#[byte_size(1)]
35+
pub struct Range_int_ {}
36+
thread_local!(
37+
pub static lo_5: Value<i64> = Rc::new(RefCell::new((-2_i32 as i64)));
38+
);
39+
thread_local!(
40+
pub static hi_6: Value<i64> = Rc::new(RefCell::new(2_i64));
41+
);
42+
#[derive(Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)]
43+
#[byte_size(1)]
44+
pub struct Range_long_ {}
2745
pub fn main() {
2846
__cpp2rust_init_globals();
2947
std::process::exit(main_0());
@@ -34,10 +52,19 @@ fn main_0() -> i32 {
3452
assert!((s_0.with(|rc| *rc.borrow()) == 22));
3553
assert!(((s_1.with(|rc| *rc.borrow()) as i32) == 33));
3654
assert!((s_2.with(|rc| *rc.borrow()) == 55_i64));
55+
assert!((lo_3.with(|rc| *rc.borrow()) == -1_i32) && (hi_4.with(|rc| *rc.borrow()) == 1));
56+
assert!(
57+
(lo_5.with(|rc| *rc.borrow()) == (-2_i32 as i64))
58+
&& (hi_6.with(|rc| *rc.borrow()) == 2_i64)
59+
);
3760
return 0;
3861
}
3962
pub fn __cpp2rust_init_globals() {
4063
let _ = s_0.with(|_| ());
4164
let _ = s_1.with(|_| ());
4265
let _ = s_2.with(|_| ());
66+
let _ = lo_3.with(|_| ());
67+
let _ = hi_4.with(|_| ());
68+
let _ = lo_5.with(|_| ());
69+
let _ = hi_6.with(|_| ());
4370
}

‎tests/unit/out/unsafe/template_static_member.rs‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,17 @@ pub static mut s_2: std::cell::LazyCell<i64> = std::cell::LazyCell::new(|| unsaf
1919
#[repr(C)]
2020
#[derive(Copy, Clone, VaArg, FnPtrArg, Default)]
2121
pub struct Static_long_ {}
22+
pub static mut lo_3: std::cell::LazyCell<i32> = std::cell::LazyCell::new(|| unsafe { -1_i32 });
23+
pub static mut hi_4: std::cell::LazyCell<i32> = std::cell::LazyCell::new(|| unsafe { 1 });
24+
#[repr(C)]
25+
#[derive(Copy, Clone, VaArg, FnPtrArg, Default)]
26+
pub struct Range_int_ {}
27+
pub static mut lo_5: std::cell::LazyCell<i64> =
28+
std::cell::LazyCell::new(|| unsafe { (-2_i32 as i64) });
29+
pub static mut hi_6: std::cell::LazyCell<i64> = std::cell::LazyCell::new(|| unsafe { 2_i64 });
30+
#[repr(C)]
31+
#[derive(Copy, Clone, VaArg, FnPtrArg, Default)]
32+
pub struct Range_long_ {}
2233
pub fn main() {
2334
unsafe {
2435
__cpp2rust_init_globals();
@@ -31,10 +42,22 @@ unsafe fn main_0() -> i32 {
3142
assert!(((*std::cell::LazyCell::force_mut(&mut *&raw mut s_0)) == (22)));
3243
assert!((((*std::cell::LazyCell::force_mut(&mut *&raw mut s_1)) as i32) == (33)));
3344
assert!(((*std::cell::LazyCell::force_mut(&mut *&raw mut s_2)) == (55_i64)));
45+
assert!(
46+
((*std::cell::LazyCell::force_mut(&mut *&raw mut lo_3)) == (-1_i32))
47+
&& ((*std::cell::LazyCell::force_mut(&mut *&raw mut hi_4)) == (1))
48+
);
49+
assert!(
50+
((*std::cell::LazyCell::force_mut(&mut *&raw mut lo_5)) == (-2_i32 as i64))
51+
&& ((*std::cell::LazyCell::force_mut(&mut *&raw mut hi_6)) == (2_i64))
52+
);
3453
return 0;
3554
}
3655
pub unsafe fn __cpp2rust_init_globals() {
3756
std::cell::LazyCell::force(&*&raw const s_0);
3857
std::cell::LazyCell::force(&*&raw const s_1);
3958
std::cell::LazyCell::force(&*&raw const s_2);
59+
std::cell::LazyCell::force(&*&raw const lo_3);
60+
std::cell::LazyCell::force(&*&raw const hi_4);
61+
std::cell::LazyCell::force(&*&raw const lo_5);
62+
std::cell::LazyCell::force(&*&raw const hi_6);
4063
}

‎tests/unit/template_static_member.cpp‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,27 @@ template <class T> struct Static {
66

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

9+
template <class T> struct Range {
10+
static T lo;
11+
static T hi;
12+
};
13+
14+
#define DEFINE_RANGE(T, a, b) \
15+
template <> T Range<T>::lo = a; \
16+
template <> T Range<T>::hi = b
17+
18+
DEFINE_RANGE(int, -1, 1);
19+
DEFINE_RANGE(long, -2, 2);
20+
921
int main() {
1022
Static<int>::s = 22;
1123
Static<char>::s = 33;
1224

1325
assert(Static<int>::s == 22);
1426
assert(Static<char>::s == 33);
1527
assert(Static<long>::s == 55);
28+
assert(Range<int>::lo == -1 && Range<int>::hi == 1);
29+
assert(Range<long>::lo == -2 && Range<long>::hi == 2);
1630

1731
return 0;
1832
}

0 commit comments

Comments
 (0)