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