@@ -11,6 +11,8 @@ use stable_mir::ty::{ConstantKind, IndexedVal, MirConst, Ty};
1111
1212use crate :: printer:: SmirJson ;
1313
14+ use crate :: compat:: rustc_demangle:: demangle;
15+
1416use super :: index:: { AllocIndex , LayoutInfo , TypeEntry , TypeIndex , TypeKind } ;
1517use super :: util:: { function_string, short_fn_name, GraphLabelString } ;
1618
@@ -23,25 +25,50 @@ pub struct GraphContext {
2325 pub allocs : AllocIndex ,
2426 pub types : TypeIndex ,
2527 pub functions : HashMap < Ty , String > ,
28+ /// When `GRAPH_UNMANGLE=1`, maps mangled symbol names to demangled
29+ /// display names. Empty otherwise. Renderers should call
30+ /// `display_name()` for labels rather than using `functions` values
31+ /// directly.
32+ display_names : HashMap < String , String > ,
2633}
2734
2835impl GraphContext {
2936 pub fn from_smir ( smir : & SmirJson ) -> Self {
3037 let types = TypeIndex :: from_types ( & smir. types ) ;
3138 let allocs = AllocIndex :: from_alloc_infos ( & smir. allocs , & types) ;
39+ let unmangle = std:: env:: var ( "GRAPH_UNMANGLE" ) . is_ok ( ) ;
3240 let functions: HashMap < Ty , String > = smir
3341 . functions
3442 . iter ( )
3543 . map ( |( k, v) | ( k. 0 , function_string ( v. clone ( ) ) ) )
3644 . collect ( ) ;
45+ let display_names: HashMap < String , String > = if unmangle {
46+ functions
47+ . values ( )
48+ . map ( |name| ( name. clone ( ) , demangle ( name) . to_string ( ) ) )
49+ . collect ( )
50+ } else {
51+ HashMap :: new ( )
52+ } ;
3753
3854 Self {
3955 allocs,
4056 types,
4157 functions,
58+ display_names,
4259 }
4360 }
4461
62+ /// Return the display-friendly name for a symbol. When
63+ /// `GRAPH_UNMANGLE=1` is set this is the demangled form;
64+ /// otherwise it's the original (mangled) name.
65+ pub fn display_name < ' a > ( & ' a self , name : & ' a str ) -> & ' a str {
66+ self . display_names
67+ . get ( name)
68+ . map ( |s| s. as_str ( ) )
69+ . unwrap_or ( name)
70+ }
71+
4572 /// Render a constant operand with alloc information
4673 pub fn render_const ( & self , const_ : & MirConst ) -> String {
4774 let ty = const_. ty ( ) ;
@@ -78,7 +105,7 @@ impl GraphContext {
78105 // Function pointers, unit type, etc.
79106 if ty. kind ( ) . is_fn ( ) {
80107 if let Some ( name) = self . functions . get ( & ty) {
81- format ! ( "const fn {}" , short_fn_name( name) )
108+ format ! ( "const fn {}" , short_fn_name( self . display_name ( name) ) )
82109 } else {
83110 format ! ( "const {}" , ty_name)
84111 }
@@ -240,7 +267,7 @@ impl GraphContext {
240267 } => {
241268 let fn_name = self
242269 . resolve_call_target ( func)
243- . map ( |n| short_fn_name ( & n ) )
270+ . map ( |n| short_fn_name ( self . display_name ( & n ) ) )
244271 . unwrap_or_else ( || "?" . to_string ( ) ) ;
245272 let arg_str = args
246273 . iter ( )
0 commit comments