1
Fork 0

syntax: print expansion info from #[attribute] macros in the correct

format.

Previously, any attempt to use this information from inside something
like #[deriving(Foo)] would result in it printing like `deriving(Foo)!`.
This commit is contained in:
Huon Wilson 2013-12-07 13:41:11 +11:00
parent 09a879460c
commit 3ef933647a
4 changed files with 29 additions and 4 deletions

View file

@ -18,7 +18,7 @@ use std::vec;
use syntax::ast_util::*;
use syntax::attr::AttrMetaMethods;
use syntax::attr;
use syntax::codemap::{dummy_sp, Span, ExpnInfo, NameAndSpan};
use syntax::codemap::{dummy_sp, Span, ExpnInfo, NameAndSpan, MacroAttribute};
use syntax::codemap;
use syntax::ext::base::ExtCtxt;
use syntax::fold::ast_fold;
@ -158,6 +158,7 @@ fn generate_test_harness(sess: session::Session, crate: ast::Crate)
call_site: dummy_sp(),
callee: NameAndSpan {
name: @"test",
format: MacroAttribute,
span: None
}
});

View file

@ -161,8 +161,22 @@ pub struct LocWithOpt {
// used to be structural records. Better names, anyone?
pub struct FileMapAndLine {fm: @FileMap, line: uint}
pub struct FileMapAndBytePos {fm: @FileMap, pos: BytePos}
#[deriving(IterBytes)]
pub struct NameAndSpan {name: @str, span: Option<Span>}
pub enum MacroFormat {
// e.g. #[deriving(...)] <item>
MacroAttribute,
// e.g. `format!()`
MacroBang
}
#[deriving(IterBytes)]
pub struct NameAndSpan {
name: @str,
// the format with which the macro was invoked.
format: MacroFormat,
span: Option<Span>
}
/// Extra information for tracking macro expansion of spans
#[deriving(IterBytes)]

View file

@ -342,8 +342,13 @@ fn highlight_lines(cm: @codemap::CodeMap,
fn print_macro_backtrace(cm: @codemap::CodeMap, sp: Span) {
for ei in sp.expn_info.iter() {
let ss = ei.callee.span.as_ref().map_default(~"", |span| cm.span_to_str(*span));
let (pre, post) = match ei.callee.format {
codemap::MacroAttribute => ("#[", "]"),
codemap::MacroBang => ("", "!")
};
print_diagnostic(ss, note,
format!("in expansion of {}!", ei.callee.name));
format!("in expansion of {}{}{}", pre, ei.callee.name, post));
let ss = cm.span_to_str(ei.call_site);
print_diagnostic(ss, note, "expansion site");
print_macro_backtrace(cm, ei.call_site);

View file

@ -18,7 +18,7 @@ use ext::build::AstBuilder;
use attr;
use attr::AttrMetaMethods;
use codemap;
use codemap::{Span, Spanned, ExpnInfo, NameAndSpan};
use codemap::{Span, Spanned, ExpnInfo, NameAndSpan, MacroBang, MacroAttribute};
use ext::base::*;
use fold::*;
use opt_vec;
@ -69,6 +69,7 @@ pub fn expand_expr(extsbox: @mut SyntaxEnv,
call_site: e.span,
callee: NameAndSpan {
name: extnamestr,
format: MacroBang,
span: exp_span,
},
});
@ -257,6 +258,7 @@ pub fn expand_mod_items(extsbox: @mut SyntaxEnv,
call_site: attr.span,
callee: NameAndSpan {
name: mname,
format: MacroAttribute,
span: None
}
});
@ -352,6 +354,7 @@ pub fn expand_item_mac(extsbox: @mut SyntaxEnv,
call_site: it.span,
callee: NameAndSpan {
name: extnamestr,
format: MacroBang,
span: span
}
});
@ -370,6 +373,7 @@ pub fn expand_item_mac(extsbox: @mut SyntaxEnv,
call_site: it.span,
callee: NameAndSpan {
name: extnamestr,
format: MacroBang,
span: span
}
});
@ -459,6 +463,7 @@ pub fn expand_stmt(extsbox: @mut SyntaxEnv,
call_site: s.span,
callee: NameAndSpan {
name: extnamestr,
format: MacroBang,
span: exp_span,
}
});