]> git.lizzy.rs Git - rust.git/blob - src/tools/rust-analyzer/crates/rust-analyzer/src/cli/analysis_stats.rs
Rollup merge of #102044 - ChrisDenton:BCrypt-system-rand, r=thomcc
[rust.git] / src / tools / rust-analyzer / crates / rust-analyzer / src / cli / analysis_stats.rs
1 //! Fully type-check project and print various stats, like the number of type
2 //! errors.
3
4 use std::{
5     env,
6     time::{SystemTime, UNIX_EPOCH},
7 };
8
9 use hir::{
10     db::{AstDatabase, DefDatabase, HirDatabase},
11     AssocItem, Crate, Function, HasSource, HirDisplay, ModuleDef,
12 };
13 use hir_def::{
14     body::{BodySourceMap, SyntheticSyntax},
15     expr::ExprId,
16     FunctionId,
17 };
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,
23 };
24 use itertools::Itertools;
25 use oorandom::Rand32;
26 use profile::{Bytes, StopWatch};
27 use project_model::{CargoConfig, ProjectManifest, ProjectWorkspace};
28 use rayon::prelude::*;
29 use rustc_hash::FxHashSet;
30 use stdx::format_to;
31 use syntax::{AstNode, SyntaxNode};
32 use vfs::{AbsPathBuf, Vfs, VfsPath};
33
34 use crate::cli::{
35     flags::{self, OutputFormat},
36     load_cargo::{load_workspace, LoadCargoConfig},
37     print_memory_usage,
38     progress_report::ProgressReport,
39     report_metric, Result, Verbosity,
40 };
41
42 /// Need to wrap Snapshot to provide `Clone` impl for `map_with`
43 struct Snap<DB>(DB);
44 impl<DB: ParallelDatabase> Clone for Snap<salsa::Snapshot<DB>> {
45     fn clone(&self) -> Snap<salsa::Snapshot<DB>> {
46         Snap(self.0.snapshot())
47     }
48 }
49
50 impl flags::AnalysisStats {
51     pub fn run(self, verbosity: Verbosity) -> Result<()> {
52         let mut rng = {
53             let seed = SystemTime::now().duration_since(UNIX_EPOCH).unwrap().as_millis() as u64;
54             Rand32::new(seed)
55         };
56
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,
63         };
64         let no_progress = &|_| ();
65
66         let mut db_load_sw = self.stop_watch();
67
68         let path = AbsPathBuf::assert(env::current_dir()?.join(&self.path));
69         let manifest = ProjectManifest::discover_single(&path)?;
70
71         let mut workspace = ProjectWorkspace::load(manifest, &cargo_config, no_progress)?;
72         let metadata_time = db_load_sw.elapsed();
73
74         let build_scripts_time = if self.disable_build_scripts {
75             None
76         } else {
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())
81         };
82
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);
90         }
91         eprintln!(")");
92
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();
97
98         let mut krates = Crate::all(db);
99         if self.randomize {
100             shuffle(&mut rng, &mut krates);
101         }
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 {
109                 num_crates += 1;
110                 visit_queue.push(module);
111             }
112         }
113
114         if self.randomize {
115             shuffle(&mut rng, &mut visit_queue);
116         }
117
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));
124
125                 for decl in module.declarations(db) {
126                     num_decls += 1;
127                     if let ModuleDef::Function(f) = decl {
128                         funcs.push(f);
129                     }
130                 }
131
132                 for impl_def in module.impl_defs(db) {
133                     for item in impl_def.items(db) {
134                         num_decls += 1;
135                         if let AssocItem::Function(f) = item {
136                             funcs.push(f);
137                         }
138                     }
139                 }
140             }
141         }
142         eprintln!(", mods: {}, decls: {}, fns: {}", visited_modules.len(), num_decls, funcs.len());
143         eprintln!("{:<20} {}", "Item Collection:", analysis_sw.elapsed());
144
145         if self.randomize {
146             shuffle(&mut rng, &mut funcs);
147         }
148
149         if !self.skip_inference {
150             self.run_inference(&host, db, &vfs, &funcs, verbosity);
151         }
152
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");
158         }
159         if let Some(memory) = total_span.memory {
160             report_metric("total memory", memory.allocated.megabytes() as u64, "MB");
161         }
162
163         if env::var("RA_COUNT").is_ok() {
164             eprintln!("{}", profile::countme::get_all());
165         }
166
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())
171             }
172
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())
177                 }
178             }
179             eprintln!("source files: {}, macro files: {}", total_file_size, total_macro_file_size);
180         }
181
182         if self.memory_usage && verbosity.is_verbose() {
183             print_memory_usage(host, vfs);
184         }
185
186         Ok(())
187     }
188
189     fn run_inference(
190         &self,
191         host: &AnalysisHost,
192         db: &RootDatabase,
193         vfs: &Vfs,
194         funcs: &[Function],
195         verbosity: Verbosity,
196     ) {
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),
201         };
202
203         if self.parallel {
204             let mut inference_sw = self.stop_watch();
205             let snap = Snap(db.snapshot());
206             funcs
207                 .par_iter()
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());
212                 })
213                 .count();
214             eprintln!("{:<20} {}", "Parallel Inference:", inference_sw.elapsed());
215         }
216
217         let mut inference_sw = self.stop_watch();
218         bar.tick();
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);
226             let full_name = f
227                 .module(db)
228                 .path_to_root(db)
229                 .into_iter()
230                 .rev()
231                 .filter_map(|it| it.name(db))
232                 .chain(Some(f.name(db)))
233                 .join("::");
234             if let Some(only_name) = self.only.as_deref() {
235                 if name.to_string() != only_name && full_name != only_name {
236                     continue;
237                 }
238             }
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);
246                 }
247             }
248             if verbosity.is_spammy() {
249                 bar.println(msg.to_string());
250             }
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];
259                 num_exprs += 1;
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)
265                         {
266                             bar.println(format!(
267                                 "{} {}:{}-{}:{}: Unknown type",
268                                 path,
269                                 start.line + 1,
270                                 start.col,
271                                 end.line + 1,
272                                 end.col,
273                             ));
274                         } else {
275                             bar.println(format!("{}: Unknown type", name,));
276                         }
277                     }
278                     true
279                 } else {
280                     let mut is_partially_unknown = false;
281                     ty.walk(&mut |ty| {
282                         if ty.is_unknown() {
283                             is_partially_unknown = true;
284                         }
285                     });
286                     if is_partially_unknown {
287                         num_exprs_partially_unknown += 1;
288                     }
289                     is_partially_unknown
290                 };
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)
295                     {
296                         bar.println(format!(
297                             "{}:{}-{}:{}: {}",
298                             start.line + 1,
299                             start.col,
300                             end.line + 1,
301                             end.col,
302                             ty.display(db)
303                         ));
304                     } else {
305                         bar.println(format!("unknown location: {}", ty.display(db)));
306                     }
307                 }
308                 if unknown_or_partial && self.output == Some(OutputFormat::Csv) {
309                     println!(
310                         r#"{},type,"{}""#,
311                         location_csv(db, &analysis, vfs, &sm, expr_id),
312                         ty.display(db)
313                     );
314                 }
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)
320                         {
321                             bar.println(format!(
322                                 "{} {}:{}-{}:{}: Expected {}, got {}",
323                                 path,
324                                 start.line + 1,
325                                 start.col,
326                                 end.line + 1,
327                                 end.col,
328                                 mismatch.expected.display(db),
329                                 mismatch.actual.display(db)
330                             ));
331                         } else {
332                             bar.println(format!(
333                                 "{}: Expected {}, got {}",
334                                 name,
335                                 mismatch.expected.display(db),
336                                 mismatch.actual.display(db)
337                             ));
338                         }
339                     }
340                     if self.output == Some(OutputFormat::Csv) {
341                         println!(
342                             r#"{},mismatch,"{}","{}""#,
343                             location_csv(db, &analysis, vfs, &sm, expr_id),
344                             mismatch.expected.display(db),
345                             mismatch.actual.display(db)
346                         );
347                     }
348                 }
349             }
350             if verbosity.is_spammy() {
351                 bar.println(format!(
352                     "In {}: {} exprs, {} unknown, {} partial",
353                     full_name,
354                     num_exprs - previous_exprs,
355                     num_exprs_unknown - previous_unknown,
356                     num_exprs_partially_unknown - previous_partially_unknown
357                 ));
358             }
359             bar.inc(1);
360         }
361
362         bar.finish_and_clear();
363         eprintln!(
364             "  exprs: {}, ??ty: {} ({}%), ?ty: {} ({}%), !ty: {}",
365             num_exprs,
366             num_exprs_unknown,
367             percentage(num_exprs_unknown, num_exprs),
368             num_exprs_partially_unknown,
369             percentage(num_exprs_partially_unknown, num_exprs),
370             num_type_mismatches
371         );
372         report_metric("unknown type", num_exprs_unknown, "#");
373         report_metric("type mismatches", num_type_mismatches, "#");
374
375         eprintln!("{:<20} {}", "Inference:", inference_sw.elapsed());
376     }
377
378     fn stop_watch(&self) -> StopWatch {
379         StopWatch::start().memory(self.memory_usage)
380     }
381 }
382
383 fn location_csv(
384     db: &RootDatabase,
385     analysis: &Analysis,
386     vfs: &Vfs,
387     sm: &BodySourceMap,
388     expr_id: ExprId,
389 ) -> String {
390     let src = match sm.expr_syntax(expr_id) {
391         Ok(s) => s,
392         Err(SyntheticSyntax) => return "synthetic,,".to_string(),
393     };
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;
400     let (start, end) =
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)
403 }
404
405 fn expr_syntax_range(
406     db: &RootDatabase,
407     analysis: &Analysis,
408     vfs: &Vfs,
409     sm: &BodySourceMap,
410     expr_id: ExprId,
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;
420         let (start, end) =
421             (line_index.line_col(text_range.start()), line_index.line_col(text_range.end()));
422         Some((path, start, end))
423     } else {
424         None
425     }
426 }
427
428 fn shuffle<T>(rng: &mut Rand32, slice: &mut [T]) {
429     for i in 0..slice.len() {
430         randomize_first(rng, &mut slice[i..]);
431     }
432
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;
436         slice.swap(0, idx);
437     }
438 }
439
440 fn percentage(n: u64, total: u64) -> u64 {
441     (n * 100).checked_div(total).unwrap_or(100)
442 }
443
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()
448 }