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
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ add_custom_command(
)

add_subdirectory(cpp2rust)
add_subdirectory(libc-dep)
add_subdirectory(libcc2rs)
add_subdirectory(tests)

Expand Down
14 changes: 14 additions & 0 deletions libc-dep/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
set(LIBC_DEP_DIR ${CMAKE_SOURCE_DIR}/libc-dep)
set(LIBC_DEP_OUTPUT ${LIBC_DEP_DIR}/target/release/libc_dep.rlib)

add_custom_command(
OUTPUT ${LIBC_DEP_OUTPUT}
COMMAND cargo +${RUST_STABLE_VERSION} build --release
WORKING_DIRECTORY ${LIBC_DEP_DIR}
DEPENDS "${RUST_STAMP_FILE}" ${LIBC_DEP_DIR}/Cargo.toml
)

add_custom_target(
libc_dep ALL
DEPENDS "${LIBC_DEP_OUTPUT}"
)
7 changes: 7 additions & 0 deletions libc-dep/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[package]
name = "libc-dep"
version = "0.1.0"
edition = "2024"

[dependencies]
libc = "0.2"
Empty file added libc-dep/src/lib.rs
Empty file.
2 changes: 1 addition & 1 deletion libcc2rs-macros/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
name = "libcc2rs-macros"
version = "0.1.0"
edition = "2021"
edition = "2024"

[lib]
proc-macro = true
Expand Down
2 changes: 1 addition & 1 deletion libcc2rs-macros/src/goto.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

use proc_macro::TokenStream;
use syn::parse::{Parse, ParseStream};
use syn::{parse_macro_input, Expr, Lifetime, Token};
use syn::{Expr, Lifetime, Token, parse_macro_input};

use crate::state_machine::{Arm, GotoStateMachine, StateMachine};

Expand Down
2 changes: 1 addition & 1 deletion libcc2rs-macros/src/switch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

use proc_macro::TokenStream;
use syn::parse::{Parse, ParseStream};
use syn::{parse_macro_input, Expr, Pat};
use syn::{Expr, Pat, parse_macro_input};

use crate::state_machine::{Arm, DispatchCase, GotoStateMachine, StateMachine, SwitchStateMachine};

Expand Down
39 changes: 25 additions & 14 deletions libcc2rs/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,21 +1,32 @@
set(RUST_LIB_DIR ${CMAKE_SOURCE_DIR}/libcc2rs)
set(RUST_MACROS_DIR ${CMAKE_SOURCE_DIR}/libcc2rs-macros)
set(RUST_BUILD_DIR ${CMAKE_BINARY_DIR}/rust)
file(GLOB_RECURSE LIBCC2RS_SOURCE "${RUST_LIB_DIR}/*.rs")
file(GLOB_RECURSE LIBCC2RS_MACROS_SOURCE "${RUST_MACROS_DIR}/*.rs")
set(LIBCC2RC_LIB_DIR ${CMAKE_SOURCE_DIR}/libcc2rs)
set(LIBCC2RC_MACROS_LIB_DIR ${CMAKE_SOURCE_DIR}/libcc2rs-macros)
set(LIBCC2RS_RLIB ${LIBCC2RC_LIB_DIR}/target/release/liblibcc2rs.rlib)
set(LIBCC2RS_MACROS_RLIB ${LIBCC2RC_LIB_DIR}/target/release/liblibcc2rs-macros.rlib)
file(GLOB_RECURSE LIBCC2RS_SOURCES "${LIBCC2RC_LIB_DIR}/src/*.rs")
file(GLOB_RECURSE LIBCC2RS_MACROS_SOURCES "${LIBCC2RC_MACROS_LIB_DIR}/src/*.rs")

add_custom_command(
OUTPUT ${RUST_BUILD_DIR}/release/liblibcc2rs.rlib
COMMAND ${CMAKE_COMMAND} -E env CARGO_TARGET_DIR=${RUST_BUILD_DIR} cargo build --release
WORKING_DIRECTORY ${RUST_LIB_DIR}
OUTPUT ${LIBCC2RS_RLIB}
COMMAND cargo +${RUST_STABLE_VERSION} build --release
WORKING_DIRECTORY ${LIBCC2RC_LIB_DIR}
DEPENDS
${LIBCC2RS_SOURCE}
${LIBCC2RS_MACROS_SOURCE}
${RUST_LIB_DIR}/Cargo.toml
${RUST_MACROS_DIR}/Cargo.toml
COMMENT "Building libcc2rs"
"${RUST_STAMP_FILE}"
${LIBCC2RS_SOURCES}
${LIBCC2RS_MACROS_SOURCES}
${LIBCC2RC_LIB_DIR}/Cargo.toml
${LIBCC2RC_MACROS_LIB_DIR}/Cargo.toml
)

