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
2 changes: 1 addition & 1 deletion cpp2rust/converter/rules/rules_loader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -524,7 +524,7 @@ std::string TypeKey(clang::QualType type) {
type = typeof_type->getUnderlyingExpr()->getType();
}
if (auto alias = type->getAs<clang::TypedefType>();
alias && type.getCanonicalType()->isBuiltinType()) {
alias && !type.getCanonicalType()->getAsTagDecl()) {
return DeclKey(alias->getDecl());
}
if (auto sugar = type->getAs<clang::PredefinedSugarType>()) {
Expand Down
3 changes: 3 additions & 0 deletions cpp2rust/rule_preprocessing/cpp_rule_indexer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down
2 changes: 2 additions & 0 deletions rules/signal/src.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
#define _GNU_SOURCE
#include <signal.h>

typedef sig_t t1;

int f1(int signum, const struct sigaction *act, struct sigaction *oldact) {
return sigaction(signum, act, oldact);
}
8 changes: 8 additions & 0 deletions rules/signal/tgt_refcount.rs
Original file line number Diff line number Diff line change
@@ -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<fn(i32)> {
FnPtr::null()
}
4 changes: 4 additions & 0 deletions rules/signal/tgt_unsafe.rs
Original file line number Diff line number Diff line change
@@ -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<unsafe fn(i32)> {
None
}

unsafe fn f1(a0: i32, a1: *const ::libc::sigaction, a2: *mut ::libc::sigaction) -> i32 {
libc::sigaction(a0, a1, a2)
}
10 changes: 10 additions & 0 deletions tests/unit/fn_ptr.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include <assert.h>
#include <cstddef>
#include <signal.h>
#include <stddef.h>

typedef int (*foo_t)(void *);
Expand Down Expand Up @@ -28,6 +29,10 @@ template <class H> 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);
Expand Down Expand Up @@ -55,5 +60,10 @@ int main() {

HashHolder<std::size_t (*)(bool)> hh(identity_hash<bool>);
assert(hh.h(true) == 1);

sig_t handler = add_to_total;
handler(2);
handler(3);
assert(total == 5);
return 0;
}
14 changes: 13 additions & 1 deletion tests/unit/out/refcount/fn_ptr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,12 @@ impl HashHolder_unsigned_long__ptr__bool__ {
Self { h: (h.read()) }
}
}
thread_local!(
pub static total_7: Value<i32> = 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());
Expand Down Expand Up @@ -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<fn(i32)> = FnPtr::<fn(i32)>::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(|_| ());
}
12 changes: 11 additions & 1 deletion tests/unit/out/unsafe/fn_ptr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ impl HashHolder_unsigned_long__ptr__bool__ {
this
}
}
pub static mut total_7: std::cell::LazyCell<i32> = 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();
Expand Down Expand Up @@ -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<unsafe fn(i32)> = 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);
}
Loading