@@ -27,8 +27,7 @@ use syn::{ItemFn, parse_macro_input};
2727/// }
2828/// ```
2929///
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
3231///
3332/// The trace record will contain the function name as a string.
3433/// 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 {
4544 let fn_output = & input_fn. sig . output ;
4645
4746 // 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) ;
5049
5150 let expanded = match fn_output {
5251 syn:: ReturnType :: Default => {
5352 // No return value (unit)
53+ #[ cfg( feature = "trace" ) ]
5454 quote ! {
5555 #( #fn_attrs) *
5656 #fn_vis #fn_sig {
57- #[ cfg( feature = "trace_guest" ) ]
5857 const _: ( ) = assert!(
59- #entry_msg . len( ) <= hyperlight_guest_tracing:: MAX_TRACE_MSG_LEN ,
58+ #_entry_msg . len( ) <= hyperlight_guest_tracing:: MAX_TRACE_MSG_LEN ,
6059 "Trace message exceeds the maximum bytes length" ,
6160 ) ;
62- #[ cfg( feature = "trace_guest" ) ]
63- :: hyperlight_guest_tracing:: create_trace_record( #entry_msg) ;
61+ :: hyperlight_guest_tracing:: create_trace_record( #_entry_msg) ;
6462 // Call the original function body
6563 #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
6872 }
6973 }
7074 }
7175 syn:: ReturnType :: Type ( _, _) => {
7276 // Has a return value
77+ #[ cfg( feature = "trace" ) ]
7378 quote ! {
7479 #( #fn_attrs) *
7580 #fn_vis #fn_sig {
76- #[ cfg( feature = "trace_guest" ) ]
7781 const _: ( ) = assert!(
78- #entry_msg . len( ) <= hyperlight_guest_tracing:: MAX_TRACE_MSG_LEN ,
82+ #_entry_msg . len( ) <= hyperlight_guest_tracing:: MAX_TRACE_MSG_LEN ,
7983 "Trace message exceeds the maximum bytes length" ,
8084 ) ;
81- #[ cfg( feature = "trace_guest" ) ]
82- :: hyperlight_guest_tracing:: create_trace_record( #entry_msg) ;
85+ :: hyperlight_guest_tracing:: create_trace_record( #_entry_msg) ;
8386 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) ;
8688 __trace_result
8789 }
8890 }
91+ #[ cfg( not( feature = "trace" ) ) ]
92+ quote ! {
93+ #( #fn_attrs) *
94+ #fn_vis #fn_sig {
95+ #fn_block
96+ }
97+ }
8998 }
9099 } ;
91100
@@ -202,36 +211,41 @@ pub fn trace(input: TokenStream) -> TokenStream {
202211 _ => unreachable ! ( ) ,
203212 } ;
204213 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" ) ]
207217 let expanded = quote ! {
208218 {
209- #[ cfg( feature = "trace_guest" ) ]
210219 const _: ( ) = assert!(
211- #entry_msg . len( ) <= hyperlight_guest_tracing:: MAX_TRACE_MSG_LEN ,
220+ #_entry_msg . len( ) <= hyperlight_guest_tracing:: MAX_TRACE_MSG_LEN ,
212221 "Trace message exceeds the maximum bytes length" ,
213222 ) ;
214- #[ cfg( feature = "trace_guest" ) ]
215- :: hyperlight_guest_tracing:: create_trace_record( #entry_msg) ;
223+ :: hyperlight_guest_tracing:: create_trace_record( #_entry_msg) ;
216224 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) ;
219226 __trace_result
220227 }
221228 } ;
229+ #[ cfg( not( feature = "trace" ) ) ]
230+ let expanded = quote ! {
231+ #statement
232+ } ;
233+
222234 TokenStream :: from ( expanded)
223235 } else {
236+ #[ cfg( feature = "trace" ) ]
224237 let expanded = quote ! {
225238 {
226- #[ cfg( feature = "trace_guest" ) ]
227239 const _: ( ) = assert!(
228240 #trace_message. len( ) <= hyperlight_guest_tracing:: MAX_TRACE_MSG_LEN ,
229241 "Trace message exceeds the maximum bytes length" ,
230242 ) ;
231- #[ cfg( feature = "trace_guest" ) ]
232243 :: hyperlight_guest_tracing:: create_trace_record( #trace_message) ;
233244 }
234245 } ;
246+ #[ cfg( not( feature = "trace" ) ) ]
247+ let expanded = quote ! { } ;
248+
235249 TokenStream :: from ( expanded)
236250 }
237251}
@@ -244,12 +258,14 @@ pub fn trace(input: TokenStream) -> TokenStream {
244258/// ```
245259#[ proc_macro]
246260pub fn flush ( _input : TokenStream ) -> TokenStream {
261+ #[ cfg( feature = "trace" ) ]
247262 let expanded = quote ! {
248263 {
249- #[ cfg( feature = "trace_guest" ) ]
250264 :: hyperlight_guest_tracing:: flush_trace_buffer( ) ;
251265 }
252266 } ;
267+ #[ cfg( not( feature = "trace" ) ) ]
268+ let expanded = quote ! { } ;
253269
254270 TokenStream :: from ( expanded)
255271}
0 commit comments