@@ -3,24 +3,29 @@ use rustc_smir::rustc_internal::internal;
33use rustc_span:: { Span , source_map:: SourceMap } ;
44use rustc_stable_hash:: { StableHasher , hashers:: SipHasher128 } ;
55use serde:: Serialize ;
6- use stable_mir:: { CrateDef , mir:: mono:: Instance } ;
7- use std:: { cmp:: Ordering , hash:: Hasher } ;
6+ use std:: hash:: Hasher ;
87
98/// Source code and potential source code before expansion.
109///
11- /// Refer to
12- #[ derive( Debug , Serialize , PartialEq , Eq , PartialOrd , Ord , Default ) ]
10+ /// The field order matters, since this struct implements Ord.
11+ #[ derive( Clone , Debug , Serialize , PartialEq , Eq , PartialOrd , Ord , Default ) ]
1312pub struct SourceCode {
14- // TODO:
15- // file: String,
16- //
13+ // A file path where src lies.
14+ // The path is stripped with pwd or sysroot prefix.
15+ pub file : String ,
16+
1717 /// Source that a stable_mir span points to.
1818 pub src : String ,
19+
1920 /// Is the stable_mir span from a macro expansion?
2021 /// If it is from an expansion, what's the source code before expansion?
2122 /// * Some(_) happens when the src (stable_mir) span comes from expansion, and tells
2223 /// the source before the expansion.
2324 /// * None if the src is not from a macro expansion.
25+ ///
26+ /// Refer to [#31] to know sepecific cases.
27+ ///
28+ /// [#31]: https://github.com/os-checker/distributed-verification/issues/31
2429 pub before_expansion : Option < String > ,
2530}
2631
@@ -47,65 +52,27 @@ fn span_to_source(span: Span, src_map: &SourceMap) -> String {
4752 . unwrap ( )
4853}
4954
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-
6055/// Source code for a stable_mir span.
6156pub fn source_code_with (
6257 stable_mir_span : stable_mir:: ty:: Span ,
6358 tcx : TyCtxt ,
6459 src_map : & SourceMap ,
60+ path_prefixes : [ & str ; 2 ] ,
6561) -> SourceCode {
6662 let span = internal ( tcx, stable_mir_span) ;
67- source_code ( span, src_map)
68- }
69-
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 > {
76- inst. body ( ) . map ( |body| source_code_with ( body. span , tcx, src_map) )
77- }
78-
79- pub fn cmp_callees ( a : & Instance , b : & Instance , tcx : TyCtxt , src_map : & SourceMap ) -> Ordering {
80- let filename_a = file_path ( a) ;
81- let filename_b = file_path ( b) ;
82- match filename_a. cmp ( & filename_b) {
83- Ordering :: Equal => ( ) ,
84- ord => return ord,
85- }
86-
87- let body_a = source_code_of_body ( a, tcx, src_map) ;
88- let body_b = source_code_of_body ( b, tcx, src_map) ;
89- body_a. cmp ( & body_b)
90- }
91-
92- pub fn file_path ( inst : & Instance ) -> String {
93- use std:: sync:: LazyLock ;
94- static PREFIXES : LazyLock < [ String ; 2 ] > = LazyLock :: new ( || {
95- let mut pwd = std:: env:: current_dir ( ) . unwrap ( ) . into_os_string ( ) . into_string ( ) . unwrap ( ) ;
96- pwd. push ( '/' ) ;
97-
98- let out = std:: process:: Command :: new ( "rustc" ) . arg ( "--print=sysroot" ) . output ( ) . unwrap ( ) ;
99- let sysroot = std:: str:: from_utf8 ( & out. stdout ) . unwrap ( ) . trim ( ) ;
100- let sysroot = format ! ( "{sysroot}/lib/rustlib/src/rust/" ) ;
101- [ pwd, sysroot]
63+ let src = span_to_source ( span, src_map) ;
64+ let before_expansion = span. from_expansion ( ) . then ( || {
65+ let ancestor_span = span. find_oldest_ancestor_in_same_ctxt ( ) ;
66+ span_to_source ( ancestor_span, src_map)
10267 } ) ;
10368
104- let file = inst. def . span ( ) . get_filename ( ) ;
105- for prefix in & * PREFIXES {
106- if let Some ( file) = file. strip_prefix ( prefix) {
107- return file. to_owned ( ) ;
69+ let mut file = stable_mir_span. get_filename ( ) ;
70+ for prefix in path_prefixes {
71+ if let Some ( file_stripped) = file. strip_prefix ( prefix) {
72+ file = file_stripped. to_owned ( ) ;
73+ break ;
10874 }
10975 }
110- file
76+
77+ SourceCode { file, src, before_expansion }
11178}
0 commit comments