1 //! Fully type-check project and print various stats, like the number of type
6 time::{SystemTime, UNIX_EPOCH},
10 db::{AstDatabase, DefDatabase, HirDatabase},
11 AssocItem, Crate, Function, HasSource, HirDisplay, ModuleDef,
14 body::{BodySourceMap, SyntheticSyntax},
18 use hir_ty::{TyExt, TypeWalk};
19 use ide::{Analysis, AnalysisHost, LineCol, RootDatabase};
20 use ide_db::base_db::{
21 salsa::{self, debug::DebugQueryTable, ParallelDatabase},
22 SourceDatabase, SourceDatabaseExt,
24 use itertools::Itertools;
26 use profile::{Bytes, StopWatch};
27 use project_model::{CargoConfig, ProjectManifest, ProjectWorkspace};
28 use rayon::prelude::*;
29 use rustc_hash::FxHashSet;
31 use syntax::{AstNode, SyntaxNode};
32 use vfs::{AbsPathBuf, Vfs, VfsPath};
35 flags::{self, OutputFormat},
36 load_cargo::{load_workspace, LoadCargoConfig},
38 progress_report::ProgressReport,
39 report_metric, Result, Verbosity,
42 /// Need to wrap Snapshot to provide `Clone` impl for `map_with`
44 impl<DB: ParallelDatabase> Clone for Snap<salsa::Snapshot<DB>> {
45 fn clone(&self) -> Snap<salsa::Snapshot<DB>> {
46 Snap(self.0.snapshot())
50 impl flags::AnalysisStats {
51 pub fn run(self, verbosity: Verbosity) -> Result<()> {
53 let seed = SystemTime::now().duration_since(UNIX_EPOCH).unwrap().as_millis() as u64;
57 let mut cargo_config = CargoConfig::default();
58 cargo_config.no_sysroot = self.no_sysroot;
59 let load_cargo_config = LoadCargoConfig {
60 load_out_dirs_from_check: !self.disable_build_scripts,
61 with_proc_macro: !self.disable_proc_macros,
62 prefill_caches: false,
64 let no_progress = &|_| ();
66 let mut db_load_sw = self.stop_watch();
68 let path = AbsPathBuf::assert(env::current_dir()?.join(&self.path));
69 let manifest = ProjectManifest::discover_single(&path)?;
71 let mut workspace = ProjectWorkspace::load(manifest, &cargo_config, no_progress)?;
72 let metadata_time = db_load_sw.elapsed();
74 let build_scripts_time = if self.disable_build_scripts {
77 let mut build_scripts_sw = self.stop_watch();
78 let bs = workspace.run_build_scripts(&cargo_config, no_progress)?;
79 workspace.set_build_scripts(bs);
80 Some(build_scripts_sw.elapsed())
83 let (host, vfs, _proc_macro) =
84 load_workspace(workspace, &cargo_config, &load_cargo_config)?;
85 let db = host.raw_database();
86 eprint!("{:<20} {}", "Database loaded:", db_load_sw.elapsed());
87 eprint!(" (metadata {}", metadata_time);
88 if let Some(build_scripts_time) = build_scripts_time {
89 eprint!("; build {}", build_scripts_time);
93 let mut analysis_sw = self.stop_watch();
94 let mut num_crates = 0;
95 let mut visited_modules = FxHashSet::default();
96 let mut visit_queue = Vec::new();
98 let mut krates = Crate::all(db);
100 shuffle(&mut rng, &mut krates);
102 for krate in krates {
103 let module = krate.root_module(db);
104 let file_id = module.definition_source(db).file_id;
105 let file_id = file_id.original_file(db);
106 let source_root = db.file_source_root(file_id);
107 let source_root = db.source_root(source_root);
108 if !source_root.is_library || self.with_deps {
110 visit_queue.push(module);
115 shuffle(&mut rng, &mut visit_queue);
118 eprint!(" crates: {}", num_crates);
119 let mut num_decls = 0;
120 let mut funcs = Vec::new();
121 while let Some(module) = visit_queue.pop() {
122 if visited_modules.insert(module) {
123 visit_queue.extend(module.children(db));
125 for decl in module.declarations(db) {
127 if let ModuleDef::Function(f) = decl {
132 for impl_def in module.impl_defs(db) {
133 for item in impl_def.items(db) {
135 if let AssocItem::Function(f) = item {
142 eprintln!(", mods: {}, decls: {}, fns: {}", visited_modules.len(), num_decls, funcs.len());
143 eprintln!("{:<20} {}", "Item Collection:", analysis_sw.elapsed());
146 shuffle(&mut rng, &mut funcs);
149 if !self.skip_inference {
150 self.run_inference(&host, db, &vfs, &funcs, verbosity);
153 let total_span = analysis_sw.elapsed();
154 eprintln!("{:<20} {}", "Total:", total_span);
155 report_metric("total time", total_span.time.as_millis() as u64, "ms");
156 if let Some(instructions) = total_span.instructions {
157 report_metric("total instructions", instructions, "#instr");
159 if let Some(memory) = total_span.memory {
160 report_metric("total memory", memory.allocated.megabytes() as u64, "MB");
163 if env::var("RA_COUNT").is_ok() {
164 eprintln!("{}", profile::countme::get_all());
167 if self.source_stats {
168 let mut total_file_size = Bytes::default();
169 for e in ide_db::base_db::ParseQuery.in_db(db).entries::<Vec<_>>() {
170 total_file_size += syntax_len(db.parse(e.key).syntax_node())
173 let mut total_macro_file_size = Bytes::default();
174 for e in hir::db::ParseMacroExpansionQuery.in_db(db).entries::<Vec<_>>() {
175 if let Some((val, _)) = db.parse_macro_expansion(e.key).value {
176 total_macro_file_size += syntax_len(val.syntax_node())
179 eprintln!("source files: {}, macro files: {}", total_file_size, total_macro_file_size);
182 if self.memory_usage && verbosity.is_verbose() {
183 print_memory_usage(host, vfs);
195 verbosity: Verbosity,
197 let mut bar = match verbosity {
198 Verbosity::Quiet | Verbosity::Spammy => ProgressReport::hidden(),
199 _ if self.parallel || self.output.is_some() => ProgressReport::hidden(),
200 _ => ProgressReport::new(funcs.len() as u64),
204 let mut inference_sw = self.stop_watch();
205 let snap = Snap(db.snapshot());
208 .map_with(snap, |snap, &f| {
209 let f_id = FunctionId::from(f);
210 snap.0.body(f_id.into());
211 snap.0.infer(f_id.into());
214 eprintln!("{:<20} {}", "Parallel Inference:", inference_sw.elapsed());
217 let mut inference_sw = self.stop_watch();
219 let mut num_exprs = 0;
220 let mut num_exprs_unknown = 0;
221 let mut num_exprs_partially_unknown = 0;
222 let mut num_type_mismatches = 0;
223 let analysis = host.analysis();
224 for f in funcs.iter().copied() {
225 let name = f.name(db);
231 .filter_map(|it| it.name(db))
232 .chain(Some(f.name(db)))
234 if let Some(only_name) = self.only.as_deref() {
235 if name.to_string() != only_name && full_name != only_name {
239 let mut msg = format!("processing: {}", full_name);
240 if verbosity.is_verbose() {
241 if let Some(src) = f.source(db) {
242 let original_file = src.file_id.original_file(db);
243 let path = vfs.file_path(original_file);
244 let syntax_range = src.value.syntax().text_range();
245 format_to!(msg, " ({} {:?})", path, syntax_range);
248 if verbosity.is_spammy() {
249 bar.println(msg.to_string());
251 bar.set_message(&msg);
252 let f_id = FunctionId::from(f);
253 let (body, sm) = db.body_with_source_map(f_id.into());
254 let inference_result = db.infer(f_id.into());
255 let (previous_exprs, previous_unknown, previous_partially_unknown) =
256 (num_exprs, num_exprs_unknown, num_exprs_partially_unknown);
257 for (expr_id, _) in body.exprs.iter() {
258 let ty = &inference_result[expr_id];
260 let unknown_or_partial = if ty.is_unknown() {
261 num_exprs_unknown += 1;
262 if verbosity.is_spammy() {
263 if let Some((path, start, end)) =
264 expr_syntax_range(db, &analysis, vfs, &sm, expr_id)
267 "{} {}:{}-{}:{}: Unknown type",
275 bar.println(format!("{}: Unknown type", name,));
280 let mut is_partially_unknown = false;
283 is_partially_unknown = true;
286 if is_partially_unknown {
287 num_exprs_partially_unknown += 1;
291 if self.only.is_some() && verbosity.is_spammy() {
292 // in super-verbose mode for just one function, we print every single expression
293 if let Some((_, start, end)) =
294 expr_syntax_range(db, &analysis, vfs, &sm, expr_id)
305 bar.println(format!("unknown location: {}", ty.display(db)));
308 if unknown_or_partial && self.output == Some(OutputFormat::Csv) {
311 location_csv(db, &analysis, vfs, &sm, expr_id),
315 if let Some(mismatch) = inference_result.type_mismatch_for_expr(expr_id) {
316 num_type_mismatches += 1;
317 if verbosity.is_verbose() {
318 if let Some((path, start, end)) =
319 expr_syntax_range(db, &analysis, vfs, &sm, expr_id)
322 "{} {}:{}-{}:{}: Expected {}, got {}",
328 mismatch.expected.display(db),
329 mismatch.actual.display(db)
333 "{}: Expected {}, got {}",
335 mismatch.expected.display(db),
336 mismatch.actual.display(db)
340 if self.output == Some(OutputFormat::Csv) {
342 r#"{},mismatch,"{}","{}""#,
343 location_csv(db, &analysis, vfs, &sm, expr_id),
344 mismatch.expected.display(db),
345 mismatch.actual.display(db)
350 if verbosity.is_spammy() {
352 "In {}: {} exprs, {} unknown, {} partial",
354 num_exprs - previous_exprs,
355 num_exprs_unknown - previous_unknown,
356 num_exprs_partially_unknown - previous_partially_unknown
362 bar.finish_and_clear();
364 " exprs: {}, ??ty: {} ({}%), ?ty: {} ({}%), !ty: {}",
367 percentage(num_exprs_unknown, num_exprs),
368 num_exprs_partially_unknown,
369 percentage(num_exprs_partially_unknown, num_exprs),
372 report_metric("unknown type", num_exprs_unknown, "#");
373 report_metric("type mismatches", num_type_mismatches, "#");
375 eprintln!("{:<20} {}", "Inference:", inference_sw.elapsed());
378 fn stop_watch(&self) -> StopWatch {
379 StopWatch::start().memory(self.memory_usage)
390 let src = match sm.expr_syntax(expr_id) {
392 Err(SyntheticSyntax) => return "synthetic,,".to_string(),
394 let root = db.parse_or_expand(src.file_id).unwrap();
395 let node = src.map(|e| e.to_node(&root).syntax().clone());
396 let original_range = node.as_ref().original_file_range(db);
397 let path = vfs.file_path(original_range.file_id);
398 let line_index = analysis.file_line_index(original_range.file_id).unwrap();
399 let text_range = original_range.range;
401 (line_index.line_col(text_range.start()), line_index.line_col(text_range.end()));
402 format!("{},{}:{},{}:{}", path, start.line + 1, start.col, end.line + 1, end.col)
405 fn expr_syntax_range(
411 ) -> Option<(VfsPath, LineCol, LineCol)> {
412 let src = sm.expr_syntax(expr_id);
413 if let Ok(src) = src {
414 let root = db.parse_or_expand(src.file_id).unwrap();
415 let node = src.map(|e| e.to_node(&root).syntax().clone());
416 let original_range = node.as_ref().original_file_range(db);
417 let path = vfs.file_path(original_range.file_id);
418 let line_index = analysis.file_line_index(original_range.file_id).unwrap();
419 let text_range = original_range.range;
421 (line_index.line_col(text_range.start()), line_index.line_col(text_range.end()));
422 Some((path, start, end))
428 fn shuffle<T>(rng: &mut Rand32, slice: &mut [T]) {
429 for i in 0..slice.len() {
430 randomize_first(rng, &mut slice[i..]);
433 fn randomize_first<T>(rng: &mut Rand32, slice: &mut [T]) {
434 assert!(!slice.is_empty());
435 let idx = rng.rand_range(0..slice.len() as u32) as usize;
440 fn percentage(n: u64, total: u64) -> u64 {
441 (n * 100).checked_div(total).unwrap_or(100)
444 fn syntax_len(node: SyntaxNode) -> usize {
445 // Macro expanded code doesn't contain whitespace, so erase *all* whitespace
446 // to make macro and non-macro code comparable.
447 node.to_string().replace(|it: char| it.is_ascii_whitespace(), "").len()