Skip to content

Commit

Permalink
Try with macros.
Browse files Browse the repository at this point in the history
  • Loading branch information
emeryberger committed Oct 1, 2023
1 parent 83146d5 commit a3d04d0
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 2 deletions.
4 changes: 4 additions & 0 deletions rust-support/chatdbg/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,8 @@ repository = "https://github.com/plasma-umass/ChatDBG/"

[dependencies]
lazy_static = "1.4.0"
quote = "1.0.33"
syn = { version = "2.0.37", features = ["full"] }

[lib]
proc-macro = true
26 changes: 24 additions & 2 deletions rust-support/chatdbg/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use std::io;
use std::fs::{File, OpenOptions};
use std::io::Write;
use std::panic;
Expand All @@ -11,7 +10,7 @@ lazy_static::lazy_static! {
static ref FILE_CREATED: AtomicBool = AtomicBool::new(false);
}

pub fn chatdbg() {
fn chatdbg() {
// Set a custom panic hook.
panic::set_hook(Box::new(|info| {
let _guard = FILE_MUTEX.lock().unwrap(); // Lock Mutex to synchronize access.
Expand Down Expand Up @@ -50,3 +49,26 @@ pub fn chatdbg() {
writeln!(file, "{}", message).expect("Unable to write to file");
}));
}

use proc_macro::TokenStream;
use quote::quote;
use syn::{parse_macro_input, ItemFn};

#[proc_macro_attribute]
pub fn startup(_attr: TokenStream, item: TokenStream) -> TokenStream {
let input = parse_macro_input!(item as ItemFn);

let expanded = quote! {
#input // Repeat the original function

fn main() {
chatdbg(); // Call your startup function
// Call the original main function
::std::rt::resume_unwind(::std::panic::catch_unwind(|| {
#input
}));
}
};

TokenStream::from(expanded)
}

0 comments on commit a3d04d0

Please sign in to comment.