2019-05-14 06:58:41 +00:00
|
|
|
use std::{collections::HashSet, time::Instant, fmt::Write};
|
2019-02-09 17:27:11 +00:00
|
|
|
|
|
|
|
use ra_db::SourceDatabase;
|
|
|
|
use ra_batch::BatchDatabase;
|
2019-06-11 15:11:17 +00:00
|
|
|
use ra_hir::{Crate, ModuleDef, Ty, ImplItem, HasSource};
|
2019-02-09 17:27:11 +00:00
|
|
|
use ra_syntax::AstNode;
|
|
|
|
|
|
|
|
use crate::Result;
|
|
|
|
|
2019-05-12 17:54:44 +00:00
|
|
|
pub fn run(verbose: bool, path: &str, only: Option<&str>) -> Result<()> {
|
2019-03-30 10:25:53 +00:00
|
|
|
let db_load_time = Instant::now();
|
2019-05-12 17:54:44 +00:00
|
|
|
let (db, roots) = BatchDatabase::load_cargo(path)?;
|
2019-03-30 10:25:53 +00:00
|
|
|
println!("Database loaded, {} roots, {:?}", roots.len(), db_load_time.elapsed());
|
|
|
|
let analysis_time = Instant::now();
|
2019-02-09 17:27:11 +00:00
|
|
|
let mut num_crates = 0;
|
|
|
|
let mut visited_modules = HashSet::new();
|
|
|
|
let mut visit_queue = Vec::new();
|
|
|
|
for root in roots {
|
|
|
|
for krate in Crate::source_root_crates(&db, root) {
|
|
|
|
num_crates += 1;
|
|
|
|
let module = krate.root_module(&db).expect("crate in source root without root module");
|
|
|
|
visit_queue.push(module);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
println!("Crates in this dir: {}", num_crates);
|
|
|
|
let mut num_decls = 0;
|
|
|
|
let mut funcs = Vec::new();
|
|
|
|
while let Some(module) = visit_queue.pop() {
|
|
|
|
if visited_modules.insert(module) {
|
|
|
|
visit_queue.extend(module.children(&db));
|
|
|
|
|
|
|
|
for decl in module.declarations(&db) {
|
|
|
|
num_decls += 1;
|
2019-06-03 14:01:10 +00:00
|
|
|
if let ModuleDef::Function(f) = decl {
|
|
|
|
funcs.push(f);
|
2019-02-09 17:27:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for impl_block in module.impl_blocks(&db) {
|
2019-02-16 20:09:58 +00:00
|
|
|
for item in impl_block.items(&db) {
|
2019-02-09 17:27:11 +00:00
|
|
|
num_decls += 1;
|
2019-06-03 14:01:10 +00:00
|
|
|
if let ImplItem::Method(f) = item {
|
|
|
|
funcs.push(f);
|
2019-02-09 17:27:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
println!("Total modules found: {}", visited_modules.len());
|
|
|
|
println!("Total declarations: {}", num_decls);
|
|
|
|
println!("Total functions: {}", funcs.len());
|
2019-05-20 18:20:52 +00:00
|
|
|
let bar = indicatif::ProgressBar::with_draw_target(
|
|
|
|
funcs.len() as u64,
|
|
|
|
indicatif::ProgressDrawTarget::stderr_nohz(),
|
|
|
|
);
|
2019-05-14 06:58:41 +00:00
|
|
|
bar.set_style(
|
|
|
|
indicatif::ProgressStyle::default_bar().template("{wide_bar} {pos}/{len}\n{msg}"),
|
|
|
|
);
|
2019-02-09 17:27:11 +00:00
|
|
|
bar.tick();
|
|
|
|
let mut num_exprs = 0;
|
|
|
|
let mut num_exprs_unknown = 0;
|
|
|
|
let mut num_exprs_partially_unknown = 0;
|
|
|
|
for f in funcs {
|
2019-05-07 10:09:10 +00:00
|
|
|
let name = f.name(&db);
|
2019-05-14 06:58:41 +00:00
|
|
|
let mut msg = format!("processing: {}", name);
|
2019-02-09 17:27:11 +00:00
|
|
|
if verbose {
|
2019-06-11 15:11:17 +00:00
|
|
|
let src = f.source(&db);
|
|
|
|
let original_file = src.file_id.original_file(&db);
|
2019-02-09 17:27:11 +00:00
|
|
|
let path = db.file_relative_path(original_file);
|
2019-06-11 15:11:17 +00:00
|
|
|
let syntax_range = src.ast.syntax().range();
|
2019-05-14 06:58:41 +00:00
|
|
|
write!(msg, " ({:?} {})", path, syntax_range).unwrap();
|
2019-02-09 17:27:11 +00:00
|
|
|
}
|
2019-05-14 06:58:41 +00:00
|
|
|
bar.set_message(&msg);
|
2019-05-07 10:09:10 +00:00
|
|
|
if let Some(only_name) = only {
|
|
|
|
if name.to_string() != only_name {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
2019-02-09 17:27:11 +00:00
|
|
|
let body = f.body(&db);
|
|
|
|
let inference_result = f.infer(&db);
|
|
|
|
for (expr_id, _) in body.exprs() {
|
|
|
|
let ty = &inference_result[expr_id];
|
|
|
|
num_exprs += 1;
|
|
|
|
if let Ty::Unknown = ty {
|
|
|
|
num_exprs_unknown += 1;
|
|
|
|
} else {
|
|
|
|
let mut is_partially_unknown = false;
|
|
|
|
ty.walk(&mut |ty| {
|
|
|
|
if let Ty::Unknown = ty {
|
|
|
|
is_partially_unknown = true;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
if is_partially_unknown {
|
|
|
|
num_exprs_partially_unknown += 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
bar.inc(1);
|
|
|
|
}
|
|
|
|
bar.finish_and_clear();
|
|
|
|
println!("Total expressions: {}", num_exprs);
|
|
|
|
println!(
|
|
|
|
"Expressions of unknown type: {} ({}%)",
|
|
|
|
num_exprs_unknown,
|
|
|
|
(num_exprs_unknown * 100 / num_exprs)
|
|
|
|
);
|
|
|
|
println!(
|
|
|
|
"Expressions of partially unknown type: {} ({}%)",
|
|
|
|
num_exprs_partially_unknown,
|
|
|
|
(num_exprs_partially_unknown * 100 / num_exprs)
|
|
|
|
);
|
2019-03-30 10:25:53 +00:00
|
|
|
println!("Analysis: {:?}", analysis_time.elapsed());
|
2019-02-09 17:27:11 +00:00
|
|
|
Ok(())
|
|
|
|
}
|