Can we get the source code location of the caller in a procedural macro attribute?
问题 I have requirement to get the source location of the caller of every method. I am trying to create a proc_macro_attribute to capture the location and print it. #[proc_macro_attribute] pub fn get_location(attr: TokenStream, item: TokenStream) -> TokenStream { // Get and print file!(), line!() of source // Should print line no. 11 item } #[get_location] fn add(x: u32, y: u32) -> u32 { x + y } fn main() { add(1, 5); // Line No. 11 } 回答1: Ready to use solutions are available (see @timotree 's