Skip to content

Commit

Permalink
WIP more tracing plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
fitzgen committed Jun 7, 2015
1 parent 422248d commit e823167
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 42 deletions.
1 change: 1 addition & 0 deletions .gitignore
@@ -1,2 +1,3 @@
target
Cargo.lock
*.racertmp
33 changes: 5 additions & 28 deletions src/lib.rs
Expand Up @@ -151,6 +151,7 @@
#![feature(core)]
#![feature(custom_derive)]
#![feature(filling_drop)]
#![feature(optin_builtin_traits)]
#![feature(plugin)]
#![feature(plugin_registrar)]
#![feature(quote)]
Expand Down Expand Up @@ -796,17 +797,17 @@ impl<T> CcBoxPtr<T> for Weak<T> {
}
}

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;
Expand All @@ -815,30 +816,6 @@ mod tests {
use std::mem::drop;
use std::clone::Clone;

// trace_macros!(true);

// #[derive(CcTrace, Debug)]
// struct CycleCollected {
// a: Cc<u32>,
// b: Cc<String>,
// }

// 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<T>`.

#[test]
Expand Down
29 changes: 17 additions & 12 deletions src/trace_plugin.rs
Expand Up @@ -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};
Expand All @@ -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<Item>))
{
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(),
Expand All @@ -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<Expr> {
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"),
);

Expand All @@ -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() {
Expand All @@ -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)));
}
7 changes: 5 additions & 2 deletions tests/simple_plugin_test.rs
Expand Up @@ -6,10 +6,12 @@ use bacon_rajan_cc::*;

trace_macros!(true);

#[derive(CcTrace, Debug)]
#[derive(Debug)]
#[derive_cc_trace]
struct CycleCollectable {
a: Cc<u32>,
b: Cc<String>,
c: u32,
}

trace_macros!(false);
Expand All @@ -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);
});

Expand Down

0 comments on commit e823167

Please sign in to comment.