@@ -69,6 +69,7 @@ impl FuncTranslator {
6969 addr2line : & addr2line:: Context < DwarfReader < ' _ > > ,
7070 session : & Session ,
7171 func_validator : & mut FuncValidator < impl WasmModuleResources > ,
72+ config : & crate :: WasmTranslationConfig ,
7273 ) -> WasmResult < ( ) > {
7374 let context = func. borrow ( ) . as_operation ( ) . context_rc ( ) ;
7475 let mut op_builder = midenc_hir:: OpBuilder :: new ( context)
@@ -110,6 +111,7 @@ impl FuncTranslator {
110111 addr2line,
111112 session,
112113 func_validator,
114+ config,
113115 ) ?;
114116
115117 builder. finalize ( ) ;
@@ -198,6 +200,7 @@ fn parse_function_body<B: ?Sized + Builder>(
198200 addr2line : & addr2line:: Context < DwarfReader < ' _ > > ,
199201 session : & Session ,
200202 func_validator : & mut FuncValidator < impl WasmModuleResources > ,
203+ config : & crate :: WasmTranslationConfig ,
201204) -> WasmResult < ( ) > {
202205 // The control stack is initialized with a single block representing the whole function.
203206 debug_assert_eq ! ( state. control_stack. len( ) , 1 , "State not initialized" ) ;
@@ -216,15 +219,59 @@ fn parse_function_body<B: ?Sized + Builder>(
216219 if let Some ( loc) = addr2line. find_location ( offset) . into_diagnostic ( ) ? {
217220 if let Some ( file) = loc. file {
218221 let path = std:: path:: Path :: new ( file) ;
219- let path = path. canonicalize ( ) . unwrap_or_else ( |_| path. to_path_buf ( ) ) ;
220- if path. exists ( ) {
221- let source_file = session. source_manager . load_file ( & path) . into_diagnostic ( ) ?;
222+
223+ // Resolve relative paths to absolute paths
224+ let resolved_path = if path. is_relative ( ) {
225+ // Strategy 1: Try trim_path_prefixes
226+ if let Some ( resolved) = config. trim_path_prefixes . iter ( ) . find_map ( |prefix| {
227+ let candidate = prefix. join ( path) ;
228+ if candidate. exists ( ) {
229+ // Canonicalize to get absolute path
230+ candidate. canonicalize ( ) . ok ( )
231+ } else {
232+ None
233+ }
234+ } ) {
235+ Some ( resolved)
236+ }
237+ // Strategy 2: Try session.options.current_dir as fallback
238+ else {
239+ let current_dir_candidate = session. options . current_dir . join ( path) ;
240+ if current_dir_candidate. exists ( ) {
241+ current_dir_candidate. canonicalize ( ) . ok ( )
242+ } else {
243+ None
244+ }
245+ }
246+ } else {
247+ // Path is absolute, but verify it exists and canonicalize it
248+ if path. exists ( ) {
249+ path. canonicalize ( ) . ok ( )
250+ } else {
251+ None
252+ }
253+ } ;
254+
255+ if let Some ( absolute_path) = resolved_path {
256+ debug_assert ! (
257+ absolute_path. is_absolute( ) ,
258+ "resolved path should be absolute: {}" ,
259+ absolute_path. display( )
260+ ) ;
261+ log:: debug!( target: "module-parser" ,
262+ "resolved source path '{}' -> '{}'" ,
263+ file,
264+ absolute_path. display( )
265+ ) ;
266+ let source_file =
267+ session. source_manager . load_file ( & absolute_path) . into_diagnostic ( ) ?;
222268 let line = loc. line . and_then ( LineNumber :: new) . unwrap_or_default ( ) ;
223269 let column = loc. column . and_then ( ColumnNumber :: new) . unwrap_or_default ( ) ;
224270 span = source_file. line_column_to_span ( line, column) . unwrap_or_default ( ) ;
225271 } else {
226272 log:: debug!( target: "module-parser" ,
227- "failed to locate span for instruction at offset {offset} in function {func_name}"
273+ "failed to resolve source path '{file}' for instruction at offset \
274+ {offset} in function {func_name}"
228275 ) ;
229276 }
230277 }
0 commit comments