11use rustc_middle:: ty:: TyCtxt ;
22use rustc_smir:: rustc_internal:: internal;
33use rustc_span:: { Span , source_map:: SourceMap } ;
4+ use rustc_stable_hash:: { StableHasher , hashers:: SipHasher128 } ;
5+ use serde:: Serialize ;
46use stable_mir:: { CrateDef , mir:: mono:: Instance } ;
5- use std:: cmp:: Ordering ;
7+ use std:: { cmp:: Ordering , hash :: Hasher } ;
68
7- /// Source code for a span.
8- fn source_code ( span : Span , src_map : & SourceMap ) -> String {
9- // println!("{span:?}\n{:?}\n\n", span.find_oldest_ancestor_in_same_ctxt());
10- // _ = src_map.span_to_source(span, |text, x, y| {
11- // println!("(stable_mir span to internal span) [{span:?}]\n{}", &text[x..y]);
12- // Ok(())
13- // });
14- // let ancestor_span = span.find_oldest_ancestor_in_same_ctxt();
15- // dbg!(span.from_expansion(), ancestor_span.from_expansion());
16- // _ = src_map.span_to_source(ancestor_span, |text, x, y| {
17- // println!("(find_oldest_ancestor_in_same_ctxt) [{ancestor_span:?}]\n{}\n\n", &text[x..y]);
18- // Ok(())
19- // });
9+ /// Source code and potential source code before expansion.
10+ ///
11+ /// Refer to
12+ #[ derive( Debug , Serialize , PartialEq , Eq , PartialOrd , Ord , Default ) ]
13+ pub struct SourceCode {
14+ // TODO:
15+ // file: String,
16+ //
17+ /// Source that a stable_mir span points to.
18+ pub src : String ,
19+ /// Is the stable_mir span from a macro expansion?
20+ /// If it is from an expansion, what's the source code before expansion?
21+ /// * Some(_) happens when the src (stable_mir) span comes from expansion, and tells
22+ /// the source before the expansion.
23+ /// * None if the src is not from a macro expansion.
24+ pub before_expansion : Option < String > ,
25+ }
26+
27+ impl SourceCode {
28+ pub fn with_hasher ( & self , hasher : & mut StableHasher < SipHasher128 > ) {
29+ hasher. write_str ( & self . src ) ;
30+ match & self . before_expansion {
31+ Some ( text) => {
32+ hasher. write_u8 ( 1 ) ;
33+ hasher. write_str ( text) ;
34+ }
35+ None => hasher. write_u8 ( 0 ) ,
36+ }
37+ }
38+ }
2039
40+ fn span_to_source ( span : Span , src_map : & SourceMap ) -> String {
2141 src_map
2242 . span_to_source ( span, |text, x, y| {
2343 let src = & text[ x..y] ;
@@ -27,17 +47,32 @@ fn source_code(span: Span, src_map: &SourceMap) -> String {
2747 . unwrap ( )
2848}
2949
50+ /// Source code for a span.
51+ fn source_code ( span : Span , src_map : & SourceMap ) -> SourceCode {
52+ let src = span_to_source ( span, src_map) ;
53+ let before_expansion = span. from_expansion ( ) . then ( || {
54+ let ancestor_span = span. find_oldest_ancestor_in_same_ctxt ( ) ;
55+ span_to_source ( ancestor_span, src_map)
56+ } ) ;
57+ SourceCode { src, before_expansion }
58+ }
59+
3060/// Source code for a stable_mir span.
3161pub fn source_code_with (
3262 stable_mir_span : stable_mir:: ty:: Span ,
3363 tcx : TyCtxt ,
3464 src_map : & SourceMap ,
35- ) -> String {
65+ ) -> SourceCode {
3666 let span = internal ( tcx, stable_mir_span) ;
3767 source_code ( span, src_map)
3868}
3969
40- pub fn source_code_of_body ( inst : & Instance , tcx : TyCtxt , src_map : & SourceMap ) -> Option < String > {
70+ // FIXME: takes body and returns SourceCode
71+ pub fn source_code_of_body (
72+ inst : & Instance ,
73+ tcx : TyCtxt ,
74+ src_map : & SourceMap ,
75+ ) -> Option < SourceCode > {
4176 inst. body ( ) . map ( |body| source_code_with ( body. span , tcx, src_map) )
4277}
4378
0 commit comments