Skip to content

Commit

Permalink
(c2rust-analyze) Allow #[] attributes on each known_fns! fn s…
Browse files Browse the repository at this point in the history
…o platform specific things can be represented, such as Linux's `__errno_location` and macOS' `__error`.
  • Loading branch information
kkysen committed Jul 6, 2023
1 parent fdbbcf9 commit 5a64de9
Showing 1 changed file with 24 additions and 12 deletions.
36 changes: 24 additions & 12 deletions c2rust-analyze/src/known_fn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ macro_rules! known_fns {
mod $module:ident {

$(
$(#[$attr:meta])?
fn $name:ident(
$($arg_name:ident: $arg_ty:ty$(: $arg_perms:tt)?,)*
) -> $return_ty:ty$(: $return_perms:tt)?
Expand All @@ -92,20 +93,23 @@ macro_rules! known_fns {
} => {{
use $module::*;

const_slice!(KnownFn, [$({
unsafe extern "C" fn $name($($arg_name: $arg_ty,)*) -> $return_ty {
$(let _ = $arg_name;)*
todo!()
}
// ensure the definitions match
[$name, $module::$name];
const_slice!(KnownFn, [$(
$(#[$attr])?
{
unsafe extern "C" fn $name($($arg_name: $arg_ty,)*) -> $return_ty {
$(let _ = $arg_name;)*
todo!()
}
// ensure the definitions match
[$name, $module::$name];

KnownFn {
name: stringify!($name),
inputs: const_slice!(KnownFnTy, [$(known_fn_ty!($arg_ty$(: $arg_perms)?).named(stringify!($arg_name)),)*]),
output: known_fn_ty!($return_ty$(: $return_perms)?),
KnownFn {
name: stringify!($name),
inputs: const_slice!(KnownFnTy, [$(known_fn_ty!($arg_ty$(: $arg_perms)?).named(stringify!($arg_name)),)*]),
output: known_fn_ty!($return_ty$(: $return_perms)?),
}
}
},)*])
,)*])
}};
}

Expand Down Expand Up @@ -212,20 +216,28 @@ mod tests {
);
}

#[cfg(any(target_os = "linux", target_os = "macos"))]
#[test]
fn __errno_location() {
assert_eq!(
known_fns! {
mod libc {

#[cfg(target_os = "linux")]
fn __errno_location() -> *mut c_int: [READ | WRITE];

#[cfg(target_os = "macos")]
fn __error() -> *mut c_int: [READ | WRITE];

}
},
const_slice!(
KnownFn,
[KnownFn {
#[cfg(target_os = "linux")]
name: "__errno_location",
#[cfg(target_os = "macos")]
name: "__errno",
inputs: &[],
output: KnownFnTy {
name: "",
Expand Down

0 comments on commit 5a64de9

Please sign in to comment.