From f2a8a712669911eb36166c35199fe18ce19ed7e9 Mon Sep 17 00:00:00 2001 From: Mikko Perttunen Date: Mon, 11 Feb 2013 18:13:18 +0200 Subject: [PATCH 1/3] Use topmost span for macro expansion location. Fixes behaviour of file!, line! and col! --- src/libsyntax/ext/source_util.rs | 35 +++++++++++++++++++++++++------- 1 file changed, 28 insertions(+), 7 deletions(-) diff --git a/src/libsyntax/ext/source_util.rs b/src/libsyntax/ext/source_util.rs index 07dacc7c1b3..aa05e9a5119 100644 --- a/src/libsyntax/ext/source_util.rs +++ b/src/libsyntax/ext/source_util.rs @@ -9,7 +9,7 @@ // except according to those terms. use codemap; -use codemap::{FileMap, Loc, Pos, span}; +use codemap::{FileMap, Loc, Pos, ExpandedFrom, span}; use ext::base::*; use ext::base; use ext::build::{mk_base_vec_e, mk_uint, mk_u8, mk_base_str}; @@ -21,20 +21,39 @@ use core::result; use core::str; use core::vec; +fn topmost_expn_info(expn_info: @codemap::ExpnInfo) -> @codemap::ExpnInfo { + let ExpandedFrom({call_site, _}) = *expn_info; + match call_site.expn_info { + Some(next_expn_info) => { + let ExpandedFrom({callie: {name, _}, _}) = *next_expn_info; + // Don't recurse into file using "include!" + if name == ~"include" { return expn_info; } + + topmost_expn_info(next_expn_info) + }, + None => expn_info + } +} + /* line!(): expands to the current line number */ pub fn expand_line(cx: ext_ctxt, sp: span, tts: ~[ast::token_tree]) -> base::MacResult { base::check_zero_tts(cx, sp, tts, "line!"); - let loc = cx.codemap().lookup_char_pos(sp.lo); - base::MRExpr(mk_uint(cx, sp, loc.line)) + + let topmost = topmost_expn_info(cx.backtrace().get()); + let loc = cx.codemap().lookup_char_pos(topmost.call_site.lo); + + base::MRExpr(mk_uint(cx, topmost.call_site, loc.line)) } /* col!(): expands to the current column number */ pub fn expand_col(cx: ext_ctxt, sp: span, tts: ~[ast::token_tree]) -> base::MacResult { base::check_zero_tts(cx, sp, tts, "col!"); - let loc = cx.codemap().lookup_char_pos(sp.lo); - base::MRExpr(mk_uint(cx, sp, loc.col.to_uint())) + + let topmost = topmost_expn_info(cx.backtrace().get()); + let loc = cx.codemap().lookup_char_pos(topmost.call_site.lo); + base::MRExpr(mk_uint(cx, topmost.call_site, loc.col.to_uint())) } /* file!(): expands to the current filename */ @@ -43,9 +62,11 @@ pub fn expand_col(cx: ext_ctxt, sp: span, tts: ~[ast::token_tree]) pub fn expand_file(cx: ext_ctxt, sp: span, tts: ~[ast::token_tree]) -> base::MacResult { base::check_zero_tts(cx, sp, tts, "file!"); + + let topmost = topmost_expn_info(cx.backtrace().get()); let Loc { file: @FileMap { name: filename, _ }, _ } = - cx.codemap().lookup_char_pos(sp.lo); - base::MRExpr(mk_base_str(cx, sp, filename)) + cx.codemap().lookup_char_pos(topmost.call_site.lo); + base::MRExpr(mk_base_str(cx, topmost.call_site, filename)) } pub fn expand_stringify(cx: ext_ctxt, sp: span, tts: ~[ast::token_tree]) From d48bc263b57c5b621f214fecf323362341682c02 Mon Sep 17 00:00:00 2001 From: Mikko Perttunen Date: Mon, 11 Feb 2013 20:26:40 +0200 Subject: [PATCH 2/3] Add test for using line! in a macro --- src/test/run-pass/syntax-extension-source-utils.rs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/test/run-pass/syntax-extension-source-utils.rs b/src/test/run-pass/syntax-extension-source-utils.rs index 342ee4e3510..11effcf330f 100644 --- a/src/test/run-pass/syntax-extension-source-utils.rs +++ b/src/test/run-pass/syntax-extension-source-utils.rs @@ -17,9 +17,12 @@ pub mod m1 { } } +macro_rules! indirect_line( () => ( line!() ) ) + pub fn main() { - assert(line!() == 21); + assert(line!() == 23); assert(col!() == 11); + assert(indirect_line!() == 25); assert(file!().to_owned().ends_with(~"syntax-extension-source-utils.rs")); assert(stringify!((2*3) + 5).to_owned() == ~"( 2 * 3 ) + 5"); assert(include!("syntax-extension-source-utils-files/includeme.fragment").to_owned() From ca030b4fc85b4273c025287fb38c4d83432f5bf2 Mon Sep 17 00:00:00 2001 From: Mikko Perttunen Date: Mon, 11 Feb 2013 21:02:36 +0200 Subject: [PATCH 3/3] Update copyright years --- src/libsyntax/ext/source_util.rs | 2 +- src/test/run-pass/syntax-extension-source-utils.rs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/libsyntax/ext/source_util.rs b/src/libsyntax/ext/source_util.rs index aa05e9a5119..a061c0dfafd 100644 --- a/src/libsyntax/ext/source_util.rs +++ b/src/libsyntax/ext/source_util.rs @@ -1,4 +1,4 @@ -// Copyright 2012 The Rust Project Developers. See the COPYRIGHT +// Copyright 2012-2013 The Rust Project Developers. See the COPYRIGHT // file at the top-level directory of this distribution and at // http://rust-lang.org/COPYRIGHT. // diff --git a/src/test/run-pass/syntax-extension-source-utils.rs b/src/test/run-pass/syntax-extension-source-utils.rs index 11effcf330f..65ef3cc4f69 100644 --- a/src/test/run-pass/syntax-extension-source-utils.rs +++ b/src/test/run-pass/syntax-extension-source-utils.rs @@ -1,4 +1,4 @@ -// Copyright 2012 The Rust Project Developers. See the COPYRIGHT +// Copyright 2012-2013 The Rust Project Developers. See the COPYRIGHT // file at the top-level directory of this distribution and at // http://rust-lang.org/COPYRIGHT. //