11//! D2 diagram format output for MIR graphs.
22
3+ use std:: collections:: HashSet ;
4+
35extern crate stable_mir;
46use stable_mir:: mir:: TerminatorKind ;
57
@@ -10,11 +12,22 @@ use crate::mk_graph::context::GraphContext;
1012use crate :: mk_graph:: util:: {
1113 escape_d2, is_unqualified, name_lines, short_name, terminator_targets,
1214} ;
15+ use crate :: mk_graph:: { compute_lang_start_exclusions, skip_lang_start} ;
1316
1417impl SmirJson < ' _ > {
1518 /// Convert the MIR to D2 diagram format
16- pub fn to_d2_file ( self ) -> String {
19+ pub fn to_d2_file ( mut self ) -> String {
1720 let ctx = GraphContext :: from_smir ( & self ) ;
21+
22+ // Optionally filter out lang_start items and their unique descendants
23+ let excluded: HashSet < String > = if skip_lang_start ( ) {
24+ let excluded = compute_lang_start_exclusions ( & self . items , & ctx) ;
25+ self . items . retain ( |i| !excluded. contains ( & i. symbol_name ) ) ;
26+ excluded
27+ } else {
28+ HashSet :: new ( )
29+ } ;
30+
1831 let mut output = String :: new ( ) ;
1932
2033 output. push_str ( "direction: right\n \n " ) ;
@@ -23,7 +36,7 @@ impl SmirJson<'_> {
2336 for item in self . items {
2437 match item. mono_item_kind {
2538 MonoItemKind :: MonoItemFn { name, body, .. } => {
26- render_d2_function ( & name, body. as_ref ( ) , & ctx, & mut output) ;
39+ render_d2_function ( & name, body. as_ref ( ) , & ctx, & excluded , & mut output) ;
2740 }
2841 MonoItemKind :: MonoItemGlobalAsm { asm } => {
2942 render_d2_asm ( & asm, & mut output) ;
@@ -61,6 +74,7 @@ fn render_d2_function(
6174 name : & str ,
6275 body : Option < & stable_mir:: mir:: Body > ,
6376 ctx : & GraphContext ,
77+ excluded : & HashSet < String > ,
6478 out : & mut String ,
6579) {
6680 let fn_id = short_name ( name) ;
@@ -80,7 +94,7 @@ fn render_d2_function(
8094
8195 // Call edges (must be outside the container)
8296 if let Some ( body) = body {
83- render_d2_call_edges ( & fn_id, body, ctx, out) ;
97+ render_d2_call_edges ( & fn_id, body, ctx, excluded , out) ;
8498 }
8599}
86100
@@ -115,6 +129,7 @@ fn render_d2_call_edges(
115129 fn_id : & str ,
116130 body : & stable_mir:: mir:: Body ,
117131 ctx : & GraphContext ,
132+ excluded : & HashSet < String > ,
118133 out : & mut String ,
119134) {
120135 for ( idx, block) in body. blocks . iter ( ) . enumerate ( ) {
@@ -124,7 +139,7 @@ fn render_d2_call_edges(
124139 let Some ( callee_name) = ctx. resolve_call_target ( func) else {
125140 continue ;
126141 } ;
127- if !is_unqualified ( & callee_name) {
142+ if !is_unqualified ( & callee_name) || excluded . contains ( & callee_name ) {
128143 continue ;
129144 }
130145
0 commit comments