Record expansion info

This commit is contained in:
David Tolnay 2016-07-03 10:51:55 -07:00
parent 5deba439c3
commit 149c87d7c2
No known key found for this signature in database
GPG Key ID: F9BA143B95FF6D82
4 changed files with 48 additions and 2 deletions

View File

@ -8,6 +8,7 @@ use syntax::ptr::P;
use syntax::tokenstream::TokenTree;
use bound;
use span;
use internals::ast::{Body, Field, Item, Style, Variant};
use internals::{attr, Error};
@ -44,7 +45,7 @@ pub fn expand_derive_deserialize(
let builder = aster::AstBuilder::new().span(span);
let impl_item = deserialize_item(cx, &builder, &item);
push(Annotatable::Item(impl_item))
push(span::record_expansion(cx, impl_item, "Deserialize"))
}
fn deserialize_item(

View File

@ -1,3 +1,4 @@
mod bound;
mod de;
mod ser;
mod span;

View File

@ -6,6 +6,7 @@ use syntax::ext::base::{Annotatable, ExtCtxt};
use syntax::ptr::P;
use bound;
use span;
use internals::ast::{Body, Field, Item, Style, Variant};
use internals::{attr, Error};
@ -38,7 +39,7 @@ pub fn expand_derive_serialize(
let builder = aster::AstBuilder::new().span(span);
let impl_item = serialize_item(cx, &builder, &item);
push(Annotatable::Item(impl_item))
push(span::record_expansion(cx, impl_item, "Serialize"))
}
fn serialize_item(

43
serde_codegen/src/span.rs Normal file
View File

@ -0,0 +1,43 @@
use syntax::ast;
use syntax::codemap::{self, ExpnId, Span};
use syntax::ext::base::{Annotatable, ExtCtxt};
use syntax::fold::{self, Folder};
use syntax::parse::token::intern;
use syntax::ptr::P;
pub fn record_expansion(
cx: &ExtCtxt,
item: P<ast::Item>,
derive: &str,
) -> Annotatable {
let info = codemap::ExpnInfo {
call_site: codemap::DUMMY_SP,
callee: codemap::NameAndSpan {
format: codemap::MacroAttribute(intern(&format!("derive({})", derive))),
span: None,
allow_internal_unstable: false,
},
};
let expn_id = cx.codemap().record_expansion(info);
let mut respanner = Respanner { expn_id: expn_id };
let item = item.map(|item| respanner.fold_item_simple(item));
Annotatable::Item(item)
}
struct Respanner {
expn_id: ExpnId,
}
impl Folder for Respanner {
fn new_span(&mut self, span: Span) -> Span {
Span {
expn_id: self.expn_id,
.. span
}
}
fn fold_mac(&mut self, mac: ast::Mac) -> ast::Mac {
fold::noop_fold_mac(mac, self)
}
}