@@ -3,26 +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 } ;
8-
9- use super :: cache;
6+ use std:: hash:: Hasher ;
107
118/// Source code and potential source code before expansion.
129///
13- /// Refer to
10+ /// The field order matters, since this struct implements Ord.
1411#[ derive( Clone , Debug , Serialize , PartialEq , Eq , PartialOrd , Ord , Default ) ]
1512pub struct SourceCode {
16- // TODO:
17- // file: String,
18- //
13+ // A file path where src lies.
14+ // The path is stripped with pwd or sysroot prefix.
15+ pub file : String ,
16+
1917 /// Source that a stable_mir span points to.
2018 pub src : String ,
19+
2120 /// Is the stable_mir span from a macro expansion?
2221 /// If it is from an expansion, what's the source code before expansion?
2322 /// * Some(_) happens when the src (stable_mir) span comes from expansion, and tells
2423 /// the source before the expansion.
2524 /// * 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
2629 pub before_expansion : Option < String > ,
2730}
2831
@@ -54,46 +57,22 @@ pub fn source_code_with(
5457 stable_mir_span : stable_mir:: ty:: Span ,
5558 tcx : TyCtxt ,
5659 src_map : & SourceMap ,
60+ path_prefixes : [ & str ; 2 ] ,
5761) -> SourceCode {
5862 let span = internal ( tcx, stable_mir_span) ;
5963 let src = span_to_source ( span, src_map) ;
6064 let before_expansion = span. from_expansion ( ) . then ( || {
6165 let ancestor_span = span. find_oldest_ancestor_in_same_ctxt ( ) ;
6266 span_to_source ( ancestor_span, src_map)
6367 } ) ;
64- SourceCode { src, before_expansion }
65- }
66-
67- pub fn cmp_callees ( a : & Instance , b : & Instance ) -> Ordering {
68- let filename_a = file_path ( a) ;
69- let filename_b = file_path ( b) ;
70- match filename_a. cmp ( & filename_b) {
71- Ordering :: Equal => ( ) ,
72- ord => return ord,
73- }
74-
75- let body_a = cache:: get_source_code ( a) ;
76- let body_b = cache:: get_source_code ( b) ;
77- body_a. cmp ( & body_b)
78- }
79-
80- pub fn file_path ( inst : & Instance ) -> String {
81- use std:: sync:: LazyLock ;
82- static PREFIXES : LazyLock < [ String ; 2 ] > = LazyLock :: new ( || {
83- let mut pwd = std:: env:: current_dir ( ) . unwrap ( ) . into_os_string ( ) . into_string ( ) . unwrap ( ) ;
84- pwd. push ( '/' ) ;
85-
86- let out = std:: process:: Command :: new ( "rustc" ) . arg ( "--print=sysroot" ) . output ( ) . unwrap ( ) ;
87- let sysroot = std:: str:: from_utf8 ( & out. stdout ) . unwrap ( ) . trim ( ) ;
88- let sysroot = format ! ( "{sysroot}/lib/rustlib/src/rust/" ) ;
89- [ pwd, sysroot]
90- } ) ;
9168
92- let file = inst. def . span ( ) . get_filename ( ) ;
93- for prefix in & * PREFIXES {
94- if let Some ( file) = file. strip_prefix ( prefix) {
95- 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 ;
9674 }
9775 }
98- file
76+
77+ SourceCode { file, src, before_expansion }
9978}
0 commit comments