From e8231672d048cc81b3029b3ef0058a221a639edc Mon Sep 17 00:00:00 2001 From: Nick Fitzgerald Date: Sun, 7 Jun 2015 10:47:51 -0700 Subject: [PATCH] WIP more tracing plugin --- .gitignore | 1 + src/lib.rs | 33 +++++---------------------------- src/trace_plugin.rs | 29 +++++++++++++++++------------ tests/simple_plugin_test.rs | 7 +++++-- 4 files changed, 28 insertions(+), 42 deletions(-) diff --git a/.gitignore b/.gitignore index a9d37c5..0723636 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ target Cargo.lock +*.racertmp \ No newline at end of file diff --git a/src/lib.rs b/src/lib.rs index 1211149..e39a1e6 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -151,6 +151,7 @@ #![feature(core)] #![feature(custom_derive)] #![feature(filling_drop)] +#![feature(optin_builtin_traits)] #![feature(plugin)] #![feature(plugin_registrar)] #![feature(quote)] @@ -796,17 +797,17 @@ impl CcBoxPtr for Weak { } } -pub type Tracer = FnMut(&CcTrace); +pub type Tracer = FnMut(&Trace); -pub trait CcTrace: fmt::Debug { - fn trace(&self, tracer: &mut Tracer); +pub trait Trace: fmt::Debug { + fn trace(&self, _tracer: &mut Tracer); } #[cfg(test)] mod tests { #![plugin(bacon_rajan_cc)] - use super::{Cc, CcTrace, Weak, weak_count, strong_count}; + use super::{Cc, Weak, weak_count, strong_count}; use std::boxed::Box; use std::cell::RefCell; use std::option::Option; @@ -815,30 +816,6 @@ mod tests { use std::mem::drop; use std::clone::Clone; - // trace_macros!(true); - - // #[derive(CcTrace, Debug)] - // struct CycleCollected { - // a: Cc, - // b: Cc, - // } - - // trace_macros!(false); - - // #[test] - // fn test_plugin() { - // let x = CycleCollected { - // a: Cc::new(5), - // b: Cc::new("hello".into()), - // }; - - // CcTrace::trace(&x, &mut |v| { - // println!("traced {:?}", v); - // }); - - // assert!(false); - // } - // Tests copied from `Rc`. #[test] diff --git a/src/trace_plugin.rs b/src/trace_plugin.rs index 46302cd..cd87a48 100644 --- a/src/trace_plugin.rs +++ b/src/trace_plugin.rs @@ -21,8 +21,8 @@ use rustc::plugin::Registry; -use syntax::ast::{Expr, MetaItem, Mutability}; -use syntax::ext::base::{Annotatable, ExtCtxt, MultiDecorator}; +use syntax::ast::{Expr, Item, MetaItem, Mutability}; +use syntax::ext::base::{Annotatable, Decorator, ExtCtxt}; use syntax::ext::build::AstBuilder; use syntax::ext::deriving::generic::{combine_substructure, EnumMatching, FieldInfo, MethodDef, Struct, Substructure, TraitDef, ty}; @@ -31,14 +31,16 @@ use syntax::parse::token::intern; use syntax::ptr::P; pub fn expand_derive_cc_trace(cx: &mut ExtCtxt, - span: Span, - mitem: &MetaItem, - item: Annotatable, - push: &mut FnMut(Annotatable)) + sp: Span, + meta_item: &MetaItem, + item: &Item, + push: &mut FnMut(P)) { println!("FITZGEN HELLO"); + let ann = Annotatable::Item(P(item.clone())); + let cc_trace_trait_def = TraitDef { - span: span, + span: sp, attributes: Vec::new(), path: ty::Path::new(vec!("bacon_rajan_cc", "Trace")), additional_bounds: Vec::new(), @@ -61,20 +63,23 @@ pub fn expand_derive_cc_trace(cx: &mut ExtCtxt, associated_types: Vec::new(), }; - cc_trace_trait_def.expand(cx, mitem, &item, push); + let mut push2 = |a: Annotatable| { + push(a.expect_item()); + }; + cc_trace_trait_def.expand(cx, meta_item, &ann, &mut push2); } fn cc_trace_substructure(cx: &mut ExtCtxt, trait_span: Span, substr: &Substructure) -> P { let state_expr = match (substr.nonself_args.len(), substr.nonself_args.get(0)) { (1, Some(o_f)) => o_f, - _ => cx.span_bug(trait_span, "incorrect number of arguments in `derive(CcTrace)`") + _ => cx.span_bug(trait_span, "incorrect number of arguments in `derive_cc_trace`") }; let call_cc_trace = |span, thing_expr| { let cc_trace_path = { let strs = vec!( cx.ident_of("bacon_rajan_cc"), - cx.ident_of("CcTrace"), + cx.ident_of("Trace"), cx.ident_of("trace"), ); @@ -88,7 +93,7 @@ fn cc_trace_substructure(cx: &mut ExtCtxt, trait_span: Span, substr: &Substructu let fields = match *substr.fields { Struct(ref fs) | EnumMatching(_, _, ref fs) => fs, - _ => cx.span_bug(trait_span, "impossible substructure in `jstraceable`") + _ => cx.span_bug(trait_span, "impossible substructure in `derice_cc_trace`") }; for &FieldInfo { ref self_, span, .. } in fields.iter() { @@ -100,5 +105,5 @@ fn cc_trace_substructure(cx: &mut ExtCtxt, trait_span: Span, substr: &Substructu #[plugin_registrar] pub fn plugin_registrar(reg: &mut Registry) { - reg.register_syntax_extension(intern("CcTrace"), MultiDecorator(Box::new(expand_derive_cc_trace))); + reg.register_syntax_extension(intern("derive_cc_trace"), Decorator(Box::new(expand_derive_cc_trace))); } diff --git a/tests/simple_plugin_test.rs b/tests/simple_plugin_test.rs index 6f36c20..db4d578 100644 --- a/tests/simple_plugin_test.rs +++ b/tests/simple_plugin_test.rs @@ -6,10 +6,12 @@ use bacon_rajan_cc::*; trace_macros!(true); -#[derive(CcTrace, Debug)] +#[derive(Debug)] +#[derive_cc_trace] struct CycleCollectable { a: Cc, b: Cc, + c: u32, } trace_macros!(false); @@ -19,9 +21,10 @@ fn test_plugin() { let x = CycleCollectable { a: Cc::new(5), b: Cc::new("hello".into()), + c: 42 }; - CcTrace::trace(&x, &mut |v| { + Trace::trace(&x, &mut |v| { println!("traced {:?}", v); });