diff --git a/cpp2rust/converter/rules/rules_loader.cpp b/cpp2rust/converter/rules/rules_loader.cpp index d1b69f724..6460f1cc9 100644 --- a/cpp2rust/converter/rules/rules_loader.cpp +++ b/cpp2rust/converter/rules/rules_loader.cpp @@ -524,7 +524,7 @@ std::string TypeKey(clang::QualType type) { type = typeof_type->getUnderlyingExpr()->getType(); } if (auto alias = type->getAs(); - alias && type.getCanonicalType()->isBuiltinType()) { + alias && !type.getCanonicalType()->getAsTagDecl()) { return DeclKey(alias->getDecl()); } if (auto sugar = type->getAs()) { diff --git a/cpp2rust/rule_preprocessing/cpp_rule_indexer.cpp b/cpp2rust/rule_preprocessing/cpp_rule_indexer.cpp index 240daa824..e3f3fda4a 100644 --- a/cpp2rust/rule_preprocessing/cpp_rule_indexer.cpp +++ b/cpp2rust/rule_preprocessing/cpp_rule_indexer.cpp @@ -215,6 +215,9 @@ std::string ExprKey(clang::ASTContext &ctx, const clang::Decl *rule) { std::string TypeKey(const clang::TypedefNameDecl *rule) { auto type = rule->getUnderlyingType(); + if (auto key = RulesLoader::TypeKey(type); !key.empty()) { + return key; + } while (type->isPointerType() || type->isReferenceType()) { type = type->getPointeeType(); } diff --git a/rules/signal/src.c b/rules/signal/src.c index 516ab7fb2..1601cc42c 100644 --- a/rules/signal/src.c +++ b/rules/signal/src.c @@ -5,6 +5,8 @@ #define _GNU_SOURCE #include +typedef sig_t t1; + int f1(int signum, const struct sigaction *act, struct sigaction *oldact) { return sigaction(signum, act, oldact); } diff --git a/rules/signal/tgt_refcount.rs b/rules/signal/tgt_refcount.rs new file mode 100644 index 000000000..865c1652e --- /dev/null +++ b/rules/signal/tgt_refcount.rs @@ -0,0 +1,8 @@ +// Copyright (c) 2022-present INESC-ID. +// Distributed under the MIT license that can be found in the LICENSE file. + +use libcc2rs::*; + +fn t1() -> FnPtr { + FnPtr::null() +} diff --git a/rules/signal/tgt_unsafe.rs b/rules/signal/tgt_unsafe.rs index dfa56f892..9908c19a2 100644 --- a/rules/signal/tgt_unsafe.rs +++ b/rules/signal/tgt_unsafe.rs @@ -1,6 +1,10 @@ // Copyright (c) 2022-present INESC-ID. // Distributed under the MIT license that can be found in the LICENSE file. +fn t1() -> Option { + None +} + unsafe fn f1(a0: i32, a1: *const ::libc::sigaction, a2: *mut ::libc::sigaction) -> i32 { libc::sigaction(a0, a1, a2) } diff --git a/tests/unit/fn_ptr.cpp b/tests/unit/fn_ptr.cpp index 25269cadb..cfe3d2a6e 100644 --- a/tests/unit/fn_ptr.cpp +++ b/tests/unit/fn_ptr.cpp @@ -1,5 +1,6 @@ #include #include +#include #include typedef int (*foo_t)(void *); @@ -28,6 +29,10 @@ template struct HashHolder { HashHolder(const H &h) : h(h) {} }; +static int total = 0; + +void add_to_total(int x) { total += x; } + int main() { foo_t fn = nullptr; assert(fn == nullptr); @@ -55,5 +60,10 @@ int main() { HashHolder hh(identity_hash); assert(hh.h(true) == 1); + + sig_t handler = add_to_total; + handler(2); + handler(3); + assert(total == 5); return 0; } diff --git a/tests/unit/out/refcount/fn_ptr.rs b/tests/unit/out/refcount/fn_ptr.rs index 3f9112232..885ca183f 100644 --- a/tests/unit/out/refcount/fn_ptr.rs +++ b/tests/unit/out/refcount/fn_ptr.rs @@ -40,6 +40,12 @@ impl HashHolder_unsigned_long__ptr__bool__ { Self { h: (h.read()) } } } +thread_local!( + pub static total_7: Value = Rc::new(RefCell::new(0)); +); +pub fn add_to_total_8(mut x: i32) { + total_7.with(|rc| *rc.borrow_mut() += x); +} pub fn main() { __cpp2rust_init_globals(); std::process::exit(main_0()); @@ -71,6 +77,12 @@ fn main_0() -> i32 { HashHolder_unsigned_long__ptr__bool__::new({ __tmp_0.as_pointer() }) }; assert!((({ hh.h.call(true,) }) == 1_u64)); + let mut handler: FnPtr = FnPtr::::new(add_to_total_8); + ({ handler.call(2) }); + ({ handler.call(3) }); + assert!((total_7.with(|rc| *rc.borrow()) == 5)); return 0; } -pub fn __cpp2rust_init_globals() {} +pub fn __cpp2rust_init_globals() { + let _ = total_7.with(|_| ()); +} diff --git a/tests/unit/out/unsafe/fn_ptr.rs b/tests/unit/out/unsafe/fn_ptr.rs index 3d6032e46..b1c1ecf33 100644 --- a/tests/unit/out/unsafe/fn_ptr.rs +++ b/tests/unit/out/unsafe/fn_ptr.rs @@ -42,6 +42,10 @@ impl HashHolder_unsigned_long__ptr__bool__ { this } } +pub static mut total_7: std::cell::LazyCell = std::cell::LazyCell::new(|| unsafe { 0 }); +pub unsafe fn add_to_total_8(mut x: i32) { + (*std::cell::LazyCell::force_mut(&mut *&raw mut total_7)) += x; +} pub fn main() { unsafe { __cpp2rust_init_globals(); @@ -76,6 +80,12 @@ unsafe fn main_0() -> i32 { HashHolder_unsigned_long__ptr__bool__::new({ &mut __tmp_0 }) }; assert!(((unsafe { (hh.h).unwrap()(true,) }) == (1_u64))); + let mut handler: Option = Some(add_to_total_8); + (unsafe { (handler).unwrap()(2) }); + (unsafe { (handler).unwrap()(3) }); + assert!(((*std::cell::LazyCell::force_mut(&mut *&raw mut total_7)) == (5))); return 0; } -pub unsafe fn __cpp2rust_init_globals() {} +pub unsafe fn __cpp2rust_init_globals() { + std::cell::LazyCell::force(&*&raw const total_7); +}