1
- // Copyright 2012 The Rust Project Developers. See the COPYRIGHT
1
+ // Copyright 2012-2013 The Rust Project Developers. See the COPYRIGHT
2
2
// file at the top-level directory of this distribution and at
3
3
// http://rust-lang.org/COPYRIGHT.
4
4
//
9
9
// except according to those terms.
10
10
11
11
use codemap;
12
- use codemap:: { FileMap , Loc , Pos , span} ;
12
+ use codemap:: { FileMap , Loc , Pos , ExpandedFrom , span} ;
13
13
use ext:: base:: * ;
14
14
use ext:: base;
15
15
use ext:: build:: { mk_base_vec_e, mk_uint, mk_u8, mk_base_str} ;
@@ -21,20 +21,39 @@ use core::result;
21
21
use core:: str;
22
22
use core:: vec;
23
23
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
+
24
38
/* line!(): expands to the current line number */
25
39
pub fn expand_line ( cx : ext_ctxt , sp : span , tts : ~[ ast:: token_tree ] )
26
40
-> base:: MacResult {
27
41
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 ) )
30
47
}
31
48
32
49
/* col!(): expands to the current column number */
33
50
pub fn expand_col ( cx : ext_ctxt , sp : span , tts : ~[ ast:: token_tree ] )
34
51
-> base:: MacResult {
35
52
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 ( ) ) )
38
57
}
39
58
40
59
/* file!(): expands to the current filename */
@@ -43,9 +62,11 @@ pub fn expand_col(cx: ext_ctxt, sp: span, tts: ~[ast::token_tree])
43
62
pub fn expand_file ( cx : ext_ctxt , sp : span , tts : ~[ ast:: token_tree ] )
44
63
-> base:: MacResult {
45
64
base:: check_zero_tts ( cx, sp, tts, "file!" ) ;
65
+
66
+ let topmost = topmost_expn_info ( cx. backtrace ( ) . get ( ) ) ;
46
67
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) )
49
70
}
50
71
51
72
pub fn expand_stringify ( cx : ext_ctxt , sp : span , tts : ~[ ast:: token_tree ] )
0 commit comments