@@ -5,7 +5,7 @@ use rustc_data_structures::memmap::Mmap;
55use rustc_data_structures:: temp_dir:: MaybeTempDir ;
66use rustc_errors:: { ErrorGuaranteed , Handler } ;
77use rustc_fs_util:: fix_windows_verbatim_for_gcc;
8- use rustc_hir:: def_id:: { CrateNum , LOCAL_CRATE } ;
8+ use rustc_hir:: def_id:: CrateNum ;
99use rustc_middle:: middle:: dependency_format:: Linkage ;
1010use rustc_middle:: middle:: exported_symbols:: SymbolExportKind ;
1111use rustc_session:: config:: { self , CFGuard , CrateType , DebugInfo , LdImpl , Strip } ;
@@ -18,6 +18,7 @@ use rustc_session::utils::NativeLibKind;
1818/// need out of the shared crate context before we get rid of it.
1919use rustc_session:: { filesearch, Session } ;
2020use rustc_span:: symbol:: Symbol ;
21+ use rustc_span:: DebuggerVisualizerFile ;
2122use rustc_target:: spec:: crt_objects:: { CrtObjects , CrtObjectsFallback } ;
2223use rustc_target:: spec:: { LinkOutputKind , LinkerFlavor , LldFlavor , SplitDebuginfo } ;
2324use rustc_target:: spec:: { PanicStrategy , RelocModel , RelroLevel , SanitizerSet , Target } ;
@@ -37,6 +38,7 @@ use regex::Regex;
3738use tempfile:: Builder as TempFileBuilder ;
3839
3940use std:: borrow:: Borrow ;
41+ use std:: collections:: BTreeSet ;
4042use std:: ffi:: OsString ;
4143use std:: fs:: { File , OpenOptions } ;
4244use std:: io:: { BufWriter , Write } ;
@@ -2099,14 +2101,16 @@ fn add_order_independent_options(
20992101 // Pass optimization flags down to the linker.
21002102 cmd. optimize ( ) ;
21012103
2102- let debugger_visualizer_paths = if sess. target . is_like_msvc {
2103- collect_debugger_visualizers ( tmpdir, sess, & codegen_results. crate_info )
2104- } else {
2105- Vec :: new ( )
2106- } ;
2104+ // Gather the set of NatVis files, if any, and write them out to a temp directory.
2105+ let natvis_visualizers = collect_natvis_visualizers (
2106+ tmpdir,
2107+ sess,
2108+ & codegen_results. crate_info . local_crate_name ,
2109+ & codegen_results. crate_info . natvis_debugger_visualizers ,
2110+ ) ;
21072111
2108- // Pass debuginfo and strip flags down to the linker.
2109- cmd. debuginfo ( strip_value ( sess) , & debugger_visualizer_paths ) ;
2112+ // Pass debuginfo, NatVis debugger visualizers and strip flags down to the linker.
2113+ cmd. debuginfo ( strip_value ( sess) , & natvis_visualizers ) ;
21102114
21112115 // We want to prevent the compiler from accidentally leaking in any system libraries,
21122116 // so by default we tell linkers not to link to any default libraries.
@@ -2125,43 +2129,33 @@ fn add_order_independent_options(
21252129 add_rpath_args ( cmd, sess, codegen_results, out_filename) ;
21262130}
21272131
2128- // Write the debugger visualizer files for each crate to the temp directory and gather the file paths.
2129- fn collect_debugger_visualizers (
2132+ // Write the NatVis debugger visualizer files for each crate to the temp directory and gather the file paths.
2133+ fn collect_natvis_visualizers (
21302134 tmpdir : & Path ,
21312135 sess : & Session ,
2132- crate_info : & CrateInfo ,
2136+ crate_name : & Symbol ,
2137+ natvis_debugger_visualizers : & BTreeSet < DebuggerVisualizerFile > ,
21332138) -> Vec < PathBuf > {
2134- let mut visualizer_paths = Vec :: new ( ) ;
2135- let debugger_visualizers = & crate_info. debugger_visualizers ;
2136- let mut index = 0 ;
2139+ let mut visualizer_paths = Vec :: with_capacity ( natvis_debugger_visualizers. len ( ) ) ;
21372140
2138- for ( & cnum, visualizers) in debugger_visualizers {
2139- let crate_name = if cnum == LOCAL_CRATE {
2140- crate_info. local_crate_name . as_str ( )
2141- } else {
2142- crate_info. crate_name [ & cnum] . as_str ( )
2143- } ;
2141+ for ( index, visualizer) in natvis_debugger_visualizers. iter ( ) . enumerate ( ) {
2142+ let visualizer_out_file = tmpdir. join ( format ! ( "{}-{}.natvis" , crate_name. as_str( ) , index) ) ;
21442143
2145- for visualizer in visualizers {
2146- let visualizer_out_file = tmpdir. join ( format ! ( "{}-{}.natvis" , crate_name, index) ) ;
2147-
2148- match fs:: write ( & visualizer_out_file, & visualizer. src ) {
2149- Ok ( ( ) ) => {
2150- visualizer_paths. push ( visualizer_out_file. clone ( ) ) ;
2151- index += 1 ;
2152- }
2153- Err ( error) => {
2154- sess. warn (
2155- format ! (
2156- "Unable to write debugger visualizer file `{}`: {} " ,
2157- visualizer_out_file. display( ) ,
2158- error
2159- )
2160- . as_str ( ) ,
2161- ) ;
2162- }
2163- } ;
2164- }
2144+ match fs:: write ( & visualizer_out_file, & visualizer. src ) {
2145+ Ok ( ( ) ) => {
2146+ visualizer_paths. push ( visualizer_out_file) ;
2147+ }
2148+ Err ( error) => {
2149+ sess. warn (
2150+ format ! (
2151+ "Unable to write debugger visualizer file `{}`: {} " ,
2152+ visualizer_out_file. display( ) ,
2153+ error
2154+ )
2155+ . as_str ( ) ,
2156+ ) ;
2157+ }
2158+ } ;
21652159 }
21662160 visualizer_paths
21672161}
0 commit comments