clif_util/
print_cfg.rs

1//! The `print-cfg` sub-command.
2//!
3//! Read a series of Cranelift IR files and print their control flow graphs
4//! in graphviz format.
5
6use crate::utils::read_to_string;
7use anyhow::Result;
8use clap::Parser;
9use cranelift_codegen::cfg_printer::CFGPrinter;
10use cranelift_reader::parse_functions;
11use std::path::{Path, PathBuf};
12
13/// Prints out cfg in GraphViz Dot format
14#[derive(Parser)]
15pub struct Options {
16    /// Specify an input file to be used. Use '-' for stdin.
17    #[arg(required = true)]
18    files: Vec<PathBuf>,
19}
20
21pub fn run(options: &Options) -> Result<()> {
22    for (i, f) in options.files.iter().enumerate() {
23        if i != 0 {
24            println!();
25        }
26        print_cfg(f)?
27    }
28    Ok(())
29}
30
31fn print_cfg(path: &Path) -> Result<()> {
32    let buffer = read_to_string(path)?;
33    let items = parse_functions(&buffer)?;
34
35    for (idx, func) in items.into_iter().enumerate() {
36        if idx != 0 {
37            println!();
38        }
39        print!("{}", CFGPrinter::new(&func));
40    }
41
42    Ok(())
43}