Skip to main content

rust_analyzer/
cli.rs

1//! Various batch processing tasks, intended primarily for debugging.
2
3#![allow(clippy::print_stdout, clippy::print_stderr)]
4
5mod analysis_stats;
6mod diagnostics;
7pub mod flags;
8mod highlight;
9mod lsif;
10mod parse;
11mod prime_caches;
12mod run_tests;
13mod rustc_tests;
14mod scip;
15mod ssr;
16mod symbols;
17mod unresolved_references;
18
19mod progress_report;
20
21use std::io::Read;
22
23use anyhow::Result;
24use hir::{Module, Name};
25use hir_ty::db::HirDatabase;
26use ide::{AnalysisHost, Edition};
27use itertools::Itertools;
28use vfs::Vfs;
29
30#[derive(Clone, Copy)]
31pub enum Verbosity {
32    Spammy,
33    Verbose,
34    Normal,
35    Quiet,
36}
37
38impl Verbosity {
39    pub fn is_verbose(self) -> bool {
40        matches!(self, Verbosity::Verbose | Verbosity::Spammy)
41    }
42    pub fn is_spammy(self) -> bool {
43        matches!(self, Verbosity::Spammy)
44    }
45    pub fn is_quiet(self) -> bool {
46        matches!(self, Verbosity::Quiet)
47    }
48}
49
50fn read_stdin() -> anyhow::Result<String> {
51    let mut buff = String::new();
52    std::io::stdin().read_to_string(&mut buff)?;
53    Ok(buff)
54}
55
56fn report_metric(metric: &str, value: u64, unit: &str) {
57    if std::env::var("RA_METRICS").is_err() {
58        return;
59    }
60    println!("METRIC:{metric}:{value}:{unit}")
61}
62
63fn print_memory_usage(mut host: AnalysisHost, vfs: Vfs) {
64    let mem = host.per_query_memory_usage();
65
66    let before = profile::memory_usage();
67    drop(vfs);
68    let vfs = before.allocated - profile::memory_usage().allocated;
69
70    let before = profile::memory_usage();
71    drop(host);
72    let unaccounted = before.allocated - profile::memory_usage().allocated;
73    let remaining = profile::memory_usage().allocated;
74
75    for (name, bytes, entries) in mem {
76        // NOTE: Not a debug print, so avoid going through the `eprintln` defined above.
77        eprintln!("{bytes:>8} {entries:>6} {name}");
78    }
79    eprintln!("{vfs:>8}        VFS");
80
81    eprintln!("{unaccounted:>8}        Unaccounted");
82
83    eprintln!("{remaining:>8}        Remaining");
84}
85
86fn full_name_of_item(db: &dyn HirDatabase, module: Module, name: Name) -> String {
87    module
88        .path_segments(db)
89        .chain(Some(name))
90        .map(|it| it.display(db, Edition::LATEST).to_string())
91        .join("::")
92}