add_custom_command(
OUTPUT ${LIBCC2RS_MACROS_RLIB}
COMMAND cargo +${RUST_STABLE_VERSION} build --release
WORKING_DIRECTORY ${LIBCC2RC_MACROS_LIB_DIR}
DEPENDS
"${RUST_STAMP_FILE}"
${LIBCC2RS_MACROS_SOURCES}
${LIBCC2RC_MACROS_LIB_DIR}/Cargo.toml
)

add_custom_target(libcc2rs ALL
DEPENDS ${RUST_BUILD_DIR}/release/liblibcc2rs.rlib "${RUST_STAMP_FILE}"
DEPENDS ${LIBCC2RS_RLIB} ${LIBCC2RS_MACROS_RLIB}
)
4 changes: 1 addition & 3 deletions libcc2rs/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
[package]
name = "libcc2rs"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
edition = "2024"

[dependencies]
libcc2rs-macros = { path = "../libcc2rs-macros", version = "0.1.0" }
6 changes: 3 additions & 3 deletions libcc2rs/src/compat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

use std::ffi::c_void;

extern "C" {
unsafe extern "C" {
#[cfg(target_os = "linux")]
#[link_name = "malloc_usable_size"]
fn platform_malloc_size(ptr: *mut c_void) -> usize;
Expand All @@ -21,10 +21,10 @@ extern "C" {
pub unsafe fn malloc_usable_size(ptr: *mut c_void) -> usize {
#[cfg(target_os = "linux")]
{
platform_malloc_size(ptr)
unsafe { platform_malloc_size(ptr) }
}
#[cfg(target_os = "macos")]
{
platform_malloc_size(ptr as *const c_void)
unsafe { platform_malloc_size(ptr as *const c_void) }
}
}
8 changes: 4 additions & 4 deletions libcc2rs/src/dec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ impl<T> UnsafePostfixDec for *const T {
#[inline]
unsafe fn postfix_dec(&mut self) -> Self {
let copy = *self;
*self = self.offset(-1);
unsafe { *self = self.offset(-1) }
copy
}
}
Expand All @@ -85,7 +85,7 @@ impl<T> UnsafePostfixDec for *mut T {
#[inline]
unsafe fn postfix_dec(&mut self) -> Self {
let copy = *self;
*self = self.offset(-1);
unsafe { *self = self.offset(-1) }
copy
}
}
Expand All @@ -100,15 +100,15 @@ pub trait UnsafePrefixDec {
impl<T> UnsafePrefixDec for *const T {
#[inline]
unsafe fn prefix_dec(&mut self) -> Self {
*self = self.offset(-1);
unsafe { *self = self.offset(-1) }
*self
}
}

impl<T> UnsafePrefixDec for *mut T {
#[inline]
unsafe fn prefix_dec(&mut self) -> Self {
*self = self.offset(-1);
unsafe { *self = self.offset(-1) }
*self
}
}
Expand Down
8 changes: 4 additions & 4 deletions libcc2rs/src/inc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ impl<T> UnsafePostfixInc for *const T {
#[inline]
unsafe fn postfix_inc(&mut self) -> Self {
let copy = *self;
*self = self.offset(1);
unsafe { *self = self.offset(1) }
copy
}
}
Expand All @@ -96,7 +96,7 @@ impl<T> UnsafePostfixInc for *mut T {
#[inline]
unsafe fn postfix_inc(&mut self) -> Self {
let copy = *self;
*self = self.offset(1);
unsafe { *self = self.offset(1) }
copy
}
}
Expand All @@ -111,15 +111,15 @@ pub trait UnsafePrefixInc {
impl<T> UnsafePrefixInc for *const T {
#[inline]
unsafe fn prefix_inc(&mut self) -> Self {
*self = self.offset(1);
unsafe { *self = self.offset(1) }
*self
}
}

impl<T> UnsafePrefixInc for *mut T {
#[inline]
unsafe fn prefix_inc(&mut self) -> Self {
*self = self.offset(1);
unsafe { *self = self.offset(1) }
*self
}
}
Expand Down
24 changes: 12 additions & 12 deletions libcc2rs/src/rc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ impl<T> fmt::Debug for PtrKind<T> {
PtrKind::HeapSingle(w) => write!(f, "HeapSingle({:?})", w.as_ptr()),
PtrKind::StackArray(w) => write!(f, "StackArray({:?})", w.as_ptr()),
PtrKind::HeapArray(w) => write!(f, "HeapArray({:?})", w.as_ptr()),
PtrKind::Reinterpreted(ref data) => {
PtrKind::Reinterpreted(data) => {
write!(f, "Reinterpreted(0x{:x})", data.address())
}
}
Expand All @@ -88,12 +88,12 @@ impl<T> Clone for PtrKind<T> {
fn clone(&self) -> Self {
match self {
PtrKind::Null => PtrKind::Null,
PtrKind::Vec(ref weak) => PtrKind::Vec(weak.clone()),
PtrKind::StackSingle(ref weak) => PtrKind::StackSingle(weak.clone()),
PtrKind::HeapSingle(ref weak) => PtrKind::HeapSingle(weak.clone()),
PtrKind::StackArray(ref weak) => PtrKind::StackArray(weak.clone()),
PtrKind::HeapArray(ref weak) => PtrKind::HeapArray(weak.clone()),
PtrKind::Reinterpreted(ref data) => PtrKind::Reinterpreted(Rc::clone(data)),
PtrKind::Vec(weak) => PtrKind::Vec(weak.clone()),
PtrKind::StackSingle(weak) => PtrKind::StackSingle(weak.clone()),
PtrKind::HeapSingle(weak) => PtrKind::HeapSingle(weak.clone()),
PtrKind::StackArray(weak) => PtrKind::StackArray(weak.clone()),
PtrKind::HeapArray(weak) => PtrKind::HeapArray(weak.clone()),
PtrKind::Reinterpreted(data) => PtrKind::Reinterpreted(Rc::clone(data)),
}
}
}
Expand All @@ -105,7 +105,7 @@ impl<T> PtrKind<T> {
PtrKind::StackSingle(w) | PtrKind::HeapSingle(w) => w.as_ptr() as usize,
PtrKind::Vec(w) => w.as_ptr() as usize,
PtrKind::StackArray(w) | PtrKind::HeapArray(w) => w.as_ptr() as usize,
PtrKind::Reinterpreted(ref data) => data.address(),
PtrKind::Reinterpreted(data) => data.address(),
}
}
}
Expand Down Expand Up @@ -346,7 +346,7 @@ impl<T> Ptr<T> {
rc: weak.upgrade().expect("ub: dangling pointer"),
offset: self.offset,
},
PtrKind::Reinterpreted(ref data) => StrongPtr::Reinterpreted {
PtrKind::Reinterpreted(data) => StrongPtr::Reinterpreted {
alloc: Rc::clone(data),
byte_offset: self.offset,
cell: RefCell::new(None),
Expand Down Expand Up @@ -416,7 +416,7 @@ impl<T> Ptr<T> {
Rc::new(SliceOriginalAlloc { weak: weak.clone() }),
self.byte_offset(),
),
PtrKind::Reinterpreted(ref data) => (Rc::clone(data), self.offset),
PtrKind::Reinterpreted(data) => (Rc::clone(data), self.offset),
};

Ptr {
Expand Down Expand Up @@ -879,7 +879,7 @@ impl<T> fmt::Debug for Ptr<T> {
.wrapping_add(self.offset.wrapping_mul(std::mem::size_of::<T>())),
PtrKind::Vec(w) => (Weak::as_ptr(w) as usize)
.wrapping_add(self.offset.wrapping_mul(std::mem::size_of::<T>())),
PtrKind::Reinterpreted(ref data) => data.address().wrapping_add(self.offset),
PtrKind::Reinterpreted(data) => data.address().wrapping_add(self.offset),
};
write!(f, "0x{:x}", addr)
}
Expand Down Expand Up @@ -1191,7 +1191,7 @@ impl<T: ?Sized> PtrDyn<T> {
pub fn upgrade(&self) -> StrongPtrDyn<T> {
match &self.kind {
PtrKindDyn::Null => panic!("ub: dereference of null pointer"),
PtrKindDyn::StackSingle(ref weak) => {
PtrKindDyn::StackSingle(weak) => {
assert_eq!(self.offset, 0, "ub: invalid offset");
StrongPtrDyn {
rc: weak.upgrade().expect("ub: dangling pointer"),
Expand Down
Loading
Loading