Skip to content

Commit 744e5ad

Browse files
committed
Merge remote-tracking branch 'cyndis/expansion-span'
2 parents faef933 + ca030b4 commit 744e5ad

File tree

2 files changed

+34
-10
lines changed

2 files changed

+34
-10
lines changed

src/libsyntax/ext/source_util.rs

Lines changed: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
1+
// Copyright 2012-2013 The Rust Project Developers. See the COPYRIGHT
22
// file at the top-level directory of this distribution and at
33
// http://rust-lang.org/COPYRIGHT.
44
//
@@ -9,7 +9,7 @@
99
// except according to those terms.
1010

1111
use codemap;
12-
use codemap::{FileMap, Loc, Pos, span};
12+
use codemap::{FileMap, Loc, Pos, ExpandedFrom, span};
1313
use ext::base::*;
1414
use ext::base;
1515
use ext::build::{mk_base_vec_e, mk_uint, mk_u8, mk_base_str};
@@ -21,20 +21,39 @@ use core::result;
2121
use core::str;
2222
use core::vec;
2323

24+
fn topmost_expn_info(expn_info: @codemap::ExpnInfo) -> @codemap::ExpnInfo {
25+
let ExpandedFrom({call_site, _}) = *expn_info;
26+
match call_site.expn_info {
27+
Some(next_expn_info) => {
28+
let ExpandedFrom({callie: {name, _}, _}) = *next_expn_info;
29+
// Don't recurse into file using "include!"
30+
if name == ~"include" { return expn_info; }
31+
32+
topmost_expn_info(next_expn_info)
33+
},
34+
None => expn_info
35+
}
36+
}
37+
2438
/* line!(): expands to the current line number */
2539
pub fn expand_line(cx: ext_ctxt, sp: span, tts: ~[ast::token_tree])
2640
-> base::MacResult {
2741
base::check_zero_tts(cx, sp, tts, "line!");
28-
let loc = cx.codemap().lookup_char_pos(sp.lo);
29-
base::MRExpr(mk_uint(cx, sp, loc.line))
42+
43+
let topmost = topmost_expn_info(cx.backtrace().get());
44+
let loc = cx.codemap().lookup_char_pos(topmost.call_site.lo);
45+
46+
base::MRExpr(mk_uint(cx, topmost.call_site, loc.line))
3047
}
3148

3249
/* col!(): expands to the current column number */
3350
pub fn expand_col(cx: ext_ctxt, sp: span, tts: ~[ast::token_tree])
3451
-> base::MacResult {
3552
base::check_zero_tts(cx, sp, tts, "col!");
36-
let loc = cx.codemap().lookup_char_pos(sp.lo);
37-
base::MRExpr(mk_uint(cx, sp, loc.col.to_uint()))
53+
54+
let topmost = topmost_expn_info(cx.backtrace().get());
55+
let loc = cx.codemap().lookup_char_pos(topmost.call_site.lo);
56+
base::MRExpr(mk_uint(cx, topmost.call_site, loc.col.to_uint()))
3857
}
3958

4059
/* file!(): expands to the current filename */
@@ -43,9 +62,11 @@ pub fn expand_col(cx: ext_ctxt, sp: span, tts: ~[ast::token_tree])
4362
pub fn expand_file(cx: ext_ctxt, sp: span, tts: ~[ast::token_tree])
4463
-> base::MacResult {
4564
base::check_zero_tts(cx, sp, tts, "file!");
65+
66+
let topmost = topmost_expn_info(cx.backtrace().get());
4667
let Loc { file: @FileMap { name: filename, _ }, _ } =
47-
cx.codemap().lookup_char_pos(sp.lo);
48-
base::MRExpr(mk_base_str(cx, sp, filename))
68+
cx.codemap().lookup_char_pos(topmost.call_site.lo);
69+
base::MRExpr(mk_base_str(cx, topmost.call_site, filename))
4970
}
5071

5172
pub fn expand_stringify(cx: ext_ctxt, sp: span, tts: ~[ast::token_tree])

src/test/run-pass/syntax-extension-source-utils.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
1+
// Copyright 2012-2013 The Rust Project Developers. See the COPYRIGHT
22
// file at the top-level directory of this distribution and at
33
// http://rust-lang.org/COPYRIGHT.
44
//
@@ -17,9 +17,12 @@ pub mod m1 {
1717
}
1818
}
1919

20+
macro_rules! indirect_line( () => ( line!() ) )
21+
2022
pub fn main() {
21-
assert(line!() == 21);
23+
assert(line!() == 23);
2224
assert(col!() == 11);
25+
assert(indirect_line!() == 25);
2326
assert(file!().to_owned().ends_with(~"syntax-extension-source-utils.rs"));
2427
assert(stringify!((2*3) + 5).to_owned() == ~"( 2 * 3 ) + 5");
2528
assert(include!("syntax-extension-source-utils-files/includeme.fragment").to_owned()

0 commit comments

Comments
 (0)