Skip to content

Commit 99df450

Browse files
committed
TODO: Using { $($body:tt)* } instead of
$body:block) because of rustc bug refered to in dtolnay/paste#50
1 parent 0734b5a commit 99df450

File tree

1 file changed

+22
-20
lines changed

1 file changed

+22
-20
lines changed

src/ld_preload.rs

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,12 @@ pub fn make_dispatch(tracevar: &str) -> (bool, Dispatch, WorkerGuard) {
4646
(tracing, Dispatch::new(subscriber), guard)
4747
}
4848

49+
/* TODO: Using { $($body:tt)* } instead of $body:block) because of rustc bug refered to
50+
in https://github.com/dtolnay/paste/issues/50 */
4951
#[macro_export]
5052
macro_rules! hook {
5153

52-
(unsafe fn $real_fn:ident ( $($v:ident : $t:ty),* ) -> $r:ty => $hook_fn:ident $body:block) => {
54+
(unsafe fn $real_fn:ident ( $($v:ident : $t:ty),* ) -> $r:ty => $hook_fn:ident { $($body:tt)* }) => {
5355
#[allow(non_camel_case_types)]
5456
pub struct $real_fn {__private_field: ()}
5557
#[allow(non_upper_case_globals)]
@@ -83,33 +85,33 @@ macro_rules! hook {
8385
// #[instrument(skip( $($v),* ))]
8486
pub unsafe fn $hook_fn ( $($v : $t),* ) -> $r {
8587
if stringify!($real_fn) == "fopen" && !MY_DISPATCH_initialized.with(Cell::get) {
86-
$body
88+
$($body)*
8789
} else {
8890
MY_DISPATCH.with(|(tracing, my_dispatch, _guard)| {
8991
// println!("tracing: {}", tracing);
9092
if *tracing {
9193
with_default(&my_dispatch, || {
9294
event!(Level::INFO, "{}()", stringify!($real_fn));
93-
$body
95+
$($body)*
9496
})
9597
} else {
96-
$body
98+
$($body)*
9799
}
98100
})
99101
}
100102
}
101103
};
102104

103-
(unsafe fn $real_fn:ident ( $($v:ident : $t:ty),* ) => $hook_fn:ident $body:block) => {
104-
$crate::hook! { unsafe fn $real_fn ( $($v : $t),* ) -> () => $hook_fn $body }
105+
(unsafe fn $real_fn:ident ( $($v:ident : $t:ty),* ) => $hook_fn:ident { $($body:tt)* }) => {
106+
$crate::hook! { unsafe fn $real_fn ( $($v : $t),* ) -> () => $hook_fn { $($body)* } }
105107
};
106108
}
107109

108110

109111
#[macro_export]
110112
macro_rules! vhook {
111113

112-
(unsafe fn $real_fn:ident ( $va:ident : $vaty:ty, $($v:ident : $t:ty),* ) -> $r:ty => $hook_fn:ident $body:block) => {
114+
(unsafe fn $real_fn:ident ( $va:ident : $vaty:ty, $($v:ident : $t:ty),* ) -> $r:ty => $hook_fn:ident { $($body:tt)* }) => {
113115
#[allow(non_camel_case_types)]
114116
pub struct $real_fn {__private_field: ()}
115117
#[allow(non_upper_case_globals)]
@@ -153,17 +155,17 @@ macro_rules! vhook {
153155
if *tracing {
154156
with_default(&my_dispatch, || {
155157
event!(Level::INFO, "{}()", stringify!($real_fn));
156-
$body
158+
$($body)*
157159
})
158160
} else {
159-
$body
161+
$($body)*
160162
}
161163
})
162164
}
163165
};
164166

165-
(unsafe fn $real_fn:ident ( $($v:ident : $t:ty),* ) => $hook_fn:ident $body:block) => {
166-
$crate::hook! { unsafe fn $real_fn ( $($v : $t),* ) -> () => $hook_fn $body }
167+
(unsafe fn $real_fn:ident ( $($v:ident : $t:ty),* ) => $hook_fn:ident { $($body:tt)* }) => {
168+
$crate::hook! { unsafe fn $real_fn ( $($v : $t),* ) -> () => $hook_fn { $($body)* } }
167169
};
168170
}
169171

@@ -173,7 +175,7 @@ macro_rules! dhook {
173175
// The orig_hook is needed because variadic functions cannot be associated functions for a structure
174176
// There is a bug open on this against rust and is being fixed.
175177
// Until then we need to store the real function pointer in a separately named structure
176-
(unsafe fn $real_fn:ident ( $va:ident : $vaty:ty, $($v:ident : $t:ty),* ) -> $r:ty => ($hook_fn:ident, $orig_fn:ident) $body:block) => {
178+
(unsafe fn $real_fn:ident ( $va:ident : $vaty:ty, $($v:ident : $t:ty),* ) -> $r:ty => ($hook_fn:ident, $orig_fn:ident) { $($body:tt)* }) => {
177179
#[allow(non_camel_case_types)]
178180
pub struct $orig_fn {__private_field: ()}
179181
#[allow(non_upper_case_globals)]
@@ -195,10 +197,10 @@ macro_rules! dhook {
195197
}
196198
}
197199

198-
$crate::dhook! { unsafe fn $real_fn ( $va: $vaty, $($v : $t),* ) -> $r => $hook_fn $body }
200+
$crate::dhook! { unsafe fn $real_fn ( $va: $vaty, $($v : $t),* ) -> $r => $hook_fn { $($body)* } }
199201
};
200202

201-
(unsafe fn $real_fn:ident ( $va:ident : $vaty:ty, $($v:ident : $t:ty),* ) -> $r:ty => $hook_fn:ident $body:block) => {
203+
(unsafe fn $real_fn:ident ( $va:ident : $vaty:ty, $($v:ident : $t:ty),* ) -> $r:ty => $hook_fn:ident { $($body:tt)* }) => {
202204
#[no_mangle]
203205
pub unsafe extern "C" fn $real_fn ( $($v : $t),* , $va: ...) -> $r {
204206
::std::panic::catch_unwind(|| {
@@ -215,21 +217,21 @@ macro_rules! dhook {
215217
if *tracing {
216218
with_default(&my_dispatch, || {
217219
event!(Level::INFO, "{}()", stringify!($real_fn));
218-
$body
220+
$($body)*
219221
})
220222
} else {
221-
$body
223+
$($body)*
222224
}
223225
})
224226
}
225227
};
226228

227-
(unsafe fn $real_fn:ident ( $va:ident : $vaty:ty, $($v:ident : $t:ty),* ) => ($hook_fn:ident, $hook_str:ident) $body:block) => {
228-
$crate::dhook! { unsafe fn $real_fn ( $va: $vaty, $($v : $t),* ) -> () => ($hook_fn, $hook_str) $body }
229+
(unsafe fn $real_fn:ident ( $va:ident : $vaty:ty, $($v:ident : $t:ty),* ) => ($hook_fn:ident, $hook_str:ident) { $($body:tt)* }) => {
230+
$crate::dhook! { unsafe fn $real_fn ( $va: $vaty, $($v : $t),* ) -> () => ($hook_fn, $hook_str) { $($body)* } }
229231
};
230232

231-
(unsafe fn $real_fn:ident ( $va:ident : $vaty:ty, $($v:ident : $t:ty),* ) => $hook_fn:ident $body:block) => {
232-
$crate::dhook! { unsafe fn $real_fn ( $va: $vaty, $($v : $t),* ) -> () => $hook_fn $body }
233+
(unsafe fn $real_fn:ident ( $va:ident : $vaty:ty, $($v:ident : $t:ty),* ) => $hook_fn:ident { $($body:tt)* }) => {
234+
$crate::dhook! { unsafe fn $real_fn ( $va: $vaty, $($v : $t),* ) -> () => $hook_fn { $($body)* } }
233235
};
234236

235237
}

0 commit comments

Comments
 (0)