@@ -27,8 +27,7 @@ use syn::{ItemFn, parse_macro_input};
27
27
/// }
28
28
/// ```
29
29
///
30
- /// This macro will create a trace record when the function is called, if the `trace_guest`
31
- /// feature is enabled.
30
+ /// This macro will create a trace record when the function is called
32
31
///
33
32
/// The trace record will contain the function name as a string.
34
33
/// Note: This macro is intended to be used with the `hyperlight_guest_tracing` crate.
@@ -45,47 +44,57 @@ pub fn trace_function(_attr: TokenStream, item: TokenStream) -> TokenStream {
45
44
let fn_output = & input_fn. sig . output ;
46
45
47
46
// Compose entry/exit messages
48
- let entry_msg = format ! ( "> {}" , fn_name_str) ;
49
- let exit_msg = format ! ( "< {}" , fn_name_str) ;
47
+ let _entry_msg = format ! ( "> {}" , fn_name_str) ;
48
+ let _exit_msg = format ! ( "< {}" , fn_name_str) ;
50
49
51
50
let expanded = match fn_output {
52
51
syn:: ReturnType :: Default => {
53
52
// No return value (unit)
53
+ #[ cfg( feature = "trace" ) ]
54
54
quote ! {
55
55
#( #fn_attrs) *
56
56
#fn_vis #fn_sig {
57
- #[ cfg( feature = "trace_guest" ) ]
58
57
const _: ( ) = assert!(
59
- #entry_msg . len( ) <= hyperlight_guest_tracing:: MAX_TRACE_MSG_LEN ,
58
+ #_entry_msg . len( ) <= hyperlight_guest_tracing:: MAX_TRACE_MSG_LEN ,
60
59
"Trace message exceeds the maximum bytes length" ,
61
60
) ;
62
- #[ cfg( feature = "trace_guest" ) ]
63
- :: hyperlight_guest_tracing:: create_trace_record( #entry_msg) ;
61
+ :: hyperlight_guest_tracing:: create_trace_record( #_entry_msg) ;
64
62
// Call the original function body
65
63
#fn_block
66
- #[ cfg( feature = "trace_guest" ) ]
67
- :: hyperlight_guest_tracing:: create_trace_record( #exit_msg) ;
64
+ :: hyperlight_guest_tracing:: create_trace_record( #_exit_msg) ;
65
+ }
66
+ }
67
+ #[ cfg( not( feature = "trace" ) ) ]
68
+ quote ! {
69
+ #( #fn_attrs) *
70
+ #fn_vis #fn_sig {
71
+ #fn_block
68
72
}
69
73
}
70
74
}
71
75
syn:: ReturnType :: Type ( _, _) => {
72
76
// Has a return value
77
+ #[ cfg( feature = "trace" ) ]
73
78
quote ! {
74
79
#( #fn_attrs) *
75
80
#fn_vis #fn_sig {
76
- #[ cfg( feature = "trace_guest" ) ]
77
81
const _: ( ) = assert!(
78
- #entry_msg . len( ) <= hyperlight_guest_tracing:: MAX_TRACE_MSG_LEN ,
82
+ #_entry_msg . len( ) <= hyperlight_guest_tracing:: MAX_TRACE_MSG_LEN ,
79
83
"Trace message exceeds the maximum bytes length" ,
80
84
) ;
81
- #[ cfg( feature = "trace_guest" ) ]
82
- :: hyperlight_guest_tracing:: create_trace_record( #entry_msg) ;
85
+ :: hyperlight_guest_tracing:: create_trace_record( #_entry_msg) ;
83
86
let __trace_result = ( || #fn_block ) ( ) ;
84
- #[ cfg( feature = "trace_guest" ) ]
85
- :: hyperlight_guest_tracing:: create_trace_record( #exit_msg) ;
87
+ :: hyperlight_guest_tracing:: create_trace_record( #_exit_msg) ;
86
88
__trace_result
87
89
}
88
90
}
91
+ #[ cfg( not( feature = "trace" ) ) ]
92
+ quote ! {
93
+ #( #fn_attrs) *
94
+ #fn_vis #fn_sig {
95
+ #fn_block
96
+ }
97
+ }
89
98
}
90
99
} ;
91
100
@@ -202,36 +211,41 @@ pub fn trace(input: TokenStream) -> TokenStream {
202
211
_ => unreachable ! ( ) ,
203
212
} ;
204
213
if let Some ( statement) = parsed. statement {
205
- let entry_msg = format ! ( "+ {}" , trace_message) ;
206
- let exit_msg = format ! ( "- {}" , trace_message) ;
214
+ let _entry_msg = format ! ( "+ {}" , trace_message) ;
215
+ let _exit_msg = format ! ( "- {}" , trace_message) ;
216
+ #[ cfg( feature = "trace" ) ]
207
217
let expanded = quote ! {
208
218
{
209
- #[ cfg( feature = "trace_guest" ) ]
210
219
const _: ( ) = assert!(
211
- #entry_msg . len( ) <= hyperlight_guest_tracing:: MAX_TRACE_MSG_LEN ,
220
+ #_entry_msg . len( ) <= hyperlight_guest_tracing:: MAX_TRACE_MSG_LEN ,
212
221
"Trace message exceeds the maximum bytes length" ,
213
222
) ;
214
- #[ cfg( feature = "trace_guest" ) ]
215
- :: hyperlight_guest_tracing:: create_trace_record( #entry_msg) ;
223
+ :: hyperlight_guest_tracing:: create_trace_record( #_entry_msg) ;
216
224
let __trace_result = #statement;
217
- #[ cfg( feature = "trace_guest" ) ]
218
- :: hyperlight_guest_tracing:: create_trace_record( #exit_msg) ;
225
+ :: hyperlight_guest_tracing:: create_trace_record( #_exit_msg) ;
219
226
__trace_result
220
227
}
221
228
} ;
229
+ #[ cfg( not( feature = "trace" ) ) ]
230
+ let expanded = quote ! {
231
+ #statement
232
+ } ;
233
+
222
234
TokenStream :: from ( expanded)
223
235
} else {
236
+ #[ cfg( feature = "trace" ) ]
224
237
let expanded = quote ! {
225
238
{
226
- #[ cfg( feature = "trace_guest" ) ]
227
239
const _: ( ) = assert!(
228
240
#trace_message. len( ) <= hyperlight_guest_tracing:: MAX_TRACE_MSG_LEN ,
229
241
"Trace message exceeds the maximum bytes length" ,
230
242
) ;
231
- #[ cfg( feature = "trace_guest" ) ]
232
243
:: hyperlight_guest_tracing:: create_trace_record( #trace_message) ;
233
244
}
234
245
} ;
246
+ #[ cfg( not( feature = "trace" ) ) ]
247
+ let expanded = quote ! { } ;
248
+
235
249
TokenStream :: from ( expanded)
236
250
}
237
251
}
@@ -244,12 +258,14 @@ pub fn trace(input: TokenStream) -> TokenStream {
244
258
/// ```
245
259
#[ proc_macro]
246
260
pub fn flush ( _input : TokenStream ) -> TokenStream {
261
+ #[ cfg( feature = "trace" ) ]
247
262
let expanded = quote ! {
248
263
{
249
- #[ cfg( feature = "trace_guest" ) ]
250
264
:: hyperlight_guest_tracing:: flush_trace_buffer( ) ;
251
265
}
252
266
} ;
267
+ #[ cfg( not( feature = "trace" ) ) ]
268
+ let expanded = quote ! { } ;
253
269
254
270
TokenStream :: from ( expanded)
255
271
}
0 commit comments