Skip to content

Commit 820eb22

Browse files
authored
Key aliases over non-tag types on their spelled name (#553)
1 parent b168af5 commit 820eb22

8 files changed

Lines changed: 52 additions & 3 deletions

File tree

‎cpp2rust/converter/rules/rules_loader.cpp‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -524,7 +524,7 @@ std::string TypeKey(clang::QualType type) {
524524
type = typeof_type->getUnderlyingExpr()->getType();
525525
}
526526
if (auto alias = type->getAs<clang::TypedefType>();
527-
alias && type.getCanonicalType()->isBuiltinType()) {
527+
alias && !type.getCanonicalType()->getAsTagDecl()) {
528528
return DeclKey(alias->getDecl());
529529
}
530530
if (auto sugar = type->getAs<clang::PredefinedSugarType>()) {

‎cpp2rust/rule_preprocessing/cpp_rule_indexer.cpp‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,9 @@ std::string ExprKey(clang::ASTContext &ctx, const clang::Decl *rule) {
215215

216216
std::string TypeKey(const clang::TypedefNameDecl *rule) {
217217
auto type = rule->getUnderlyingType();
218+
if (auto key = RulesLoader::TypeKey(type); !key.empty()) {
219+
return key;
220+
}
218221
while (type->isPointerType() || type->isReferenceType()) {
219222
type = type->getPointeeType();
220223
}

‎rules/signal/src.c‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
#define _GNU_SOURCE
66
#include <signal.h>
77

8+
typedef sig_t t1;
9+
810
int f1(int signum, const struct sigaction *act, struct sigaction *oldact) {
911
return sigaction(signum, act, oldact);
1012
}

‎rules/signal/tgt_refcount.rs‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// Copyright (c) 2022-present INESC-ID.
2+
// Distributed under the MIT license that can be found in the LICENSE file.
3+
4+
use libcc2rs::*;
5+
6+
fn t1() -> FnPtr<fn(i32)> {
7+
FnPtr::null()
8+
}

‎rules/signal/tgt_unsafe.rs‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
// Copyright (c) 2022-present INESC-ID.
22
// Distributed under the MIT license that can be found in the LICENSE file.
33

4+
fn t1() -> Option<unsafe fn(i32)> {
5+
None
6+
}
7+
48
unsafe fn f1(a0: i32, a1: *const ::libc::sigaction, a2: *mut ::libc::sigaction) -> i32 {
59
libc::sigaction(a0, a1, a2)
610
}

‎tests/unit/fn_ptr.cpp‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#include <assert.h>
22
#include <cstddef>
3+
#include <signal.h>
34
#include <stddef.h>
45

56
typedef int (*foo_t)(void *);
@@ -28,6 +29,10 @@ template <class H> struct HashHolder {
2829
HashHolder(const H &h) : h(h) {}
2930
};
3031

32+
static int total = 0;
33+
34+
void add_to_total(int x) { total += x; }
35+
3136
int main() {
3237
foo_t fn = nullptr;
3338
assert(fn == nullptr);
@@ -55,5 +60,10 @@ int main() {
5560

5661
HashHolder<std::size_t (*)(bool)> hh(identity_hash<bool>);
5762
assert(hh.h(true) == 1);
63+
64+
sig_t handler = add_to_total;
65+
handler(2);
66+
handler(3);
67+
assert(total == 5);
5868
return 0;
5969
}

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

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,12 @@ impl HashHolder_unsigned_long__ptr__bool__ {
4040
Self { h: (h.read()) }
4141
}
4242
}
43+
thread_local!(
44+
pub static total_7: Value<i32> = Rc::new(RefCell::new(0));
45+
);
46+
pub fn add_to_total_8(mut x: i32) {
47+
total_7.with(|rc| *rc.borrow_mut() += x);
48+
}
4349
pub fn main() {
4450
__cpp2rust_init_globals();
4551
std::process::exit(main_0());
@@ -71,6 +77,12 @@ fn main_0() -> i32 {
7177
HashHolder_unsigned_long__ptr__bool__::new({ __tmp_0.as_pointer() })
7278
};
7379
assert!((({ hh.h.call(true,) }) == 1_u64));
80+
let mut handler: FnPtr<fn(i32)> = FnPtr::<fn(i32)>::new(add_to_total_8);
81+
({ handler.call(2) });
82+
({ handler.call(3) });
83+
assert!((total_7.with(|rc| *rc.borrow()) == 5));
7484
return 0;
7585
}
76-
pub fn __cpp2rust_init_globals() {}
86+
pub fn __cpp2rust_init_globals() {
87+
let _ = total_7.with(|_| ());
88+
}

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

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,10 @@ impl HashHolder_unsigned_long__ptr__bool__ {
4242
this
4343
}
4444
}
45+
pub static mut total_7: std::cell::LazyCell<i32> = std::cell::LazyCell::new(|| unsafe { 0 });
46+
pub unsafe fn add_to_total_8(mut x: i32) {
47+
(*std::cell::LazyCell::force_mut(&mut *&raw mut total_7)) += x;
48+
}
4549
pub fn main() {
4650
unsafe {
4751
__cpp2rust_init_globals();
@@ -76,6 +80,12 @@ unsafe fn main_0() -> i32 {
7680
HashHolder_unsigned_long__ptr__bool__::new({ &mut __tmp_0 })
7781
};
7882
assert!(((unsafe { (hh.h).unwrap()(true,) }) == (1_u64)));
83+
let mut handler: Option<unsafe fn(i32)> = Some(add_to_total_8);
84+
(unsafe { (handler).unwrap()(2) });
85+
(unsafe { (handler).unwrap()(3) });
86+
assert!(((*std::cell::LazyCell::force_mut(&mut *&raw mut total_7)) == (5)));
7987
return 0;
8088
}
81-
pub unsafe fn __cpp2rust_init_globals() {}
89+
pub unsafe fn __cpp2rust_init_globals() {
90+
std::cell::LazyCell::force(&*&raw const total_7);
91+
}

0 commit comments

Comments
 (0)